annotate net.c @ 1692:5dacaef602a7 segacd

Merge from default
author Michael Pavone <pavone@retrodev.com>
date Sat, 05 Jan 2019 00:58:08 -0800
parents 4f94e0f90c83
children 78abbabfd58d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1514
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1 #include <sys/types.h>
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
2 #include <ifaddrs.h>
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
3 #include <netinet/in.h>
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
4 #include "net.h"
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
5
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
6 static uint8_t is_loopback(struct sockaddr_in *addr)
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
7 {
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
8 return (addr->sin_addr.s_addr & 0xFF) == 127;
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
9 }
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
10
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
11 static void format_address(uint8_t *dst, struct sockaddr_in *addr)
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
12 {
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
13 long ip = addr->sin_addr.s_addr;
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
14 dst[0] = ip;
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
15 dst[1] = ip >> 8;
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
16 dst[2] = ip >> 16;
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
17 dst[3] = ip >> 24;
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
18 }
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
19
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
20 uint8_t get_host_address(iface_info *out)
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
21 {
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
22 struct ifaddrs *entries, *current, *localhost;
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
23 if (getifaddrs(&entries)) {
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
24 return 0;
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
25 }
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
26
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
27 for (current = entries; current; current = current->ifa_next)
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
28 {
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
29 if (current->ifa_addr && current->ifa_addr->sa_family == AF_INET) {
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
30 struct sockaddr_in *addr = (struct sockaddr_in *)current->ifa_addr;
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
31 if (is_loopback(addr)) {
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
32 localhost = current;
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
33 } else {
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
34 break;
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
35 }
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
36 }
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
37 }
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
38 if (!current && localhost) {
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
39 current = localhost;
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
40 }
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
41 uint8_t ret = 0;
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
42 if (current) {
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
43 ret = 1;
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
44 format_address(out->ip, (struct sockaddr_in *)current->ifa_addr);
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
45 format_address(out->net_mask, (struct sockaddr_in *)current->ifa_netmask);
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
46 }
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
47 freeifaddrs(entries);
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
48 return ret;
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
49 }