📄 addrtoname.c
字号:
} *cp = '\0'; tp->e_name = savestr(buf); return (tp->e_name);}char *etherproto_string(u_short port){ register char *cp; register struct hnamemem *tp; register u_int32_t i = port; char buf[sizeof("0000")]; for (tp = &eprototable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt) if (tp->addr == i) return (tp->name); tp->addr = i; tp->nxt = newhnamemem(); cp = buf; NTOHS(port); *cp++ = hex[port >> 12 & 0xf]; *cp++ = hex[port >> 8 & 0xf]; *cp++ = hex[port >> 4 & 0xf]; *cp++ = hex[port & 0xf]; *cp++ = '\0'; tp->name = savestr(buf); return (tp->name);}char *protoid_string(register const u_char *pi){ register u_int i, j; register char *cp; register struct protoidmem *tp; char buf[sizeof("00:00:00:00:00")]; tp = lookup_protoid(pi); if (tp->p_name) return tp->p_name; cp = buf; if ((j = *pi >> 4) != 0) *cp++ = hex[j]; *cp++ = hex[*pi++ & 0xf]; for (i = 4; (int)--i >= 0;) { *cp++ = ':'; if ((j = *pi >> 4) != 0) *cp++ = hex[j]; *cp++ = hex[*pi++ & 0xf]; } *cp = '\0'; tp->p_name = savestr(buf); return (tp->p_name);}char *llcsap_string(u_char sap){ register char *cp; register struct hnamemem *tp; register u_int32_t i = sap; char buf[sizeof("sap 00")]; for (tp = &llcsaptable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt) if (tp->addr == i) return (tp->name); tp->addr = i; tp->nxt = newhnamemem(); cp = buf; (void)strcpy(cp, "sap "); cp += strlen(cp); *cp++ = hex[sap >> 4 & 0xf]; *cp++ = hex[sap & 0xf]; *cp++ = '\0'; tp->name = savestr(buf); return (tp->name);}char *isonsap_string(const u_char *nsap){ register u_int i, nlen = nsap[0]; register char *cp; register struct enamemem *tp; tp = lookup_nsap(nsap); if (tp->e_name) return tp->e_name; tp->e_name = cp = (char *)malloc(nlen * 2 + 2); if (cp == NULL) error("isonsap_string: malloc"); nsap++; *cp++ = '/'; for (i = nlen; (int)--i >= 0;) { *cp++ = hex[*nsap >> 4]; *cp++ = hex[*nsap++ & 0xf]; } *cp = '\0'; return (tp->e_name);}char *tcpport_string(u_short port){ register struct hnamemem *tp; register u_int32_t i = port; char buf[sizeof("00000")]; for (tp = &tporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt) if (tp->addr == i) return (tp->name); tp->addr = i; tp->nxt = newhnamemem(); (void)sprintf(buf, "%u", i); tp->name = savestr(buf); return (tp->name);}char *udpport_string(register u_short port){ register struct hnamemem *tp; register u_int32_t i = port; char buf[sizeof("00000")]; for (tp = &uporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt) if (tp->addr == i) return (tp->name); tp->addr = i; tp->nxt = newhnamemem(); (void)sprintf(buf, "%u", i); tp->name = savestr(buf); return (tp->name);}static voidinit_servarray(void){ struct servent *sv; register struct hnamemem *table; register int i; char buf[sizeof("0000000000")]; while ((sv = getservent()) != NULL) { int port = ntohs(sv->s_port); i = port & (HASHNAMESIZE-1); if (strcmp(sv->s_proto, "tcp") == 0) table = &tporttable[i]; else if (strcmp(sv->s_proto, "udp") == 0) table = &uporttable[i]; else continue; while (table->name) table = table->nxt; if (nflag) { (void)sprintf(buf, "%d", port); table->name = savestr(buf); } else table->name = savestr(sv->s_name); table->addr = port; table->nxt = newhnamemem(); } endservent();}/*XXX from libbpfc.a */extern struct eproto { char *s; u_short p;} eproto_db[];static voidinit_eprotoarray(void){ register int i; register struct hnamemem *table; for (i = 0; eproto_db[i].s; i++) { int j = ntohs(eproto_db[i].p) & (HASHNAMESIZE-1); table = &eprototable[j]; while (table->name) table = table->nxt; table->name = eproto_db[i].s; table->addr = ntohs(eproto_db[i].p); table->nxt = newhnamemem(); }}/* * SNAP proto IDs with org code 0:0:0 are actually encapsulated Ethernet * types. */static voidinit_protoidarray(void){ register int i; register struct protoidmem *tp; u_char protoid[5]; protoid[0] = 0; protoid[1] = 0; protoid[2] = 0; for (i = 0; eproto_db[i].s; i++) { u_short etype = htons(eproto_db[i].p); memcpy((char *)&protoid[3], (char *)&etype, 2); tp = lookup_protoid(protoid); tp->p_name = savestr(eproto_db[i].s); }}static struct etherlist { u_char addr[6]; char *name;} etherlist[] = { {{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, "Broadcast" }, {{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL }};/* * Initialize the ethers hash table. We take two different approaches * depending on whether or not the system provides the ethers name * service. If it does, we just wire in a few names at startup, * and etheraddr_string() fills in the table on demand. If it doesn't, * then we suck in the entire /etc/ethers file at startup. The idea * is that parsing the local file will be fast, but spinning through * all the ethers entries via NIS & next_etherent might be very slow. * * XXX pcap_next_etherent doesn't belong in the pcap interface, but * since the pcap module already does name-to-address translation, * it's already does most of the work for the ethernet address-to-name * translation, so we just pcap_next_etherent as a convenience. */static voidinit_etherarray(void){ register struct etherlist *el; register struct enamemem *tp;#ifdef HAVE_ETHER_NTOHOST char name[256];#else register struct pcap_etherent *ep; register FILE *fp; /* Suck in entire ethers file */ fp = fopen(PCAP_ETHERS_FILE, "r"); if (fp != NULL) { while ((ep = pcap_next_etherent(fp)) != NULL) { tp = lookup_emem(ep->addr); tp->e_name = savestr(ep->name); } (void)fclose(fp); }#endif /* Hardwire some ethernet names */ for (el = etherlist; el->name != NULL; ++el) { tp = lookup_emem(el->addr); /* Don't override existing name */ if (tp->e_name != NULL) continue;#ifdef HAVE_ETHER_NTOHOST /* Use yp/nis version of name if available */ if (ether_ntohost(name, (struct ether_addr *)el->addr) == 0) { tp->e_name = savestr(name); continue; }#endif tp->e_name = el->name; }}static struct tok llcsap_db[] = { { LLCSAP_NULL, "null" }, { LLCSAP_8021B_I, "802.1b-gsap" }, { LLCSAP_8021B_G, "802.1b-isap" }, { LLCSAP_IP, "ip-sap" }, { LLCSAP_PROWAYNM, "proway-nm" }, { LLCSAP_8021D, "802.1d" }, { LLCSAP_RS511, "eia-rs511" }, { LLCSAP_ISO8208, "x.25/llc2" }, { LLCSAP_PROWAY, "proway" }, { LLCSAP_ISONS, "iso-clns" }, { LLCSAP_GLOBAL, "global" }, { 0, NULL }};static voidinit_llcsaparray(void){ register int i; register struct hnamemem *table; for (i = 0; llcsap_db[i].s != NULL; i++) { table = &llcsaptable[llcsap_db[i].v]; while (table->name) table = table->nxt; table->name = llcsap_db[i].s; table->addr = llcsap_db[i].v; table->nxt = newhnamemem(); }}/* * Initialize the address to name translation machinery. We map all * non-local IP addresses to numeric addresses if fflag is true (i.e., * to prevent blocking on the nameserver). localnet is the IP address * of the local network. mask is its subnet mask. */voidinit_addrtoname(u_int32_t localnet, u_int32_t mask){ netmask = mask; if (fflag) { f_localnet = localnet; f_netmask = mask; } if (nflag) /* * Simplest way to suppress names. */ return; init_etherarray(); init_servarray(); init_eprotoarray(); init_llcsaparray(); init_protoidarray();}char *dnaddr_string(u_short dnaddr){ register struct hnamemem *tp; for (tp = &dnaddrtable[dnaddr & (HASHNAMESIZE-1)]; tp->nxt != 0; tp = tp->nxt) if (tp->addr == dnaddr) return (tp->name); tp->addr = dnaddr; tp->nxt = newhnamemem(); if (nflag) tp->name = dnnum_string(dnaddr); else tp->name = dnname_string(dnaddr); return(tp->name);}/* Return a zero'ed hnamemem struct and cuts down on calloc() overhead */struct hnamemem *newhnamemem(void){ register struct hnamemem *p; static struct hnamemem *ptr = NULL; static u_int num = 0; if (num <= 0) { num = 64; ptr = (struct hnamemem *)calloc(num, sizeof (*ptr)); if (ptr == NULL) error("newhnamemem: calloc"); } --num; p = ptr++; return (p);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -