Coverage Report

Created: 2026-02-09 06:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libpcap/nametoaddr.c
Line
Count
Source
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
#include <config.h>
26
27
#ifdef _WIN32
28
  #include <winsock2.h>
29
  #include <ws2tcpip.h>
30
#else /* _WIN32 */
31
  #include <sys/param.h>
32
  #include <sys/types.h>
33
  #include <sys/socket.h>
34
  #include <sys/time.h>
35
36
  #include <netinet/in.h>
37
38
  #if defined(__linux__) && defined(HAVE_ETHER_HOSTTON)
39
    #include <features.h>
40
    #if ! defined(__GLIBC__) && ! defined(__UCLIBC__)
41
      /*
42
       * In musl libc (which does not identify itself) ether_hostton() is
43
       * present and does not work.
44
       */
45
      #undef HAVE_ETHER_HOSTTON
46
    #endif
47
  #endif // defined(__linux__) && defined(HAVE_ETHER_HOSTTON)
48
49
  #ifdef HAVE_ETHER_HOSTTON
50
    #if defined(NET_ETHERNET_H_DECLARES_ETHER_HOSTTON)
51
      /*
52
       * OK, just include <net/ethernet.h>.
53
       */
54
      #include <net/ethernet.h>
55
    #elif defined(NETINET_ETHER_H_DECLARES_ETHER_HOSTTON)
56
      /*
57
       * OK, just include <netinet/ether.h>
58
       */
59
      #include <netinet/ether.h>
60
    #elif defined(SYS_ETHERNET_H_DECLARES_ETHER_HOSTTON)
61
      /*
62
       * OK, just include <sys/ethernet.h>
63
       */
64
      #include <sys/ethernet.h>
65
    #elif defined(ARPA_INET_H_DECLARES_ETHER_HOSTTON)
66
      /*
67
       * OK, just include <arpa/inet.h>
68
       */
69
      #include <arpa/inet.h>
70
    #elif defined(NETINET_IF_ETHER_H_DECLARES_ETHER_HOSTTON)
71
      /*
72
       * OK, include <netinet/if_ether.h>, after all the other stuff we
73
       * need to include or define for its benefit.
74
       */
75
      #define NEED_NETINET_IF_ETHER_H
76
    #else
77
      /*
78
       * We'll have to declare it ourselves.
79
       * If <netinet/if_ether.h> defines struct ether_addr, include
80
       * it.  Otherwise, define it ourselves.
81
       */
82
      #ifdef HAVE_STRUCT_ETHER_ADDR
83
        #define NEED_NETINET_IF_ETHER_H
84
      #else /* HAVE_STRUCT_ETHER_ADDR */
85
  struct ether_addr {
86
    unsigned char ether_addr_octet[6];
87
  };
88
      #endif /* HAVE_STRUCT_ETHER_ADDR */
89
    #endif /* what declares ether_hostton() */
90
91
    #ifdef NEED_NETINET_IF_ETHER_H
92
      #include <net/if.h> /* Needed on some platforms */
93
      #include <netinet/in.h> /* Needed on some platforms */
94
      #include <netinet/if_ether.h>
95
    #endif /* NEED_NETINET_IF_ETHER_H */
96
97
    #ifndef HAVE_DECL_ETHER_HOSTTON
98
      /*
99
       * No header declares it, so declare it ourselves.
100
       */
101
      extern int ether_hostton(const char *, struct ether_addr *);
102
    #endif /* !defined(HAVE_DECL_ETHER_HOSTTON) */
103
  #endif /* HAVE_ETHER_HOSTTON */
104
105
  #include <arpa/inet.h>
106
  #include <netdb.h>
107
#endif /* _WIN32 */
108
109
#include <errno.h>
110
#include <stdlib.h>
111
#include <string.h>
112
#include <stdio.h>
113
114
#include "pcap-int.h"
115
116
#include "diag-control.h"
117
118
#include "gencode.h"
119
#include <pcap/namedb.h>
120
#include "nametoaddr.h"
121
122
#include "thread-local.h"
123
124
#ifdef HAVE_OS_PROTO_H
125
#include "os-proto.h"
126
#endif
127
128
#ifndef NTOHL
129
0
#define NTOHL(x) (x) = ntohl(x)
130
#endif
131
132
/*
133
 *  Convert host name to internet address.
134
 *  Return 0 upon failure.
135
 *  XXX - not thread-safe; don't use it inside libpcap.
136
 */
137
bpf_u_int32 **
138
pcap_nametoaddr(const char *name)
139
0
{
140
#ifndef h_addr
141
  static bpf_u_int32 *hlist[2];
142
#endif
143
0
  bpf_u_int32 **p;
144
0
  struct hostent *hp;
145
146
  /*
147
   * gethostbyname() is deprecated on Windows, perhaps because
148
   * it's not thread-safe, or because it doesn't support IPv6,
149
   * or both.
150
   *
151
   * We deprecate pcap_nametoaddr() on all platforms because
152
   * it's not thread-safe; we supply it for backwards compatibility,
153
   * so suppress the deprecation warning.  We could, I guess,
154
   * use getaddrinfo() and construct the array ourselves, but
155
   * that's probably not worth the effort, as that wouldn't make
156
   * this thread-safe - we can't change the API to require that
157
   * our caller free the address array, so we still have to reuse
158
   * a local array.
159
   */
160
0
DIAG_OFF_DEPRECATION
161
0
  if ((hp = gethostbyname(name)) != NULL) {
162
0
DIAG_ON_DEPRECATION
163
#ifndef h_addr
164
    hlist[0] = (bpf_u_int32 *)hp->h_addr;
165
    NTOHL(hp->h_addr);
166
    return hlist;
167
#else
168
0
    for (p = (bpf_u_int32 **)hp->h_addr_list; *p; ++p)
169
0
      NTOHL(**p);
170
0
    return (bpf_u_int32 **)hp->h_addr_list;
171
0
#endif
172
0
  }
173
0
  else
174
0
    return 0;
175
0
}
176
177
struct addrinfo *
178
pcap_nametoaddrinfo(const char *name)
179
268
{
180
268
  struct addrinfo hints, *res;
181
268
  int error;
182
183
268
  memset(&hints, 0, sizeof(hints));
184
268
  hints.ai_family = PF_UNSPEC;
185
268
  hints.ai_socktype = SOCK_STREAM;  /*not really*/
186
268
  hints.ai_protocol = IPPROTO_TCP;  /*not really*/
187
268
  error = getaddrinfo(name, NULL, &hints, &res);
188
268
  if (error)
189
17
    return NULL;
190
251
  else
191
251
    return res;
192
268
}
193
194
/*
195
 *  Convert net name to internet address.
196
 *  Return 0 upon failure.
197
 *  XXX - not guaranteed to be thread-safe!  See below for platforms
198
 *  on which it is thread-safe and on which it isn't.
199
 */
