mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-07 19:58:30 +00:00
Add test for ioctl(SIOCGIFCONF) and polyfill on BSDs
- Use nullness checks when calling weakly linked functions. - Avoid typedef for reasons described in Linux Kernel style guide. - Avoid enum in in Windows headers. Earlier in Cosmo's history all one hundred files in libc/nt/enum/ used to be enums and it resulted in gigabytes of DWARF data almost as large as everything else in the codebase combined. - Bitfields aren't our friends. They have frequent ABI breakages, inconsistent arithmetic across compilers, and different endianness between cpus. Compiler authors also haven't invested much roi into making bit fields go fast so they produce poor assembly. - Use memccpy() instead of strncpy() or snprintf() for length-bounded copying of C strings. strncpy() is a misunderstood function and snprintf() is awesome but memccpy() deserves more love.
This commit is contained in:
parent
86ab24ce56
commit
5144c22189
41 changed files with 502 additions and 476 deletions
|
@ -83,6 +83,12 @@ o/$(MODE)/libc/calls/mkntenvblock.o: \
|
|||
OVERRIDE_CPPFLAGS += \
|
||||
-DSTACK_FRAME_UNLIMITED
|
||||
|
||||
o/$(MODE)/libc/calls/ioctl-siocgifconf.o \
|
||||
o/$(MODE)/libc/calls/ioctl-siocgifconf-nt.o: \
|
||||
OVERRIDE_COPTS += \
|
||||
-ffunction-sections \
|
||||
-fdata-sections
|
||||
|
||||
LIBC_CALLS_LIBS = $(foreach x,$(LIBC_CALLS_ARTIFACTS),$($(x)))
|
||||
LIBC_CALLS_SRCS = $(foreach x,$(LIBC_CALLS_ARTIFACTS),$($(x)_SRCS))
|
||||
LIBC_CALLS_HDRS = $(foreach x,$(LIBC_CALLS_ARTIFACTS),$($(x)_HDRS))
|
||||
|
|
|
@ -16,45 +16,45 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/assert.h"
|
||||
#include "libc/bits/weaken.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/internal.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "libc/sysv/consts/af.h"
|
||||
#include "libc/sock/sock.h"
|
||||
#include "libc/sock/internal.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
#include "libc/sysv/consts/iff.h"
|
||||
#include "libc/nt/winsock.h"
|
||||
#include "libc/nt/errors.h"
|
||||
#include "libc/nt/iphlpapi.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
#include "libc/nt/struct/ipadapteraddresses.h"
|
||||
#include "libc/bits/weaken.h"
|
||||
#include "libc/nt/winsock.h"
|
||||
#include "libc/sock/internal.h"
|
||||
#include "libc/sock/sock.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/assert.h"
|
||||
//#include "libc/nt/windows.h" /* Needed for WideCharToMultiByte */
|
||||
#include "libc/sysv/consts/af.h"
|
||||
#include "libc/sysv/consts/iff.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
|
||||
/* Maximum number of unicast addresses handled for each interface */
|
||||
#define MAX_UNICAST_ADDR 32
|
||||
#define MAX_NAME_CLASH ((int)('z'-'a')) /* Allow a..z */
|
||||
|
||||
static int insertAdapterName(NtIpAdapterAddresses *aa);
|
||||
#define MAX_UNICAST_ADDR 32
|
||||
#define MAX_NAME_CLASH ((int)('z' - 'a')) /* Allow a..z */
|
||||
|
||||
struct HostAdapterInfoNode {
|
||||
struct HostAdapterInfoNode * next;
|
||||
char name[IFNAMSIZ]; /* Obtained from FriendlyName */
|
||||
struct HostAdapterInfoNode *next;
|
||||
char name[IFNAMSIZ]; /* Obtained from FriendlyName */
|
||||
struct sockaddr unicast;
|
||||
struct sockaddr netmask;
|
||||
struct sockaddr broadcast;
|
||||
short flags;
|
||||
} *__hostInfo;
|
||||
} * __hostInfo;
|
||||
|
||||
/* Frees all the nodes of the _hostInfo */
|
||||
static void freeHostInfo() {
|
||||
static void freeHostInfo(void) {
|
||||
struct HostAdapterInfoNode *next, *node = __hostInfo;
|
||||
while(node) {
|
||||
next = node->next;
|
||||
weaken(free)(node);
|
||||
node = next;
|
||||
if (weaken(free)) {
|
||||
while (node) {
|
||||
next = node->next;
|
||||
weaken(free)(node);
|
||||
node = next;
|
||||
}
|
||||
}
|
||||
__hostInfo = NULL;
|
||||
}
|
||||
|
@ -65,14 +65,12 @@ static void freeHostInfo() {
|
|||
*/
|
||||
static struct HostAdapterInfoNode *findAdapterByName(const char *name) {
|
||||
struct HostAdapterInfoNode *node = __hostInfo;
|
||||
while(node) {
|
||||
while (node) {
|
||||
if (!strncmp(name, node->name, IFNAMSIZ)) {
|
||||
/* Found */
|
||||
return node;
|
||||
}
|
||||
node=node->next;
|
||||
node = node->next;
|
||||
}
|
||||
/* Not found */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@ -86,19 +84,22 @@ static struct HostAdapterInfoNode *findAdapterByName(const char *name) {
|
|||
*/
|
||||
struct HostAdapterInfoNode *appendHostInfo(
|
||||
struct HostAdapterInfoNode *parentInfoNode,
|
||||
const char *baseName, /* Max length = IFNAMSIZ-1 */
|
||||
const NtIpAdapterAddresses *aa, /* Top level adapter object being processed */
|
||||
NtIpAdapterUnicastAddress **ptrUA, /* Ptr to ptr to unicast address list node */
|
||||
NtIpAdapterPrefix **ptrAP, /* Ptr to ptr to Adapter prefix list node */
|
||||
int count) { /* count is used to create a unique name in case of alias */
|
||||
const char *baseName, /* Max length = IFNAMSIZ-1 */
|
||||
const struct NtIpAdapterAddresses
|
||||
*aa, /* Top level adapter object being processed */
|
||||
struct NtIpAdapterUnicastAddress *
|
||||
*ptrUA, /* Ptr to ptr to unicast address list node */
|
||||
struct NtIpAdapterPrefix *
|
||||
*ptrAP, /* Ptr to ptr to Adapter prefix list node */
|
||||
int count) { /* count is used to create a unique name in case of alias */
|
||||
|
||||
struct HostAdapterInfoNode *temp;
|
||||
struct HostAdapterInfoNode *node;
|
||||
struct sockaddr_in tempAddr;
|
||||
uint32_t ip, netmask, broadcast;
|
||||
struct sockaddr_in *a;
|
||||
int attemptNum;
|
||||
|
||||
node = weaken(calloc)(1, sizeof(*node));
|
||||
if (!node) {
|
||||
if (!weaken(calloc) || !(node = weaken(calloc)(1, sizeof(*node)))) {
|
||||
errno = ENOMEM;
|
||||
return NULL;
|
||||
}
|
||||
|
@ -109,37 +110,38 @@ struct HostAdapterInfoNode *appendHostInfo(
|
|||
if (count > 0 || ((*ptrUA)->Next != NULL)) {
|
||||
/* Yes, compose it using <baseName>:<count> */
|
||||
size_t nameLen = strlen(node->name);
|
||||
if (nameLen+2 > IFNAMSIZ-2) {
|
||||
if (nameLen + 2 > IFNAMSIZ - 2) {
|
||||
/* Appending the ":x" will exceed the size, need to chop the end */
|
||||
nameLen -= 2;
|
||||
}
|
||||
node->name[nameLen-2] = ':';
|
||||
node->name[nameLen-1] = '0'+count;
|
||||
node->name[nameLen - 2] = ':';
|
||||
node->name[nameLen - 1] = '0' + count;
|
||||
node->name[nameLen] = '\0';
|
||||
}
|
||||
|
||||
/* Is there a name clash with other interfaces? */
|
||||
for (attemptNum=0; attemptNum < MAX_NAME_CLASH; ++attemptNum) {
|
||||
for (attemptNum = 0; attemptNum < MAX_NAME_CLASH; ++attemptNum) {
|
||||
temp = findAdapterByName(node->name);
|
||||
if (!temp) {
|
||||
break;
|
||||
|
||||
} else {
|
||||
/* Yes, this name has been already used, append an extra
|
||||
/* Yes, this name has been already used, append an extra
|
||||
* character to resolve conflict. Note since the max length
|
||||
* of the string now is IFNAMSIZ-2, we have just enough space for this.
|
||||
* E.g. 'Ethernet_1' -> 'Ethernet_1a'
|
||||
*/
|
||||
|
||||
size_t pos = strlen(node->name);
|
||||
node->name[pos] = 'a' + attemptNum;
|
||||
node->name[pos+1] = '\0';
|
||||
node->name[pos + 1] = '\0';
|
||||
/* Try again */
|
||||
}
|
||||
}
|
||||
|
||||
if (attemptNum == MAX_NAME_CLASH) {
|
||||
/* Cannot resolve the conflict */
|
||||
weaken(free)(node);
|
||||
if (weaken(free)) {
|
||||
weaken(free)(node);
|
||||
}
|
||||
errno = EEXIST;
|
||||
return NULL;
|
||||
}
|
||||
|
@ -161,28 +163,47 @@ struct HostAdapterInfoNode *appendHostInfo(
|
|||
flags = 0;
|
||||
if (aa->OperStatus == kNtIfOperStatusUp) flags |= IFF_UP | IFF_RUNNING;
|
||||
if (aa->IfType == kNtIfTypePpp) flags |= IFF_POINTOPOINT;
|
||||
//if (aa->TunnelType != TUNNEL_TYPE_NONE) flags |= IFF_POINTOPOINT;
|
||||
if (aa->NoMulticast == 0) flags |= IFF_MULTICAST;
|
||||
if (!(aa->Flags & kNtIpAdapterNoMulticast)) flags |= IFF_MULTICAST;
|
||||
if (aa->IfType == kNtIfTypeSoftwareLoopback) flags |= IFF_LOOPBACK;
|
||||
if (aa->FirstPrefix != NULL) flags |= IFF_BROADCAST;
|
||||
if (aa->FirstPrefix) flags |= IFF_BROADCAST;
|
||||
node->flags = flags;
|
||||
} else {
|
||||
/* Copy from previous node */
|
||||
node->flags = parentInfoNode->flags;
|
||||
}
|
||||
|
||||
ip = ntohl(
|
||||
((struct sockaddr_in *)(*ptrUA)->Address.lpSockaddr)->sin_addr.s_addr);
|
||||
netmask = (uint32_t)-1 << (32 - (*ptrUA)->OnLinkPrefixLength);
|
||||
broadcast = (ip & netmask) | (~netmask & -1);
|
||||
|
||||
a = (struct sockaddr_in *)&node->netmask;
|
||||
a->sin_family = AF_INET;
|
||||
a->sin_addr.s_addr = htonl(netmask);
|
||||
|
||||
a = (struct sockaddr_in *)&node->broadcast;
|
||||
a->sin_family = AF_INET;
|
||||
a->sin_addr.s_addr = htonl(broadcast);
|
||||
|
||||
/* Process the prefix and extract the netmask and broadcast */
|
||||
/* According to the doc:
|
||||
* ... On Windows Vista and later, the linked IP_ADAPTER_PREFIX structures pointed to
|
||||
* by the FirstPrefix member include three IP adapter prefixes for each IP address
|
||||
* assigned to the adapter. These include the host IP address prefix, the subnet IP
|
||||
* address prefix, and the subnet broadcast IP address prefix. In addition, for each
|
||||
* adapter there is a multicast address prefix and a broadcast address prefix.
|
||||
*
|
||||
* On Windows Vista and later, the linked IP_ADAPTER_PREFIX
|
||||
* structures pointed to by the FirstPrefix member include three
|
||||
* IP adapter prefixes for each IP address assigned to the
|
||||
* adapter. These include the host IP address prefix, the subnet
|
||||
* IP address prefix, and the subnet broadcast IP address prefix.
|
||||
* In addition, for each adapter there is a multicast address
|
||||
* prefix and a broadcast address prefix.
|
||||
* -Source: MSDN on IP_ADAPTER_ADDRESSES_LH
|
||||
*
|
||||
* For example, interface "Ethernet", with 2 unicast addresses:
|
||||
* - 192.168.1.84
|
||||
*
|
||||
* - 192.168.1.84
|
||||
* - 192.168.5.99
|
||||
*
|
||||
* The Prefix list has 8 elements:
|
||||
*
|
||||
* #1: 192.168.1.0/24 <- Network, use the PrefixLength for netmask
|
||||
* #2: 192.168.1.84/32 <- Host IP
|
||||
* #3: 192.168.1.255/32 <- Subnet broadcast
|
||||
|
@ -195,20 +216,16 @@ struct HostAdapterInfoNode *appendHostInfo(
|
|||
* #8: 255.255.255.255/32 <- Broadcast
|
||||
*/
|
||||
|
||||
/* Netmask */
|
||||
memset(&tempAddr, 0, sizeof(tempAddr));
|
||||
tempAddr.sin_family = AF_INET;
|
||||
tempAddr.sin_addr.s_addr = (uint32_t)((1LLU << (*ptrAP)->PrefixLength) - 1LLU);
|
||||
memcpy(&node->netmask, &tempAddr, sizeof(tempAddr));
|
||||
if (ptrAP && *ptrAP) {
|
||||
*ptrAP = (*ptrAP)->Next; /* skip net ip */
|
||||
if (*ptrAP) {
|
||||
*ptrAP = (*ptrAP)->Next; /* skip host ip */
|
||||
if (*ptrAP) {
|
||||
node->broadcast = *((*ptrAP)->Address.lpSockaddr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*ptrAP = (*ptrAP)->Next;
|
||||
*ptrAP = (*ptrAP)->Next; /* Skip over Host IP */
|
||||
|
||||
/* Broadcast */
|
||||
node->broadcast = *((*ptrAP)->Address.lpSockaddr);
|
||||
*ptrAP = (*ptrAP)->Next;
|
||||
|
||||
/* Move pointer to Unicast Address record */
|
||||
*ptrUA = (*ptrUA)->Next;
|
||||
|
||||
/* Append this node to the last node (if any) */
|
||||
|
@ -221,10 +238,10 @@ struct HostAdapterInfoNode *appendHostInfo(
|
|||
}
|
||||
|
||||
/* Returns -1 in case of failure */
|
||||
static int createHostInfo(NtIpAdapterAddresses *firstAdapter) {
|
||||
NtIpAdapterAddresses *aa;
|
||||
NtIpAdapterUnicastAddress *ua;
|
||||
NtIpAdapterPrefix *ap;
|
||||
static int createHostInfo(struct NtIpAdapterAddresses *firstAdapter) {
|
||||
struct NtIpAdapterAddresses *aa;
|
||||
struct NtIpAdapterUnicastAddress *ua;
|
||||
struct NtIpAdapterPrefix *ap;
|
||||
struct HostAdapterInfoNode *node = NULL;
|
||||
char baseName[IFNAMSIZ];
|
||||
char name[IFNAMSIZ];
|
||||
|
@ -232,34 +249,31 @@ static int createHostInfo(NtIpAdapterAddresses *firstAdapter) {
|
|||
|
||||
/* __hostInfo must be empty */
|
||||
assert(__hostInfo == NULL);
|
||||
assert(weaken(tprecode16to8));
|
||||
|
||||
for (aa = firstAdapter; aa; aa = aa->Next) {
|
||||
/* Skip all the interfaces with no address and the ones that are not AF_INET */
|
||||
if (!aa->FirstUnicastAddress ||
|
||||
/* Skip all the interfaces with no address and the ones that are not AF_INET
|
||||
*/
|
||||
if (!aa->FirstUnicastAddress ||
|
||||
aa->FirstUnicastAddress->Address.lpSockaddr->sa_family != AF_INET) {
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Use max IFNAMSIZ-1 chars, leave the last char for eventual conficts */
|
||||
tprecode16to8(baseName, IFNAMSIZ-1, aa->FriendlyName);
|
||||
baseName[IFNAMSIZ-2] = '\0';
|
||||
tprecode16to8(baseName, IFNAMSIZ - 1, aa->FriendlyName);
|
||||
baseName[IFNAMSIZ - 2] = '\0';
|
||||
/* Replace any space with a '_' */
|
||||
for (i = 0; i < IFNAMSIZ-2; ++i) {
|
||||
for (i = 0; i < IFNAMSIZ - 2; ++i) {
|
||||
if (baseName[i] == ' ') baseName[i] = '_';
|
||||
if (!baseName[i]) break;
|
||||
}
|
||||
for (count = 0, ua = aa->FirstUnicastAddress, ap = aa->FirstPrefix;
|
||||
(ua != NULL) && (count < MAX_UNICAST_ADDR);
|
||||
++count) {
|
||||
for (count = 0, ua = aa->FirstUnicastAddress, ap = aa->FirstPrefix;
|
||||
(ua != NULL) && (count < MAX_UNICAST_ADDR); ++count) {
|
||||
node = appendHostInfo(node, baseName, aa, &ua, &ap, count);
|
||||
if (!node) {
|
||||
goto err;
|
||||
}
|
||||
if (!node) goto err;
|
||||
if (!__hostInfo) __hostInfo = node;
|
||||
}
|
||||
|
||||
/* Note: do we need to process the remaining adapter prefix?
|
||||
/* Note: do we need to process the remaining adapter prefix?
|
||||
* ap - points to broadcast addr
|
||||
* ap->Next - points to interface multicast addr
|
||||
* Ignoring them for now
|
||||
|
@ -268,55 +282,57 @@ static int createHostInfo(NtIpAdapterAddresses *firstAdapter) {
|
|||
return 0;
|
||||
|
||||
err:
|
||||
freeHostInfo(__hostInfo);
|
||||
freeHostInfo();
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int readAdapterAddresses(void) {
|
||||
uint32_t size, rc;
|
||||
NtIpAdapterAddresses * aa = NULL;
|
||||
|
||||
assert(weaken(GetAdaptersAddresses));
|
||||
struct NtIpAdapterAddresses *aa = NULL;
|
||||
|
||||
/* Calculate the required data size
|
||||
* Note: alternatively you can use AF_UNSPEC to also return IPv6 interfaces
|
||||
* Note: alternatively you can use AF_UNSPEC to also return IPv6 interfaces
|
||||
*/
|
||||
rc = weaken(GetAdaptersAddresses)(AF_INET,
|
||||
kNtGaaFlagSkipAnycast | kNtGaaFlagSkipMulticast | kNtGaaFlagSkipDnsServer | kNtGaaFlagIncludePrefix,
|
||||
NULL, /* Reserved */
|
||||
NULL, /* Ptr */
|
||||
&size);
|
||||
rc = GetAdaptersAddresses(AF_INET,
|
||||
kNtGaaFlagSkipAnycast | kNtGaaFlagSkipMulticast |
|
||||
kNtGaaFlagSkipDnsServer |
|
||||
kNtGaaFlagIncludePrefix,
|
||||
NULL, /* Reserved */
|
||||
NULL, /* Ptr */
|
||||
&size);
|
||||
if (rc != kNtErrorBufferOverflow) {
|
||||
ebadf();
|
||||
goto err;
|
||||
}
|
||||
|
||||
aa = (NtIpAdapterAddresses *)weaken(malloc)(size);
|
||||
if (!aa) {
|
||||
if (!weaken(malloc) ||
|
||||
!(aa = (struct NtIpAdapterAddresses *)weaken(malloc)(size))) {
|
||||
enomem();
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* Re-run GetAdaptersAddresses this time with a valid buffer */
|
||||
rc = weaken(GetAdaptersAddresses)(AF_INET,
|
||||
kNtGaaFlagSkipAnycast | kNtGaaFlagSkipMulticast | kNtGaaFlagSkipDnsServer | kNtGaaFlagIncludePrefix,
|
||||
//kNtGaaFlagIncludePrefix,
|
||||
NULL,
|
||||
aa,
|
||||
&size);
|
||||
rc = GetAdaptersAddresses(AF_INET,
|
||||
kNtGaaFlagSkipAnycast | kNtGaaFlagSkipMulticast |
|
||||
kNtGaaFlagSkipDnsServer |
|
||||
kNtGaaFlagIncludePrefix,
|
||||
// kNtGaaFlagIncludePrefix,
|
||||
NULL, aa, &size);
|
||||
if (rc != kNtErrorSuccess) {
|
||||
efault();
|
||||
errno = GetLastError();
|
||||
goto err;
|
||||
}
|
||||
if (createHostInfo(aa) == -1) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
weaken(free)(aa);
|
||||
if (weaken(free)) {
|
||||
weaken(free)(aa);
|
||||
}
|
||||
return 0;
|
||||
|
||||
err:
|
||||
if (aa) {
|
||||
if (weaken(free)) {
|
||||
weaken(free)(aa);
|
||||
}
|
||||
freeHostInfo();
|
||||
|
@ -324,7 +340,7 @@ err:
|
|||
}
|
||||
|
||||
textwindows int ioctl_siocgifconf_nt(int fd, struct ifconf *ifc) {
|
||||
NtIpAdapterAddresses *aa;
|
||||
struct NtIpAdapterAddresses *aa;
|
||||
struct HostAdapterInfoNode *node;
|
||||
struct ifreq *ptr;
|
||||
|
||||
|
@ -337,8 +353,8 @@ textwindows int ioctl_siocgifconf_nt(int fd, struct ifconf *ifc) {
|
|||
}
|
||||
|
||||
for (ptr = ifc->ifc_req, node = __hostInfo;
|
||||
(((char *)(ptr+1) - ifc->ifc_buf) < ifc->ifc_len) && node;
|
||||
ptr++, node = node->next) {
|
||||
(((char *)(ptr + 1) - ifc->ifc_buf) < ifc->ifc_len) && node;
|
||||
ptr++, node = node->next) {
|
||||
memcpy(ptr->ifr_name, node->name, IFNAMSIZ);
|
||||
memcpy(&ptr->ifr_addr, &node->unicast, sizeof(struct sockaddr));
|
||||
}
|
||||
|
@ -347,14 +363,13 @@ textwindows int ioctl_siocgifconf_nt(int fd, struct ifconf *ifc) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
/* Performs the SIOCGIFADDR operation */
|
||||
/**
|
||||
* Returns unicast addresses.
|
||||
*/
|
||||
int ioctl_siocgifaddr_nt(int fd, struct ifreq *ifr) {
|
||||
struct HostAdapterInfoNode *node;
|
||||
|
||||
node = findAdapterByName(ifr->ifr_name);
|
||||
if (!node) {
|
||||
return ebadf();
|
||||
}
|
||||
if (!node) return ebadf();
|
||||
memcpy(&ifr->ifr_addr, &node->unicast, sizeof(struct sockaddr));
|
||||
return 0;
|
||||
}
|
||||
|
@ -362,37 +377,28 @@ int ioctl_siocgifaddr_nt(int fd, struct ifreq *ifr) {
|
|||
/* Performs the SIOCGIFFLAGS operation */
|
||||
int ioctl_siocgifflags_nt(int fd, struct ifreq *ifr) {
|
||||
struct HostAdapterInfoNode *node;
|
||||
|
||||
node = findAdapterByName(ifr->ifr_name);
|
||||
if (!node) {
|
||||
return ebadf();
|
||||
}
|
||||
if (!node) return ebadf();
|
||||
ifr->ifr_flags = node->flags;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* Performs the SIOCGIFNETMASK operation */
|
||||
int ioctl_siocgifnetmask_nt(int fd, struct ifreq *ifr) {
|
||||
struct HostAdapterInfoNode *node;
|
||||
|
||||
node = findAdapterByName(ifr->ifr_name);
|
||||
if (!node) {
|
||||
return ebadf();
|
||||
}
|
||||
if (!node) return ebadf();
|
||||
memcpy(&ifr->ifr_netmask, &node->netmask, sizeof(struct sockaddr));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Performs the SIOCGIFBRDADDR operation */
|
||||
/**
|
||||
* Returns broadcast address.
|
||||
*/
|
||||
int ioctl_siocgifbrdaddr_nt(int fd, struct ifreq *ifr) {
|
||||
struct HostAdapterInfoNode *node;
|
||||
|
||||
node = findAdapterByName(ifr->ifr_name);
|
||||
if (!node) {
|
||||
return ebadf();
|
||||
}
|
||||
if (!node) return ebadf();
|
||||
memcpy(&ifr->ifr_broadaddr, &node->broadcast, sizeof(struct sockaddr));
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -16,12 +16,15 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/assert.h"
|
||||
#include "libc/bits/weaken.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
#include "libc/calls/internal.h"
|
||||
#include "libc/sock/sock.h"
|
||||
#include "libc/sock/internal.h"
|
||||
#include "libc/sock/sock.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/af.h"
|
||||
#include "libc/sysv/consts/sio.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
|
||||
/* SIOCGIFCONF:
|
||||
* Takes an struct ifconf object of a given size
|
||||
|
@ -29,7 +32,7 @@
|
|||
* - ifc_len: set it to the number of valid ifreq structures representing
|
||||
* the interfaces
|
||||
* - ifc_ifcu.ifcu_req: sets the name of the interface for each interface
|
||||
* The ifc_len is an input/output parameter: set it to the total size of
|
||||
* The ifc_len is an input/output parameter: set it to the total size of
|
||||
* the ifcu_buf (ifcu_req) buffer on input.
|
||||
*/
|
||||
int ioctl_default(int, uint64_t, void *) hidden;
|
||||
|
@ -40,79 +43,59 @@ int ioctl_siocgifnetmask_nt(int, struct ifreq *) hidden;
|
|||
int ioctl_siocgifbrdaddr_nt(int, struct ifreq *) hidden;
|
||||
|
||||
static int ioctl_siocgifconf_sysv(int fd, struct ifconf *ifc) {
|
||||
if (IsBsd()) {
|
||||
/* BSD uses a slightly different memory structure where:
|
||||
* - sizeof(struct ifreq) is 32 bytes instead of 40 bytes
|
||||
* - ifc.ifc_len is uint32_t instead of uint64_t
|
||||
* - struct ifconf is #pragma pack(4) (instead of the default pack(8))
|
||||
*/
|
||||
int i;
|
||||
char *buf; /* Temporary buffer to store ifreq */
|
||||
char *pBsdReq; /* Scan through buf records */
|
||||
char *pBsdEnd; /* End of the ifreq */
|
||||
|
||||
struct ifconf_bsd ifc_bsd;
|
||||
struct ifreq_bsd *bsdReq;
|
||||
struct ifreq *req;
|
||||
size_t numReq = ifc->ifc_len / sizeof(struct ifreq);
|
||||
|
||||
if (!weaken(malloc)) {
|
||||
return enomem();
|
||||
}
|
||||
ifc_bsd.ifc_len = numReq * sizeof(struct ifreq_bsd);
|
||||
buf = weaken(malloc)(ifc_bsd.ifc_len);
|
||||
if (!buf) {
|
||||
return enomem();
|
||||
}
|
||||
ifc_bsd.ifc_buf = buf;
|
||||
|
||||
if ((i = sys_ioctl(fd, SIOCGIFCONF, &ifc_bsd)) < 0) {
|
||||
weaken(free)(buf);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* On BSD the size of the struct ifreq is different than Linux.
|
||||
* On Linux is fixed (40 bytes), but on BSD the struct sockaddr
|
||||
/*
|
||||
* We're 100% compatible with Linux.
|
||||
* BSD ABIs mainly differ by having sockaddr::sa_len
|
||||
* XNU uses a 32-bit length in a struct that's packed!
|
||||
*/
|
||||
int i, rc, fam;
|
||||
char *b, *p, *e;
|
||||
char ifcBsd[16];
|
||||
struct ifreq *req;
|
||||
struct ifreq *end;
|
||||
uint32_t bufLen, ip;
|
||||
size_t numReq, bufMax;
|
||||
if (IsLinux()) return sys_ioctl(fd, SIOCGIFCONF, ifc);
|
||||
if (!weaken(malloc)) return enomem();
|
||||
bufMax = 15000; /* conservative guesstimate */
|
||||
if (!(b = weaken(malloc)(bufMax))) return enomem();
|
||||
memcpy(ifcBsd, &bufMax, 8); /* ifc_len */
|
||||
memcpy(ifcBsd + (IsXnu() ? 4 : 8), &b, 8); /* ifc_buf */
|
||||
if ((rc = sys_ioctl(fd, SIOCGIFCONF, &ifcBsd)) != -1) {
|
||||
/*
|
||||
* On XNU the size of the struct ifreq is different than Linux.
|
||||
* On Linux is fixed (40 bytes), but on XNU the struct sockaddr
|
||||
* has variable length, making the whole struct ifreq a variable
|
||||
* sized record.
|
||||
*/
|
||||
for (pBsdReq = buf, pBsdEnd = buf + ifc_bsd.ifc_len, req = ifc->ifc_req;
|
||||
pBsdReq < pBsdEnd;
|
||||
++req) {
|
||||
bsdReq = (struct ifreq_bsd *)pBsdReq;
|
||||
memcpy(req->ifr_name, bsdReq->ifr_name, IFNAMSIZ);
|
||||
memcpy(&req->ifr_addr, &bsdReq->ifr_addr, sizeof(struct sockaddr_bsd));
|
||||
sockaddr2linux(&req->ifr_addr);
|
||||
|
||||
pBsdReq += IFNAMSIZ + bsdReq->ifr_addr.sa_len;
|
||||
memcpy(&bufLen, b, 4);
|
||||
req = ifc->ifc_req;
|
||||
end = req + ifc->ifc_len / sizeof(*req);
|
||||
for (p = b, e = p + MIN(bufMax, READ32LE(ifcBsd)); p + 16 + 16 <= e;
|
||||
p += IsBsd() ? 16 + MAX(16, p[16] & 255) : 40) {
|
||||
fam = p[IsBsd() ? 17 : 16] & 255;
|
||||
if (fam != AF_INET) continue;
|
||||
ip = READ32BE(p + 20);
|
||||
memset(req, 0, sizeof(*req));
|
||||
memcpy(req->ifr_name, p, 16);
|
||||
memcpy(&req->ifr_addr, p + 16, 16);
|
||||
req->ifr_addr.sa_family = fam;
|
||||
((struct sockaddr_in *)&req->ifr_addr)->sin_addr.s_addr = htonl(ip);
|
||||
++req;
|
||||
}
|
||||
|
||||
/* Adjust len */
|
||||
ifc->ifc_len = (size_t)((char *)req - ifc->ifc_buf);
|
||||
weaken(free)(buf);
|
||||
return 0;
|
||||
|
||||
} else {
|
||||
/* 100% compatible with Linux */
|
||||
return sys_ioctl(fd, SIOCGIFCONF, ifc);
|
||||
ifc->ifc_len = (char *)req - ifc->ifc_buf; /* Adjust len */
|
||||
}
|
||||
if (weaken(free)) weaken(free)(b);
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* Used for all the ioctl that returns sockaddr structure that
|
||||
* requires adjustment between Linux and BSD
|
||||
* requires adjustment between Linux and XNU
|
||||
*/
|
||||
static int ioctl_siocgifaddr_sysv(int fd, uint64_t op, struct ifreq *ifr) {
|
||||
int i;
|
||||
|
||||
if (IsBsd()) {
|
||||
sockaddr2bsd(&ifr->ifr_addr);
|
||||
}
|
||||
if ((i = sys_ioctl(fd, op, ifr)) < 0) {
|
||||
return -1;
|
||||
}
|
||||
if (IsBsd()) {
|
||||
sockaddr2linux(&ifr->ifr_addr);
|
||||
}
|
||||
if (IsBsd()) sockaddr2bsd(&ifr->ifr_addr);
|
||||
if (sys_ioctl(fd, op, ifr) == -1) return -1;
|
||||
if (IsBsd()) sockaddr2linux(&ifr->ifr_addr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -163,14 +146,13 @@ int ioctl_siocgifdstaddr(int fd, void *ifr) {
|
|||
* GetAdaptersAddresses function
|
||||
*
|
||||
return ioctl_siocgifbrdaddr_nt(fd, ifc);
|
||||
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
int ioctl_siocgifflags(int fd, void *ifr) {
|
||||
if (!IsWindows()) {
|
||||
/* Both BSD and Linux are for once compatible here... */
|
||||
/* Both XNU and Linux are for once compatible here... */
|
||||
return ioctl_default(fd, SIOCGIFFLAGS, ifr);
|
||||
} else {
|
||||
return ioctl_siocgifflags_nt(fd, (struct ifreq *)ifr);
|
22
libc/calls/struct/sockaddr6.h
Normal file
22
libc/calls/struct/sockaddr6.h
Normal file
|
@ -0,0 +1,22 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_CALLS_STRUCT_SOCKADDR6_H_
|
||||
#define COSMOPOLITAN_LIBC_CALLS_STRUCT_SOCKADDR6_H_
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
|
||||
struct in6_addr {
|
||||
union {
|
||||
uint8_t s6_addr[16];
|
||||
uint16_t s6_addr16[8];
|
||||
uint32_t s6_addr32[4];
|
||||
};
|
||||
};
|
||||
|
||||
struct sockaddr_in6 { /* Linux+NT ABI */
|
||||
uint16_t sin6_family;
|
||||
uint16_t sin6_port;
|
||||
uint32_t sin6_flowinfo;
|
||||
struct in6_addr sin6_addr;
|
||||
uint32_t sin6_scope_id; /* rfc2553 */
|
||||
};
|
||||
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
#endif /* COSMOPOLITAN_LIBC_CALLS_STRUCT_SOCKADDR6_H_ */
|
Loading…
Add table
Add a link
Reference in a new issue