/*ARGSUSED*/ static int getbroadcastnets( struct in_addr *addrs, int sock, /* any valid socket will do */ char *buf) /* why allocate more when we can use existing... */ { #if 0 #ifdef SIOCGIFCONF struct ifconf ifc; struct ifreq ifreq, *ifr; struct sockaddr_in *sin; int n, i; ifc.ifc_len = UDPMSGSIZE; ifc.ifc_buf = buf; if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) { perror("broadcast: ioctl (get interface configuration)"); return (0); } ifr = ifc.ifc_req; for (i = 0, n = ifc.ifc_len/sizeof (struct ifreq); n > 0; n--, ifr++) { ifreq = *ifr; if (ioctl(sock, SIOCGIFFLAGS, &ifreq) < 0) { perror("broadcast: ioctl (get interface flags)"); continue; } if ((ifreq.ifr_flags & IFF_BROADCAST) && (ifreq.ifr_flags & IFF_UP) && ifr->ifr_addr.sa_family == AF_INET) { sin = (struct sockaddr_in *)&ifr->ifr_addr; #ifdef SIOCGIFBRDADDR /* 4.3BSD */ if (ioctl(sock, SIOCGIFBRDADDR, &ifreq) < 0) { addrs[i++] = inet_makeaddr(inet_netof (sin->sin_addr), INADDR_ANY); } else { addrs[i++] = ((struct sockaddr_in*) &ifreq.ifr_addr)->sin_addr; } #else /* 4.2 BSD */ addrs[i++] = inet_makeaddr(inet_netof (sin->sin_addr.s_addr), INADDR_ANY); #endif } } return (i); #else return 0; #endif #else return 0; #endif }
void start_auth(struct Client* client) { struct AuthRequest* auth = 0; assert(0 != client); if (conf_check_slines(client)) { sendheader(client, REPORT_USING_SLINE); SetSLined(client); release_auth_client(client); return; } auth = make_auth_request(client); assert(0 != auth); Debug((DEBUG_INFO, "Beginning auth request on client %p", client)); if (!feature_bool(FEAT_NODNS)) { if (LOOPBACK == inet_netof(cli_ip(client))) strcpy(cli_sockhost(client), cli_name(&me)); else { struct DNSQuery query; query.vptr = auth; query.callback = auth_dns_callback; if (IsUserPort(auth->client)) sendheader(client, REPORT_DO_DNS); cli_dns_reply(client) = gethost_byaddr((const char*) &(cli_ip(client)), &query); if (cli_dns_reply(client)) { ++(cli_dns_reply(client))->ref_count; ircd_strncpy(cli_sockhost(client), cli_dns_reply(client)->hp->h_name, HOSTLEN); if (IsUserPort(auth->client)) sendheader(client, REPORT_FIN_DNSC); Debug((DEBUG_LIST, "DNS entry for %p was cached", auth->client)); } else SetDNSPending(auth); } } if (start_auth_query(auth)) { Debug((DEBUG_LIST, "identd query for %p initiated successfully", auth->client)); link_auth_request(auth, &AuthPollList); } else if (IsDNSPending(auth)) { Debug((DEBUG_LIST, "identd query for %p not initiated successfully; " "waiting on DNS", auth->client)); link_auth_request(auth, &AuthIncompleteList); } else { Debug((DEBUG_LIST, "identd query for %p not initiated successfully; " "no DNS pending; releasing immediately", auth->client)); free_auth_request(auth); release_auth_client(client); } }
int main() { struct in_addr addr; addr.s_addr = 0x1f23ab02; printf("network address is: %x\n", addr.s_addr); printf("local network address is: %x\n", inet_lnaof(addr)); printf("network number is: %x\n", inet_netof(addr)); return 0; }
int main(int argc,char **argv) { int x; struct sockaddr_in adr_inet;/* AF_INET */ const char *addr[] = { "44.135.86.12", "127.0.0.1", "172.16.23.95", "192.168.9.1" }; unsigned long net, hst; for ( x=0; x<4; ++x ) { /* * Create a socket address: */ memset(&adr_inet,0,sizeof adr_inet); adr_inet.sin_family = AF_INET; adr_inet.sin_port = htons(9000); if ( !inet_aton(addr[x], &adr_inet.sin_addr) ) puts("bad address."); /* * Split address into Host & Net ID */ hst = inet_lnaof(adr_inet.sin_addr); net = inet_netof(adr_inet.sin_addr); printf("%14s : net=0x%08lX host=0x%08lX\n", inet_ntoa(adr_inet.sin_addr),net,hst); /* * Zero the address to prove later that * we can reconstruct this value : */ memset(&adr_inet,0,sizeof adr_inet); adr_inet.sin_family = AF_INET; adr_inet.sin_port = htons(9000); adr_inet.sin_addr = inet_makeaddr(net,hst); /* * Now display the reconstructed * address : */ printf("%14s : %s\n\n", "inet_makeaddr", inet_ntoa(adr_inet.sin_addr)); } return 0; }
/* * Construct an Internet address representation. * If the nflag has been supplied, give * numeric value, otherwise try for symbolic name. */ char * inetname(struct in_addr *inp) { char *cp; static char line[50]; struct hostent *hp; struct netent *np; static char domain[MAXHOSTNAMELEN]; static int first = 1; if (first && !nflag) { first = 0; if (gethostname(domain, sizeof(domain)) == 0 && (cp = strchr(domain, '.'))) (void) strlcpy(domain, cp + 1, sizeof domain); else domain[0] = '\0'; } cp = NULL; if (!nflag && inp->s_addr != INADDR_ANY) { int net = inet_netof(*inp); int lna = inet_lnaof(*inp); if (lna == INADDR_ANY) { np = getnetbyaddr(net, AF_INET); if (np) cp = np->n_name; } if (cp == NULL) { hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET); if (hp) { if ((cp = strchr(hp->h_name, '.')) && !strcmp(cp + 1, domain)) *cp = '\0'; cp = hp->h_name; } } } if (inp->s_addr == INADDR_ANY) snprintf(line, sizeof line, "*"); else if (cp) snprintf(line, sizeof line, "%s", cp); else { inp->s_addr = ntohl(inp->s_addr); #define C(x) ((x) & 0xff) snprintf(line, sizeof line, "%u.%u.%u.%u", C(inp->s_addr >> 24), C(inp->s_addr >> 16), C(inp->s_addr >> 8), C(inp->s_addr)); } return (line); }
int main() { char buffer[32]; int ret = 0; int host = 0; int network = 0; unsigned int address = 0; struct in_addr in; in.s_addr = 0; /*输入一个以.分隔的字符串形式的ip地址*/ printf("please input your ip address:"); fgets(buffer, 31, stdin); buffer[31] = '\0'; /*示例使用inet_aton()函数*/ if((ret = inet_aton(buffer, &in) == 0)) printf("inet_aton:\t invalid address\n"); else printf("inet_aton:\t0x%x\n", in.s_addr); /*示例使用inet_addr()函数*/ if((address = inet_addr(buffer)) == INADDR_NONE) printf("inet_addr:\t invalid address\n"); else printf("inet_addr:\t 0x%x\n", address); /*示例使用inet_network()函数*/ if((address = inet_network(buffer)) == -1) printf("inet_work:\t invalid address\n"); else printf("inet_work:\t 0x%x\n", address); /*示例使用inet_ntoa()函数*/ if(inet_ntoa(in) == NULL) printf("inet_ntoa:\t invalid address\n"); else printf("inet_ntoa:\t%s\n", inet_ntoa(in)); /*示例使用inet_lnaof()与inet_netof()函数*/ host = inet_lnaof(in); network = inet_netof(in); printf("inet_lnaof:\t 0x%x\n", host); printf("inet_netof:\t 0x%x\n", network); in = inet_makeaddr(network, host); printf("inet_makeaddr: 0x%x\n", in.s_addr); return 0; }
/* * Construct an Internet address representation. * If the nflag has been supplied, give * numeric value, otherwise try for symbolic name. */ static char * inetname(struct in_addr in) { register char *cp; static char line[50]; struct hostent *hp; struct netent *np; static char domain[MAXHOSTNAMELEN + 1]; static int first = 1; if (first && !nflag) { first = 0; if (gethostname(domain, MAXHOSTNAMELEN) == 0 && (cp = (char *) strchr(domain, '.'))) (void) strcpy(domain, cp + 1); else domain[0] = 0; } cp = 0; if (!nflag && in.s_addr != INADDR_ANY) { u_long net = inet_netof(in); u_long lna = inet_lnaof(in); if (lna == INADDR_ANY) { np = getnetbyaddr(net, AF_INET); if (np) cp = np->n_name; } if (cp == 0) { hp = gethostbyaddr((char *)&in, sizeof (in), AF_INET); if (hp) { if ((cp = (char *) strchr(hp->h_name, '.')) && !strcmp(cp + 1, domain)) *cp = 0; cp = (char *)hp->h_name; } } } if (in.s_addr == INADDR_ANY) strcpy(line, "*"); else if (cp) strcpy(line, cp); else { in.s_addr = ntohl(in.s_addr); #define C(x) (unsigned)((x) & 0xff) sprintf(line, "%u.%u.%u.%u", C(in.s_addr >> 24), C(in.s_addr >> 16), C(in.s_addr >> 8), C(in.s_addr)); } return (line); }
unsigned char part_evenAndOddIp(int dlt, const u_char *bytes) { unsigned char result; switch (dlt) { case DLT_EN10MB: if (ntohs( ((struct ether_header *)bytes)->ether_type ) == ETHERTYPE_IP) { bytes += ETHER_ADDR_LEN; result = (inet_lnaof(((struct ip *)bytes)->ip_src) + inet_netof(((struct ip *)bytes)->ip_src)) % 2; } else { result = 0; } break; default: result = 0; } return result; }
int main (int argc, char * argv []) { int i; struct in_addr adresse; unsigned long int reseau; unsigned long int locale; for (i = 1; i < argc; i ++) { fprintf (stdout, "inet_netof (%s) = ", argv [i]); if (inet_aton (argv [i], & adresse) == 0) { fprintf (stdout, "invalide \n"); continue; } reseau = inet_netof (adresse); locale = inet_lnaof (adresse); fprintf (stdout, "%08lX + %08lX\n", reseau, locale); } return (0); }
/* * Construct an Internet address representation. * If numeric_addr has been supplied, give * numeric value, otherwise try for symbolic name. */ char * inetname(struct in_addr *inp) { char *cp; static char line[MAXHOSTNAMELEN]; struct hostent *hp; struct netent *np; cp = 0; if (!numeric_addr && inp->s_addr != INADDR_ANY) { int net = inet_netof(*inp); int lna = inet_lnaof(*inp); if (lna == INADDR_ANY) { np = getnetbyaddr(net, AF_INET); if (np) cp = np->n_name; } if (cp == NULL) { hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET); if (hp) { cp = hp->h_name; trimdomain(cp, strlen(cp)); } } } if (inp->s_addr == INADDR_ANY) strcpy(line, "*"); else if (cp) { strlcpy(line, cp, sizeof(line)); } else { inp->s_addr = ntohl(inp->s_addr); #define C(x) ((u_int)((x) & 0xff)) snprintf(line, sizeof(line), "%u.%u.%u.%u", C(inp->s_addr >> 24), C(inp->s_addr >> 16), C(inp->s_addr >> 8), C(inp->s_addr)); } return (line); }
static int read_routes(void) { struct route_struct *route, *temp; char buffer[1023], iface[16], net_addr[64], gate_addr[64], mask_addr[64]; int n, iflags, refcnt, use, metric, mss, window; struct in_addr address; unsigned long int netmask; unsigned long int network; FILE *fp; if (first_route != NULL) { route = first_route; while (route != NULL) { temp = route->next; free(route); route = temp; } first_route = NULL; } if ((fp = fopen(PROC_IP_ROUTE_FILE, "r")) == NULL) { if (logging) syslog(LOG_ERR, "error cannot open %s\n", PROC_IP_ROUTE_FILE); return FALSE; } while (fgets(buffer, 1023, fp) != NULL) { n = sscanf(buffer, "%s %s %s %X %d %d %d %s %d %d\n", iface, net_addr, gate_addr, &iflags, &refcnt, &use, &metric, mask_addr, &mss, &window); if (n != 10) continue; address.s_addr = htonl(hex2intrev(net_addr)); netmask = mask2bits(hex2intrev(mask_addr)); network = inet_netof(address); if (network == 0 || network == 127) { if (debug && logging) syslog(LOG_DEBUG, "rejecting route to %s/%ld - should not be propogated\n", inet_ntoa(address), netmask); continue; } if (route_restrict) { if (inet_netof(address) != 44) { if (debug && logging) syslog(LOG_DEBUG, "rejecting route to %s/%ld - not ampr.org\n", inet_ntoa(address), netmask); continue; } } if ((route = malloc(sizeof(struct route_struct))) == NULL) { if (logging) syslog(LOG_ERR, "out of memory !\n"); return FALSE; } route->addr = address; route->bits = netmask; route->metric = metric; route->action = (iflags & RTF_DYNAMIC) ? ORIG_ROUTE : FIXED_ROUTE; route->next = first_route; first_route = route; } fclose(fp); return TRUE; }
/* * Construct an Internet address representation. * If the nflag has been supplied, give * numeric value, otherwise try for symbolic name. */ char * inetname(struct in_addr *inp) { char *cp; static char line[50]; struct hostent *hp; struct netent *np; static char domain[MAXHOSTNAMELEN]; static int first = 1; #if defined (WIN32) || defined (cygwin) char host_temp[] = "localhost"; #endif if (first && !nflag) { first = 0; if (gethostname(domain, sizeof(domain)) == 0 && (cp = strchr(domain, '.'))) (void) strlcpy(domain, cp + 1, sizeof domain); else domain[0] = '\0'; } cp = NULL; if (!nflag && inp->s_addr != INADDR_ANY) { int net = inet_netof(*inp); int lna = inet_lnaof(*inp); if (lna == INADDR_ANY) { np = getnetbyaddr(net, AF_INET); if (np) cp = np->n_name; } if (cp == NULL) { hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET); if (hp) { if ((cp = strchr(hp->h_name, '.')) && !strcmp(cp + 1, domain)) *cp = '\0'; #if defined (WIN32) || defined (cygwin) /* Windows insists on returning the computer name for 127.0.0.1 * even if the hosts file lists something else such as 'localhost'. * If we are trying to look up 127.0.0.1, just return 'localhost' */ if (!strcmp(inet_ntoa(*inp),"127.0.0.1")) cp = host_temp; else #endif cp = hp->h_name; } } } if (inp->s_addr == INADDR_ANY) snprintf(line, sizeof line, "*"); else if (cp) snprintf(line, sizeof line, "%s", cp); else { inp->s_addr = ntohl(inp->s_addr); #define C(x) ((x) & 0xff) snprintf(line, sizeof line, "%u.%u.%u.%u", C(inp->s_addr >> 24), C(inp->s_addr >> 16), C(inp->s_addr >> 8), C(inp->s_addr)); } return (line); }
void rip_input(struct sockaddr *sa, int size) { struct sockaddr_in *from = (struct sockaddr_in *)sa; register struct rip *msg = (struct rip *)packet; register struct netinfo *n; const char *name; int lna, net, subnet; struct hostent *hp; struct netent *np; if (msg->rip_cmd != RIPCMD_RESPONSE) return; printf("%d bytes from ", size); if (nflag) printf("%s:\n", inet_ntoa(from->sin_addr)); else { hp = gethostbyaddr((char *)&from->sin_addr, sizeof (struct in_addr), AF_INET); name = hp == 0 ? "???" : hp->h_name; printf("%s(%s):\n", name, inet_ntoa(from->sin_addr)); } size -= sizeof (int); n = msg->rip_nets; while (size > 0) { if (size < (int)sizeof (struct netinfo)) break; if (msg->rip_vers > 0) { n->rip_dst.sa_family = ntohs(n->rip_dst.sa_family); n->rip_metric = ntohl(n->rip_metric); } switch (n->rip_dst.sa_family) { case AF_INET: { register struct sockaddr_in *sin; sin = (struct sockaddr_in *)&n->rip_dst; net = inet_netof(sin->sin_addr); subnet = inet_subnetof(sin->sin_addr); lna = inet_lnaof(sin->sin_addr); name = "???"; if (!nflag) { if (sin->sin_addr.s_addr == 0) name = "default"; else if (lna == INADDR_ANY) { np = getnetbyaddr(net, AF_INET); if (np) name = np->n_name; else if (net == 0) name = "default"; } else if ((lna & 0xff) == 0 && (np = getnetbyaddr(subnet, AF_INET))) { struct in_addr subnaddr; subnaddr = inet_makeaddr(subnet, INADDR_ANY); if (memcmp(&sin->sin_addr, &subnaddr, sizeof(subnaddr)) == 0) name = np->n_name; else goto host; } else { host: hp = gethostbyaddr((char *)&sin->sin_addr, sizeof (struct in_addr), AF_INET); if (hp) name = hp->h_name; } printf("\t%-17s metric %2d name %s\n", inet_ntoa(sin->sin_addr), n->rip_metric, name); } else printf("\t%-17s metric %2d\n", inet_ntoa(sin->sin_addr), n->rip_metric); break; } default: { u_short *p = (u_short *)n->rip_dst.sa_data; printf("\t(af %d) %x %x %x %x %x %x %x, metric %d\n", p[0], p[1], p[2], p[3], p[4], p[5], p[6], n->rip_dst.sa_family, n->rip_metric); break; } } size -= sizeof (struct netinfo), n++; } }
/** @SYMTestCaseID SYSLIB-STDLIB-CT-1072 @SYMTestCaseDesc Tests for ARPA net functions @SYMTestPriority High @SYMTestActions Tests for all basic network functions @SYMTestExpectedResults Test must not fail @SYMREQ REQ0000 */ void testArpa() { char* cp; char* cp2; struct in_addr iaddr; unsigned long ul1, ul2; int err; int i; test_Next("ARPA/INET.H functions"); iaddr.s_addr=11; cp="16.33.50.67"; err=inet_aton(cp, &iaddr); test(err==1); test(iaddr.s_addr==htonl(0x10213243)); test(iaddr.s_addr==inet_addr(cp)); cp2=inet_ntoa(iaddr); test(strcmp(cp2,cp)==0); iaddr.s_addr=11; err=inet_aton("16.rubbish.67", &iaddr); test(err==0); for (i=0;i<N_ADDRESSES;i++) { iaddr=inet_makeaddr(addresses[i][0], addresses[i][1]); test(iaddr.s_addr==ntohl(addresses[i][2])); ul1=inet_netof(iaddr); ul2=inet_lnaof(iaddr); test(ul1==addresses[i][0]); test(ul2==addresses[i][1]); } }