200
#if defined(_WIN32) || defined(__CYGWIN__)
201
bpf_u_int32
202
pcap_nametonetaddr(const char *name _U_)
203
{
204
  /*
205
   * There's no "getnetbyname()" on Windows.
206
   *
207
   * XXX - I guess we could use the BSD code to read
208
   * C:\Windows\System32\drivers\etc/networks, assuming
209
   * that's its home on all the versions of Windows
210
   * we use, but that file probably just has the loopback
211
   * network on 127/24 on 99 44/100% of Windows machines.
212
   *
213
   * (Heck, these days it probably just has that on 99 44/100%
214
   * of *UN*X* machines.)
215
   */
216
  return 0;
217
}
218
#else /* _WIN32 */
219
bpf_u_int32
220
pcap_nametonetaddr(const char *name)
221
6
{
222
  /*
223
   * UN*X.
224
   */
225
6
  struct netent *np;
226
6
  #if defined(HAVE_LINUX_GETNETBYNAME_R)
227
  /*
228
   * We have Linux's reentrant getnetbyname_r().
229
   */
230
6
  struct netent result_buf;
231
6
  char buf[1024]; /* arbitrary size */
232
6
  int h_errnoval;
233
6
  int err;
234
235
  /*
236
   * Apparently, the man page at
237
   *
238
   *    http://man7.org/linux/man-pages/man3/getnetbyname_r.3.html
239
   *
240
   * lies when it says
241
   *
242
   *    If the function call successfully obtains a network record,
243
   *    then *result is set pointing to result_buf; otherwise, *result
244
   *    is set to NULL.
245
   *
246
   * and, in fact, at least in some versions of GNU libc, it does
247
   * *not* always get set if getnetbyname_r() succeeds.
248
   */
249
6
  np = NULL;
250
6
  err = getnetbyname_r(name, &result_buf, buf, sizeof buf, &np,
251
6
      &h_errnoval);
252
6
  if (err != 0) {
253
    /*
254
     * XXX - dynamically allocate the buffer, and make it
255
     * bigger if we get ERANGE back?
256
     */
257
0
    return 0;
258
0
  }
259
  #elif defined(HAVE_SOLARIS_GETNETBYNAME_R)
260
  /*
261
   * We have Solaris's reentrant getnetbyname_r().
262
   */
263
  struct netent result_buf;
264
  char buf[1024]; /* arbitrary size */
265
266
  np = getnetbyname_r(name, &result_buf, buf, (int)sizeof buf);
267
  #elif defined(HAVE_AIX_GETNETBYNAME_R)
268
  /*
269
   * We have AIX's reentrant getnetbyname_r().
270
   */
271
  struct netent result_buf;
272
  struct netent_data net_data;
273
274
  if (getnetbyname_r(name, &result_buf, &net_data) == -1)
275
    np = NULL;
276
  else
277
    np = &result_buf;
278
  #else
279
  /*
280
   * We don't have any getnetbyname_r(); either we have a
281
   * getnetbyname() that uses thread-specific data, in which
282
   * case we're thread-safe (sufficiently recent FreeBSD,
283
   * sufficiently recent Darwin-based OS, sufficiently recent
284
   * HP-UX, or we have the
285
   * traditional getnetbyname() (everything else, including
286
   * current NetBSD and OpenBSD), in which case we're not
287
   * thread-safe.
288
   */
289
  np = getnetbyname(name);
290
  #endif
291
6
  if (np != NULL)
292
0
    return np->n_net;
293
6
  else
294
6
    return 0;
295
6
}
296
#endif /* _WIN32 */
297
298
/*
299
 * Convert a port name to its port and protocol numbers.
300
 * We assume only TCP or UDP.
301
 * Return 0 upon failure.
302
 */
