Coverage Report

Created: 2021-11-03 07:11

/src/libpcap-1.9.1/nametoaddr.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
3
 *  The Regents of the University of California.  All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that: (1) source code distributions
7
 * retain the above copyright notice and this paragraph in its entirety, (2)
8
 * distributions including binary code include the above copyright notice and
9
 * this paragraph in its entirety in the documentation or other materials
10
 * provided with the distribution, and (3) all advertising materials mentioning
11
 * features or use of this software display the following acknowledgement:
12
 * ``This product includes software developed by the University of California,
13
 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14
 * the University nor the names of its contributors may be used to endorse
15
 * or promote products derived from this software without specific prior
16
 * written permission.
17
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20
 *
21
 * Name to id translation routines used by the scanner.
22
 * These functions are not time critical.
23
 */
24
25
#ifdef HAVE_CONFIG_H
26
#include <config.h>
27
#endif
28
29
#ifdef DECNETLIB
30
#include <sys/types.h>
31
#include <netdnet/dnetdb.h>
32
#endif
33
34
#ifdef _WIN32
35
  #include <winsock2.h>
36
  #include <ws2tcpip.h>
37
38
  #ifdef INET6
39
    /*
40
     * To quote the MSDN page for getaddrinfo() at
41
     *
42
     *    https://msdn.microsoft.com/en-us/library/windows/desktop/ms738520(v=vs.85).aspx
43
     *
44
     * "Support for getaddrinfo on Windows 2000 and older versions
45
     * The getaddrinfo function was added to the Ws2_32.dll on Windows XP and
46
     * later. To execute an application that uses this function on earlier
47
     * versions of Windows, then you need to include the Ws2tcpip.h and
48
     * Wspiapi.h files. When the Wspiapi.h include file is added, the
49
     * getaddrinfo function is defined to the WspiapiGetAddrInfo inline
50
     * function in the Wspiapi.h file. At runtime, the WspiapiGetAddrInfo
51
     * function is implemented in such a way that if the Ws2_32.dll or the
52
     * Wship6.dll (the file containing getaddrinfo in the IPv6 Technology
53
     * Preview for Windows 2000) does not include getaddrinfo, then a
54
     * version of getaddrinfo is implemented inline based on code in the
55
     * Wspiapi.h header file. This inline code will be used on older Windows
56
     * platforms that do not natively support the getaddrinfo function."
57
     *
58
     * We use getaddrinfo(), so we include Wspiapi.h here.
59
     */
60
    #include <wspiapi.h>
61
  #endif /* INET6 */
62
#else /* _WIN32 */
63
  #include <sys/param.h>
64
  #include <sys/types.h>
65
  #include <sys/socket.h>
66
  #include <sys/time.h>
67
68
  #include <netinet/in.h>
69
70
  #ifdef HAVE_ETHER_HOSTTON
71
    #if defined(NET_ETHERNET_H_DECLARES_ETHER_HOSTTON)
72
      /*
73
       * OK, just include <net/ethernet.h>.
74
       */
75
      #include <net/ethernet.h>
76
    #elif defined(NETINET_ETHER_H_DECLARES_ETHER_HOSTTON)
77
      /*
78
       * OK, just include <netinet/ether.h>
79
       */
80
      #include <netinet/ether.h>
81
    #elif defined(SYS_ETHERNET_H_DECLARES_ETHER_HOSTTON)
82
      /*
83
       * OK, just include <sys/ethernet.h>
84
       */
85
      #include <sys/ethernet.h>
86
    #elif defined(ARPA_INET_H_DECLARES_ETHER_HOSTTON)
87
      /*
88
       * OK, just include <arpa/inet.h>
89
       */
90
      #include <arpa/inet.h>
91
    #elif defined(NETINET_IF_ETHER_H_DECLARES_ETHER_HOSTTON)
92
      /*
93
       * OK, include <netinet/if_ether.h>, after all the other stuff we
94
       * need to include or define for its benefit.
95
       */
96
      #define NEED_NETINET_IF_ETHER_H
97
    #else
98
      /*
99
       * We'll have to declare it ourselves.
100
       * If <netinet/if_ether.h> defines struct ether_addr, include
101
       * it.  Otherwise, define it ourselves.
102
       */
