]> git.ozlabs.org Git - ppp.git/blob - pppd/nametoaddr.c
took out bpf filtering stuff
[ppp.git] / pppd / nametoaddr.c
1 /*      From NetBSD: nametoaddr.c,v 1.3 1995/04/29 05:42:23 cgd Exp */
2
3 /*
4  * Copyright (c) 1990, 1991, 1992, 1993, 1994
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 char rcsid[] =
29     "@(#) Header: nametoaddr.c,v 1.21 94/06/20 19:07:54 leres Exp (LBL)";
30 #endif
31
32 #include <stdio.h>
33 #ifdef __NetBSD__
34 #include <stdlib.h>
35 #include <string.h>
36 #endif
37 #include <ctype.h>
38 #include <errno.h>
39 #include <netdb.h>
40 #include <sys/param.h>
41 #include <sys/socket.h>
42 #include <net/if.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
45 #include <net/ppp_defs.h>
46
47 #include "bpf_compile.h"
48 #include "gencode.h"
49
50 #ifndef __GNUC__
51 #define inline
52 #endif
53
54 #ifndef NTOHL
55 #define NTOHL(x) (x) = ntohl(x)
56 #define NTOHS(x) (x) = ntohs(x)
57 #endif
58
59 /*
60  *  Convert host name to internet address.
61  *  Return 0 upon failure.
62  */
63 u_long **
64 pcap_nametoaddr(const char *name)
65 {
66 #ifndef h_addr
67         static u_long *hlist[2];
68 #endif
69         u_long **p;
70         struct hostent *hp;
71
72         if ((hp = gethostbyname(name)) != NULL) {
73 #ifndef h_addr
74                 hlist[0] = (u_long *)hp->h_addr;
75                 NTOHL(hp->h_addr);
76                 return hlist;
77 #else
78                 for (p = (u_long **)hp->h_addr_list; *p; ++p)
79                         NTOHL(**p);
80                 return (u_long **)hp->h_addr_list;
81 #endif
82         }
83         else
84                 return 0;
85 }
86
87 /*
88  *  Convert net name to internet address.
89  *  Return 0 upon failure.
90  */
91 u_long
92 pcap_nametonetaddr(const char *name)
93 {
94         struct netent *np;
95
96         if ((np = getnetbyname(name)) != NULL)
97                 return np->n_net;
98         else
99                 return 0;
100 }
101
102 /*
103  * Convert a port name to its port and protocol numbers.
104  * We assume only TCP or UDP.
105  * Return 0 upon failure.
106  */
107 int
108 pcap_nametoport(const char *name, int *port, int *proto)
109 {
110         struct servent *sp;
111         char *other;
112
113         sp = getservbyname(name, (char *)0);
114         if (sp != NULL) {
115                 NTOHS(sp->s_port);
116                 *port = sp->s_port;
117                 *proto = pcap_nametoproto(sp->s_proto);
118                 /*
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.
123                  */
124                 if (*proto == IPPROTO_TCP)
125                         other = "udp";
126                 else
127                         other = "tcp";
128
129                 sp = getservbyname(name, other);
130                 if (sp != 0) {
131                         NTOHS(sp->s_port);
132                         if (*port != sp->s_port)
133                                 /* Can't handle ambiguous names that refer
134                                    to different port numbers. */
135 #ifdef notdef
136                                 warning("ambiguous port %s in /etc/services",
137                                         name);
138 #else
139                         ;
140 #endif
141                         *proto = PROTO_UNDEF;
142                 }
143                 return 1;
144         }
145 #if defined(ultrix) || defined(__osf__)
146         /* Special hack in case NFS isn't in /etc/services */
147         if (strcmp(name, "nfs") == 0) {
148                 *port = 2049;
149                 *proto = PROTO_UNDEF;
150                 return 1;
151         }
152 #endif
153         return 0;
154 }
155
156 int
157 pcap_nametoproto(const char *str)
158 {
159         struct protoent *p;
160
161         p = getprotobyname(str);
162         if (p != 0)
163                 return p->p_proto;
164         else
165                 return PROTO_UNDEF;
166 }
167
168 u_long
169 __pcap_atoin(const char *s)
170 {
171         u_long addr = 0;
172         u_int n;
173
174         while (1) {
175                 n = 0;
176                 while (*s && *s != '.')
177                         n = n * 10 + *s++ - '0';
178                 addr <<= 8;
179                 addr |= n & 0xff;
180                 if (*s == '\0')
181                         return addr;
182                 ++s;
183         }
184         /* NOTREACHED */
185 }
186
187 struct pppproto {
188         char *s;
189         u_short p;
190 };
191
192 /* Static data base of PPP protocol types. */
193 struct pppproto pppproto_db[] = {
194         { "ip", PPP_IP },
195         { (char *)0, 0 }
196 };
197
198 int
199 pcap_nametopppproto(const char *s)
200 {
201         struct pppproto *p = pppproto_db;
202
203         while (p->s != 0) {
204                 if (strcmp(p->s, s) == 0)
205                         return p->p;
206                 p += 1;
207         }
208         return PROTO_UNDEF;
209 }