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