103
      #ifdef HAVE_STRUCT_ETHER_ADDR
104
        #define NEED_NETINET_IF_ETHER_H
105
      #else /* HAVE_STRUCT_ETHER_ADDR */
106
  struct ether_addr {
107
    unsigned char ether_addr_octet[6];
108
  };
109
      #endif /* HAVE_STRUCT_ETHER_ADDR */
110
    #endif /* what declares ether_hostton() */
111
112
    #ifdef NEED_NETINET_IF_ETHER_H
113
      #include <net/if.h> /* Needed on some platforms */
114
      #include <netinet/in.h> /* Needed on some platforms */
115
      #include <netinet/if_ether.h>
116
    #endif /* NEED_NETINET_IF_ETHER_H */
117
118
    #ifndef HAVE_DECL_ETHER_HOSTTON
119
      /*
120
       * No header declares it, so declare it ourselves.
121
       */
122
      extern int ether_hostton(const char *, struct ether_addr *);
123
    #endif /* !defined(HAVE_DECL_ETHER_HOSTTON) */
124
  #endif /* HAVE_ETHER_HOSTTON */
125
126
  #include <arpa/inet.h>
127
  #include <netdb.h>
128
#endif /* _WIN32 */
129
130
#include <ctype.h>
131
#include <errno.h>
132
#include <stdlib.h>
133
#include <string.h>
134
#include <stdio.h>
135
136
#include "pcap-int.h"
137
138
#include "gencode.h"
139
#include <pcap/namedb.h>
140
#include "nametoaddr.h"
141
142
#ifdef HAVE_OS_PROTO_H
143
#include "os-proto.h"
144
#endif
145
146
#ifndef NTOHL
147
0
#define NTOHL(x) (x) = ntohl(x)
148
#define NTOHS(x) (x) = ntohs(x)
149
#endif
150
151
/*
152
 *  Convert host name to internet address.
153
 *  Return 0 upon failure.
154
 *  XXX - not thread-safe; don't use it inside libpcap.
155
 */
156
bpf_u_int32 **
157
pcap_nametoaddr(const char *name)
158
0
{
159
#ifndef h_addr
160
  static bpf_u_int32 *hlist[2];
161
#endif
162
0
  bpf_u_int32 **p;
163
0
  struct hostent *hp;
164
165
0
  if ((hp = gethostbyname(name)) != NULL) {
166
#ifndef h_addr
167
    hlist[0] = (bpf_u_int32 *)hp->h_addr;
168
    NTOHL(hp->h_addr);
169
    return hlist;
170
#else
171
0
    for (p = (bpf_u_int32 **)hp->h_addr_list; *p; ++p)
172
0
      NTOHL(**p);
173
0
    return (bpf_u_int32 **)hp->h_addr_list;
174
0
#endif
175
0
  }
176
0
  else
177
0
    return 0;
178
0
}
179
180
struct addrinfo *
181
pcap_nametoaddrinfo(const char *name)
182
0
{
183
0
  struct addrinfo hints, *res;
184
0
  int error;
185
186
0
  memset(&hints, 0, sizeof(hints));
187
0
  hints.ai_family = PF_UNSPEC;
188
0
  hints.ai_socktype = SOCK_STREAM;  /*not really*/
189
0
  hints.ai_protocol = IPPROTO_TCP;  /*not really*/
190
0
  error = getaddrinfo(name, NULL, &hints, &res);
191
0
  if (error)
192
0
    return NULL;
193
0
  else
194
0
    return res;
195
0
}
196
197
/*
198
 *  Convert net name to internet address.
199
 *  Return 0 upon failure.
200
 *  XXX - not guaranteed to be thread-safe!  See below for platforms
201
 *  on which it is thread-safe and on which it isn't.
202
 */
