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 
29 #include <netdb.h>
30 
31 #include <endian.h>
32 #include <stdlib.h>
33 #include <string.h>
34 
35 #include "resolv_static.h"
36 #include "services.h"
37 
getservent_r(struct res_static * rs)38 struct servent* getservent_r(struct res_static* rs) {
39     const char*  p;
40     const char*  q;
41     int          namelen;
42     int          nn,count;
43     int          total = 0;
44     int          port;
45     char*        p2;
46 
47     p = rs->servent_ptr;
48     if (p == NULL)
49         p = _services;
50     else if (p[0] == 0)
51         return NULL;
52 
53     /* first compute the total size */
54     namelen = p[0];
55     total  += namelen + 1;
56     q       = p + 1 + namelen + 3;  /* skip name + port + proto */
57     count   = q[0];   /* get aliascount */
58     q      += 1;
59 
60     total += (count+1)*sizeof(char*);
61     for (nn = 0; nn < count; nn++) {
62         int  len2 = q[0];
63         total += 1 + len2;
64         q     += 1 + len2;
65     }
66 
67     /* reallocate the thread-specific servent struct */
68     p2 = realloc( (char*)rs->servent.s_aliases, total );
69     if (p2 == NULL)
70         return NULL;
71 
72     /* now write to it */
73     rs->servent.s_aliases = (char**) p2;
74     p2                   += (count+1)*sizeof(char*);
75     rs->servent.s_name    = p2;
76     p2                   += namelen + 1;
77     rs->servent.s_proto   = p2;
78 
79     /* copy name + port + setup protocol */
80     memcpy( rs->servent.s_name, p+1, namelen );
81     rs->servent.s_name[namelen] = 0;
82     p += 1 + namelen;
83 
84     /* s_port must be in network byte order */
85     port = ((((unsigned char*)p)[0] << 8) |
86              ((unsigned char*)p)[1]);
87 
88     rs->servent.s_port  = htons(port);
89     rs->servent.s_proto = p[2] == 't' ? "tcp" : "udp";
90     p += 4;  /* skip port(2) + proto(1) + aliascount(1) */
91 
92     for (nn = 0; nn < count; nn++) {
93         int  len2 = p[0];
94         rs->servent.s_aliases[nn] = p2;
95         memcpy( p2, p+1, len2 );
96         p2[len2] = 0;
97         p2 += len2 + 1;
98         p  += len2 + 1;
99     }
100     rs->servent.s_aliases[nn] = NULL;
101 
102     rs->servent_ptr = p;
103 
104     return &rs->servent;
105 }
106 
setservent(int stayopen)107 void setservent(int stayopen) {
108   endservent();
109 }
110 
endservent(void)111 void endservent(void) {
112   struct res_static* rs = __res_get_static();
113   if (rs) rs->servent_ptr = NULL;
114 }
115 
getservent(void)116 struct servent* getservent(void) {
117   struct res_static* rs = __res_get_static();
118   return rs ? getservent_r(rs) : NULL;
119 }
120 
getservbyname(const char * name,const char * proto)121 struct servent* getservbyname(const char* name, const char* proto) {
122   struct res_static* rs = __res_get_static();
123   if (rs == NULL) return NULL;
124 
125   const char* old_servent_ptr = rs->servent_ptr;
126   rs->servent_ptr = NULL;
127   struct servent* s;
128   while ((s = getservent_r(rs)) != NULL) {
129     if (strcmp(s->s_name, name) == 0 && (proto == NULL || strcmp(s->s_proto, proto) == 0)) {
130       break;
131     }
132   }
133   rs->servent_ptr = old_servent_ptr;
134   return s;
135 }
136 
getservbyport(int port,const char * proto)137 struct servent* getservbyport(int port, const char* proto) {
138   struct res_static* rs = __res_get_static();
139   if (rs == NULL) return NULL;
140 
141   const char* old_servent_ptr = rs->servent_ptr;
142   rs->servent_ptr = NULL;
143   struct servent* s;
144   while ((s = getservent_r(rs)) != NULL) {
145     if (s->s_port == port && (proto == NULL || strcmp(s->s_proto, proto) == 0)) {
146       break;
147     }
148   }
149   rs->servent_ptr = old_servent_ptr;
150   return s;
151 }
152