303
int
304
pcap_nametoport(const char *name, int *port, int *proto)
305
616
{
306
616
  struct addrinfo hints, *res, *ai;
307
616
  int error;
308
616
  struct sockaddr_in *in4;
309
616
  struct sockaddr_in6 *in6;
310
616
  int tcp_port = -1;
311
616
  int udp_port = -1;
312
313
  /*
314
   * We check for both TCP and UDP in case there are
315
   * ambiguous entries.
316
   */
317
616
  memset(&hints, 0, sizeof(hints));
318
616
  hints.ai_family = PF_UNSPEC;
319
616
  hints.ai_socktype = SOCK_STREAM;
320
616
  hints.ai_protocol = IPPROTO_TCP;
321
616
  error = getaddrinfo(NULL, name, &hints, &res);
322
616
  if (error != 0) {
323
244
    if (error != EAI_NONAME &&
324
243
        error != EAI_SERVICE) {
325
      /*
326
       * This is a real error, not just "there's
327
       * no such service name".
328
       * XXX - this doesn't return an error string.
329
       */
330
0
      return 0;
331
0
    }
332
372
  } else {
333
    /*
334
     * OK, we found it.  Did it find anything?
335
     */
336
372
    for (ai = res; ai != NULL; ai = ai->ai_next) {
337
      /*
338
       * Does it have an address?
339
       */
340
372
      if (ai->ai_addr != NULL) {
341
        /*
342
         * Yes.  Get a port number; we're done.
343
         */
344
372
        if (ai->ai_addr->sa_family == AF_INET) {
345
372
          in4 = (struct sockaddr_in *)ai->ai_addr;
346
372
          tcp_port = ntohs(in4->sin_port);
347
372
          break;
348
372
        }
349
0
        if (ai->ai_addr->sa_family == AF_INET6) {
350
0
          in6 = (struct sockaddr_in6 *)ai->ai_addr;
351
0
          tcp_port = ntohs(in6->sin6_port);
352
0
          break;
353
0
        }
354
0
      }
355
372
    }
356
372
    freeaddrinfo(res);
357
372
  }
358
359
616
  memset(&hints, 0, sizeof(hints));
360
616
  hints.ai_family = PF_UNSPEC;
361
616
  hints.ai_socktype = SOCK_DGRAM;
362
616
  hints.ai_protocol = IPPROTO_UDP;
363
616
  error = getaddrinfo(NULL, name, &hints, &res);
364
616
  if (error != 0) {
365
244
    if (error != EAI_NONAME &&
366
243
        error != EAI_SERVICE) {
367
      /*
368
       * This is a real error, not just "there's
369
       * no such service name".
370
       * XXX - this doesn't return an error string.
371
       */
372
0
      return 0;
373
0
    }
374
372
  } else {
375
    /*
376
     * OK, we found it.  Did it find anything?
377
     */
378
372
    for (ai = res; ai != NULL; ai = ai->ai_next) {
379
      /*
380
       * Does it have an address?
381
       */
382
372
      if (ai->ai_addr != NULL) {
383
        /*
384
         * Yes.  Get a port number; we're done.
385
         */
386
372
        if (ai->ai_addr->sa_family == AF_INET) {
387
372
          in4 = (struct sockaddr_in *)ai->ai_addr;
388
372
          udp_port = ntohs(in4->sin_port);
389
372
          break;
390
372
        }
391
0
        if (ai->ai_addr->sa_family == AF_INET6) {
392
0
          in6 = (struct sockaddr_in6 *)ai->ai_addr;
393
0
          udp_port = ntohs(in6->sin6_port);
394
0
          break;
395
0
        }
396
0
      }
397
372
    }
398
372
    freeaddrinfo(res);
399
372
  }
400
401
  /*
402
   * We need to check /etc/services for ambiguous entries.
403
   * If we find an ambiguous entry, and it has the
404
   * same port number, change the proto to PROTO_UNDEF
405
   * so both TCP and UDP will be checked.
406
   */
407
616
  if (tcp_port >= 0) {
408
372
    *port = tcp_port;
409
372
    *proto = IPPROTO_TCP;
410
372
    if (udp_port >= 0) {
411
372
      if (udp_port == tcp_port)
412
372
        *proto = PROTO_UNDEF;
413
#ifdef notdef
414
      else
415
        /* Can't handle ambiguous names that refer
416
           to different port numbers. */
417
        warning("ambiguous port %s in /etc/services",
418
          name);
419
#endif
420
372
    }
421
372
    return 1;
422
372
  }
423
244
  if (udp_port >= 0) {
424
0
    *port = udp_port;
425
0
    *proto = IPPROTO_UDP;
426
0
    return 1;
427
0
  }
428
244
  return 0;
429
244
}
430
431
/*
432
 * Convert a string in the form PPP-PPP, where correspond to ports, to
433
 * a starting and ending port in a port range.
434
 * Return 0 on failure.
435
 */
436
int
437
pcap_nametoportrange(const char *name, int *port1, int *port2, int *proto)
438
0
{
439
0
  char *off, *cpy;
440
0
  int save_proto;
441
442
0
  if ((cpy = strdup(name)) == NULL)
443
0
    return 0;
444
445
0
  if ((off = strchr(cpy, '-')) == NULL) {
446
0
    free(cpy);
447
0
    return 0;
448
0
  }
449
450
0
  *off = '\0';
451
452
0
  if (pcap_nametoport(cpy, port1, proto) == 0) {
453
0
    free(cpy);
454
0
    return 0;
455
0
  }
456
0
  save_proto = *proto;
457
458
0
  if (pcap_nametoport(off + 1, port2, proto) == 0) {
459
0
    free(cpy);
460
0
    return 0;
461
0
  }
462
0
  free(cpy);
463
464
0
  if (*proto != save_proto)
465
0
    *proto = PROTO_UNDEF;
466
467
0
  return 1;
468
0
}
469
470
/*
471
 * XXX - not guaranteed to be thread-safe!  See below for platforms
472
 * on which it is thread-safe and on which it isn't.
473
 */
