1 /* $OpenBSD: nametoaddr.c,v 1.24 2021/12/01 18:28:45 deraadt Exp $ */ 2 3 /* 4 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996 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 #include <sys/types.h> /* concession to AIX */ 28 #include <sys/socket.h> 29 #include <sys/time.h> 30 31 #include <net/if.h> 32 #include <netinet/in.h> 33 #include <netinet/if_ether.h> 34 #include <arpa/inet.h> 35 36 #include <ctype.h> 37 #include <errno.h> 38 #include <stdlib.h> 39 #include <netdb.h> 40 #include <stdio.h> 41 #include <string.h> 42 43 #include "pcap-int.h" 44 45 #include "gencode.h" 46 #include <pcap-namedb.h> 47 48 #ifdef HAVE_OS_PROTO_H 49 #include "os-proto.h" 50 #endif 51 52 #ifndef NTOHL 53 #define NTOHL(x) (x) = ntohl(x) 54 #define NTOHS(x) (x) = ntohs(x) 55 #endif 56 57 static __inline int xdtoi(int); 58 59 /* 60 * Convert host name to internet address. 61 * Return 0 upon failure. 62 */ 63 bpf_u_int32 ** 64 pcap_nametoaddr(const char *name) 65 { 66 #ifndef h_addr 67 static bpf_u_int32 *hlist[2]; 68 #endif 69 bpf_u_int32 **p; 70 struct hostent *hp; 71 72 if ((hp = gethostbyname(name)) != NULL) { 73 #ifndef h_addr 74 hlist[0] = (bpf_u_int32 *)hp->h_addr; 75 NTOHL(hp->h_addr); 76 return hlist; 77 #else 78 for (p = (bpf_u_int32 **)hp->h_addr_list; *p; ++p) 79 NTOHL(**p); 80 return (bpf_u_int32 **)hp->h_addr_list; 81 #endif 82 } 83 else 84 return 0; 85 } 86 87 #ifdef INET6 88 struct addrinfo * 89 pcap_nametoaddrinfo(const char *name) 90 { 91 struct addrinfo hints, *res; 92 int error; 93 94 memset(&hints, 0, sizeof(hints)); 95 hints.ai_family = PF_UNSPEC; 96 hints.ai_socktype = SOCK_STREAM; /*not really*/ 97 error = getaddrinfo(name, NULL, &hints, &res); 98 if (error) 99 return NULL; 100 else 101 return res; 102 } 103 #endif /*INET6*/ 104 105 /* 106 * Convert net name to internet address. 107 * Return 0 upon failure. 108 */ 109 bpf_u_int32 110 pcap_nametonetaddr(const char *name) 111 { 112 struct netent *np; 113 114 if ((np = getnetbyname(name)) != NULL) 115 return np->n_net; 116 else 117 return 0; 118 } 119 120 /* 121 * Convert a port name to its port and protocol numbers. 122 * We assume only TCP or UDP. 123 * Return 0 upon failure. 124 */ 125 int 126 pcap_nametoport(const char *name, int *port, int *proto) 127 { 128 struct servent *sp; 129 char *other; 130 131 sp = getservbyname(name, (char *)0); 132 if (sp != NULL) { 133 NTOHS(sp->s_port); 134 *port = sp->s_port; 135 *proto = pcap_nametoproto(sp->s_proto); 136 /* 137 * We need to check /etc/services for ambiguous entries. 138 * If we find the ambiguous entry, and it has the 139 * same port number, change the proto to PROTO_UNDEF 140 * so both TCP and UDP will be checked. 141 */ 142 if (*proto == IPPROTO_TCP) 143 other = "udp"; 144 else 145 other = "tcp"; 146 147 sp = getservbyname(name, other); 148 if (sp != 0) { 149 NTOHS(sp->s_port); 150 #ifdef notdef 151 if (*port != sp->s_port) 152 /* Can't handle ambiguous names that refer 153 to different port numbers. */ 154 warning("ambiguous port %s in /etc/services", 155 name); 156 #endif 157 *proto = PROTO_UNDEF; 158 } 159 return 1; 160 } 161 #if defined(ultrix) || defined(__osf__) 162 /* Special hack in case NFS isn't in /etc/services */ 163 if (strcmp(name, "nfs") == 0) { 164 *port = 2049; 165 *proto = PROTO_UNDEF; 166 return 1; 167 } 168 #endif 169 return 0; 170 } 171 172 int 173 pcap_nametoproto(const char *str) 174 { 175 struct protoent *p; 176 177 p = getprotobyname(str); 178 if (p != 0) 179 return p->p_proto; 180 else 181 return PROTO_UNDEF; 182 } 183 184 #include "ethertype.h" 185 186 struct eproto { 187 char *s; 188 u_short p; 189 }; 190 191 /* Static data base of ether protocol types. */ 192 static const struct eproto _eproto_db[] = { 193 { "pup", ETHERTYPE_PUP }, 194 { "xns", ETHERTYPE_NS }, 195 { "ip", ETHERTYPE_IP }, 196 #ifdef INET6 197 { "ip6", ETHERTYPE_IPV6 }, 198 #endif 199 { "arp", ETHERTYPE_ARP }, 200 { "rarp", ETHERTYPE_REVARP }, 201 { "lldp", ETHERTYPE_LLDP }, 202 { "slow", ETHERTYPE_SLOW }, 203 { "sprite", ETHERTYPE_SPRITE }, 204 { "mopdl", ETHERTYPE_MOPDL }, 205 { "moprc", ETHERTYPE_MOPRC }, 206 { "decnet", ETHERTYPE_DN }, 207 { "lat", ETHERTYPE_LAT }, 208 { "sca", ETHERTYPE_SCA }, 209 { "lanbridge", ETHERTYPE_LANBRIDGE }, 210 { "vexp", ETHERTYPE_VEXP }, 211 { "vprod", ETHERTYPE_VPROD }, 212 { "atalk", ETHERTYPE_ATALK }, 213 { "atalkarp", ETHERTYPE_AARP }, 214 { "loopback", ETHERTYPE_LOOPBACK }, 215 { "decdts", ETHERTYPE_DECDTS }, 216 { "decdns", ETHERTYPE_DECDNS }, 217 { (char *)0, 0 } 218 }; 219 /* Accessor for tcpdump */ 220 const struct eproto * const eproto_db = _eproto_db; 221 222 int 223 pcap_nametoeproto(const char *s) 224 { 225 const struct eproto *p = _eproto_db; 226 227 while (p->s != 0) { 228 if (strcmp(p->s, s) == 0) 229 return p->p; 230 p += 1; 231 } 232 return PROTO_UNDEF; 233 } 234 235 #include "llc.h" 236 237 /* Static data base of LLC values. */ 238 static const struct eproto llc_db[] = { 239 { "stp", LLCSAP_8021D }, 240 { (char *)0, 0 } 241 }; 242 243 int 244 pcap_nametollc(const char *s) 245 { 246 const struct eproto *p = llc_db; 247 248 while (p->s != 0) { 249 if (strcmp(p->s, s) == 0) 250 return p->p; 251 p += 1; 252 } 253 return PROTO_UNDEF; 254 } 255 256 /* Hex digit to integer. */ 257 static __inline int 258 xdtoi(c) 259 int c; 260 { 261 if (isdigit(c)) 262 return c - '0'; 263 else if (islower(c)) 264 return c - 'a' + 10; 265 else 266 return c - 'A' + 10; 267 } 268 269 int 270 __pcap_atoin(const char *s, bpf_u_int32 *addr) 271 { 272 u_int n; 273 int len; 274 275 *addr = 0; 276 len = 0; 277 while (1) { 278 n = 0; 279 while (*s && *s != '.') 280 n = n * 10 + *s++ - '0'; 281 *addr <<= 8; 282 *addr |= n & 0xff; 283 len += 8; 284 if (*s == '\0') 285 return len; 286 ++s; 287 } 288 /* NOTREACHED */ 289 } 290 291 int 292 __pcap_atodn(const char *s, bpf_u_int32 *addr) 293 { 294 #define AREASHIFT 10 295 #define AREAMASK 0176000 296 #define NODEMASK 01777 297 298 u_int node, area; 299 300 if (sscanf((char *)s, "%d.%d", &area, &node) != 2) 301 bpf_error("malformed decnet address '%s'", s); 302 303 *addr = (area << AREASHIFT) & AREAMASK; 304 *addr |= (node & NODEMASK); 305 306 return(32); 307 } 308 309 /* 310 * Convert 's' which has the form "xx:xx:xx:xx:xx:xx" into a new 311 * ethernet address. Assumes 's' is well formed. 312 */ 313 u_char * 314 pcap_ether_aton(const char *s) 315 { 316 u_char *ep, *e; 317 u_int d; 318 319 e = ep = malloc(6); 320 if (e == NULL) 321 bpf_error("malloc"); 322 323 while (*s) { 324 if (*s == ':') 325 s += 1; 326 d = xdtoi(*s++); 327 if (isxdigit((unsigned char)*s)) { 328 d <<= 4; 329 d |= xdtoi(*s++); 330 } 331 *ep++ = d; 332 } 333 334 return (e); 335 } 336 337 #ifndef HAVE_ETHER_HOSTTON 338 /* Roll our own */ 339 u_char * 340 pcap_ether_hostton(const char *name) 341 { 342 struct pcap_etherent *ep; 343 u_char *ap; 344 static FILE *fp = NULL; 345 static init = 0; 346 347 if (!init) { 348 fp = fopen(PCAP_ETHERS_FILE, "r"); 349 ++init; 350 if (fp == NULL) 351 return (NULL); 352 } else if (fp == NULL) 353 return (NULL); 354 else 355 rewind(fp); 356 357 while ((ep = pcap_next_etherent(fp)) != NULL) { 358 if (strcmp(ep->name, name) == 0) { 359 ap = malloc(6); 360 if (ap != NULL) { 361 memcpy(ap, ep->addr, 6); 362 return (ap); 363 } 364 break; 365 } 366 } 367 return (NULL); 368 } 369 #else 370 371 /* Use the os supplied routines */ 372 u_char * 373 pcap_ether_hostton(const char *name) 374 { 375 u_char *ap; 376 u_char a[6]; 377 378 ap = NULL; 379 if (ether_hostton(name, (struct ether_addr *)a) == 0) { 380 ap = malloc(6); 381 if (ap != NULL) 382 memcpy((char *)ap, (char *)a, 6); 383 } 384 return (ap); 385 } 386 #endif 387 388 u_short 389 __pcap_nametodnaddr(const char *name) 390 { 391 #ifdef DECNETLIB 392 struct nodeent *getnodebyname(); 393 struct nodeent *nep; 394 unsigned short res; 395 396 nep = getnodebyname(name); 397 if (nep == ((struct nodeent *)0)) 398 bpf_error("unknown decnet host name '%s'\n", name); 399 400 memcpy((char *)&res, (char *)nep->n_addr, sizeof(unsigned short)); 401 return(res); 402 #else 403 bpf_error("decnet name support not included, '%s' cannot be translated\n", 404 name); 405 /* NOTREACHED */ 406 #endif 407 } 408