static inline void netif_get_addrs_nix(struct netif_saddr_data *ifaddrs) { struct ifaddrs *ifaddr, *ifa; unsigned int family, s; char host[NI_MAXHOST]; if (getifaddrs(&ifaddr) == -1) { warn("getifaddrs() failed"); return; } for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { if (ifa->ifa_addr == NULL || is_loopback(ifa)) continue; family = ifa->ifa_addr->sa_family; if ((family == AF_INET) || (family == AF_INET6)) { s = getnameinfo(ifa->ifa_addr, (family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); if (s != 0) { warn("getnameinfo() failed: %s", gai_strerror(s)); continue; } netif_push(ifa->ifa_addr, ifaddrs, ifa->ifa_name); } } freeifaddrs(ifaddr); }
// ARP请求 void arp_request(u32 req_ip) { arp_frm *arp_rep; eth_frm *eth_arp; eth_arp = netif_alloc(ETH_HDRLEN + ARP_FRMLEN); if(eth_arp != NULL) { arp_rep = (arp_frm *)(eth_arp->data); arp_rep->hardware_type = htons(ARP_HWTYPE_ETH); arp_rep->protocol_type = htons(ARP_PTTYPE_IP); arp_rep->hw_addr_len = MAC_ADDR_LEN; arp_rep->pt_addr_len = IP_ADDR_LEN; arp_rep->operation = htons(ARP_TYPE_REQ); mem_copy(arp_rep->src_mac, local.mac, MAC_ADDR_LEN); arp_rep->src_ip.inw = local.ip.inw; mem_setbytes(arp_rep->dst_mac, 0x00, MAC_ADDR_LEN); arp_rep->dst_ip.inw = req_ip; mem_setbytes(eth_arp->dst, 0xFF, MAC_ADDR_LEN); mem_copy(eth_arp->src, local.mac, MAC_ADDR_LEN); eth_arp->type = htons(ETH_TYPE_ARP); netif_push(eth_arp, ETH_HDRLEN + ARP_FRMLEN, &local); } }
static inline void netif_get_addrs_win32(struct netif_saddr_data *ifaddrs) { PIP_ADAPTER_ADDRESSES adapter = get_adapters(); PIP_ADAPTER_UNICAST_ADDRESS unicast = NULL; PIP_ADAPTER_ADDRESSES cur_adap = NULL; SOCKET_ADDRESS socket_addr; int family; if (!adapter) return; for (cur_adap = adapter; !!cur_adap; cur_adap = cur_adap->Next) { char *adap_name = NULL; if (cur_adap->OperStatus != IfOperStatusUp || cur_adap->IfType == IF_TYPE_SOFTWARE_LOOPBACK) continue; os_wcs_to_utf8_ptr(cur_adap->FriendlyName, 0, &adap_name); unicast = cur_adap->FirstUnicastAddress; for (; !!unicast; unicast = unicast->Next) { socket_addr = unicast->Address; family = socket_addr.lpSockaddr->sa_family; if (family == AF_INET || family == AF_INET6) netif_push(socket_addr.lpSockaddr, ifaddrs, adap_name); } bfree(adap_name); } bfree(adapter); }