474
int
475
pcap_nametoproto(const char *str)
476
302
{
477
302
  struct protoent *p;
478
302
  #if defined(HAVE_LINUX_GETPROTOBYNAME_R)
479
  /*
480
   * We have Linux's reentrant getprotobyname_r().
481
   */
482
302
  struct protoent result_buf;
483
302
  char buf[1024]; // "...1024 bytes should be sufficient for most applications."
484
302
  int err;
485
486
302
  err = getprotobyname_r(str, &result_buf, buf, sizeof buf, &p);
487
  /*
488
   * As far as GNU libc implementation goes, an "error" means the
489
   * protocol database could not be searched, which could mean err ==
490
   * ERANGE if the buffer is too small or ENOENT if the protocols(5)
491
   * file does not exist (the man page does not document the latter
492
   * eventuality).  If the database has been searched normally and the
493
   * requested protocol name was not found, it is not an "error" and
494
   * err == 0.
495
   *
496
   * This notwithstanding, p == NULL iff a record was not found for any
497
   * reason (whether an "error" or not), which is the same semantics as
498
   * in every other HAVE_xxxxx branch of this block.  The final check
499
   * after the block will catch that if necessary.
500
   */
501
302
  if (err != 0) {
502
    /*
503
     * XXX - dynamically allocate the buffer, and make it
504
     * bigger if we get ERANGE back?
505
     */
506
302
    return PROTO_UNDEF;
507
302
  }
508
  #elif defined(HAVE_SOLARIS_GETPROTOBYNAME_R)
509
  /*
510
   * We have Solaris's reentrant getprotobyname_r().
511
   */
512
  struct protoent result_buf;
513
  char buf[1024]; // "...must be at least 1024 bytes."
514
515
  p = getprotobyname_r(str, &result_buf, buf, (int)sizeof buf);
516
  #elif defined(HAVE_AIX_GETPROTOBYNAME_R)
517
  /*
518
   * We have AIX's reentrant getprotobyname_r().
519
   */
520
  struct protoent result_buf;
521
  // "The structure must be zero-filled before it is used..." (OpenBSD).
522
  struct protoent_data proto_data = {0};
523
524
  if (getprotobyname_r(str, &result_buf, &proto_data) == -1)
525
    p = NULL;
526
  else
527
    p = &result_buf;
528
  #else
529
  /*
530
   * We don't have any getprotobyname_r(); either we have a
531
   * getprotobyname() that uses thread-specific data, in which
532
   * case we're thread-safe (sufficiently recent FreeBSD,
533
   * sufficiently recent Darwin-based OS, sufficiently recent
534
   * HP-UX, Windows), or we have
535
   * the traditional getprotobyname() (everything else, including
536
   * current NetBSD and OpenBSD), in which case we're not
537
   * thread-safe.
538
   */
539
  p = getprotobyname(str);
540
  #endif
541
0
  if (p != 0)
542
0
    return p->p_proto;
543
0
  else
544
0
    return PROTO_UNDEF;
545
0
}
546
547
#include "ethertype.h"
548
549
struct eproto {
550
  const char *s;
551
  u_short p;
552
};
553
554
/*
555
 * Static data base of ether protocol types.
556
 * tcpdump used to import this, and it's declared as an export on
557
 * Debian, at least, so make it a public symbol, even though we
558
 * don't officially export it by declaring it in a header file.
559
 * (Programs *should* do this themselves, as tcpdump now does.)
560
 *
561
 * We declare it here, right before defining it, to squelch any
562
 * warnings we might get from compilers about the lack of a
563
 * declaration.
564
 */
565
PCAP_API struct eproto eproto_db[];
566
PCAP_API_DEF struct eproto eproto_db[] = {
567
  { "aarp", ETHERTYPE_AARP },
568
  { "arp", ETHERTYPE_ARP },
569
  { "atalk", ETHERTYPE_ATALK },
570
  { "decnet", ETHERTYPE_DN },
571
  { "ip", ETHERTYPE_IP },
572
  { "ip6", ETHERTYPE_IPV6 },
573
  { "lat", ETHERTYPE_LAT },
574
  { "lldp", ETHERTYPE_LLDP },
575
  { "loopback", ETHERTYPE_LOOPBACK },
576
  { "mopdl", ETHERTYPE_MOPDL },
577
  { "moprc", ETHERTYPE_MOPRC },
578
  { "rarp", ETHERTYPE_REVARP },
579
  { "sca", ETHERTYPE_SCA },
580
  { "slow", ETHERTYPE_SLOW },
581
  { NULL, 0 }
582
};
583
584
static int
585
proto_or_undef(const struct eproto *p, const char *s)
586
420
{
587
3.97k
  while (p->s) {
588
3.66k
    if (strcmp(p->s, s) == 0)
589
110
      return p->p;
590
3.55k
    p += 1;
591
3.55k
  }
592
310
  return PROTO_UNDEF;
593
420
}
594
595
int
596
pcap_nametoeproto(const char *s)
597
222
{
598
222
  return proto_or_undef(eproto_db, s);
599
222
}
600
601
#include "llc.h"
602
603
/* Static data base of LLC values. */
604
static struct eproto llc_db[] = {
605
  { "iso", LLCSAP_ISONS },
606
  { "stp", LLCSAP_8021D },
607
  { "ipx", LLCSAP_IPX },
608
  { "netbeui", LLCSAP_NETBEUI },
609
  { NULL, 0 }
610
};
611
612
int
613
pcap_nametollc(const char *s)
614
198
{
615
198
  return proto_or_undef(llc_db, s);
616
198
}
617
618
/* Hex digit to 8-bit unsigned integer. */
619
u_char
620
pcapint_xdtoi(const u_char c)
621
20.2k
{
622
20.2k
  if (c >= '0' && c <= '9')
623
7.42k
    return (u_char)(c - '0');
624
12.8k
  else if (c >= 'a' && c <= 'f')
625
6.76k
    return (u_char)(c - 'a' + 10);
626
6.05k
  else
627
6.05k
    return (u_char)(c - 'A' + 10);
628
20.2k
}
629
630
int
631
pcapint_atoin(const char *s, bpf_u_int32 *addr)
632
376
{
633
376
  u_int n;
634
376
  int len;
635
636
376
  *addr = 0;
637
376
  len = 0;
638
784
  for (;;) {
639
784
    n = 0;
640
1.87k
    while (*s && *s != '.') {
641
1.09k
      if (n > 25) {
642
        /* The result will be > 255 */
643
2
        return -1;
644
2
      }
645
1.08k
      n = n * 10 + *s++ - '0';
646
1.08k
    }
647
782
    if (n > 255)
648
1
      return -1;
649
781
    *addr <<= 8;
650
781
    *addr |= n & 0xff;
651
781
    len += 8;
652
781
    if (*s == '\0')
653
373
      return len;
654
408
    ++s;
655
408
  }
656
  /* NOTREACHED */
657
376
}
658
659
/*
660
 * Iff 's' is a string that is a well-formed DECnet address (aa.nnnn), parse
661
 * the address into 'addr' and return 1, otherwise return 0.
662
 *
663
 * A binary DECnet address consists of a 6-bit area number and a 10-bit node
664
 * number; neither area 0 nor node 0 are valid for normal addressing purposes,
665
 * but either can potentially appear on the wire.
666
 */
