diff options
| author | jsing <> | 2014-04-17 14:24:41 +0000 |
|---|---|---|
| committer | jsing <> | 2014-04-17 14:24:41 +0000 |
| commit | a3daace834b5f4711a8998100f0bd114c7d1cd74 (patch) | |
| tree | 886bb286703739c4951e54d1bd9934d51a5533c8 /src | |
| parent | 0914a3ee8317cea5bf708dacfe811e5856ba7971 (diff) | |
| download | openbsd-a3daace834b5f4711a8998100f0bd114c7d1cd74.tar.gz openbsd-a3daace834b5f4711a8998100f0bd114c7d1cd74.tar.bz2 openbsd-a3daace834b5f4711a8998100f0bd114c7d1cd74.zip | |
Initial KNF.
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/libssl/src/apps/s_socket.c | 426 |
1 files changed, 214 insertions, 212 deletions
diff --git a/src/lib/libssl/src/apps/s_socket.c b/src/lib/libssl/src/apps/s_socket.c index cc02b51b6d..07c3b9afde 100644 --- a/src/lib/libssl/src/apps/s_socket.c +++ b/src/lib/libssl/src/apps/s_socket.c | |||
| @@ -5,21 +5,21 @@ | |||
| 5 | * This package is an SSL implementation written | 5 | * This package is an SSL implementation written |
| 6 | * by Eric Young (eay@cryptsoft.com). | 6 | * by Eric Young (eay@cryptsoft.com). |
| 7 | * The implementation was written so as to conform with Netscapes SSL. | 7 | * The implementation was written so as to conform with Netscapes SSL. |
| 8 | * | 8 | * |
| 9 | * This library is free for commercial and non-commercial use as long as | 9 | * This library is free for commercial and non-commercial use as long as |
| 10 | * the following conditions are aheared to. The following conditions | 10 | * the following conditions are aheared to. The following conditions |
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | 11 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 13 | * included with this distribution is covered by the same copyright terms | 13 | * included with this distribution is covered by the same copyright terms |
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 15 | * | 15 | * |
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | 16 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 17 | * the code are not to be removed. | 17 | * the code are not to be removed. |
| 18 | * If this package is used in a product, Eric Young should be given attribution | 18 | * If this package is used in a product, Eric Young should be given attribution |
| 19 | * as the author of the parts of the library used. | 19 | * as the author of the parts of the library used. |
| 20 | * This can be in the form of a textual message at program startup or | 20 | * This can be in the form of a textual message at program startup or |
| 21 | * in documentation (online or textual) provided with the package. | 21 | * in documentation (online or textual) provided with the package. |
| 22 | * | 22 | * |
| 23 | * Redistribution and use in source and binary forms, with or without | 23 | * Redistribution and use in source and binary forms, with or without |
| 24 | * modification, are permitted provided that the following conditions | 24 | * modification, are permitted provided that the following conditions |
| 25 | * are met: | 25 | * are met: |
| @@ -34,10 +34,10 @@ | |||
| 34 | * Eric Young (eay@cryptsoft.com)" | 34 | * Eric Young (eay@cryptsoft.com)" |
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | 35 | * The word 'cryptographic' can be left out if the rouines from the library |
| 36 | * being used are not cryptographic related :-). | 36 | * being used are not cryptographic related :-). |
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | 37 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 38 | * the apps directory (application code) you must include an acknowledgement: | 38 | * the apps directory (application code) you must include an acknowledgement: |
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 40 | * | 40 | * |
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| @@ -49,7 +49,7 @@ | |||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 51 | * SUCH DAMAGE. | 51 | * SUCH DAMAGE. |
| 52 | * | 52 | * |
| 53 | * The licence and distribution terms for any publically available version or | 53 | * The licence and distribution terms for any publically available version or |
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 55 | * copied and put under another distribution licence | 55 | * copied and put under another distribution licence |
| @@ -88,194 +88,204 @@ | |||
| 88 | static struct hostent *GetHostByName(char *name); | 88 | static struct hostent *GetHostByName(char *name); |
| 89 | static int ssl_sock_init(void); | 89 | static int ssl_sock_init(void); |
| 90 | static int init_server(int *sock, int port, int type); | 90 | static int init_server(int *sock, int port, int type); |
| 91 | static int init_server_long(int *sock, int port,char *ip, int type); | 91 | static int init_server_long(int *sock, int port, char *ip, int type); |
| 92 | static int do_accept(int acc_sock, int *sock, char **host); | 92 | static int do_accept(int acc_sock, int *sock, char **host); |
| 93 | 93 | ||
| 94 | #define SOCKET_PROTOCOL IPPROTO_TCP | 94 | #define SOCKET_PROTOCOL IPPROTO_TCP |
| 95 | 95 | ||
| 96 | 96 | ||
| 97 | static int | ||
| 98 | ssl_sock_init(void) | ||
| 99 | { | ||
| 100 | return (1); | ||
| 101 | } | ||
| 97 | 102 | ||
| 98 | 103 | int | |
| 99 | static int ssl_sock_init(void) | 104 | init_client(int *sock, char *host, char *port, int type, int af) |
| 100 | { | 105 | { |
| 101 | return(1); | ||
| 102 | } | ||
| 103 | |||
| 104 | int init_client(int *sock, char *host, char *port, int type, int af) | ||
| 105 | { | ||
| 106 | struct addrinfo hints, *ai_top, *ai; | 106 | struct addrinfo hints, *ai_top, *ai; |
| 107 | int i, s; | 107 | int i, s; |
| 108 | 108 | ||
| 109 | if (!ssl_sock_init()) return(0); | 109 | if (!ssl_sock_init()) |
| 110 | return (0); | ||
| 110 | 111 | ||
| 111 | memset(&hints, '\0', sizeof(hints)); | 112 | memset(&hints, '\0', sizeof(hints)); |
| 112 | hints.ai_family = af; | 113 | hints.ai_family = af; |
| 113 | hints.ai_socktype = type; | 114 | hints.ai_socktype = type; |
| 114 | 115 | ||
| 115 | if ((i = getaddrinfo(host, port, &hints, &ai_top)) != 0) | 116 | if ((i = getaddrinfo(host, port, &hints, &ai_top)) != 0) { |
| 116 | { | 117 | BIO_printf(bio_err, "getaddrinfo: %s\n", gai_strerror(i)); |
| 117 | BIO_printf(bio_err,"getaddrinfo: %s\n", gai_strerror(i)); | ||
| 118 | return (0); | 118 | return (0); |
| 119 | } | ||
| 120 | if (ai_top == NULL || ai_top->ai_addr == NULL) { | ||
| 121 | BIO_printf(bio_err, "getaddrinfo returned no addresses\n"); | ||
| 122 | if (ai_top != NULL) { | ||
| 123 | freeaddrinfo(ai_top); | ||
| 119 | } | 124 | } |
| 120 | if (ai_top == NULL || ai_top->ai_addr == NULL) | ||
| 121 | { | ||
| 122 | BIO_printf(bio_err,"getaddrinfo returned no addresses\n"); | ||
| 123 | if (ai_top != NULL) { freeaddrinfo(ai_top); } | ||
| 124 | return (0); | 125 | return (0); |
| 125 | } | 126 | } |
| 126 | 127 | ||
| 127 | for (ai = ai_top; ai != NULL; ai = ai->ai_next) | 128 | for (ai = ai_top; ai != NULL; ai = ai->ai_next) { |
| 128 | { | 129 | s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
| 129 | s=socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); | 130 | if (s == -1) { |
| 130 | if (s == -1) { continue; } | 131 | continue; |
| 131 | if (type == SOCK_STREAM) | 132 | } |
| 132 | { | 133 | if (type == SOCK_STREAM) { |
| 133 | i=0; | 134 | i = 0; |
| 134 | i=setsockopt(s,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i)); | 135 | i = setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, |
| 135 | if (i < 0) { perror("keepalive"); return(0); } | 136 | (char *)&i, sizeof(i)); |
| 137 | if (i < 0) { | ||
| 138 | perror("keepalive"); | ||
| 139 | return (0); | ||
| 140 | } | ||
| 141 | } | ||
| 142 | if ((i = connect(s, ai->ai_addr, ai->ai_addrlen)) == 0) { | ||
| 143 | *sock = s; | ||
| 144 | freeaddrinfo(ai_top); | ||
| 145 | return (1); | ||
| 136 | } | 146 | } |
| 137 | if ((i = connect(s, ai->ai_addr, ai->ai_addrlen)) == 0) | ||
| 138 | { *sock=s; freeaddrinfo(ai_top); return (1); } | ||
| 139 | 147 | ||
| 140 | close(s); | 148 | close(s); |
| 141 | } | 149 | } |
| 142 | 150 | ||
| 143 | perror("connect"); | 151 | perror("connect"); |
| 144 | close(s); | 152 | close(s); |
| 145 | freeaddrinfo(ai_top); | 153 | freeaddrinfo(ai_top); |
| 146 | return(0); | 154 | return (0); |
| 147 | } | 155 | } |
| 148 | 156 | ||
| 149 | int do_server(int port, int type, int *ret, int (*cb)(char *hostname, int s, unsigned char *context), unsigned char *context) | 157 | int |
| 150 | { | 158 | do_server(int port, int type, int *ret, |
| 159 | int (*cb)(char *hostname, int s, unsigned char *context), | ||
| 160 | unsigned char *context) | ||
| 161 | { | ||
| 151 | int sock; | 162 | int sock; |
| 152 | char *name = NULL; | 163 | char *name = NULL; |
| 153 | int accept_socket = 0; | 164 | int accept_socket = 0; |
| 154 | int i; | 165 | int i; |
| 155 | 166 | ||
| 156 | if (!init_server(&accept_socket,port,type)) return(0); | 167 | if (!init_server(&accept_socket, port, type)) |
| 168 | return (0); | ||
| 157 | 169 | ||
| 158 | if (ret != NULL) | 170 | if (ret != NULL) { |
| 159 | { | 171 | *ret = accept_socket; |
| 160 | *ret=accept_socket; | ||
| 161 | /* return(1);*/ | 172 | /* return(1);*/ |
| 162 | } | 173 | } |
| 163 | for (;;) | 174 | for (;;) { |
| 164 | { | 175 | if (type == SOCK_STREAM) { |
| 165 | if (type==SOCK_STREAM) | 176 | if (do_accept(accept_socket, &sock, &name) == 0) { |
| 166 | { | ||
| 167 | if (do_accept(accept_socket,&sock,&name) == 0) | ||
| 168 | { | ||
| 169 | shutdown(accept_socket, SHUT_RD); | 177 | shutdown(accept_socket, SHUT_RD); |
| 170 | close(accept_socket); | 178 | close(accept_socket); |
| 171 | return(0); | 179 | return (0); |
| 172 | } | ||
| 173 | } | 180 | } |
| 174 | else | 181 | } else |
| 175 | sock = accept_socket; | 182 | sock = accept_socket; |
| 176 | i=(*cb)(name,sock, context); | 183 | i = (*cb)(name, sock, context); |
| 177 | if (name != NULL) free(name); | 184 | if (name != NULL) |
| 178 | if (type==SOCK_STREAM) { | 185 | free(name); |
| 186 | if (type == SOCK_STREAM) { | ||
| 179 | shutdown(sock, SHUT_RDWR); | 187 | shutdown(sock, SHUT_RDWR); |
| 180 | close(sock); | 188 | close(sock); |
| 181 | } | 189 | } |
| 182 | if (i < 0) | 190 | if (i < 0) { |
| 183 | { | ||
| 184 | shutdown(accept_socket, SHUT_RDWR); | 191 | shutdown(accept_socket, SHUT_RDWR); |
| 185 | close(accept_socket); | 192 | close(accept_socket); |
| 186 | return(i); | 193 | return (i); |
| 187 | } | ||
| 188 | } | 194 | } |
| 189 | } | 195 | } |
| 196 | } | ||
| 190 | 197 | ||
| 191 | static int init_server_long(int *sock, int port, char *ip, int type) | 198 | static int |
| 192 | { | 199 | init_server_long(int *sock, int port, char *ip, int type) |
| 193 | int ret=0; | 200 | { |
| 201 | int ret = 0; | ||
| 194 | struct sockaddr_in server; | 202 | struct sockaddr_in server; |
| 195 | int s= -1; | 203 | int s = -1; |
| 196 | 204 | ||
| 197 | if (!ssl_sock_init()) return(0); | 205 | if (!ssl_sock_init()) |
| 206 | return (0); | ||
| 198 | 207 | ||
| 199 | memset((char *)&server,0,sizeof(server)); | 208 | memset((char *)&server, 0,sizeof(server)); |
| 200 | server.sin_family=AF_INET; | 209 | server.sin_family = AF_INET; |
| 201 | server.sin_port=htons((unsigned short)port); | 210 | server.sin_port = htons((unsigned short)port); |
| 202 | if (ip == NULL) | 211 | if (ip == NULL) |
| 203 | server.sin_addr.s_addr=INADDR_ANY; | 212 | server.sin_addr.s_addr = INADDR_ANY; |
| 204 | else | 213 | else |
| 205 | /* Added for T3E, address-of fails on bit field (beckman@acl.lanl.gov) */ | 214 | /* Added for T3E, address-of fails on bit field (beckman@acl.lanl.gov) */ |
| 206 | #ifndef BIT_FIELD_LIMITS | 215 | #ifndef BIT_FIELD_LIMITS |
| 207 | memcpy(&server.sin_addr.s_addr,ip,4); | 216 | memcpy(&server.sin_addr.s_addr, ip, 4); |
| 208 | #else | 217 | #else |
| 209 | memcpy(&server.sin_addr,ip,4); | 218 | memcpy(&server.sin_addr, ip, 4); |
| 210 | #endif | 219 | #endif |
| 211 | |||
| 212 | if (type == SOCK_STREAM) | ||
| 213 | s=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL); | ||
| 214 | else /* type == SOCK_DGRAM */ | ||
| 215 | s=socket(AF_INET, SOCK_DGRAM,IPPROTO_UDP); | ||
| 216 | 220 | ||
| 217 | if (s == -1) goto err; | 221 | if (type == SOCK_STREAM) |
| 222 | s = socket(AF_INET, SOCK_STREAM, SOCKET_PROTOCOL); | ||
| 223 | else /* type == SOCK_DGRAM */ | ||
| 224 | s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); | ||
| 225 | |||
| 226 | if (s == -1) | ||
| 227 | goto err; | ||
| 218 | #if defined SOL_SOCKET && defined SO_REUSEADDR | 228 | #if defined SOL_SOCKET && defined SO_REUSEADDR |
| 219 | { | 229 | { |
| 220 | int j = 1; | 230 | int j = 1; |
| 221 | setsockopt(s, SOL_SOCKET, SO_REUSEADDR, | 231 | setsockopt(s, SOL_SOCKET, SO_REUSEADDR, |
| 222 | (void *) &j, sizeof j); | 232 | (void *) &j, sizeof j); |
| 223 | } | 233 | } |
| 224 | #endif | 234 | #endif |
| 225 | if (bind(s,(struct sockaddr *)&server,sizeof(server)) == -1) | 235 | if (bind(s, (struct sockaddr *)&server, sizeof(server)) == -1) { |
| 226 | { | ||
| 227 | perror("bind"); | 236 | perror("bind"); |
| 228 | goto err; | 237 | goto err; |
| 229 | } | 238 | } |
| 230 | /* Make it 128 for linux */ | 239 | /* Make it 128 for linux */ |
| 231 | if (type==SOCK_STREAM && listen(s,128) == -1) goto err; | 240 | if (type == SOCK_STREAM && listen(s, 128) == -1) |
| 232 | *sock=s; | 241 | goto err; |
| 233 | ret=1; | 242 | *sock = s; |
| 243 | ret = 1; | ||
| 234 | err: | 244 | err: |
| 235 | if ((ret == 0) && (s != -1)) | 245 | if ((ret == 0) && (s != -1)) { |
| 236 | { | ||
| 237 | shutdown(s, SHUT_RD); | 246 | shutdown(s, SHUT_RD); |
| 238 | close(s); | 247 | close(s); |
| 239 | } | ||
| 240 | return(ret); | ||
| 241 | } | 248 | } |
| 242 | 249 | return (ret); | |
| 243 | static int init_server(int *sock, int port, int type) | 250 | } |
| 244 | { | 251 | |
| 245 | return(init_server_long(sock, port, NULL, type)); | 252 | static int |
| 246 | } | 253 | init_server(int *sock, int port, int type) |
| 247 | 254 | { | |
| 248 | static int do_accept(int acc_sock, int *sock, char **host) | 255 | return (init_server_long(sock, port, NULL, type)); |
| 249 | { | 256 | } |
| 257 | |||
| 258 | static int | ||
| 259 | do_accept(int acc_sock, int *sock, char **host) | ||
| 260 | { | ||
| 250 | int ret; | 261 | int ret; |
| 251 | struct hostent *h1,*h2; | 262 | struct hostent *h1, *h2; |
| 252 | static struct sockaddr_in from; | 263 | static struct sockaddr_in from; |
| 253 | int len; | 264 | int len; |
| 254 | /* struct linger ling; */ | 265 | /* struct linger ling; */ |
| 255 | 266 | ||
| 256 | if (!ssl_sock_init()) return(0); | 267 | if (!ssl_sock_init()) |
| 268 | return (0); | ||
| 257 | 269 | ||
| 258 | redoit: | 270 | redoit: |
| 259 | 271 | ||
| 260 | memset((char *)&from,0,sizeof(from)); | 272 | memset((char *)&from, 0, sizeof(from)); |
| 261 | len=sizeof(from); | 273 | len = sizeof(from); |
| 262 | /* Note: under VMS with SOCKETSHR the fourth parameter is currently | 274 | /* Note: under VMS with SOCKETSHR the fourth parameter is currently |
| 263 | * of type (int *) whereas under other systems it is (void *) if | 275 | * of type (int *) whereas under other systems it is (void *) if |
| 264 | * you don't have a cast it will choke the compiler: if you do | 276 | * you don't have a cast it will choke the compiler: if you do |
| 265 | * have a cast then you can either go for (int *) or (void *). | 277 | * have a cast then you can either go for (int *) or (void *). |
| 266 | */ | 278 | */ |
| 267 | ret=accept(acc_sock,(struct sockaddr *)&from,(void *)&len); | 279 | ret = accept(acc_sock, (struct sockaddr *)&from, (void *)&len); |
| 268 | if (ret == -1) | 280 | if (ret == -1) { |
| 269 | { | 281 | if (errno == EINTR) { |
| 270 | if (errno == EINTR) | ||
| 271 | { | ||
| 272 | /*check_timeout(); */ | 282 | /*check_timeout(); */ |
| 273 | goto redoit; | 283 | goto redoit; |
| 274 | } | 284 | } |
| 275 | fprintf(stderr,"errno=%d ",errno); | 285 | fprintf(stderr,"errno=%d ",errno); |
| 276 | perror("accept"); | 286 | perror("accept"); |
| 277 | return(0); | 287 | return (0); |
| 278 | } | 288 | } |
| 279 | 289 | ||
| 280 | /* | 290 | /* |
| 281 | ling.l_onoff=1; | 291 | ling.l_onoff=1; |
| @@ -287,144 +297,136 @@ redoit: | |||
| 287 | if (i < 0) { perror("keepalive"); return(0); } | 297 | if (i < 0) { perror("keepalive"); return(0); } |
| 288 | */ | 298 | */ |
| 289 | 299 | ||
| 290 | if (host == NULL) goto end; | 300 | if (host == NULL) |
| 301 | goto end; | ||
| 291 | #ifndef BIT_FIELD_LIMITS | 302 | #ifndef BIT_FIELD_LIMITS |
| 292 | /* I should use WSAAsyncGetHostByName() under windows */ | 303 | /* I should use WSAAsyncGetHostByName() under windows */ |
| 293 | h1=gethostbyaddr((char *)&from.sin_addr.s_addr, | 304 | h1 = gethostbyaddr((char *)&from.sin_addr.s_addr, |
| 294 | sizeof(from.sin_addr.s_addr),AF_INET); | 305 | sizeof(from.sin_addr.s_addr), AF_INET); |
| 295 | #else | 306 | #else |
| 296 | h1=gethostbyaddr((char *)&from.sin_addr, | 307 | h1 = gethostbyaddr((char *)&from.sin_addr, |
| 297 | sizeof(struct in_addr),AF_INET); | 308 | sizeof(struct in_addr), AF_INET); |
| 298 | #endif | 309 | #endif |
| 299 | if (h1 == NULL) | 310 | if (h1 == NULL) { |
| 300 | { | 311 | BIO_printf(bio_err, "bad gethostbyaddr\n"); |
| 301 | BIO_printf(bio_err,"bad gethostbyaddr\n"); | 312 | *host = NULL; |
| 302 | *host=NULL; | ||
| 303 | /* return(0); */ | 313 | /* return(0); */ |
| 304 | } | 314 | } else { |
| 305 | else | 315 | if ((*host = (char *)malloc(strlen(h1->h_name) + 1)) == NULL) { |
| 306 | { | ||
| 307 | if ((*host=(char *)malloc(strlen(h1->h_name)+1)) == NULL) | ||
| 308 | { | ||
| 309 | perror("malloc"); | 316 | perror("malloc"); |
| 310 | return(0); | 317 | return (0); |
| 311 | } | 318 | } |
| 312 | BUF_strlcpy(*host,h1->h_name,strlen(h1->h_name)+1); | 319 | BUF_strlcpy(*host, h1->h_name, strlen(h1->h_name) + 1); |
| 313 | 320 | ||
| 314 | h2=GetHostByName(*host); | 321 | h2 = GetHostByName(*host); |
| 315 | if (h2 == NULL) | 322 | if (h2 == NULL) { |
| 316 | { | 323 | BIO_printf(bio_err, "gethostbyname failure\n"); |
| 317 | BIO_printf(bio_err,"gethostbyname failure\n"); | 324 | return (0); |
| 318 | return(0); | 325 | } |
| 319 | } | 326 | if (h2->h_addrtype != AF_INET) { |
| 320 | if (h2->h_addrtype != AF_INET) | 327 | BIO_printf(bio_err, "gethostbyname addr is not AF_INET\n"); |
| 321 | { | 328 | return (0); |
| 322 | BIO_printf(bio_err,"gethostbyname addr is not AF_INET\n"); | ||
| 323 | return(0); | ||
| 324 | } | ||
| 325 | } | 329 | } |
| 326 | end: | ||
| 327 | *sock=ret; | ||
| 328 | return(1); | ||
| 329 | } | 330 | } |
| 330 | 331 | ||
| 331 | int extract_host_port(char *str, char **host_ptr, unsigned char *ip, | 332 | end: |
| 332 | char **port_ptr) | 333 | *sock = ret; |
| 333 | { | 334 | return (1); |
| 334 | char *h,*p; | 335 | } |
| 335 | 336 | ||
| 336 | h=str; | 337 | int |
| 337 | p=strrchr(str,'/'); /* IPv6 host/port */ | 338 | extract_host_port(char *str, char **host_ptr, unsigned char *ip, |
| 338 | if (p == NULL) { p=strrchr(str,':'); } | 339 | char **port_ptr) |
| 339 | if (p == NULL) | 340 | { |
| 340 | { | 341 | char *h, *p; |
| 341 | BIO_printf(bio_err,"no port defined\n"); | 342 | |
| 342 | return(0); | 343 | h = str; |
| 343 | } | 344 | p = strrchr(str, '/'); /* IPv6 host/port */ |
| 344 | *(p++)='\0'; | 345 | if (p == NULL) { |
| 346 | p = strrchr(str, ':'); | ||
| 347 | } | ||
| 348 | if (p == NULL) { | ||
| 349 | BIO_printf(bio_err, "no port defined\n"); | ||
| 350 | return (0); | ||
| 351 | } | ||
| 352 | *(p++) = '\0'; | ||
| 345 | 353 | ||
| 346 | if (host_ptr != NULL) *host_ptr=h; | 354 | if (host_ptr != NULL) |
| 355 | *host_ptr = h; | ||
| 347 | 356 | ||
| 348 | if (port_ptr != NULL && p != NULL && *p != '\0') | 357 | if (port_ptr != NULL && p != NULL && *p != '\0') |
| 349 | *port_ptr = p; | 358 | *port_ptr = p; |
| 350 | 359 | ||
| 351 | return(1); | 360 | return (1); |
| 352 | err: | 361 | err: |
| 353 | return(0); | 362 | return (0); |
| 354 | } | 363 | } |
| 355 | 364 | ||
| 356 | int extract_port(char *str, short *port_ptr) | 365 | int |
| 357 | { | 366 | extract_port(char *str, short *port_ptr) |
| 367 | { | ||
| 358 | int i; | 368 | int i; |
| 359 | struct servent *s; | 369 | struct servent *s; |
| 360 | 370 | ||
| 361 | i=atoi(str); | 371 | i = atoi(str); |
| 362 | if (i != 0) | 372 | if (i != 0) |
| 363 | *port_ptr=(unsigned short)i; | 373 | *port_ptr = (unsigned short)i; |
| 364 | else | 374 | else { |
| 365 | { | 375 | s = getservbyname(str, "tcp"); |
| 366 | s=getservbyname(str,"tcp"); | 376 | if (s == NULL) { |
| 367 | if (s == NULL) | 377 | BIO_printf(bio_err, "getservbyname failure for %s\n", str); |
| 368 | { | 378 | return (0); |
| 369 | BIO_printf(bio_err,"getservbyname failure for %s\n",str); | ||
| 370 | return(0); | ||
| 371 | } | ||
| 372 | *port_ptr=ntohs((unsigned short)s->s_port); | ||
| 373 | } | 379 | } |
| 374 | return(1); | 380 | *port_ptr = ntohs((unsigned short)s->s_port); |
| 375 | } | 381 | } |
| 382 | return (1); | ||
| 383 | } | ||
| 376 | 384 | ||
| 377 | #define GHBN_NUM 4 | 385 | #define GHBN_NUM 4 |
| 378 | static struct ghbn_cache_st | 386 | static struct ghbn_cache_st { |
| 379 | { | ||
| 380 | char name[128]; | 387 | char name[128]; |
| 381 | struct hostent ent; | 388 | struct hostent ent; |
| 382 | unsigned long order; | 389 | unsigned long order; |
| 383 | } ghbn_cache[GHBN_NUM]; | 390 | } ghbn_cache[GHBN_NUM]; |
| 384 | 391 | ||
| 385 | static unsigned long ghbn_hits=0L; | 392 | static unsigned long ghbn_hits = 0L; |
| 386 | static unsigned long ghbn_miss=0L; | 393 | static unsigned long ghbn_miss = 0L; |
| 387 | 394 | ||
| 388 | static struct hostent *GetHostByName(char *name) | 395 | static struct hostent * |
| 389 | { | 396 | GetHostByName(char *name) { |
| 390 | struct hostent *ret; | 397 | struct hostent *ret; |
| 391 | int i,lowi=0; | 398 | int i, lowi = 0; |
| 392 | unsigned long low= (unsigned long)-1; | 399 | unsigned long low = (unsigned long) - 1; |
| 393 | 400 | ||
| 394 | for (i=0; i<GHBN_NUM; i++) | 401 | for (i = 0; i < GHBN_NUM; i++) { |
| 395 | { | 402 | if (low > ghbn_cache[i].order) { |
| 396 | if (low > ghbn_cache[i].order) | 403 | low = ghbn_cache[i].order; |
| 397 | { | 404 | lowi = i; |
| 398 | low=ghbn_cache[i].order; | 405 | } |
| 399 | lowi=i; | 406 | if (ghbn_cache[i].order > 0) { |
| 400 | } | 407 | if (strncmp(name, ghbn_cache[i].name, 128) == 0) |
| 401 | if (ghbn_cache[i].order > 0) | ||
| 402 | { | ||
| 403 | if (strncmp(name,ghbn_cache[i].name,128) == 0) | ||
| 404 | break; | 408 | break; |
| 405 | } | ||
| 406 | } | 409 | } |
| 410 | } | ||
| 407 | if (i == GHBN_NUM) /* no hit*/ | 411 | if (i == GHBN_NUM) /* no hit*/ |
| 408 | { | 412 | { |
| 409 | ghbn_miss++; | 413 | ghbn_miss++; |
| 410 | ret=gethostbyname(name); | 414 | ret = gethostbyname(name); |
| 411 | if (ret == NULL) return(NULL); | 415 | if (ret == NULL) |
| 416 | return (NULL); | ||
| 412 | /* else add to cache */ | 417 | /* else add to cache */ |
| 413 | if(strlen(name) < sizeof ghbn_cache[0].name) | 418 | if (strlen(name) < sizeof ghbn_cache[0].name) { |
| 414 | { | 419 | strlcpy(ghbn_cache[lowi].name, name, sizeof(ghbn_cache[0].name)); |
| 415 | strlcpy(ghbn_cache[lowi].name,name, sizeof(ghbn_cache[0].name)); | 420 | memcpy((char *)&(ghbn_cache[lowi].ent), ret, sizeof(struct hostent)); |
| 416 | memcpy((char *)&(ghbn_cache[lowi].ent),ret,sizeof(struct hostent)); | 421 | ghbn_cache[lowi].order = ghbn_miss + ghbn_hits; |
| 417 | ghbn_cache[lowi].order=ghbn_miss+ghbn_hits; | ||
| 418 | } | ||
| 419 | return(ret); | ||
| 420 | } | 422 | } |
| 421 | else | 423 | return (ret); |
| 422 | { | 424 | } else { |
| 423 | ghbn_hits++; | 425 | ghbn_hits++; |
| 424 | ret= &(ghbn_cache[i].ent); | 426 | ret = &(ghbn_cache[i].ent); |
| 425 | ghbn_cache[i].order=ghbn_miss+ghbn_hits; | 427 | ghbn_cache[i].order = ghbn_miss + ghbn_hits; |
| 426 | return(ret); | 428 | return (ret); |
| 427 | } | ||
| 428 | } | 429 | } |
| 430 | } | ||
| 429 | 431 | ||
| 430 | #endif | 432 | #endif |
