1 /*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28 #include <sys/cdefs.h>
29 #include <sys/types.h>
30 #include <endian.h>
31 #include <malloc.h>
32 #include <netdb.h>
33 #include "servent.h"
34 #include "services.h"
35
36 void
setservent(int f)37 setservent(int f)
38 {
39 res_static rs = __res_get_static();
40 if (rs) {
41 rs->servent_ptr = NULL;
42 }
43 }
44
45 void
endservent(void)46 endservent(void)
47 {
48 /* nothing to do */
49 }
50
51 struct servent *
getservent_r(res_static rs)52 getservent_r( res_static rs )
53 {
54 const char* p;
55 const char* q;
56 int namelen;
57 int nn,count;
58 int total = 0;
59 int port;
60 char* p2;
61
62 p = rs->servent_ptr;
63 if (p == NULL)
64 p = _services;
65 else if (p[0] == 0)
66 return NULL;
67
68 /* first compute the total size */
69 namelen = p[0];
70 total += namelen + 1;
71 q = p + 1 + namelen + 3; /* skip name + port + proto */
72 count = q[0]; /* get aliascount */
73 q += 1;
74
75 total += (count+1)*sizeof(char*);
76 for (nn = 0; nn < count; nn++) {
77 int len2 = q[0];
78 total += 1 + len2;
79 q += 1 + len2;
80 }
81
82 /* reallocate the thread-specific servent struct */
83 p2 = realloc( (char*)rs->servent.s_aliases, total );
84 if (p2 == NULL)
85 return NULL;
86
87 /* now write to it */
88 rs->servent.s_aliases = (char**) p2;
89 p2 += (count+1)*sizeof(char*);
90 rs->servent.s_name = p2;
91 p2 += namelen + 1;
92 rs->servent.s_proto = p2;
93
94 /* copy name + port + setup protocol */
95 memcpy( rs->servent.s_name, p+1, namelen );
96 rs->servent.s_name[namelen] = 0;
97 p += 1 + namelen;
98
99 /* s_port must be in network byte order */
100 port = ((((unsigned char*)p)[0] << 8) |
101 ((unsigned char*)p)[1]);
102
103 rs->servent.s_port = htons(port);
104 rs->servent.s_proto = p[2] == 't' ? "tcp" : "udp";
105 p += 4; /* skip port(2) + proto(1) + aliascount(1) */
106
107 for (nn = 0; nn < count; nn++) {
108 int len2 = p[0];
109 rs->servent.s_aliases[nn] = p2;
110 memcpy( p2, p+1, len2 );
111 p2[len2] = 0;
112 p2 += len2 + 1;
113 p += len2 + 1;
114 }
115 rs->servent.s_aliases[nn] = NULL;
116
117 rs->servent_ptr = p;
118
119 return &rs->servent;
120 }
121
122 struct servent *
getservent(void)123 getservent(void)
124 {
125 res_static rs = __res_get_static();
126
127 if (rs == NULL) return NULL;
128
129 return getservent_r(rs);
130 }
131