667
int
668
pcapint_atodn(const char *s, uint16_t *addr)
669
308
{
670
743
#define AREASHIFT 10
671
450
#define AREAMASK 0176000
672
499
#define NODEMASK 01777
673
674
  /* Initialize to squelch a compiler warning only. */
675
308
  u_int node = 0, area = 0;
676
  /*
677
   *               +--+             +--+
678
   *               |  |             |  |
679
   *               v  |             v  |
680
   * --> START --> AREA --> DOT --> NODE -->
681
   *       |          |     |        |
682
   *       |          v     v        |
683
   *       +--------> INVALID <------+
684
   */
685
308
  enum {
686
308
    START,
687
308
    AREA,
688
308
    DOT,
689
308
    NODE,
690
308
    INVALID
691
308
  } fsm_state = START;
692
693
2.17k
  while (*s) {
694
1.88k
    switch (fsm_state) {
695
308
    case START:
696
308
      if (PCAP_ISDIGIT(*s)) {
697
308
        area = *s - '0';
698
308
        fsm_state = AREA;
699
308
        break;
700
308
      }
701
0
      fsm_state = INVALID;
702
0
      break;
703
754
    case AREA:
704
754
      if (*s == '.') {
705
303
        fsm_state = DOT;
706
303
        break;
707
303
      }
708
451
      if (PCAP_ISDIGIT(*s)) {
709
450
        area = area * 10 + *s - '0';
710
450
        if (area <= AREAMASK >> AREASHIFT)
711
446
          break;
712
450
      }
713
5
      fsm_state = INVALID;
714
5
      break;
715
303
    case DOT:
716
303
      if (PCAP_ISDIGIT(*s)) {
717
303
        node = *s - '0';
718
303
        fsm_state = NODE;
719
303
        break;
720
303
      }
721
0
      fsm_state = INVALID;
722
0
      break;
723
504
    case NODE:
724
504
      if (PCAP_ISDIGIT(*s)) {
725
499
        node = node * 10 + *s - '0';
726
499
        if (node <= NODEMASK)
727
494
          break;
728
499
      }
729
10
      fsm_state = INVALID;
730
10
      break;
731
12
    case INVALID:
732
12
      return 0;
733
1.88k
    } /* switch */
734
1.86k
    s++;
735
1.86k
  } /* while */
736
  /*
737
   * This condition is false if the string comes from the lexer, but
738
   * let's not depend on that.
739
   */
740
296
  if (fsm_state != NODE)
741
3
    return 0;
742
743
  // Both 'area' and 'node' have been verified to be in range.
744
293
  *addr = (uint16_t)(area << AREASHIFT | node);
745
293
  return 1;
746
296
}
747
748
/*
749
 * libpcap ARCnet address format is "^\$[0-9a-fA-F]{1,2}$" in regexp syntax.
750
 * Iff the given string is a well-formed ARCnet address, parse the string,
751
 * store the 8-bit unsigned value into the provided integer and return 1.
752
 * Otherwise return 0.
753
 *
754
 *  --> START -- $ --> DOLLAR -- [0-9a-fA-F] --> HEX1 -- \0 -->-+
755
 *        |              |                        |             |
756
 *       [.]            [.]                  [0-9a-fA-F]        |
757
 *        |              |                        |             |
758
 *        v              v                        v             v
759
 *    (invalid) <--------+-<---------------[.]-- HEX2 -- \0 -->-+--> (valid)
760
 */
