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