diff options
author | deraadt <> | 1995-10-18 08:42:23 +0000 |
---|---|---|
committer | deraadt <> | 1995-10-18 08:42:23 +0000 |
commit | 0527d29da443886d92e9a418180c5b25a5f8d270 (patch) | |
tree | 86b3a64928451a669cefa27900e5884036b4e349 /src/lib/libc/net | |
download | openbsd-0527d29da443886d92e9a418180c5b25a5f8d270.tar.gz openbsd-0527d29da443886d92e9a418180c5b25a5f8d270.tar.bz2 openbsd-0527d29da443886d92e9a418180c5b25a5f8d270.zip |
initial import of NetBSD tree
Diffstat (limited to 'src/lib/libc/net')
49 files changed, 8313 insertions, 0 deletions
diff --git a/src/lib/libc/net/Makefile.inc b/src/lib/libc/net/Makefile.inc new file mode 100644 index 0000000000..2d220067e4 --- /dev/null +++ b/src/lib/libc/net/Makefile.inc | |||
@@ -0,0 +1,46 @@ | |||
1 | # $NetBSD: Makefile.inc,v 1.23 1995/03/02 09:09:07 chopps Exp $ | ||
2 | # @(#)Makefile.inc 8.2 (Berkeley) 9/5/93 | ||
3 | |||
4 | # net sources | ||
5 | .PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/net ${.CURDIR}/net | ||
6 | |||
7 | SRCS+= gethostnamadr.c getnetbyaddr.c getnetbyname.c getnetent.c \ | ||
8 | getproto.c getprotoent.c getprotoname.c getservbyname.c \ | ||
9 | getservbyport.c getservent.c herror.c inet_addr.c inet_lnaof.c \ | ||
10 | inet_makeaddr.c inet_netof.c inet_network.c inet_ntoa.c \ | ||
11 | iso_addr.c linkaddr.c ns_addr.c ns_ntoa.c rcmd.c recv.c res_comp.c \ | ||
12 | res_debug.c res_init.c res_mkquery.c res_query.c res_send.c \ | ||
13 | send.c sethostent.c ethers.c | ||
14 | |||
15 | # machine-dependent net sources | ||
16 | # m-d Makefile.inc must include sources for: | ||
17 | # htonl() htons() ntohl() ntohs() | ||
18 | |||
19 | .include "${.CURDIR}/arch/${MACHINE_ARCH}/net/Makefile.inc" | ||
20 | |||
21 | MAN+= byteorder.3 gethostbyname.3 getnetent.3 getprotoent.3 getservent.3 \ | ||
22 | inet.3 linkaddr.3 ns.3 rcmd.3 resolver.3 ethers.3 | ||
23 | |||
24 | MLINKS+=byteorder.3 htonl.3 byteorder.3 htons.3 byteorder.3 ntohl.3 \ | ||
25 | byteorder.3 ntohs.3 | ||
26 | MLINKS+=ethers.3 ether_aton.3 ethers.3 ether_hostton.3 ethers.3 ether_line.3 \ | ||
27 | ethers.3 ether_ntoa.3 ethers.3 ether_ntohost.3 | ||
28 | MLINKS+=gethostbyname.3 endhostent.3 gethostbyname.3 gethostbyaddr.3 \ | ||
29 | gethostbyname.3 sethostent.3 gethostbyname.3 gethostent.3 \ | ||
30 | gethostbyname.3 herror.3 | ||
31 | MLINKS+=getnetent.3 endnetent.3 getnetent.3 getnetbyaddr.3 \ | ||
32 | getnetent.3 getnetbyname.3 getnetent.3 setnetent.3 | ||
33 | MLINKS+=getprotoent.3 endprotoent.3 getprotoent.3 getprotobyname.3 \ | ||
34 | getprotoent.3 getprotobynumber.3 getprotoent.3 setprotoent.3 | ||
35 | MLINKS+=getservent.3 endservent.3 getservent.3 getservbyname.3 \ | ||
36 | getservent.3 getservbyport.3 getservent.3 setservent.3 | ||
37 | MLINKS+=inet.3 addr.3 inet.3 inet_addr.3 inet.3 inet_aton.3 \ | ||
38 | inet.3 inet_lnaof.3 inet.3 inet_makeaddr.3 inet.3 inet_netof.3 \ | ||
39 | inet.3 inet_network.3 inet.3 inet_ntoa.3 inet.3 network.3 \ | ||
40 | inet.3 ntoa.3 | ||
41 | MLINKS+=linkaddr.3 linkntoa.3 | ||
42 | MLINKS+=ns.3 ns_addr.3 ns.3 ns_ntoa.3 | ||
43 | MLINKS+=rcmd.3 iruserok.3 rcmd.3 rresvport.3 rcmd.3 ruserok.3 | ||
44 | MLINKS+=resolver.3 dn_comp.3 resolver.3 dn_expand.3 resolver.3 res_init.3 \ | ||
45 | resolver.3 res_mkquery.3 resolver.3 res_send.3 resolver.3 res_query.3 \ | ||
46 | resolver.3 res_search.3 | ||
diff --git a/src/lib/libc/net/byteorder.3 b/src/lib/libc/net/byteorder.3 new file mode 100644 index 0000000000..701a69f688 --- /dev/null +++ b/src/lib/libc/net/byteorder.3 | |||
@@ -0,0 +1,78 @@ | |||
1 | .\" $NetBSD: byteorder.3,v 1.3 1995/02/25 06:20:27 cgd Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1983, 1991, 1993 | ||
4 | .\" The Regents of the University of California. All rights reserved. | ||
5 | .\" | ||
6 | .\" Redistribution and use in source and binary forms, with or without | ||
7 | .\" modification, are permitted provided that the following conditions | ||
8 | .\" are met: | ||
9 | .\" 1. Redistributions of source code must retain the above copyright | ||
10 | .\" notice, this list of conditions and the following disclaimer. | ||
11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
12 | .\" notice, this list of conditions and the following disclaimer in the | ||
13 | .\" documentation and/or other materials provided with the distribution. | ||
14 | .\" 3. All advertising materials mentioning features or use of this software | ||
15 | .\" must display the following acknowledgement: | ||
16 | .\" This product includes software developed by the University of | ||
17 | .\" California, Berkeley and its contributors. | ||
18 | .\" 4. Neither the name of the University nor the names of its contributors | ||
19 | .\" may be used to endorse or promote products derived from this software | ||
20 | .\" without specific prior written permission. | ||
21 | .\" | ||
22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
32 | .\" SUCH DAMAGE. | ||
33 | .\" | ||
34 | .\" @(#)byteorder.3 8.1 (Berkeley) 6/4/93 | ||
35 | .\" | ||
36 | .Dd June 4, 1993 | ||
37 | .Dt BYTEORDER 3 | ||
38 | .Os BSD 4.2 | ||
39 | .Sh NAME | ||
40 | .Nm htonl , | ||
41 | .Nm htons , | ||
42 | .Nm ntohl , | ||
43 | .Nm ntohs | ||
44 | .Nd convert values between host and network byte order | ||
45 | .Sh SYNOPSIS | ||
46 | .Fd #include <sys/param.h> | ||
47 | .Ft u_long | ||
48 | .Fn htonl "u_long hostlong" | ||
49 | .Ft u_short | ||
50 | .Fn htons "u_short hostshort" | ||
51 | .Ft u_long | ||
52 | .Fn ntohl "u_long netlong" | ||
53 | .Ft u_short | ||
54 | .Fn ntohs "u_short netshort" | ||
55 | .Sh DESCRIPTION | ||
56 | These routines convert 16 and 32 bit quantities between network | ||
57 | byte order and host byte order. | ||
58 | On machines which have a byte order which is the same as the network | ||
59 | order, routines are defined as null macros. | ||
60 | .Pp | ||
61 | These routines are most often used in conjunction with Internet | ||
62 | addresses and ports as returned by | ||
63 | .Xr gethostbyname 3 | ||
64 | and | ||
65 | .Xr getservent 3 . | ||
66 | .Sh SEE ALSO | ||
67 | .Xr gethostbyname 3 , | ||
68 | .Xr getservent 3 | ||
69 | .Sh HISTORY | ||
70 | The | ||
71 | .Nm byteorder | ||
72 | functions appeared in | ||
73 | .Bx 4.2 . | ||
74 | .Sh BUGS | ||
75 | On the | ||
76 | .Tn VAX | ||
77 | bytes are handled backwards from most everyone else in | ||
78 | the world. This is not expected to be fixed in the near future. | ||
diff --git a/src/lib/libc/net/ethers.3 b/src/lib/libc/net/ethers.3 new file mode 100644 index 0000000000..81e6c65935 --- /dev/null +++ b/src/lib/libc/net/ethers.3 | |||
@@ -0,0 +1,102 @@ | |||
1 | .\" | ||
2 | .\" Written by roland@frob.com. Public domain. | ||
3 | .\" | ||
4 | .Dd December 16, 1993 | ||
5 | .Dt ETHERS 3 | ||
6 | .Os NetBSD | ||
7 | .Sh NAME | ||
8 | .Nm ether_ntoa , | ||
9 | .Nm ether_addr , | ||
10 | .Nm ether_ntohost , | ||
11 | .Nm ether_hostton , | ||
12 | .Nm ether_line , | ||
13 | .Nd get ethers entry | ||
14 | .Sh SYNOPSIS | ||
15 | .Fd #include <netinet/if_ether.h> | ||
16 | .Ft char * | ||
17 | .Fn ether_ntoa "struct ether_addr *e" | ||
18 | .Ft struct ether_addr * | ||
19 | .Fn ether_aton "char *s" | ||
20 | .Fn ether_ntohost "char *hostname" "struct ether_addr *e" | ||
21 | .Fn ether_hostton "char *hostname" "struct ether_addr *e" | ||
22 | .Fn ether_line "char *l" "struct ether_addr *e" "char *hostname" | ||
23 | .Sh DESCRIPTION | ||
24 | Ethernet addresses are represented by the | ||
25 | following structure: | ||
26 | .Bd -literal -offset indent | ||
27 | struct ether_addr { | ||
28 | u_char ether_addr_octet[6]; | ||
29 | }; | ||
30 | .Ed | ||
31 | .Pp | ||
32 | The | ||
33 | .Fn ether_ntoa | ||
34 | function converts this structure into an ASCII string of the form | ||
35 | ``xx:xx:xx:xx:xx:xx'', consisting of 6 hexadecimal numbers separated | ||
36 | by colons. It returns a pointer to a static buffer that is reused for | ||
37 | each call. | ||
38 | The | ||
39 | .Fn ether_aton | ||
40 | converts an ASCII string of the same form and to a structure | ||
41 | containing the 6 octets of the address. It returns a pointer to a | ||
42 | static structure that is reused for each call. | ||
43 | .Pp | ||
44 | The | ||
45 | .Fn ether_ntohost | ||
46 | and | ||
47 | .Fn ether_hostton | ||
48 | functions interrogate the data base mapping host names to Ethernet | ||
49 | addresses, | ||
50 | .Pa /etc/ethers . | ||
51 | The | ||
52 | .Fn ether_ntohost | ||
53 | function looks up the given Ethernet address and writes the associated | ||
54 | host name into the character buffer passed. | ||
55 | The | ||
56 | .Fn ether_hostton | ||
57 | function looks up the given host name and writes the associated | ||
58 | Ethernet address into the structure passed. Both functions return | ||
59 | zero if they find the requested host name or address, and -1 if not. | ||
60 | Each call reads | ||
61 | .Pa /etc/ethers | ||
62 | from the beginning; if a + appears alone on a line in the file, then | ||
63 | .Fn ether_hostton | ||
64 | will consult the | ||
65 | .Pa ethers.byname | ||
66 | YP map, and | ||
67 | .Fn ether_ntohost | ||
68 | will consult the | ||
69 | .Pa ethers.byaddr | ||
70 | YP map. | ||
71 | .Pp | ||
72 | The | ||
73 | .Fn ether_line | ||
74 | function parses a line from the | ||
75 | .Pa /etc/ethers | ||
76 | file and fills in the passed ``struct ether_addr'' and character | ||
77 | buffer with the Ethernet address and host name on the line. It | ||
78 | returns zero if the line was successfully parsed and -1 if not. | ||
79 | .Sh FILES | ||
80 | .Bl -tag -width /etc/ethers -compact | ||
81 | .It Pa /etc/ethers | ||
82 | .El | ||
83 | .Sh SEE ALSO | ||
84 | .Xr ethers 5 | ||
85 | .Sh HISTORY | ||
86 | The | ||
87 | .Fn ether_ntoa , | ||
88 | .Fn ether_aton , | ||
89 | .Fn ether_ntohost , | ||
90 | .Fn ether_hostton , | ||
91 | and | ||
92 | .Fn ether_line | ||
93 | functions were adopted from SunOS and appeared in | ||
94 | NetBSD 0.9b. | ||
95 | .Sh BUGS | ||
96 | The data space used by these functions is static; if future use | ||
97 | requires the data, it should be copied before any subsequent calls to | ||
98 | these functions overwrite it. There is no way to restrict how many | ||
99 | character will be written into the host name buffer passed. A very | ||
100 | long line in | ||
101 | .Pa /etc/ethers | ||
102 | could overflow your buffer. | ||
diff --git a/src/lib/libc/net/ethers.c b/src/lib/libc/net/ethers.c new file mode 100644 index 0000000000..0f32b9b71b --- /dev/null +++ b/src/lib/libc/net/ethers.c | |||
@@ -0,0 +1,178 @@ | |||
1 | /* $NetBSD: ethers.c,v 1.5 1995/02/25 06:20:28 cgd Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * ethers(3N) a la Sun. | ||
5 | * | ||
6 | * Written by Roland McGrath <roland@frob.com> 10/14/93. | ||
7 | * Public domain. | ||
8 | */ | ||
9 | |||
10 | #include <sys/types.h> | ||
11 | #include <sys/socket.h> | ||
12 | #include <net/if.h> | ||
13 | #include <netinet/in.h> | ||
14 | #include <netinet/if_ether.h> | ||
15 | #include <sys/param.h> | ||
16 | #include <paths.h> | ||
17 | #include <errno.h> | ||
18 | #include <stdio.h> | ||
19 | #include <stdlib.h> | ||
20 | #include <string.h> | ||
21 | |||
22 | #ifndef _PATH_ETHERS | ||
23 | #define _PATH_ETHERS "/etc/ethers" | ||
24 | #endif | ||
25 | |||
26 | char * | ||
27 | ether_ntoa(e) | ||
28 | struct ether_addr *e; | ||
29 | { | ||
30 | static char a[] = "xx:xx:xx:xx:xx:xx"; | ||
31 | |||
32 | sprintf(a, "%02x:%02x:%02x:%02x:%02x:%02x", | ||
33 | e->ether_addr_octet[0], e->ether_addr_octet[1], | ||
34 | e->ether_addr_octet[2], e->ether_addr_octet[3], | ||
35 | e->ether_addr_octet[4], e->ether_addr_octet[5]); | ||
36 | return a; | ||
37 | } | ||
38 | |||
39 | struct ether_addr * | ||
40 | ether_aton(s) | ||
41 | char *s; | ||
42 | { | ||
43 | static struct ether_addr n; | ||
44 | u_int i[6]; | ||
45 | |||
46 | if (sscanf(s, " %x:%x:%x:%x:%x:%x ", &i[0], &i[1], | ||
47 | &i[2], &i[3], &i[4], &i[5]) == 6) { | ||
48 | n.ether_addr_octet[0] = (u_char)i[0]; | ||
49 | n.ether_addr_octet[1] = (u_char)i[1]; | ||
50 | n.ether_addr_octet[2] = (u_char)i[2]; | ||
51 | n.ether_addr_octet[3] = (u_char)i[3]; | ||
52 | n.ether_addr_octet[4] = (u_char)i[4]; | ||
53 | n.ether_addr_octet[5] = (u_char)i[5]; | ||
54 | return &n; | ||
55 | } | ||
56 | return NULL; | ||
57 | } | ||
58 | |||
59 | ether_ntohost(hostname, e) | ||
60 | char *hostname; | ||
61 | struct ether_addr *e; | ||
62 | { | ||
63 | FILE *f; | ||
64 | char buf[BUFSIZ]; | ||
65 | struct ether_addr try; | ||
66 | |||
67 | #ifdef YP | ||
68 | char trybuf[sizeof "xx:xx:xx:xx:xx:xx"]; | ||
69 | int trylen; | ||
70 | |||
71 | sprintf(trybuf, "%x:%x:%x:%x:%x:%x", | ||
72 | e->ether_addr_octet[0], e->ether_addr_octet[1], | ||
73 | e->ether_addr_octet[2], e->ether_addr_octet[3], | ||
74 | e->ether_addr_octet[4], e->ether_addr_octet[5]); | ||
75 | trylen = strlen(trybuf); | ||
76 | #endif | ||
77 | |||
78 | f = fopen(_PATH_ETHERS, "r"); | ||
79 | if (f==NULL) | ||
80 | return -1; | ||
81 | while (fgets(buf, sizeof buf, f)) { | ||
82 | #ifdef YP | ||
83 | /* A + in the file means try YP now. */ | ||
84 | if (!strncmp(buf, "+\n", sizeof buf)) { | ||
85 | char *ypbuf, *ypdom; | ||
86 | int ypbuflen; | ||
87 | |||
88 | if (yp_get_default_domain(&ypdom)) | ||
89 | continue; | ||
90 | if (yp_match(ypdom, "ethers.byaddr", trybuf, | ||
91 | trylen, &ypbuf, &ypbuflen)) | ||
92 | continue; | ||
93 | if (ether_line(ypbuf, &try, hostname) == 0) { | ||
94 | free(ypbuf); | ||
95 | (void)fclose(f); | ||
96 | return 0; | ||
97 | } | ||
98 | free(ypbuf); | ||
99 | continue; | ||
100 | } | ||
101 | #endif | ||
102 | if (ether_line(buf, &try, hostname) == 0 && | ||
103 | bcmp((char *)&try, (char *)e, sizeof try) == 0) { | ||
104 | (void)fclose(f); | ||
105 | return 0; | ||
106 | } | ||
107 | } | ||
108 | (void)fclose(f); | ||
109 | errno = ENOENT; | ||
110 | return -1; | ||
111 | } | ||
112 | |||
113 | ether_hostton(hostname, e) | ||
114 | char *hostname; | ||
115 | struct ether_addr *e; | ||
116 | { | ||
117 | FILE *f; | ||
118 | char buf[BUFSIZ]; | ||
119 | char try[MAXHOSTNAMELEN]; | ||
120 | #ifdef YP | ||
121 | int hostlen = strlen(hostname); | ||
122 | #endif | ||
123 | |||
124 | f = fopen(_PATH_ETHERS, "r"); | ||
125 | if (f==NULL) | ||
126 | return -1; | ||
127 | |||
128 | while (fgets(buf, sizeof buf, f)) { | ||
129 | #ifdef YP | ||
130 | /* A + in the file means try YP now. */ | ||
131 | if (!strncmp(buf, "+\n", sizeof buf)) { | ||
132 | char *ypbuf, *ypdom; | ||
133 | int ypbuflen; | ||
134 | |||
135 | if (yp_get_default_domain(&ypdom)) | ||
136 | continue; | ||
137 | if (yp_match(ypdom, "ethers.byname", hostname, hostlen, | ||
138 | &ypbuf, &ypbuflen)) | ||
139 | continue; | ||
140 | if (ether_line(ypbuf, e, try) == 0) { | ||
141 | free(ypbuf); | ||
142 | (void)fclose(f); | ||
143 | return 0; | ||
144 | } | ||
145 | free(ypbuf); | ||
146 | continue; | ||
147 | } | ||
148 | #endif | ||
149 | if (ether_line(buf, e, try) == 0 && strcmp(hostname, try) == 0) { | ||
150 | (void)fclose(f); | ||
151 | return 0; | ||
152 | } | ||
153 | } | ||
154 | (void)fclose(f); | ||
155 | errno = ENOENT; | ||
156 | return -1; | ||
157 | } | ||
158 | |||
159 | ether_line(l, e, hostname) | ||
160 | char *l; | ||
161 | struct ether_addr *e; | ||
162 | char *hostname; | ||
163 | { | ||
164 | u_int i[6]; | ||
165 | |||
166 | if (sscanf(l, " %x:%x:%x:%x:%x:%x %s\n", &i[0], &i[1], | ||
167 | &i[2], &i[3], &i[4], &i[5], hostname) == 7) { | ||
168 | e->ether_addr_octet[0] = (u_char)i[0]; | ||
169 | e->ether_addr_octet[1] = (u_char)i[1]; | ||
170 | e->ether_addr_octet[2] = (u_char)i[2]; | ||
171 | e->ether_addr_octet[3] = (u_char)i[3]; | ||
172 | e->ether_addr_octet[4] = (u_char)i[4]; | ||
173 | e->ether_addr_octet[5] = (u_char)i[5]; | ||
174 | return 0; | ||
175 | } | ||
176 | errno = EINVAL; | ||
177 | return -1; | ||
178 | } | ||
diff --git a/src/lib/libc/net/gethostbyname.3 b/src/lib/libc/net/gethostbyname.3 new file mode 100644 index 0000000000..bac0368296 --- /dev/null +++ b/src/lib/libc/net/gethostbyname.3 | |||
@@ -0,0 +1,254 @@ | |||
1 | .\" $NetBSD: gethostbyname.3,v 1.6 1995/02/25 06:20:28 cgd Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1983, 1987, 1991, 1993 | ||
4 | .\" The Regents of the University of California. All rights reserved. | ||
5 | .\" | ||
6 | .\" Redistribution and use in source and binary forms, with or without | ||
7 | .\" modification, are permitted provided that the following conditions | ||
8 | .\" are met: | ||
9 | .\" 1. Redistributions of source code must retain the above copyright | ||
10 | .\" notice, this list of conditions and the following disclaimer. | ||
11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
12 | .\" notice, this list of conditions and the following disclaimer in the | ||
13 | .\" documentation and/or other materials provided with the distribution. | ||
14 | .\" 3. All advertising materials mentioning features or use of this software | ||
15 | .\" must display the following acknowledgement: | ||
16 | .\" This product includes software developed by the University of | ||
17 | .\" California, Berkeley and its contributors. | ||
18 | .\" 4. Neither the name of the University nor the names of its contributors | ||
19 | .\" may be used to endorse or promote products derived from this software | ||
20 | .\" without specific prior written permission. | ||
21 | .\" | ||
22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
32 | .\" SUCH DAMAGE. | ||
33 | .\" | ||
34 | .\" @(#)gethostbyname.3 8.2 (Berkeley) 4/19/94 | ||
35 | .\" | ||
36 | .Dd April 19, 1994 | ||
37 | .Dt GETHOSTBYNAME 3 | ||
38 | .Os BSD 4.2 | ||
39 | .Sh NAME | ||
40 | .Nm gethostbyname , | ||
41 | .Nm gethostbyaddr , | ||
42 | .Nm gethostent , | ||
43 | .Nm sethostent , | ||
44 | .Nm endhostent , | ||
45 | .Nm herror | ||
46 | .Nd get network host entry | ||
47 | .Sh SYNOPSIS | ||
48 | .Fd #include <netdb.h> | ||
49 | .Fd extern int h_errno; | ||
50 | .Ft struct hostent * | ||
51 | .Fn gethostbyname "const char *name" | ||
52 | .Ft struct hostent * | ||
53 | .Fn gethostbyaddr "const char *addr" "int len" "int type" | ||
54 | .Ft struct hostent * | ||
55 | .Fn gethostent void | ||
56 | .Fn sethostent "int stayopen" | ||
57 | .Fn endhostent void | ||
58 | .Fn herror "char *string" | ||
59 | .Sh DESCRIPTION | ||
60 | The | ||
61 | .Fn gethostbyname | ||
62 | and | ||
63 | .Fn gethostbyaddr | ||
64 | functions | ||
65 | each return a pointer to an object with the | ||
66 | following structure describing an internet host | ||
67 | referenced by name or by address, respectively. | ||
68 | This structure contains either the information obtained from the name server, | ||
69 | .Xr named 8 , | ||
70 | broken-out fields from a line in | ||
71 | .Pa /etc/hosts , | ||
72 | or database entries supplied by the | ||
73 | .Xr yp 8 | ||
74 | system . | ||
75 | If the local name server is not running these routines do a lookup in | ||
76 | .Pa /etc/hosts . | ||
77 | .Bd -literal | ||
78 | struct hostent { | ||
79 | char *h_name; /* official name of host */ | ||
80 | char **h_aliases; /* alias list */ | ||
81 | int h_addrtype; /* host address type */ | ||
82 | int h_length; /* length of address */ | ||
83 | char **h_addr_list; /* list of addresses from name server */ | ||
84 | }; | ||
85 | #define h_addr h_addr_list[0] /* address, for backward compatibility */ | ||
86 | .Ed | ||
87 | .Pp | ||
88 | The members of this structure are: | ||
89 | .Bl -tag -width h_addr_list | ||
90 | .It Fa h_name | ||
91 | Official name of the host. | ||
92 | .It Fa h_aliases | ||
93 | A zero terminated array of alternate names for the host. | ||
94 | .It Fa h_addrtype | ||
95 | The type of address being returned; currently always | ||
96 | .Dv AF_INET . | ||
97 | .It Fa h_length | ||
98 | The length, in bytes, of the address. | ||
99 | .It Fa h_addr_list | ||
100 | A zero terminated array of network addresses for the host. | ||
101 | Host addresses are returned in network byte order. | ||
102 | .It Fa h_addr | ||
103 | The first address in | ||
104 | .Fa h_addr_list ; | ||
105 | this is for backward compatibility. | ||
106 | .Pp | ||
107 | When using the nameserver, | ||
108 | .Fn gethostbyname | ||
109 | will search for the named host in the current domain and its parents | ||
110 | unless the name ends in a dot. | ||
111 | If the name contains no dot, and if the environment variable | ||
112 | .Dq Ev HOSTALIASES | ||
113 | contains the name of an alias file, the alias file will first be searched | ||
114 | for an alias matching the input name. | ||
115 | See | ||
116 | .Xr hostname 7 | ||
117 | for the domain search procedure and the alias file format. | ||
118 | .Pp | ||
119 | The | ||
120 | .Fn sethostent | ||
121 | function | ||
122 | may be used to request the use of a connected | ||
123 | .Tn TCP | ||
124 | socket for queries. | ||
125 | If the | ||
126 | .Fa stayopen | ||
127 | flag is non-zero, | ||
128 | this sets the option to send all queries to the name server using | ||
129 | .Tn TCP | ||
130 | and to retain the connection after each call to | ||
131 | .Fn gethostbyname | ||
132 | or | ||
133 | .Fn gethostbyaddr . | ||
134 | Otherwise, queries are performed using | ||
135 | .Tn UDP | ||
136 | datagrams. | ||
137 | .Pp | ||
138 | The | ||
139 | .Fn endhostent | ||
140 | function | ||
141 | closes the | ||
142 | .Tn TCP | ||
143 | connection. | ||
144 | .Sh FILES | ||
145 | .Bl -tag -width /etc/hosts -compact | ||
146 | .It Pa /etc/hosts | ||
147 | .El | ||
148 | .Sh DIAGNOSTICS | ||
149 | Error return status from | ||
150 | .Fn gethostbyname | ||
151 | and | ||
152 | .Fn gethostbyaddr | ||
153 | is indicated by return of a null pointer. | ||
154 | The external integer | ||
155 | .Va h_errno | ||
156 | may then be checked to see whether this is a temporary failure | ||
157 | or an invalid or unknown host. | ||
158 | The routine | ||
159 | .Fn herror | ||
160 | can be used to print an error message describing the failure. | ||
161 | If its argument | ||
162 | .Fa string | ||
163 | is | ||
164 | .Pf non Dv -NULL , | ||
165 | it is printed, followed by a colon and a space. | ||
166 | The error message is printed with a trailing newline. | ||
167 | .Pp | ||
168 | The variable | ||
169 | .Va h_errno | ||
170 | can have the following values: | ||
171 | .Bl -tag -width HOST_NOT_FOUND | ||
172 | .It Dv HOST_NOT_FOUND | ||
173 | No such host is known. | ||
174 | .It Dv TRY_AGAIN | ||
175 | This is usually a temporary error | ||
176 | and means that the local server did not receive | ||
177 | a response from an authoritative server. | ||
178 | A retry at some later time may succeed. | ||
179 | .It Dv NO_RECOVERY | ||
180 | Some unexpected server failure was encountered. | ||
181 | This is a non-recoverable error. | ||
182 | .It Dv NO_DATA | ||
183 | The requested name is valid but does not have an IP address; | ||
184 | this is not a temporary error. | ||
185 | This means that the name is known to the name server but there is no address | ||
186 | associated with this name. | ||
187 | Another type of request to the name server using this domain name | ||
188 | will result in an answer; | ||
189 | for example, a mail-forwarder may be registered for this domain. | ||
190 | .El | ||
191 | .Sh SEE ALSO | ||
192 | .Xr resolver 3 , | ||
193 | .Xr hosts 5 , | ||
194 | .Xr hostname 7 , | ||
195 | .Xr named 8 | ||
196 | .Sh CAVEAT | ||
197 | The | ||
198 | .Fn gethostent | ||
199 | function | ||
200 | is defined, and | ||
201 | .Fn sethostent | ||
202 | and | ||
203 | .Fn endhostent | ||
204 | are redefined, | ||
205 | when | ||
206 | .Xr libc 3 | ||
207 | is built to use only the routines to lookup in | ||
208 | .Pa /etc/hosts | ||
209 | and not the name server. | ||
210 | .Pp | ||
211 | The | ||
212 | .Fn gethostent | ||
213 | function | ||
214 | reads the next line of | ||
215 | .Pa /etc/hosts , | ||
216 | opening the file if necessary. | ||
217 | .Pp | ||
218 | The | ||
219 | .Fn sethostent | ||
220 | function | ||
221 | opens and/or rewinds the file | ||
222 | .Pa /etc/hosts . | ||
223 | If the | ||
224 | .Fa stayopen | ||
225 | argument is non-zero, | ||
226 | the file will not be closed after each call to | ||
227 | .Fn gethostbyname | ||
228 | or | ||
229 | .Fn gethostbyaddr . | ||
230 | .Pp | ||
231 | The | ||
232 | .Fn endhostent | ||
233 | function | ||
234 | closes the file. | ||
235 | .Sh HISTORY | ||
236 | The | ||
237 | .Fn herror | ||
238 | function appeared in | ||
239 | .Bx 4.3 . | ||
240 | The | ||
241 | .Fn endhostent , | ||
242 | .Fn gethostbyaddr , | ||
243 | .Fn gethostbyname , | ||
244 | .Fn gethostent , | ||
245 | and | ||
246 | .Fn sethostent | ||
247 | functions appeared in | ||
248 | .Bx 4.2 . | ||
249 | .Sh BUGS | ||
250 | These functions use static data storage; | ||
251 | if the data is needed for future use, it should be | ||
252 | copied before any subsequent calls overwrite it. | ||
253 | Only the Internet | ||
254 | address format is currently understood. | ||
diff --git a/src/lib/libc/net/gethostnamadr.c b/src/lib/libc/net/gethostnamadr.c new file mode 100644 index 0000000000..ec3f14a900 --- /dev/null +++ b/src/lib/libc/net/gethostnamadr.c | |||
@@ -0,0 +1,654 @@ | |||
1 | /* $NetBSD: gethostnamadr.c,v 1.13 1995/05/21 16:21:14 mycroft Exp $ */ | ||
2 | |||
3 | /*- | ||
4 | * Copyright (c) 1985, 1988, 1993 | ||
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 the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | * - | ||
35 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
36 | * | ||
37 | * Permission to use, copy, modify, and distribute this software for any | ||
38 | * purpose with or without fee is hereby granted, provided that the above | ||
39 | * copyright notice and this permission notice appear in all copies, and that | ||
40 | * the name of Digital Equipment Corporation not be used in advertising or | ||
41 | * publicity pertaining to distribution of the document or software without | ||
42 | * specific, written prior permission. | ||
43 | * | ||
44 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
45 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
46 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
47 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
48 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
49 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
50 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
51 | * SOFTWARE. | ||
52 | * - | ||
53 | * --Copyright-- | ||
54 | */ | ||
55 | |||
56 | #if defined(LIBC_SCCS) && !defined(lint) | ||
57 | #if 0 | ||
58 | static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93"; | ||
59 | static char rcsid[] = "$Id: gethnamaddr.c,v 4.9.1.1 1993/05/02 22:43:03 vixie Rel "; | ||
60 | #else | ||
61 | static char rcsid[] = "$NetBSD: gethostnamadr.c,v 1.13 1995/05/21 16:21:14 mycroft Exp $"; | ||
62 | #endif | ||
63 | #endif /* LIBC_SCCS and not lint */ | ||
64 | |||
65 | #include <sys/param.h> | ||
66 | #include <sys/socket.h> | ||
67 | #include <netinet/in.h> | ||
68 | #include <arpa/inet.h> | ||
69 | #include <arpa/nameser.h> | ||
70 | #include <netdb.h> | ||
71 | #include <resolv.h> | ||
72 | #include <stdio.h> | ||
73 | #include <ctype.h> | ||
74 | #include <errno.h> | ||
75 | #include <string.h> | ||
76 | #ifdef YP | ||
77 | #include <rpc/rpc.h> | ||
78 | #include <rpcsvc/yp_prot.h> | ||
79 | #include <rpcsvc/ypclnt.h> | ||
80 | #endif | ||
81 | |||
82 | #define MAXALIASES 35 | ||
83 | #define MAXADDRS 35 | ||
84 | |||
85 | static char *h_addr_ptrs[MAXADDRS + 1]; | ||
86 | |||
87 | #ifdef YP | ||
88 | static char *__ypdomain; | ||
89 | #endif | ||
90 | |||
91 | static struct hostent host; | ||
92 | static char *host_aliases[MAXALIASES]; | ||
93 | static char hostbuf[BUFSIZ+1]; | ||
94 | static struct in_addr host_addr; | ||
95 | static FILE *hostf = NULL; | ||
96 | static int stayopen = 0; | ||
97 | |||
98 | #if PACKETSZ > 1024 | ||
99 | #define MAXPACKET PACKETSZ | ||
100 | #else | ||
101 | #define MAXPACKET 1024 | ||
102 | #endif | ||
103 | |||
104 | typedef union { | ||
105 | HEADER hdr; | ||
106 | u_char buf[MAXPACKET]; | ||
107 | } querybuf; | ||
108 | |||
109 | typedef union { | ||
110 | int32_t al; | ||
111 | char ac; | ||
112 | } align; | ||
113 | |||
114 | static int qcomp __P((struct in_addr **, struct in_addr **)); | ||
115 | static struct hostent *getanswer __P((querybuf *, int, int)); | ||
116 | |||
117 | extern int h_errno; | ||
118 | |||
119 | static struct hostent * | ||
120 | getanswer(answer, anslen, iquery) | ||
121 | querybuf *answer; | ||
122 | int anslen; | ||
123 | int iquery; | ||
124 | { | ||
125 | register HEADER *hp; | ||
126 | register u_char *cp; | ||
127 | register int n; | ||
128 | u_char *eom; | ||
129 | char *bp, **ap; | ||
130 | int type, class, buflen, ancount, qdcount; | ||
131 | int haveanswer, getclass = C_ANY; | ||
132 | char **hap; | ||
133 | |||
134 | eom = answer->buf + anslen; | ||
135 | /* | ||
136 | * find first satisfactory answer | ||
137 | */ | ||
138 | hp = &answer->hdr; | ||
139 | ancount = ntohs(hp->ancount); | ||
140 | qdcount = ntohs(hp->qdcount); | ||
141 | bp = hostbuf; | ||
142 | buflen = sizeof(hostbuf); | ||
143 | cp = answer->buf + sizeof(HEADER); | ||
144 | if (qdcount) { | ||
145 | if (iquery) { | ||
146 | if ((n = dn_expand((u_char *)answer->buf, | ||
147 | (u_char *)eom, (u_char *)cp, (u_char *)bp, | ||
148 | buflen)) < 0) { | ||
149 | h_errno = NO_RECOVERY; | ||
150 | return ((struct hostent *) NULL); | ||
151 | } | ||
152 | cp += n + QFIXEDSZ; | ||
153 | host.h_name = bp; | ||
154 | n = strlen(bp) + 1; | ||
155 | bp += n; | ||
156 | buflen -= n; | ||
157 | } else | ||
158 | cp += __dn_skipname(cp, eom) + QFIXEDSZ; | ||
159 | while (--qdcount > 0) | ||
160 | cp += __dn_skipname(cp, eom) + QFIXEDSZ; | ||
161 | } else if (iquery) { | ||
162 | if (hp->aa) | ||
163 | h_errno = HOST_NOT_FOUND; | ||
164 | else | ||
165 | h_errno = TRY_AGAIN; | ||
166 | return ((struct hostent *) NULL); | ||
167 | } | ||
168 | ap = host_aliases; | ||
169 | *ap = NULL; | ||
170 | host.h_aliases = host_aliases; | ||
171 | hap = h_addr_ptrs; | ||
172 | *hap = NULL; | ||
173 | host.h_addr_list = h_addr_ptrs; | ||
174 | haveanswer = 0; | ||
175 | while (--ancount >= 0 && cp < eom) { | ||
176 | if ((n = dn_expand((u_char *)answer->buf, (u_char *)eom, | ||
177 | (u_char *)cp, (u_char *)bp, buflen)) < 0) | ||
178 | break; | ||
179 | cp += n; | ||
180 | type = _getshort(cp); | ||
181 | cp += sizeof(u_int16_t); | ||
182 | class = _getshort(cp); | ||
183 | cp += sizeof(u_int16_t) + sizeof(u_int32_t); | ||
184 | n = _getshort(cp); | ||
185 | cp += sizeof(u_int16_t); | ||
186 | if (type == T_CNAME) { | ||
187 | cp += n; | ||
188 | if (ap >= &host_aliases[MAXALIASES-1]) | ||
189 | continue; | ||
190 | *ap++ = bp; | ||
191 | n = strlen(bp) + 1; | ||
192 | bp += n; | ||
193 | buflen -= n; | ||
194 | continue; | ||
195 | } | ||
196 | if (iquery && type == T_PTR) { | ||
197 | if ((n = dn_expand((u_char *)answer->buf, | ||
198 | (u_char *)eom, (u_char *)cp, (u_char *)bp, | ||
199 | buflen)) < 0) | ||
200 | break; | ||
201 | cp += n; | ||
202 | host.h_name = bp; | ||
203 | return(&host); | ||
204 | } | ||
205 | if (iquery || type != T_A) { | ||
206 | #ifdef DEBUG | ||
207 | if (_res.options & RES_DEBUG) | ||
208 | printf("unexpected answer type %d, size %d\n", | ||
209 | type, n); | ||
210 | #endif | ||
211 | cp += n; | ||
212 | continue; | ||
213 | } | ||
214 | if (haveanswer) { | ||
215 | if (n != host.h_length) { | ||
216 | cp += n; | ||
217 | continue; | ||
218 | } | ||
219 | if (class != getclass) { | ||
220 | cp += n; | ||
221 | continue; | ||
222 | } | ||
223 | } else { | ||
224 | host.h_length = n; | ||
225 | getclass = class; | ||
226 | host.h_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC; | ||
227 | if (!iquery) { | ||
228 | host.h_name = bp; | ||
229 | bp += strlen(bp) + 1; | ||
230 | } | ||
231 | } | ||
232 | |||
233 | bp += sizeof(align) - ((u_long)bp % sizeof(align)); | ||
234 | |||
235 | if (bp + n >= &hostbuf[sizeof(hostbuf)]) { | ||
236 | #ifdef DEBUG | ||
237 | if (_res.options & RES_DEBUG) | ||
238 | printf("size (%d) too big\n", n); | ||
239 | #endif | ||
240 | break; | ||
241 | } | ||
242 | bcopy(cp, *hap++ = bp, n); | ||
243 | bp +=n; | ||
244 | cp += n; | ||
245 | haveanswer++; | ||
246 | } | ||
247 | if (haveanswer) { | ||
248 | *ap = NULL; | ||
249 | *hap = NULL; | ||
250 | if (_res.nsort) { | ||
251 | qsort(host.h_addr_list, haveanswer, | ||
252 | sizeof(struct in_addr), | ||
253 | (int (*)__P((const void *, const void *)))qcomp); | ||
254 | } | ||
255 | return (&host); | ||
256 | } else { | ||
257 | h_errno = TRY_AGAIN; | ||
258 | return ((struct hostent *) NULL); | ||
259 | } | ||
260 | } | ||
261 | |||
262 | struct hostent * | ||
263 | gethostbyname(name) | ||
264 | const char *name; | ||
265 | { | ||
266 | querybuf buf; | ||
267 | register const char *cp; | ||
268 | int n, i; | ||
269 | extern struct hostent *_gethtbyname(), *_yp_gethtbyname(); | ||
270 | register struct hostent *hp; | ||
271 | char lookups[MAXDNSLUS]; | ||
272 | |||
273 | /* | ||
274 | * disallow names consisting only of digits/dots, unless | ||
275 | * they end in a dot. | ||
276 | */ | ||
277 | if (isdigit(name[0])) | ||
278 | for (cp = name;; ++cp) { | ||
279 | if (!*cp) { | ||
280 | if (*--cp == '.') | ||
281 | break; | ||
282 | /* | ||
283 | * All-numeric, no dot at the end. | ||
284 | * Fake up a hostent as if we'd actually | ||
285 | * done a lookup. | ||
286 | */ | ||
287 | if (!inet_aton(name, &host_addr)) { | ||
288 | h_errno = HOST_NOT_FOUND; | ||
289 | return((struct hostent *) NULL); | ||
290 | } | ||
291 | host.h_name = (char *)name; | ||
292 | host.h_aliases = host_aliases; | ||
293 | host_aliases[0] = NULL; | ||
294 | host.h_addrtype = AF_INET; | ||
295 | host.h_length = sizeof(u_int32_t); | ||
296 | h_addr_ptrs[0] = (char *)&host_addr; | ||
297 | h_addr_ptrs[1] = NULL; | ||
298 | host.h_addr_list = h_addr_ptrs; | ||
299 | return (&host); | ||
300 | } | ||
301 | if (!isdigit(*cp) && *cp != '.') | ||
302 | break; | ||
303 | } | ||
304 | |||
305 | if ((_res.options & RES_INIT) == 0 && res_init() == -1) | ||
306 | return (_gethtbyname(name)); | ||
307 | |||
308 | bcopy(_res.lookups, lookups, sizeof lookups); | ||
309 | if (lookups[0] == '\0') | ||
310 | strncpy(lookups, "bf", sizeof lookups); | ||
311 | |||
312 | hp = (struct hostent *)NULL; | ||
313 | for (i = 0; i < MAXDNSLUS && hp == NULL && lookups[i]; i++) { | ||
314 | switch (lookups[i]) { | ||
315 | #ifdef YP | ||
316 | case 'y': | ||
317 | hp = _yp_gethtbyname(name); | ||
318 | break; | ||
319 | #endif | ||
320 | case 'b': | ||
321 | if ((n = res_search(name, C_IN, T_A, buf.buf, | ||
322 | sizeof(buf))) < 0) { | ||
323 | #ifdef DEBUG | ||
324 | if (_res.options & RES_DEBUG) | ||
325 | printf("res_search failed\n"); | ||
326 | #endif | ||
327 | break; | ||
328 | } | ||
329 | hp = getanswer(&buf, n, 0); | ||
330 | break; | ||
331 | case 'f': | ||
332 | hp = _gethtbyname(name); | ||
333 | break; | ||
334 | } | ||
335 | } | ||
336 | return (hp); | ||
337 | } | ||
338 | |||
339 | struct hostent * | ||
340 | gethostbyaddr(addr, len, type) | ||
341 | const char *addr; | ||
342 | int len, type; | ||
343 | { | ||
344 | int n, i; | ||
345 | querybuf buf; | ||
346 | register struct hostent *hp; | ||
347 | char qbuf[MAXDNAME]; | ||
348 | extern struct hostent *_gethtbyaddr(), *_yp_gethtbyaddr(); | ||
349 | char lookups[MAXDNSLUS]; | ||
350 | |||
351 | if (type != AF_INET) | ||
352 | return ((struct hostent *) NULL); | ||
353 | (void)sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", | ||
354 | ((unsigned)addr[3] & 0xff), | ||
355 | ((unsigned)addr[2] & 0xff), | ||
356 | ((unsigned)addr[1] & 0xff), | ||
357 | ((unsigned)addr[0] & 0xff)); | ||
358 | |||
359 | if ((_res.options & RES_INIT) == 0 && res_init() == -1) | ||
360 | return (_gethtbyaddr(addr, len, type)); | ||
361 | |||
362 | bcopy(_res.lookups, lookups, sizeof lookups); | ||
363 | if (lookups[0] == '\0') | ||
364 | strncpy(lookups, "bf", sizeof lookups); | ||
365 | |||
366 | hp = (struct hostent *)NULL; | ||
367 | for (i = 0; i < MAXDNSLUS && hp == NULL && lookups[i]; i++) { | ||
368 | switch (lookups[i]) { | ||
369 | #ifdef YP | ||
370 | case 'y': | ||
371 | hp = _yp_gethtbyaddr(addr, len, type); | ||
372 | break; | ||
373 | #endif | ||
374 | case 'b': | ||
375 | n = res_query(qbuf, C_IN, T_PTR, (char *)&buf, sizeof(buf)); | ||
376 | if (n < 0) { | ||
377 | #ifdef DEBUG | ||
378 | if (_res.options & RES_DEBUG) | ||
379 | printf("res_query failed\n"); | ||
380 | #endif | ||
381 | break; | ||
382 | } | ||
383 | hp = getanswer(&buf, n, 1); | ||
384 | if (hp == NULL) | ||
385 | break; | ||
386 | hp->h_addrtype = type; | ||
387 | hp->h_length = len; | ||
388 | h_addr_ptrs[0] = (char *)&host_addr; | ||
389 | h_addr_ptrs[1] = (char *)0; | ||
390 | host_addr = *(struct in_addr *)addr; | ||
391 | break; | ||
392 | case 'f': | ||
393 | hp = _gethtbyaddr(addr, len, type); | ||
394 | break; | ||
395 | } | ||
396 | } | ||
397 | return (hp); | ||
398 | } | ||
399 | |||
400 | void | ||
401 | _sethtent(f) | ||
402 | int f; | ||
403 | { | ||
404 | if (hostf == NULL) | ||
405 | hostf = fopen(_PATH_HOSTS, "r" ); | ||
406 | else | ||
407 | rewind(hostf); | ||
408 | stayopen = f; | ||
409 | } | ||
410 | |||
411 | void | ||
412 | _endhtent() | ||
413 | { | ||
414 | if (hostf && !stayopen) { | ||
415 | (void) fclose(hostf); | ||
416 | hostf = NULL; | ||
417 | } | ||
418 | } | ||
419 | |||
420 | struct hostent * | ||
421 | _gethtent() | ||
422 | { | ||
423 | char *p; | ||
424 | register char *cp, **q; | ||
425 | |||
426 | if (hostf == NULL && (hostf = fopen(_PATH_HOSTS, "r" )) == NULL) | ||
427 | return (NULL); | ||
428 | again: | ||
429 | if ((p = fgets(hostbuf, BUFSIZ, hostf)) == NULL) | ||
430 | return (NULL); | ||
431 | if (*p == '#') | ||
432 | goto again; | ||
433 | cp = strpbrk(p, "#\n"); | ||
434 | if (cp == NULL) | ||
435 | goto again; | ||
436 | *cp = '\0'; | ||
437 | cp = strpbrk(p, " \t"); | ||
438 | if (cp == NULL) | ||
439 | goto again; | ||
440 | *cp++ = '\0'; | ||
441 | /* THIS STUFF IS INTERNET SPECIFIC */ | ||
442 | h_addr_ptrs[0] = (char *)&host_addr; | ||
443 | h_addr_ptrs[1] = NULL; | ||
444 | (void) inet_aton(p, &host_addr); | ||
445 | host.h_addr_list = h_addr_ptrs; | ||
446 | host.h_length = sizeof(u_int32_t); | ||
447 | host.h_addrtype = AF_INET; | ||
448 | while (*cp == ' ' || *cp == '\t') | ||
449 | cp++; | ||
450 | host.h_name = cp; | ||
451 | q = host.h_aliases = host_aliases; | ||
452 | cp = strpbrk(cp, " \t"); | ||
453 | if (cp != NULL) | ||
454 | *cp++ = '\0'; | ||
455 | while (cp && *cp) { | ||
456 | if (*cp == ' ' || *cp == '\t') { | ||
457 | cp++; | ||
458 | continue; | ||
459 | } | ||
460 | if (q < &host_aliases[MAXALIASES - 1]) | ||
461 | *q++ = cp; | ||
462 | cp = strpbrk(cp, " \t"); | ||
463 | if (cp != NULL) | ||
464 | *cp++ = '\0'; | ||
465 | } | ||
466 | *q = NULL; | ||
467 | return (&host); | ||
468 | } | ||
469 | |||
470 | struct hostent * | ||
471 | _gethtbyname(name) | ||
472 | char *name; | ||
473 | { | ||
474 | register struct hostent *p; | ||
475 | register char **cp; | ||
476 | |||
477 | _sethtent(0); | ||
478 | while (p = _gethtent()) { | ||
479 | if (strcasecmp(p->h_name, name) == 0) | ||
480 | break; | ||
481 | for (cp = p->h_aliases; *cp != 0; cp++) | ||
482 | if (strcasecmp(*cp, name) == 0) | ||
483 | goto found; | ||
484 | } | ||
485 | found: | ||
486 | _endhtent(); | ||
487 | if (p==NULL) | ||
488 | h_errno = HOST_NOT_FOUND; | ||
489 | return (p); | ||
490 | } | ||
491 | |||
492 | struct hostent * | ||
493 | _gethtbyaddr(addr, len, type) | ||
494 | const char *addr; | ||
495 | int len, type; | ||
496 | { | ||
497 | register struct hostent *p; | ||
498 | |||
499 | _sethtent(0); | ||
500 | while (p = _gethtent()) | ||
501 | if (p->h_addrtype == type && !bcmp(p->h_addr, addr, len)) | ||
502 | break; | ||
503 | _endhtent(); | ||
504 | if (p==NULL) | ||
505 | h_errno = HOST_NOT_FOUND; | ||
506 | return (p); | ||
507 | } | ||
508 | |||
509 | static int | ||
510 | qcomp(a1, a2) | ||
511 | struct in_addr **a1, **a2; | ||
512 | { | ||
513 | int pos1, pos2; | ||
514 | |||
515 | for (pos1 = 0; pos1 < _res.nsort; pos1++) | ||
516 | if (_res.sort_list[pos1].addr.s_addr == | ||
517 | ((*a1)->s_addr & _res.sort_list[pos1].mask)) | ||
518 | break; | ||
519 | for (pos2 = 0; pos2 < _res.nsort; pos2++) | ||
520 | if (_res.sort_list[pos2].addr.s_addr == | ||
521 | ((*a2)->s_addr & _res.sort_list[pos2].mask)) | ||
522 | break; | ||
523 | return pos1 - pos2; | ||
524 | } | ||
525 | |||
526 | #ifdef YP | ||
527 | struct hostent * | ||
528 | _yphostent(line) | ||
529 | char *line; | ||
530 | { | ||
531 | static struct in_addr host_addrs[MAXADDRS]; | ||
532 | char *p = line; | ||
533 | char *cp, **q; | ||
534 | char **hap; | ||
535 | struct in_addr *buf; | ||
536 | int more; | ||
537 | |||
538 | host.h_name = NULL; | ||
539 | host.h_addr_list = h_addr_ptrs; | ||
540 | host.h_length = sizeof(u_int32_t); | ||
541 | host.h_addrtype = AF_INET; | ||
542 | hap = h_addr_ptrs; | ||
543 | buf = host_addrs; | ||
544 | q = host.h_aliases = host_aliases; | ||
545 | |||
546 | nextline: | ||
547 | more = 0; | ||
548 | cp = strpbrk(p, " \t"); | ||
549 | if (cp == NULL) { | ||
550 | if (host.h_name == NULL) | ||
551 | return (NULL); | ||
552 | else | ||
553 | goto done; | ||
554 | } | ||
555 | *cp++ = '\0'; | ||
556 | |||
557 | *hap++ = (char *)buf; | ||
558 | (void) inet_aton(p, buf++); | ||
559 | |||
560 | while (*cp == ' ' || *cp == '\t') | ||
561 | cp++; | ||
562 | p = cp; | ||
563 | cp = strpbrk(p, " \t\n"); | ||
564 | if (cp != NULL) { | ||
565 | if (*cp == '\n') | ||
566 | more = 1; | ||
567 | *cp++ = '\0'; | ||
568 | } | ||
569 | if (!host.h_name) | ||
570 | host.h_name = p; | ||
571 | else if (strcmp(host.h_name, p)==0) | ||
572 | ; | ||
573 | else if (q < &host_aliases[MAXALIASES - 1]) | ||
574 | *q++ = p; | ||
575 | p = cp; | ||
576 | if (more) | ||
577 | goto nextline; | ||
578 | |||
579 | while (cp && *cp) { | ||
580 | if (*cp == ' ' || *cp == '\t') { | ||
581 | cp++; | ||
582 | continue; | ||
583 | } | ||
584 | if (*cp == '\n') { | ||
585 | cp++; | ||
586 | goto nextline; | ||
587 | } | ||
588 | if (q < &host_aliases[MAXALIASES - 1]) | ||
589 | *q++ = cp; | ||
590 | cp = strpbrk(cp, " \t"); | ||
591 | if (cp != NULL) | ||
592 | *cp++ = '\0'; | ||
593 | } | ||
594 | done: | ||
595 | *q = NULL; | ||
596 | *hap = NULL; | ||
597 | return (&host); | ||
598 | } | ||
599 | |||
600 | struct hostent * | ||
601 | _yp_gethtbyaddr(addr, len, type) | ||
602 | const char *addr; | ||
603 | int len, type; | ||
604 | { | ||
605 | struct hostent *hp = (struct hostent *)NULL; | ||
606 | static char *__ypcurrent; | ||
607 | int __ypcurrentlen, r; | ||
608 | char name[sizeof("xxx.xxx.xxx.xxx") + 1]; | ||
609 | |||
610 | if (!__ypdomain) { | ||
611 | if (_yp_check(&__ypdomain) == 0) | ||
612 | return (hp); | ||
613 | } | ||
614 | sprintf(name, "%u.%u.%u.%u", | ||
615 | ((unsigned)addr[0] & 0xff), | ||
616 | ((unsigned)addr[1] & 0xff), | ||
617 | ((unsigned)addr[2] & 0xff), | ||
618 | ((unsigned)addr[3] & 0xff)); | ||
619 | if (__ypcurrent) | ||
620 | free(__ypcurrent); | ||
621 | __ypcurrent = NULL; | ||
622 | r = yp_match(__ypdomain, "hosts.byaddr", name, | ||
623 | strlen(name), &__ypcurrent, &__ypcurrentlen); | ||
624 | if (r==0) | ||
625 | hp = _yphostent(__ypcurrent); | ||
626 | if (hp==NULL) | ||
627 | h_errno = HOST_NOT_FOUND; | ||
628 | return (hp); | ||
629 | } | ||
630 | |||
631 | struct hostent * | ||
632 | _yp_gethtbyname(name) | ||
633 | const char *name; | ||
634 | { | ||
635 | struct hostent *hp = (struct hostent *)NULL; | ||
636 | static char *__ypcurrent; | ||
637 | int __ypcurrentlen, r; | ||
638 | |||
639 | if (!__ypdomain) { | ||
640 | if (_yp_check(&__ypdomain) == 0) | ||
641 | return (hp); | ||
642 | } | ||
643 | if (__ypcurrent) | ||
644 | free(__ypcurrent); | ||
645 | __ypcurrent = NULL; | ||
646 | r = yp_match(__ypdomain, "hosts.byname", name, | ||
647 | strlen(name), &__ypcurrent, &__ypcurrentlen); | ||
648 | if (r==0) | ||
649 | hp = _yphostent(__ypcurrent); | ||
650 | if (hp==NULL) | ||
651 | h_errno = HOST_NOT_FOUND; | ||
652 | return (hp); | ||
653 | } | ||
654 | #endif | ||
diff --git a/src/lib/libc/net/getnetbyaddr.c b/src/lib/libc/net/getnetbyaddr.c new file mode 100644 index 0000000000..c193860e36 --- /dev/null +++ b/src/lib/libc/net/getnetbyaddr.c | |||
@@ -0,0 +1,62 @@ | |||
1 | /* $NetBSD: getnetbyaddr.c,v 1.4 1995/02/25 06:20:30 cgd Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1983, 1993 | ||
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 the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | */ | ||
35 | |||
36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
37 | #if 0 | ||
38 | static char sccsid[] = "@(#)getnetbyaddr.c 8.1 (Berkeley) 6/4/93"; | ||
39 | #else | ||
40 | static char rcsid[] = "$NetBSD: getnetbyaddr.c,v 1.4 1995/02/25 06:20:30 cgd Exp $"; | ||
41 | #endif | ||
42 | #endif /* LIBC_SCCS and not lint */ | ||
43 | |||
44 | #include <netdb.h> | ||
45 | |||
46 | extern int _net_stayopen; | ||
47 | |||
48 | struct netent * | ||
49 | getnetbyaddr(net, type) | ||
50 | register long net; | ||
51 | register int type; | ||
52 | { | ||
53 | register struct netent *p; | ||
54 | |||
55 | setnetent(_net_stayopen); | ||
56 | while (p = getnetent()) | ||
57 | if (p->n_addrtype == type && p->n_net == net) | ||
58 | break; | ||
59 | if (!_net_stayopen) | ||
60 | endnetent(); | ||
61 | return (p); | ||
62 | } | ||
diff --git a/src/lib/libc/net/getnetbyname.c b/src/lib/libc/net/getnetbyname.c new file mode 100644 index 0000000000..93a2e1256c --- /dev/null +++ b/src/lib/libc/net/getnetbyname.c | |||
@@ -0,0 +1,68 @@ | |||
1 | /* $NetBSD: getnetbyname.c,v 1.4 1995/02/25 06:20:31 cgd Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1983, 1993 | ||
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 the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | */ | ||
35 | |||
36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
37 | #if 0 | ||
38 | static char sccsid[] = "@(#)getnetbyname.c 8.1 (Berkeley) 6/4/93"; | ||
39 | #else | ||
40 | static char rcsid[] = "$NetBSD: getnetbyname.c,v 1.4 1995/02/25 06:20:31 cgd Exp $"; | ||
41 | #endif | ||
42 | #endif /* LIBC_SCCS and not lint */ | ||
43 | |||
44 | #include <netdb.h> | ||
45 | #include <string.h> | ||
46 | |||
47 | extern int _net_stayopen; | ||
48 | |||
49 | struct netent * | ||
50 | getnetbyname(name) | ||
51 | register const char *name; | ||
52 | { | ||
53 | register struct netent *p; | ||
54 | register char **cp; | ||
55 | |||
56 | setnetent(_net_stayopen); | ||
57 | while (p = getnetent()) { | ||
58 | if (strcmp(p->n_name, name) == 0) | ||
59 | break; | ||
60 | for (cp = p->n_aliases; *cp != 0; cp++) | ||
61 | if (strcmp(*cp, name) == 0) | ||
62 | goto found; | ||
63 | } | ||
64 | found: | ||
65 | if (!_net_stayopen) | ||
66 | endnetent(); | ||
67 | return (p); | ||
68 | } | ||
diff --git a/src/lib/libc/net/getnetent.3 b/src/lib/libc/net/getnetent.3 new file mode 100644 index 0000000000..d4f0bedbf9 --- /dev/null +++ b/src/lib/libc/net/getnetent.3 | |||
@@ -0,0 +1,153 @@ | |||
1 | .\" $NetBSD: getnetent.3,v 1.3 1995/02/25 06:20:32 cgd Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1983, 1991, 1993 | ||
4 | .\" The Regents of the University of California. All rights reserved. | ||
5 | .\" | ||
6 | .\" Redistribution and use in source and binary forms, with or without | ||
7 | .\" modification, are permitted provided that the following conditions | ||
8 | .\" are met: | ||
9 | .\" 1. Redistributions of source code must retain the above copyright | ||
10 | .\" notice, this list of conditions and the following disclaimer. | ||
11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
12 | .\" notice, this list of conditions and the following disclaimer in the | ||
13 | .\" documentation and/or other materials provided with the distribution. | ||
14 | .\" 3. All advertising materials mentioning features or use of this software | ||
15 | .\" must display the following acknowledgement: | ||
16 | .\" This product includes software developed by the University of | ||
17 | .\" California, Berkeley and its contributors. | ||
18 | .\" 4. Neither the name of the University nor the names of its contributors | ||
19 | .\" may be used to endorse or promote products derived from this software | ||
20 | .\" without specific prior written permission. | ||
21 | .\" | ||
22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
32 | .\" SUCH DAMAGE. | ||
33 | .\" | ||
34 | .\" @(#)getnetent.3 8.1 (Berkeley) 6/4/93 | ||
35 | .\" | ||
36 | .Dd June 4, 1993 | ||
37 | .Dt GETNETENT 3 | ||
38 | .Os BSD 4.2 | ||
39 | .Sh NAME | ||
40 | .Nm getnetent , | ||
41 | .Nm getnetbyaddr , | ||
42 | .Nm getnetbyname , | ||
43 | .Nm setnetent , | ||
44 | .Nm endnetent | ||
45 | .Nd get network entry | ||
46 | .Sh SYNOPSIS | ||
47 | .Fd #include <netdb.h> | ||
48 | .Ft struct netent * | ||
49 | .Fn getnetent | ||
50 | .Ft struct netent * | ||
51 | .Fn getnetbyname "char *name" | ||
52 | .Ft struct netent * | ||
53 | .Fn getnetbyaddr "long net" "int type" | ||
54 | .Fn setnetent "int stayopen" | ||
55 | .Fn endnetent | ||
56 | .Sh DESCRIPTION | ||
57 | The | ||
58 | .Fn getnetent , | ||
59 | .Fn getnetbyname , | ||
60 | and | ||
61 | .Fn getnetbyaddr | ||
62 | functions | ||
63 | each return a pointer to an object with the | ||
64 | following structure | ||
65 | containing the broken-out | ||
66 | fields of a line in the network data base, | ||
67 | .Pa /etc/networks . | ||
68 | .Bd -literal -offset indent | ||
69 | struct netent { | ||
70 | char *n_name; /* official name of net */ | ||
71 | char **n_aliases; /* alias list */ | ||
72 | int n_addrtype; /* net number type */ | ||
73 | unsigned long n_net; /* net number */ | ||
74 | }; | ||
75 | .Ed | ||
76 | .Pp | ||
77 | The members of this structure are: | ||
78 | .Bl -tag -width n_addrtype | ||
79 | .It Fa n_name | ||
80 | The official name of the network. | ||
81 | .It Fa n_aliases | ||
82 | A zero terminated list of alternate names for the network. | ||
83 | .It Fa n_addrtype | ||
84 | The type of the network number returned; currently only AF_INET. | ||
85 | .It Fa n_net | ||
86 | The network number. Network numbers are returned in machine byte | ||
87 | order. | ||
88 | .El | ||
89 | .Pp | ||
90 | The | ||
91 | .Fn getnetent | ||
92 | function | ||
93 | reads the next line of the file, opening the file if necessary. | ||
94 | .Pp | ||
95 | The | ||
96 | .Fn setnetent | ||
97 | function | ||
98 | opens and rewinds the file. If the | ||
99 | .Fa stayopen | ||
100 | flag is non-zero, | ||
101 | the net data base will not be closed after each call to | ||
102 | .Fn getnetbyname | ||
103 | or | ||
104 | .Fn getnetbyaddr . | ||
105 | .Pp | ||
106 | The | ||
107 | .Fn endnetent | ||
108 | function | ||
109 | closes the file. | ||
110 | .Pp | ||
111 | The | ||
112 | .Fn getnetbyname | ||
113 | function | ||
114 | and | ||
115 | .Fn getnetbyaddr | ||
116 | sequentially search from the beginning | ||
117 | of the file until a matching | ||
118 | net name or | ||
119 | net address and type is found, | ||
120 | or until | ||
121 | .Dv EOF | ||
122 | is encountered. | ||
123 | Network numbers are supplied in host order. | ||
124 | .Sh FILES | ||
125 | .Bl -tag -width /etc/networks -compact | ||
126 | .It Pa /etc/networks | ||
127 | .El | ||
128 | .Sh DIAGNOSTICS | ||
129 | Null pointer | ||
130 | (0) returned on | ||
131 | .Dv EOF | ||
132 | or error. | ||
133 | .Sh SEE ALSO | ||
134 | .Xr networks 5 | ||
135 | .Sh HISTORY | ||
136 | The | ||
137 | .Fn getnetent , | ||
138 | .Fn getnetbyaddr , | ||
139 | .Fn getnetbyname , | ||
140 | .Fn setnetent , | ||
141 | and | ||
142 | .Fn endnetent | ||
143 | functions appeared in | ||
144 | .Bx 4.2 . | ||
145 | .Sh BUGS | ||
146 | The data space used by | ||
147 | these functions is static; if future use requires the data, it should be | ||
148 | copied before any subsequent calls to these functions overwrite it. | ||
149 | Only Internet network | ||
150 | numbers are currently understood. | ||
151 | Expecting network numbers to fit | ||
152 | in no more than 32 bits is probably | ||
153 | naive. | ||
diff --git a/src/lib/libc/net/getnetent.c b/src/lib/libc/net/getnetent.c new file mode 100644 index 0000000000..b4e16b8f5d --- /dev/null +++ b/src/lib/libc/net/getnetent.c | |||
@@ -0,0 +1,127 @@ | |||
1 | /* $NetBSD: getnetent.c,v 1.4 1995/02/25 06:20:33 cgd Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1983, 1993 | ||
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 the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | */ | ||
35 | |||
36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
37 | #if 0 | ||
38 | static char sccsid[] = "@(#)getnetent.c 8.1 (Berkeley) 6/4/93"; | ||
39 | #else | ||
40 | static char rcsid[] = "$NetBSD: getnetent.c,v 1.4 1995/02/25 06:20:33 cgd Exp $"; | ||
41 | #endif | ||
42 | #endif /* LIBC_SCCS and not lint */ | ||
43 | |||
44 | #include <sys/types.h> | ||
45 | #include <sys/socket.h> | ||
46 | #include <netinet/in.h> | ||
47 | #include <arpa/inet.h> | ||
48 | #include <netdb.h> | ||
49 | #include <stdio.h> | ||
50 | #include <string.h> | ||
51 | |||
52 | #define MAXALIASES 35 | ||
53 | |||
54 | static FILE *netf; | ||
55 | static char line[BUFSIZ+1]; | ||
56 | static struct netent net; | ||
57 | static char *net_aliases[MAXALIASES]; | ||
58 | int _net_stayopen; | ||
59 | |||
60 | void | ||
61 | setnetent(f) | ||
62 | int f; | ||
63 | { | ||
64 | if (netf == NULL) | ||
65 | netf = fopen(_PATH_NETWORKS, "r" ); | ||
66 | else | ||
67 | rewind(netf); | ||
68 | _net_stayopen |= f; | ||
69 | } | ||
70 | |||
71 | void | ||
72 | endnetent() | ||
73 | { | ||
74 | if (netf) { | ||
75 | fclose(netf); | ||
76 | netf = NULL; | ||
77 | } | ||
78 | _net_stayopen = 0; | ||
79 | } | ||
80 | |||
81 | struct netent * | ||
82 | getnetent() | ||
83 | { | ||
84 | char *p; | ||
85 | register char *cp, **q; | ||
86 | |||
87 | if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "r" )) == NULL) | ||
88 | return (NULL); | ||
89 | again: | ||
90 | p = fgets(line, BUFSIZ, netf); | ||
91 | if (p == NULL) | ||
92 | return (NULL); | ||
93 | if (*p == '#') | ||
94 | goto again; | ||
95 | cp = strpbrk(p, "#\n"); | ||
96 | if (cp == NULL) | ||
97 | goto again; | ||
98 | *cp = '\0'; | ||
99 | net.n_name = p; | ||
100 | cp = strpbrk(p, " \t"); | ||
101 | if (cp == NULL) | ||
102 | goto again; | ||
103 | *cp++ = '\0'; | ||
104 | while (*cp == ' ' || *cp == '\t') | ||
105 | cp++; | ||
106 | p = strpbrk(cp, " \t"); | ||
107 | if (p != NULL) | ||
108 | *p++ = '\0'; | ||
109 | net.n_net = inet_network(cp); | ||
110 | net.n_addrtype = AF_INET; | ||
111 | q = net.n_aliases = net_aliases; | ||
112 | if (p != NULL) | ||
113 | cp = p; | ||
114 | while (cp && *cp) { | ||
115 | if (*cp == ' ' || *cp == '\t') { | ||
116 | cp++; | ||
117 | continue; | ||
118 | } | ||
119 | if (q < &net_aliases[MAXALIASES - 1]) | ||
120 | *q++ = cp; | ||
121 | cp = strpbrk(cp, " \t"); | ||
122 | if (cp != NULL) | ||
123 | *cp++ = '\0'; | ||
124 | } | ||
125 | *q = NULL; | ||
126 | return (&net); | ||
127 | } | ||
diff --git a/src/lib/libc/net/getproto.c b/src/lib/libc/net/getproto.c new file mode 100644 index 0000000000..49c09b0806 --- /dev/null +++ b/src/lib/libc/net/getproto.c | |||
@@ -0,0 +1,61 @@ | |||
1 | /* $NetBSD: getproto.c,v 1.4 1995/02/25 06:20:33 cgd Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1983, 1993 | ||
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 the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | */ | ||
35 | |||
36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
37 | #if 0 | ||
38 | static char sccsid[] = "@(#)getproto.c 8.1 (Berkeley) 6/4/93"; | ||
39 | #else | ||
40 | static char rcsid[] = "$NetBSD: getproto.c,v 1.4 1995/02/25 06:20:33 cgd Exp $"; | ||
41 | #endif | ||
42 | #endif /* LIBC_SCCS and not lint */ | ||
43 | |||
44 | #include <netdb.h> | ||
45 | |||
46 | extern int _proto_stayopen; | ||
47 | |||
48 | struct protoent * | ||
49 | getprotobynumber(proto) | ||
50 | register int proto; | ||
51 | { | ||
52 | register struct protoent *p; | ||
53 | |||
54 | setprotoent(_proto_stayopen); | ||
55 | while (p = getprotoent()) | ||
56 | if (p->p_proto == proto) | ||
57 | break; | ||
58 | if (!_proto_stayopen) | ||
59 | endprotoent(); | ||
60 | return (p); | ||
61 | } | ||
diff --git a/src/lib/libc/net/getprotoent.3 b/src/lib/libc/net/getprotoent.3 new file mode 100644 index 0000000000..8d607199ef --- /dev/null +++ b/src/lib/libc/net/getprotoent.3 | |||
@@ -0,0 +1,146 @@ | |||
1 | .\" $NetBSD: getprotoent.3,v 1.3 1995/02/25 06:20:34 cgd Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1983, 1991, 1993 | ||
4 | .\" The Regents of the University of California. All rights reserved. | ||
5 | .\" | ||
6 | .\" Redistribution and use in source and binary forms, with or without | ||
7 | .\" modification, are permitted provided that the following conditions | ||
8 | .\" are met: | ||
9 | .\" 1. Redistributions of source code must retain the above copyright | ||
10 | .\" notice, this list of conditions and the following disclaimer. | ||
11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
12 | .\" notice, this list of conditions and the following disclaimer in the | ||
13 | .\" documentation and/or other materials provided with the distribution. | ||
14 | .\" 3. All advertising materials mentioning features or use of this software | ||
15 | .\" must display the following acknowledgement: | ||
16 | .\" This product includes software developed by the University of | ||
17 | .\" California, Berkeley and its contributors. | ||
18 | .\" 4. Neither the name of the University nor the names of its contributors | ||
19 | .\" may be used to endorse or promote products derived from this software | ||
20 | .\" without specific prior written permission. | ||
21 | .\" | ||
22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
32 | .\" SUCH DAMAGE. | ||
33 | .\" | ||
34 | .\" @(#)getprotoent.3 8.1 (Berkeley) 6/4/93 | ||
35 | .\" | ||
36 | .Dd June 4, 1993 | ||
37 | .Dt GETPROTOENT 3 | ||
38 | .Os BSD 4.2 | ||
39 | .Sh NAME | ||
40 | .Nm getprotoent , | ||
41 | .Nm getprotobynumber , | ||
42 | .Nm getprotobyname , | ||
43 | .Nm setprotoent , | ||
44 | .Nm endprotoent | ||
45 | .Nd get protocol entry | ||
46 | .Sh SYNOPSIS | ||
47 | .Fd #include <netdb.h> | ||
48 | .Ft struct protoent * | ||
49 | .Fn getprotoent | ||
50 | .Ft struct protoent * | ||
51 | .Fn getprotobyname "char *name" | ||
52 | .Ft struct protoent * | ||
53 | .Fn getprotobynumber "int proto" | ||
54 | .Fn setprotoent "int stayopen" | ||
55 | .Fn endprotoent | ||
56 | .Sh DESCRIPTION | ||
57 | The | ||
58 | .Fn getprotoent , | ||
59 | .Fn getprotobyname , | ||
60 | and | ||
61 | .Fn getprotobynumber | ||
62 | functions | ||
63 | each return a pointer to an object with the | ||
64 | following structure | ||
65 | containing the broken-out | ||
66 | fields of a line in the network protocol data base, | ||
67 | .Pa /etc/protocols . | ||
68 | .Bd -literal -offset indent | ||
69 | .Pp | ||
70 | struct protoent { | ||
71 | char *p_name; /* official name of protocol */ | ||
72 | char **p_aliases; /* alias list */ | ||
73 | int p_proto; /* protocol number */ | ||
74 | }; | ||
75 | .Ed | ||
76 | .Pp | ||
77 | The members of this structure are: | ||
78 | .Bl -tag -width p_aliases | ||
79 | .It Fa p_name | ||
80 | The official name of the protocol. | ||
81 | .It Fa p_aliases | ||
82 | A zero terminated list of alternate names for the protocol. | ||
83 | .It Fa p_proto | ||
84 | The protocol number. | ||
85 | .El | ||
86 | .Pp | ||
87 | The | ||
88 | .Fn getprotoent | ||
89 | function | ||
90 | reads the next line of the file, opening the file if necessary. | ||
91 | .Pp | ||
92 | The | ||
93 | .Fn setprotoent | ||
94 | function | ||
95 | opens and rewinds the file. If the | ||
96 | .Fa stayopen | ||
97 | flag is non-zero, | ||
98 | the net data base will not be closed after each call to | ||
99 | .Fn getprotobyname | ||
100 | or | ||
101 | .Fn getprotobynumber . | ||
102 | .Pp | ||
103 | The | ||
104 | .Fn endprotoent | ||
105 | function | ||
106 | closes the file. | ||
107 | .Pp | ||
108 | The | ||
109 | .Fn getprotobyname | ||
110 | function | ||
111 | and | ||
112 | .Fn getprotobynumber | ||
113 | sequentially search from the beginning | ||
114 | of the file until a matching | ||
115 | protocol name or | ||
116 | protocol number is found, | ||
117 | or until | ||
118 | .Dv EOF | ||
119 | is encountered. | ||
120 | .Sh RETURN VALUES | ||
121 | Null pointer | ||
122 | (0) returned on | ||
123 | .Dv EOF | ||
124 | or error. | ||
125 | .Sh FILES | ||
126 | .Bl -tag -width /etc/protocols -compact | ||
127 | .It Pa /etc/protocols | ||
128 | .El | ||
129 | .Sh SEE ALSO | ||
130 | .Xr protocols 5 | ||
131 | .Sh HISTORY | ||
132 | The | ||
133 | .Fn getprotoent , | ||
134 | .Fn getprotobynumber , | ||
135 | .Fn getprotobyname , | ||
136 | .Fn setprotoent , | ||
137 | and | ||
138 | .Fn endprotoent | ||
139 | functions appeared in | ||
140 | .Bx 4.2 . | ||
141 | .Sh BUGS | ||
142 | These functions use a static data space; | ||
143 | if the data is needed for future use, it should be | ||
144 | copied before any subsequent calls overwrite it. | ||
145 | Only the Internet | ||
146 | protocols are currently understood. | ||
diff --git a/src/lib/libc/net/getprotoent.c b/src/lib/libc/net/getprotoent.c new file mode 100644 index 0000000000..1179b9029b --- /dev/null +++ b/src/lib/libc/net/getprotoent.c | |||
@@ -0,0 +1,125 @@ | |||
1 | /* $NetBSD: getprotoent.c,v 1.4 1995/02/25 06:20:35 cgd Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1983, 1993 | ||
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 the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | */ | ||
35 | |||
36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
37 | #if 0 | ||
38 | static char sccsid[] = "@(#)getprotoent.c 8.1 (Berkeley) 6/4/93"; | ||
39 | #else | ||
40 | static char rcsid[] = "$NetBSD: getprotoent.c,v 1.4 1995/02/25 06:20:35 cgd Exp $"; | ||
41 | #endif | ||
42 | #endif /* LIBC_SCCS and not lint */ | ||
43 | |||
44 | #include <sys/types.h> | ||
45 | #include <sys/socket.h> | ||
46 | #include <netdb.h> | ||
47 | #include <stdio.h> | ||
48 | #include <stdlib.h> | ||
49 | #include <string.h> | ||
50 | |||
51 | #define MAXALIASES 35 | ||
52 | |||
53 | static FILE *protof = NULL; | ||
54 | static char line[BUFSIZ+1]; | ||
55 | static struct protoent proto; | ||
56 | static char *proto_aliases[MAXALIASES]; | ||
57 | int _proto_stayopen; | ||
58 | |||
59 | void | ||
60 | setprotoent(f) | ||
61 | int f; | ||
62 | { | ||
63 | if (protof == NULL) | ||
64 | protof = fopen(_PATH_PROTOCOLS, "r" ); | ||
65 | else | ||
66 | rewind(protof); | ||
67 | _proto_stayopen |= f; | ||
68 | } | ||
69 | |||
70 | void | ||
71 | endprotoent() | ||
72 | { | ||
73 | if (protof) { | ||
74 | fclose(protof); | ||
75 | protof = NULL; | ||
76 | } | ||
77 | _proto_stayopen = 0; | ||
78 | } | ||
79 | |||
80 | struct protoent * | ||
81 | getprotoent() | ||
82 | { | ||
83 | char *p; | ||
84 | register char *cp, **q; | ||
85 | |||
86 | if (protof == NULL && (protof = fopen(_PATH_PROTOCOLS, "r" )) == NULL) | ||
87 | return (NULL); | ||
88 | again: | ||
89 | if ((p = fgets(line, BUFSIZ, protof)) == NULL) | ||
90 | return (NULL); | ||
91 | if (*p == '#') | ||
92 | goto again; | ||
93 | cp = strpbrk(p, "#\n"); | ||
94 | if (cp == NULL) | ||
95 | goto again; | ||
96 | *cp = '\0'; | ||
97 | proto.p_name = p; | ||
98 | cp = strpbrk(p, " \t"); | ||
99 | if (cp == NULL) | ||
100 | goto again; | ||
101 | *cp++ = '\0'; | ||
102 | while (*cp == ' ' || *cp == '\t') | ||
103 | cp++; | ||
104 | p = strpbrk(cp, " \t"); | ||
105 | if (p != NULL) | ||
106 | *p++ = '\0'; | ||
107 | proto.p_proto = atoi(cp); | ||
108 | q = proto.p_aliases = proto_aliases; | ||
109 | if (p != NULL) { | ||
110 | cp = p; | ||
111 | while (cp && *cp) { | ||
112 | if (*cp == ' ' || *cp == '\t') { | ||
113 | cp++; | ||
114 | continue; | ||
115 | } | ||
116 | if (q < &proto_aliases[MAXALIASES - 1]) | ||
117 | *q++ = cp; | ||
118 | cp = strpbrk(cp, " \t"); | ||
119 | if (cp != NULL) | ||
120 | *cp++ = '\0'; | ||
121 | } | ||
122 | } | ||
123 | *q = NULL; | ||
124 | return (&proto); | ||
125 | } | ||
diff --git a/src/lib/libc/net/getprotoname.c b/src/lib/libc/net/getprotoname.c new file mode 100644 index 0000000000..4f8cf21c3f --- /dev/null +++ b/src/lib/libc/net/getprotoname.c | |||
@@ -0,0 +1,68 @@ | |||
1 | /* $NetBSD: getprotoname.c,v 1.4 1995/02/25 06:20:36 cgd Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1983, 1993 | ||
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 the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | */ | ||
35 | |||
36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
37 | #if 0 | ||
38 | static char sccsid[] = "@(#)getprotoname.c 8.1 (Berkeley) 6/4/93"; | ||
39 | #else | ||
40 | static char rcsid[] = "$NetBSD: getprotoname.c,v 1.4 1995/02/25 06:20:36 cgd Exp $"; | ||
41 | #endif | ||
42 | #endif /* LIBC_SCCS and not lint */ | ||
43 | |||
44 | #include <netdb.h> | ||
45 | #include <string.h> | ||
46 | |||
47 | extern int _proto_stayopen; | ||
48 | |||
49 | struct protoent * | ||
50 | getprotobyname(name) | ||
51 | register const char *name; | ||
52 | { | ||
53 | register struct protoent *p; | ||
54 | register char **cp; | ||
55 | |||
56 | setprotoent(_proto_stayopen); | ||
57 | while (p = getprotoent()) { | ||
58 | if (strcmp(p->p_name, name) == 0) | ||
59 | break; | ||
60 | for (cp = p->p_aliases; *cp != 0; cp++) | ||
61 | if (strcmp(*cp, name) == 0) | ||
62 | goto found; | ||
63 | } | ||
64 | found: | ||
65 | if (!_proto_stayopen) | ||
66 | endprotoent(); | ||
67 | return (p); | ||
68 | } | ||
diff --git a/src/lib/libc/net/getservbyname.c b/src/lib/libc/net/getservbyname.c new file mode 100644 index 0000000000..b4a6311966 --- /dev/null +++ b/src/lib/libc/net/getservbyname.c | |||
@@ -0,0 +1,71 @@ | |||
1 | /* $NetBSD: getservbyname.c,v 1.4 1995/02/25 06:20:36 cgd Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1983, 1993 | ||
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 the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | */ | ||
35 | |||
36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
37 | #if 0 | ||
38 | static char sccsid[] = "@(#)getservbyname.c 8.1 (Berkeley) 6/4/93"; | ||
39 | #else | ||
40 | static char rcsid[] = "$NetBSD: getservbyname.c,v 1.4 1995/02/25 06:20:36 cgd Exp $"; | ||
41 | #endif | ||
42 | #endif /* LIBC_SCCS and not lint */ | ||
43 | |||
44 | #include <netdb.h> | ||
45 | #include <string.h> | ||
46 | |||
47 | extern int _serv_stayopen; | ||
48 | |||
49 | struct servent * | ||
50 | getservbyname(name, proto) | ||
51 | const char *name, *proto; | ||
52 | { | ||
53 | register struct servent *p; | ||
54 | register char **cp; | ||
55 | |||
56 | setservent(_serv_stayopen); | ||
57 | while (p = getservent()) { | ||
58 | if (strcmp(name, p->s_name) == 0) | ||
59 | goto gotname; | ||
60 | for (cp = p->s_aliases; *cp; cp++) | ||
61 | if (strcmp(name, *cp) == 0) | ||
62 | goto gotname; | ||
63 | continue; | ||
64 | gotname: | ||
65 | if (proto == 0 || strcmp(p->s_proto, proto) == 0) | ||
66 | break; | ||
67 | } | ||
68 | if (!_serv_stayopen) | ||
69 | endservent(); | ||
70 | return (p); | ||
71 | } | ||
diff --git a/src/lib/libc/net/getservbyport.c b/src/lib/libc/net/getservbyport.c new file mode 100644 index 0000000000..c34790737b --- /dev/null +++ b/src/lib/libc/net/getservbyport.c | |||
@@ -0,0 +1,66 @@ | |||
1 | /* $NetBSD: getservbyport.c,v 1.4 1995/02/25 06:20:37 cgd Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1983, 1993 | ||
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 the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | */ | ||
35 | |||
36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
37 | #if 0 | ||
38 | static char sccsid[] = "@(#)getservbyport.c 8.1 (Berkeley) 6/4/93"; | ||
39 | #else | ||
40 | static char rcsid[] = "$NetBSD: getservbyport.c,v 1.4 1995/02/25 06:20:37 cgd Exp $"; | ||
41 | #endif | ||
42 | #endif /* LIBC_SCCS and not lint */ | ||
43 | |||
44 | #include <netdb.h> | ||
45 | #include <string.h> | ||
46 | |||
47 | extern int _serv_stayopen; | ||
48 | |||
49 | struct servent * | ||
50 | getservbyport(port, proto) | ||
51 | int port; | ||
52 | const char *proto; | ||
53 | { | ||
54 | register struct servent *p; | ||
55 | |||
56 | setservent(_serv_stayopen); | ||
57 | while (p = getservent()) { | ||
58 | if (p->s_port != port) | ||
59 | continue; | ||
60 | if (proto == 0 || strcmp(p->s_proto, proto) == 0) | ||
61 | break; | ||
62 | } | ||
63 | if (!_serv_stayopen) | ||
64 | endservent(); | ||
65 | return (p); | ||
66 | } | ||
diff --git a/src/lib/libc/net/getservent.3 b/src/lib/libc/net/getservent.3 new file mode 100644 index 0000000000..9e0656be00 --- /dev/null +++ b/src/lib/libc/net/getservent.3 | |||
@@ -0,0 +1,157 @@ | |||
1 | .\" $NetBSD: getservent.3,v 1.3 1995/02/25 06:20:38 cgd Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1983, 1991, 1993 | ||
4 | .\" The Regents of the University of California. All rights reserved. | ||
5 | .\" | ||
6 | .\" Redistribution and use in source and binary forms, with or without | ||
7 | .\" modification, are permitted provided that the following conditions | ||
8 | .\" are met: | ||
9 | .\" 1. Redistributions of source code must retain the above copyright | ||
10 | .\" notice, this list of conditions and the following disclaimer. | ||
11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
12 | .\" notice, this list of conditions and the following disclaimer in the | ||
13 | .\" documentation and/or other materials provided with the distribution. | ||
14 | .\" 3. All advertising materials mentioning features or use of this software | ||
15 | .\" must display the following acknowledgement: | ||
16 | .\" This product includes software developed by the University of | ||
17 | .\" California, Berkeley and its contributors. | ||
18 | .\" 4. Neither the name of the University nor the names of its contributors | ||
19 | .\" may be used to endorse or promote products derived from this software | ||
20 | .\" without specific prior written permission. | ||
21 | .\" | ||
22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
32 | .\" SUCH DAMAGE. | ||
33 | .\" | ||
34 | .\" @(#)getservent.3 8.3 (Berkeley) 1/12/94 | ||
35 | .\" | ||
36 | .Dd January 12, 1994 | ||
37 | .Dt GETSERVENT 3 | ||
38 | .Os BSD 4.2 | ||
39 | .Sh NAME | ||
40 | .Nm getservent , | ||
41 | .Nm getservbyport , | ||
42 | .Nm getservbyname , | ||
43 | .Nm setservent , | ||
44 | .Nm endservent | ||
45 | .Nd get service entry | ||
46 | .Sh SYNOPSIS | ||
47 | .Fd #include <netdb.h> | ||
48 | .Ft struct servent * | ||
49 | .Fn getservent | ||
50 | .Ft struct servent * | ||
51 | .Fn getservbyname "char *name" "char *proto" | ||
52 | .Ft struct servent * | ||
53 | .Fn getservbyport "int port" proto | ||
54 | .Ft void | ||
55 | .Fn setservent "int stayopen" | ||
56 | .Ft void | ||
57 | .Fn endservent void | ||
58 | .Sh DESCRIPTION | ||
59 | The | ||
60 | .Fn getservent , | ||
61 | .Fn getservbyname , | ||
62 | and | ||
63 | .Fn getservbyport | ||
64 | functions | ||
65 | each return a pointer to an object with the | ||
66 | following structure | ||
67 | containing the broken-out | ||
68 | fields of a line in the network services data base, | ||
69 | .Pa /etc/services . | ||
70 | .Bd -literal -offset indent | ||
71 | struct servent { | ||
72 | char *s_name; /* official name of service */ | ||
73 | char **s_aliases; /* alias list */ | ||
74 | int s_port; /* port service resides at */ | ||
75 | char *s_proto; /* protocol to use */ | ||
76 | }; | ||
77 | .Ed | ||
78 | .Pp | ||
79 | The members of this structure are: | ||
80 | .Bl -tag -width s_aliases | ||
81 | .It Fa s_name | ||
82 | The official name of the service. | ||
83 | .It Fa s_aliases | ||
84 | A zero terminated list of alternate names for the service. | ||
85 | .It Fa s_port | ||
86 | The port number at which the service resides. | ||
87 | Port numbers are returned in network byte order. | ||
88 | .It Fa s_proto | ||
89 | The name of the protocol to use when contacting the | ||
90 | service. | ||
91 | .El | ||
92 | .Pp | ||
93 | The | ||
94 | .Fn getservent | ||
95 | function | ||
96 | reads the next line of the file, opening the file if necessary. | ||
97 | .Pp | ||
98 | The | ||
99 | .Fn setservent | ||
100 | function | ||
101 | opens and rewinds the file. If the | ||
102 | .Fa stayopen | ||
103 | flag is non-zero, | ||
104 | the net data base will not be closed after each call to | ||
105 | .Fn getservbyname | ||
106 | or | ||
107 | .Fn getservbyport . | ||
108 | .Pp | ||
109 | The | ||
110 | .Fn endservent | ||
111 | function | ||
112 | closes the file. | ||
113 | .Pp | ||
114 | The | ||
115 | .Fn getservbyname | ||
116 | and | ||
117 | .Fn getservbyport | ||
118 | functions | ||
119 | sequentially search from the beginning | ||
120 | of the file until a matching | ||
121 | protocol name or | ||
122 | port number is found, | ||
123 | or until | ||
124 | .Dv EOF | ||
125 | is encountered. | ||
126 | If a protocol name is also supplied (non- | ||
127 | .Dv NULL ) , | ||
128 | searches must also match the protocol. | ||
129 | .ne 1i | ||
130 | .Sh FILES | ||
131 | .Bl -tag -width /etc/services -compact | ||
132 | .It Pa /etc/services | ||
133 | .El | ||
134 | .Sh DIAGNOSTICS | ||
135 | Null pointer | ||
136 | (0) returned on | ||
137 | .Dv EOF | ||
138 | or error. | ||
139 | .Sh SEE ALSO | ||
140 | .Xr getprotoent 3 , | ||
141 | .Xr services 5 | ||
142 | .Sh HISTORY | ||
143 | The | ||
144 | .Fn getservent , | ||
145 | .Fn getservbyport , | ||
146 | .Fn getservbyname , | ||
147 | .Fn setservent , | ||
148 | and | ||
149 | .Fn endservent | ||
150 | functions appeared in | ||
151 | .Bx 4.2 . | ||
152 | .Sh BUGS | ||
153 | These functions use static data storage; | ||
154 | if the data is needed for future use, it should be | ||
155 | copied before any subsequent calls overwrite it. | ||
156 | Expecting port numbers to fit in a 32 bit | ||
157 | quantity is probably naive. | ||
diff --git a/src/lib/libc/net/getservent.c b/src/lib/libc/net/getservent.c new file mode 100644 index 0000000000..316891450e --- /dev/null +++ b/src/lib/libc/net/getservent.c | |||
@@ -0,0 +1,127 @@ | |||
1 | /* $NetBSD: getservent.c,v 1.4 1995/02/25 06:20:38 cgd Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1983, 1993 | ||
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 the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | */ | ||
35 | |||
36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
37 | #if 0 | ||
38 | static char sccsid[] = "@(#)getservent.c 8.1 (Berkeley) 6/4/93"; | ||
39 | #else | ||
40 | static char rcsid[] = "$NetBSD: getservent.c,v 1.4 1995/02/25 06:20:38 cgd Exp $"; | ||
41 | #endif | ||
42 | #endif /* LIBC_SCCS and not lint */ | ||
43 | |||
44 | #include <sys/types.h> | ||
45 | #include <sys/socket.h> | ||
46 | #include <netdb.h> | ||
47 | #include <stdio.h> | ||
48 | #include <string.h> | ||
49 | #include <stdlib.h> | ||
50 | |||
51 | #define MAXALIASES 35 | ||
52 | |||
53 | static FILE *servf = NULL; | ||
54 | static char line[BUFSIZ+1]; | ||
55 | static struct servent serv; | ||
56 | static char *serv_aliases[MAXALIASES]; | ||
57 | int _serv_stayopen; | ||
58 | |||
59 | void | ||
60 | setservent(f) | ||
61 | int f; | ||
62 | { | ||
63 | if (servf == NULL) | ||
64 | servf = fopen(_PATH_SERVICES, "r" ); | ||
65 | else | ||
66 | rewind(servf); | ||
67 | _serv_stayopen |= f; | ||
68 | } | ||
69 | |||
70 | void | ||
71 | endservent() | ||
72 | { | ||
73 | if (servf) { | ||
74 | fclose(servf); | ||
75 | servf = NULL; | ||
76 | } | ||
77 | _serv_stayopen = 0; | ||
78 | } | ||
79 | |||
80 | struct servent * | ||
81 | getservent() | ||
82 | { | ||
83 | char *p; | ||
84 | register char *cp, **q; | ||
85 | |||
86 | if (servf == NULL && (servf = fopen(_PATH_SERVICES, "r" )) == NULL) | ||
87 | return (NULL); | ||
88 | again: | ||
89 | if ((p = fgets(line, BUFSIZ, servf)) == NULL) | ||
90 | return (NULL); | ||
91 | if (*p == '#') | ||
92 | goto again; | ||
93 | cp = strpbrk(p, "#\n"); | ||
94 | if (cp == NULL) | ||
95 | goto again; | ||
96 | *cp = '\0'; | ||
97 | serv.s_name = p; | ||
98 | p = strpbrk(p, " \t"); | ||
99 | if (p == NULL) | ||
100 | goto again; | ||
101 | *p++ = '\0'; | ||
102 | while (*p == ' ' || *p == '\t') | ||
103 | p++; | ||
104 | cp = strpbrk(p, ",/"); | ||
105 | if (cp == NULL) | ||
106 | goto again; | ||
107 | *cp++ = '\0'; | ||
108 | serv.s_port = htons((u_short)atoi(p)); | ||
109 | serv.s_proto = cp; | ||
110 | q = serv.s_aliases = serv_aliases; | ||
111 | cp = strpbrk(cp, " \t"); | ||
112 | if (cp != NULL) | ||
113 | *cp++ = '\0'; | ||
114 | while (cp && *cp) { | ||
115 | if (*cp == ' ' || *cp == '\t') { | ||
116 | cp++; | ||
117 | continue; | ||
118 | } | ||
119 | if (q < &serv_aliases[MAXALIASES - 1]) | ||
120 | *q++ = cp; | ||
121 | cp = strpbrk(cp, " \t"); | ||
122 | if (cp != NULL) | ||
123 | *cp++ = '\0'; | ||
124 | } | ||
125 | *q = NULL; | ||
126 | return (&serv); | ||
127 | } | ||
diff --git a/src/lib/libc/net/herror.c b/src/lib/libc/net/herror.c new file mode 100644 index 0000000000..41adbf1055 --- /dev/null +++ b/src/lib/libc/net/herror.c | |||
@@ -0,0 +1,115 @@ | |||
1 | /* $NetBSD: herror.c,v 1.5 1995/02/25 06:20:39 cgd Exp $ */ | ||
2 | |||
3 | /*- | ||
4 | * Copyright (c) 1987, 1993 | ||
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 the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | * - | ||
35 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
36 | * | ||
37 | * Permission to use, copy, modify, and distribute this software for any | ||
38 | * purpose with or without fee is hereby granted, provided that the above | ||
39 | * copyright notice and this permission notice appear in all copies, and that | ||
40 | * the name of Digital Equipment Corporation not be used in advertising or | ||
41 | * publicity pertaining to distribution of the document or software without | ||
42 | * specific, written prior permission. | ||
43 | * | ||
44 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
45 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
46 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
47 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
48 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
49 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
50 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
51 | * SOFTWARE. | ||
52 | * - | ||
53 | * --Copyright-- | ||
54 | */ | ||
55 | |||
56 | #if defined(LIBC_SCCS) && !defined(lint) | ||
57 | #if 0 | ||
58 | static char sccsid[] = "@(#)herror.c 8.1 (Berkeley) 6/4/93"; | ||
59 | static char rcsid[] = "$Id: herror.c,v 4.9.1.1 1993/05/02 23:14:35 vixie Rel "; | ||
60 | #else | ||
61 | static char rcsid[] = "$NetBSD: herror.c,v 1.5 1995/02/25 06:20:39 cgd Exp $"; | ||
62 | #endif | ||
63 | #endif /* LIBC_SCCS and not lint */ | ||
64 | |||
65 | #include <sys/types.h> | ||
66 | #include <sys/uio.h> | ||
67 | #include <netdb.h> | ||
68 | #include <unistd.h> | ||
69 | #include <string.h> | ||
70 | |||
71 | char *h_errlist[] = { | ||
72 | "Error 0", | ||
73 | "Unknown host", /* 1 HOST_NOT_FOUND */ | ||
74 | "Host name lookup failure", /* 2 TRY_AGAIN */ | ||
75 | "Unknown server error", /* 3 NO_RECOVERY */ | ||
76 | "No address associated with name", /* 4 NO_ADDRESS */ | ||
77 | }; | ||
78 | int h_nerr = { sizeof(h_errlist)/sizeof(h_errlist[0]) }; | ||
79 | |||
80 | extern int h_errno; | ||
81 | |||
82 | /* | ||
83 | * herror -- | ||
84 | * print the error indicated by the h_errno value. | ||
85 | */ | ||
86 | void | ||
87 | herror(s) | ||
88 | const char *s; | ||
89 | { | ||
90 | struct iovec iov[4]; | ||
91 | register struct iovec *v = iov; | ||
92 | |||
93 | if (s && *s) { | ||
94 | v->iov_base = (char *)s; | ||
95 | v->iov_len = strlen(s); | ||
96 | v++; | ||
97 | v->iov_base = ": "; | ||
98 | v->iov_len = 2; | ||
99 | v++; | ||
100 | } | ||
101 | v->iov_base = (u_int)h_errno < h_nerr ? | ||
102 | h_errlist[h_errno] : "Unknown error"; | ||
103 | v->iov_len = strlen(v->iov_base); | ||
104 | v++; | ||
105 | v->iov_base = "\n"; | ||
106 | v->iov_len = 1; | ||
107 | writev(STDERR_FILENO, iov, (v - iov) + 1); | ||
108 | } | ||
109 | |||
110 | char * | ||
111 | hstrerror(err) | ||
112 | int err; | ||
113 | { | ||
114 | return (u_int)err < h_nerr ? h_errlist[err] : "Unknown resolver error"; | ||
115 | } | ||
diff --git a/src/lib/libc/net/htonl.c b/src/lib/libc/net/htonl.c new file mode 100644 index 0000000000..ac85cb0fd7 --- /dev/null +++ b/src/lib/libc/net/htonl.c | |||
@@ -0,0 +1,29 @@ | |||
1 | /* $NetBSD: htonl.c,v 1.5 1995/04/28 23:25:14 jtc Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Written by J.T. Conklin <jtc@netbsd.org>. | ||
5 | * Public domain. | ||
6 | */ | ||
7 | |||
8 | #if defined(LIBC_SCCS) && !defined(lint) | ||
9 | static char *rcsid = "$NetBSD: htonl.c,v 1.5 1995/04/28 23:25:14 jtc Exp $"; | ||
10 | #endif | ||
11 | |||
12 | #include <sys/types.h> | ||
13 | #include <machine/endian.h> | ||
14 | |||
15 | #undef htonl | ||
16 | |||
17 | unsigned long | ||
18 | htonl(x) | ||
19 | unsigned long x; | ||
20 | { | ||
21 | u_int32_t y = x; | ||
22 | |||
23 | #if BYTE_ORDER == LITTLE_ENDIAN | ||
24 | u_char *s = (u_char *)&y; | ||
25 | return s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3]; | ||
26 | #else | ||
27 | return y; | ||
28 | #endif | ||
29 | } | ||
diff --git a/src/lib/libc/net/htons.c b/src/lib/libc/net/htons.c new file mode 100644 index 0000000000..b2500a51d1 --- /dev/null +++ b/src/lib/libc/net/htons.c | |||
@@ -0,0 +1,27 @@ | |||
1 | /* $NetBSD: htons.c,v 1.5 1995/04/28 23:25:19 jtc Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Written by J.T. Conklin <jtc@netbsd.org>. | ||
5 | * Public domain. | ||
6 | */ | ||
7 | |||
8 | #if defined(LIBC_SCCS) && !defined(lint) | ||
9 | static char *rcsid = "$NetBSD: htons.c,v 1.5 1995/04/28 23:25:19 jtc Exp $"; | ||
10 | #endif | ||
11 | |||
12 | #include <sys/types.h> | ||
13 | #include <machine/endian.h> | ||
14 | |||
15 | #undef htons | ||
16 | |||
17 | unsigned short | ||
18 | htons(x) | ||
19 | unsigned short x; | ||
20 | { | ||
21 | #if BYTE_ORDER == LITTLE_ENDIAN | ||
22 | u_char *s = (u_char *) &x; | ||
23 | return s[0] << 8 | s[1]; | ||
24 | #else | ||
25 | return x; | ||
26 | #endif | ||
27 | } | ||
diff --git a/src/lib/libc/net/inet.3 b/src/lib/libc/net/inet.3 new file mode 100644 index 0000000000..49bac97e96 --- /dev/null +++ b/src/lib/libc/net/inet.3 | |||
@@ -0,0 +1,197 @@ | |||
1 | .\" $NetBSD: inet.3,v 1.4 1995/02/27 09:45:26 chopps Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1983, 1990, 1991, 1993 | ||
4 | .\" The Regents of the University of California. All rights reserved. | ||
5 | .\" | ||
6 | .\" Redistribution and use in source and binary forms, with or without | ||
7 | .\" modification, are permitted provided that the following conditions | ||
8 | .\" are met: | ||
9 | .\" 1. Redistributions of source code must retain the above copyright | ||
10 | .\" notice, this list of conditions and the following disclaimer. | ||
11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
12 | .\" notice, this list of conditions and the following disclaimer in the | ||
13 | .\" documentation and/or other materials provided with the distribution. | ||
14 | .\" 3. All advertising materials mentioning features or use of this software | ||
15 | .\" must display the following acknowledgement: | ||
16 | .\" This product includes software developed by the University of | ||
17 | .\" California, Berkeley and its contributors. | ||
18 | .\" 4. Neither the name of the University nor the names of its contributors | ||
19 | .\" may be used to endorse or promote products derived from this software | ||
20 | .\" without specific prior written permission. | ||
21 | .\" | ||
22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
32 | .\" SUCH DAMAGE. | ||
33 | .\" | ||
34 | .\" @(#)inet.3 8.1 (Berkeley) 6/4/93 | ||
35 | .\" | ||
36 | .Dd June 4, 1993 | ||
37 | .Dt INET 3 | ||
38 | .Os BSD 4.2 | ||
39 | .Sh NAME | ||
40 | .Nm inet_aton , | ||
41 | .Nm inet_addr , | ||
42 | .Nm inet_network , | ||
43 | .Nm inet_ntoa , | ||
44 | .Nm inet_makeaddr , | ||
45 | .Nm inet_lnaof , | ||
46 | .Nm inet_netof | ||
47 | .Nd Internet address manipulation routines | ||
48 | .Sh SYNOPSIS | ||
49 | .Fd #include <sys/socket.h> | ||
50 | .Fd #include <netinet/in.h> | ||
51 | .Fd #include <arpa/inet.h> | ||
52 | .Ft int | ||
53 | .Fn inet_aton "const char *cp" "struct in_addr *pin" | ||
54 | .Ft unsigned long | ||
55 | .Fn inet_addr "const char *cp" | ||
56 | .Ft unsigned long | ||
57 | .Fn inet_network "const char *cp" | ||
58 | .Ft char * | ||
59 | .Fn inet_ntoa "struct in_addr in" | ||
60 | .Ft struct in_addr | ||
61 | .Fn inet_makeaddr "int net" "int lna" | ||
62 | .Ft unsigned long | ||
63 | .Fn inet_lnaof "struct in_addr in" | ||
64 | .Ft unsigned long | ||
65 | .Fn inet_netof "struct in_addr in" | ||
66 | .Sh DESCRIPTION | ||
67 | The routines | ||
68 | .Fn inet_aton , | ||
69 | .Fn inet_addr | ||
70 | and | ||
71 | .Fn inet_network | ||
72 | interpret character strings representing | ||
73 | numbers expressed in the Internet standard | ||
74 | .Ql \&. | ||
75 | notation. | ||
76 | The | ||
77 | .Fn inet_aton | ||
78 | routine interprets the specified character string as an Internet address, | ||
79 | placing the address into the structure provided. | ||
80 | It returns 1 if the string was successfully interpreted, | ||
81 | or 0 if the string is invalid. | ||
82 | The | ||
83 | .Fn inet_addr | ||
84 | and | ||
85 | .Fn inet_network | ||
86 | functions return numbers suitable for use | ||
87 | as Internet addresses and Internet network | ||
88 | numbers, respectively. | ||
89 | The routine | ||
90 | .Fn inet_ntoa | ||
91 | takes an Internet address and returns an | ||
92 | .Tn ASCII | ||
93 | string representing the address in | ||
94 | .Ql \&. | ||
95 | notation. The routine | ||
96 | .Fn inet_makeaddr | ||
97 | takes an Internet network number and a local | ||
98 | network address and constructs an Internet address | ||
99 | from it. The routines | ||
100 | .Fn inet_netof | ||
101 | and | ||
102 | .Fn inet_lnaof | ||
103 | break apart Internet host addresses, returning | ||
104 | the network number and local network address part, | ||
105 | respectively. | ||
106 | .Pp | ||
107 | All Internet addresses are returned in network | ||
108 | order (bytes ordered from left to right). | ||
109 | All network numbers and local address parts are | ||
110 | returned as machine format integer values. | ||
111 | .Sh INTERNET ADDRESSES | ||
112 | Values specified using the | ||
113 | .Ql \&. | ||
114 | notation take one | ||
115 | of the following forms: | ||
116 | .Bd -literal -offset indent | ||
117 | a.b.c.d | ||
118 | a.b.c | ||
119 | a.b | ||
120 | a | ||
121 | .Ed | ||
122 | .Pp | ||
123 | When four parts are specified, each is interpreted | ||
124 | as a byte of data and assigned, from left to right, | ||
125 | to the four bytes of an Internet address. Note | ||
126 | that when an Internet address is viewed as a 32-bit | ||
127 | integer quantity on the | ||
128 | .Tn VAX | ||
129 | the bytes referred to | ||
130 | above appear as | ||
131 | .Dq Li d.c.b.a . | ||
132 | That is, | ||
133 | .Tn VAX | ||
134 | bytes are | ||
135 | ordered from right to left. | ||
136 | .Pp | ||
137 | When a three part address is specified, the last | ||
138 | part is interpreted as a 16-bit quantity and placed | ||
139 | in the right-most two bytes of the network address. | ||
140 | This makes the three part address format convenient | ||
141 | for specifying Class B network addresses as | ||
142 | .Dq Li 128.net.host . | ||
143 | .Pp | ||
144 | When a two part address is supplied, the last part | ||
145 | is interpreted as a 24-bit quantity and placed in | ||
146 | the right most three bytes of the network address. | ||
147 | This makes the two part address format convenient | ||
148 | for specifying Class A network addresses as | ||
149 | .Dq Li net.host . | ||
150 | .Pp | ||
151 | When only one part is given, the value is stored | ||
152 | directly in the network address without any byte | ||
153 | rearrangement. | ||
154 | .Pp | ||
155 | All numbers supplied as | ||
156 | .Dq parts | ||
157 | in a | ||
158 | .Ql \&. | ||
159 | notation | ||
160 | may be decimal, octal, or hexadecimal, as specified | ||
161 | in the C language (i.e., a leading 0x or 0X implies | ||
162 | hexadecimal; otherwise, a leading 0 implies octal; | ||
163 | otherwise, the number is interpreted as decimal). | ||
164 | .Sh DIAGNOSTICS | ||
165 | The constant | ||
166 | .Dv INADDR_NONE | ||
167 | is returned by | ||
168 | .Fn inet_addr | ||
169 | and | ||
170 | .Fn inet_network | ||
171 | for malformed requests. | ||
172 | .Sh SEE ALSO | ||
173 | .Xr gethostbyname 3 , | ||
174 | .Xr getnetent 3 , | ||
175 | .Xr hosts 5 , | ||
176 | .Xr networks 5 , | ||
177 | .Sh HISTORY | ||
178 | These | ||
179 | functions appeared in | ||
180 | .Bx 4.2 . | ||
181 | .Sh BUGS | ||
182 | The value | ||
183 | .Dv INADDR_NONE | ||
184 | (0xffffffff) is a valid broadcast address, but | ||
185 | .Fn inet_addr | ||
186 | cannot return that value without indicating failure. | ||
187 | The newer | ||
188 | .Fn inet_aton | ||
189 | function does not share this problem. | ||
190 | The problem of host byte ordering versus network byte ordering is | ||
191 | confusing. | ||
192 | The string returned by | ||
193 | .Fn inet_ntoa | ||
194 | resides in a static memory area. | ||
195 | .Pp | ||
196 | Inet_addr should return a | ||
197 | .Fa struct in_addr . | ||
diff --git a/src/lib/libc/net/inet_addr.c b/src/lib/libc/net/inet_addr.c new file mode 100644 index 0000000000..b5b9d8302f --- /dev/null +++ b/src/lib/libc/net/inet_addr.c | |||
@@ -0,0 +1,158 @@ | |||
1 | /* $NetBSD: inet_addr.c,v 1.5 1995/02/25 06:20:41 cgd Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1983, 1990, 1993 | ||
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 the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | */ | ||
35 | |||
36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
37 | #if 0 | ||
38 | static char sccsid[] = "@(#)inet_addr.c 8.1 (Berkeley) 6/17/93"; | ||
39 | #else | ||
40 | static char rcsid[] = "$NetBSD: inet_addr.c,v 1.5 1995/02/25 06:20:41 cgd Exp $"; | ||
41 | #endif | ||
42 | #endif /* LIBC_SCCS and not lint */ | ||
43 | |||
44 | #include <sys/param.h> | ||
45 | #include <netinet/in.h> | ||
46 | #include <arpa/inet.h> | ||
47 | #include <ctype.h> | ||
48 | |||
49 | /* | ||
50 | * Ascii internet address interpretation routine. | ||
51 | * The value returned is in network order. | ||
52 | */ | ||
53 | u_long | ||
54 | inet_addr(cp) | ||
55 | register const char *cp; | ||
56 | { | ||
57 | struct in_addr val; | ||
58 | |||
59 | if (inet_aton(cp, &val)) | ||
60 | return (val.s_addr); | ||
61 | return (INADDR_NONE); | ||
62 | } | ||
63 | |||
64 | /* | ||
65 | * Check whether "cp" is a valid ascii representation | ||
66 | * of an Internet address and convert to a binary address. | ||
67 | * Returns 1 if the address is valid, 0 if not. | ||
68 | * This replaces inet_addr, the return value from which | ||
69 | * cannot distinguish between failure and a local broadcast address. | ||
70 | */ | ||
71 | int | ||
72 | inet_aton(cp, addr) | ||
73 | register const char *cp; | ||
74 | struct in_addr *addr; | ||
75 | { | ||
76 | register u_long val; | ||
77 | register int base, n; | ||
78 | register char c; | ||
79 | u_int parts[4]; | ||
80 | register u_int *pp = parts; | ||
81 | |||
82 | for (;;) { | ||
83 | /* | ||
84 | * Collect number up to ``.''. | ||
85 | * Values are specified as for C: | ||
86 | * 0x=hex, 0=octal, other=decimal. | ||
87 | */ | ||
88 | val = 0; base = 10; | ||
89 | if (*cp == '0') { | ||
90 | if (*++cp == 'x' || *cp == 'X') | ||
91 | base = 16, cp++; | ||
92 | else | ||
93 | base = 8; | ||
94 | } | ||
95 | while ((c = *cp) != '\0') { | ||
96 | if (isascii(c) && isdigit(c)) { | ||
97 | val = (val * base) + (c - '0'); | ||
98 | cp++; | ||
99 | continue; | ||
100 | } | ||
101 | if (base == 16 && isascii(c) && isxdigit(c)) { | ||
102 | val = (val << 4) + | ||
103 | (c + 10 - (islower(c) ? 'a' : 'A')); | ||
104 | cp++; | ||
105 | continue; | ||
106 | } | ||
107 | break; | ||
108 | } | ||
109 | if (*cp == '.') { | ||
110 | /* | ||
111 | * Internet format: | ||
112 | * a.b.c.d | ||
113 | * a.b.c (with c treated as 16-bits) | ||
114 | * a.b (with b treated as 24 bits) | ||
115 | */ | ||
116 | if (pp >= parts + 3 || val > 0xff) | ||
117 | return (0); | ||
118 | *pp++ = val, cp++; | ||
119 | } else | ||
120 | break; | ||
121 | } | ||
122 | /* | ||
123 | * Check for trailing characters. | ||
124 | */ | ||
125 | if (*cp && (!isascii(*cp) || !isspace(*cp))) | ||
126 | return (0); | ||
127 | /* | ||
128 | * Concoct the address according to | ||
129 | * the number of parts specified. | ||
130 | */ | ||
131 | n = pp - parts + 1; | ||
132 | switch (n) { | ||
133 | |||
134 | case 1: /* a -- 32 bits */ | ||
135 | break; | ||
136 | |||
137 | case 2: /* a.b -- 8.24 bits */ | ||
138 | if (val > 0xffffff) | ||
139 | return (0); | ||
140 | val |= parts[0] << 24; | ||
141 | break; | ||
142 | |||
143 | case 3: /* a.b.c -- 8.8.16 bits */ | ||
144 | if (val > 0xffff) | ||
145 | return (0); | ||
146 | val |= (parts[0] << 24) | (parts[1] << 16); | ||
147 | break; | ||
148 | |||
149 | case 4: /* a.b.c.d -- 8.8.8.8 bits */ | ||
150 | if (val > 0xff) | ||
151 | return (0); | ||
152 | val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); | ||
153 | break; | ||
154 | } | ||
155 | if (addr) | ||
156 | addr->s_addr = htonl(val); | ||
157 | return (1); | ||
158 | } | ||
diff --git a/src/lib/libc/net/inet_lnaof.c b/src/lib/libc/net/inet_lnaof.c new file mode 100644 index 0000000000..ce1257bf68 --- /dev/null +++ b/src/lib/libc/net/inet_lnaof.c | |||
@@ -0,0 +1,65 @@ | |||
1 | /* $NetBSD: inet_lnaof.c,v 1.4 1995/02/25 06:20:42 cgd Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1983, 1993 | ||
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 the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | */ | ||
35 | |||
36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
37 | #if 0 | ||
38 | static char sccsid[] = "@(#)inet_lnaof.c 8.1 (Berkeley) 6/4/93"; | ||
39 | #else | ||
40 | static char rcsid[] = "$NetBSD: inet_lnaof.c,v 1.4 1995/02/25 06:20:42 cgd Exp $"; | ||
41 | #endif | ||
42 | #endif /* LIBC_SCCS and not lint */ | ||
43 | |||
44 | #include <sys/param.h> | ||
45 | #include <netinet/in.h> | ||
46 | #include <arpa/inet.h> | ||
47 | |||
48 | /* | ||
49 | * Return the local network address portion of an | ||
50 | * internet address; handles class a/b/c network | ||
51 | * number formats. | ||
52 | */ | ||
53 | u_long | ||
54 | inet_lnaof(in) | ||
55 | struct in_addr in; | ||
56 | { | ||
57 | register u_long i = ntohl(in.s_addr); | ||
58 | |||
59 | if (IN_CLASSA(i)) | ||
60 | return ((i)&IN_CLASSA_HOST); | ||
61 | else if (IN_CLASSB(i)) | ||
62 | return ((i)&IN_CLASSB_HOST); | ||
63 | else | ||
64 | return ((i)&IN_CLASSC_HOST); | ||
65 | } | ||
diff --git a/src/lib/libc/net/inet_makeaddr.c b/src/lib/libc/net/inet_makeaddr.c new file mode 100644 index 0000000000..84d366e03a --- /dev/null +++ b/src/lib/libc/net/inet_makeaddr.c | |||
@@ -0,0 +1,68 @@ | |||
1 | /* $NetBSD: inet_makeaddr.c,v 1.4 1995/02/25 06:20:42 cgd Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1983, 1993 | ||
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 the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | */ | ||
35 | |||
36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
37 | #if 0 | ||
38 | static char sccsid[] = "@(#)inet_makeaddr.c 8.1 (Berkeley) 6/4/93"; | ||
39 | #else | ||
40 | static char rcsid[] = "$NetBSD: inet_makeaddr.c,v 1.4 1995/02/25 06:20:42 cgd Exp $"; | ||
41 | #endif | ||
42 | #endif /* LIBC_SCCS and not lint */ | ||
43 | |||
44 | #include <sys/param.h> | ||
45 | #include <netinet/in.h> | ||
46 | #include <arpa/inet.h> | ||
47 | |||
48 | /* | ||
49 | * Formulate an Internet address from network + host. Used in | ||
50 | * building addresses stored in the ifnet structure. | ||
51 | */ | ||
52 | struct in_addr | ||
53 | inet_makeaddr(net, host) | ||
54 | u_long net, host; | ||
55 | { | ||
56 | u_long addr; | ||
57 | |||
58 | if (net < 128) | ||
59 | addr = (net << IN_CLASSA_NSHIFT) | (host & IN_CLASSA_HOST); | ||
60 | else if (net < 65536) | ||
61 | addr = (net << IN_CLASSB_NSHIFT) | (host & IN_CLASSB_HOST); | ||
62 | else if (net < 16777216L) | ||
63 | addr = (net << IN_CLASSC_NSHIFT) | (host & IN_CLASSC_HOST); | ||
64 | else | ||
65 | addr = net | host; | ||
66 | addr = htonl(addr); | ||
67 | return (*(struct in_addr *)&addr); | ||
68 | } | ||
diff --git a/src/lib/libc/net/inet_netof.c b/src/lib/libc/net/inet_netof.c new file mode 100644 index 0000000000..02f52ca318 --- /dev/null +++ b/src/lib/libc/net/inet_netof.c | |||
@@ -0,0 +1,64 @@ | |||
1 | /* $NetBSD: inet_netof.c,v 1.4 1995/02/25 06:20:43 cgd Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1983, 1993 | ||
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 the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | */ | ||
35 | |||
36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
37 | #if 0 | ||
38 | static char sccsid[] = "@(#)inet_netof.c 8.1 (Berkeley) 6/4/93"; | ||
39 | #else | ||
40 | static char rcsid[] = "$NetBSD: inet_netof.c,v 1.4 1995/02/25 06:20:43 cgd Exp $"; | ||
41 | #endif | ||
42 | #endif /* LIBC_SCCS and not lint */ | ||
43 | |||
44 | #include <sys/param.h> | ||
45 | #include <netinet/in.h> | ||
46 | #include <arpa/inet.h> | ||
47 | |||
48 | /* | ||
49 | * Return the network number from an internet | ||
50 | * address; handles class a/b/c network #'s. | ||
51 | */ | ||
52 | u_long | ||
53 | inet_netof(in) | ||
54 | struct in_addr in; | ||
55 | { | ||
56 | register u_long i = ntohl(in.s_addr); | ||
57 | |||
58 | if (IN_CLASSA(i)) | ||
59 | return (((i)&IN_CLASSA_NET) >> IN_CLASSA_NSHIFT); | ||
60 | else if (IN_CLASSB(i)) | ||
61 | return (((i)&IN_CLASSB_NET) >> IN_CLASSB_NSHIFT); | ||
62 | else | ||
63 | return (((i)&IN_CLASSC_NET) >> IN_CLASSC_NSHIFT); | ||
64 | } | ||
diff --git a/src/lib/libc/net/inet_network.c b/src/lib/libc/net/inet_network.c new file mode 100644 index 0000000000..35105fa75a --- /dev/null +++ b/src/lib/libc/net/inet_network.c | |||
@@ -0,0 +1,99 @@ | |||
1 | /* $NetBSD: inet_network.c,v 1.4 1995/02/25 06:20:45 cgd Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1983, 1993 | ||
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 the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | */ | ||
35 | |||
36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
37 | #if 0 | ||
38 | static char sccsid[] = "@(#)inet_network.c 8.1 (Berkeley) 6/4/93"; | ||
39 | #else | ||
40 | static char rcsid[] = "$NetBSD: inet_network.c,v 1.4 1995/02/25 06:20:45 cgd Exp $"; | ||
41 | #endif | ||
42 | #endif /* LIBC_SCCS and not lint */ | ||
43 | |||
44 | #include <sys/types.h> | ||
45 | #include <netinet/in.h> | ||
46 | #include <arpa/inet.h> | ||
47 | #include <ctype.h> | ||
48 | |||
49 | /* | ||
50 | * Internet network address interpretation routine. | ||
51 | * The library routines call this routine to interpret | ||
52 | * network numbers. | ||
53 | */ | ||
54 | u_long | ||
55 | inet_network(cp) | ||
56 | register const char *cp; | ||
57 | { | ||
58 | register u_long val, base, n; | ||
59 | register char c; | ||
60 | u_long parts[4], *pp = parts; | ||
61 | register int i; | ||
62 | |||
63 | again: | ||
64 | val = 0; base = 10; | ||
65 | if (*cp == '0') | ||
66 | base = 8, cp++; | ||
67 | if (*cp == 'x' || *cp == 'X') | ||
68 | base = 16, cp++; | ||
69 | while (c = *cp) { | ||
70 | if (isdigit(c)) { | ||
71 | val = (val * base) + (c - '0'); | ||
72 | cp++; | ||
73 | continue; | ||
74 | } | ||
75 | if (base == 16 && isxdigit(c)) { | ||
76 | val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A')); | ||
77 | cp++; | ||
78 | continue; | ||
79 | } | ||
80 | break; | ||
81 | } | ||
82 | if (*cp == '.') { | ||
83 | if (pp >= parts + 4) | ||
84 | return (INADDR_NONE); | ||
85 | *pp++ = val, cp++; | ||
86 | goto again; | ||
87 | } | ||
88 | if (*cp && !isspace(*cp)) | ||
89 | return (INADDR_NONE); | ||
90 | *pp++ = val; | ||
91 | n = pp - parts; | ||
92 | if (n > 4) | ||
93 | return (INADDR_NONE); | ||
94 | for (val = 0, i = 0; i < n; i++) { | ||
95 | val <<= 8; | ||
96 | val |= parts[i] & 0xff; | ||
97 | } | ||
98 | return (val); | ||
99 | } | ||
diff --git a/src/lib/libc/net/inet_ntoa.c b/src/lib/libc/net/inet_ntoa.c new file mode 100644 index 0000000000..2da0ab00ff --- /dev/null +++ b/src/lib/libc/net/inet_ntoa.c | |||
@@ -0,0 +1,65 @@ | |||
1 | /* $NetBSD: inet_ntoa.c,v 1.4 1995/02/25 06:20:46 cgd Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1983, 1993 | ||
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 the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | */ | ||
35 | |||
36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
37 | #if 0 | ||
38 | static char sccsid[] = "@(#)inet_ntoa.c 8.1 (Berkeley) 6/4/93"; | ||
39 | #else | ||
40 | static char rcsid[] = "$NetBSD: inet_ntoa.c,v 1.4 1995/02/25 06:20:46 cgd Exp $"; | ||
41 | #endif | ||
42 | #endif /* LIBC_SCCS and not lint */ | ||
43 | |||
44 | /* | ||
45 | * Convert network-format internet address | ||
46 | * to base 256 d.d.d.d representation. | ||
47 | */ | ||
48 | #include <sys/types.h> | ||
49 | #include <netinet/in.h> | ||
50 | #include <arpa/inet.h> | ||
51 | #include <stdio.h> | ||
52 | |||
53 | char * | ||
54 | inet_ntoa(in) | ||
55 | struct in_addr in; | ||
56 | { | ||
57 | static char b[18]; | ||
58 | register char *p; | ||
59 | |||
60 | p = (char *)∈ | ||
61 | #define UC(b) (((int)b)&0xff) | ||
62 | (void)snprintf(b, sizeof(b), | ||
63 | "%d.%d.%d.%d", UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3])); | ||
64 | return (b); | ||
65 | } | ||
diff --git a/src/lib/libc/net/iso_addr.3 b/src/lib/libc/net/iso_addr.3 new file mode 100644 index 0000000000..95c136e5fc --- /dev/null +++ b/src/lib/libc/net/iso_addr.3 | |||
@@ -0,0 +1,112 @@ | |||
1 | .\" $NetBSD: iso_addr.3,v 1.2 1995/02/25 06:20:46 cgd Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1993 | ||
4 | .\" The Regents of the University of California. All rights reserved. | ||
5 | .\" | ||
6 | .\" Redistribution and use in source and binary forms, with or without | ||
7 | .\" modification, are permitted provided that the following conditions | ||
8 | .\" are met: | ||
9 | .\" 1. Redistributions of source code must retain the above copyright | ||
10 | .\" notice, this list of conditions and the following disclaimer. | ||
11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
12 | .\" notice, this list of conditions and the following disclaimer in the | ||
13 | .\" documentation and/or other materials provided with the distribution. | ||
14 | .\" 3. All advertising materials mentioning features or use of this software | ||
15 | .\" must display the following acknowledgement: | ||
16 | .\" This product includes software developed by the University of | ||
17 | .\" California, Berkeley and its contributors. | ||
18 | .\" 4. Neither the name of the University nor the names of its contributors | ||
19 | .\" may be used to endorse or promote products derived from this software | ||
20 | .\" without specific prior written permission. | ||
21 | .\" | ||
22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
32 | .\" SUCH DAMAGE. | ||
33 | .\" | ||
34 | .\" @(#)iso_addr.3 8.1 (Berkeley) 6/4/93 | ||
35 | .\" | ||
36 | .Dd June 4, 1993 | ||
37 | .Dt ISO_ADDR 3 | ||
38 | .Os | ||
39 | .Sh NAME | ||
40 | .Nm iso_addr , | ||
41 | .Nm iso_ntoa | ||
42 | .Nd "elementary network address conversion routines for Open System Interconnection | ||
43 | .Sh SYNOPSIS | ||
44 | .Fd #include <sys/types.h> | ||
45 | .Fd #include <netiso/iso.h> | ||
46 | .Ft struct iso_addr * | ||
47 | .Fn iso_addr "char *cp" | ||
48 | .Ft char * | ||
49 | .Fn iso_ntoa "struct iso_addr *isoa" | ||
50 | .Sh DESCRIPTION | ||
51 | The routine | ||
52 | .Fn iso_addr | ||
53 | interprets character strings representing | ||
54 | .Tn OSI | ||
55 | addresses, returning binary information suitable | ||
56 | for use in system calls. | ||
57 | The routine | ||
58 | .Fn iso_ntoa | ||
59 | takes | ||
60 | .Tn OSI | ||
61 | addresses and returns | ||
62 | .Tn ASCII | ||
63 | strings representing NSAPs (network service | ||
64 | access points) in a | ||
65 | notation inverse to that accepted by | ||
66 | .Fn iso_addr . | ||
67 | .Pp | ||
68 | Unfortunately, no universal standard exists for representing | ||
69 | .Tn OSI | ||
70 | network addresses. | ||
71 | .Pp | ||
72 | The format employed by | ||
73 | .Fn iso_addr | ||
74 | is a sequence of hexadecimal | ||
75 | .Dq digits | ||
76 | (optionally separated by periods), | ||
77 | of the form: | ||
78 | .Bd -filled -offset indent | ||
79 | <hex digits>.<hex digits>.<hex digits> | ||
80 | .Ed | ||
81 | .Pp | ||
82 | Each pair of hexadecimal digits represents a byte | ||
83 | with the leading digit indicating the higher-ordered bits. | ||
84 | A period following an even number of bytes has no | ||
85 | effect (but may be used to increase legibility). | ||
86 | A period following an odd number of bytes has the | ||
87 | effect of causing the byte of address being translated | ||
88 | to have its higher order bits filled with zeros. | ||
89 | .Sh RETURN VALUES | ||
90 | .Fn iso_ntoa | ||
91 | always returns a null terminated string. | ||
92 | .Fn iso_addr | ||
93 | always returns a pointer to a struct iso_addr. | ||
94 | (See | ||
95 | .Sx BUGS . ) | ||
96 | .Sh SEE ALSO | ||
97 | .Xr iso 4 | ||
98 | .Sh HISTORY | ||
99 | The | ||
100 | .Fn iso_addr | ||
101 | and | ||
102 | .Fn iso_ntoa | ||
103 | functions appeared in | ||
104 | .Bx 4.3 Reno . | ||
105 | .Sh BUGS | ||
106 | The returned values | ||
107 | reside in a static memory area. | ||
108 | .Pp | ||
109 | The function | ||
110 | .Fn iso_addr | ||
111 | should diagnose improperly formed input, and there should be an unambiguous | ||
112 | way to recognize this. | ||
diff --git a/src/lib/libc/net/iso_addr.c b/src/lib/libc/net/iso_addr.c new file mode 100644 index 0000000000..c26ec1a64a --- /dev/null +++ b/src/lib/libc/net/iso_addr.c | |||
@@ -0,0 +1,125 @@ | |||
1 | /* $NetBSD: iso_addr.c,v 1.4 1995/02/25 06:20:47 cgd Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1989, 1993 | ||
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 the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | */ | ||
35 | |||
36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
37 | #if 0 | ||
38 | static char sccsid[] = "@(#)iso_addr.c 8.1 (Berkeley) 6/4/93"; | ||
39 | #else | ||
40 | static char rcsid[] = "$NetBSD: iso_addr.c,v 1.4 1995/02/25 06:20:47 cgd Exp $"; | ||
41 | #endif | ||
42 | #endif /* LIBC_SCCS and not lint */ | ||
43 | |||
44 | #include <sys/types.h> | ||
45 | #include <netiso/iso.h> | ||
46 | #include <string.h> | ||
47 | |||
48 | /* States*/ | ||
49 | #define VIRGIN 0 | ||
50 | #define GOTONE 1 | ||
51 | #define GOTTWO 2 | ||
52 | /* Inputs */ | ||
53 | #define DIGIT (4*0) | ||
54 | #define END (4*1) | ||
55 | #define DELIM (4*2) | ||
56 | |||
57 | struct iso_addr * | ||
58 | iso_addr(addr) | ||
59 | register const char *addr; | ||
60 | { | ||
61 | static struct iso_addr out_addr; | ||
62 | register char *cp = out_addr.isoa_genaddr; | ||
63 | char *cplim = cp + sizeof(out_addr.isoa_genaddr); | ||
64 | register int byte = 0, state = VIRGIN, new; | ||
65 | |||
66 | bzero((char *)&out_addr, sizeof(out_addr)); | ||
67 | do { | ||
68 | if ((*addr >= '0') && (*addr <= '9')) { | ||
69 | new = *addr - '0'; | ||
70 | } else if ((*addr >= 'a') && (*addr <= 'f')) { | ||
71 | new = *addr - 'a' + 10; | ||
72 | } else if ((*addr >= 'A') && (*addr <= 'F')) { | ||
73 | new = *addr - 'A' + 10; | ||
74 | } else if (*addr == 0) | ||
75 | state |= END; | ||
76 | else | ||
77 | state |= DELIM; | ||
78 | addr++; | ||
79 | switch (state /* | INPUT */) { | ||
80 | case GOTTWO | DIGIT: | ||
81 | *cp++ = byte; /*FALLTHROUGH*/ | ||
82 | case VIRGIN | DIGIT: | ||
83 | state = GOTONE; byte = new; continue; | ||
84 | case GOTONE | DIGIT: | ||
85 | state = GOTTWO; byte = new + (byte << 4); continue; | ||
86 | default: /* | DELIM */ | ||
87 | state = VIRGIN; *cp++ = byte; byte = 0; continue; | ||
88 | case GOTONE | END: | ||
89 | case GOTTWO | END: | ||
90 | *cp++ = byte; /* FALLTHROUGH */ | ||
91 | case VIRGIN | END: | ||
92 | break; | ||
93 | } | ||
94 | break; | ||
95 | } while (cp < cplim); | ||
96 | out_addr.isoa_len = cp - out_addr.isoa_genaddr; | ||
97 | return (&out_addr); | ||
98 | } | ||
99 | static char hexlist[] = "0123456789abcdef"; | ||
100 | |||
101 | char * | ||
102 | iso_ntoa(isoa) | ||
103 | const struct iso_addr *isoa; | ||
104 | { | ||
105 | static char obuf[64]; | ||
106 | register char *out = obuf; | ||
107 | register int i; | ||
108 | register u_char *in = (u_char *)isoa->isoa_genaddr; | ||
109 | u_char *inlim = in + isoa->isoa_len; | ||
110 | |||
111 | out[1] = 0; | ||
112 | while (in < inlim) { | ||
113 | i = *in++; | ||
114 | *out++ = '.'; | ||
115 | if (i > 0xf) { | ||
116 | out[1] = hexlist[i & 0xf]; | ||
117 | i >>= 4; | ||
118 | out[0] = hexlist[i]; | ||
119 | out += 2; | ||
120 | } else | ||
121 | *out++ = hexlist[i]; | ||
122 | } | ||
123 | *out = 0; | ||
124 | return(obuf + 1); | ||
125 | } | ||
diff --git a/src/lib/libc/net/linkaddr.3 b/src/lib/libc/net/linkaddr.3 new file mode 100644 index 0000000000..1a2af9b30d --- /dev/null +++ b/src/lib/libc/net/linkaddr.3 | |||
@@ -0,0 +1,133 @@ | |||
1 | .\" $NetBSD: linkaddr.3,v 1.2 1995/02/25 06:20:48 cgd Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1993 | ||
4 | .\" The Regents of the University of California. All rights reserved. | ||
5 | .\" | ||
6 | .\" This code is derived from software contributed to Berkeley by | ||
7 | .\" Donn Seeley at BSDI. | ||
8 | .\" | ||
9 | .\" Redistribution and use in source and binary forms, with or without | ||
10 | .\" modification, are permitted provided that the following conditions | ||
11 | .\" are met: | ||
12 | .\" 1. Redistributions of source code must retain the above copyright | ||
13 | .\" notice, this list of conditions and the following disclaimer. | ||
14 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
15 | .\" notice, this list of conditions and the following disclaimer in the | ||
16 | .\" documentation and/or other materials provided with the distribution. | ||
17 | .\" 3. All advertising materials mentioning features or use of this software | ||
18 | .\" must display the following acknowledgement: | ||
19 | .\" This product includes software developed by the University of | ||
20 | .\" California, Berkeley and its contributors. | ||
21 | .\" 4. Neither the name of the University nor the names of its contributors | ||
22 | .\" may be used to endorse or promote products derived from this software | ||
23 | .\" without specific prior written permission. | ||
24 | .\" | ||
25 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
26 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
27 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
28 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
29 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
30 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
31 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
32 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
33 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
34 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
35 | .\" SUCH DAMAGE. | ||
36 | .\" | ||
37 | .\" @(#)linkaddr.3 8.1 (Berkeley) 7/28/93 | ||
38 | .\" | ||
39 | .Dd July 28, 1993 | ||
40 | .Dt LINK_ADDR 3 | ||
41 | .Os BSD 4.4 | ||
42 | .Sh NAME | ||
43 | .Nm link_addr , | ||
44 | .Nm link_ntoa | ||
45 | .Nd elementary address specification routines for link level access | ||
46 | .Sh SYNOPSIS | ||
47 | .Fd #include <sys/types.h> | ||
48 | .Fd #include <sys/socket.h> | ||
49 | .Fd #include <net/if_dl.h> | ||
50 | .Ft void | ||
51 | .Fn link_addr "const char *addr" "struct sockaddr_dl *sdl" | ||
52 | .Ft char * | ||
53 | .Fn link_ntoa "const struct sockaddr_dl *sdl" | ||
54 | .Sh DESCRIPTION | ||
55 | The routine | ||
56 | .Fn link_addr | ||
57 | interprets character strings representing | ||
58 | link-level addresses, returning binary information suitable | ||
59 | for use in system calls. | ||
60 | The routine | ||
61 | .Fn link_ntoa | ||
62 | takes | ||
63 | a link-level | ||
64 | address and returns an | ||
65 | .Tn ASCII | ||
66 | string representing some of the information present, | ||
67 | including the link level address itself, and the interface name | ||
68 | or number, if present. | ||
69 | This facility is experimental and is | ||
70 | still subject to change. | ||
71 | .Pp | ||
72 | For | ||
73 | .Fn link_addr , | ||
74 | the string | ||
75 | .Fa addr | ||
76 | may contain | ||
77 | an optional network interface identifier of the form | ||
78 | .Dq "name unit-number" , | ||
79 | suitable for the first argument to | ||
80 | .Xr ifconfig 4 , | ||
81 | followed in all cases by a colon and | ||
82 | an interface address in the form of | ||
83 | groups of hexadecimal digits | ||
84 | separated by periods. | ||
85 | Each group represents a byte of address; | ||
86 | address bytes are filled left to right from | ||
87 | low order bytes through high order bytes. | ||
88 | .Pp | ||
89 | .\" A regular expression may make this format clearer: | ||
90 | .\" .Bd -literal -offset indent | ||
91 | .\" ([a-z]+[0-9]+:)?[0-9a-f]+(\e.[0-9a-f]+)* | ||
92 | .\" .Ed | ||
93 | .\" .Pp | ||
94 | Thus | ||
95 | .Li le0:8.0.9.13.d.30 | ||
96 | represents an ethernet address | ||
97 | to be transmitted on the first Lance ethernet interface. | ||
98 | .Sh RETURN VALUES | ||
99 | .Fn link_ntoa | ||
100 | always returns a null terminated string. | ||
101 | .Fn link_addr | ||
102 | has no return value. | ||
103 | (See | ||
104 | .Sx BUGS . ) | ||
105 | .Sh SEE ALSO | ||
106 | .Xr iso 4 , | ||
107 | .Sh HISTORY | ||
108 | The | ||
109 | .Fn link_addr | ||
110 | and | ||
111 | .Fn link_ntoa | ||
112 | functions appeared in | ||
113 | .Bx 4.3 Reno . | ||
114 | .Sh BUGS | ||
115 | The returned values for link_ntoa | ||
116 | reside in a static memory area. | ||
117 | .Pp | ||
118 | The function | ||
119 | .Fn link_addr | ||
120 | should diagnose improperly formed input, and there should be an unambiguous | ||
121 | way to recognize this. | ||
122 | .Pp | ||
123 | If the | ||
124 | .Va sdl_len | ||
125 | field of the link socket address | ||
126 | .Fa sdl | ||
127 | is 0, | ||
128 | .Fn link_ntoa | ||
129 | will not insert a colon before the interface address bytes. | ||
130 | If this translated address is given to | ||
131 | .Fn link_addr | ||
132 | without inserting an initial colon, | ||
133 | the latter will not interpret it correctly. | ||
diff --git a/src/lib/libc/net/linkaddr.c b/src/lib/libc/net/linkaddr.c new file mode 100644 index 0000000000..19a0de3abd --- /dev/null +++ b/src/lib/libc/net/linkaddr.c | |||
@@ -0,0 +1,164 @@ | |||
1 | /* $NetBSD: linkaddr.c,v 1.5 1995/02/25 06:20:49 cgd Exp $ */ | ||
2 | |||
3 | /*- | ||
4 | * Copyright (c) 1990, 1993 | ||
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 the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | */ | ||
35 | |||
36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
37 | #if 0 | ||
38 | static char sccsid[] = "@(#)linkaddr.c 8.1 (Berkeley) 6/4/93"; | ||
39 | #else | ||
40 | static char rcsid[] = "$NetBSD: linkaddr.c,v 1.5 1995/02/25 06:20:49 cgd Exp $"; | ||
41 | #endif | ||
42 | #endif /* LIBC_SCCS and not lint */ | ||
43 | |||
44 | #include <sys/types.h> | ||
45 | #include <sys/socket.h> | ||
46 | #include <net/if_dl.h> | ||
47 | #include <string.h> | ||
48 | |||
49 | /* States*/ | ||
50 | #define NAMING 0 | ||
51 | #define GOTONE 1 | ||
52 | #define GOTTWO 2 | ||
53 | #define RESET 3 | ||
54 | /* Inputs */ | ||
55 | #define DIGIT (4*0) | ||
56 | #define END (4*1) | ||
57 | #define DELIM (4*2) | ||
58 | #define LETTER (4*3) | ||
59 | |||
60 | void | ||
61 | link_addr(addr, sdl) | ||
62 | register const char *addr; | ||
63 | register struct sockaddr_dl *sdl; | ||
64 | { | ||
65 | register char *cp = sdl->sdl_data; | ||
66 | char *cplim = sdl->sdl_len + (char *)sdl; | ||
67 | register int byte = 0, state = NAMING, new; | ||
68 | |||
69 | bzero((char *)&sdl->sdl_family, sdl->sdl_len - 1); | ||
70 | sdl->sdl_family = AF_LINK; | ||
71 | do { | ||
72 | state &= ~LETTER; | ||
73 | if ((*addr >= '0') && (*addr <= '9')) { | ||
74 | new = *addr - '0'; | ||
75 | } else if ((*addr >= 'a') && (*addr <= 'f')) { | ||
76 | new = *addr - 'a' + 10; | ||
77 | } else if ((*addr >= 'A') && (*addr <= 'F')) { | ||
78 | new = *addr - 'A' + 10; | ||
79 | } else if (*addr == 0) { | ||
80 | state |= END; | ||
81 | } else if (state == NAMING && | ||
82 | (((*addr >= 'A') && (*addr <= 'Z')) || | ||
83 | ((*addr >= 'a') && (*addr <= 'z')))) | ||
84 | state |= LETTER; | ||
85 | else | ||
86 | state |= DELIM; | ||
87 | addr++; | ||
88 | switch (state /* | INPUT */) { | ||
89 | case NAMING | DIGIT: | ||
90 | case NAMING | LETTER: | ||
91 | *cp++ = addr[-1]; | ||
92 | continue; | ||
93 | case NAMING | DELIM: | ||
94 | state = RESET; | ||
95 | sdl->sdl_nlen = cp - sdl->sdl_data; | ||
96 | continue; | ||
97 | case GOTTWO | DIGIT: | ||
98 | *cp++ = byte; | ||
99 | /* FALLTHROUGH */ | ||
100 | case RESET | DIGIT: | ||
101 | state = GOTONE; | ||
102 | byte = new; | ||
103 | continue; | ||
104 | case GOTONE | DIGIT: | ||
105 | state = GOTTWO; | ||
106 | byte = new + (byte << 4); | ||
107 | continue; | ||
108 | default: /* | DELIM */ | ||
109 | state = RESET; | ||
110 | *cp++ = byte; | ||
111 | byte = 0; | ||
112 | continue; | ||
113 | case GOTONE | END: | ||
114 | case GOTTWO | END: | ||
115 | *cp++ = byte; | ||
116 | /* FALLTHROUGH */ | ||
117 | case RESET | END: | ||
118 | break; | ||
119 | } | ||
120 | break; | ||
121 | } while (cp < cplim); | ||
122 | sdl->sdl_alen = cp - LLADDR(sdl); | ||
123 | new = cp - (char *)sdl; | ||
124 | if (new > sizeof(*sdl)) | ||
125 | sdl->sdl_len = new; | ||
126 | return; | ||
127 | } | ||
128 | |||
129 | static char hexlist[] = "0123456789abcdef"; | ||
130 | |||
131 | char * | ||
132 | link_ntoa(sdl) | ||
133 | register const struct sockaddr_dl *sdl; | ||
134 | { | ||
135 | static char obuf[64]; | ||
136 | register char *out = obuf; | ||
137 | register int i; | ||
138 | register u_char *in = (u_char *)LLADDR(sdl); | ||
139 | u_char *inlim = in + sdl->sdl_alen; | ||
140 | int firsttime = 1; | ||
141 | |||
142 | if (sdl->sdl_nlen) { | ||
143 | bcopy(sdl->sdl_data, obuf, sdl->sdl_nlen); | ||
144 | out += sdl->sdl_nlen; | ||
145 | if (sdl->sdl_alen) | ||
146 | *out++ = ':'; | ||
147 | } | ||
148 | while (in < inlim) { | ||
149 | if (firsttime) | ||
150 | firsttime = 0; | ||
151 | else | ||
152 | *out++ = '.'; | ||
153 | i = *in++; | ||
154 | if (i > 0xf) { | ||
155 | out[1] = hexlist[i & 0xf]; | ||
156 | i >>= 4; | ||
157 | out[0] = hexlist[i]; | ||
158 | out += 2; | ||
159 | } else | ||
160 | *out++ = hexlist[i]; | ||
161 | } | ||
162 | *out = 0; | ||
163 | return (obuf); | ||
164 | } | ||
diff --git a/src/lib/libc/net/ns.3 b/src/lib/libc/net/ns.3 new file mode 100644 index 0000000000..f89b4fe042 --- /dev/null +++ b/src/lib/libc/net/ns.3 | |||
@@ -0,0 +1,132 @@ | |||
1 | .\" $NetBSD: ns.3,v 1.3 1995/02/25 06:20:50 cgd Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1986, 1991, 1993 | ||
4 | .\" The Regents of the University of California. All rights reserved. | ||
5 | .\" | ||
6 | .\" Redistribution and use in source and binary forms, with or without | ||
7 | .\" modification, are permitted provided that the following conditions | ||
8 | .\" are met: | ||
9 | .\" 1. Redistributions of source code must retain the above copyright | ||
10 | .\" notice, this list of conditions and the following disclaimer. | ||
11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
12 | .\" notice, this list of conditions and the following disclaimer in the | ||
13 | .\" documentation and/or other materials provided with the distribution. | ||
14 | .\" 3. All advertising materials mentioning features or use of this software | ||
15 | .\" must display the following acknowledgement: | ||
16 | .\" This product includes software developed by the University of | ||
17 | .\" California, Berkeley and its contributors. | ||
18 | .\" 4. Neither the name of the University nor the names of its contributors | ||
19 | .\" may be used to endorse or promote products derived from this software | ||
20 | .\" without specific prior written permission. | ||
21 | .\" | ||
22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
32 | .\" SUCH DAMAGE. | ||
33 | .\" | ||
34 | .\" @(#)ns.3 8.1 (Berkeley) 6/4/93 | ||
35 | .\" | ||
36 | .Dd June 4, 1993 | ||
37 | .Dt NS 3 | ||
38 | .Os BSD 4.3 | ||
39 | .Sh NAME | ||
40 | .Nm ns_addr , | ||
41 | .Nm ns_ntoa | ||
42 | .Nd Xerox | ||
43 | .Tn NS Ns (tm) | ||
44 | address conversion routines | ||
45 | .Sh SYNOPSIS | ||
46 | .Fd #include <sys/types.h> | ||
47 | .Fd #include <netns/ns.h> | ||
48 | .Ft struct ns_addr | ||
49 | .Fn ns_addr "char *cp" | ||
50 | .Ft char * | ||
51 | .Fn ns_ntoa "struct ns_addr ns" | ||
52 | .Sh DESCRIPTION | ||
53 | The routine | ||
54 | .Fn ns_addr | ||
55 | interprets character strings representing | ||
56 | .Tn XNS | ||
57 | addresses, returning binary information suitable | ||
58 | for use in system calls. | ||
59 | The routine | ||
60 | .Fn ns_ntoa | ||
61 | takes | ||
62 | .Tn XNS | ||
63 | addresses and returns | ||
64 | .Tn ASCII | ||
65 | strings representing the address in a | ||
66 | notation in common use in the Xerox Development Environment: | ||
67 | .Bd -filled -offset indent | ||
68 | <network number>.<host number>.<port number> | ||
69 | .Ed | ||
70 | .Pp | ||
71 | Trailing zero fields are suppressed, and each number is printed in hexadecimal, | ||
72 | in a format suitable for input to | ||
73 | .Fn ns_addr . | ||
74 | Any fields lacking super-decimal digits will have a | ||
75 | trailing | ||
76 | .Ql H | ||
77 | appended. | ||
78 | .Pp | ||
79 | Unfortunately, no universal standard exists for representing | ||
80 | .Tn XNS | ||
81 | addresses. | ||
82 | An effort has been made to insure that | ||
83 | .Fn ns_addr | ||
84 | be compatible with most formats in common use. | ||
85 | It will first separate an address into 1 to 3 fields using a single delimiter | ||
86 | chosen from | ||
87 | period | ||
88 | .Ql \&. , | ||
89 | colon | ||
90 | .Ql \&: | ||
91 | or pound-sign | ||
92 | .Ql \&# . | ||
93 | Each field is then examined for byte separators (colon or period). | ||
94 | If there are byte separators, each subfield separated is taken to be | ||
95 | a small hexadecimal number, and the entirety is taken as a network-byte-ordered | ||
96 | quantity to be zero extended in the high-network-order bytes. | ||
97 | Next, the field is inspected for hyphens, in which case | ||
98 | the field is assumed to be a number in decimal notation | ||
99 | with hyphens separating the millenia. | ||
100 | Next, the field is assumed to be a number: | ||
101 | It is interpreted | ||
102 | as hexadecimal if there is a leading | ||
103 | .Ql 0x | ||
104 | (as in C), | ||
105 | a trailing | ||
106 | .Ql H | ||
107 | (as in Mesa), or there are any super-decimal digits present. | ||
108 | It is interpreted as octal is there is a leading | ||
109 | .Ql 0 | ||
110 | and there are no super-octal digits. | ||
111 | Otherwise, it is converted as a decimal number. | ||
112 | .Sh RETURN VALUES | ||
113 | None. (See | ||
114 | .Sx BUGS . ) | ||
115 | .Sh SEE ALSO | ||
116 | .Xr hosts 5 , | ||
117 | .Xr networks 5 , | ||
118 | .Sh HISTORY | ||
119 | The | ||
120 | .Fn ns_addr | ||
121 | and | ||
122 | .Fn ns_toa | ||
123 | functions appeared in | ||
124 | .Bx 4.3 . | ||
125 | .Sh BUGS | ||
126 | The string returned by | ||
127 | .Fn ns_ntoa | ||
128 | resides in a static memory area. | ||
129 | The function | ||
130 | .Fn ns_addr | ||
131 | should diagnose improperly formed input, and there should be an unambiguous | ||
132 | way to recognize this. | ||
diff --git a/src/lib/libc/net/ns_addr.c b/src/lib/libc/net/ns_addr.c new file mode 100644 index 0000000000..f75ddb23b7 --- /dev/null +++ b/src/lib/libc/net/ns_addr.c | |||
@@ -0,0 +1,233 @@ | |||
1 | /* $NetBSD: ns_addr.c,v 1.5 1995/02/25 06:20:51 cgd Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1986, 1993 | ||
5 | * The Regents of the University of California. All rights reserved. | ||
6 | * | ||
7 | * This code is derived from software contributed to Berkeley by | ||
8 | * J.Q. Johnson. | ||
9 | * | ||
10 | * Redistribution and use in source and binary forms, with or without | ||
11 | * modification, are permitted provided that the following conditions | ||
12 | * are met: | ||
13 | * 1. Redistributions of source code must retain the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer. | ||
15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
16 | * notice, this list of conditions and the following disclaimer in the | ||
17 | * documentation and/or other materials provided with the distribution. | ||
18 | * 3. All advertising materials mentioning features or use of this software | ||
19 | * must display the following acknowledgement: | ||
20 | * This product includes software developed by the University of | ||
21 | * California, Berkeley and its contributors. | ||
22 | * 4. Neither the name of the University nor the names of its contributors | ||
23 | * may be used to endorse or promote products derived from this software | ||
24 | * without specific prior written permission. | ||
25 | * | ||
26 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
27 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
29 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
30 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
31 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
32 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
33 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
34 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
35 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
36 | * SUCH DAMAGE. | ||
37 | */ | ||
38 | |||
39 | #if defined(LIBC_SCCS) && !defined(lint) | ||
40 | #if 0 | ||
41 | static char sccsid[] = "@(#)ns_addr.c 8.1 (Berkeley) 6/7/93"; | ||
42 | #else | ||
43 | static char rcsid[] = "$NetBSD: ns_addr.c,v 1.5 1995/02/25 06:20:51 cgd Exp $"; | ||
44 | #endif | ||
45 | #endif /* LIBC_SCCS and not lint */ | ||
46 | |||
47 | #include <sys/param.h> | ||
48 | #include <netns/ns.h> | ||
49 | #include <stdio.h> | ||
50 | #include <string.h> | ||
51 | |||
52 | static struct ns_addr addr, zero_addr; | ||
53 | |||
54 | static void Field(), cvtbase(); | ||
55 | |||
56 | struct ns_addr | ||
57 | ns_addr(name) | ||
58 | const char *name; | ||
59 | { | ||
60 | char separator; | ||
61 | char *hostname, *socketname, *cp; | ||
62 | char buf[50]; | ||
63 | |||
64 | (void)strncpy(buf, name, sizeof(buf) - 1); | ||
65 | buf[sizeof(buf) - 1] = '\0'; | ||
66 | |||
67 | /* | ||
68 | * First, figure out what he intends as a field separtor. | ||
69 | * Despite the way this routine is written, the prefered | ||
70 | * form 2-272.AA001234H.01777, i.e. XDE standard. | ||
71 | * Great efforts are made to insure backward compatability. | ||
72 | */ | ||
73 | if (hostname = strchr(buf, '#')) | ||
74 | separator = '#'; | ||
75 | else { | ||
76 | hostname = strchr(buf, '.'); | ||
77 | if ((cp = strchr(buf, ':')) && | ||
78 | ((hostname && cp < hostname) || (hostname == 0))) { | ||
79 | hostname = cp; | ||
80 | separator = ':'; | ||
81 | } else | ||
82 | separator = '.'; | ||
83 | } | ||
84 | if (hostname) | ||
85 | *hostname++ = 0; | ||
86 | |||
87 | addr = zero_addr; | ||
88 | Field(buf, addr.x_net.c_net, 4); | ||
89 | if (hostname == 0) | ||
90 | return (addr); /* No separator means net only */ | ||
91 | |||
92 | socketname = strchr(hostname, separator); | ||
93 | if (socketname) { | ||
94 | *socketname++ = 0; | ||
95 | Field(socketname, (u_char *)&addr.x_port, 2); | ||
96 | } | ||
97 | |||
98 | Field(hostname, addr.x_host.c_host, 6); | ||
99 | |||
100 | return (addr); | ||
101 | } | ||
102 | |||
103 | static void | ||
104 | Field(buf, out, len) | ||
105 | char *buf; | ||
106 | u_char *out; | ||
107 | int len; | ||
108 | { | ||
109 | register char *bp = buf; | ||
110 | int i, ibase, base16 = 0, base10 = 0, clen = 0; | ||
111 | int hb[6], *hp; | ||
112 | char *fmt; | ||
113 | |||
114 | /* | ||
115 | * first try 2-273#2-852-151-014#socket | ||
116 | */ | ||
117 | if ((*buf != '-') && | ||
118 | (1 < (i = sscanf(buf, "%d-%d-%d-%d-%d", | ||
119 | &hb[0], &hb[1], &hb[2], &hb[3], &hb[4])))) { | ||
120 | cvtbase(1000L, 256, hb, i, out, len); | ||
121 | return; | ||
122 | } | ||
123 | /* | ||
124 | * try form 8E1#0.0.AA.0.5E.E6#socket | ||
125 | */ | ||
126 | if (1 < (i = sscanf(buf,"%x.%x.%x.%x.%x.%x", | ||
127 | &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) { | ||
128 | cvtbase(256L, 256, hb, i, out, len); | ||
129 | return; | ||
130 | } | ||
131 | /* | ||
132 | * try form 8E1#0:0:AA:0:5E:E6#socket | ||
133 | */ | ||
134 | if (1 < (i = sscanf(buf,"%x:%x:%x:%x:%x:%x", | ||
135 | &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) { | ||
136 | cvtbase(256L, 256, hb, i, out, len); | ||
137 | return; | ||
138 | } | ||
139 | /* | ||
140 | * This is REALLY stretching it but there was a | ||
141 | * comma notation separting shorts -- definitely non standard | ||
142 | */ | ||
143 | if (1 < (i = sscanf(buf,"%x,%x,%x", | ||
144 | &hb[0], &hb[1], &hb[2]))) { | ||
145 | hb[0] = htons(hb[0]); hb[1] = htons(hb[1]); | ||
146 | hb[2] = htons(hb[2]); | ||
147 | cvtbase(65536L, 256, hb, i, out, len); | ||
148 | return; | ||
149 | } | ||
150 | |||
151 | /* Need to decide if base 10, 16 or 8 */ | ||
152 | while (*bp) switch (*bp++) { | ||
153 | |||
154 | case '0': case '1': case '2': case '3': case '4': case '5': | ||
155 | case '6': case '7': case '-': | ||
156 | break; | ||
157 | |||
158 | case '8': case '9': | ||
159 | base10 = 1; | ||
160 | break; | ||
161 | |||
162 | case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': | ||
163 | case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': | ||
164 | base16 = 1; | ||
165 | break; | ||
166 | |||
167 | case 'x': case 'X': | ||
168 | *--bp = '0'; | ||
169 | base16 = 1; | ||
170 | break; | ||
171 | |||
172 | case 'h': case 'H': | ||
173 | base16 = 1; | ||
174 | /* fall into */ | ||
175 | |||
176 | default: | ||
177 | *--bp = 0; /* Ends Loop */ | ||
178 | } | ||
179 | if (base16) { | ||
180 | fmt = "%3x"; | ||
181 | ibase = 4096; | ||
182 | } else if (base10 == 0 && *buf == '0') { | ||
183 | fmt = "%3o"; | ||
184 | ibase = 512; | ||
185 | } else { | ||
186 | fmt = "%3d"; | ||
187 | ibase = 1000; | ||
188 | } | ||
189 | |||
190 | for (bp = buf; *bp++; ) clen++; | ||
191 | if (clen == 0) clen++; | ||
192 | if (clen > 18) clen = 18; | ||
193 | i = ((clen - 1) / 3) + 1; | ||
194 | bp = clen + buf - 3; | ||
195 | hp = hb + i - 1; | ||
196 | |||
197 | while (hp > hb) { | ||
198 | (void)sscanf(bp, fmt, hp); | ||
199 | bp[0] = 0; | ||
200 | hp--; | ||
201 | bp -= 3; | ||
202 | } | ||
203 | (void)sscanf(buf, fmt, hp); | ||
204 | cvtbase((long)ibase, 256, hb, i, out, len); | ||
205 | } | ||
206 | |||
207 | static void | ||
208 | cvtbase(oldbase,newbase,input,inlen,result,reslen) | ||
209 | long oldbase; | ||
210 | int newbase; | ||
211 | int input[]; | ||
212 | int inlen; | ||
213 | unsigned char result[]; | ||
214 | int reslen; | ||
215 | { | ||
216 | int d, e; | ||
217 | long sum; | ||
218 | |||
219 | e = 1; | ||
220 | while (e > 0 && reslen > 0) { | ||
221 | d = 0; e = 0; sum = 0; | ||
222 | /* long division: input=input/newbase */ | ||
223 | while (d < inlen) { | ||
224 | sum = sum*oldbase + (long) input[d]; | ||
225 | e += (sum > 0); | ||
226 | input[d++] = sum / newbase; | ||
227 | sum %= newbase; | ||
228 | } | ||
229 | result[--reslen] = sum; /* accumulate remainder */ | ||
230 | } | ||
231 | for (d=0; d < reslen; d++) | ||
232 | result[d] = 0; | ||
233 | } | ||
diff --git a/src/lib/libc/net/ns_ntoa.c b/src/lib/libc/net/ns_ntoa.c new file mode 100644 index 0000000000..ad3265399b --- /dev/null +++ b/src/lib/libc/net/ns_ntoa.c | |||
@@ -0,0 +1,106 @@ | |||
1 | /* $NetBSD: ns_ntoa.c,v 1.4 1995/02/25 06:20:51 cgd Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1986, 1993 | ||
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 the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | */ | ||
35 | |||
36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
37 | #if 0 | ||
38 | static char sccsid[] = "@(#)ns_ntoa.c 8.1 (Berkeley) 6/4/93"; | ||
39 | #else | ||
40 | static char rcsid[] = "$NetBSD: ns_ntoa.c,v 1.4 1995/02/25 06:20:51 cgd Exp $"; | ||
41 | #endif | ||
42 | #endif /* LIBC_SCCS and not lint */ | ||
43 | |||
44 | #include <sys/param.h> | ||
45 | #include <netns/ns.h> | ||
46 | #include <stdio.h> | ||
47 | |||
48 | char * | ||
49 | ns_ntoa(addr) | ||
50 | struct ns_addr addr; | ||
51 | { | ||
52 | static char obuf[40]; | ||
53 | union { union ns_net net_e; u_long long_e; } net; | ||
54 | u_short port = htons(addr.x_port); | ||
55 | register char *cp; | ||
56 | char *cp2; | ||
57 | register u_char *up = addr.x_host.c_host; | ||
58 | u_char *uplim = up + 6; | ||
59 | static char *spectHex(); | ||
60 | |||
61 | net.net_e = addr.x_net; | ||
62 | sprintf(obuf, "%lx", ntohl(net.long_e)); | ||
63 | cp = spectHex(obuf); | ||
64 | cp2 = cp + 1; | ||
65 | while (*up==0 && up < uplim) up++; | ||
66 | if (up == uplim) { | ||
67 | if (port) { | ||
68 | sprintf(cp, ".0"); | ||
69 | cp += 2; | ||
70 | } | ||
71 | } else { | ||
72 | sprintf(cp, ".%x", *up++); | ||
73 | while (up < uplim) { | ||
74 | while (*cp) cp++; | ||
75 | sprintf(cp, "%02x", *up++); | ||
76 | } | ||
77 | cp = spectHex(cp2); | ||
78 | } | ||
79 | if (port) { | ||
80 | sprintf(cp, ".%x", port); | ||
81 | spectHex(cp + 1); | ||
82 | } | ||
83 | return (obuf); | ||
84 | } | ||
85 | |||
86 | static char * | ||
87 | spectHex(p0) | ||
88 | char *p0; | ||
89 | { | ||
90 | int ok = 0; | ||
91 | int nonzero = 0; | ||
92 | register char *p = p0; | ||
93 | for (; *p; p++) switch (*p) { | ||
94 | |||
95 | case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': | ||
96 | *p += ('A' - 'a'); | ||
97 | /* fall into . . . */ | ||
98 | case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': | ||
99 | ok = 1; | ||
100 | case '1': case '2': case '3': case '4': case '5': | ||
101 | case '6': case '7': case '8': case '9': | ||
102 | nonzero = 1; | ||
103 | } | ||
104 | if (nonzero && !ok) { *p++ = 'H'; *p = 0; } | ||
105 | return (p); | ||
106 | } | ||
diff --git a/src/lib/libc/net/ntohl.c b/src/lib/libc/net/ntohl.c new file mode 100644 index 0000000000..05b7f4c9a3 --- /dev/null +++ b/src/lib/libc/net/ntohl.c | |||
@@ -0,0 +1,29 @@ | |||
1 | /* $NetBSD: ntohl.c,v 1.5 1995/04/28 23:25:21 jtc Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Written by J.T. Conklin <jtc@netbsd.org>. | ||
5 | * Public domain. | ||
6 | */ | ||
7 | |||
8 | #if defined(LIBC_SCCS) && !defined(lint) | ||
9 | static char *rcsid = "$NetBSD: ntohl.c,v 1.5 1995/04/28 23:25:21 jtc Exp $"; | ||
10 | #endif | ||
11 | |||
12 | #include <sys/types.h> | ||
13 | #include <machine/endian.h> | ||
14 | |||
15 | #undef ntohl | ||
16 | |||
17 | unsigned long | ||
18 | ntohl(x) | ||
19 | unsigned long x; | ||
20 | { | ||
21 | u_int32_t y = x; | ||
22 | |||
23 | #if BYTE_ORDER == LITTLE_ENDIAN | ||
24 | u_char *s = (u_char *)&y; | ||
25 | return s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3]; | ||
26 | #else | ||
27 | return y; | ||
28 | #endif | ||
29 | } | ||
diff --git a/src/lib/libc/net/ntohs.c b/src/lib/libc/net/ntohs.c new file mode 100644 index 0000000000..93ab83ee4d --- /dev/null +++ b/src/lib/libc/net/ntohs.c | |||
@@ -0,0 +1,27 @@ | |||
1 | /* $NetBSD: ntohs.c,v 1.5 1995/04/28 23:25:23 jtc Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Written by J.T. Conklin <jtc@netbsd.org>. | ||
5 | * Public domain. | ||
6 | */ | ||
7 | |||
8 | #if defined(LIBC_SCCS) && !defined(lint) | ||
9 | static char *rcsid = "$NetBSD: ntohs.c,v 1.5 1995/04/28 23:25:23 jtc Exp $"; | ||
10 | #endif | ||
11 | |||
12 | #include <sys/types.h> | ||
13 | #include <machine/endian.h> | ||
14 | |||
15 | #undef ntohs | ||
16 | |||
17 | unsigned short | ||
18 | ntohs(x) | ||
19 | unsigned short x; | ||
20 | { | ||
21 | #if BYTE_ORDER == LITTLE_ENDIAN | ||
22 | u_char *s = (u_char *) &x; | ||
23 | return s[0] << 8 | s[1]; | ||
24 | #else | ||
25 | return x; | ||
26 | #endif | ||
27 | } | ||
diff --git a/src/lib/libc/net/rcmd.3 b/src/lib/libc/net/rcmd.3 new file mode 100644 index 0000000000..4db847c392 --- /dev/null +++ b/src/lib/libc/net/rcmd.3 | |||
@@ -0,0 +1,204 @@ | |||
1 | .\" $NetBSD: rcmd.3,v 1.8 1995/02/25 06:20:52 cgd Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1983, 1991, 1993 | ||
4 | .\" The Regents of the University of California. All rights reserved. | ||
5 | .\" | ||
6 | .\" Redistribution and use in source and binary forms, with or without | ||
7 | .\" modification, are permitted provided that the following conditions | ||
8 | .\" are met: | ||
9 | .\" 1. Redistributions of source code must retain the above copyright | ||
10 | .\" notice, this list of conditions and the following disclaimer. | ||
11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
12 | .\" notice, this list of conditions and the following disclaimer in the | ||
13 | .\" documentation and/or other materials provided with the distribution. | ||
14 | .\" 3. All advertising materials mentioning features or use of this software | ||
15 | .\" must display the following acknowledgement: | ||
16 | .\" This product includes software developed by the University of | ||
17 | .\" California, Berkeley and its contributors. | ||
18 | .\" 4. Neither the name of the University nor the names of its contributors | ||
19 | .\" may be used to endorse or promote products derived from this software | ||
20 | .\" without specific prior written permission. | ||
21 | .\" | ||
22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
32 | .\" SUCH DAMAGE. | ||
33 | .\" | ||
34 | .\" @(#)rcmd.3 8.1 (Berkeley) 6/4/93 | ||
35 | .\" | ||
36 | .Dd June 4, 1993 | ||
37 | .Dt RCMD 3 | ||
38 | .Os BSD 4.2 | ||
39 | .Sh NAME | ||
40 | .Nm rcmd , | ||
41 | .Nm rresvport , | ||
42 | .Nm iruserok , | ||
43 | .Nm ruserok | ||
44 | .Nd routines for returning a stream to a remote command | ||
45 | .Sh SYNOPSIS | ||
46 | .Fd #include <unistd.h> | ||
47 | .Ft int | ||
48 | .Fn rcmd "char **ahost" "int inport" "const char *locuser" "const char *remuser" "const char *cmd" "int *fd2p" | ||
49 | .Ft int | ||
50 | .Fn rresvport "int *port" | ||
51 | .Ft int | ||
52 | .Fn iruserok "u_long raddr" "int superuser" "const char *ruser" "const char *luser" | ||
53 | .Ft int | ||
54 | .Fn ruserok "const char *rhost" "int superuser" "const char *ruser" "const char *luser" | ||
55 | .Sh DESCRIPTION | ||
56 | The | ||
57 | .Fn rcmd | ||
58 | function | ||
59 | is used by the super-user to execute a command on | ||
60 | a remote machine using an authentication scheme based | ||
61 | on reserved port numbers. | ||
62 | The | ||
63 | .Fn rresvport | ||
64 | function | ||
65 | returns a descriptor to a socket | ||
66 | with an address in the privileged port space. | ||
67 | The | ||
68 | .Fn iruserok | ||
69 | and | ||
70 | .Fn ruserok | ||
71 | functions are used by servers | ||
72 | to authenticate clients requesting service with | ||
73 | .Fn rcmd . | ||
74 | All four functions are present in the same file and are used | ||
75 | by the | ||
76 | .Xr rshd 8 | ||
77 | server (among others). | ||
78 | .Pp | ||
79 | The | ||
80 | .Fn rcmd | ||
81 | function | ||
82 | looks up the host | ||
83 | .Fa *ahost | ||
84 | using | ||
85 | .Xr gethostbyname 3 , | ||
86 | returning \-1 if the host does not exist. | ||
87 | Otherwise | ||
88 | .Fa *ahost | ||
89 | is set to the standard name of the host | ||
90 | and a connection is established to a server | ||
91 | residing at the well-known Internet port | ||
92 | .Fa inport . | ||
93 | .Pp | ||
94 | If the connection succeeds, | ||
95 | a socket in the Internet domain of type | ||
96 | .Dv SOCK_STREAM | ||
97 | is returned to the caller, and given to the remote | ||
98 | command as | ||
99 | .Em stdin | ||
100 | and | ||
101 | .Em stdout . | ||
102 | If | ||
103 | .Fa fd2p | ||
104 | is non-zero, then an auxiliary channel to a control | ||
105 | process will be set up, and a descriptor for it will be placed | ||
106 | in | ||
107 | .Fa *fd2p . | ||
108 | The control process will return diagnostic | ||
109 | output from the command (unit 2) on this channel, and will also | ||
110 | accept bytes on this channel as being | ||
111 | .Tn UNIX | ||
112 | signal numbers, to be | ||
113 | forwarded to the process group of the command. | ||
114 | If | ||
115 | .Fa fd2p | ||
116 | is 0, then the | ||
117 | .Em stderr | ||
118 | (unit 2 of the remote | ||
119 | command) will be made the same as the | ||
120 | .Em stdout | ||
121 | and no | ||
122 | provision is made for sending arbitrary signals to the remote process, | ||
123 | although you may be able to get its attention by using out-of-band data. | ||
124 | .Pp | ||
125 | The protocol is described in detail in | ||
126 | .Xr rshd 8 . | ||
127 | .Pp | ||
128 | The | ||
129 | .Fn rresvport | ||
130 | function is used to obtain a socket with a privileged | ||
131 | address bound to it. This socket is suitable for use | ||
132 | by | ||
133 | .Fn rcmd | ||
134 | and several other functions. Privileged Internet ports are those | ||
135 | in the range 0 to 1023. Only the super-user | ||
136 | is allowed to bind an address of this sort to a socket. | ||
137 | .Pp | ||
138 | The | ||
139 | .Fn iruserok | ||
140 | and | ||
141 | .Fn ruserok | ||
142 | functions take a remote host's IP address or name, respectively, | ||
143 | two user names and a flag indicating whether the local user's | ||
144 | name is that of the super-user. | ||
145 | Then, if the user is | ||
146 | .Em NOT | ||
147 | the super-user, it checks the | ||
148 | .Pa /etc/hosts.equiv | ||
149 | file. | ||
150 | If that lookup is not done, or is unsuccessful, the | ||
151 | .Pa .rhosts | ||
152 | in the local user's home directory is checked to see if the request for | ||
153 | service is allowed. | ||
154 | .Pp | ||
155 | If this file does not exist, is not a regular file, is owned by anyone | ||
156 | other than the user or the super-user, or is writeable by anyone other | ||
157 | than the owner, the check automatically fails. | ||
158 | Zero is returned if the machine name is listed in the | ||
159 | .Dq Pa hosts.equiv | ||
160 | file, or the host and remote user name are found in the | ||
161 | .Dq Pa .rhosts | ||
162 | file; otherwise | ||
163 | .Fn iruserok | ||
164 | and | ||
165 | .Fn ruserok | ||
166 | return \-1. | ||
167 | If the local domain (as obtained from | ||
168 | .Xr gethostname 2 ) | ||
169 | is the same as the remote domain, only the machine name need be specified. | ||
170 | .Pp | ||
171 | If the IP address of the remote host is known, | ||
172 | .Fn iruserok | ||
173 | should be used in preference to | ||
174 | .Fn ruserok , | ||
175 | as it does not require trusting the DNS server for the remote host's domain. | ||
176 | .Sh DIAGNOSTICS | ||
177 | The | ||
178 | .Fn rcmd | ||
179 | function | ||
180 | returns a valid socket descriptor on success. | ||
181 | It returns \-1 on error and prints a diagnostic message on the standard error. | ||
182 | .Pp | ||
183 | The | ||
184 | .Fn rresvport | ||
185 | function | ||
186 | returns a valid, bound socket descriptor on success. | ||
187 | It returns \-1 on error with the global value | ||
188 | .Va errno | ||
189 | set according to the reason for failure. | ||
190 | The error code | ||
191 | .Dv EAGAIN | ||
192 | is overloaded to mean ``All network ports in use.'' | ||
193 | .Sh SEE ALSO | ||
194 | .Xr rlogin 1 , | ||
195 | .Xr rsh 1 , | ||
196 | .Xr intro 2 , | ||
197 | .Xr rexec 3 , | ||
198 | .Xr rexecd 8 , | ||
199 | .Xr rlogind 8 , | ||
200 | .Xr rshd 8 | ||
201 | .Sh HISTORY | ||
202 | These | ||
203 | functions appeared in | ||
204 | .Bx 4.2 . | ||
diff --git a/src/lib/libc/net/rcmd.c b/src/lib/libc/net/rcmd.c new file mode 100644 index 0000000000..e0310031b0 --- /dev/null +++ b/src/lib/libc/net/rcmd.c | |||
@@ -0,0 +1,517 @@ | |||
1 | /* $NetBSD: rcmd.c,v 1.12 1995/06/03 22:33:34 mycroft Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1983, 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 the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | */ | ||
35 | |||
36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
37 | #if 0 | ||
38 | static char sccsid[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94"; | ||
39 | #else | ||
40 | static char *rcsid = "$NetBSD: rcmd.c,v 1.12 1995/06/03 22:33:34 mycroft Exp $"; | ||
41 | #endif | ||
42 | #endif /* LIBC_SCCS and not lint */ | ||
43 | |||
44 | #include <sys/param.h> | ||
45 | #include <sys/socket.h> | ||
46 | #include <sys/stat.h> | ||
47 | |||
48 | #include <netinet/in.h> | ||
49 | #include <arpa/inet.h> | ||
50 | |||
51 | #include <signal.h> | ||
52 | #include <fcntl.h> | ||
53 | #include <netdb.h> | ||
54 | #include <unistd.h> | ||
55 | #include <pwd.h> | ||
56 | #include <errno.h> | ||
57 | #include <stdio.h> | ||
58 | #include <ctype.h> | ||
59 | #include <string.h> | ||
60 | |||
61 | int __ivaliduser __P((FILE *, u_long, const char *, const char *)); | ||
62 | static int __icheckhost __P((u_long, const char *)); | ||
63 | |||
64 | int | ||
65 | rcmd(ahost, rport, locuser, remuser, cmd, fd2p) | ||
66 | char **ahost; | ||
67 | u_short rport; | ||
68 | const char *locuser, *remuser, *cmd; | ||
69 | int *fd2p; | ||
70 | { | ||
71 | struct hostent *hp; | ||
72 | struct sockaddr_in sin, from; | ||
73 | fd_set reads; | ||
74 | long oldmask; | ||
75 | pid_t pid; | ||
76 | int s, lport, timo; | ||
77 | char c; | ||
78 | |||
79 | pid = getpid(); | ||
80 | hp = gethostbyname(*ahost); | ||
81 | if (hp == NULL) { | ||
82 | herror(*ahost); | ||
83 | return (-1); | ||
84 | } | ||
85 | *ahost = hp->h_name; | ||
86 | oldmask = sigblock(sigmask(SIGURG)); | ||
87 | for (timo = 1, lport = IPPORT_RESERVED - 1;;) { | ||
88 | s = rresvport(&lport); | ||
89 | if (s < 0) { | ||
90 | if (errno == EAGAIN) | ||
91 | (void)fprintf(stderr, | ||
92 | "rcmd: socket: All ports in use\n"); | ||
93 | else | ||
94 | (void)fprintf(stderr, "rcmd: socket: %s\n", | ||
95 | strerror(errno)); | ||
96 | sigsetmask(oldmask); | ||
97 | return (-1); | ||
98 | } | ||
99 | fcntl(s, F_SETOWN, pid); | ||
100 | sin.sin_len = sizeof(struct sockaddr_in); | ||
101 | sin.sin_family = hp->h_addrtype; | ||
102 | sin.sin_port = rport; | ||
103 | bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length); | ||
104 | if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) | ||
105 | break; | ||
106 | (void)close(s); | ||
107 | if (errno == EADDRINUSE) { | ||
108 | lport--; | ||
109 | continue; | ||
110 | } | ||
111 | if (errno == ECONNREFUSED && timo <= 16) { | ||
112 | (void)sleep(timo); | ||
113 | timo *= 2; | ||
114 | continue; | ||
115 | } | ||
116 | if (hp->h_addr_list[1] != NULL) { | ||
117 | int oerrno = errno; | ||
118 | |||
119 | (void)fprintf(stderr, "connect to address %s: ", | ||
120 | inet_ntoa(sin.sin_addr)); | ||
121 | errno = oerrno; | ||
122 | perror(0); | ||
123 | hp->h_addr_list++; | ||
124 | bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length); | ||
125 | (void)fprintf(stderr, "Trying %s...\n", | ||
126 | inet_ntoa(sin.sin_addr)); | ||
127 | continue; | ||
128 | } | ||
129 | (void)fprintf(stderr, "%s: %s\n", hp->h_name, strerror(errno)); | ||
130 | sigsetmask(oldmask); | ||
131 | return (-1); | ||
132 | } | ||
133 | lport--; | ||
134 | if (fd2p == 0) { | ||
135 | write(s, "", 1); | ||
136 | lport = 0; | ||
137 | } else { | ||
138 | char num[8]; | ||
139 | int s2 = rresvport(&lport), s3; | ||
140 | int len = sizeof(from); | ||
141 | |||
142 | if (s2 < 0) | ||
143 | goto bad; | ||
144 | listen(s2, 1); | ||
145 | (void)snprintf(num, sizeof(num), "%d", lport); | ||
146 | if (write(s, num, strlen(num)+1) != strlen(num)+1) { | ||
147 | (void)fprintf(stderr, | ||
148 | "rcmd: write (setting up stderr): %s\n", | ||
149 | strerror(errno)); | ||
150 | (void)close(s2); | ||
151 | goto bad; | ||
152 | } | ||
153 | FD_ZERO(&reads); | ||
154 | FD_SET(s, &reads); | ||
155 | FD_SET(s2, &reads); | ||
156 | errno = 0; | ||
157 | if (select(MAX(s, s2) + 1, &reads, 0, 0, 0) < 1 || | ||
158 | !FD_ISSET(s2, &reads)) { | ||
159 | if (errno != 0) | ||
160 | (void)fprintf(stderr, | ||
161 | "rcmd: select (setting up stderr): %s\n", | ||
162 | strerror(errno)); | ||
163 | else | ||
164 | (void)fprintf(stderr, | ||
165 | "select: protocol failure in circuit setup\n"); | ||
166 | (void)close(s2); | ||
167 | goto bad; | ||
168 | } | ||
169 | s3 = accept(s2, (struct sockaddr *)&from, &len); | ||
170 | (void)close(s2); | ||
171 | if (s3 < 0) { | ||
172 | (void)fprintf(stderr, | ||
173 | "rcmd: accept: %s\n", strerror(errno)); | ||
174 | lport = 0; | ||
175 | goto bad; | ||
176 | } | ||
177 | *fd2p = s3; | ||
178 | from.sin_port = ntohs(from.sin_port); | ||
179 | if (from.sin_family != AF_INET || | ||
180 | from.sin_port >= IPPORT_RESERVED || | ||
181 | from.sin_port < IPPORT_RESERVED / 2) { | ||
182 | (void)fprintf(stderr, | ||
183 | "socket: protocol failure in circuit setup.\n"); | ||
184 | goto bad2; | ||
185 | } | ||
186 | } | ||
187 | (void)write(s, locuser, strlen(locuser)+1); | ||
188 | (void)write(s, remuser, strlen(remuser)+1); | ||
189 | (void)write(s, cmd, strlen(cmd)+1); | ||
190 | if (read(s, &c, 1) != 1) { | ||
191 | (void)fprintf(stderr, | ||
192 | "rcmd: %s: %s\n", *ahost, strerror(errno)); | ||
193 | goto bad2; | ||
194 | } | ||
195 | if (c != 0) { | ||
196 | while (read(s, &c, 1) == 1) { | ||
197 | (void)write(STDERR_FILENO, &c, 1); | ||
198 | if (c == '\n') | ||
199 | break; | ||
200 | } | ||
201 | goto bad2; | ||
202 | } | ||
203 | sigsetmask(oldmask); | ||
204 | return (s); | ||
205 | bad2: | ||
206 | if (lport) | ||
207 | (void)close(*fd2p); | ||
208 | bad: | ||
209 | (void)close(s); | ||
210 | sigsetmask(oldmask); | ||
211 | return (-1); | ||
212 | } | ||
213 | |||
214 | int | ||
215 | rresvport(alport) | ||
216 | int *alport; | ||
217 | { | ||
218 | struct sockaddr_in sin; | ||
219 | int s; | ||
220 | |||
221 | sin.sin_len = sizeof(struct sockaddr_in); | ||
222 | sin.sin_family = AF_INET; | ||
223 | sin.sin_addr.s_addr = INADDR_ANY; | ||
224 | s = socket(AF_INET, SOCK_STREAM, 0); | ||
225 | if (s < 0) | ||
226 | return (-1); | ||
227 | for (;;) { | ||
228 | sin.sin_port = htons((u_short)*alport); | ||
229 | if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) | ||
230 | return (s); | ||
231 | if (errno != EADDRINUSE) { | ||
232 | (void)close(s); | ||
233 | return (-1); | ||
234 | } | ||
235 | (*alport)--; | ||
236 | if (*alport == IPPORT_RESERVED/2) { | ||
237 | (void)close(s); | ||
238 | errno = EAGAIN; /* close */ | ||
239 | return (-1); | ||
240 | } | ||
241 | } | ||
242 | } | ||
243 | |||
244 | int __check_rhosts_file = 1; | ||
245 | char *__rcmd_errstr; | ||
246 | |||
247 | int | ||
248 | ruserok(rhost, superuser, ruser, luser) | ||
249 | const char *rhost, *ruser, *luser; | ||
250 | int superuser; | ||
251 | { | ||
252 | struct hostent *hp; | ||
253 | char **ap; | ||
254 | int i; | ||
255 | #define MAXADDRS 35 | ||
256 | u_long addrs[MAXADDRS + 1]; | ||
257 | |||
258 | if ((hp = gethostbyname(rhost)) == NULL) | ||
259 | return (-1); | ||
260 | for (i = 0, ap = hp->h_addr_list; *ap && i < MAXADDRS; ++ap, ++i) | ||
261 | bcopy(*ap, &addrs[i], sizeof(addrs[i])); | ||
262 | addrs[i] = 0; | ||
263 | |||
264 | for (i = 0; i < MAXADDRS && addrs[i]; i++) | ||
265 | if (iruserok(addrs[i], superuser, ruser, luser) == 0) | ||
266 | return (0); | ||
267 | return (-1); | ||
268 | } | ||
269 | |||
270 | /* | ||
271 | * New .rhosts strategy: We are passed an ip address. We spin through | ||
272 | * hosts.equiv and .rhosts looking for a match. When the .rhosts only | ||
273 | * has ip addresses, we don't have to trust a nameserver. When it | ||
274 | * contains hostnames, we spin through the list of addresses the nameserver | ||
275 | * gives us and look for a match. | ||
276 | * | ||
277 | * Returns 0 if ok, -1 if not ok. | ||
278 | */ | ||
279 | int | ||
280 | iruserok(raddr, superuser, ruser, luser) | ||
281 | u_long raddr; | ||
282 | int superuser; | ||
283 | const char *ruser, *luser; | ||
284 | { | ||
285 | register char *cp; | ||
286 | struct stat sbuf; | ||
287 | struct passwd *pwd; | ||
288 | FILE *hostf; | ||
289 | uid_t uid; | ||
290 | int first; | ||
291 | char pbuf[MAXPATHLEN]; | ||
292 | |||
293 | first = 1; | ||
294 | hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r"); | ||
295 | again: | ||
296 | if (hostf) { | ||
297 | if (__ivaliduser(hostf, raddr, luser, ruser) == 0) { | ||
298 | (void)fclose(hostf); | ||
299 | return (0); | ||
300 | } | ||
301 | (void)fclose(hostf); | ||
302 | } | ||
303 | if (first == 1 && (__check_rhosts_file || superuser)) { | ||
304 | first = 0; | ||
305 | if ((pwd = getpwnam(luser)) == NULL) | ||
306 | return (-1); | ||
307 | (void)strcpy(pbuf, pwd->pw_dir); | ||
308 | (void)strcat(pbuf, "/.rhosts"); | ||
309 | |||
310 | /* | ||
311 | * Change effective uid while opening .rhosts. If root and | ||
312 | * reading an NFS mounted file system, can't read files that | ||
313 | * are protected read/write owner only. | ||
314 | */ | ||
315 | uid = geteuid(); | ||
316 | (void)seteuid(pwd->pw_uid); | ||
317 | hostf = fopen(pbuf, "r"); | ||
318 | (void)seteuid(uid); | ||
319 | |||
320 | if (hostf == NULL) | ||
321 | return (-1); | ||
322 | /* | ||
323 | * If not a regular file, or is owned by someone other than | ||
324 | * user or root or if writeable by anyone but the owner, quit. | ||
325 | */ | ||
326 | cp = NULL; | ||
327 | if (lstat(pbuf, &sbuf) < 0) | ||
328 | cp = ".rhosts lstat failed"; | ||
329 | else if (!S_ISREG(sbuf.st_mode)) | ||
330 | cp = ".rhosts not regular file"; | ||
331 | else if (fstat(fileno(hostf), &sbuf) < 0) | ||
332 | cp = ".rhosts fstat failed"; | ||
333 | else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) | ||
334 | cp = "bad .rhosts owner"; | ||
335 | else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) | ||
336 | cp = ".rhosts writeable by other than owner"; | ||
337 | /* If there were any problems, quit. */ | ||
338 | if (cp) { | ||
339 | __rcmd_errstr = cp; | ||
340 | (void)fclose(hostf); | ||
341 | return (-1); | ||
342 | } | ||
343 | goto again; | ||
344 | } | ||
345 | return (-1); | ||
346 | } | ||
347 | |||
348 | /* | ||
349 | * XXX | ||
350 | * Don't make static, used by lpd(8). | ||
351 | * | ||
352 | * Returns 0 if ok, -1 if not ok. | ||
353 | */ | ||
354 | int | ||
355 | __ivaliduser(hostf, raddr, luser, ruser) | ||
356 | FILE *hostf; | ||
357 | u_long raddr; | ||
358 | const char *luser, *ruser; | ||
359 | { | ||
360 | register char *user, *p; | ||
361 | int ch; | ||
362 | char buf[MAXHOSTNAMELEN + 128]; /* host + login */ | ||
363 | const char *auser, *ahost; | ||
364 | int hostok, userok; | ||
365 | char rhost[MAXHOSTNAMELEN]; | ||
366 | struct hostent *hp; | ||
367 | char domain[MAXHOSTNAMELEN]; | ||
368 | |||
369 | getdomainname(domain, sizeof(domain)); | ||
370 | |||
371 | while (fgets(buf, sizeof(buf), hostf)) { | ||
372 | p = buf; | ||
373 | /* Skip lines that are too long. */ | ||
374 | if (strchr(p, '\n') == NULL) { | ||
375 | while ((ch = getc(hostf)) != '\n' && ch != EOF); | ||
376 | continue; | ||
377 | } | ||
378 | while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') { | ||
379 | *p = isupper(*p) ? tolower(*p) : *p; | ||
380 | p++; | ||
381 | } | ||
382 | if (*p == ' ' || *p == '\t') { | ||
383 | *p++ = '\0'; | ||
384 | while (*p == ' ' || *p == '\t') | ||
385 | p++; | ||
386 | user = p; | ||
387 | while (*p != '\n' && *p != ' ' && | ||
388 | *p != '\t' && *p != '\0') | ||
389 | p++; | ||
390 | } else | ||
391 | user = p; | ||
392 | *p = '\0'; | ||
393 | |||
394 | if (p == buf) | ||
395 | continue; | ||
396 | |||
397 | auser = *user ? user : luser; | ||
398 | ahost = buf; | ||
399 | |||
400 | if ((hp = gethostbyaddr((char *) &raddr, | ||
401 | sizeof(raddr), AF_INET)) == NULL) { | ||
402 | abort(); | ||
403 | return -1; | ||
404 | } | ||
405 | (void) strncpy(rhost, hp->h_name, sizeof(rhost)); | ||
406 | rhost[sizeof(rhost) - 1] = '\0'; | ||
407 | |||
408 | if (ahost[0] == '+') | ||
409 | switch (ahost[1]) { | ||
410 | case '\0': | ||
411 | hostok = 1; | ||
412 | break; | ||
413 | |||
414 | case '@': | ||
415 | hostok = innetgr(&ahost[2], rhost, NULL, | ||
416 | domain); | ||
417 | break; | ||
418 | |||
419 | default: | ||
420 | hostok = __icheckhost(raddr, &ahost[1]); | ||
421 | break; | ||
422 | } | ||
423 | else if (ahost[0] == '-') | ||
424 | switch (ahost[1]) { | ||
425 | case '\0': | ||
426 | hostok = -1; | ||
427 | break; | ||
428 | |||
429 | case '@': | ||
430 | hostok = -innetgr(&ahost[2], rhost, NULL, | ||
431 | domain); | ||
432 | break; | ||
433 | |||
434 | default: | ||
435 | hostok = -__icheckhost(raddr, &ahost[1]); | ||
436 | break; | ||
437 | } | ||
438 | else | ||
439 | hostok = __icheckhost(raddr, ahost); | ||
440 | |||
441 | |||
442 | if (auser[0] == '+') | ||
443 | switch (auser[1]) { | ||
444 | case '\0': | ||
445 | userok = 1; | ||
446 | break; | ||
447 | |||
448 | case '@': | ||
449 | userok = innetgr(&auser[2], NULL, ruser, | ||
450 | domain); | ||
451 | break; | ||
452 | |||
453 | default: | ||
454 | userok = strcmp(ruser, &auser[1]) == 0; | ||
455 | break; | ||
456 | } | ||
457 | else if (auser[0] == '-') | ||
458 | switch (auser[1]) { | ||
459 | case '\0': | ||
460 | userok = -1; | ||
461 | break; | ||
462 | |||
463 | case '@': | ||
464 | userok = -innetgr(&auser[2], NULL, ruser, | ||
465 | domain); | ||
466 | break; | ||
467 | |||
468 | default: | ||
469 | userok = -(strcmp(ruser, &auser[1]) == 0); | ||
470 | break; | ||
471 | } | ||
472 | else | ||
473 | userok = strcmp(ruser, auser) == 0; | ||
474 | |||
475 | /* Check if one component did not match */ | ||
476 | if (hostok == 0 || userok == 0) | ||
477 | continue; | ||
478 | |||
479 | /* Check if we got a forbidden pair */ | ||
480 | if (userok == -1 || hostok == -1) | ||
481 | return -1; | ||
482 | |||
483 | /* Check if we got a valid pair */ | ||
484 | if (hostok == 1 && userok == 1) | ||
485 | return 0; | ||
486 | } | ||
487 | return -1; | ||
488 | } | ||
489 | |||
490 | /* | ||
491 | * Returns "true" if match, 0 if no match. | ||
492 | */ | ||
493 | static int | ||
494 | __icheckhost(raddr, lhost) | ||
495 | u_long raddr; | ||
496 | const char *lhost; | ||
497 | { | ||
498 | register struct hostent *hp; | ||
499 | register u_long laddr; | ||
500 | register char **pp; | ||
501 | |||
502 | /* Try for raw ip address first. */ | ||
503 | if (isdigit(*lhost) && (long)(laddr = inet_addr(lhost)) != -1) | ||
504 | return (raddr == laddr); | ||
505 | |||
506 | /* Better be a hostname. */ | ||
507 | if ((hp = gethostbyname(lhost)) == NULL) | ||
508 | return (0); | ||
509 | |||
510 | /* Spin through ip addresses. */ | ||
511 | for (pp = hp->h_addr_list; *pp; ++pp) | ||
512 | if (!bcmp(&raddr, *pp, sizeof(u_long))) | ||
513 | return (1); | ||
514 | |||
515 | /* No match. */ | ||
516 | return (0); | ||
517 | } | ||
diff --git a/src/lib/libc/net/recv.c b/src/lib/libc/net/recv.c new file mode 100644 index 0000000000..44296378cb --- /dev/null +++ b/src/lib/libc/net/recv.c | |||
@@ -0,0 +1,56 @@ | |||
1 | /* $NetBSD: recv.c,v 1.6 1995/02/25 06:20:54 cgd Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1988, 1993 | ||
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 the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | */ | ||
35 | |||
36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
37 | #if 0 | ||
38 | static char sccsid[] = "@(#)recv.c 8.2 (Berkeley) 2/21/94"; | ||
39 | #else | ||
40 | static char rcsid[] = "$NetBSD: recv.c,v 1.6 1995/02/25 06:20:54 cgd Exp $"; | ||
41 | #endif | ||
42 | #endif /* LIBC_SCCS and not lint */ | ||
43 | |||
44 | #include <sys/types.h> | ||
45 | #include <sys/socket.h> | ||
46 | |||
47 | #include <stddef.h> | ||
48 | |||
49 | ssize_t | ||
50 | recv(s, buf, len, flags) | ||
51 | int s, flags; | ||
52 | size_t len; | ||
53 | void *buf; | ||
54 | { | ||
55 | return (recvfrom(s, buf, len, flags, NULL, 0)); | ||
56 | } | ||
diff --git a/src/lib/libc/net/res_comp.c b/src/lib/libc/net/res_comp.c new file mode 100644 index 0000000000..9d7bbecdda --- /dev/null +++ b/src/lib/libc/net/res_comp.c | |||
@@ -0,0 +1,364 @@ | |||
1 | /* $NetBSD: res_comp.c,v 1.6 1995/02/25 06:20:55 cgd Exp $ */ | ||
2 | |||
3 | /*- | ||
4 | * Copyright (c) 1985, 1993 | ||
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 the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | * - | ||
35 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
36 | * | ||
37 | * Permission to use, copy, modify, and distribute this software for any | ||
38 | * purpose with or without fee is hereby granted, provided that the above | ||
39 | * copyright notice and this permission notice appear in all copies, and that | ||
40 | * the name of Digital Equipment Corporation not be used in advertising or | ||
41 | * publicity pertaining to distribution of the document or software without | ||
42 | * specific, written prior permission. | ||
43 | * | ||
44 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
45 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
46 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
47 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
48 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
49 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
50 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
51 | * SOFTWARE. | ||
52 | * - | ||
53 | * --Copyright-- | ||
54 | */ | ||
55 | |||
56 | #if defined(LIBC_SCCS) && !defined(lint) | ||
57 | #if 0 | ||
58 | static char sccsid[] = "@(#)res_comp.c 8.1 (Berkeley) 6/4/93"; | ||
59 | static char rcsid[] = "$Id: res_comp.c,v 4.9.1.1 1993/05/02 22:43:03 vixie Rel "; | ||
60 | #else | ||
61 | static char rcsid[] = "$NetBSD: res_comp.c,v 1.6 1995/02/25 06:20:55 cgd Exp $"; | ||
62 | #endif | ||
63 | #endif /* LIBC_SCCS and not lint */ | ||
64 | |||
65 | #include <sys/param.h> | ||
66 | #include <arpa/nameser.h> | ||
67 | #include <netinet/in.h> | ||
68 | #include <resolv.h> | ||
69 | #include <stdio.h> | ||
70 | |||
71 | static int dn_find(); | ||
72 | |||
73 | /* | ||
74 | * Expand compressed domain name 'comp_dn' to full domain name. | ||
75 | * 'msg' is a pointer to the begining of the message, | ||
76 | * 'eomorig' points to the first location after the message, | ||
77 | * 'exp_dn' is a pointer to a buffer of size 'length' for the result. | ||
78 | * Return size of compressed name or -1 if there was an error. | ||
79 | */ | ||
80 | dn_expand(msg, eomorig, comp_dn, exp_dn, length) | ||
81 | const u_char *msg, *eomorig, *comp_dn; | ||
82 | u_char *exp_dn; | ||
83 | int length; | ||
84 | { | ||
85 | register u_char *cp, *dn; | ||
86 | register int n, c; | ||
87 | u_char *eom; | ||
88 | int len = -1, checked = 0; | ||
89 | |||
90 | dn = exp_dn; | ||
91 | cp = (u_char *)comp_dn; | ||
92 | eom = exp_dn + length; | ||
93 | /* | ||
94 | * fetch next label in domain name | ||
95 | */ | ||
96 | while (n = *cp++) { | ||
97 | /* | ||
98 | * Check for indirection | ||
99 | */ | ||
100 | switch (n & INDIR_MASK) { | ||
101 | case 0: | ||
102 | if (dn != exp_dn) { | ||
103 | if (dn >= eom) | ||
104 | return (-1); | ||
105 | *dn++ = '.'; | ||
106 | } | ||
107 | if (dn+n >= eom) | ||
108 | return (-1); | ||
109 | checked += n + 1; | ||
110 | while (--n >= 0) { | ||
111 | if ((c = *cp++) == '.') { | ||
112 | if (dn + n + 2 >= eom) | ||
113 | return (-1); | ||
114 | *dn++ = '\\'; | ||
115 | } | ||
116 | *dn++ = c; | ||
117 | if (cp >= eomorig) /* out of range */ | ||
118 | return(-1); | ||
119 | } | ||
120 | break; | ||
121 | |||
122 | case INDIR_MASK: | ||
123 | if (len < 0) | ||
124 | len = cp - comp_dn + 1; | ||
125 | cp = (u_char *)msg + (((n & 0x3f) << 8) | (*cp & 0xff)); | ||
126 | if (cp < msg || cp >= eomorig) /* out of range */ | ||
127 | return(-1); | ||
128 | checked += 2; | ||
129 | /* | ||
130 | * Check for loops in the compressed name; | ||
131 | * if we've looked at the whole message, | ||
132 | * there must be a loop. | ||
133 | */ | ||
134 | if (checked >= eomorig - msg) | ||
135 | return (-1); | ||
136 | break; | ||
137 | |||
138 | default: | ||
139 | return (-1); /* flag error */ | ||
140 | } | ||
141 | } | ||
142 | *dn = '\0'; | ||
143 | if (len < 0) | ||
144 | len = cp - comp_dn; | ||
145 | return (len); | ||
146 | } | ||
147 | |||
148 | /* | ||
149 | * Compress domain name 'exp_dn' into 'comp_dn'. | ||
150 | * Return the size of the compressed name or -1. | ||
151 | * 'length' is the size of the array pointed to by 'comp_dn'. | ||
152 | * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0] | ||
153 | * is a pointer to the beginning of the message. The list ends with NULL. | ||
154 | * 'lastdnptr' is a pointer to the end of the arrary pointed to | ||
155 | * by 'dnptrs'. Side effect is to update the list of pointers for | ||
156 | * labels inserted into the message as we compress the name. | ||
157 | * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr' | ||
158 | * is NULL, we don't update the list. | ||
159 | */ | ||
160 | dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr) | ||
161 | const u_char *exp_dn; | ||
162 | u_char *comp_dn, **dnptrs, **lastdnptr; | ||
163 | int length; | ||
164 | { | ||
165 | register u_char *cp, *dn; | ||
166 | register int c, l; | ||
167 | u_char **cpp, **lpp, *sp, *eob; | ||
168 | u_char *msg; | ||
169 | |||
170 | dn = (u_char *)exp_dn; | ||
171 | cp = comp_dn; | ||
172 | eob = cp + length; | ||
173 | if (dnptrs != NULL) { | ||
174 | if ((msg = *dnptrs++) != NULL) { | ||
175 | for (cpp = dnptrs; *cpp != NULL; cpp++) | ||
176 | ; | ||
177 | lpp = cpp; /* end of list to search */ | ||
178 | } | ||
179 | } else | ||
180 | msg = NULL; | ||
181 | for (c = *dn++; c != '\0'; ) { | ||
182 | /* look to see if we can use pointers */ | ||
183 | if (msg != NULL) { | ||
184 | if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) { | ||
185 | if (cp+1 >= eob) | ||
186 | return (-1); | ||
187 | *cp++ = (l >> 8) | INDIR_MASK; | ||
188 | *cp++ = l % 256; | ||
189 | return (cp - comp_dn); | ||
190 | } | ||
191 | /* not found, save it */ | ||
192 | if (lastdnptr != NULL && cpp < lastdnptr-1) { | ||
193 | *cpp++ = cp; | ||
194 | *cpp = NULL; | ||
195 | } | ||
196 | } | ||
197 | sp = cp++; /* save ptr to length byte */ | ||
198 | do { | ||
199 | if (c == '.') { | ||
200 | c = *dn++; | ||
201 | break; | ||
202 | } | ||
203 | if (c == '\\') { | ||
204 | if ((c = *dn++) == '\0') | ||
205 | break; | ||
206 | } | ||
207 | if (cp >= eob) { | ||
208 | if (msg != NULL) | ||
209 | *lpp = NULL; | ||
210 | return (-1); | ||
211 | } | ||
212 | *cp++ = c; | ||
213 | } while ((c = *dn++) != '\0'); | ||
214 | /* catch trailing '.'s but not '..' */ | ||
215 | if ((l = cp - sp - 1) == 0 && c == '\0') { | ||
216 | cp--; | ||
217 | break; | ||
218 | } | ||
219 | if (l <= 0 || l > MAXLABEL) { | ||
220 | if (msg != NULL) | ||
221 | *lpp = NULL; | ||
222 | return (-1); | ||
223 | } | ||
224 | *sp = l; | ||
225 | } | ||
226 | if (cp >= eob) { | ||
227 | if (msg != NULL) | ||
228 | *lpp = NULL; | ||
229 | return (-1); | ||
230 | } | ||
231 | *cp++ = '\0'; | ||
232 | return (cp - comp_dn); | ||
233 | } | ||
234 | |||
235 | /* | ||
236 | * Skip over a compressed domain name. Return the size or -1. | ||
237 | */ | ||
238 | __dn_skipname(comp_dn, eom) | ||
239 | const u_char *comp_dn, *eom; | ||
240 | { | ||
241 | register u_char *cp; | ||
242 | register int n; | ||
243 | |||
244 | cp = (u_char *)comp_dn; | ||
245 | while (cp < eom && (n = *cp++)) { | ||
246 | /* | ||
247 | * check for indirection | ||
248 | */ | ||
249 | switch (n & INDIR_MASK) { | ||
250 | case 0: /* normal case, n == len */ | ||
251 | cp += n; | ||
252 | continue; | ||
253 | case INDIR_MASK: /* indirection */ | ||
254 | cp++; | ||
255 | break; | ||
256 | default: /* illegal type */ | ||
257 | return (-1); | ||
258 | } | ||
259 | break; | ||
260 | } | ||
261 | if (cp > eom) | ||
262 | return -1; | ||
263 | return (cp - comp_dn); | ||
264 | } | ||
265 | |||
266 | /* | ||
267 | * Search for expanded name from a list of previously compressed names. | ||
268 | * Return the offset from msg if found or -1. | ||
269 | * dnptrs is the pointer to the first name on the list, | ||
270 | * not the pointer to the start of the message. | ||
271 | */ | ||
272 | static int | ||
273 | dn_find(exp_dn, msg, dnptrs, lastdnptr) | ||
274 | u_char *exp_dn, *msg; | ||
275 | u_char **dnptrs, **lastdnptr; | ||
276 | { | ||
277 | register u_char *dn, *cp, **cpp; | ||
278 | register int n; | ||
279 | u_char *sp; | ||
280 | |||
281 | for (cpp = dnptrs; cpp < lastdnptr; cpp++) { | ||
282 | dn = exp_dn; | ||
283 | sp = cp = *cpp; | ||
284 | while (n = *cp++) { | ||
285 | /* | ||
286 | * check for indirection | ||
287 | */ | ||
288 | switch (n & INDIR_MASK) { | ||
289 | case 0: /* normal case, n == len */ | ||
290 | while (--n >= 0) { | ||
291 | if (*dn == '.') | ||
292 | goto next; | ||
293 | if (*dn == '\\') | ||
294 | dn++; | ||
295 | if (*dn++ != *cp++) | ||
296 | goto next; | ||
297 | } | ||
298 | if ((n = *dn++) == '\0' && *cp == '\0') | ||
299 | return (sp - msg); | ||
300 | if (n == '.') | ||
301 | continue; | ||
302 | goto next; | ||
303 | |||
304 | default: /* illegal type */ | ||
305 | return (-1); | ||
306 | |||
307 | case INDIR_MASK: /* indirection */ | ||
308 | cp = msg + (((n & 0x3f) << 8) | *cp); | ||
309 | } | ||
310 | } | ||
311 | if (*dn == '\0') | ||
312 | return (sp - msg); | ||
313 | next: ; | ||
314 | } | ||
315 | return (-1); | ||
316 | } | ||
317 | |||
318 | /* | ||
319 | * Routines to insert/extract short/long's. Must account for byte | ||
320 | * order and non-alignment problems. This code at least has the | ||
321 | * advantage of being portable. | ||
322 | * | ||
323 | * used by sendmail. | ||
324 | */ | ||
325 | |||
326 | u_short | ||
327 | _getshort(msgp) | ||
328 | register u_char *msgp; | ||
329 | { | ||
330 | register u_int16_t u; | ||
331 | |||
332 | GETSHORT(u, msgp); | ||
333 | return (u); | ||
334 | } | ||
335 | |||
336 | u_int32_t | ||
337 | _getlong(msgp) | ||
338 | register u_char *msgp; | ||
339 | { | ||
340 | register u_int32_t u; | ||
341 | |||
342 | GETLONG(u, msgp); | ||
343 | return (u); | ||
344 | } | ||
345 | |||
346 | void | ||
347 | #if defined(__STDC__) || defined(__cplusplus) | ||
348 | __putshort(register u_int16_t s, register u_char *msgp) | ||
349 | #else | ||
350 | __putshort(s, msgp) | ||
351 | register u_int16_t s; | ||
352 | register u_char *msgp; | ||
353 | #endif | ||
354 | { | ||
355 | PUTSHORT(s, msgp); | ||
356 | } | ||
357 | |||
358 | void | ||
359 | __putlong(l, msgp) | ||
360 | register u_int32_t l; | ||
361 | register u_char *msgp; | ||
362 | { | ||
363 | PUTLONG(l, msgp); | ||
364 | } | ||
diff --git a/src/lib/libc/net/res_debug.c b/src/lib/libc/net/res_debug.c new file mode 100644 index 0000000000..d841293f18 --- /dev/null +++ b/src/lib/libc/net/res_debug.c | |||
@@ -0,0 +1,749 @@ | |||
1 | /* $NetBSD: res_debug.c,v 1.7 1995/02/25 06:20:56 cgd Exp $ */ | ||
2 | |||
3 | /*- | ||
4 | * Copyright (c) 1985, 1990, 1993 | ||
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 the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | * - | ||
35 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
36 | * | ||
37 | * Permission to use, copy, modify, and distribute this software for any | ||
38 | * purpose with or without fee is hereby granted, provided that the above | ||
39 | * copyright notice and this permission notice appear in all copies, and that | ||
40 | * the name of Digital Equipment Corporation not be used in advertising or | ||
41 | * publicity pertaining to distribution of the document or software without | ||
42 | * specific, written prior permission. | ||
43 | * | ||
44 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
45 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
46 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
47 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
48 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
49 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
50 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
51 | * SOFTWARE. | ||
52 | * - | ||
53 | * --Copyright-- | ||
54 | */ | ||
55 | |||
56 | #if defined(LIBC_SCCS) && !defined(lint) | ||
57 | #if 0 | ||
58 | static char sccsid[] = "@(#)res_debug.c 8.1 (Berkeley) 6/4/93"; | ||
59 | #else | ||
60 | static char rcsid[] = "$NetBSD: res_debug.c,v 1.7 1995/02/25 06:20:56 cgd Exp $"; | ||
61 | #endif | ||
62 | #endif /* LIBC_SCCS and not lint */ | ||
63 | |||
64 | #include <sys/param.h> | ||
65 | #include <netinet/in.h> | ||
66 | #include <arpa/inet.h> | ||
67 | #include <arpa/nameser.h> | ||
68 | #include <resolv.h> | ||
69 | #include <stdio.h> | ||
70 | #include <string.h> | ||
71 | |||
72 | void __fp_query(); | ||
73 | char *__p_class(), *__p_time(), *__p_type(); | ||
74 | char *p_cdname(), *p_fqname(), *p_rr(); | ||
75 | static char *p_option __P((u_int32_t)); | ||
76 | |||
77 | char *_res_opcodes[] = { | ||
78 | "QUERY", | ||
79 | "IQUERY", | ||
80 | "CQUERYM", | ||
81 | "CQUERYU", | ||
82 | "4", | ||
83 | "5", | ||
84 | "6", | ||
85 | "7", | ||
86 | "8", | ||
87 | "UPDATEA", | ||
88 | "UPDATED", | ||
89 | "UPDATEDA", | ||
90 | "UPDATEM", | ||
91 | "UPDATEMA", | ||
92 | "ZONEINIT", | ||
93 | "ZONEREF", | ||
94 | }; | ||
95 | |||
96 | char *_res_resultcodes[] = { | ||
97 | "NOERROR", | ||
98 | "FORMERR", | ||
99 | "SERVFAIL", | ||
100 | "NXDOMAIN", | ||
101 | "NOTIMP", | ||
102 | "REFUSED", | ||
103 | "6", | ||
104 | "7", | ||
105 | "8", | ||
106 | "9", | ||
107 | "10", | ||
108 | "11", | ||
109 | "12", | ||
110 | "13", | ||
111 | "14", | ||
112 | "NOCHANGE", | ||
113 | }; | ||
114 | |||
115 | static char retbuf[16]; | ||
116 | |||
117 | static char * | ||
118 | dewks(wks) | ||
119 | int wks; | ||
120 | { | ||
121 | switch (wks) { | ||
122 | case 5: return("rje"); | ||
123 | case 7: return("echo"); | ||
124 | case 9: return("discard"); | ||
125 | case 11: return("systat"); | ||
126 | case 13: return("daytime"); | ||
127 | case 15: return("netstat"); | ||
128 | case 17: return("qotd"); | ||
129 | case 19: return("chargen"); | ||
130 | case 20: return("ftp-data"); | ||
131 | case 21: return("ftp"); | ||
132 | case 23: return("telnet"); | ||
133 | case 25: return("smtp"); | ||
134 | case 37: return("time"); | ||
135 | case 39: return("rlp"); | ||
136 | case 42: return("name"); | ||
137 | case 43: return("whois"); | ||
138 | case 53: return("domain"); | ||
139 | case 57: return("apts"); | ||
140 | case 59: return("apfs"); | ||
141 | case 67: return("bootps"); | ||
142 | case 68: return("bootpc"); | ||
143 | case 69: return("tftp"); | ||
144 | case 77: return("rje"); | ||
145 | case 79: return("finger"); | ||
146 | case 87: return("link"); | ||
147 | case 95: return("supdup"); | ||
148 | case 100: return("newacct"); | ||
149 | case 101: return("hostnames"); | ||
150 | case 102: return("iso-tsap"); | ||
151 | case 103: return("x400"); | ||
152 | case 104: return("x400-snd"); | ||
153 | case 105: return("csnet-ns"); | ||
154 | case 109: return("pop-2"); | ||
155 | case 111: return("sunrpc"); | ||
156 | case 113: return("auth"); | ||
157 | case 115: return("sftp"); | ||
158 | case 117: return("uucp-path"); | ||
159 | case 119: return("nntp"); | ||
160 | case 121: return("erpc"); | ||
161 | case 123: return("ntp"); | ||
162 | case 133: return("statsrv"); | ||
163 | case 136: return("profile"); | ||
164 | case 144: return("NeWS"); | ||
165 | case 161: return("snmp"); | ||
166 | case 162: return("snmp-trap"); | ||
167 | case 170: return("print-srv"); | ||
168 | default: (void) sprintf(retbuf, "%d", wks); return(retbuf); | ||
169 | } | ||
170 | } | ||
171 | |||
172 | static char * | ||
173 | deproto(protonum) | ||
174 | int protonum; | ||
175 | { | ||
176 | switch (protonum) { | ||
177 | case 1: return("icmp"); | ||
178 | case 2: return("igmp"); | ||
179 | case 3: return("ggp"); | ||
180 | case 5: return("st"); | ||
181 | case 6: return("tcp"); | ||
182 | case 7: return("ucl"); | ||
183 | case 8: return("egp"); | ||
184 | case 9: return("igp"); | ||
185 | case 11: return("nvp-II"); | ||
186 | case 12: return("pup"); | ||
187 | case 16: return("chaos"); | ||
188 | case 17: return("udp"); | ||
189 | default: (void) sprintf(retbuf, "%d", protonum); return(retbuf); | ||
190 | } | ||
191 | } | ||
192 | |||
193 | static char * | ||
194 | do_rrset(msg, cp, cnt, pflag, file, hs) | ||
195 | int cnt, pflag; | ||
196 | char *cp,*msg, *hs; | ||
197 | FILE *file; | ||
198 | { | ||
199 | int n; | ||
200 | int sflag; | ||
201 | /* | ||
202 | * Print answer records | ||
203 | */ | ||
204 | sflag = (_res.pfcode & pflag); | ||
205 | if (n = ntohs(cnt)) { | ||
206 | if ((!_res.pfcode) || ((sflag) && (_res.pfcode & RES_PRF_HEAD1))) | ||
207 | fprintf(file, hs); | ||
208 | while (--n >= 0) { | ||
209 | cp = p_rr(cp, msg, file); | ||
210 | if ((cp-msg) > PACKETSZ) | ||
211 | return (NULL); | ||
212 | } | ||
213 | if ((!_res.pfcode) || ((sflag) && (_res.pfcode & RES_PRF_HEAD1))) | ||
214 | putc('\n', file); | ||
215 | } | ||
216 | return(cp); | ||
217 | } | ||
218 | |||
219 | __p_query(msg) | ||
220 | char *msg; | ||
221 | { | ||
222 | __fp_query(msg, stdout); | ||
223 | } | ||
224 | |||
225 | /* | ||
226 | * Print the current options. | ||
227 | * This is intended to be primarily a debugging routine. | ||
228 | */ | ||
229 | void | ||
230 | __fp_resstat(statp, file) | ||
231 | struct __res_state *statp; | ||
232 | FILE *file; | ||
233 | { | ||
234 | int bit; | ||
235 | |||
236 | fprintf(file, ";; res options:"); | ||
237 | if (!statp) | ||
238 | statp = &_res; | ||
239 | for (bit = 0; bit < 32; bit++) { /* XXX 32 - bad assumption! */ | ||
240 | if (statp->options & (1<<bit)) | ||
241 | fprintf(file, " %s", p_option(1<<bit)); | ||
242 | } | ||
243 | putc('\n', file); | ||
244 | } | ||
245 | |||
246 | /* | ||
247 | * Print the contents of a query. | ||
248 | * This is intended to be primarily a debugging routine. | ||
249 | */ | ||
250 | void | ||
251 | __fp_query(msg,file) | ||
252 | char *msg; | ||
253 | FILE *file; | ||
254 | { | ||
255 | register char *cp; | ||
256 | register HEADER *hp; | ||
257 | register int n; | ||
258 | |||
259 | /* | ||
260 | * Print header fields. | ||
261 | */ | ||
262 | hp = (HEADER *)msg; | ||
263 | cp = msg + sizeof(HEADER); | ||
264 | if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEADX) || hp->rcode) { | ||
265 | fprintf(file,";; ->>HEADER<<- opcode: %s, status: %s, id: %d", | ||
266 | _res_opcodes[hp->opcode], | ||
267 | _res_resultcodes[hp->rcode], | ||
268 | ntohs(hp->id)); | ||
269 | putc('\n', file); | ||
270 | } | ||
271 | putc(';', file); | ||
272 | if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEAD2)) { | ||
273 | fprintf(file,"; flags:"); | ||
274 | if (hp->qr) | ||
275 | fprintf(file," qr"); | ||
276 | if (hp->aa) | ||
277 | fprintf(file," aa"); | ||
278 | if (hp->tc) | ||
279 | fprintf(file," tc"); | ||
280 | if (hp->rd) | ||
281 | fprintf(file," rd"); | ||
282 | if (hp->ra) | ||
283 | fprintf(file," ra"); | ||
284 | if (hp->pr) | ||
285 | fprintf(file," pr"); | ||
286 | } | ||
287 | if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEAD1)) { | ||
288 | fprintf(file,"; Ques: %d", ntohs(hp->qdcount)); | ||
289 | fprintf(file,", Ans: %d", ntohs(hp->ancount)); | ||
290 | fprintf(file,", Auth: %d", ntohs(hp->nscount)); | ||
291 | fprintf(file,", Addit: %d\n", ntohs(hp->arcount)); | ||
292 | } | ||
293 | #if 0 | ||
294 | if (_res.pfcode & (RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1)) { | ||
295 | putc('\n',file); | ||
296 | } | ||
297 | #endif | ||
298 | /* | ||
299 | * Print question records. | ||
300 | */ | ||
301 | if (n = ntohs(hp->qdcount)) { | ||
302 | if ((!_res.pfcode) || (_res.pfcode & RES_PRF_QUES)) | ||
303 | fprintf(file,";; QUESTIONS:\n"); | ||
304 | while (--n >= 0) { | ||
305 | fprintf(file,";;\t"); | ||
306 | cp = p_cdname(cp, msg, file); | ||
307 | if (cp == NULL) | ||
308 | return; | ||
309 | if ((!_res.pfcode) || (_res.pfcode & RES_PRF_QUES)) | ||
310 | fprintf(file, ", type = %s", | ||
311 | __p_type(_getshort(cp))); | ||
312 | cp += sizeof(u_int16_t); | ||
313 | if ((!_res.pfcode) || (_res.pfcode & RES_PRF_QUES)) | ||
314 | fprintf(file, ", class = %s\n\n", | ||
315 | __p_class(_getshort(cp))); | ||
316 | cp += sizeof(u_int16_t); | ||
317 | } | ||
318 | } | ||
319 | /* | ||
320 | * Print authoritative answer records | ||
321 | */ | ||
322 | cp = do_rrset(msg, cp, hp->ancount, RES_PRF_ANS, file, | ||
323 | ";; ANSWERS:\n"); | ||
324 | if (cp == NULL) | ||
325 | return; | ||
326 | |||
327 | /* | ||
328 | * print name server records | ||
329 | */ | ||
330 | cp = do_rrset(msg, cp, hp->nscount, RES_PRF_AUTH, file, | ||
331 | ";; AUTHORITY RECORDS:\n"); | ||
332 | if (!cp) | ||
333 | return; | ||
334 | |||
335 | /* | ||
336 | * print additional records | ||
337 | */ | ||
338 | cp = do_rrset(msg, cp, hp->arcount, RES_PRF_ADD, file, | ||
339 | ";; ADDITIONAL RECORDS:\n"); | ||
340 | if (!cp) | ||
341 | return; | ||
342 | } | ||
343 | |||
344 | char * | ||
345 | p_cdname(cp, msg, file) | ||
346 | char *cp, *msg; | ||
347 | FILE *file; | ||
348 | { | ||
349 | char name[MAXDNAME]; | ||
350 | int n; | ||
351 | |||
352 | if ((n = dn_expand((u_char *)msg, (u_char *)cp + MAXCDNAME, | ||
353 | (u_char *)cp, (u_char *)name, sizeof(name))) < 0) | ||
354 | return (NULL); | ||
355 | if (name[0] == '\0') | ||
356 | putc('.', file); | ||
357 | else | ||
358 | fputs(name, file); | ||
359 | return (cp + n); | ||
360 | } | ||
361 | |||
362 | char * | ||
363 | p_fqname(cp, msg, file) | ||
364 | char *cp, *msg; | ||
365 | FILE *file; | ||
366 | { | ||
367 | char name[MAXDNAME]; | ||
368 | int n, len; | ||
369 | |||
370 | if ((n = dn_expand((u_char *)msg, (u_char *)cp + MAXCDNAME, | ||
371 | (u_char *)cp, (u_char *)name, sizeof(name))) < 0) | ||
372 | return (NULL); | ||
373 | if (name[0] == '\0') { | ||
374 | putc('.', file); | ||
375 | } else { | ||
376 | fputs(name, file); | ||
377 | if (name[strlen(name) - 1] != '.') | ||
378 | putc('.', file); | ||
379 | } | ||
380 | return (cp + n); | ||
381 | } | ||
382 | |||
383 | /* | ||
384 | * Print resource record fields in human readable form. | ||
385 | */ | ||
386 | char * | ||
387 | p_rr(cp, msg, file) | ||
388 | char *cp, *msg; | ||
389 | FILE *file; | ||
390 | { | ||
391 | int type, class, dlen, n, c; | ||
392 | struct in_addr inaddr; | ||
393 | char *cp1, *cp2; | ||
394 | u_int32_t tmpttl, t; | ||
395 | int lcnt; | ||
396 | |||
397 | if ((cp = p_fqname(cp, msg, file)) == NULL) | ||
398 | return (NULL); /* compression error */ | ||
399 | type = _getshort(cp); | ||
400 | cp += sizeof(u_int16_t); | ||
401 | class = _getshort(cp); | ||
402 | cp += sizeof(u_int16_t); | ||
403 | tmpttl = _getlong(cp); | ||
404 | cp += sizeof(u_int32_t); | ||
405 | dlen = _getshort(cp); | ||
406 | cp += sizeof(u_int16_t); | ||
407 | cp1 = cp; | ||
408 | if ((!_res.pfcode) || (_res.pfcode & RES_PRF_TTLID)) | ||
409 | fprintf(file, "\t%lu", tmpttl); | ||
410 | if ((!_res.pfcode) || (_res.pfcode & RES_PRF_CLASS)) | ||
411 | fprintf(file, "\t%s", __p_class(class)); | ||
412 | fprintf(file, "\t%s", __p_type(type)); | ||
413 | /* | ||
414 | * Print type specific data, if appropriate | ||
415 | */ | ||
416 | switch (type) { | ||
417 | case T_A: | ||
418 | switch (class) { | ||
419 | case C_IN: | ||
420 | case C_HS: | ||
421 | bcopy(cp, (char *)&inaddr, sizeof(inaddr)); | ||
422 | if (dlen == 4) { | ||
423 | fprintf(file,"\t%s", inet_ntoa(inaddr)); | ||
424 | cp += dlen; | ||
425 | } else if (dlen == 7) { | ||
426 | char *address; | ||
427 | u_char protocol; | ||
428 | u_short port; | ||
429 | |||
430 | address = inet_ntoa(inaddr); | ||
431 | cp += sizeof(inaddr); | ||
432 | protocol = *(u_char*)cp; | ||
433 | cp += sizeof(u_char); | ||
434 | port = _getshort(cp); | ||
435 | cp += sizeof(u_int16_t); | ||
436 | fprintf(file, "\t%s\t; proto %d, port %d", | ||
437 | address, protocol, port); | ||
438 | } | ||
439 | break; | ||
440 | default: | ||
441 | cp += dlen; | ||
442 | } | ||
443 | break; | ||
444 | case T_CNAME: | ||
445 | case T_MB: | ||
446 | case T_MG: | ||
447 | case T_MR: | ||
448 | case T_NS: | ||
449 | case T_PTR: | ||
450 | putc('\t', file); | ||
451 | cp = p_fqname(cp, msg, file); | ||
452 | break; | ||
453 | |||
454 | case T_HINFO: | ||
455 | if (n = *cp++) { | ||
456 | fprintf(file,"\t%.*s", n, cp); | ||
457 | cp += n; | ||
458 | } | ||
459 | if (n = *cp++) { | ||
460 | fprintf(file,"\t%.*s", n, cp); | ||
461 | cp += n; | ||
462 | } | ||
463 | break; | ||
464 | |||
465 | case T_SOA: | ||
466 | putc('\t', file); | ||
467 | cp = p_fqname(cp, msg, file); /* origin */ | ||
468 | putc(' ', file); | ||
469 | cp = p_fqname(cp, msg, file); /* mail addr */ | ||
470 | fputs(" (\n", file); | ||
471 | t = _getlong(cp); cp += sizeof(u_int32_t); | ||
472 | fprintf(file,"\t\t\t%lu\t; serial\n", t); | ||
473 | t = _getlong(cp); cp += sizeof(u_int32_t); | ||
474 | fprintf(file,"\t\t\t%lu\t; refresh (%s)\n", t, __p_time(t)); | ||
475 | t = _getlong(cp); cp += sizeof(u_int32_t); | ||
476 | fprintf(file,"\t\t\t%lu\t; retry (%s)\n", t, __p_time(t)); | ||
477 | t = _getlong(cp); cp += sizeof(u_int32_t); | ||
478 | fprintf(file,"\t\t\t%lu\t; expire (%s)\n", t, __p_time(t)); | ||
479 | t = _getlong(cp); cp += sizeof(u_int32_t); | ||
480 | fprintf(file,"\t\t\t%lu )\t; minimum (%s)", t, __p_time(t)); | ||
481 | break; | ||
482 | |||
483 | case T_MX: | ||
484 | case T_AFSDB: | ||
485 | fprintf(file,"\t%d ", _getshort(cp)); | ||
486 | cp += sizeof(u_int16_t); | ||
487 | cp = p_fqname(cp, msg, file); | ||
488 | break; | ||
489 | |||
490 | case T_TXT: | ||
491 | (void) fputs("\t\"", file); | ||
492 | cp2 = cp1 + dlen; | ||
493 | while (cp < cp2) { | ||
494 | if (n = (unsigned char) *cp++) { | ||
495 | for (c = n; c > 0 && cp < cp2; c--) | ||
496 | if (*cp == '\n') { | ||
497 | (void) putc('\\', file); | ||
498 | (void) putc(*cp++, file); | ||
499 | } else | ||
500 | (void) putc(*cp++, file); | ||
501 | } | ||
502 | } | ||
503 | putc('"', file); | ||
504 | break; | ||
505 | |||
506 | case T_MINFO: | ||
507 | case T_RP: | ||
508 | putc('\t', file); | ||
509 | cp = p_fqname(cp, msg, file); | ||
510 | putc(' ', file); | ||
511 | cp = p_fqname(cp, msg, file); | ||
512 | break; | ||
513 | |||
514 | case T_UINFO: | ||
515 | putc('\t', file); | ||
516 | fputs(cp, file); | ||
517 | cp += dlen; | ||
518 | break; | ||
519 | |||
520 | case T_UID: | ||
521 | case T_GID: | ||
522 | if (dlen == 4) { | ||
523 | fprintf(file,"\t%u", _getlong(cp)); | ||
524 | cp += sizeof(int32_t); | ||
525 | } | ||
526 | break; | ||
527 | |||
528 | case T_WKS: | ||
529 | if (dlen < sizeof(u_int32_t) + 1) | ||
530 | break; | ||
531 | bcopy(cp, (char *)&inaddr, sizeof(inaddr)); | ||
532 | cp += sizeof(u_int32_t); | ||
533 | fprintf(file, "\t%s %s ( ", | ||
534 | inet_ntoa(inaddr), | ||
535 | deproto((int) *cp)); | ||
536 | cp += sizeof(u_char); | ||
537 | n = 0; | ||
538 | lcnt = 0; | ||
539 | while (cp < cp1 + dlen) { | ||
540 | c = *cp++; | ||
541 | do { | ||
542 | if (c & 0200) { | ||
543 | if (lcnt == 0) { | ||
544 | fputs("\n\t\t\t", file); | ||
545 | lcnt = 5; | ||
546 | } | ||
547 | fputs(dewks(n), file); | ||
548 | putc(' ', file); | ||
549 | lcnt--; | ||
550 | } | ||
551 | c <<= 1; | ||
552 | } while (++n & 07); | ||
553 | } | ||
554 | putc(')', file); | ||
555 | break; | ||
556 | |||
557 | #ifdef ALLOW_T_UNSPEC | ||
558 | case T_UNSPEC: | ||
559 | { | ||
560 | int NumBytes = 8; | ||
561 | char *DataPtr; | ||
562 | int i; | ||
563 | |||
564 | if (dlen < NumBytes) NumBytes = dlen; | ||
565 | fprintf(file, "\tFirst %d bytes of hex data:", | ||
566 | NumBytes); | ||
567 | for (i = 0, DataPtr = cp; i < NumBytes; i++, DataPtr++) | ||
568 | fprintf(file, " %x", *DataPtr); | ||
569 | cp += dlen; | ||
570 | } | ||
571 | break; | ||
572 | #endif /* ALLOW_T_UNSPEC */ | ||
573 | |||
574 | default: | ||
575 | fprintf(file,"\t?%d?", type); | ||
576 | cp += dlen; | ||
577 | } | ||
578 | #if 0 | ||
579 | fprintf(file, "\t; dlen=%d, ttl %s\n", dlen, __p_time(tmpttl)); | ||
580 | #else | ||
581 | putc('\n', file); | ||
582 | #endif | ||
583 | if (cp - cp1 != dlen) { | ||
584 | fprintf(file,";; packet size error (found %d, dlen was %d)\n", | ||
585 | cp - cp1, dlen); | ||
586 | cp = NULL; | ||
587 | } | ||
588 | return (cp); | ||
589 | } | ||
590 | |||
591 | static char nbuf[40]; | ||
592 | |||
593 | /* | ||
594 | * Return a string for the type | ||
595 | */ | ||
596 | char * | ||
597 | __p_type(type) | ||
598 | int type; | ||
599 | { | ||
600 | switch (type) { | ||
601 | case T_A: | ||
602 | return("A"); | ||
603 | case T_NS: /* authoritative server */ | ||
604 | return("NS"); | ||
605 | case T_CNAME: /* canonical name */ | ||
606 | return("CNAME"); | ||
607 | case T_SOA: /* start of authority zone */ | ||
608 | return("SOA"); | ||
609 | case T_MB: /* mailbox domain name */ | ||
610 | return("MB"); | ||
611 | case T_MG: /* mail group member */ | ||
612 | return("MG"); | ||
613 | case T_MR: /* mail rename name */ | ||
614 | return("MR"); | ||
615 | case T_NULL: /* null resource record */ | ||
616 | return("NULL"); | ||
617 | case T_WKS: /* well known service */ | ||
618 | return("WKS"); | ||
619 | case T_PTR: /* domain name pointer */ | ||
620 | return("PTR"); | ||
621 | case T_HINFO: /* host information */ | ||
622 | return("HINFO"); | ||
623 | case T_MINFO: /* mailbox information */ | ||
624 | return("MINFO"); | ||
625 | case T_MX: /* mail routing info */ | ||
626 | return("MX"); | ||
627 | case T_TXT: /* text */ | ||
628 | return("TXT"); | ||
629 | case T_RP: /* responsible person */ | ||
630 | return("RP"); | ||
631 | case T_AFSDB: /* AFS cell database */ | ||
632 | return("AFSDB"); | ||
633 | case T_AXFR: /* zone transfer */ | ||
634 | return("AXFR"); | ||
635 | case T_MAILB: /* mail box */ | ||
636 | return("MAILB"); | ||
637 | case T_MAILA: /* mail address */ | ||
638 | return("MAILA"); | ||
639 | case T_ANY: /* matches any type */ | ||
640 | return("ANY"); | ||
641 | case T_UINFO: | ||
642 | return("UINFO"); | ||
643 | case T_UID: | ||
644 | return("UID"); | ||
645 | case T_GID: | ||
646 | return("GID"); | ||
647 | #ifdef ALLOW_T_UNSPEC | ||
648 | case T_UNSPEC: | ||
649 | return("UNSPEC"); | ||
650 | #endif /* ALLOW_T_UNSPEC */ | ||
651 | |||
652 | default: | ||
653 | (void)sprintf(nbuf, "%d", type); | ||
654 | return(nbuf); | ||
655 | } | ||
656 | } | ||
657 | |||
658 | /* | ||
659 | * Return a mnemonic for class | ||
660 | */ | ||
661 | char * | ||
662 | __p_class(class) | ||
663 | int class; | ||
664 | { | ||
665 | |||
666 | switch (class) { | ||
667 | case C_IN: /* internet class */ | ||
668 | return("IN"); | ||
669 | case C_HS: /* hesiod class */ | ||
670 | return("HS"); | ||
671 | case C_ANY: /* matches any class */ | ||
672 | return("ANY"); | ||
673 | default: | ||
674 | (void)sprintf(nbuf, "%d", class); | ||
675 | return(nbuf); | ||
676 | } | ||
677 | } | ||
678 | |||
679 | /* | ||
680 | * Return a mnemonic for an option | ||
681 | */ | ||
682 | static char * | ||
683 | p_option(option) | ||
684 | u_int32_t option; | ||
685 | { | ||
686 | switch (option) { | ||
687 | case RES_INIT: return "init"; | ||
688 | case RES_DEBUG: return "debug"; | ||
689 | case RES_AAONLY: return "aaonly"; | ||
690 | case RES_USEVC: return "usevc"; | ||
691 | case RES_PRIMARY: return "primry"; | ||
692 | case RES_IGNTC: return "igntc"; | ||
693 | case RES_RECURSE: return "recurs"; | ||
694 | case RES_DEFNAMES: return "defnam"; | ||
695 | case RES_STAYOPEN: return "styopn"; | ||
696 | case RES_DNSRCH: return "dnsrch"; | ||
697 | default: sprintf(nbuf, "?0x%x?", option); return nbuf; | ||
698 | } | ||
699 | } | ||
700 | |||
701 | /* | ||
702 | * Return a mnemonic for a time to live | ||
703 | */ | ||
704 | char * | ||
705 | __p_time(value) | ||
706 | u_int32_t value; | ||
707 | { | ||
708 | int secs, mins, hours, days; | ||
709 | register char *p; | ||
710 | |||
711 | if (value == 0) { | ||
712 | strcpy(nbuf, "0 secs"); | ||
713 | return(nbuf); | ||
714 | } | ||
715 | |||
716 | secs = value % 60; | ||
717 | value /= 60; | ||
718 | mins = value % 60; | ||
719 | value /= 60; | ||
720 | hours = value % 24; | ||
721 | value /= 24; | ||
722 | days = value; | ||
723 | value = 0; | ||
724 | |||
725 | #define PLURALIZE(x) x, (x == 1) ? "" : "s" | ||
726 | p = nbuf; | ||
727 | if (days) { | ||
728 | (void)sprintf(p, "%d day%s", PLURALIZE(days)); | ||
729 | while (*++p); | ||
730 | } | ||
731 | if (hours) { | ||
732 | if (days) | ||
733 | *p++ = ' '; | ||
734 | (void)sprintf(p, "%d hour%s", PLURALIZE(hours)); | ||
735 | while (*++p); | ||
736 | } | ||
737 | if (mins) { | ||
738 | if (days || hours) | ||
739 | *p++ = ' '; | ||
740 | (void)sprintf(p, "%d min%s", PLURALIZE(mins)); | ||
741 | while (*++p); | ||
742 | } | ||
743 | if (secs || ! (days || hours || mins)) { | ||
744 | if (days || hours || mins) | ||
745 | *p++ = ' '; | ||
746 | (void)sprintf(p, "%d sec%s", PLURALIZE(secs)); | ||
747 | } | ||
748 | return(nbuf); | ||
749 | } | ||
diff --git a/src/lib/libc/net/res_init.c b/src/lib/libc/net/res_init.c new file mode 100644 index 0000000000..33cc8d39f1 --- /dev/null +++ b/src/lib/libc/net/res_init.c | |||
@@ -0,0 +1,394 @@ | |||
1 | /* $NetBSD: res_init.c,v 1.8 1995/06/03 22:33:36 mycroft Exp $ */ | ||
2 | |||
3 | /*- | ||
4 | * Copyright (c) 1985, 1989, 1993 | ||
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 the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | * - | ||
35 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
36 | * | ||
37 | * Permission to use, copy, modify, and distribute this software for any | ||
38 | * purpose with or without fee is hereby granted, provided that the above | ||
39 | * copyright notice and this permission notice appear in all copies, and that | ||
40 | * the name of Digital Equipment Corporation not be used in advertising or | ||
41 | * publicity pertaining to distribution of the document or software without | ||
42 | * specific, written prior permission. | ||
43 | * | ||
44 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
45 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
46 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
47 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
48 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
49 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
50 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
51 | * SOFTWARE. | ||
52 | * - | ||
53 | * --Copyright-- | ||
54 | */ | ||
55 | |||
56 | #if defined(LIBC_SCCS) && !defined(lint) | ||
57 | #if 0 | ||
58 | static char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93"; | ||
59 | static char rcsid[] = "$Id: res_init.c,v 4.9.1.1 1993/05/02 22:43:03 vixie Rel "; | ||
60 | #else | ||
61 | static char rcsid[] = "$NetBSD: res_init.c,v 1.8 1995/06/03 22:33:36 mycroft Exp $"; | ||
62 | #endif | ||
63 | #endif /* LIBC_SCCS and not lint */ | ||
64 | |||
65 | #include <sys/param.h> | ||
66 | #include <sys/socket.h> | ||
67 | #include <netinet/in.h> | ||
68 | #include <arpa/inet.h> | ||
69 | #include <arpa/nameser.h> | ||
70 | #include <resolv.h> | ||
71 | #include <unistd.h> | ||
72 | #include <stdio.h> | ||
73 | #include <stdlib.h> | ||
74 | #include <string.h> | ||
75 | |||
76 | static void res_setoptions __P((char *, char *)); | ||
77 | static u_int32_t net_mask __P((struct in_addr)); | ||
78 | |||
79 | /* | ||
80 | * Resolver state default settings | ||
81 | */ | ||
82 | |||
83 | struct __res_state _res = { | ||
84 | RES_TIMEOUT, /* retransmition time interval */ | ||
85 | 4, /* number of times to retransmit */ | ||
86 | RES_DEFAULT, /* options flags */ | ||
87 | 1, /* number of name servers */ | ||
88 | }; | ||
89 | |||
90 | /* | ||
91 | * Set up default settings. If the configuration file exist, the values | ||
92 | * there will have precedence. Otherwise, the server address is set to | ||
93 | * INADDR_ANY and the default domain name comes from the gethostname(). | ||
94 | * | ||
95 | * The configuration file should only be used if you want to redefine your | ||
96 | * domain or run without a server on your machine. | ||
97 | * | ||
98 | * Return 0 if completes successfully, -1 on error | ||
99 | */ | ||
100 | res_init() | ||
101 | { | ||
102 | register FILE *fp; | ||
103 | register char *cp, **pp, *net; | ||
104 | register int n; | ||
105 | char buf[BUFSIZ], buf2[BUFSIZ]; | ||
106 | int nserv = 0; /* number of nameserver records read from file */ | ||
107 | int haveenv = 0; | ||
108 | int havesearch = 0; | ||
109 | int nsort = 0; | ||
110 | u_long mask; | ||
111 | |||
112 | _res.nsaddr.sin_len = sizeof(struct sockaddr_in); | ||
113 | _res.nsaddr.sin_family = AF_INET; | ||
114 | _res.nsaddr.sin_port = htons(NAMESERVER_PORT); | ||
115 | #ifdef USELOOPBACK | ||
116 | _res.nsaddr.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1); | ||
117 | #else | ||
118 | _res.nsaddr.sin_addr.s_addr = INADDR_ANY; | ||
119 | #endif | ||
120 | _res.nscount = 1; | ||
121 | _res.ndots = 1; | ||
122 | _res.pfcode = 0; | ||
123 | strncpy(_res.lookups, "f", sizeof _res.lookups); | ||
124 | |||
125 | /* Allow user to override the local domain definition */ | ||
126 | if ((cp = getenv("LOCALDOMAIN")) != NULL) { | ||
127 | (void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1); | ||
128 | if ((cp = strpbrk(_res.defdname, " \t\n")) != NULL) | ||
129 | *cp = '\0'; | ||
130 | haveenv++; | ||
131 | |||
132 | /* | ||
133 | * Set search list to be blank-separated strings | ||
134 | * from rest of env value. Permits users of LOCALDOMAIN | ||
135 | * to still have a search list, and anyone to set the | ||
136 | * one that they want to use as an individual (even more | ||
137 | * important now that the rfc1535 stuff restricts searches) | ||
138 | */ | ||
139 | cp = _res.defdname; | ||
140 | pp = _res.dnsrch; | ||
141 | *pp++ = cp; | ||
142 | for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) { | ||
143 | if (*cp == '\n') /* silly backwards compat */ | ||
144 | break; | ||
145 | else if (*cp == ' ' || *cp == '\t') { | ||
146 | *cp = 0; | ||
147 | n = 1; | ||
148 | } else if (n) { | ||
149 | *pp++ = cp; | ||
150 | n = 0; | ||
151 | havesearch = 1; | ||
152 | } | ||
153 | } | ||
154 | /* null terminate last domain if there are excess */ | ||
155 | while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n') | ||
156 | cp++; | ||
157 | *cp = '\0'; | ||
158 | *pp++ = 0; | ||
159 | } | ||
160 | |||
161 | if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) { | ||
162 | strncpy(_res.lookups, "bf", sizeof _res.lookups); | ||
163 | |||
164 | /* read the config file */ | ||
165 | while (fgets(buf, sizeof(buf), fp) != NULL) { | ||
166 | /* skip comments */ | ||
167 | if ((*buf == ';') || (*buf == '#')) | ||
168 | continue; | ||
169 | /* read default domain name */ | ||
170 | if (!strncmp(buf, "domain", sizeof("domain") - 1)) { | ||
171 | if (haveenv) /* skip if have from environ */ | ||
172 | continue; | ||
173 | cp = buf + sizeof("domain") - 1; | ||
174 | while (*cp == ' ' || *cp == '\t') | ||
175 | cp++; | ||
176 | if ((*cp == '\0') || (*cp == '\n')) | ||
177 | continue; | ||
178 | (void)strncpy(_res.defdname, cp, | ||
179 | sizeof(_res.defdname) - 1); | ||
180 | if ((cp = strpbrk(_res.defdname, " \t\n")) != NULL) | ||
181 | *cp = '\0'; | ||
182 | havesearch = 0; | ||
183 | continue; | ||
184 | } | ||
185 | /* lookup types */ | ||
186 | if (!strncmp(buf, "lookup", sizeof("lookup") -1)) { | ||
187 | char *sp = NULL; | ||
188 | |||
189 | bzero(_res.lookups, sizeof _res.lookups); | ||
190 | cp = buf + sizeof("lookup") - 1; | ||
191 | for (n = 0;; cp++) { | ||
192 | if (n == MAXDNSLUS) | ||
193 | break; | ||
194 | if ((*cp == '\0') || (*cp == '\n')) { | ||
195 | if (sp) { | ||
196 | if (*sp=='y' || *sp=='b' || *sp=='f') | ||
197 | _res.lookups[n++] = *sp; | ||
198 | sp = NULL; | ||
199 | } | ||
200 | break; | ||
201 | } else if ((*cp == ' ') || (*cp == '\t') || (*cp == ',')) { | ||
202 | if (sp) { | ||
203 | if (*sp=='y' || *sp=='b' || *sp=='f') | ||
204 | _res.lookups[n++] = *sp; | ||
205 | sp = NULL; | ||
206 | } | ||
207 | } else if (sp == NULL) | ||
208 | sp = cp; | ||
209 | } | ||
210 | continue; | ||
211 | } | ||
212 | /* set search list */ | ||
213 | if (!strncmp(buf, "search", sizeof("search") - 1)) { | ||
214 | if (haveenv) /* skip if have from environ */ | ||
215 | continue; | ||
216 | cp = buf + sizeof("search") - 1; | ||
217 | while (*cp == ' ' || *cp == '\t') | ||
218 | cp++; | ||
219 | if ((*cp == '\0') || (*cp == '\n')) | ||
220 | continue; | ||
221 | (void)strncpy(_res.defdname, cp, | ||
222 | sizeof(_res.defdname) - 1); | ||
223 | if ((cp = strchr(_res.defdname, '\n')) != NULL) | ||
224 | *cp = '\0'; | ||
225 | /* | ||
226 | * Set search list to be blank-separated strings | ||
227 | * on rest of line. | ||
228 | */ | ||
229 | cp = _res.defdname; | ||
230 | pp = _res.dnsrch; | ||
231 | *pp++ = cp; | ||
232 | for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) { | ||
233 | if (*cp == ' ' || *cp == '\t') { | ||
234 | *cp = 0; | ||
235 | n = 1; | ||
236 | } else if (n) { | ||
237 | *pp++ = cp; | ||
238 | n = 0; | ||
239 | } | ||
240 | } | ||
241 | /* null terminate last domain if there are excess */ | ||
242 | while (*cp != '\0' && *cp != ' ' && *cp != '\t') | ||
243 | cp++; | ||
244 | *cp = '\0'; | ||
245 | *pp++ = 0; | ||
246 | havesearch = 1; | ||
247 | continue; | ||
248 | } | ||
249 | /* read nameservers to query */ | ||
250 | if (!strncmp(buf, "nameserver", sizeof("nameserver") - 1) && | ||
251 | nserv < MAXNS) { | ||
252 | struct in_addr a; | ||
253 | |||
254 | cp = buf + sizeof("nameserver") - 1; | ||
255 | while (*cp == ' ' || *cp == '\t') | ||
256 | cp++; | ||
257 | if ((*cp != '\0') && (*cp != '\n') && inet_aton(cp, &a)) { | ||
258 | _res.nsaddr_list[nserv].sin_len = sizeof(struct sockaddr_in); | ||
259 | _res.nsaddr_list[nserv].sin_family = AF_INET; | ||
260 | _res.nsaddr_list[nserv].sin_port = | ||
261 | htons(NAMESERVER_PORT); | ||
262 | _res.nsaddr_list[nserv].sin_addr = a; | ||
263 | nserv++; | ||
264 | } | ||
265 | continue; | ||
266 | } | ||
267 | if (!strncmp(buf, "sortlist", sizeof("sortlist") - 1)) { | ||
268 | struct in_addr a; | ||
269 | |||
270 | cp = buf + sizeof("sortlist") - 1; | ||
271 | while (*cp == ' ' || *cp == '\t') | ||
272 | cp++; | ||
273 | while (sscanf(cp,"%[0-9./]s", buf2) && nsort < MAXRESOLVSORT) { | ||
274 | if (net = strchr(buf2, '/')) | ||
275 | *net = '\0'; | ||
276 | if (inet_aton(buf2, &a)) { | ||
277 | _res.sort_list[nsort].addr = a; | ||
278 | if (net && inet_aton(net+1, &a)) { | ||
279 | _res.sort_list[nsort].mask = a.s_addr; | ||
280 | } else { | ||
281 | _res.sort_list[nsort].mask = | ||
282 | net_mask(_res.sort_list[nsort].addr); | ||
283 | } | ||
284 | nsort++; | ||
285 | } | ||
286 | if (net) | ||
287 | *net = '/'; | ||
288 | cp += strlen(buf2); | ||
289 | while (*cp == ' ' || *cp == '\t') | ||
290 | cp++; | ||
291 | } | ||
292 | continue; | ||
293 | } | ||
294 | if (!strncmp(buf, "options", sizeof("options") -1)) { | ||
295 | res_setoptions(buf + sizeof("options") - 1, "conf"); | ||
296 | continue; | ||
297 | } | ||
298 | } | ||
299 | if (nserv > 1) | ||
300 | _res.nscount = nserv; | ||
301 | _res.nsort = nsort; | ||
302 | (void) fclose(fp); | ||
303 | } | ||
304 | if (_res.defdname[0] == 0) { | ||
305 | if (gethostname(buf, sizeof(_res.defdname) - 1) == 0 && | ||
306 | (cp = strchr(buf, '.'))) | ||
307 | (void)strcpy(_res.defdname, cp + 1); | ||
308 | } | ||
309 | |||
310 | /* find components of local domain that might be searched */ | ||
311 | if (havesearch == 0) { | ||
312 | pp = _res.dnsrch; | ||
313 | *pp++ = _res.defdname; | ||
314 | #ifndef SEARCH_LOCAL_DOMAINS | ||
315 | *pp = NULL; | ||
316 | #else | ||
317 | for (cp = _res.defdname, n = 0; *cp; cp++) | ||
318 | if (*cp == '.') | ||
319 | n++; | ||
320 | cp = _res.defdname; | ||
321 | for (; n >= LOCALDOMAINPARTS && pp < _res.dnsrch + MAXDFLSRCH; | ||
322 | n--) { | ||
323 | cp = strchr(cp, '.'); | ||
324 | *pp++ = ++cp; | ||
325 | } | ||
326 | *pp++ = 0; | ||
327 | #endif | ||
328 | } | ||
329 | |||
330 | if ((cp = getenv("RES_OPTIONS")) != NULL) | ||
331 | res_setoptions(cp, "env"); | ||
332 | _res.options |= RES_INIT; | ||
333 | return (0); | ||
334 | } | ||
335 | |||
336 | static void | ||
337 | res_setoptions(options, source) | ||
338 | char *options, *source; | ||
339 | { | ||
340 | char *cp = options; | ||
341 | int i; | ||
342 | |||
343 | #ifdef DEBUG | ||
344 | if (_res.options & RES_DEBUG) { | ||
345 | printf(";; res_setoptions(\"%s\", \"%s\")...\n", | ||
346 | options, source); | ||
347 | } | ||
348 | #endif | ||
349 | while (*cp) { | ||
350 | /* skip leading and inner runs of spaces */ | ||
351 | while (*cp == ' ' || *cp == '\t') | ||
352 | cp++; | ||
353 | /* search for and process individual options */ | ||
354 | if (!strncmp(cp, "ndots:", sizeof("ndots:")-1)) { | ||
355 | i = atoi(cp + sizeof("ndots:") - 1); | ||
356 | if (i <= RES_MAXNDOTS) | ||
357 | _res.ndots = i; | ||
358 | else | ||
359 | _res.ndots = RES_MAXNDOTS; | ||
360 | #ifdef DEBUG | ||
361 | if (_res.options & RES_DEBUG) { | ||
362 | printf(";;\tndots=%d\n", _res.ndots); | ||
363 | } | ||
364 | #endif | ||
365 | } else if (!strncmp(cp, "debug", sizeof("debug")-1)) { | ||
366 | #ifdef DEBUG | ||
367 | if (!(_res.options & RES_DEBUG)) { | ||
368 | printf(";; res_setoptions(\"%s\", \"%s\")..\n", | ||
369 | options, source); | ||
370 | _res.options |= RES_DEBUG; | ||
371 | } | ||
372 | printf(";;\tdebug\n"); | ||
373 | #endif | ||
374 | } else { | ||
375 | /* XXX - print a warning here? */ | ||
376 | } | ||
377 | /* skip to next run of spaces */ | ||
378 | while (*cp && *cp != ' ' && *cp != '\t') | ||
379 | cp++; | ||
380 | } | ||
381 | } | ||
382 | |||
383 | static u_int32_t | ||
384 | net_mask(in) /* XXX - should really use system's version of this */ | ||
385 | struct in_addr in; | ||
386 | { | ||
387 | register u_int32_t i = ntohl(in.s_addr); | ||
388 | |||
389 | if (IN_CLASSA(i)) | ||
390 | return (htonl(IN_CLASSA_NET)); | ||
391 | if (IN_CLASSB(i)) | ||
392 | return (htonl(IN_CLASSB_NET)); | ||
393 | return (htonl(IN_CLASSC_NET)); | ||
394 | } | ||
diff --git a/src/lib/libc/net/res_mkquery.c b/src/lib/libc/net/res_mkquery.c new file mode 100644 index 0000000000..25f025e147 --- /dev/null +++ b/src/lib/libc/net/res_mkquery.c | |||
@@ -0,0 +1,236 @@ | |||
1 | /* $NetBSD: res_mkquery.c,v 1.5 1995/02/25 06:20:58 cgd Exp $ */ | ||
2 | |||
3 | /*- | ||
4 | * Copyright (c) 1985, 1993 | ||
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 the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | * - | ||
35 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
36 | * | ||
37 | * Permission to use, copy, modify, and distribute this software for any | ||
38 | * purpose with or without fee is hereby granted, provided that the above | ||
39 | * copyright notice and this permission notice appear in all copies, and that | ||
40 | * the name of Digital Equipment Corporation not be used in advertising or | ||
41 | * publicity pertaining to distribution of the document or software without | ||
42 | * specific, written prior permission. | ||
43 | * | ||
44 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
45 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
46 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
47 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
48 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
49 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
50 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
51 | * SOFTWARE. | ||
52 | * - | ||
53 | * --Copyright-- | ||
54 | */ | ||
55 | |||
56 | #if defined(LIBC_SCCS) && !defined(lint) | ||
57 | #if 0 | ||
58 | static char sccsid[] = "@(#)res_mkquery.c 8.1 (Berkeley) 6/4/93"; | ||
59 | static char rcsid[] = "$Id: res_mkquery.c,v 4.9.1.2 1993/05/17 10:00:01 vixie Exp "; | ||
60 | #else | ||
61 | static char rcsid[] = "$NetBSD: res_mkquery.c,v 1.5 1995/02/25 06:20:58 cgd Exp $"; | ||
62 | #endif | ||
63 | #endif /* LIBC_SCCS and not lint */ | ||
64 | |||
65 | #include <sys/param.h> | ||
66 | #include <netinet/in.h> | ||
67 | #include <arpa/nameser.h> | ||
68 | #include <resolv.h> | ||
69 | #include <stdio.h> | ||
70 | #include <string.h> | ||
71 | |||
72 | /* | ||
73 | * Form all types of queries. | ||
74 | * Returns the size of the result or -1. | ||
75 | */ | ||
76 | res_mkquery(op, dname, class, type, data, datalen, newrr_in, buf, buflen) | ||
77 | int op; /* opcode of query */ | ||
78 | const char *dname; /* domain name */ | ||
79 | int class, type; /* class and type of query */ | ||
80 | const char *data; /* resource record data */ | ||
81 | int datalen; /* length of data */ | ||
82 | const char *newrr_in; /* new rr for modify or append */ | ||
83 | char *buf; /* buffer to put query */ | ||
84 | int buflen; /* size of buffer */ | ||
85 | { | ||
86 | register HEADER *hp; | ||
87 | register char *cp; | ||
88 | register int n; | ||
89 | struct rrec *newrr = (struct rrec *) newrr_in; | ||
90 | char *dnptrs[10], **dpp, **lastdnptr; | ||
91 | |||
92 | #ifdef DEBUG | ||
93 | if (_res.options & RES_DEBUG) | ||
94 | printf(";; res_mkquery(%d, %s, %d, %d)\n", | ||
95 | op, dname, class, type); | ||
96 | #endif | ||
97 | /* | ||
98 | * Initialize header fields. | ||
99 | */ | ||
100 | if ((buf == NULL) || (buflen < sizeof(HEADER))) | ||
101 | return(-1); | ||
102 | bzero(buf, sizeof(HEADER)); | ||
103 | hp = (HEADER *) buf; | ||
104 | hp->id = htons(++_res.id); | ||
105 | hp->opcode = op; | ||
106 | hp->pr = (_res.options & RES_PRIMARY) != 0; | ||
107 | hp->rd = (_res.options & RES_RECURSE) != 0; | ||
108 | hp->rcode = NOERROR; | ||
109 | cp = buf + sizeof(HEADER); | ||
110 | buflen -= sizeof(HEADER); | ||
111 | dpp = dnptrs; | ||
112 | *dpp++ = buf; | ||
113 | *dpp++ = NULL; | ||
114 | lastdnptr = dnptrs + sizeof(dnptrs)/sizeof(dnptrs[0]); | ||
115 | /* | ||
116 | * perform opcode specific processing | ||
117 | */ | ||
118 | switch (op) { | ||
119 | case QUERY: | ||
120 | if ((buflen -= QFIXEDSZ) < 0) | ||
121 | return(-1); | ||
122 | if ((n = dn_comp((u_char *)dname, (u_char *)cp, buflen, | ||
123 | (u_char **)dnptrs, (u_char **)lastdnptr)) < 0) | ||
124 | return (-1); | ||
125 | cp += n; | ||
126 | buflen -= n; | ||
127 | __putshort(type, (u_char *)cp); | ||
128 | cp += sizeof(u_int16_t); | ||
129 | __putshort(class, (u_char *)cp); | ||
130 | cp += sizeof(u_int16_t); | ||
131 | hp->qdcount = htons(1); | ||
132 | if (op == QUERY || data == NULL) | ||
133 | break; | ||
134 | /* | ||
135 | * Make an additional record for completion domain. | ||
136 | */ | ||
137 | buflen -= RRFIXEDSZ; | ||
138 | if ((n = dn_comp((u_char *)data, (u_char *)cp, buflen, | ||
139 | (u_char **)dnptrs, (u_char **)lastdnptr)) < 0) | ||
140 | return (-1); | ||
141 | cp += n; | ||
142 | buflen -= n; | ||
143 | __putshort(T_NULL, (u_char *)cp); | ||
144 | cp += sizeof(u_int16_t); | ||
145 | __putshort(class, (u_char *)cp); | ||
146 | cp += sizeof(u_int16_t); | ||
147 | __putlong(0, (u_char *)cp); | ||
148 | cp += sizeof(u_int32_t); | ||
149 | __putshort(0, (u_char *)cp); | ||
150 | cp += sizeof(u_int16_t); | ||
151 | hp->arcount = htons(1); | ||
152 | break; | ||
153 | |||
154 | case IQUERY: | ||
155 | /* | ||
156 | * Initialize answer section | ||
157 | */ | ||
158 | if (buflen < 1 + RRFIXEDSZ + datalen) | ||
159 | return (-1); | ||
160 | *cp++ = '\0'; /* no domain name */ | ||
161 | __putshort(type, (u_char *)cp); | ||
162 | cp += sizeof(u_int16_t); | ||
163 | __putshort(class, (u_char *)cp); | ||
164 | cp += sizeof(u_int16_t); | ||
165 | __putlong(0, (u_char *)cp); | ||
166 | cp += sizeof(u_int32_t); | ||
167 | __putshort(datalen, (u_char *)cp); | ||
168 | cp += sizeof(u_int16_t); | ||
169 | if (datalen) { | ||
170 | bcopy(data, cp, datalen); | ||
171 | cp += datalen; | ||
172 | } | ||
173 | hp->ancount = htons(1); | ||
174 | break; | ||
175 | |||
176 | #ifdef ALLOW_UPDATES | ||
177 | /* | ||
178 | * For UPDATEM/UPDATEMA, do UPDATED/UPDATEDA followed by UPDATEA | ||
179 | * (Record to be modified is followed by its replacement in msg.) | ||
180 | */ | ||
181 | case UPDATEM: | ||
182 | case UPDATEMA: | ||
183 | |||
184 | case UPDATED: | ||
185 | /* | ||
186 | * The res code for UPDATED and UPDATEDA is the same; user | ||
187 | * calls them differently: specifies data for UPDATED; server | ||
188 | * ignores data if specified for UPDATEDA. | ||
189 | */ | ||
190 | case UPDATEDA: | ||
191 | buflen -= RRFIXEDSZ + datalen; | ||
192 | if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0) | ||
193 | return (-1); | ||
194 | cp += n; | ||
195 | __putshort(type, cp); | ||
196 | cp += sizeof(u_int16_t); | ||
197 | __putshort(class, cp); | ||
198 | cp += sizeof(u_int16_t); | ||
199 | __putlong(0, cp); | ||
200 | cp += sizeof(u_int32_t); | ||
201 | __putshort(datalen, cp); | ||
202 | cp += sizeof(u_int16_t); | ||
203 | if (datalen) { | ||
204 | bcopy(data, cp, datalen); | ||
205 | cp += datalen; | ||
206 | } | ||
207 | if ( (op == UPDATED) || (op == UPDATEDA) ) { | ||
208 | hp->ancount = htons(0); | ||
209 | break; | ||
210 | } | ||
211 | /* Else UPDATEM/UPDATEMA, so drop into code for UPDATEA */ | ||
212 | |||
213 | case UPDATEA: /* Add new resource record */ | ||
214 | buflen -= RRFIXEDSZ + datalen; | ||
215 | if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0) | ||
216 | return (-1); | ||
217 | cp += n; | ||
218 | __putshort(newrr->r_type, cp); | ||
219 | cp += sizeof(u_int16_t); | ||
220 | __putshort(newrr->r_class, cp); | ||
221 | cp += sizeof(u_int16_t); | ||
222 | __putlong(0, cp); | ||
223 | cp += sizeof(u_int32_t); | ||
224 | __putshort(newrr->r_size, cp); | ||
225 | cp += sizeof(u_int16_t); | ||
226 | if (newrr->r_size) { | ||
227 | bcopy(newrr->r_data, cp, newrr->r_size); | ||
228 | cp += newrr->r_size; | ||
229 | } | ||
230 | hp->ancount = htons(0); | ||
231 | break; | ||
232 | |||
233 | #endif /* ALLOW_UPDATES */ | ||
234 | } | ||
235 | return (cp - buf); | ||
236 | } | ||
diff --git a/src/lib/libc/net/res_query.c b/src/lib/libc/net/res_query.c new file mode 100644 index 0000000000..7649462e56 --- /dev/null +++ b/src/lib/libc/net/res_query.c | |||
@@ -0,0 +1,366 @@ | |||
1 | /* $NetBSD: res_query.c,v 1.9 1995/02/25 06:58:58 cgd Exp $ */ | ||
2 | |||
3 | /*- | ||
4 | * Copyright (c) 1988, 1993 | ||
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 the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | * - | ||
35 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
36 | * | ||
37 | * Permission to use, copy, modify, and distribute this software for any | ||
38 | * purpose with or without fee is hereby granted, provided that the above | ||
39 | * copyright notice and this permission notice appear in all copies, and that | ||
40 | * the name of Digital Equipment Corporation not be used in advertising or | ||
41 | * publicity pertaining to distribution of the document or software without | ||
42 | * specific, written prior permission. | ||
43 | * | ||
44 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
45 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
46 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
47 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
48 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
49 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
50 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
51 | * SOFTWARE. | ||
52 | * - | ||
53 | * --Copyright-- | ||
54 | */ | ||
55 | |||
56 | #if defined(LIBC_SCCS) && !defined(lint) | ||
57 | #if 0 | ||
58 | static char sccsid[] = "@(#)res_query.c 8.1 (Berkeley) 6/4/93"; | ||
59 | static char rcsid[] = "$Id: res_query.c,v 1.1 1993/06/01 09:42:14 vixie Exp vixie "; | ||
60 | #else | ||
61 | static char rcsid[] = "$NetBSD: res_query.c,v 1.9 1995/02/25 06:58:58 cgd Exp $"; | ||
62 | #endif | ||
63 | #endif /* LIBC_SCCS and not lint */ | ||
64 | |||
65 | #include <sys/param.h> | ||
66 | #include <netinet/in.h> | ||
67 | #include <arpa/inet.h> | ||
68 | #include <arpa/nameser.h> | ||
69 | #include <netdb.h> | ||
70 | #include <resolv.h> | ||
71 | #include <stdio.h> | ||
72 | #include <ctype.h> | ||
73 | #include <errno.h> | ||
74 | #include <stdlib.h> | ||
75 | #include <string.h> | ||
76 | |||
77 | #if PACKETSZ > 1024 | ||
78 | #define MAXPACKET PACKETSZ | ||
79 | #else | ||
80 | #define MAXPACKET 1024 | ||
81 | #endif | ||
82 | |||
83 | char *__hostalias __P((const char *)); | ||
84 | int h_errno; | ||
85 | |||
86 | /* | ||
87 | * Formulate a normal query, send, and await answer. | ||
88 | * Returned answer is placed in supplied buffer "answer". | ||
89 | * Perform preliminary check of answer, returning success only | ||
90 | * if no error is indicated and the answer count is nonzero. | ||
91 | * Return the size of the response on success, -1 on error. | ||
92 | * Error number is left in h_errno. | ||
93 | * Caller must parse answer and determine whether it answers the question. | ||
94 | */ | ||
95 | res_query(name, class, type, answer, anslen) | ||
96 | char *name; /* domain name */ | ||
97 | int class, type; /* class and type of query */ | ||
98 | u_char *answer; /* buffer to put answer */ | ||
99 | int anslen; /* size of answer buffer */ | ||
100 | { | ||
101 | char buf[MAXPACKET]; | ||
102 | HEADER *hp; | ||
103 | int n; | ||
104 | |||
105 | if ((_res.options & RES_INIT) == 0 && res_init() == -1) | ||
106 | return (-1); | ||
107 | #ifdef DEBUG | ||
108 | if (_res.options & RES_DEBUG) | ||
109 | printf(";; res_query(%s, %d, %d)\n", name, class, type); | ||
110 | #endif | ||
111 | n = res_mkquery(QUERY, name, class, type, (char *)NULL, 0, NULL, | ||
112 | buf, sizeof(buf)); | ||
113 | |||
114 | if (n <= 0) { | ||
115 | #ifdef DEBUG | ||
116 | if (_res.options & RES_DEBUG) | ||
117 | printf(";; res_query: mkquery failed\n"); | ||
118 | #endif | ||
119 | h_errno = NO_RECOVERY; | ||
120 | return (n); | ||
121 | } | ||
122 | n = res_send(buf, n, (char *)answer, anslen); | ||
123 | if (n < 0) { | ||
124 | #ifdef DEBUG | ||
125 | if (_res.options & RES_DEBUG) | ||
126 | printf(";; res_query: send error\n"); | ||
127 | #endif | ||
128 | h_errno = TRY_AGAIN; | ||
129 | return (n); | ||
130 | } | ||
131 | |||
132 | hp = (HEADER *) answer; | ||
133 | if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) { | ||
134 | #ifdef DEBUG | ||
135 | if (_res.options & RES_DEBUG) | ||
136 | printf(";; rcode = %d, ancount=%d\n", hp->rcode, | ||
137 | ntohs(hp->ancount)); | ||
138 | #endif | ||
139 | switch (hp->rcode) { | ||
140 | case NXDOMAIN: | ||
141 | h_errno = HOST_NOT_FOUND; | ||
142 | break; | ||
143 | case SERVFAIL: | ||
144 | h_errno = TRY_AGAIN; | ||
145 | break; | ||
146 | case NOERROR: | ||
147 | h_errno = NO_DATA; | ||
148 | break; | ||
149 | case FORMERR: | ||
150 | case NOTIMP: | ||
151 | case REFUSED: | ||
152 | default: | ||
153 | h_errno = NO_RECOVERY; | ||
154 | break; | ||
155 | } | ||
156 | return (-1); | ||
157 | } | ||
158 | return (n); | ||
159 | } | ||
160 | |||
161 | /* | ||
162 | * Formulate a normal query, send, and retrieve answer in supplied buffer. | ||
163 | * Return the size of the response on success, -1 on error. | ||
164 | * If enabled, implement search rules until answer or unrecoverable failure | ||
165 | * is detected. Error number is left in h_errno. | ||
166 | * Only useful for queries in the same name hierarchy as the local host | ||
167 | * (not, for example, for host address-to-name lookups in domain in-addr.arpa). | ||
168 | */ | ||
169 | int | ||
170 | res_search(name, class, type, answer, anslen) | ||
171 | const char *name; /* domain name */ | ||
172 | int class, type; /* class and type of query */ | ||
173 | u_char *answer; /* buffer to put answer */ | ||
174 | int anslen; /* size of answer */ | ||
175 | { | ||
176 | register char *cp, **domain; | ||
177 | int dots, trailing_dot, ret, got_nodata, saved_herrno, tried_as_is; | ||
178 | |||
179 | if ((_res.options & RES_INIT) == 0 && res_init() == -1) | ||
180 | return (-1); | ||
181 | |||
182 | got_nodata = 0; | ||
183 | errno = 0; | ||
184 | h_errno = HOST_NOT_FOUND; /* default, if we never query */ | ||
185 | dots = 0; | ||
186 | for (cp = (char *)name; *cp; cp++) { | ||
187 | if (*cp == '.') | ||
188 | dots++; | ||
189 | } | ||
190 | trailing_dot = 0; | ||
191 | if ((cp > name) && (*--cp == '.')) | ||
192 | trailing_dot++; | ||
193 | |||
194 | /* | ||
195 | * if there aren't any dots, it could be a user-level alias | ||
196 | */ | ||
197 | if (!dots && (cp = __hostalias(name))) | ||
198 | return (res_query(cp, class, type, answer, anslen)); | ||
199 | |||
200 | /* | ||
201 | * If there are dots in the name already, let's just give it a try | ||
202 | * 'as is'. The threshold can be set with the "ndots" option. | ||
203 | */ | ||
204 | saved_herrno = -1; | ||
205 | tried_as_is = 0; | ||
206 | if (dots >= _res.ndots) { | ||
207 | ret = res_querydomain(name, NULL, class, type, answer, anslen); | ||
208 | if (ret > 0) | ||
209 | return (ret); | ||
210 | saved_herrno = h_errno; | ||
211 | tried_as_is++; | ||
212 | } | ||
213 | |||
214 | /* | ||
215 | * We do at least one level of search if | ||
216 | * - there is no dot and RES_DEFNAME is set, or | ||
217 | * - there is at least one dot, there is no trailing dot, | ||
218 | * and RES_DNSRCH is set. | ||
219 | */ | ||
220 | if ((!dots && (_res.options & RES_DEFNAMES)) || | ||
221 | (dots && !trailing_dot && (_res.options & RES_DNSRCH))) { | ||
222 | for (domain = _res.dnsrch; *domain; domain++) { | ||
223 | int done = 0; | ||
224 | |||
225 | ret = res_querydomain(name, *domain, class, type, | ||
226 | answer, anslen); | ||
227 | if (ret > 0) | ||
228 | return (ret); | ||
229 | /* | ||
230 | * If no server present, give up. | ||
231 | * If name isn't found in this domain, | ||
232 | * keep trying higher domains in the search list | ||
233 | * (if that's enabled). | ||
234 | * On a NO_DATA error, keep trying, otherwise | ||
235 | * a wildcard entry of another type could keep us | ||
236 | * from finding this entry higher in the domain. | ||
237 | * If we get some other error (negative answer or | ||
238 | * server failure), then stop searching up, | ||
239 | * but try the input name below in case it's | ||
240 | * fully-qualified. | ||
241 | */ | ||
242 | if (errno == ECONNREFUSED) { | ||
243 | h_errno = TRY_AGAIN; | ||
244 | return (-1); | ||
245 | } | ||
246 | |||
247 | switch (h_errno) { | ||
248 | case NO_DATA: | ||
249 | got_nodata++; | ||
250 | /* FALLTHROUGH */ | ||
251 | case HOST_NOT_FOUND: | ||
252 | /* keep trying */ | ||
253 | break; | ||
254 | default: | ||
255 | /* anything else implies that we're done */ | ||
256 | done++; | ||
257 | } | ||
258 | /* | ||
259 | * if we got here for some reason other than DNSRCH, | ||
260 | * we only wanted one iteration of the loop, so stop. | ||
261 | */ | ||
262 | if (!(_res.options & RES_DNSRCH)) | ||
263 | done++; | ||
264 | |||
265 | if (done) | ||
266 | break; | ||
267 | } | ||
268 | } | ||
269 | |||
270 | /* | ||
271 | * if we have not already tried the name "as is", do that now. | ||
272 | * note that we do this regardless of how many dots were in the | ||
273 | * name or whether it ends with a dot. | ||
274 | */ | ||
275 | if (!tried_as_is) { | ||
276 | ret = res_querydomain(name, NULL, class, type, answer, anslen); | ||
277 | if (ret > 0) | ||
278 | return (ret); | ||
279 | saved_herrno = h_errno; | ||
280 | } | ||
281 | |||
282 | /* | ||
283 | * if we got here, we didn't satisfy the search. | ||
284 | * if we did an initial full query, return that query's h_errno | ||
285 | * (note that we wouldn't be here if that query had succeeded). | ||
286 | * else if we ever got a nodata, send that back as the reason. | ||
287 | * else send back meaningless h_errno, that being the one from | ||
288 | * the last DNSRCH we did. | ||
289 | */ | ||
290 | if (saved_herrno != -1) | ||
291 | h_errno = saved_herrno; | ||
292 | else if (got_nodata) | ||
293 | h_errno = NO_DATA; | ||
294 | return (-1); | ||
295 | } | ||
296 | |||
297 | /* | ||
298 | * Perform a call on res_query on the concatenation of name and domain, | ||
299 | * removing a trailing dot from name if domain is NULL. | ||
300 | */ | ||
301 | res_querydomain(name, domain, class, type, answer, anslen) | ||
302 | char *name, *domain; | ||
303 | int class, type; /* class and type of query */ | ||
304 | u_char *answer; /* buffer to put answer */ | ||
305 | int anslen; /* size of answer */ | ||
306 | { | ||
307 | char nbuf[2*MAXDNAME+2]; | ||
308 | char *longname = nbuf; | ||
309 | int n; | ||
310 | |||
311 | #ifdef DEBUG | ||
312 | if (_res.options & RES_DEBUG) | ||
313 | printf(";; res_querydomain(%s, %s, %d, %d)\n", | ||
314 | name, domain, class, type); | ||
315 | #endif | ||
316 | if (domain == NULL) { | ||
317 | /* | ||
318 | * Check for trailing '.'; | ||
319 | * copy without '.' if present. | ||
320 | */ | ||
321 | n = strlen(name) - 1; | ||
322 | if (n != (0 - 1) && name[n] == '.' && n < sizeof(nbuf) - 1) { | ||
323 | bcopy(name, nbuf, n); | ||
324 | nbuf[n] = '\0'; | ||
325 | } else | ||
326 | longname = name; | ||
327 | } else | ||
328 | (void)sprintf(nbuf, "%.*s.%.*s", | ||
329 | MAXDNAME, name, MAXDNAME, domain); | ||
330 | |||
331 | return (res_query(longname, class, type, answer, anslen)); | ||
332 | } | ||
333 | |||
334 | char * | ||
335 | __hostalias(name) | ||
336 | register const char *name; | ||
337 | { | ||
338 | register char *cp1, *cp2; | ||
339 | FILE *fp; | ||
340 | char *file, *getenv(), *strcpy(), *strncpy(); | ||
341 | char buf[BUFSIZ]; | ||
342 | static char abuf[MAXDNAME]; | ||
343 | |||
344 | file = getenv("HOSTALIASES"); | ||
345 | if (file == NULL || (fp = fopen(file, "r")) == NULL) | ||
346 | return (NULL); | ||
347 | buf[sizeof(buf) - 1] = '\0'; | ||
348 | while (fgets(buf, sizeof(buf), fp)) { | ||
349 | for (cp1 = buf; *cp1 && !isspace(*cp1); ++cp1); | ||
350 | if (!*cp1) | ||
351 | break; | ||
352 | *cp1 = '\0'; | ||
353 | if (!strcasecmp(buf, name)) { | ||
354 | while (isspace(*++cp1)); | ||
355 | if (!*cp1) | ||
356 | break; | ||
357 | for (cp2 = cp1 + 1; *cp2 && !isspace(*cp2); ++cp2); | ||
358 | abuf[sizeof(abuf) - 1] = *cp2 = '\0'; | ||
359 | (void)strncpy(abuf, cp1, sizeof(abuf) - 1); | ||
360 | fclose(fp); | ||
361 | return (abuf); | ||
362 | } | ||
363 | } | ||
364 | fclose(fp); | ||
365 | return (NULL); | ||
366 | } | ||
diff --git a/src/lib/libc/net/res_send.c b/src/lib/libc/net/res_send.c new file mode 100644 index 0000000000..e608358180 --- /dev/null +++ b/src/lib/libc/net/res_send.c | |||
@@ -0,0 +1,474 @@ | |||
1 | /* $NetBSD: res_send.c,v 1.4 1995/02/25 06:21:01 cgd Exp $ */ | ||
2 | |||
3 | /*- | ||
4 | * Copyright (c) 1985, 1989, 1993 | ||
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 the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | * - | ||
35 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
36 | * | ||
37 | * Permission to use, copy, modify, and distribute this software for any | ||
38 | * purpose with or without fee is hereby granted, provided that the above | ||
39 | * copyright notice and this permission notice appear in all copies, and that | ||
40 | * the name of Digital Equipment Corporation not be used in advertising or | ||
41 | * publicity pertaining to distribution of the document or software without | ||
42 | * specific, written prior permission. | ||
43 | * | ||
44 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
45 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
46 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
47 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
48 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
49 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
50 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
51 | * SOFTWARE. | ||
52 | * - | ||
53 | * --Copyright-- | ||
54 | */ | ||
55 | |||
56 | #if defined(LIBC_SCCS) && !defined(lint) | ||
57 | #if 0 | ||
58 | static char sccsid[] = "@(#)res_send.c 8.1 (Berkeley) 6/4/93"; | ||
59 | static char rcsid[] = "$Id: res_send.c,v 4.9.1.1 1993/05/02 22:43:03 vixie Rel "; | ||
60 | #else | ||
61 | static char rcsid[] = "$NetBSD: res_send.c,v 1.4 1995/02/25 06:21:01 cgd Exp $"; | ||
62 | #endif | ||
63 | #endif /* LIBC_SCCS and not lint */ | ||
64 | |||
65 | /* | ||
66 | * Send query to name server and wait for reply. | ||
67 | */ | ||
68 | |||
69 | #include <sys/param.h> | ||
70 | #include <sys/time.h> | ||
71 | #include <sys/socket.h> | ||
72 | #include <sys/uio.h> | ||
73 | #include <netinet/in.h> | ||
74 | #include <arpa/nameser.h> | ||
75 | #include <arpa/inet.h> | ||
76 | #include <stdio.h> | ||
77 | #include <errno.h> | ||
78 | #include <resolv.h> | ||
79 | #include <unistd.h> | ||
80 | #include <string.h> | ||
81 | |||
82 | static int s = -1; /* socket used for communications */ | ||
83 | static struct sockaddr no_addr; | ||
84 | |||
85 | #ifndef FD_SET | ||
86 | #define NFDBITS 32 | ||
87 | #define FD_SETSIZE 32 | ||
88 | #define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS))) | ||
89 | #define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS))) | ||
90 | #define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS))) | ||
91 | #define FD_ZERO(p) bzero((char *)(p), sizeof(*(p))) | ||
92 | #endif | ||
93 | |||
94 | res_send(buf, buflen, answer, anslen) | ||
95 | const char *buf; | ||
96 | int buflen; | ||
97 | char *answer; | ||
98 | int anslen; | ||
99 | { | ||
100 | register int n; | ||
101 | int try, v_circuit, resplen, ns; | ||
102 | int gotsomewhere = 0, connected = 0; | ||
103 | int connreset = 0; | ||
104 | u_short id, len; | ||
105 | char *cp; | ||
106 | fd_set dsmask; | ||
107 | struct timeval timeout; | ||
108 | HEADER *hp = (HEADER *) buf; | ||
109 | HEADER *anhp = (HEADER *) answer; | ||
110 | u_int badns; /* XXX NSMAX can't exceed #/bits per this */ | ||
111 | struct iovec iov[2]; | ||
112 | int terrno = ETIMEDOUT; | ||
113 | char junk[512]; | ||
114 | |||
115 | #ifdef DEBUG | ||
116 | if ((_res.options & RES_DEBUG) || (_res.pfcode & RES_PRF_QUERY)) { | ||
117 | printf(";; res_send()\n"); | ||
118 | __p_query(buf); | ||
119 | } | ||
120 | #endif | ||
121 | if (!(_res.options & RES_INIT)) | ||
122 | if (res_init() == -1) { | ||
123 | return(-1); | ||
124 | } | ||
125 | v_circuit = (_res.options & RES_USEVC) || buflen > PACKETSZ; | ||
126 | id = hp->id; | ||
127 | badns = 0; | ||
128 | /* | ||
129 | * Send request, RETRY times, or until successful | ||
130 | */ | ||
131 | for (try = 0; try < _res.retry; try++) { | ||
132 | for (ns = 0; ns < _res.nscount; ns++) { | ||
133 | if (badns & (1<<ns)) | ||
134 | continue; | ||
135 | #ifdef DEBUG | ||
136 | if (_res.options & RES_DEBUG) | ||
137 | printf(";; Querying server (# %d) address = %s\n", | ||
138 | ns+1, | ||
139 | inet_ntoa(_res.nsaddr_list[ns].sin_addr)); | ||
140 | #endif | ||
141 | usevc: | ||
142 | if (v_circuit) { | ||
143 | int truncated = 0; | ||
144 | |||
145 | /* | ||
146 | * Use virtual circuit; | ||
147 | * at most one attempt per server. | ||
148 | */ | ||
149 | try = _res.retry; | ||
150 | if (s < 0) { | ||
151 | s = socket(AF_INET, SOCK_STREAM, 0); | ||
152 | if (s < 0) { | ||
153 | terrno = errno; | ||
154 | #ifdef DEBUG | ||
155 | if (_res.options & RES_DEBUG) | ||
156 | perror("socket (vc) failed"); | ||
157 | #endif | ||
158 | continue; | ||
159 | } | ||
160 | if (connect(s, | ||
161 | (struct sockaddr *)&(_res.nsaddr_list[ns]), | ||
162 | sizeof(struct sockaddr)) < 0) { | ||
163 | terrno = errno; | ||
164 | #ifdef DEBUG | ||
165 | if (_res.options & RES_DEBUG) | ||
166 | perror("connect failed"); | ||
167 | #endif | ||
168 | (void) close(s); | ||
169 | s = -1; | ||
170 | continue; | ||
171 | } | ||
172 | } | ||
173 | /* | ||
174 | * Send length & message | ||
175 | */ | ||
176 | len = htons((u_short)buflen); | ||
177 | iov[0].iov_base = (caddr_t)&len; | ||
178 | iov[0].iov_len = sizeof(len); | ||
179 | iov[1].iov_base = (char *)buf; | ||
180 | iov[1].iov_len = buflen; | ||
181 | if (writev(s, iov, 2) != sizeof(len) + buflen) { | ||
182 | terrno = errno; | ||
183 | #ifdef DEBUG | ||
184 | if (_res.options & RES_DEBUG) | ||
185 | perror("write failed"); | ||
186 | #endif | ||
187 | (void) close(s); | ||
188 | s = -1; | ||
189 | continue; | ||
190 | } | ||
191 | /* | ||
192 | * Receive length & response | ||
193 | */ | ||
194 | cp = answer; | ||
195 | len = sizeof(short); | ||
196 | while (len != 0 && | ||
197 | (n = read(s, (char *)cp, (int)len)) > 0) { | ||
198 | cp += n; | ||
199 | len -= n; | ||
200 | } | ||
201 | if (n <= 0) { | ||
202 | terrno = errno; | ||
203 | #ifdef DEBUG | ||
204 | if (_res.options & RES_DEBUG) | ||
205 | perror("read failed"); | ||
206 | #endif | ||
207 | (void) close(s); | ||
208 | s = -1; | ||
209 | /* | ||
210 | * A long running process might get its TCP | ||
211 | * connection reset if the remote server was | ||
212 | * restarted. Requery the server instead of | ||
213 | * trying a new one. When there is only one | ||
214 | * server, this means that a query might work | ||
215 | * instead of failing. We only allow one reset | ||
216 | * per query to prevent looping. | ||
217 | */ | ||
218 | if (terrno == ECONNRESET && !connreset) { | ||
219 | connreset = 1; | ||
220 | ns--; | ||
221 | } | ||
222 | continue; | ||
223 | } | ||
224 | cp = answer; | ||
225 | if ((resplen = ntohs(*(u_short *)cp)) > anslen) { | ||
226 | #ifdef DEBUG | ||
227 | if (_res.options & RES_DEBUG) | ||
228 | fprintf(stderr, | ||
229 | ";; response truncated\n"); | ||
230 | #endif | ||
231 | len = anslen; | ||
232 | truncated = 1; | ||
233 | } else | ||
234 | len = resplen; | ||
235 | while (len != 0 && | ||
236 | (n = read(s, (char *)cp, (int)len)) > 0) { | ||
237 | cp += n; | ||
238 | len -= n; | ||
239 | } | ||
240 | if (n <= 0) { | ||
241 | terrno = errno; | ||
242 | #ifdef DEBUG | ||
243 | if (_res.options & RES_DEBUG) | ||
244 | perror("read failed"); | ||
245 | #endif | ||
246 | (void) close(s); | ||
247 | s = -1; | ||
248 | continue; | ||
249 | } | ||
250 | if (truncated) { | ||
251 | /* | ||
252 | * Flush rest of answer | ||
253 | * so connection stays in synch. | ||
254 | */ | ||
255 | anhp->tc = 1; | ||
256 | len = resplen - anslen; | ||
257 | while (len != 0) { | ||
258 | n = (len > sizeof(junk) ? | ||
259 | sizeof(junk) : len); | ||
260 | if ((n = read(s, junk, n)) > 0) | ||
261 | len -= n; | ||
262 | else | ||
263 | break; | ||
264 | } | ||
265 | } | ||
266 | } else { | ||
267 | /* | ||
268 | * Use datagrams. | ||
269 | */ | ||
270 | if (s < 0) { | ||
271 | s = socket(AF_INET, SOCK_DGRAM, 0); | ||
272 | if (s < 0) { | ||
273 | terrno = errno; | ||
274 | #ifdef DEBUG | ||
275 | if (_res.options & RES_DEBUG) | ||
276 | perror("socket (dg) failed"); | ||
277 | #endif | ||
278 | continue; | ||
279 | } | ||
280 | } | ||
281 | /* | ||
282 | * I'm tired of answering this question, so: | ||
283 | * On a 4.3BSD+ machine (client and server, | ||
284 | * actually), sending to a nameserver datagram | ||
285 | * port with no nameserver will cause an | ||
286 | * ICMP port unreachable message to be returned. | ||
287 | * If our datagram socket is "connected" to the | ||
288 | * server, we get an ECONNREFUSED error on the next | ||
289 | * socket operation, and select returns if the | ||
290 | * error message is received. We can thus detect | ||
291 | * the absence of a nameserver without timing out. | ||
292 | * If we have sent queries to at least two servers, | ||
293 | * however, we don't want to remain connected, | ||
294 | * as we wish to receive answers from the first | ||
295 | * server to respond. | ||
296 | */ | ||
297 | if (_res.nscount == 1 || (try == 0 && ns == 0)) { | ||
298 | /* | ||
299 | * Don't use connect if we might | ||
300 | * still receive a response | ||
301 | * from another server. | ||
302 | */ | ||
303 | if (connected == 0) { | ||
304 | if (connect(s, | ||
305 | (struct sockaddr *) | ||
306 | &_res.nsaddr_list[ns], | ||
307 | sizeof(struct sockaddr)) < 0) { | ||
308 | #ifdef DEBUG | ||
309 | if (_res.options & RES_DEBUG) | ||
310 | perror("connect"); | ||
311 | #endif | ||
312 | continue; | ||
313 | } | ||
314 | connected = 1; | ||
315 | } | ||
316 | if (send(s, buf, buflen, 0) != buflen) { | ||
317 | #ifdef DEBUG | ||
318 | if (_res.options & RES_DEBUG) | ||
319 | perror("send"); | ||
320 | #endif | ||
321 | continue; | ||
322 | } | ||
323 | } else { | ||
324 | /* | ||
325 | * Disconnect if we want to listen | ||
326 | * for responses from more than one server. | ||
327 | */ | ||
328 | if (connected) { | ||
329 | (void) connect(s, &no_addr, | ||
330 | sizeof(no_addr)); | ||
331 | connected = 0; | ||
332 | } | ||
333 | if (sendto(s, buf, buflen, 0, | ||
334 | (struct sockaddr *)&_res.nsaddr_list[ns], | ||
335 | sizeof(struct sockaddr)) != buflen) { | ||
336 | #ifdef DEBUG | ||
337 | if (_res.options & RES_DEBUG) | ||
338 | perror("sendto"); | ||
339 | #endif | ||
340 | continue; | ||
341 | } | ||
342 | } | ||
343 | |||
344 | /* | ||
345 | * Wait for reply | ||
346 | */ | ||
347 | timeout.tv_sec = (_res.retrans << try); | ||
348 | if (try > 0) | ||
349 | timeout.tv_sec /= _res.nscount; | ||
350 | if ((long) timeout.tv_sec <= 0) | ||
351 | timeout.tv_sec = 1; | ||
352 | timeout.tv_usec = 0; | ||
353 | wait: | ||
354 | FD_ZERO(&dsmask); | ||
355 | FD_SET(s, &dsmask); | ||
356 | n = select(s+1, &dsmask, (fd_set *)NULL, | ||
357 | (fd_set *)NULL, &timeout); | ||
358 | if (n < 0) { | ||
359 | #ifdef DEBUG | ||
360 | if (_res.options & RES_DEBUG) | ||
361 | perror("select"); | ||
362 | #endif | ||
363 | continue; | ||
364 | } | ||
365 | if (n == 0) { | ||
366 | /* | ||
367 | * timeout | ||
368 | */ | ||
369 | #ifdef DEBUG | ||
370 | if (_res.options & RES_DEBUG) | ||
371 | printf(";; timeout\n"); | ||
372 | #endif | ||
373 | gotsomewhere = 1; | ||
374 | continue; | ||
375 | } | ||
376 | if ((resplen = recv(s, answer, anslen, 0)) <= 0) { | ||
377 | #ifdef DEBUG | ||
378 | if (_res.options & RES_DEBUG) | ||
379 | perror("recvfrom"); | ||
380 | #endif | ||
381 | continue; | ||
382 | } | ||
383 | gotsomewhere = 1; | ||
384 | if (id != anhp->id) { | ||
385 | /* | ||
386 | * response from old query, ignore it | ||
387 | */ | ||
388 | #ifdef DEBUG | ||
389 | if ((_res.options & RES_DEBUG) || | ||
390 | (_res.pfcode & RES_PRF_REPLY)) { | ||
391 | printf(";; old answer:\n"); | ||
392 | __p_query(answer); | ||
393 | } | ||
394 | #endif | ||
395 | goto wait; | ||
396 | } | ||
397 | if (anhp->rcode == SERVFAIL || anhp->rcode == NOTIMP || | ||
398 | anhp->rcode == REFUSED) { | ||
399 | #ifdef DEBUG | ||
400 | if (_res.options & RES_DEBUG) { | ||
401 | printf("server rejected query:\n"); | ||
402 | __p_query(answer); | ||
403 | } | ||
404 | #endif | ||
405 | badns |= (1<<ns); | ||
406 | continue; | ||
407 | } | ||
408 | if (!(_res.options & RES_IGNTC) && anhp->tc) { | ||
409 | /* | ||
410 | * get rest of answer; | ||
411 | * use TCP with same server. | ||
412 | */ | ||
413 | #ifdef DEBUG | ||
414 | if (_res.options & RES_DEBUG) | ||
415 | printf(";; truncated answer\n"); | ||
416 | #endif | ||
417 | (void) close(s); | ||
418 | s = -1; | ||
419 | v_circuit = 1; | ||
420 | goto usevc; | ||
421 | } | ||
422 | } | ||
423 | #ifdef DEBUG | ||
424 | if (_res.options & RES_DEBUG) | ||
425 | printf(";; got answer:\n"); | ||
426 | if ((_res.options & RES_DEBUG) || | ||
427 | (_res.pfcode & RES_PRF_REPLY)) | ||
428 | __p_query(answer); | ||
429 | #endif | ||
430 | /* | ||
431 | * If using virtual circuits, we assume that the first server | ||
432 | * is preferred * over the rest (i.e. it is on the local | ||
433 | * machine) and only keep that one open. | ||
434 | * If we have temporarily opened a virtual circuit, | ||
435 | * or if we haven't been asked to keep a socket open, | ||
436 | * close the socket. | ||
437 | */ | ||
438 | if ((v_circuit && | ||
439 | ((_res.options & RES_USEVC) == 0 || ns != 0)) || | ||
440 | (_res.options & RES_STAYOPEN) == 0) { | ||
441 | (void) close(s); | ||
442 | s = -1; | ||
443 | } | ||
444 | return (resplen); | ||
445 | } | ||
446 | } | ||
447 | if (s >= 0) { | ||
448 | (void) close(s); | ||
449 | s = -1; | ||
450 | } | ||
451 | if (v_circuit == 0) | ||
452 | if (gotsomewhere == 0) | ||
453 | errno = ECONNREFUSED; /* no nameservers found */ | ||
454 | else | ||
455 | errno = ETIMEDOUT; /* no answer obtained */ | ||
456 | else | ||
457 | errno = terrno; | ||
458 | return (-1); | ||
459 | } | ||
460 | |||
461 | /* | ||
462 | * This routine is for closing the socket if a virtual circuit is used and | ||
463 | * the program wants to close it. This provides support for endhostent() | ||
464 | * which expects to close the socket. | ||
465 | * | ||
466 | * This routine is not expected to be user visible. | ||
467 | */ | ||
468 | _res_close() | ||
469 | { | ||
470 | if (s != -1) { | ||
471 | (void) close(s); | ||
472 | s = -1; | ||
473 | } | ||
474 | } | ||
diff --git a/src/lib/libc/net/resolver.3 b/src/lib/libc/net/resolver.3 new file mode 100644 index 0000000000..99abe17f03 --- /dev/null +++ b/src/lib/libc/net/resolver.3 | |||
@@ -0,0 +1,344 @@ | |||
1 | .\" $NetBSD: resolver.3,v 1.5 1995/02/25 06:21:02 cgd Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1985, 1991, 1993 | ||
4 | .\" The Regents of the University of California. All rights reserved. | ||
5 | .\" | ||
6 | .\" Redistribution and use in source and binary forms, with or without | ||
7 | .\" modification, are permitted provided that the following conditions | ||
8 | .\" are met: | ||
9 | .\" 1. Redistributions of source code must retain the above copyright | ||
10 | .\" notice, this list of conditions and the following disclaimer. | ||
11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
12 | .\" notice, this list of conditions and the following disclaimer in the | ||
13 | .\" documentation and/or other materials provided with the distribution. | ||
14 | .\" 3. All advertising materials mentioning features or use of this software | ||
15 | .\" must display the following acknowledgement: | ||
16 | .\" This product includes software developed by the University of | ||
17 | .\" California, Berkeley and its contributors. | ||
18 | .\" 4. Neither the name of the University nor the names of its contributors | ||
19 | .\" may be used to endorse or promote products derived from this software | ||
20 | .\" without specific prior written permission. | ||
21 | .\" | ||
22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
32 | .\" SUCH DAMAGE. | ||
33 | .\" | ||
34 | .\" @(#)resolver.3 8.1 (Berkeley) 6/4/93 | ||
35 | .\" | ||
36 | .Dd June 4, 1993 | ||
37 | .Dt RESOLVER 3 | ||
38 | .Os BSD 4.3 | ||
39 | .Sh NAME | ||
40 | .Nm res_query , | ||
41 | .Nm res_search , | ||
42 | .Nm res_mkquery , | ||
43 | .Nm res_send , | ||
44 | .Nm res_init , | ||
45 | .Nm dn_comp , | ||
46 | .Nm dn_expand | ||
47 | .Nd resolver routines | ||
48 | .Sh SYNOPSIS | ||
49 | .Fd #include <sys/types.h> | ||
50 | .Fd #include <netinet/in.h> | ||
51 | .Fd #include <arpa/nameser.h> | ||
52 | .Fd #include <resolv.h> | ||
53 | .Fo res_query | ||
54 | .Fa "char *dname" | ||
55 | .Fa "int class" | ||
56 | .Fa "int type" | ||
57 | .Fa "u_char *answer" | ||
58 | .Fa "int anslen" | ||
59 | .Fc | ||
60 | .Fo res_search | ||
61 | .Fa "char *dname" | ||
62 | .Fa "int class" | ||
63 | .Fa "int type" | ||
64 | .Fa "u_char *answer" | ||
65 | .Fa "int anslen" | ||
66 | .Fc | ||
67 | .Fo res_mkquery | ||
68 | .Fa "int op" | ||
69 | .Fa "char *dname" | ||
70 | .Fa "int class" | ||
71 | .Fa "int type" | ||
72 | .Fa "char *data" | ||
73 | .Fa "int datalen" | ||
74 | .Fa "struct rrec *newrr" | ||
75 | .Fa "char *buf" | ||
76 | .Fa "int buflen" | ||
77 | .Fc | ||
78 | .Fo res_send | ||
79 | .Fa "char *msg" | ||
80 | .Fa "int msglen" | ||
81 | .Fa "char *answer" | ||
82 | .Fa "int anslen" | ||
83 | .Fc | ||
84 | .Fn res_init | ||
85 | .Fo dn_comp | ||
86 | .Fa "char *exp_dn" | ||
87 | .Fa "char *comp_dn" | ||
88 | .Fa "int length" | ||
89 | .Fa "char **dnptrs" | ||
90 | .Fa "char **lastdnptr" | ||
91 | .Fc | ||
92 | .Fo dn_expand | ||
93 | .Fa "u_char *msg" | ||
94 | .Fa "u_char *eomorig" | ||
95 | .Fa "u_char *comp_dn" | ||
96 | .Fa "u_char *exp_dn" | ||
97 | .Fa "int length" | ||
98 | .Fc | ||
99 | .Sh DESCRIPTION | ||
100 | These routines are used for making, sending and interpreting | ||
101 | query and reply messages with Internet domain name servers. | ||
102 | .Pp | ||
103 | Global configuration and state information that is used by the | ||
104 | resolver routines is kept in the structure | ||
105 | .Em _res . | ||
106 | Most of the values have reasonable defaults and can be ignored. | ||
107 | Options | ||
108 | stored in | ||
109 | .Em _res.options | ||
110 | are defined in | ||
111 | .Pa resolv.h | ||
112 | and are as follows. | ||
113 | Options are stored as a simple bit mask containing the bitwise ``or'' | ||
114 | of the options enabled. | ||
115 | .Bl -tag -width RES_DEFNAMES | ||
116 | .It Dv RES_INIT | ||
117 | True if the initial name server address and default domain name are | ||
118 | initialized (i.e., | ||
119 | .Fn res_init | ||
120 | has been called). | ||
121 | .It Dv RES_DEBUG | ||
122 | Print debugging messages. | ||
123 | .It Dv RES_AAONLY | ||
124 | Accept authoritative answers only. | ||
125 | With this option, | ||
126 | .Fn res_send | ||
127 | should continue until it finds an authoritative answer or finds an error. | ||
128 | Currently this is not implemented. | ||
129 | .It Dv RES_USEVC | ||
130 | Use | ||
131 | .Tn TCP | ||
132 | connections for queries instead of | ||
133 | .Tn UDP | ||
134 | datagrams. | ||
135 | .It Dv RES_STAYOPEN | ||
136 | Used with | ||
137 | .Dv RES_USEVC | ||
138 | to keep the | ||
139 | .Tn TCP | ||
140 | connection open between | ||
141 | queries. | ||
142 | This is useful only in programs that regularly do many queries. | ||
143 | .Tn UDP | ||
144 | should be the normal mode used. | ||
145 | .It Dv RES_IGNTC | ||
146 | Unused currently (ignore truncation errors, i.e., don't retry with | ||
147 | .Tn TCP ) . | ||
148 | .It Dv RES_RECURSE | ||
149 | Set the recursion-desired bit in queries. | ||
150 | This is the default. | ||
151 | .Pf ( Fn res_send | ||
152 | does not do iterative queries and expects the name server | ||
153 | to handle recursion.) | ||
154 | .It Dv RES_DEFNAMES | ||
155 | If set, | ||
156 | .Fn res_search | ||
157 | will append the default domain name to single-component names | ||
158 | (those that do not contain a dot). | ||
159 | This option is enabled by default. | ||
160 | .It Dv RES_DNSRCH | ||
161 | If this option is set, | ||
162 | .Fn res_search | ||
163 | will search for host names in the current domain and in parent domains; see | ||
164 | .Xr hostname 7 . | ||
165 | This is used by the standard host lookup routine | ||
166 | .Xr gethostbyname 3 . | ||
167 | This option is enabled by default. | ||
168 | .El | ||
169 | .Pp | ||
170 | The | ||
171 | .Fn res_init | ||
172 | routine | ||
173 | reads the configuration file (if any; see | ||
174 | .Xr resolv.conf 5 ) | ||
175 | to get the default domain name, | ||
176 | search list and | ||
177 | the Internet address of the local name server(s). | ||
178 | If no server is configured, the host running | ||
179 | the resolver is tried. | ||
180 | The current domain name is defined by the hostname | ||
181 | if not specified in the configuration file; | ||
182 | it can be overridden by the environment variable | ||
183 | .Ev LOCALDOMAIN . | ||
184 | This environment variable may contain several blank-separated | ||
185 | tokens if you wish to override the | ||
186 | .Fa search list | ||
187 | on a per-process basis. | ||
188 | This is similar to the | ||
189 | .Fa search | ||
190 | command in the configuration file. | ||
191 | Another environment variable | ||
192 | .Ev RES_OPTIONS | ||
193 | can be set to override certain internal resolver options which | ||
194 | are otherwise set by changing fields in the | ||
195 | .Fa _res | ||
196 | structure or are inherited from the configuration file's | ||
197 | .Fa options | ||
198 | command. | ||
199 | The syntax of the | ||
200 | .Ev RES_OPTIONS | ||
201 | environment variable is explained in | ||
202 | .Xr resolv.conf 5 . | ||
203 | Initialization normally occurs on the first call | ||
204 | to one of the following routines. | ||
205 | .Pp | ||
206 | The | ||
207 | .Fn res_query | ||
208 | function provides an interface to the server query mechanism. | ||
209 | It constructs a query, sends it to the local server, | ||
210 | awaits a response, and makes preliminary checks on the reply. | ||
211 | The query requests information of the specified | ||
212 | .Fa type | ||
213 | and | ||
214 | .Fa class | ||
215 | for the specified fully-qualified domain name | ||
216 | .Fa dname . | ||
217 | The reply message is left in the | ||
218 | .Fa answer | ||
219 | buffer with length | ||
220 | .Fa anslen | ||
221 | supplied by the caller. | ||
222 | .Pp | ||
223 | The | ||
224 | .Fn res_search | ||
225 | routine makes a query and awaits a response like | ||
226 | .Fn res_query , | ||
227 | but in addition, it implements the default and search rules | ||
228 | controlled by the | ||
229 | .Dv RES_DEFNAMES | ||
230 | and | ||
231 | .Dv RES_DNSRCH | ||
232 | options. | ||
233 | It returns the first successful reply. | ||
234 | .Pp | ||
235 | The remaining routines are lower-level routines used by | ||
236 | .Fn res_query . | ||
237 | The | ||
238 | .Fn res_mkquery | ||
239 | function | ||
240 | constructs a standard query message and places it in | ||
241 | .Fa buf . | ||
242 | It returns the size of the query, or \-1 if the query is | ||
243 | larger than | ||
244 | .Fa buflen . | ||
245 | The query type | ||
246 | .Fa op | ||
247 | is usually | ||
248 | .Dv QUERY , | ||
249 | but can be any of the query types defined in | ||
250 | .Aq Pa arpa/nameser.h . | ||
251 | The domain name for the query is given by | ||
252 | .Fa dname . | ||
253 | .Fa Newrr | ||
254 | is currently unused but is intended for making update messages. | ||
255 | .Pp | ||
256 | The | ||
257 | .Fn res_send | ||
258 | routine | ||
259 | sends a pre-formatted query and returns an answer. | ||
260 | It will call | ||
261 | .Fn res_init | ||
262 | if | ||
263 | .Dv RES_INIT | ||
264 | is not set, send the query to the local name server, and | ||
265 | handle timeouts and retries. | ||
266 | The length of the reply message is returned, or | ||
267 | \-1 if there were errors. | ||
268 | .Pp | ||
269 | The | ||
270 | .Fn dn_comp | ||
271 | function | ||
272 | compresses the domain name | ||
273 | .Fa exp_dn | ||
274 | and stores it in | ||
275 | .Fa comp_dn . | ||
276 | The size of the compressed name is returned or \-1 if there were errors. | ||
277 | The size of the array pointed to by | ||
278 | .Fa comp_dn | ||
279 | is given by | ||
280 | .Fa length . | ||
281 | The compression uses | ||
282 | an array of pointers | ||
283 | .Fa dnptrs | ||
284 | to previously-compressed names in the current message. | ||
285 | The first pointer points to | ||
286 | to the beginning of the message and the list ends with | ||
287 | .Dv NULL . | ||
288 | The limit to the array is specified by | ||
289 | .Fa lastdnptr . | ||
290 | A side effect of | ||
291 | .Fn dn_comp | ||
292 | is to update the list of pointers for | ||
293 | labels inserted into the message | ||
294 | as the name is compressed. | ||
295 | If | ||
296 | .Em dnptr | ||
297 | is | ||
298 | .Dv NULL, names are not compressed. | ||
299 | If | ||
300 | .Fa lastdnptr | ||
301 | is | ||
302 | .Dv NULL , | ||
303 | the list of labels is not updated. | ||
304 | .Pp | ||
305 | The | ||
306 | .Fn dn_expand | ||
307 | entry | ||
308 | expands the compressed domain name | ||
309 | .Fa comp_dn | ||
310 | to a full domain name | ||
311 | The compressed name is contained in a query or reply message; | ||
312 | .Fa msg | ||
313 | is a pointer to the beginning of the message. | ||
314 | The uncompressed name is placed in the buffer indicated by | ||
315 | .Fa exp_dn | ||
316 | which is of size | ||
317 | .Fa length . | ||
318 | The size of compressed name is returned or \-1 if there was an error. | ||
319 | .Sh FILES | ||
320 | .Bl -tag -width Pa | ||
321 | /etc/resolv.conf | ||
322 | The configuration file | ||
323 | see | ||
324 | .Xr resolv.conf 5 . | ||
325 | .El | ||
326 | .Sh SEE ALSO | ||
327 | .Xr gethostbyname 3 , | ||
328 | .Xr named 8 , | ||
329 | .Xr resolv.conf 5 , | ||
330 | .Xr hostname 7 , | ||
331 | .Pp | ||
332 | .%T RFC1032 , | ||
333 | .%T RFC1033 , | ||
334 | .%T RFC1034 , | ||
335 | .%T RFC1035 , | ||
336 | .%T RFC974 | ||
337 | .Rs | ||
338 | .%T "Name Server Operations Guide for BIND" | ||
339 | .Re | ||
340 | .Sh HISTORY | ||
341 | The | ||
342 | .Nm | ||
343 | function appeared in | ||
344 | .Bx 4.3 . | ||
diff --git a/src/lib/libc/net/send.c b/src/lib/libc/net/send.c new file mode 100644 index 0000000000..88f3550ec6 --- /dev/null +++ b/src/lib/libc/net/send.c | |||
@@ -0,0 +1,56 @@ | |||
1 | /* $NetBSD: send.c,v 1.6 1995/02/25 06:21:02 cgd Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1988, 1993 | ||
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 the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | */ | ||
35 | |||
36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
37 | #if 0 | ||
38 | static char sccsid[] = "@(#)send.c 8.2 (Berkeley) 2/21/94"; | ||
39 | #else | ||
40 | static char rcsid[] = "$NetBSD: send.c,v 1.6 1995/02/25 06:21:02 cgd Exp $"; | ||
41 | #endif | ||
42 | #endif /* LIBC_SCCS and not lint */ | ||
43 | |||
44 | #include <sys/types.h> | ||
45 | #include <sys/socket.h> | ||
46 | |||
47 | #include <stddef.h> | ||
48 | |||
49 | ssize_t | ||
50 | send(s, msg, len, flags) | ||
51 | int s, flags; | ||
52 | size_t len; | ||
53 | const void *msg; | ||
54 | { | ||
55 | return (sendto(s, msg, len, flags, NULL, 0)); | ||
56 | } | ||
diff --git a/src/lib/libc/net/sethostent.c b/src/lib/libc/net/sethostent.c new file mode 100644 index 0000000000..00f6499695 --- /dev/null +++ b/src/lib/libc/net/sethostent.c | |||
@@ -0,0 +1,62 @@ | |||
1 | /* $NetBSD: sethostent.c,v 1.4 1995/02/25 06:21:03 cgd Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1985, 1993 | ||
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 the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | */ | ||
35 | |||
36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
37 | #if 0 | ||
38 | static char sccsid[] = "@(#)sethostent.c 8.1 (Berkeley) 6/4/93"; | ||
39 | #else | ||
40 | static char rcsid[] = "$NetBSD: sethostent.c,v 1.4 1995/02/25 06:21:03 cgd Exp $"; | ||
41 | #endif | ||
42 | #endif /* LIBC_SCCS and not lint */ | ||
43 | |||
44 | #include <sys/param.h> | ||
45 | #include <netinet/in.h> | ||
46 | #include <arpa/nameser.h> | ||
47 | #include <netdb.h> | ||
48 | #include <resolv.h> | ||
49 | |||
50 | void | ||
51 | sethostent(stayopen) | ||
52 | { | ||
53 | if (stayopen) | ||
54 | _res.options |= RES_STAYOPEN | RES_USEVC; | ||
55 | } | ||
56 | |||
57 | void | ||
58 | endhostent() | ||
59 | { | ||
60 | _res.options &= ~(RES_STAYOPEN | RES_USEVC); | ||
61 | _res_close(); | ||
62 | } | ||