203
bpf_u_int32
204
pcap_nametonetaddr(const char *name)
205
0
{
206
#ifdef _WIN32
207
  /*
208
   * There's no "getnetbyname()" on Windows.
209
   *
210
   * XXX - I guess we could use the BSD code to read
211
   * C:\Windows\System32\drivers\etc/networks, assuming
212
   * that's its home on all the versions of Windows
213
   * we use, but that file probably just has the loopback
214
   * network on 127/24 on 99 44/100% of Windows machines.
215
   *
216
   * (Heck, these days it probably just has that on 99 44/100%
217
   * of *UN*X* machines.)
218
   */
219
  return 0;
220
#else
221
  /*
222
   * UN*X.
223
   */
224
0
  struct netent *np;
225
0
  #if defined(HAVE_LINUX_GETNETBYNAME_R)
226
  /*
227
   * We have Linux's reentrant getnetbyname_r().
228
   */
229
0
  struct netent result_buf;
230
0
  char buf[1024]; /* arbitrary size */
231
0
  int h_errnoval;
232
0
  int err;
233
234
  /*
235
   * Apparently, the man page at
236
   *
237
   *    http://man7.org/linux/man-pages/man3/getnetbyname_r.3.html
238
   *
239
   * lies when it says
240
   *
241
   *    If the function call successfully obtains a network record,
242
   *    then *result is set pointing to result_buf; otherwise, *result
243
   *    is set to NULL.
244
   *
245
   * and, in fact, at least in some versions of GNU libc, it does
246
   * *not* always get set if getnetbyname_r() succeeds.
247
   */
248
0
  np = NULL;
249
0
  err = getnetbyname_r(name, &result_buf, buf, sizeof buf, &np,
250
0
      &h_errnoval);
251
0
  if (err != 0) {
252
    /*
253
     * XXX - dynamically allocate the buffer, and make it
254
     * bigger if we get ERANGE back?
255
     */
256
0
    return 0;
257
0
  }
258
  #elif defined(HAVE_SOLARIS_IRIX_GETNETBYNAME_R)
259
  /*
260
   * We have Solaris's and IRIX's reentrant getnetbyname_r().
261
   */
262
  struct netent result_buf;
263
  char buf[1024]; /* arbitrary size */
264
265
  np = getnetbyname_r(name, &result_buf, buf, (int)sizeof buf);
266
  #elif defined(HAVE_AIX_GETNETBYNAME_R)
267
  /*
268
   * We have AIX's reentrant getnetbyname_r().
269
   */
270
  struct netent result_buf;
271
  struct netent_data net_data;
272
273
  if (getnetbyname_r(name, &result_buf, &net_data) == -1)
274
    np = NULL;
275
  else
276
    np = &result_buf;
277
  #else
278
  /*
279
   * We don't have any getnetbyname_r(); either we have a
280
   * getnetbyname() that uses thread-specific data, in which
281
   * case we're thread-safe (sufficiently recent FreeBSD,
282
   * sufficiently recent Darwin-based OS, sufficiently recent
283
   * HP-UX, sufficiently recent Tru64 UNIX), or we have the
284
   * traditional getnetbyname() (everything else, including
285
   * current NetBSD and OpenBSD), in which case we're not
286
   * thread-safe.
287
   */
288
  np = getnetbyname(name);
289
  #endif
290
0
  if (np != NULL)
291
0
    return np->n_net;
292
0
  else
293
0
    return 0;
294
0
#endif /* _WIN32 */
295
0
}
296
297
/*
298
 * Convert a port name to its port and protocol numbers.
299
 * We assume only TCP or UDP.
300
 * Return 0 upon failure.
301
 */
302
int
303
pcap_nametoport(const char *name, int *port, int *proto)
304
0
{
305
0
  struct addrinfo hints, *res, *ai;
306
0
  int error;
307
0
  struct sockaddr_in *in4;
308
0
#ifdef INET6
309
0
  struct sockaddr_in6 *in6;
310
0
#endif
311
0
  int tcp_port = -1;
312
0
  int udp_port = -1;
313
314
  /*
315
   * We check for both TCP and UDP in case there are
316
   * ambiguous entries.
317
   */
318
0
  memset(&hints, 0, sizeof(hints));
319
0
  hints.ai_family = PF_UNSPEC;
320
0
  hints.ai_socktype = SOCK_STREAM;
321
0
  hints.ai_protocol = IPPROTO_TCP;
322
0
  error = getaddrinfo(NULL, name, &hints, &res);
323
0
  if (error != 0) {
324
0
    if (error != EAI_NONAME &&
325
0
        error != EAI_SERVICE) {
326
      /*
327
       * This is a real error, not just "there's
328
       * no such service name".
329
       * XXX - this doesn't return an error string.
330
       */
331
0
      return 0;
332
0
    }
333
0
  } else {
334
    /*
335
     * OK, we found it.  Did it find anything?
336
     */
337
0
    for (ai = res; ai != NULL; ai = ai->ai_next) {
338
      /*
339
       * Does it have an address?
340
       */
341
0
      if (ai->ai_addr != NULL) {
342
        /*
343
         * Yes.  Get a port number; we're done.
344
         */
345
0
        if (ai->ai_addr->sa_family == AF_INET) {
346
0
          in4 = (struct sockaddr_in *)ai->ai_addr;
347
0
          tcp_port = ntohs(in4->sin_port);
348
0
          break;
349
0
        }
350
0
#ifdef INET6
351
0
        if (ai->ai_addr->sa_family == AF_INET6) {
352
0
          in6 = (struct sockaddr_in6 *)ai->ai_addr;
353
0
          tcp_port = ntohs(in6->sin6_port);
354
0
          break;
355
0
        }
356
0
#endif
357
0
      }
358
0
    }
359
0
    freeaddrinfo(res);
360
0
  }
361
362
0
  memset(&hints, 0, sizeof(hints));
363
0
  hints.ai_family = PF_UNSPEC;
364
0
  hints.ai_socktype = SOCK_DGRAM;
365
0
  hints.ai_protocol = IPPROTO_UDP;
366
0
  error = getaddrinfo(NULL, name, &hints, &res);
367
0
  if (error != 0) {
368
0
    if (error != EAI_NONAME &&
369
0
        error != EAI_SERVICE) {
370
      /*
371
       * This is a real error, not just "there's
372
       * no such service name".
373
       * XXX - this doesn't return an error string.
374
       */
375
0
      return 0;
376
0
    }
377
0
  } else {
378
    /*
379
     * OK, we found it.  Did it find anything?
380
     */
381
0
    for (ai = res; ai != NULL; ai = ai->ai_next) {
382
      /*
383
       * Does it have an address?
384
       */
385
0
      if (ai->ai_addr != NULL) {
386
        /*
387
         * Yes.  Get a port number; we're done.
388
         */
389
0
        if (ai->ai_addr->sa_family == AF_INET) {
390
0
          in4 = (struct sockaddr_in *)ai->ai_addr;
391
0
          udp_port = ntohs(in4->sin_port);
392
0
          break;
393
0
        }
394
0
#ifdef INET6
395
0
        if (ai->ai_addr->sa_family == AF_INET6) {
396
0
          in6 = (struct sockaddr_in6 *)ai->ai_addr;
397
0
          udp_port = ntohs(in6->sin6_port);
398
0
          break;
399
0
        }
400
0
#endif
401
0
      }
402
0
    }
403
0
    freeaddrinfo(res);
404
0
  }
405
406
  /*
407
   * We need to check /etc/services for ambiguous entries.
408
   * If we find an ambiguous entry, and it has the
409
   * same port number, change the proto to PROTO_UNDEF
410
   * so both TCP and UDP will be checked.
411
   */
412
0
  if (tcp_port >= 0) {
413
0
    *port = tcp_port;
414
0
    *proto = IPPROTO_TCP;
415
0
    if (udp_port >= 0) {
416
0
      if (udp_port == tcp_port)
417
0
        *proto = PROTO_UNDEF;
418
#ifdef notdef
419
      else
420
        /* Can't handle ambiguous names that refer
421
           to different port numbers. */
422
        warning("ambiguous port %s in /etc/services",
423
          name);
424
#endif
425
0
    }
426
0
    return 1;
427
0
  }
428
0
  if (udp_port >= 0) {
429
0
    *port = udp_port;
430
0
    *proto = IPPROTO_UDP;
431
0
    return 1;
432
0
  }
433
#if defined(ultrix) || defined(__osf__)
434
  /* Special hack in case NFS isn't in /etc/services */
435
  if (strcmp(name, "nfs") == 0) {
436
    *port = 2049;
437
    *proto = PROTO_UNDEF;
438
    return 1;
439
  }
440
#endif
441
0
  return 0;
442
0
}
443
444
/*
445
 * Convert a string in the form PPP-PPP, where correspond to ports, to
446
 * a starting and ending port in a port range.
447
 * Return 0 on failure.
448
 */
449
int
450
pcap_nametoportrange(const char *name, int *port1, int *port2, int *proto)
451
0
{
452
0
  u_int p1, p2;
453
0
  char *off, *cpy;
454
0
  int save_proto;
455
456
0
  if (sscanf(name, "%d-%d", &p1, &p2) != 2) {
457
0
    if ((cpy = strdup(name)) == NULL)
458
0
      return 0;
459
460
0
    if ((off = strchr(cpy, '-')) == NULL) {
461
0
      free(cpy);
462
0
      return 0;
463
0
    }
464
465
0
    *off = '\0';
466
467
0
    if (pcap_nametoport(cpy, port1, proto) == 0) {
468
0
      free(cpy);
469
0
      return 0;
470
0
    }
471
0
    save_proto = *proto;
472
473
0
    if (pcap_nametoport(off + 1, port2, proto) == 0) {
474
0
      free(cpy);
475
0
      return 0;
476
0
    }
477
0
    free(cpy);
478
479
0
    if (*proto != save_proto)
480
0
      *proto = PROTO_UNDEF;
481
0
  } else {
482
0
    *port1 = p1;
483
0
    *port2 = p2;
484
0
    *proto = PROTO_UNDEF;
485
0
  }
486
487
0
  return 1;
488
0
}
489
490
/*
491
 * XXX - not guaranteed to be thread-safe!  See below for platforms
492
 * on which it is thread-safe and on which it isn't.
493
 */
494
int
495
pcap_nametoproto(const char *str)
496
0
{
497
0
  struct protoent *p;
498
0
  #if defined(HAVE_LINUX_GETNETBYNAME_R)
499
  /*
500
   * We have Linux's reentrant getprotobyname_r().
501
   */
502
0
  struct protoent result_buf;
503
0
  char buf[1024]; /* arbitrary size */
504
0
  int err;
505
506
0
  err = getprotobyname_r(str, &result_buf, buf, sizeof buf, &p);
507
0
  if (err != 0) {
508
    /*
509
     * XXX - dynamically allocate the buffer, and make it
510
     * bigger if we get ERANGE back?
511
     */
512
0
    return 0;
513
0
  }
514
  #elif defined(HAVE_SOLARIS_IRIX_GETNETBYNAME_R)
515
  /*
516
   * We have Solaris's and IRIX's reentrant getprotobyname_r().
517
   */
518
  struct protoent result_buf;
519
  char buf[1024]; /* arbitrary size */
520
521
  p = getprotobyname_r(str, &result_buf, buf, (int)sizeof buf);
522
  #elif defined(HAVE_AIX_GETNETBYNAME_R)
523
  /*
524
   * We have AIX's reentrant getprotobyname_r().
525
   */
526
  struct protoent result_buf;
527
  struct protoent_data proto_data;
528
529
  if (getprotobyname_r(str, &result_buf, &proto_data) == -1)
530
    p = NULL;
531
  else
532
    p = &result_buf;
533
  #else
534
  /*
535
   * We don't have any getprotobyname_r(); either we have a
536
   * getprotobyname() that uses thread-specific data, in which
537
   * case we're thread-safe (sufficiently recent FreeBSD,
538
   * sufficiently recent Darwin-based OS, sufficiently recent
539
   * HP-UX, sufficiently recent Tru64 UNIX, Windows), or we have
540
   * the traditional getprotobyname() (everything else, including
541
   * current NetBSD and OpenBSD), in which case we're not
542
   * thread-safe.
543
   */
544
  p = getprotobyname(str);
545
  #endif
546
0
  if (p != 0)
547
0
    return p->p_proto;
548
0
  else
549
0
    return PROTO_UNDEF;
550
0
}
551
552
#include "ethertype.h"
553
554
struct eproto {
555
  const char *s;
556
  u_short p;
557
};
558
559
/*
560
 * Static data base of ether protocol types.
561
 * tcpdump used to import this, and it's declared as an export on
562
 * Debian, at least, so make it a public symbol, even though we
563
 * don't officially export it by declaring it in a header file.
564
 * (Programs *should* do this themselves, as tcpdump now does.)
565
 *
566
 * We declare it here, right before defining it, to squelch any
567
 * warnings we might get from compilers about the lack of a
568
 * declaration.
569
 */
570
PCAP_API struct eproto eproto_db[];
571
PCAP_API_DEF struct eproto eproto_db[] = {
572
  { "pup", ETHERTYPE_PUP },
573
  { "xns", ETHERTYPE_NS },
574
  { "ip", ETHERTYPE_IP },
575
#ifdef INET6
576
  { "ip6", ETHERTYPE_IPV6 },
577
#endif
578
  { "arp", ETHERTYPE_ARP },
579
  { "rarp", ETHERTYPE_REVARP },
580
  { "sprite", ETHERTYPE_SPRITE },
581
  { "mopdl", ETHERTYPE_MOPDL },
582
  { "moprc", ETHERTYPE_MOPRC },
583
  { "decnet", ETHERTYPE_DN },
584
  { "lat", ETHERTYPE_LAT },
585
  { "sca", ETHERTYPE_SCA },
586
  { "lanbridge", ETHERTYPE_LANBRIDGE },
587
  { "vexp", ETHERTYPE_VEXP },
588
  { "vprod", ETHERTYPE_VPROD },
589
  { "atalk", ETHERTYPE_ATALK },
590
  { "atalkarp", ETHERTYPE_AARP },
591
  { "loopback", ETHERTYPE_LOOPBACK },
592
  { "decdts", ETHERTYPE_DECDTS },
593
  { "decdns", ETHERTYPE_DECDNS },
594
  { (char *)0, 0 }
595
};
596
597
int
598
pcap_nametoeproto(const char *s)
599
0
{
600
0
  struct eproto *p = eproto_db;
601
602
0
  while (p->s != 0) {
603
0
    if (strcmp(p->s, s) == 0)
604
0
      return p->p;
605
0
    p += 1;
606
0
  }
607
0
  return PROTO_UNDEF;
608
0
}
609
610
#include "llc.h"
611
612
/* Static data base of LLC values. */
613
static struct eproto llc_db[] = {
614
  { "iso", LLCSAP_ISONS },
615
  { "stp", LLCSAP_8021D },
616
  { "ipx", LLCSAP_IPX },
617
  { "netbeui", LLCSAP_NETBEUI },
618
  { (char *)0, 0 }
619
};
620
621
int
622
pcap_nametollc(const char *s)
623
0
{
624
0
  struct eproto *p = llc_db;
625
626
0
  while (p->s != 0) {
627
0
    if (strcmp(p->s, s) == 0)
628
0
      return p->p;
629
0
    p += 1;
630
0
  }
631
0
  return PROTO_UNDEF;
632
0
}
633
634
/* Hex digit to 8-bit unsigned integer. */
635
static inline u_char
636
xdtoi(u_char c)
637
0
{
638
0
  if (isdigit(c))
639
0
    return (u_char)(c - '0');
640
0
  else if (islower(c))
641
0
    return (u_char)(c - 'a' + 10);
642
0
  else
643
0
    return (u_char)(c - 'A' + 10);
644
0
}
645
646
int
647
__pcap_atoin(const char *s, bpf_u_int32 *addr)
648
0
{
649
0
  u_int n;
650
0
  int len;
651
652
0
  *addr = 0;
653
0
  len = 0;
654
0
  for (;;) {
655
0
    n = 0;
656
0
    while (*s && *s != '.')
657
0
      n = n * 10 + *s++ - '0';
658
0
    *addr <<= 8;
659
0
    *addr |= n & 0xff;
660
0
    len += 8;
661
0
    if (*s == '\0')
662
0
      return len;
663
0
    ++s;
664
0
  }
665
  /* NOTREACHED */
666
0
}
667
668
int
669
__pcap_atodn(const char *s, bpf_u_int32 *addr)
670
0
{
671
0
#define AREASHIFT 10
672
0
#define AREAMASK 0176000
673
0
#define NODEMASK 01777
674
675
0
  u_int node, area;
676
677
0
  if (sscanf(s, "%d.%d", &area, &node) != 2)
678
0
    return(0);
679
680
0
  *addr = (area << AREASHIFT) & AREAMASK;
681
0
  *addr |= (node & NODEMASK);
682
683
0
  return(32);
684
0
}
685
686
/*
687
 * Convert 's', which can have the one of the forms:
688
 *
689
 *  "xx:xx:xx:xx:xx:xx"
690
 *  "xx.xx.xx.xx.xx.xx"
691
 *  "xx-xx-xx-xx-xx-xx"
692
 *  "xxxx.xxxx.xxxx"
693
 *  "xxxxxxxxxxxx"
694
 *
695
 * (or various mixes of ':', '.', and '-') into a new
696
 * ethernet address.  Assumes 's' is well formed.
697
 */
698
u_char *
699
pcap_ether_aton(const char *s)
700
0
{
701
0
  register u_char *ep, *e;
702
0
  register u_char d;
703
704
0
  e = ep = (u_char *)malloc(6);
705
0
  if (e == NULL)
706
0
    return (NULL);
707
708
0
  while (*s) {
709
0
    if (*s == ':' || *s == '.' || *s == '-')
710
0
      s += 1;
711
0
    d = xdtoi(*s++);
712
0
    if (isxdigit((unsigned char)*s)) {
713
0
      d <<= 4;
714
0
      d |= xdtoi(*s++);
715
0
    }
716
0
    *ep++ = d;
717
0
  }
718
719
0
  return (e);
720
0
}
721
722
#ifndef HAVE_ETHER_HOSTTON
723
/*
724
 * Roll our own.
725
 * XXX - not thread-safe, because pcap_next_etherent() isn't thread-
726
 * safe!  Needs a mutex or a thread-safe pcap_next_etherent().
727
 */
728
u_char *
729
pcap_ether_hostton(const char *name)
730
{
731
  register struct pcap_etherent *ep;
732
  register u_char *ap;
733
  static FILE *fp = NULL;
734
  static int init = 0;
735
736
  if (!init) {
737
    fp = fopen(PCAP_ETHERS_FILE, "r");
738
    ++init;
739
    if (fp == NULL)
740
      return (NULL);
741
  } else if (fp == NULL)
742
    return (NULL);
743
  else
744
    rewind(fp);
745
746
  while ((ep = pcap_next_etherent(fp)) != NULL) {
747
    if (strcmp(ep->name, name) == 0) {
748
      ap = (u_char *)malloc(6);
749
      if (ap != NULL) {
750
        memcpy(ap, ep->addr, 6);
751
        return (ap);
752
      }
753
      break;
754
    }
755
  }
756
  return (NULL);
757
}
758
#else
759
/*
760
 * Use the OS-supplied routine.
761
 * This *should* be thread-safe; the API doesn't have a static buffer.
762
 */
763
u_char *
764
pcap_ether_hostton(const char *name)
765
0
{
766
0
  register u_char *ap;
767
0
  u_char a[6];
768
769
0
  ap = NULL;
770
0
  if (ether_hostton(name, (struct ether_addr *)a) == 0) {
771
0
    ap = (u_char *)malloc(6);
772
0
    if (ap != NULL)
773
0
      memcpy((char *)ap, (char *)a, 6);
774
0
  }
775
0
  return (ap);
776
0
}
777
#endif
778
779
/*
780
 * XXX - not guaranteed to be thread-safe!
781
 */
782
int
783
#ifdef  DECNETLIB
784
__pcap_nametodnaddr(const char *name, u_short *res)
785
{
786
  struct nodeent *getnodebyname();
787
  struct nodeent *nep;
788
789
  nep = getnodebyname(name);
790
  if (nep == ((struct nodeent *)0))
791
    return(0);
792
793
  memcpy((char *)res, (char *)nep->n_addr, sizeof(unsigned short));
794
  return(1);
795
#else
796
__pcap_nametodnaddr(const char *name _U_, u_short *res _U_)
797
0
{
798
0
  return(0);
799
0
#endif
800
0
}