1 1.1 christos /* $NetBSD: getservent_r.c,v 1.1.1.2 2012/09/09 16:07:59 christos Exp $ */ 2 1.1 christos 3 1.1 christos /* 4 1.1 christos * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 5 1.1 christos * Copyright (c) 1998-1999 by Internet Software Consortium. 6 1.1 christos * 7 1.1 christos * Permission to use, copy, modify, and distribute this software for any 8 1.1 christos * purpose with or without fee is hereby granted, provided that the above 9 1.1 christos * copyright notice and this permission notice appear in all copies. 10 1.1 christos * 11 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 12 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 14 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 17 1.1 christos * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 1.1 christos */ 19 1.1 christos 20 1.1 christos #if defined(LIBC_SCCS) && !defined(lint) 21 1.1.1.2 christos static const char rcsid[] = "Id: getservent_r.c,v 1.6 2006/08/01 01:14:16 marka Exp "; 22 1.1 christos #endif /* LIBC_SCCS and not lint */ 23 1.1 christos 24 1.1 christos #include <port_before.h> 25 1.1 christos #if !defined(_REENTRANT) || !defined(DO_PTHREADS) 26 1.1 christos static int getservent_r_not_required = 0; 27 1.1 christos #else 28 1.1 christos #include <errno.h> 29 1.1 christos #include <string.h> 30 1.1 christos #include <stdio.h> 31 1.1 christos #include <sys/types.h> 32 1.1 christos #include <netinet/in.h> 33 1.1 christos #include <netdb.h> 34 1.1 christos #include <sys/param.h> 35 1.1 christos #include <port_after.h> 36 1.1 christos 37 1.1 christos #ifdef SERV_R_RETURN 38 1.1 christos 39 1.1 christos static SERV_R_RETURN 40 1.1 christos copy_servent(struct servent *, struct servent *, SERV_R_COPY_ARGS); 41 1.1 christos 42 1.1 christos SERV_R_RETURN 43 1.1 christos getservbyname_r(const char *name, const char *proto, 44 1.1 christos struct servent *sptr, SERV_R_ARGS) { 45 1.1 christos struct servent *se = getservbyname(name, proto); 46 1.1 christos #ifdef SERV_R_SETANSWER 47 1.1 christos int n = 0; 48 1.1 christos 49 1.1 christos if (se == NULL || (n = copy_servent(se, sptr, SERV_R_COPY)) != 0) 50 1.1 christos *answerp = NULL; 51 1.1 christos else 52 1.1 christos *answerp = sptr; 53 1.1 christos 54 1.1 christos return (n); 55 1.1 christos #else 56 1.1 christos if (se == NULL) 57 1.1 christos return (SERV_R_BAD); 58 1.1 christos 59 1.1 christos return (copy_servent(se, sptr, SERV_R_COPY)); 60 1.1 christos #endif 61 1.1 christos } 62 1.1 christos 63 1.1 christos SERV_R_RETURN 64 1.1 christos getservbyport_r(int port, const char *proto, 65 1.1 christos struct servent *sptr, SERV_R_ARGS) { 66 1.1 christos struct servent *se = getservbyport(port, proto); 67 1.1 christos #ifdef SERV_R_SETANSWER 68 1.1 christos int n = 0; 69 1.1 christos 70 1.1 christos if (se == NULL || (n = copy_servent(se, sptr, SERV_R_COPY)) != 0) 71 1.1 christos *answerp = NULL; 72 1.1 christos else 73 1.1 christos *answerp = sptr; 74 1.1 christos 75 1.1 christos return (n); 76 1.1 christos #else 77 1.1 christos if (se == NULL) 78 1.1 christos return (SERV_R_BAD); 79 1.1 christos 80 1.1 christos return (copy_servent(se, sptr, SERV_R_COPY)); 81 1.1 christos #endif 82 1.1 christos } 83 1.1 christos 84 1.1 christos /*% 85 1.1 christos * These assume a single context is in operation per thread. 86 1.1 christos * If this is not the case we will need to call irs directly 87 1.1 christos * rather than through the base functions. 88 1.1 christos */ 89 1.1 christos 90 1.1 christos SERV_R_RETURN 91 1.1 christos getservent_r(struct servent *sptr, SERV_R_ARGS) { 92 1.1 christos struct servent *se = getservent(); 93 1.1 christos #ifdef SERV_R_SETANSWER 94 1.1 christos int n = 0; 95 1.1 christos 96 1.1 christos if (se == NULL || (n = copy_servent(se, sptr, SERV_R_COPY)) != 0) 97 1.1 christos *answerp = NULL; 98 1.1 christos else 99 1.1 christos *answerp = sptr; 100 1.1 christos 101 1.1 christos return (n); 102 1.1 christos #else 103 1.1 christos if (se == NULL) 104 1.1 christos return (SERV_R_BAD); 105 1.1 christos 106 1.1 christos return (copy_servent(se, sptr, SERV_R_COPY)); 107 1.1 christos #endif 108 1.1 christos } 109 1.1 christos 110 1.1 christos SERV_R_SET_RETURN 111 1.1 christos #ifdef SERV_R_ENT_ARGS 112 1.1 christos setservent_r(int stay_open, SERV_R_ENT_ARGS) 113 1.1 christos #else 114 1.1 christos setservent_r(int stay_open) 115 1.1 christos #endif 116 1.1 christos { 117 1.1 christos #ifdef SERV_R_ENT_UNUSED 118 1.1 christos SERV_R_ENT_UNUSED; 119 1.1 christos #endif 120 1.1 christos setservent(stay_open); 121 1.1 christos #ifdef SERV_R_SET_RESULT 122 1.1 christos return (SERV_R_SET_RESULT); 123 1.1 christos #endif 124 1.1 christos } 125 1.1 christos 126 1.1 christos SERV_R_END_RETURN 127 1.1 christos #ifdef SERV_R_ENT_ARGS 128 1.1 christos endservent_r(SERV_R_ENT_ARGS) 129 1.1 christos #else 130 1.1 christos endservent_r() 131 1.1 christos #endif 132 1.1 christos { 133 1.1 christos #ifdef SERV_R_ENT_UNUSED 134 1.1 christos SERV_R_ENT_UNUSED; 135 1.1 christos #endif 136 1.1 christos endservent(); 137 1.1 christos SERV_R_END_RESULT(SERV_R_OK); 138 1.1 christos } 139 1.1 christos 140 1.1 christos /* Private */ 141 1.1 christos 142 1.1 christos #ifndef SERVENT_DATA 143 1.1 christos static SERV_R_RETURN 144 1.1 christos copy_servent(struct servent *se, struct servent *sptr, SERV_R_COPY_ARGS) { 145 1.1 christos char *cp; 146 1.1 christos int i, n; 147 1.1 christos int numptr, len; 148 1.1 christos 149 1.1 christos /* Find out the amount of space required to store the answer. */ 150 1.1 christos numptr = 1; /*%< NULL ptr */ 151 1.1 christos len = (char *)ALIGN(buf) - buf; 152 1.1 christos for (i = 0; se->s_aliases[i]; i++, numptr++) { 153 1.1 christos len += strlen(se->s_aliases[i]) + 1; 154 1.1 christos } 155 1.1 christos len += strlen(se->s_name) + 1; 156 1.1 christos len += strlen(se->s_proto) + 1; 157 1.1 christos len += numptr * sizeof(char*); 158 1.1 christos 159 1.1 christos if (len > (int)buflen) { 160 1.1 christos errno = ERANGE; 161 1.1 christos return (SERV_R_BAD); 162 1.1 christos } 163 1.1 christos 164 1.1 christos /* copy port value */ 165 1.1 christos sptr->s_port = se->s_port; 166 1.1 christos 167 1.1 christos cp = (char *)ALIGN(buf) + numptr * sizeof(char *); 168 1.1 christos 169 1.1 christos /* copy official name */ 170 1.1 christos n = strlen(se->s_name) + 1; 171 1.1 christos strcpy(cp, se->s_name); 172 1.1 christos sptr->s_name = cp; 173 1.1 christos cp += n; 174 1.1 christos 175 1.1 christos /* copy aliases */ 176 1.1 christos sptr->s_aliases = (char **)ALIGN(buf); 177 1.1 christos for (i = 0 ; se->s_aliases[i]; i++) { 178 1.1 christos n = strlen(se->s_aliases[i]) + 1; 179 1.1 christos strcpy(cp, se->s_aliases[i]); 180 1.1 christos sptr->s_aliases[i] = cp; 181 1.1 christos cp += n; 182 1.1 christos } 183 1.1 christos sptr->s_aliases[i] = NULL; 184 1.1 christos 185 1.1 christos /* copy proto */ 186 1.1 christos n = strlen(se->s_proto) + 1; 187 1.1 christos strcpy(cp, se->s_proto); 188 1.1 christos sptr->s_proto = cp; 189 1.1 christos cp += n; 190 1.1 christos 191 1.1 christos return (SERV_R_OK); 192 1.1 christos } 193 1.1 christos #else /* !SERVENT_DATA */ 194 1.1 christos static int 195 1.1 christos copy_servent(struct servent *se, struct servent *sptr, SERV_R_COPY_ARGS) { 196 1.1 christos char *cp, *eob; 197 1.1 christos int i, n; 198 1.1 christos 199 1.1 christos /* copy port value */ 200 1.1 christos sptr->s_port = se->s_port; 201 1.1 christos 202 1.1 christos /* copy official name */ 203 1.1 christos cp = sdptr->line; 204 1.1 christos eob = sdptr->line + sizeof(sdptr->line); 205 1.1 christos if ((n = strlen(se->s_name) + 1) < (eob - cp)) { 206 1.1 christos strcpy(cp, se->s_name); 207 1.1 christos sptr->s_name = cp; 208 1.1 christos cp += n; 209 1.1 christos } else { 210 1.1 christos return (-1); 211 1.1 christos } 212 1.1 christos 213 1.1 christos /* copy aliases */ 214 1.1 christos i = 0; 215 1.1 christos sptr->s_aliases = sdptr->serv_aliases; 216 1.1 christos while (se->s_aliases[i] && i < (_MAXALIASES-1)) { 217 1.1 christos if ((n = strlen(se->s_aliases[i]) + 1) < (eob - cp)) { 218 1.1 christos strcpy(cp, se->s_aliases[i]); 219 1.1 christos sptr->s_aliases[i] = cp; 220 1.1 christos cp += n; 221 1.1 christos } else { 222 1.1 christos break; 223 1.1 christos } 224 1.1 christos i++; 225 1.1 christos } 226 1.1 christos sptr->s_aliases[i] = NULL; 227 1.1 christos 228 1.1 christos /* copy proto */ 229 1.1 christos if ((n = strlen(se->s_proto) + 1) < (eob - cp)) { 230 1.1 christos strcpy(cp, se->s_proto); 231 1.1 christos sptr->s_proto = cp; 232 1.1 christos cp += n; 233 1.1 christos } else { 234 1.1 christos return (-1); 235 1.1 christos } 236 1.1 christos 237 1.1 christos return (SERV_R_OK); 238 1.1 christos } 239 1.1 christos #endif /* !SERVENT_DATA */ 240 1.1 christos #else /*SERV_R_RETURN */ 241 1.1 christos static int getservent_r_unknown_system = 0; 242 1.1 christos #endif /*SERV_R_RETURN */ 243 1.1 christos #endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */ 244 1.1 christos /*! \file */ 245