761
int
762
pcapint_atoan(const char *s, uint8_t *addr)
763
565
{
764
565
  enum {
765
565
    START,
766
565
    DOLLAR,
767
565
    HEX1,
768
565
    HEX2,
769
565
  } fsm_state = START;
770
565
  uint8_t tmp = 0;
771
772
1.96k
  while (*s) {
773
1.40k
    switch (fsm_state) {
774
565
    case START:
775
565
      if (*s != '$')
776
0
        goto invalid;
777
565
      fsm_state = DOLLAR;
778
565
      break;
779
565
    case DOLLAR:
780
565
      if (! PCAP_ISXDIGIT(*s))
781
0
        goto invalid;
782
565
      tmp = pcapint_xdtoi(*s);
783
565
      fsm_state = HEX1;
784
565
      break;
785
273
    case HEX1:
786
273
      if (! PCAP_ISXDIGIT(*s))
787
0
        goto invalid;
788
273
      tmp <<= 4;
789
273
      tmp |= pcapint_xdtoi(*s);
790
273
      fsm_state = HEX2;
791
273
      break;
792
0
    case HEX2:
793
0
      goto invalid;
794
1.40k
    } // switch
795
1.40k
    s++;
796
1.40k
  } // while
797
565
  if (fsm_state == HEX1 || fsm_state == HEX2) {
798
565
    *addr = tmp;
799
565
    return 1;
800
565
  }
801
802
0
invalid:
803
0
  return 0;
804
565
}
805
806
// Man page: "xxxxxxxxxxxx", regexp: "^[0-9a-fA-F]{12}$".
807
static u_char
808
pcapint_atomac48_xxxxxxxxxxxx(const char *s, uint8_t *addr)
809
1.95k
{
810
1.95k
  if (strlen(s) == 12 &&
811
1.23k
      PCAP_ISXDIGIT(s[0]) &&
812
1.23k
      PCAP_ISXDIGIT(s[1]) &&
813
907
      PCAP_ISXDIGIT(s[2]) &&
814
842
      PCAP_ISXDIGIT(s[3]) &&
815
842
      PCAP_ISXDIGIT(s[4]) &&
816
842
      PCAP_ISXDIGIT(s[5]) &&
817
842
      PCAP_ISXDIGIT(s[6]) &&
818
842
      PCAP_ISXDIGIT(s[7]) &&
819
842
      PCAP_ISXDIGIT(s[8]) &&
820
842
      PCAP_ISXDIGIT(s[9]) &&
821
842
      PCAP_ISXDIGIT(s[10]) &&
822
842
      PCAP_ISXDIGIT(s[11])) {
823
842
    addr[0] = pcapint_xdtoi(s[0]) << 4 | pcapint_xdtoi(s[1]);
824
842
    addr[1] = pcapint_xdtoi(s[2]) << 4 | pcapint_xdtoi(s[3]);
825
842
    addr[2] = pcapint_xdtoi(s[4]) << 4 | pcapint_xdtoi(s[5]);
826
842
    addr[3] = pcapint_xdtoi(s[6]) << 4 | pcapint_xdtoi(s[7]);
827
842
    addr[4] = pcapint_xdtoi(s[8]) << 4 | pcapint_xdtoi(s[9]);
828
842
    addr[5] = pcapint_xdtoi(s[10]) << 4 | pcapint_xdtoi(s[11]);
829
842
    return 1;
830
842
  }
831
1.11k
  return 0;
832
1.95k
}
833
834
// Man page: "xxxx.xxxx.xxxx", regexp: "^[0-9a-fA-F]{4}(\.[0-9a-fA-F]{4}){2}$".
835
static u_char
836
pcapint_atomac48_xxxx_3_times(const char *s, uint8_t *addr)
837
1.11k
{
838
1.11k
  const char sep = '.';
839
1.11k
  if (strlen(s) == 14 &&
840
394
      PCAP_ISXDIGIT(s[0]) &&
841
394
      PCAP_ISXDIGIT(s[1]) &&
842
321
      PCAP_ISXDIGIT(s[2]) &&
843
238
      PCAP_ISXDIGIT(s[3]) &&
844
238
      s[4] == sep &&
845
238
      PCAP_ISXDIGIT(s[5]) &&
846
238
      PCAP_ISXDIGIT(s[6]) &&
847
238
      PCAP_ISXDIGIT(s[7]) &&
848
238
      PCAP_ISXDIGIT(s[8]) &&
849
238
      s[9] == sep &&
850
238
      PCAP_ISXDIGIT(s[10]) &&
851
238
      PCAP_ISXDIGIT(s[11]) &&
852
238
      PCAP_ISXDIGIT(s[12]) &&
853
238
      PCAP_ISXDIGIT(s[13])) {
854
238
    addr[0] = pcapint_xdtoi(s[0]) << 4 | pcapint_xdtoi(s[1]);
855
238
    addr[1] = pcapint_xdtoi(s[2]) << 4 | pcapint_xdtoi(s[3]);
856
238
    addr[2] = pcapint_xdtoi(s[5]) << 4 | pcapint_xdtoi(s[6]);
857
238
    addr[3] = pcapint_xdtoi(s[7]) << 4 | pcapint_xdtoi(s[8]);
858
238
    addr[4] = pcapint_xdtoi(s[10]) << 4 | pcapint_xdtoi(s[11]);
859
238
    addr[5] = pcapint_xdtoi(s[12]) << 4 | pcapint_xdtoi(s[13]);
860
238
    return 1;
861
238
  }
862
875
  return 0;
863
1.11k
}
864
865
/*
866
 * Man page: "xx:xx:xx:xx:xx:xx", regexp: "^[0-9a-fA-F]{1,2}(:[0-9a-fA-F]{1,2}){5}$".
867
 * Man page: "xx-xx-xx-xx-xx-xx", regexp: "^[0-9a-fA-F]{1,2}(-[0-9a-fA-F]{1,2}){5}$".
868
 * Man page: "xx.xx.xx.xx.xx.xx", regexp: "^[0-9a-fA-F]{1,2}(\.[0-9a-fA-F]{1,2}){5}$".
869
 * (Any "xx" above can be "x", which is equivalent to "0x".)
870
 *
871
 * An equivalent (and parametrisable for EUI-64) FSM could be implemented using
872
 * a smaller graph, but that graph would be neither acyclic nor planar nor
873
 * trivial to verify.
874
 *
875
 *                |
876
 *    [.]         v
877
 * +<---------- START
878
 * |              |
879
 * |              | [0-9a-fA-F]
880
 * |  [.]         v
881
 * +<--------- BYTE0_X ----------+
882
 * |              |              |
883
 * |              | [0-9a-fA-F]  |
884
 * |  [.]         v              |
885
 * +<--------- BYTE0_XX          | [:\.-]
886
 * |              |              |
887
 * |              | [:\.-]       |
888
 * |  [.]         v              |
889
 * +<----- BYTE0_SEP_BYTE1 <-----+
890
 * |              |
891
 * |              | [0-9a-fA-F]
892
 * |  [.]         v
893
 * +<--------- BYTE1_X ----------+
894
 * |              |              |
895
 * |              | [0-9a-fA-F]  |
896
 * |  [.]         v              |
897
 * +<--------- BYTE1_XX          | <sep>
898
 * |              |              |
899
 * |              | <sep>        |
900
 * |  [.]         v              |
901
 * +<----- BYTE1_SEP_BYTE2 <-----+
902
 * |              |
903
 * |              | [0-9a-fA-F]
904
 * |  [.]         v
905
 * +<--------- BYTE2_X ----------+
906
 * |              |              |
907
 * |              | [0-9a-fA-F]  |
908
 * |  [.]         v              |
909
 * +<--------- BYTE2_XX          | <sep>
910
 * |              |              |
911
 * |              | <sep>        |
912
 * |  [.]         v              |
913
 * +<----- BYTE2_SEP_BYTE3 <-----+
914
 * |              |
915
 * |              | [0-9a-fA-F]
916
 * |  [.]         v
917
 * +<--------- BYTE3_X ----------+
918
 * |              |              |
919
 * |              | [0-9a-fA-F]  |
920
 * |  [.]         v              |
921
 * +<--------- BYTE3_XX          | <sep>
922
 * |              |              |
923
 * |              | <sep>        |
924
 * |  [.]         v              |
925
 * +<----- BYTE3_SEP_BYTE4 <-----+
926
 * |              |
927
 * |              | [0-9a-fA-F]
928
 * |  [.]         v
929
 * +<--------- BYTE4_X ----------+
930
 * |              |              |
931
 * |              | [0-9a-fA-F]  |
932
 * |  [.]         v              |
933
 * +<--------- BYTE4_XX          | <sep>
934
 * |              |              |
935
 * |              | <sep>        |
936
 * |  [.]         v              |
937
 * +<----- BYTE4_SEP_BYTE5 <-----+
938
 * |              |
939
 * |              | [0-9a-fA-F]
940
 * |  [.]         v
941
 * +<--------- BYTE5_X ----------+
942
 * |              |              |
943
 * |              | [0-9a-fA-F]  |
944
 * |  [.]         v              |
945
 * +<--------- BYTE5_XX          | \0
946
 * |              |              |
947
 * |              | \0           |
948
 * |              |              v
949
 * +--> (reject)  +---------> (accept)
950
 *
951
 */
