| OLD | NEW |
| 1 /* apps/s_socket.c - socket-related functions used by s_client and s_server */ | 1 /* apps/s_socket.c - socket-related functions used by s_client and s_server */ |
| 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 3 * All rights reserved. | 3 * All rights reserved. |
| 4 * | 4 * |
| 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 |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 return(0); | 230 return(0); |
| 231 } | 231 } |
| 232 } | 232 } |
| 233 #endif /* OPENSSL_SYS_WINDOWS */ | 233 #endif /* OPENSSL_SYS_WINDOWS */ |
| 234 return(1); | 234 return(1); |
| 235 } | 235 } |
| 236 | 236 |
| 237 int init_client(int *sock, char *host, int port, int type) | 237 int init_client(int *sock, char *host, int port, int type) |
| 238 { | 238 { |
| 239 unsigned char ip[4]; | 239 unsigned char ip[4]; |
| 240 short p=0; | |
| 241 | 240 |
| 242 if (!host_ip(host,&(ip[0]))) | 241 if (!host_ip(host,&(ip[0]))) |
| 243 { | 242 { |
| 244 return(0); | 243 return(0); |
| 245 } | 244 } |
| 246 if (p != 0) port=p; | |
| 247 return(init_client_ip(sock,ip,port,type)); | 245 return(init_client_ip(sock,ip,port,type)); |
| 248 } | 246 } |
| 249 | 247 |
| 250 static int init_client_ip(int *sock, unsigned char ip[4], int port, int type) | 248 static int init_client_ip(int *sock, unsigned char ip[4], int port, int type) |
| 251 { | 249 { |
| 252 unsigned long addr; | 250 unsigned long addr; |
| 253 struct sockaddr_in them; | 251 struct sockaddr_in them; |
| 254 int s,i; | 252 int s,i; |
| 255 | 253 |
| 256 if (!ssl_sock_init()) return(0); | 254 if (!ssl_sock_init()) return(0); |
| 257 | 255 |
| 258 memset((char *)&them,0,sizeof(them)); | 256 memset((char *)&them,0,sizeof(them)); |
| 259 them.sin_family=AF_INET; | 257 them.sin_family=AF_INET; |
| 260 them.sin_port=htons((unsigned short)port); | 258 them.sin_port=htons((unsigned short)port); |
| 261 addr=(unsigned long) | 259 addr=(unsigned long) |
| 262 ((unsigned long)ip[0]<<24L)| | 260 ((unsigned long)ip[0]<<24L)| |
| 263 ((unsigned long)ip[1]<<16L)| | 261 ((unsigned long)ip[1]<<16L)| |
| 264 ((unsigned long)ip[2]<< 8L)| | 262 ((unsigned long)ip[2]<< 8L)| |
| 265 ((unsigned long)ip[3]); | 263 ((unsigned long)ip[3]); |
| 266 them.sin_addr.s_addr=htonl(addr); | 264 them.sin_addr.s_addr=htonl(addr); |
| 267 | 265 |
| 268 if (type == SOCK_STREAM) | 266 if (type == SOCK_STREAM) |
| 269 s=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL); | 267 s=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL); |
| 270 else /* ( type == SOCK_DGRAM) */ | 268 else /* ( type == SOCK_DGRAM) */ |
| 271 s=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); | 269 s=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); |
| 272 | 270 |
| 273 if (s == INVALID_SOCKET) { perror("socket"); return(0); } | 271 if (s == INVALID_SOCKET) { perror("socket"); return(0); } |
| 274 | 272 |
| 275 #ifndef OPENSSL_SYS_MPE | 273 #if defined(SO_KEEPALIVE) && !defined(OPENSSL_SYS_MPE) |
| 276 if (type == SOCK_STREAM) | 274 if (type == SOCK_STREAM) |
| 277 { | 275 { |
| 278 i=0; | 276 i=0; |
| 279 i=setsockopt(s,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i)); | 277 i=setsockopt(s,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i)); |
| 280 if (i < 0) { perror("keepalive"); return(0); } | 278 if (i < 0) { perror("keepalive"); return(0); } |
| 281 } | 279 } |
| 282 #endif | 280 #endif |
| 283 | 281 |
| 284 if (connect(s,(struct sockaddr *)&them,sizeof(them)) == -1) | 282 if (connect(s,(struct sockaddr *)&them,sizeof(them)) == -1) |
| 285 » » { close(s); perror("connect"); return(0); } | 283 » » { closesocket(s); perror("connect"); return(0); } |
| 286 *sock=s; | 284 *sock=s; |
| 287 return(1); | 285 return(1); |
| 288 } | 286 } |
| 289 | 287 |
| 290 int do_server(int port, int type, int *ret, int (*cb)(char *hostname, int s, uns
igned char *context), unsigned char *context) | 288 int do_server(int port, int type, int *ret, int (*cb)(char *hostname, int s, uns
igned char *context), unsigned char *context) |
| 291 { | 289 { |
| 292 int sock; | 290 int sock; |
| 293 char *name = NULL; | 291 char *name = NULL; |
| 294 » int accept_socket; | 292 » int accept_socket = 0; |
| 295 int i; | 293 int i; |
| 296 | 294 |
| 297 if (!init_server(&accept_socket,port,type)) return(0); | 295 if (!init_server(&accept_socket,port,type)) return(0); |
| 298 | 296 |
| 299 if (ret != NULL) | 297 if (ret != NULL) |
| 300 { | 298 { |
| 301 *ret=accept_socket; | 299 *ret=accept_socket; |
| 302 /* return(1);*/ | 300 /* return(1);*/ |
| 303 } | 301 } |
| 304 for (;;) | 302 for (;;) |
| (...skipping 17 matching lines...) Expand all Loading... |
| 322 SHUTDOWN2(accept_socket); | 320 SHUTDOWN2(accept_socket); |
| 323 return(i); | 321 return(i); |
| 324 } | 322 } |
| 325 } | 323 } |
| 326 } | 324 } |
| 327 | 325 |
| 328 static int init_server_long(int *sock, int port, char *ip, int type) | 326 static int init_server_long(int *sock, int port, char *ip, int type) |
| 329 { | 327 { |
| 330 int ret=0; | 328 int ret=0; |
| 331 struct sockaddr_in server; | 329 struct sockaddr_in server; |
| 332 » int s= -1,i; | 330 » int s= -1; |
| 333 | 331 |
| 334 if (!ssl_sock_init()) return(0); | 332 if (!ssl_sock_init()) return(0); |
| 335 | 333 |
| 336 memset((char *)&server,0,sizeof(server)); | 334 memset((char *)&server,0,sizeof(server)); |
| 337 server.sin_family=AF_INET; | 335 server.sin_family=AF_INET; |
| 338 server.sin_port=htons((unsigned short)port); | 336 server.sin_port=htons((unsigned short)port); |
| 339 if (ip == NULL) | 337 if (ip == NULL) |
| 340 server.sin_addr.s_addr=INADDR_ANY; | 338 server.sin_addr.s_addr=INADDR_ANY; |
| 341 else | 339 else |
| 342 /* Added for T3E, address-of fails on bit field (beckman@acl.lanl.gov) */ | 340 /* Added for T3E, address-of fails on bit field (beckman@acl.lanl.gov) */ |
| (...skipping 18 matching lines...) Expand all Loading... |
| 361 #endif | 359 #endif |
| 362 if (bind(s,(struct sockaddr *)&server,sizeof(server)) == -1) | 360 if (bind(s,(struct sockaddr *)&server,sizeof(server)) == -1) |
| 363 { | 361 { |
| 364 #ifndef OPENSSL_SYS_WINDOWS | 362 #ifndef OPENSSL_SYS_WINDOWS |
| 365 perror("bind"); | 363 perror("bind"); |
| 366 #endif | 364 #endif |
| 367 goto err; | 365 goto err; |
| 368 } | 366 } |
| 369 /* Make it 128 for linux */ | 367 /* Make it 128 for linux */ |
| 370 if (type==SOCK_STREAM && listen(s,128) == -1) goto err; | 368 if (type==SOCK_STREAM && listen(s,128) == -1) goto err; |
| 371 i=0; | |
| 372 *sock=s; | 369 *sock=s; |
| 373 ret=1; | 370 ret=1; |
| 374 err: | 371 err: |
| 375 if ((ret == 0) && (s != -1)) | 372 if ((ret == 0) && (s != -1)) |
| 376 { | 373 { |
| 377 SHUTDOWN(s); | 374 SHUTDOWN(s); |
| 378 } | 375 } |
| 379 return(ret); | 376 return(ret); |
| 380 } | 377 } |
| 381 | 378 |
| 382 static int init_server(int *sock, int port, int type) | 379 static int init_server(int *sock, int port, int type) |
| 383 { | 380 { |
| 384 return(init_server_long(sock, port, NULL, type)); | 381 return(init_server_long(sock, port, NULL, type)); |
| 385 } | 382 } |
| 386 | 383 |
| 387 static int do_accept(int acc_sock, int *sock, char **host) | 384 static int do_accept(int acc_sock, int *sock, char **host) |
| 388 { | 385 { |
| 389 » int ret,i; | 386 » int ret; |
| 390 struct hostent *h1,*h2; | 387 struct hostent *h1,*h2; |
| 391 static struct sockaddr_in from; | 388 static struct sockaddr_in from; |
| 392 int len; | 389 int len; |
| 393 /* struct linger ling; */ | 390 /* struct linger ling; */ |
| 394 | 391 |
| 395 if (!ssl_sock_init()) return(0); | 392 if (!ssl_sock_init()) return(0); |
| 396 | 393 |
| 397 #ifndef OPENSSL_SYS_WINDOWS | 394 #ifndef OPENSSL_SYS_WINDOWS |
| 398 redoit: | 395 redoit: |
| 399 #endif | 396 #endif |
| 400 | 397 |
| 401 memset((char *)&from,0,sizeof(from)); | 398 memset((char *)&from,0,sizeof(from)); |
| 402 len=sizeof(from); | 399 len=sizeof(from); |
| 403 /* Note: under VMS with SOCKETSHR the fourth parameter is currently | 400 /* Note: under VMS with SOCKETSHR the fourth parameter is currently |
| 404 * of type (int *) whereas under other systems it is (void *) if | 401 * of type (int *) whereas under other systems it is (void *) if |
| 405 * you don't have a cast it will choke the compiler: if you do | 402 * you don't have a cast it will choke the compiler: if you do |
| 406 * have a cast then you can either go for (int *) or (void *). | 403 * have a cast then you can either go for (int *) or (void *). |
| 407 */ | 404 */ |
| 408 ret=accept(acc_sock,(struct sockaddr *)&from,(void *)&len); | 405 ret=accept(acc_sock,(struct sockaddr *)&from,(void *)&len); |
| 409 if (ret == INVALID_SOCKET) | 406 if (ret == INVALID_SOCKET) |
| 410 { | 407 { |
| 411 #if defined(OPENSSL_SYS_WINDOWS) || (defined(OPENSSL_SYS_NETWARE) && !defined(NE
TWARE_BSDSOCK)) | 408 #if defined(OPENSSL_SYS_WINDOWS) || (defined(OPENSSL_SYS_NETWARE) && !defined(NE
TWARE_BSDSOCK)) |
| 409 int i; |
| 412 i=WSAGetLastError(); | 410 i=WSAGetLastError(); |
| 413 BIO_printf(bio_err,"accept error %d\n",i); | 411 BIO_printf(bio_err,"accept error %d\n",i); |
| 414 #else | 412 #else |
| 415 if (errno == EINTR) | 413 if (errno == EINTR) |
| 416 { | 414 { |
| 417 /*check_timeout(); */ | 415 /*check_timeout(); */ |
| 418 goto redoit; | 416 goto redoit; |
| 419 } | 417 } |
| 420 fprintf(stderr,"errno=%d ",errno); | 418 fprintf(stderr,"errno=%d ",errno); |
| 421 perror("accept"); | 419 perror("accept"); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 456 return(0); | 454 return(0); |
| 457 } | 455 } |
| 458 BUF_strlcpy(*host,h1->h_name,strlen(h1->h_name)+1); | 456 BUF_strlcpy(*host,h1->h_name,strlen(h1->h_name)+1); |
| 459 | 457 |
| 460 h2=GetHostByName(*host); | 458 h2=GetHostByName(*host); |
| 461 if (h2 == NULL) | 459 if (h2 == NULL) |
| 462 { | 460 { |
| 463 BIO_printf(bio_err,"gethostbyname failure\n"); | 461 BIO_printf(bio_err,"gethostbyname failure\n"); |
| 464 return(0); | 462 return(0); |
| 465 } | 463 } |
| 466 i=0; | |
| 467 if (h2->h_addrtype != AF_INET) | 464 if (h2->h_addrtype != AF_INET) |
| 468 { | 465 { |
| 469 BIO_printf(bio_err,"gethostbyname addr is not AF_INET\n"
); | 466 BIO_printf(bio_err,"gethostbyname addr is not AF_INET\n"
); |
| 470 return(0); | 467 return(0); |
| 471 } | 468 } |
| 472 } | 469 } |
| 473 end: | 470 end: |
| 474 *sock=ret; | 471 *sock=ret; |
| 475 return(1); | 472 return(1); |
| 476 } | 473 } |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 614 else | 611 else |
| 615 { | 612 { |
| 616 ghbn_hits++; | 613 ghbn_hits++; |
| 617 ret= &(ghbn_cache[i].ent); | 614 ret= &(ghbn_cache[i].ent); |
| 618 ghbn_cache[i].order=ghbn_miss+ghbn_hits; | 615 ghbn_cache[i].order=ghbn_miss+ghbn_hits; |
| 619 return(ret); | 616 return(ret); |
| 620 } | 617 } |
| 621 } | 618 } |
| 622 | 619 |
| 623 #endif | 620 #endif |
| OLD | NEW |