⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 a2name.c

📁 This directory contains source code for tcpdump, a tool for network monitoring and data acquisition
💻 C
📖 第 1 页 / 共 2 页
字号:
  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 (const u_char * pi)
{
  struct protoidmem *tp;
  char  *cp, buf [sizeof("00:00:00:00:00")];
  u_int  i, j;

  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)
{
  struct hnamemem *tp;
  char   buf [sizeof("sap 00")];
  u_int  i = sap;

  for (tp = &llcsaptable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
      if (tp->addr == i)
         return (tp->name);

  tp->addr = i;
  tp->nxt  = newhnamemem();

  snprintf (buf, sizeof(buf), "sap %02x", sap & 0xff);
  tp->name = savestr (buf);
  return (tp->name);
}

char *isonsap_string (const u_char *nsap)
{
  struct enamemem *tp = lookup_nsap (nsap);
  char  *cp;
  u_int  i, nlen = nsap[0];

  if (tp->e_name)
     return (tp->e_name);

  tp->e_name = cp = malloc (nlen * 2 + 2);
  if (!cp)
  {
    PERROR (("isonsap_string: malloc"));
    /* not reached */
  }
  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)
{
  struct hnamemem *tp;
  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();
  snprintf (buf, sizeof(buf), "%lu", i);
  tp->name = savestr (buf);
  return (tp->name);
}

char *udpport_string (u_short port)
{
  struct hnamemem *tp;
  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();
  snprintf (buf, sizeof(buf), "%lu", i);
  tp->name = savestr (buf);
  return (tp->name);
}

static void init_servarray (void)
{
  struct servent  *sv;
  struct hnamemem *table;
  int    i;
  u_long num_add = 0;
  char   buf [sizeof("0000000000")];

  setservent (1);  /* rewind the services file */

  while ((sv = getservent()) != NULL)
  {
    int port = ntohs (sv->s_port);

    if (++num_add >= HASHNAMESIZE-1)
    {
      WARNING (("/etc/services file too big"));
      break;
    }
    i = port & (HASHNAMESIZE-1);
    if (!strcmp (sv->s_proto, "tcp"))
         table = &tporttable[i];
    else if (!strcmp (sv->s_proto, "udp"))
         table = &uporttable[i];
    else continue;

    while (table->name)
           table = table->nxt;

    if (nflag)
    {
      snprintf (buf, sizeof(buf), "%d", port);
      table->name = savestr (buf);
    }
    else
      table->name = savestr (sv->s_name);
    table->addr = port;
    table->nxt  = newhnamemem();
  }
  endservent();
}

static void init_eprotoarray (void)
{
  struct hnamemem *table;
  int    i;

  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 void init_protoidarray (void)
{
  struct protoidmem *tp;
  u_char protoid[5];
  int    i;

  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 (&protoid[3], &etype, 2);
    tp = lookup_protoid (protoid);
    tp->p_name = savestr (eproto_db[i].s);
  }
}

static struct etherlist {
       u_char addr[6];
       char  *name;
     } ether_list[] = {
       {
         { 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 void init_etherarray (void)
{
  struct etherlist     *el;
  struct enamemem      *tp;
  struct pcap_etherent *ep;
  FILE  *fp = fopen (ether_file, "rt");  /* Suck in entire ethers file */

  if (fp)
  {
    while ((ep = pcap_next_etherent(fp)) != NULL)
    {
      tp = lookup_emem (ep->addr);
      tp->e_name = savestr (ep->name);
    }
    fclose (fp);
  }

  /* Hardwire some ethernet names */
  for (el = ether_list; el->name; el++)
  {
    tp = lookup_emem (el->addr);
    /* Don't override existing name */
    if (tp->e_name)
      continue;
    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 void init_llcsaparray (void)
{
  int i;

  for (i = 0; llcsap_db[i].s; i++)
  {
    struct hnamemem *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.
 */
void init_addrtoname (int fflag, u_long localnet, u_long mask)
{
  if (fflag)
  {
    f_localnet = localnet;
    f_netmask  = mask;
  }
  if (nflag)
  {
    /* Simplest way to suppress names */
    return;
  }
  memset (&hnametable,  0, sizeof(hnametable));
  memset (&hname6table, 0, sizeof(hname6table));
  memset (&tporttable,  0, sizeof(tporttable));
  memset (&uporttable,  0, sizeof(uporttable));
  memset (&eprototable, 0, sizeof(eprototable));
  memset (&dnaddrtable, 0, sizeof(dnaddrtable));
  memset (&llcsaptable, 0, sizeof(llcsaptable));

  init_etherarray();
  init_servarray();
  init_eprotoarray();
  init_llcsaparray();
  init_protoidarray();
}

char *dnaddr_string (u_short dnaddr)
{
  struct hnamemem *tp;

  for (tp = &dnaddrtable[dnaddr & (HASHNAMESIZE-1)]; tp->nxt; 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)
{
  static struct hnamemem *ptr = NULL;
  static u_int  num = 0;

  if (num <= 0)
  {
    num = 64;
    ptr = calloc (num, sizeof(*ptr));
    if (!ptr)
    {
      PERROR (("newhnamemem: calloc"));
      /* not reached */
    }
  }
  num--;
  return (++ptr);
}

struct h6namemem *newh6namemem (void)
{
  struct h6namemem *p;
  static struct h6namemem *ptr = NULL;
  static u_int  num = 0;

  if (num <= 0)
  {
    num = 64;
    ptr = calloc (num, sizeof(*ptr));
    if (!ptr)
    {
      PERROR (("newh6namemem: calloc"));
      /* not reached */
    }
  }
  num--;
  p = ptr++;
  return (p);
}

/*
 *  Append new host name/addresses resolved during a session to
 *  "host.add" file
 */
void dump_hostnames (void)
{
  struct hnamemem *a;
  struct hostent  *he;
  u_long num_add = 0;
  time_t now     = time (NULL);
  char  *fname   = "hosts.add";
  FILE  *fil;

  for (a = ip4_append0; a; a = a->nxt)
      num_add++;

  if (num_add == 0)
  {
    fprintf (stderr, "No host-entries to append\n");
    return;
  }
  sethostent (1);   /* rewind hosts file */

 /* For each entry in /etc/hosts file, traverse the append list.
  * If found remove appended entry.
  */
  while ((he = gethostent()) != NULL)
  {
    for (a = ip4_append0; a; a = a->nxt)
    {
      if (a->name && !strcmp(a->name,he->h_name))
      {
        a->name = NULL;
        num_add--;
        break;
      }
    }
  }

  if (num_add == 0)
  {
    fprintf (stderr, "Hosts file need no update\n");
    return;
  }

  fil = fopen (fname, "at");
  if (!fil)
  {
    fprintf (stderr, "Cannot open `%s'\n", fname);
    return;
  }

  fprintf (fil, "#\n# New host entries at %.24s\n#\n", ctime(&now));

  for (a = ip4_append0; a; a = a->nxt)
      if (a->name)
      {
        struct in_addr addr;
        addr.s_addr = a->addr;
        fprintf (fil, "%-18s %s\n", inet_ntoa(addr), a->name);
      }

  fprintf (stderr, "%lu hosts file entries added to `%s'\n", num_add, fname);
  fclose (fil);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -