Path: utzoo!censor!geac!torsqnt!lethe!yunexus!ists!helios.physics.utoronto.ca!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!apollo!beierl_c From: beierl_c@apollo.HP.COM (Christopher Beierl) Newsgroups: comp.sys.apollo Subject: Re: internet address Message-ID: <50115509.19050@apollo.HP.COM> Date: 27 Feb 91 17:07:00 GMT References: <9102261044.AA02546@apo.esiee.fr> Reply-To: beierl_c@apollo.HP.COM (Christopher Beierl) Organization: Hewlett-Packard Apollo Division - Chelmsford, MA Lines: 180 In article <9102261044.AA02546@apo.esiee.fr> bonnetf@apo.esiee.fr (bonnet-franck) writes: >Hi, > >Does somebody knows how to get the internet address of a machine >when I am working on it ( whithout looking in /etc/hosts ) ? How about /etc/ifconfig where is one of dr0, dr1, eth0, etc. Apollo Token Ring dr0,1 IEEE 802.3 (ETHERNET) eth0,1 IEEE 802.5 (IBM Token Ring) itr0,1 Serial Line (SLIP) sl0 -Chris Here's a little program which interprets the network address info supplied by ifconfig. I have found it useful for explaining and understanding network addresses, subnetting, etc. --------------------------- Cut Here ---------------------------------- /* ** Pipe the output of "/etc/ifconfig " into this program */ #include #define MAXLEN 256 main() { int net_shift, subnet_shift; char buf[MAXLEN], *ptr, class; unsigned a1, a2, a3, a4, n1, n2, n3, n4, s1, s2, s3, s4, b1, b2, b3, b4; unsigned net, subnet, host, address, local_address, net_mask, subnet_mask; void print_bin(), print_hex(); fgets(buf, MAXLEN, stdin); printf(buf); if (fgets(buf, MAXLEN, stdin)) { printf(buf); for (ptr = buf; *ptr; ++ptr) { if (*ptr == '.') { *ptr = ' '; } } if (sscanf(buf, "%*s%u%u%u%u%*s%x%*s%u%u%u%u", &a1, &a2, &a3, &a4, &subnet_mask, &b1, &b2, &b3, &b4) == 9) { address = (a1 << 24) + (a2 << 16) + (a3 << 8) + a4; if ((address & 0x80000000) == 0) { class = 'A'; net_mask = 0xff000000; net_shift = 24; } else if ((address & 0x40000000) == 0) { class = 'B'; net_mask = 0xffff0000; net_shift = 16; } else if ((address & 0x20000000) == 0) { class = 'C'; net_mask = 0xffffff00; net_shift = 8; } else { class = 'D'; net_mask = 0xfffffff; net_shift = 4; } n1 = (net_mask & 0xff000000) >> 24; n2 = (net_mask & 0x00ff0000) >> 16; n3 = (net_mask & 0x0000ff00) >> 8; n4 = net_mask & 0x000000ff; s1 = (subnet_mask & 0xff000000) >> 24; s2 = (subnet_mask & 0x00ff0000) >> 16; s3 = (subnet_mask & 0x0000ff00) >> 8; s4 = subnet_mask & 0x000000ff; for (subnet_shift = 0; subnet_shift < net_shift; ++subnet_shift) { if (subnet_mask & (1 << subnet_shift)) { break; } } printf("\nADDRESS\n"); printf("(decimal) = %6u . %6u . %6u . %6u\n", a1, a2, a3, a4); print_hex(a1, a2, a3, a4); print_bin(a1, a2, a3, a4); printf("\nBROADCAST ADDRESS\n"); printf("(decimal) = %6u . %6u . %6u . %6u\n", b1, b2, b3, b4); print_hex(b1, b2, b3, b4); print_bin(b1, b2, b3, b4); printf("\nNET MASK\n"); printf("(decimal) = %6u . %6u . %6u . %6u\n", n1, n2, n3, n4); print_hex(n1, n2, n3, n4); print_bin(n1, n2, n3, n4); printf("\nSUBNET MASK\n"); printf("(decimal) = %6u . %6u . %6u . %6u\n", s1, s2, s3, s4); print_hex(s1, s2, s3, s4); print_bin(s1, s2, s3, s4); net = (address & net_mask) >> net_shift; subnet = ((address & ~net_mask) & subnet_mask) >> subnet_shift; host = address & ~subnet_mask; printf("\nThis means:\n"); printf(" Class: %c\n", class); printf(" Net: %d (%x hex)\n", net, net); printf(" Subnet: %d (%x hex)\n", subnet, subnet); printf(" Host: %d (%x hex)\n", host, host); } } } void print_hex(v1, v2, v3, v4) unsigned v1, v2, v3, v4; { void p_hex(); printf(" (hex) = "); p_hex(v1); printf(" . "); p_hex(v2); printf(" . "); p_hex(v3); printf(" . "); p_hex(v4); putchar('\n'); } void p_hex(d) unsigned d; { printf("%3x %3x", (d & 0xf0) >> 4, d & 0xf); } void print_bin(v1, v2, v3, v4) unsigned v1, v2, v3, v4; { void p_bin(); printf(" (binary) = "); p_bin(v1, 8); printf(". "); p_bin(v2, 8); printf(". "); p_bin(v3, 8); printf(". "); p_bin(v4, 8); putchar('\n'); } void p_bin(d, bits) unsigned d; int bits; { for (--bits; bits >= 0; --bits) { putchar((d & (1 << bits)) ? '1' : '0'); if (!(bits % 4)) { putchar(' '); } } } --------------------------- Cut Here ---------------------------------- -- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= Christopher T. Beierl Internet: beierl_c@apollo.HP.COM;beierl_c@apollo.com Apollo Computer, Inc. UUCP: {mit-eddie,yale,uw-beaver}!apollo!beierl_c A Subsidiary of Hewlett-Packard Phone: (508) 256-6600