X-Git-Url: https://git.openssl.org/?a=blobdiff_plain;f=apps%2Fs_socket.c;h=9d1f04a6c575cfb7bf866bd166764ae97b1206eb;hb=b91dd150d2b9b5ddca37722e7f52ea59ba7f80da;hp=ea64dbdc6a4948fc8b738a68a269b81263d23473;hpb=0f113f3ee4d629ef9a4a30911b22b224772085e5;p=openssl.git diff --git a/apps/s_socket.c b/apps/s_socket.c index ea64dbdc6a..9d1f04a6c5 100644 --- a/apps/s_socket.c +++ b/apps/s_socket.c @@ -1,6 +1,3 @@ -/* - * apps/s_socket.c - socket-related functions used by s_client and s_server - */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -57,7 +54,56 @@ * copied and put under another distribution licence * [including the GNU Public Licence.] */ +/* ==================================================================== + * Copyright (c) 199-2015 The OpenSSL Project. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. All advertising materials mentioning features or use of this + * software must display the following acknowledgment: + * "This product includes software developed by the OpenSSL Project + * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" + * + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to + * endorse or promote products derived from this software without + * prior written permission. For written permission, please contact + * licensing@OpenSSL.org. + * + * 5. Products derived from this software may not be called "OpenSSL" + * nor may "OpenSSL" appear in their names without prior written + * permission of the OpenSSL Project. + * + * 6. Redistributions of any form whatsoever must retain the following + * acknowledgment: + * "This product includes software developed by the OpenSSL Project + * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" + * + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * ==================================================================== + */ +/* socket-related functions used by s_client and s_server */ #include #include #include @@ -76,10 +122,8 @@ typedef unsigned int u_int; #endif #define USE_SOCKETS -#define NON_MAIN #include "apps.h" #undef USE_SOCKETS -#undef NON_MAIN #include "s_apps.h" #include @@ -95,7 +139,6 @@ typedef unsigned int u_int; # include "netdb.h" # endif -static struct hostent *GetHostByName(const char *name); # if defined(OPENSSL_SYS_WINDOWS) || (defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)) static void ssl_sock_cleanup(void); # endif @@ -185,7 +228,7 @@ static int ssl_sock_init(void) return (0); } } -# endif /* OPENSSL_SYS_WINDOWS */ +# endif return (1); } @@ -209,7 +252,7 @@ static int init_client_ip(int *sock, const unsigned char ip[4], int port, if (!ssl_sock_init()) return (0); - memset((char *)&them, 0, sizeof(them)); + memset(&them, 0, sizeof(them)); them.sin_family = AF_INET; them.sin_port = htons((unsigned short)port); addr = (unsigned long) @@ -223,7 +266,7 @@ static int init_client_ip(int *sock, const unsigned char ip[4], int port, else /* ( type == SOCK_DGRAM) */ s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - if (s == INVALID_SOCKET) { + if (s == (int)INVALID_SOCKET) { perror("socket"); return (0); } @@ -260,12 +303,12 @@ int init_client_unix(int *sock, const char *server) return (0); s = socket(AF_UNIX, SOCK_STREAM, 0); - if (s == INVALID_SOCKET) { + if (s == (int)INVALID_SOCKET) { perror("socket"); return (0); } - memset((char *)&them, 0, sizeof(them)); + memset(&them, 0, sizeof(them)); them.sun_family = AF_UNIX; strcpy(them.sun_path, server); @@ -310,8 +353,7 @@ int do_server(int port, int type, int *ret, } else sock = accept_socket; i = (*cb) (name, sock, type, context); - if (name != NULL) - OPENSSL_free(name); + OPENSSL_free(name); if (type == SOCK_STREAM) SHUTDOWN2(sock); if (naccept != -1) @@ -368,7 +410,7 @@ static int init_server_long(int *sock, int port, char *ip, int type) if (!ssl_sock_init()) return (0); - memset((char *)&server, 0, sizeof(server)); + memset(&server, 0, sizeof(server)); server.sin_family = AF_INET; server.sin_port = htons((unsigned short)port); if (ip == NULL) @@ -386,7 +428,7 @@ static int init_server_long(int *sock, int port, char *ip, int type) else /* type == SOCK_DGRAM */ s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - if (s == INVALID_SOCKET) + if (s == (int)INVALID_SOCKET) goto err; # if defined SOL_SOCKET && defined SO_REUSEADDR { @@ -430,10 +472,10 @@ static int init_server_unix(int *sock, const char *path) return (0); s = socket(AF_UNIX, SOCK_STREAM, 0); - if (s == INVALID_SOCKET) + if (s == (int)INVALID_SOCKET) goto err; - memset((char *)&server, 0, sizeof(server)); + memset(&server, 0, sizeof(server)); server.sun_family = AF_UNIX; strcpy(server.sun_path, path); @@ -476,7 +518,7 @@ static int do_accept(int acc_sock, int *sock, char **host) redoit: # endif - memset((char *)&from, 0, sizeof(from)); + memset(&from, 0, sizeof(from)); len = sizeof(from); /* * Note: under VMS with SOCKETSHR the fourth parameter is currently of @@ -485,7 +527,7 @@ static int do_accept(int acc_sock, int *sock, char **host) * can either go for (int *) or (void *). */ ret = accept(acc_sock, (struct sockaddr *)&from, (void *)&len); - if (ret == INVALID_SOCKET) { + if (ret == (int)INVALID_SOCKET) { # if defined(OPENSSL_SYS_WINDOWS) || (defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)) int i; i = WSAGetLastError(); @@ -497,22 +539,11 @@ static int do_accept(int acc_sock, int *sock, char **host) */ goto redoit; } - fprintf(stderr, "errno=%d ", errno); - perror("accept"); + BIO_printf(bio_err, "accept errno=%d, %s\n", errno, strerror(errno)); # endif return (0); } -/*- - ling.l_onoff=1; - ling.l_linger=0; - i=setsockopt(ret,SOL_SOCKET,SO_LINGER,(char *)&ling,sizeof(ling)); - if (i < 0) { perror("linger"); return(0); } - i=0; - i=setsockopt(ret,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i)); - if (i < 0) { perror("keepalive"); return(0); } -*/ - if (host == NULL) goto end; # ifndef BIT_FIELD_LIMITS @@ -528,14 +559,10 @@ static int do_accept(int acc_sock, int *sock, char **host) *host = NULL; /* return(0); */ } else { - if ((*host = (char *)OPENSSL_malloc(strlen(h1->h_name) + 1)) == NULL) { - perror("OPENSSL_malloc"); - closesocket(ret); - return (0); - } + *host = app_malloc(strlen(h1->h_name) + 1, "copy hostname"); BUF_strlcpy(*host, h1->h_name, strlen(h1->h_name) + 1); - h2 = GetHostByName(*host); + h2 = gethostbyname(*host); if (h2 == NULL) { BIO_printf(bio_err, "gethostbyname failure\n"); closesocket(ret); @@ -562,15 +589,14 @@ static int do_accept_unix(int acc_sock, int *sock) redoit: ret = accept(acc_sock, NULL, NULL); - if (ret == INVALID_SOCKET) { + if (ret == (int)INVALID_SOCKET) { if (errno == EINTR) { /* * check_timeout(); */ goto redoit; } - fprintf(stderr, "errno=%d ", errno); - perror("accept"); + BIO_printf(bio_err, "accept errno=%d, %s\n", errno, strerror(errno)); return (0); } @@ -580,7 +606,7 @@ static int do_accept_unix(int acc_sock, int *sock) # endif int extract_host_port(char *str, char **host_ptr, unsigned char *ip, - short *port_ptr) + unsigned short *port_ptr) { char *h, *p; @@ -626,7 +652,7 @@ static int host_ip(const char *str, unsigned char ip[4]) if (!ssl_sock_init()) return (0); - he = GetHostByName(str); + he = gethostbyname(str); if (he == NULL) { BIO_printf(bio_err, "gethostbyname failure\n"); goto err; @@ -645,7 +671,7 @@ static int host_ip(const char *str, unsigned char ip[4]) return (0); } -int extract_port(const char *str, short *port_ptr) +int extract_port(const char *str, unsigned short *port_ptr) { int i; struct servent *s; @@ -664,51 +690,4 @@ int extract_port(const char *str, short *port_ptr) return (1); } -# define GHBN_NUM 4 -static struct ghbn_cache_st { - char name[128]; - struct hostent ent; - unsigned long order; -} ghbn_cache[GHBN_NUM]; - -static unsigned long ghbn_hits = 0L; -static unsigned long ghbn_miss = 0L; - -static struct hostent *GetHostByName(const char *name) -{ - struct hostent *ret; - int i, lowi = 0; - unsigned long low = (unsigned long)-1; - - for (i = 0; i < GHBN_NUM; i++) { - if (low > ghbn_cache[i].order) { - low = ghbn_cache[i].order; - lowi = i; - } - if (ghbn_cache[i].order > 0) { - if (strncmp(name, ghbn_cache[i].name, 128) == 0) - break; - } - } - if (i == GHBN_NUM) { /* no hit */ - ghbn_miss++; - ret = gethostbyname(name); - if (ret == NULL) - return (NULL); - /* else add to cache */ - if (strlen(name) < sizeof ghbn_cache[0].name) { - strcpy(ghbn_cache[lowi].name, name); - memcpy((char *)&(ghbn_cache[lowi].ent), ret, - sizeof(struct hostent)); - ghbn_cache[lowi].order = ghbn_miss + ghbn_hits; - } - return (ret); - } else { - ghbn_hits++; - ret = &(ghbn_cache[i].ent); - ghbn_cache[i].order = ghbn_miss + ghbn_hits; - return (ret); - } -} - #endif