-/*
- * 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.
*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#endif
#define USE_SOCKETS
-#define NON_MAIN
#include "apps.h"
#undef USE_SOCKETS
-#undef NON_MAIN
#include "s_apps.h"
#include <openssl/ssl.h>
# 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
return (0);
}
}
-# endif /* OPENSSL_SYS_WINDOWS */
+# endif
return (1);
}
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)
return (0);
}
- memset((char *)&them, 0, sizeof(them));
+ memset(&them, 0, sizeof(them));
them.sun_family = AF_UNIX;
strcpy(them.sun_path, server);
} 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)
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)
if (s == 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);
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
*/
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
*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);
*/
goto redoit;
}
- fprintf(stderr, "errno=%d ", errno);
- perror("accept");
+ BIO_printf(bio_err, "accept errno=%d, %s\n", errno, strerror(errno));
return (0);
}
# endif
int extract_host_port(char *str, char **host_ptr, unsigned char *ip,
- short *port_ptr)
+ unsigned short *port_ptr)
{
char *h, *p;
if (!ssl_sock_init())
return (0);
- he = GetHostByName(str);
+ he = gethostbyname(str);
if (he == NULL) {
BIO_printf(bio_err, "gethostbyname failure\n");
goto err;
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;
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