1 /* $NetBSD: nametoaddr.c,v 1.1.1.4 2013/12/31 16:57:25 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that: (1) source code distributions 9 * retain the above copyright notice and this paragraph in its entirety, (2) 10 * distributions including binary code include the above copyright notice and 11 * this paragraph in its entirety in the documentation or other materials 12 * provided with the distribution, and (3) all advertising materials mentioning 13 * features or use of this software display the following acknowledgement: 14 * ``This product includes software developed by the University of California, 15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 16 * the University nor the names of its contributors may be used to endorse 17 * or promote products derived from this software without specific prior 18 * written permission. 19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 22 * 23 * Name to id translation routines used by the scanner. 24 * These functions are not time critical. 25 */ 26 27 #ifndef lint 28 static const char rcsid[] _U_ = 29 "@(#) Header: /tcpdump/master/libpcap/nametoaddr.c,v 1.83 2008-02-06 10:21:30 guy Exp (LBL)"; 30 #endif 31 32 #ifdef HAVE_CONFIG_H 33 #include "config.h" 34 #endif 35 36 #ifdef DECNETLIB 37 #include <sys/types.h> 38 #include <netdnet/dnetdb.h> 39 #endif 40 41 #ifdef WIN32 42 #include <pcap-stdinc.h> 43 44 #else /* WIN32 */ 45 46 #include <sys/param.h> 47 #include <sys/types.h> /* concession to AIX */ 48 #include <sys/socket.h> 49 #include <sys/time.h> 50 51 #include <netinet/in.h> 52 #endif /* WIN32 */ 53 54 #ifndef WIN32 55 #ifdef HAVE_ETHER_HOSTTON 56 /* 57 * XXX - do we need any of this if <netinet/if_ether.h> doesn't declare 58 * ether_hostton()? 59 */ 60 #ifdef HAVE_NETINET_IF_ETHER_H 61 struct mbuf; /* Squelch compiler warnings on some platforms for */ 62 struct rtentry; /* declarations in <net/if.h> */ 63 #include <net/if.h> /* for "struct ifnet" in "struct arpcom" on Solaris */ 64 #include <netinet/if_ether.h> 65 #endif /* HAVE_NETINET_IF_ETHER_H */ 66 #ifdef NETINET_ETHER_H_DECLARES_ETHER_HOSTTON 67 #include <netinet/ether.h> 68 #endif /* NETINET_ETHER_H_DECLARES_ETHER_HOSTTON */ 69 #endif /* HAVE_ETHER_HOSTTON */ 70 #include <arpa/inet.h> 71 #include <netdb.h> 72 #endif /* WIN32 */ 73 74 #include <ctype.h> 75 #include <errno.h> 76 #include <stdlib.h> 77 #include <string.h> 78 #include <stdio.h> 79 80 #include "pcap-int.h" 81 82 #include "gencode.h" 83 #include <pcap/namedb.h> 84 85 #ifdef HAVE_OS_PROTO_H 86 #include "os-proto.h" 87 #endif 88 89 #ifndef NTOHL 90 #define NTOHL(x) (x) = ntohl(x) 91 #define NTOHS(x) (x) = ntohs(x) 92 #endif 93 94 static inline int xdtoi(int); 95 96 /* 97 * Convert host name to internet address. 98 * Return 0 upon failure. 99 */ 100 bpf_u_int32 ** 101 pcap_nametoaddr(const char *name) 102 { 103 #ifndef h_addr 104 static bpf_u_int32 *hlist[2]; 105 #endif 106 bpf_u_int32 **p; 107 struct hostent *hp; 108 109 if ((hp = gethostbyname(name)) != NULL) { 110 #ifndef h_addr 111 hlist[0] = (bpf_u_int32 *)hp->h_addr; 112 NTOHL(hp->h_addr); 113 return hlist; 114 #else 115 for (p = (bpf_u_int32 **)hp->h_addr_list; *p; ++p) 116 NTOHL(**p); 117 return (bpf_u_int32 **)hp->h_addr_list; 118 #endif 119 } 120 else 121 return 0; 122 } 123 124 #ifdef INET6 125 struct addrinfo * 126 pcap_nametoaddrinfo(const char *name) 127 { 128 struct addrinfo hints, *res; 129 int error; 130 131 memset(&hints, 0, sizeof(hints)); 132 hints.ai_family = PF_UNSPEC; 133 hints.ai_socktype = SOCK_STREAM; /*not really*/ 134 hints.ai_protocol = IPPROTO_TCP; /*not really*/ 135 error = getaddrinfo(name, NULL, &hints, &res); 136 if (error) 137 return NULL; 138 else 139 return res; 140 } 141 #endif /*INET6*/ 142 143 /* 144 * Convert net name to internet address. 145 * Return 0 upon failure. 146 */ 147 bpf_u_int32 148 pcap_nametonetaddr(const char *name) 149 { 150 #ifndef WIN32 151 struct netent *np; 152 153 if ((np = getnetbyname(name)) != NULL) 154 return np->n_net; 155 else 156 return 0; 157 #else 158 /* 159 * There's no "getnetbyname()" on Windows. 160 */ 161 return 0; 162 #endif 163 } 164 165 /* 166 * Convert a port name to its port and protocol numbers. 167 * We assume only TCP or UDP. 168 * Return 0 upon failure. 169 */ 170 int 171 pcap_nametoport(const char *name, int *port, int *proto) 172 { 173 struct servent *sp; 174 int tcp_port = -1; 175 int udp_port = -1; 176 177 /* 178 * We need to check /etc/services for ambiguous entries. 179 * If we find the ambiguous entry, and it has the 180 * same port number, change the proto to PROTO_UNDEF 181 * so both TCP and UDP will be checked. 182 */ 183 sp = getservbyname(name, "tcp"); 184 if (sp != NULL) tcp_port = ntohs(sp->s_port); 185 sp = getservbyname(name, "udp"); 186 if (sp != NULL) udp_port = ntohs(sp->s_port); 187 if (tcp_port >= 0) { 188 *port = tcp_port; 189 *proto = IPPROTO_TCP; 190 if (udp_port >= 0) { 191 if (udp_port == tcp_port) 192 *proto = PROTO_UNDEF; 193 #ifdef notdef 194 else 195 /* Can't handle ambiguous names that refer 196 to different port numbers. */ 197 warning("ambiguous port %s in /etc/services", 198 name); 199 #endif 200 } 201 return 1; 202 } 203 if (udp_port >= 0) { 204 *port = udp_port; 205 *proto = IPPROTO_UDP; 206 return 1; 207 } 208 #if defined(ultrix) || defined(__osf__) 209 /* Special hack in case NFS isn't in /etc/services */ 210 if (strcmp(name, "nfs") == 0) { 211 *port = 2049; 212 *proto = PROTO_UNDEF; 213 return 1; 214 } 215 #endif 216 return 0; 217 } 218 219 /* 220 * Convert a string in the form PPP-PPP, where correspond to ports, to 221 * a starting and ending port in a port range. 222 * Return 0 on failure. 223 */ 224 int 225 pcap_nametoportrange(const char *name, int *port1, int *port2, int *proto) 226 { 227 u_int p1, p2; 228 char *off, *cpy; 229 int save_proto; 230 231 if (sscanf(name, "%d-%d", &p1, &p2) != 2) { 232 if ((cpy = strdup(name)) == NULL) 233 return 0; 234 235 if ((off = strchr(cpy, '-')) == NULL) { 236 free(cpy); 237 return 0; 238 } 239 240 *off = '\0'; 241 242 if (pcap_nametoport(cpy, port1, proto) == 0) { 243 free(cpy); 244 return 0; 245 } 246 save_proto = *proto; 247 248 if (pcap_nametoport(off + 1, port2, proto) == 0) { 249 free(cpy); 250 return 0; 251 } 252 free(cpy); 253 254 if (*proto != save_proto) 255 *proto = PROTO_UNDEF; 256 } else { 257 *port1 = p1; 258 *port2 = p2; 259 *proto = PROTO_UNDEF; 260 } 261 262 return 1; 263 } 264 265 int 266 pcap_nametoproto(const char *str) 267 { 268 struct protoent *p; 269 270 p = getprotobyname(str); 271 if (p != 0) 272 return p->p_proto; 273 else 274 return PROTO_UNDEF; 275 } 276 277 #include "ethertype.h" 278 279 struct eproto { 280 const char *s; 281 u_short p; 282 }; 283 284 /* Static data base of ether protocol types. */ 285 struct eproto eproto_db[] = { 286 { "pup", ETHERTYPE_PUP }, 287 { "xns", ETHERTYPE_NS }, 288 { "ip", ETHERTYPE_IP }, 289 #ifdef INET6 290 { "ip6", ETHERTYPE_IPV6 }, 291 #endif 292 { "arp", ETHERTYPE_ARP }, 293 { "rarp", ETHERTYPE_REVARP }, 294 { "sprite", ETHERTYPE_SPRITE }, 295 { "mopdl", ETHERTYPE_MOPDL }, 296 { "moprc", ETHERTYPE_MOPRC }, 297 { "decnet", ETHERTYPE_DN }, 298 { "lat", ETHERTYPE_LAT }, 299 { "sca", ETHERTYPE_SCA }, 300 { "lanbridge", ETHERTYPE_LANBRIDGE }, 301 { "vexp", ETHERTYPE_VEXP }, 302 { "vprod", ETHERTYPE_VPROD }, 303 { "atalk", ETHERTYPE_ATALK }, 304 { "atalkarp", ETHERTYPE_AARP }, 305 { "loopback", ETHERTYPE_LOOPBACK }, 306 { "decdts", ETHERTYPE_DECDTS }, 307 { "decdns", ETHERTYPE_DECDNS }, 308 { (char *)0, 0 } 309 }; 310 311 int 312 pcap_nametoeproto(const char *s) 313 { 314 struct eproto *p = eproto_db; 315 316 while (p->s != 0) { 317 if (strcmp(p->s, s) == 0) 318 return p->p; 319 p += 1; 320 } 321 return PROTO_UNDEF; 322 } 323 324 #include "llc.h" 325 326 /* Static data base of LLC values. */ 327 static struct eproto llc_db[] = { 328 { "iso", LLCSAP_ISONS }, 329 { "stp", LLCSAP_8021D }, 330 { "ipx", LLCSAP_IPX }, 331 { "netbeui", LLCSAP_NETBEUI }, 332 { (char *)0, 0 } 333 }; 334 335 int 336 pcap_nametollc(const char *s) 337 { 338 struct eproto *p = llc_db; 339 340 while (p->s != 0) { 341 if (strcmp(p->s, s) == 0) 342 return p->p; 343 p += 1; 344 } 345 return PROTO_UNDEF; 346 } 347 348 /* Hex digit to integer. */ 349 static inline int 350 xdtoi(c) 351 register int c; 352 { 353 if (isdigit(c)) 354 return c - '0'; 355 else if (islower(c)) 356 return c - 'a' + 10; 357 else 358 return c - 'A' + 10; 359 } 360 361 int 362 __pcap_atoin(const char *s, bpf_u_int32 *addr) 363 { 364 u_int n; 365 int len; 366 367 *addr = 0; 368 len = 0; 369 while (1) { 370 n = 0; 371 while (*s && *s != '.') 372 n = n * 10 + *s++ - '0'; 373 *addr <<= 8; 374 *addr |= n & 0xff; 375 len += 8; 376 if (*s == '\0') 377 return len; 378 ++s; 379 } 380 /* NOTREACHED */ 381 } 382 383 int 384 __pcap_atodn(const char *s, bpf_u_int32 *addr) 385 { 386 #define AREASHIFT 10 387 #define AREAMASK 0176000 388 #define NODEMASK 01777 389 390 u_int node, area; 391 392 if (sscanf(s, "%d.%d", &area, &node) != 2) 393 bpf_error("malformed decnet address '%s'", s); 394 395 *addr = (area << AREASHIFT) & AREAMASK; 396 *addr |= (node & NODEMASK); 397 398 return(32); 399 } 400 401 /* 402 * Convert 's', which can have the one of the forms: 403 * 404 * "xx:xx:xx:xx:xx:xx" 405 * "xx.xx.xx.xx.xx.xx" 406 * "xx-xx-xx-xx-xx-xx" 407 * "xxxx.xxxx.xxxx" 408 * "xxxxxxxxxxxx" 409 * 410 * (or various mixes of ':', '.', and '-') into a new 411 * ethernet address. Assumes 's' is well formed. 412 */ 413 u_char * 414 pcap_ether_aton(const char *s) 415 { 416 register u_char *ep, *e; 417 register u_int d; 418 419 e = ep = (u_char *)malloc(6); 420 if (e == NULL) 421 return (NULL); 422 423 while (*s) { 424 if (*s == ':' || *s == '.' || *s == '-') 425 s += 1; 426 d = xdtoi(*s++); 427 if (isxdigit((unsigned char)*s)) { 428 d <<= 4; 429 d |= xdtoi(*s++); 430 } 431 *ep++ = d; 432 } 433 434 return (e); 435 } 436 437 #ifndef HAVE_ETHER_HOSTTON 438 /* Roll our own */ 439 u_char * 440 pcap_ether_hostton(const char *name) 441 { 442 register struct pcap_etherent *ep; 443 register u_char *ap; 444 static FILE *fp = NULL; 445 static int init = 0; 446 447 if (!init) { 448 fp = fopen(PCAP_ETHERS_FILE, "r"); 449 ++init; 450 if (fp == NULL) 451 return (NULL); 452 } else if (fp == NULL) 453 return (NULL); 454 else 455 rewind(fp); 456 457 while ((ep = pcap_next_etherent(fp)) != NULL) { 458 if (strcmp(ep->name, name) == 0) { 459 ap = (u_char *)malloc(6); 460 if (ap != NULL) { 461 memcpy(ap, ep->addr, 6); 462 return (ap); 463 } 464 break; 465 } 466 } 467 return (NULL); 468 } 469 #else 470 471 #if !defined(HAVE_DECL_ETHER_HOSTTON) || !HAVE_DECL_ETHER_HOSTTON 472 #ifndef HAVE_STRUCT_ETHER_ADDR 473 struct ether_addr { 474 unsigned char ether_addr_octet[6]; 475 }; 476 #endif 477 extern int ether_hostton(const char *, struct ether_addr *); 478 #endif 479 480 /* Use the os supplied routines */ 481 u_char * 482 pcap_ether_hostton(const char *name) 483 { 484 register u_char *ap; 485 u_char a[6]; 486 487 ap = NULL; 488 if (ether_hostton(name, (struct ether_addr *)a) == 0) { 489 ap = (u_char *)malloc(6); 490 if (ap != NULL) 491 memcpy((char *)ap, (char *)a, 6); 492 } 493 return (ap); 494 } 495 #endif 496 497 u_short 498 __pcap_nametodnaddr(const char *name) 499 { 500 #ifdef DECNETLIB 501 struct nodeent *getnodebyname(); 502 struct nodeent *nep; 503 unsigned short res; 504 505 nep = getnodebyname(name); 506 if (nep == ((struct nodeent *)0)) 507 bpf_error("unknown decnet host name '%s'\n", name); 508 509 memcpy((char *)&res, (char *)nep->n_addr, sizeof(unsigned short)); 510 return(res); 511 #else 512 bpf_error("decnet name support not included, '%s' cannot be translated\n", 513 name); 514 return(0); 515 #endif 516 } 517