int main(void) { /* Test conversion of string to IPv4 address and back again. */ struct ipaddr addr; int rc = ipaddr_local(&addr, "1.2.3.4", 5555, 0); errno_assert(rc == 0); int family = ipaddr_family(&addr); assert(family == AF_INET); const struct sockaddr_in *sin = (const struct sockaddr_in*)ipaddr_sockaddr(&addr); uint32_t a = *(uint32_t*)(&sin->sin_addr); assert(ntohl(a) == 0x01020304); assert(ntohs(sin->sin_port) == 5555); int port = ipaddr_port(&addr); assert(port == 5555); char buf[IPADDR_MAXSTRLEN]; ipaddr_str(&addr, buf); assert(strcmp(buf, "1.2.3.4") == 0); /* Test conversion of string to IPv6 address and back again. */ rc = ipaddr_local(&addr, "::1", 5555, 0); errno_assert(rc == 0); family = ipaddr_family(&addr); assert(family == AF_INET6); const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6*)ipaddr_sockaddr(&addr); assert(ntohs(sin6->sin6_port) == 5555); port = ipaddr_port(&addr); assert(port == 5555); ipaddr_str(&addr, buf); assert(strcmp(buf, "::1") == 0); return 0; }
int sw_conn(SockAddr* sa) { int sockfd; struct sockaddr_in srvaddr; char str_sa[20]; struct hostent* s; int e; ipaddr_str(sa, str_sa); s = gethostbyname(str_sa); sockfd = socket(AF_INET, SOCK_STREAM, 0); bzero((void*)&srvaddr, sizeof(srvaddr)); bcopy((char*)(s->h_addr), (char*)&srvaddr.sin_addr.s_addr, s->h_length); //why s->h_length rather than sizoef? srvaddr.sin_family = AF_INET; srvaddr.sin_port = htons(sa->port); e = connect(sockfd, (struct sockaddr*)&srvaddr, sizeof(srvaddr)); if(e<0) { // printf("sockwrap error: %d\n", e); perror("sockwrap error"); //generate error!!! } return sockfd; //must be valid! }
int hostapd_ctrl_iface_sta_list(struct hostapd_data *hapd,char *buf, size_t buflen) { int len,res,ret; struct sta_info *sta = hapd->sta_list; len = 0; char *SSID = NULL; time_t now,online; struct hostap_sta_driver_data data; SSID = (char *)hapd->conf->ssid.ssid; ret = os_snprintf(buf+len,buflen-len,"\nSSID:%s " MACSTR "\n",SSID,MAC2STR(hapd->own_addr)); len += ret; ret = os_snprintf(buf+len,buflen-len,"======================================================================\n"); len += ret; ret = os_snprintf(buf+len,buflen-len,"STA_MAC IP OnlineTime RX TX \n"); len += ret; while(sta){ time(&now); online = now - sta->sta_add_time; if (hostapd_drv_read_sta_data(hapd, &data, sta->addr)) return -1; sta->last_rx_bytes = data.rx_bytes; sta->last_tx_bytes = data.tx_bytes; ret = os_snprintf(buf+len,buflen-len,MACSTR " %-17s %-10lu %-7lu %-7lu\n", MAC2STR(sta->addr),ipaddr_str(ntohl(sta->ipaddr)),online,sta->last_rx_bytes,sta->last_tx_bytes); len += ret; sta = sta->next; } ret = os_snprintf(buf+len,buflen-len,"======================================================================\n"); len += ret; return len; }
static void handle_dhcp(void *ctx, const u8 *src_addr, const u8 *buf, size_t len) { struct hostapd_data *hapd = ctx; const struct bootp_pkt *b; struct sta_info *sta; int exten_len; const u8 *end, *pos; int res, msgtype = 0, prefixlen = 32; u32 subnet_mask = 0; u16 tot_len; int dns_flag = 0; u32 dns1 = 0; u32 dns2 = 0; u8 *tmp; /* printPacketBuffer(buf, len); */ exten_len = len - ETH_HLEN - (sizeof(*b) - sizeof(b->exten)); if (exten_len < 4) return; b = (const struct bootp_pkt *) &buf[ETH_HLEN]; tot_len = ntohs(b->iph.tot_len); if (tot_len > (unsigned int) (len - ETH_HLEN)) return; if (os_memcmp(b->exten, ic_bootp_cookie, ARRAY_SIZE(ic_bootp_cookie))) return; printf("dhcp_snoop: Found DHCP IPv4 address %s/%d", ipaddr_str(ntohl(b->your_ip)), prefixlen); /* Parse DHCP options */ end = (const u8 *) b + tot_len; pos = &b->exten[4]; while (pos < end && *pos != 0xff) { const u8 *opt = pos++; if (*opt == 0) /* padding */ continue; pos += *pos + 1; if (pos >= end) break; switch (*opt) { case 1: /* subnet mask */ if (opt[1] == 4) subnet_mask = WPA_GET_BE32(&opt[2]); if (subnet_mask == 0) return; while (!(subnet_mask & 0x1)) { subnet_mask >>= 1; prefixlen--; } break; case 6: /* dns */ dns_flag = 1; if (opt[1] == 4) { dns1 = WPA_GET_BE32(&opt[2]); tmp = (u8 *)&dns1; wpa_printf(MSG_DEBUG, "%d.%d.%d.%d\n", tmp[0], tmp[1], tmp[2], tmp[3]); } if (opt[1] == 8) { dns1 = WPA_GET_BE32(&opt[2]); dns2 = WPA_GET_BE32(&opt[6]); tmp = (u8 *)&dns1; wpa_printf(MSG_DEBUG, "dns1 %d.%d.%d.%d\n", tmp[0], tmp[1], tmp[2], tmp[3]); tmp = (u8 *)&dns2; wpa_printf(MSG_DEBUG, "dns2 %d.%d.%d.%d\n", tmp[0], tmp[1], tmp[2], tmp[3]); } case 53: /* message type */ if (opt[1]) msgtype = opt[2]; break; default: break; } } if (msgtype == DHCPACK) { if (b->your_ip == 0) return; /* DHCPACK for DHCPREQUEST */ sta = ap_get_sta(hapd, b->hw_addr); if (!sta) return; wpa_printf(MSG_DEBUG, "dhcp_snoop: Found DHCPACK for " MACSTR " @ IPv4 address %s/%d", MAC2STR(sta->addr), ipaddr_str(ntohl(b->your_ip)), prefixlen); if (sta->ipaddr == b->your_ip) return; sta->ipaddr = b->your_ip; if (dns_flag) { sta->dns[0] = dns1; sta->dns[1] = dns2; } #if 0 /* temporarily close */ if (sta->ipaddr != 0) { wpa_printf(MSG_DEBUG, "dhcp_snoop: Removing IPv4 address %s from the ip neigh table", ipaddr_str(be_to_host32(sta->ipaddr))); hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr); } res = hostapd_drv_br_add_ip_neigh(hapd, 4, (u8 *) &b->your_ip, prefixlen, sta->addr); if (res) { wpa_printf(MSG_DEBUG, "dhcp_snoop: Adding ip neigh table failed: %d", res); return; } sta->ipaddr = b->your_ip; #endif send_msg_to_eag(hapd, sta, STA_ADD); } if (hapd->conf->disable_dgaf && is_broadcast_ether_addr(buf)) { for (sta = hapd->sta_list; sta; sta = sta->next) { if (!(sta->flags & WLAN_STA_AUTHORIZED)) continue; x_snoop_mcast_to_ucast_convert_send(hapd, sta, (u8 *) buf, len); } } }