1 /* From NetBSD: nametoaddr.c,v 1.3 1995/04/29 05:42:23 cgd Exp */
4 * Copyright (c) 1990, 1991, 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
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
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.
23 * Name to id translation routines used by the scanner.
24 * These functions are not time critical.
29 "@(#) Header: nametoaddr.c,v 1.21 94/06/20 19:07:54 leres Exp (LBL)";
40 #include <sys/param.h>
41 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
45 #include <net/ppp_defs.h>
47 #include "bpf_compile.h"
55 #define NTOHL(x) (x) = ntohl(x)
56 #define NTOHS(x) (x) = ntohs(x)
60 * Convert host name to internet address.
61 * Return 0 upon failure.
64 pcap_nametoaddr(const char *name)
67 static u_long *hlist[2];
72 if ((hp = gethostbyname(name)) != NULL) {
74 hlist[0] = (u_long *)hp->h_addr;
78 for (p = (u_long **)hp->h_addr_list; *p; ++p)
80 return (u_long **)hp->h_addr_list;
88 * Convert net name to internet address.
89 * Return 0 upon failure.
92 pcap_nametonetaddr(const char *name)
96 if ((np = getnetbyname(name)) != NULL)
103 * Convert a port name to its port and protocol numbers.
104 * We assume only TCP or UDP.
105 * Return 0 upon failure.
108 pcap_nametoport(const char *name, int *port, int *proto)
113 sp = getservbyname(name, (char *)0);
117 *proto = pcap_nametoproto(sp->s_proto);
119 * We need to check /etc/services for ambiguous entries.
120 * If we find the ambiguous entry, and it has the
121 * same port number, change the proto to PROTO_UNDEF
122 * so both TCP and UDP will be checked.
124 if (*proto == IPPROTO_TCP)
129 sp = getservbyname(name, other);
132 if (*port != sp->s_port)
133 /* Can't handle ambiguous names that refer
134 to different port numbers. */
136 warning("ambiguous port %s in /etc/services",
141 *proto = PROTO_UNDEF;
145 #if defined(ultrix) || defined(__osf__)
146 /* Special hack in case NFS isn't in /etc/services */
147 if (strcmp(name, "nfs") == 0) {
149 *proto = PROTO_UNDEF;
157 pcap_nametoproto(const char *str)
161 p = getprotobyname(str);
169 __pcap_atoin(const char *s)
176 while (*s && *s != '.')
177 n = n * 10 + *s++ - '0';
192 /* Static data base of PPP protocol types. */
193 struct pppproto pppproto_db[] = {
199 pcap_nametopppproto(const char *s)
201 struct pppproto *p = pppproto_db;
204 if (strcmp(p->s, s) == 0)