952
static u_char
953
pcapint_atomac48_x_xx_6_times(const char *s, uint8_t *addr)
954
875
{
955
875
  enum {
956
875
    START,
957
875
    BYTE0_X,
958
875
    BYTE0_XX,
959
875
    BYTE0_SEP_BYTE1,
960
875
    BYTE1_X,
961
875
    BYTE1_XX,
962
875
    BYTE1_SEP_BYTE2,
963
875
    BYTE2_X,
964
875
    BYTE2_XX,
965
875
    BYTE2_SEP_BYTE3,
966
875
    BYTE3_X,
967
875
    BYTE3_XX,
968
875
    BYTE3_SEP_BYTE4,
969
875
    BYTE4_X,
970
875
    BYTE4_XX,
971
875
    BYTE4_SEP_BYTE5,
972
875
    BYTE5_X,
973
875
    BYTE5_XX,
974
875
  } fsm_state = START;
975
875
  uint8_t buf[6];
976
875
  const char *seplist = ":.-";
977
  /*
978
   * XXX - the state diagram indicates that we cannot get to states
979
   * BYTEn_X or BYTEn_XX, for n > 0, without first going through
980
   * states BYTE0_X or BYTE0_XX, but at least some versions of
981
   * MSVC complain that sep may be used uninitialized, so we
982
   * initialize it here.
983
   */
984
875
  char sep = '\0';
985
986
11.6k
  while (*s) {
987
10.8k
    switch (fsm_state) {
988
875
    case START:
989
875
      if (PCAP_ISXDIGIT(*s)) {
990
875
        buf[0] = pcapint_xdtoi(*s);
991
875
        fsm_state = BYTE0_X;
992
875
        break;
993
875
      }
994
0
      goto reject;
995
875
    case BYTE0_X:
996
875
      if (strchr(seplist, *s)) {
997
669
        sep = *s;
998
669
        fsm_state = BYTE0_SEP_BYTE1;
999
669
        break;
1000
669
      }
1001
206
      if (PCAP_ISXDIGIT(*s)) {
1002
206
        buf[0] = buf[0] << 4 | pcapint_xdtoi(*s);
1003
206
        fsm_state = BYTE0_XX;
1004
206
        break;
1005
206
      }
1006
0
      goto reject;
1007
206
    case BYTE0_XX:
1008
206
      if (strchr(seplist, *s)) {
1009
206
        sep = *s;
1010
206
        fsm_state = BYTE0_SEP_BYTE1;
1011
206
        break;
1012
206
      }
1013
0
      goto reject;
1014
875
    case BYTE0_SEP_BYTE1:
1015
875
      if (PCAP_ISXDIGIT(*s)) {
1016
875
        buf[1] = pcapint_xdtoi(*s);
1017
875
        fsm_state = BYTE1_X;
1018
875
        break;
1019
875
      }
1020
0
      goto reject;
1021
875
    case BYTE1_X:
1022
875
      if (*s == sep) {
1023
674
        fsm_state = BYTE1_SEP_BYTE2;
1024
674
        break;
1025
674
      }
1026
201
      if (PCAP_ISXDIGIT(*s)) {
1027
201
        buf[1] = buf[1] << 4 | pcapint_xdtoi(*s);
1028
201
        fsm_state = BYTE1_XX;
1029
201
        break;
1030
201
      }
1031
0
      goto reject;
1032
201
    case BYTE1_XX:
1033
201
      if (*s == sep) {
1034
201
        fsm_state = BYTE1_SEP_BYTE2;
1035
201
        break;
1036
201
      }
1037
0
      goto reject;
1038
875
    case BYTE1_SEP_BYTE2:
1039
875
      if (PCAP_ISXDIGIT(*s)) {
1040
875
        buf[2] = pcapint_xdtoi(*s);
1041
875
        fsm_state = BYTE2_X;
1042
875
        break;
1043
875
      }
1044
0
      goto reject;
1045
875
    case BYTE2_X:
1046
875
      if (*s == sep) {
1047
685
        fsm_state = BYTE2_SEP_BYTE3;
1048
685
        break;
1049
685
      }
1050
190
      if (PCAP_ISXDIGIT(*s)) {
1051
190
        buf[2] = buf[2] << 4 | pcapint_xdtoi(*s);
1052
190
        fsm_state = BYTE2_XX;
1053
190
        break;
1054
190
      }
1055
0
      goto reject;
1056
190
    case BYTE2_XX:
1057
190
      if (*s == sep) {
1058
190
        fsm_state = BYTE2_SEP_BYTE3;
1059
190
        break;
1060
190
      }
1061
0
      goto reject;
1062
875
    case BYTE2_SEP_BYTE3:
1063
875
      if (PCAP_ISXDIGIT(*s)) {
1064
875
        buf[3] = pcapint_xdtoi(*s);
1065
875
        fsm_state = BYTE3_X;
1066
875
        break;
1067
875
      }
1068
0
      goto reject;
1069
875
    case BYTE3_X:
1070
875
      if (*s == sep) {
1071
695
        fsm_state = BYTE3_SEP_BYTE4;
1072
695
        break;
1073
695
      }
1074
180
      if (PCAP_ISXDIGIT(*s)) {
1075
180
        buf[3] = buf[3] << 4 | pcapint_xdtoi(*s);
1076
180
        fsm_state = BYTE3_XX;
1077
180
        break;
1078
180
      }
1079
0
      goto reject;
1080
180
    case BYTE3_XX:
1081
180
      if (*s == sep) {
1082
180
        fsm_state = BYTE3_SEP_BYTE4;
1083
180
        break;
1084
180
      }
1085
0
      goto reject;
1086
875
    case BYTE3_SEP_BYTE4:
1087
875
      if (PCAP_ISXDIGIT(*s)) {
1088
875
        buf[4] = pcapint_xdtoi(*s);
1089
875
        fsm_state = BYTE4_X;
1090
875
        break;
1091
875
      }
1092
0
      goto reject;
1093
875
    case BYTE4_X:
1094
875
      if (*s == sep) {
1095
659
        fsm_state = BYTE4_SEP_BYTE5;
1096
659
        break;
1097
659
      }
1098
216
      if (PCAP_ISXDIGIT(*s)) {
1099
216
        buf[4] = buf[4] << 4 | pcapint_xdtoi(*s);
1100
216
        fsm_state = BYTE4_XX;
1101
216
        break;
1102
216
      }
1103
0
      goto reject;
1104
216
    case BYTE4_XX:
1105
216
      if (*s == sep) {
1106
216
        fsm_state = BYTE4_SEP_BYTE5;
1107
216
        break;
1108
216
      }
1109
0
      goto reject;
1110
875
    case BYTE4_SEP_BYTE5:
1111
875
      if (PCAP_ISXDIGIT(*s)) {
1112
875
        buf[5] = pcapint_xdtoi(*s);
1113
875
        fsm_state = BYTE5_X;
1114
875
        break;
1115
875
      }
1116
0
      goto reject;
1117
202
    case BYTE5_X:
1118
202
      if (PCAP_ISXDIGIT(*s)) {
1119
202
        buf[5] = buf[5] << 4 | pcapint_xdtoi(*s);
1120
202
        fsm_state = BYTE5_XX;
1121
202
        break;
1122
202
      }
1123
0
      goto reject;
1124
0
    case BYTE5_XX:
1125
0
      goto reject;
1126
10.8k
    } // switch
1127
10.8k
    s++;
1128
10.8k
  } // while
1129
1130
875
  if (fsm_state == BYTE5_X || fsm_state == BYTE5_XX) {
1131
    // accept
1132
875
    memcpy(addr, buf, sizeof(buf));
1133
875
    return 1;
1134
875
  }
1135
1136
0
reject:
1137
0
  return 0;
1138
875
}
1139
1140
// The 'addr' argument must point to an array of at least 6 elements.
1141
int
1142
pcapint_atomac48(const char *s, uint8_t *addr)
1143
1.95k
{
1144
1.95k
  return s && (
1145
1.95k
      pcapint_atomac48_xxxxxxxxxxxx(s, addr) ||
1146
1.11k
      pcapint_atomac48_xxxx_3_times(s, addr) ||
1147
875
      pcapint_atomac48_x_xx_6_times(s, addr)
1148
1.95k
  );
1149
1.95k
}
1150
1151
/*
1152
 * If 's' is a MAC-48 address in one of the forms documented in pcap-filter(7)
1153
 * for "ether host", return a pointer to an allocated buffer with the binary
1154
 * value of the address.  Return NULL on any error.
1155
 */
1156
u_char *
1157
pcap_ether_aton(const char *s)
1158
0
{
1159
0
  uint8_t tmp[6];
1160
0
  if (! pcapint_atomac48(s, tmp))
1161
0
    return (NULL);
1162
1163
0
  u_char *e = malloc(6);
1164
0
  if (e == NULL)
1165
0
    return (NULL);
1166
0
  memcpy(e, tmp, sizeof(tmp));
1167
0
  return (e);
1168
0
}
1169
1170
#ifndef HAVE_ETHER_HOSTTON
1171
/*
1172
 * Roll our own.
1173
 *
1174
 * This should be thread-safe, as we define the static variables
1175
 * we use to be thread-local, and as pcap_next_etherent() does so
1176
 * as well.
1177
 */
1178
u_char *
1179
pcap_ether_hostton(const char *name)
1180
{
1181
  struct pcap_etherent *ep;
1182
  u_char *ap;
1183
  static thread_local FILE *fp = NULL;
1184
  static thread_local int init = 0;
1185
1186
  if (!init) {
1187
    fp = fopen(PCAP_ETHERS_FILE, "r");
1188
    ++init;
1189
    if (fp == NULL)
1190
      return (NULL);
1191
  } else if (fp == NULL)
1192
    return (NULL);
1193
  else
1194
    rewind(fp);
1195
1196
  while ((ep = pcap_next_etherent(fp)) != NULL) {
1197
    if (strcmp(ep->name, name) == 0) {
1198
      ap = (u_char *)malloc(6);
1199
      if (ap != NULL) {
1200
        memcpy(ap, ep->addr, 6);
1201
        return (ap);
1202
      }
1203
      break;
1204
    }
1205
  }
1206
  return (NULL);
1207
}
1208
#else
1209
/*
1210
 * Use the OS-supplied routine.
1211
 * This *should* be thread-safe; the API doesn't have a static buffer.
1212
 */
1213
u_char *
1214
pcap_ether_hostton(const char *name)
1215
97
{
1216
97
  u_char *ap;
1217
97
  u_char a[6];
1218
97
  char namebuf[1024];
1219
1220
  /*
1221
   * In AIX 7.1 and 7.2: int ether_hostton(char *, struct ether_addr *);
1222
   */
1223
97
  pcapint_strlcpy(namebuf, name, sizeof(namebuf));
1224
97
  ap = NULL;
1225
97
  if (ether_hostton(namebuf, (struct ether_addr *)a) == 0) {
1226
0
    ap = (u_char *)malloc(6);
1227
0
    if (ap != NULL)
1228
0
      memcpy((char *)ap, (char *)a, 6);
1229
0
  }
1230
97
  return (ap);
1231
97
}
1232
#endif