|
|
fd2893 |
diff -up openssl-1.0.2a/apps/s_apps.h.ipv6-apps openssl-1.0.2a/apps/s_apps.h
|
|
|
fd2893 |
--- openssl-1.0.2a/apps/s_apps.h.ipv6-apps 2015-04-20 15:01:24.029120104 +0200
|
|
|
fd2893 |
+++ openssl-1.0.2a/apps/s_apps.h 2015-04-20 15:05:00.353137701 +0200
|
|
|
fd2893 |
@@ -151,7 +151,7 @@ typedef fd_mask fd_set;
|
|
|
fd2893 |
#define PORT_STR "4433"
|
|
|
fd2893 |
#define PROTOCOL "tcp"
|
|
|
fd2893 |
|
|
|
fd2893 |
-int do_server(int port, int type, int *ret,
|
|
|
fd2893 |
+int do_server(char *port, int type, int *ret,
|
|
|
fd2893 |
int (*cb) (char *hostname, int s, int stype,
|
|
|
fd2893 |
unsigned char *context), unsigned char *context,
|
|
|
fd2893 |
int naccept);
|
|
|
fd2893 |
@@ -167,11 +167,10 @@ int ssl_print_point_formats(BIO *out, SS
|
|
|
fd2893 |
int ssl_print_curves(BIO *out, SSL *s, int noshared);
|
|
|
fd2893 |
#endif
|
|
|
fd2893 |
int ssl_print_tmp_key(BIO *out, SSL *s);
|
|
|
fd2893 |
-int init_client(int *sock, char *server, int port, int type);
|
|
|
fd2893 |
+int init_client(int *sock, char *server, char *port, int type);
|
|
|
fd2893 |
int should_retry(int i);
|
|
|
fd2893 |
int extract_port(char *str, short *port_ptr);
|
|
|
fd2893 |
-int extract_host_port(char *str, char **host_ptr, unsigned char *ip,
|
|
|
fd2893 |
- short *p);
|
|
|
fd2893 |
+int extract_host_port(char *str, char **host_ptr, char **port_ptr);
|
|
|
fd2893 |
|
|
|
fd2893 |
long MS_CALLBACK bio_dump_callback(BIO *bio, int cmd, const char *argp,
|
|
|
fd2893 |
int argi, long argl, long ret);
|
|
|
fd2893 |
diff -up openssl-1.0.2a/apps/s_client.c.ipv6-apps openssl-1.0.2a/apps/s_client.c
|
|
|
fd2893 |
--- openssl-1.0.2a/apps/s_client.c.ipv6-apps 2015-04-20 15:01:24.022119942 +0200
|
|
|
fd2893 |
+++ openssl-1.0.2a/apps/s_client.c 2015-04-20 15:06:42.338503234 +0200
|
|
|
fd2893 |
@@ -662,7 +662,7 @@ int MAIN(int argc, char **argv)
|
|
|
fd2893 |
int cbuf_len, cbuf_off;
|
|
|
fd2893 |
int sbuf_len, sbuf_off;
|
|
|
fd2893 |
fd_set readfds, writefds;
|
|
|
fd2893 |
- short port = PORT;
|
|
|
fd2893 |
+ char *port_str = PORT_STR;
|
|
|
fd2893 |
int full_log = 1;
|
|
|
fd2893 |
char *host = SSL_HOST_NAME;
|
|
|
fd2893 |
char *cert_file = NULL, *key_file = NULL, *chain_file = NULL;
|
|
|
fd2893 |
@@ -785,13 +785,11 @@ int MAIN(int argc, char **argv)
|
|
|
fd2893 |
} else if (strcmp(*argv, "-port") == 0) {
|
|
|
fd2893 |
if (--argc < 1)
|
|
|
fd2893 |
goto bad;
|
|
|
fd2893 |
- port = atoi(*(++argv));
|
|
|
fd2893 |
- if (port == 0)
|
|
|
fd2893 |
- goto bad;
|
|
|
fd2893 |
+ port_str = *(++argv);
|
|
|
fd2893 |
} else if (strcmp(*argv, "-connect") == 0) {
|
|
|
fd2893 |
if (--argc < 1)
|
|
|
fd2893 |
goto bad;
|
|
|
fd2893 |
- if (!extract_host_port(*(++argv), &host, NULL, &port))
|
|
|
fd2893 |
+ if (!extract_host_port(*(++argv), &host, &port_str))
|
|
|
fd2893 |
goto bad;
|
|
|
fd2893 |
} else if (strcmp(*argv, "-verify") == 0) {
|
|
|
fd2893 |
verify = SSL_VERIFY_PEER;
|
|
|
fd2893 |
@@ -1417,7 +1415,7 @@ int MAIN(int argc, char **argv)
|
|
|
fd2893 |
|
|
|
fd2893 |
re_start:
|
|
|
fd2893 |
|
|
|
fd2893 |
- if (init_client(&s, host, port, socket_type) == 0) {
|
|
|
fd2893 |
+ if (init_client(&s, host, port_str, socket_type) == 0) {
|
|
|
fd2893 |
BIO_printf(bio_err, "connect:errno=%d\n", get_last_socket_error());
|
|
|
fd2893 |
SHUTDOWN(s);
|
|
|
fd2893 |
goto end;
|
|
|
fd2893 |
diff -up openssl-1.0.2a/apps/s_server.c.ipv6-apps openssl-1.0.2a/apps/s_server.c
|
|
|
fd2893 |
--- openssl-1.0.2a/apps/s_server.c.ipv6-apps 2015-04-20 15:01:24.030120127 +0200
|
|
|
fd2893 |
+++ openssl-1.0.2a/apps/s_server.c 2015-04-20 15:10:47.245187746 +0200
|
|
|
fd2893 |
@@ -1061,7 +1061,7 @@ int MAIN(int argc, char *argv[])
|
|
|
fd2893 |
{
|
|
|
fd2893 |
X509_VERIFY_PARAM *vpm = NULL;
|
|
|
fd2893 |
int badarg = 0;
|
|
|
fd2893 |
- short port = PORT;
|
|
|
fd2893 |
+ char *port_str = PORT_STR;
|
|
|
fd2893 |
char *CApath = NULL, *CAfile = NULL;
|
|
|
fd2893 |
char *chCApath = NULL, *chCAfile = NULL;
|
|
|
fd2893 |
char *vfyCApath = NULL, *vfyCAfile = NULL;
|
|
|
fd2893 |
@@ -1148,7 +1148,8 @@ int MAIN(int argc, char *argv[])
|
|
|
fd2893 |
if ((strcmp(*argv, "-port") == 0) || (strcmp(*argv, "-accept") == 0)) {
|
|
|
fd2893 |
if (--argc < 1)
|
|
|
fd2893 |
goto bad;
|
|
|
fd2893 |
- if (!extract_port(*(++argv), &port))
|
|
|
fd2893 |
+ port_str = *(++argv);
|
|
|
fd2893 |
+ if (port_str == NULL || *port_str == '\0')
|
|
|
fd2893 |
goto bad;
|
|
|
fd2893 |
} else if (strcmp(*argv, "-naccept") == 0) {
|
|
|
fd2893 |
if (--argc < 1)
|
|
|
fd2893 |
@@ -2020,13 +2021,13 @@ int MAIN(int argc, char *argv[])
|
|
|
fd2893 |
BIO_printf(bio_s_out, "ACCEPT\n");
|
|
|
fd2893 |
(void)BIO_flush(bio_s_out);
|
|
|
fd2893 |
if (rev)
|
|
|
fd2893 |
- do_server(port, socket_type, &accept_socket, rev_body, context,
|
|
|
fd2893 |
+ do_server(port_str, socket_type, &accept_socket, rev_body, context,
|
|
|
fd2893 |
naccept);
|
|
|
fd2893 |
else if (www)
|
|
|
fd2893 |
- do_server(port, socket_type, &accept_socket, www_body, context,
|
|
|
fd2893 |
+ do_server(port_str, socket_type, &accept_socket, www_body, context,
|
|
|
fd2893 |
naccept);
|
|
|
fd2893 |
else
|
|
|
fd2893 |
- do_server(port, socket_type, &accept_socket, sv_body, context,
|
|
|
fd2893 |
+ do_server(port_str, socket_type, &accept_socket, sv_body, context,
|
|
|
fd2893 |
naccept);
|
|
|
fd2893 |
print_stats(bio_s_out, ctx);
|
|
|
fd2893 |
ret = 0;
|
|
|
fd2893 |
diff -up openssl-1.0.2a/apps/s_socket.c.ipv6-apps openssl-1.0.2a/apps/s_socket.c
|
|
|
fd2893 |
--- openssl-1.0.2a/apps/s_socket.c.ipv6-apps 2015-03-19 14:30:36.000000000 +0100
|
|
|
fd2893 |
+++ openssl-1.0.2a/apps/s_socket.c 2015-04-20 15:32:53.960079507 +0200
|
|
|
fd2893 |
@@ -106,9 +106,7 @@ static struct hostent *GetHostByName(cha
|
|
|
fd2893 |
static void ssl_sock_cleanup(void);
|
|
|
fd2893 |
# endif
|
|
|
fd2893 |
static int ssl_sock_init(void);
|
|
|
fd2893 |
-static int init_client_ip(int *sock, unsigned char ip[4], int port, int type);
|
|
|
fd2893 |
-static int init_server(int *sock, int port, int type);
|
|
|
fd2893 |
-static int init_server_long(int *sock, int port, char *ip, int type);
|
|
|
fd2893 |
+static int init_server(int *sock, char *port, int type);
|
|
|
fd2893 |
static int do_accept(int acc_sock, int *sock, char **host);
|
|
|
fd2893 |
static int host_ip(char *str, unsigned char ip[4]);
|
|
|
fd2893 |
|
|
|
fd2893 |
@@ -231,65 +229,66 @@ static int ssl_sock_init(void)
|
|
|
fd2893 |
return (1);
|
|
|
fd2893 |
}
|
|
|
fd2893 |
|
|
|
fd2893 |
-int init_client(int *sock, char *host, int port, int type)
|
|
|
fd2893 |
+int init_client(int *sock, char *host, char *port, int type)
|
|
|
fd2893 |
{
|
|
|
fd2893 |
- unsigned char ip[4];
|
|
|
fd2893 |
-
|
|
|
fd2893 |
- memset(ip, '\0', sizeof ip);
|
|
|
fd2893 |
- if (!host_ip(host, &(ip[0])))
|
|
|
fd2893 |
- return 0;
|
|
|
fd2893 |
- return init_client_ip(sock, ip, port, type);
|
|
|
fd2893 |
-}
|
|
|
fd2893 |
-
|
|
|
fd2893 |
-static int init_client_ip(int *sock, unsigned char ip[4], int port, int type)
|
|
|
fd2893 |
-{
|
|
|
fd2893 |
- unsigned long addr;
|
|
|
fd2893 |
- struct sockaddr_in them;
|
|
|
fd2893 |
- int s, i;
|
|
|
fd2893 |
+ struct addrinfo *res, *res0, hints;
|
|
|
fd2893 |
+ char *failed_call = NULL;
|
|
|
fd2893 |
+ int s;
|
|
|
fd2893 |
+ int e;
|
|
|
fd2893 |
|
|
|
fd2893 |
if (!ssl_sock_init())
|
|
|
fd2893 |
return (0);
|
|
|
fd2893 |
|
|
|
fd2893 |
- memset((char *)&them, 0, sizeof(them));
|
|
|
fd2893 |
- them.sin_family = AF_INET;
|
|
|
fd2893 |
- them.sin_port = htons((unsigned short)port);
|
|
|
fd2893 |
- addr = (unsigned long)
|
|
|
fd2893 |
- ((unsigned long)ip[0] << 24L) |
|
|
|
fd2893 |
- ((unsigned long)ip[1] << 16L) |
|
|
|
fd2893 |
- ((unsigned long)ip[2] << 8L) | ((unsigned long)ip[3]);
|
|
|
fd2893 |
- them.sin_addr.s_addr = htonl(addr);
|
|
|
fd2893 |
-
|
|
|
fd2893 |
- if (type == SOCK_STREAM)
|
|
|
fd2893 |
- s = socket(AF_INET, SOCK_STREAM, SOCKET_PROTOCOL);
|
|
|
fd2893 |
- else /* ( type == SOCK_DGRAM) */
|
|
|
fd2893 |
- s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
|
|
fd2893 |
-
|
|
|
fd2893 |
- if (s == INVALID_SOCKET) {
|
|
|
fd2893 |
- perror("socket");
|
|
|
fd2893 |
+ memset(&hints, '\0', sizeof(hints));
|
|
|
fd2893 |
+ hints.ai_socktype = type;
|
|
|
fd2893 |
+ hints.ai_flags = AI_ADDRCONFIG;
|
|
|
fd2893 |
+
|
|
|
fd2893 |
+ e = getaddrinfo(host, port, &hints, &res;;
|
|
|
fd2893 |
+ if (e) {
|
|
|
fd2893 |
+ fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(e));
|
|
|
fd2893 |
+ if (e == EAI_SYSTEM)
|
|
|
fd2893 |
+ perror("getaddrinfo");
|
|
|
fd2893 |
return (0);
|
|
|
fd2893 |
}
|
|
|
fd2893 |
+
|
|
|
fd2893 |
+ res0 = res;
|
|
|
fd2893 |
+ while (res) {
|
|
|
fd2893 |
+ s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
|
|
fd2893 |
+ if (s == INVALID_SOCKET) {
|
|
|
fd2893 |
+ failed_call = "socket";
|
|
|
fd2893 |
+ goto nextres;
|
|
|
fd2893 |
+ }
|
|
|
fd2893 |
# if defined(SO_KEEPALIVE) && !defined(OPENSSL_SYS_MPE)
|
|
|
fd2893 |
- if (type == SOCK_STREAM) {
|
|
|
fd2893 |
- i = 0;
|
|
|
fd2893 |
- i = setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (char *)&i, sizeof(i));
|
|
|
fd2893 |
- if (i < 0) {
|
|
|
fd2893 |
- closesocket(s);
|
|
|
fd2893 |
- perror("keepalive");
|
|
|
fd2893 |
- return (0);
|
|
|
fd2893 |
+ if (type == SOCK_STREAM) {
|
|
|
fd2893 |
+ int i = 0;
|
|
|
fd2893 |
+ i = setsockopt(s, SOL_SOCKET, SO_KEEPALIVE,
|
|
|
fd2893 |
+ (char *)&i, sizeof(i));
|
|
|
fd2893 |
+ if (i < 0) {
|
|
|
fd2893 |
+ failed_call = "keepalive";
|
|
|
fd2893 |
+ goto nextres;
|
|
|
fd2893 |
+ }
|
|
|
fd2893 |
}
|
|
|
fd2893 |
- }
|
|
|
fd2893 |
# endif
|
|
|
fd2893 |
-
|
|
|
fd2893 |
- if (connect(s, (struct sockaddr *)&them, sizeof(them)) == -1) {
|
|
|
fd2893 |
- closesocket(s);
|
|
|
fd2893 |
- perror("connect");
|
|
|
fd2893 |
- return (0);
|
|
|
fd2893 |
+ if (connect(s, (struct sockaddr *)res->ai_addr, res->ai_addrlen) == 0) {
|
|
|
fd2893 |
+ freeaddrinfo(res0);
|
|
|
fd2893 |
+ *sock = s;
|
|
|
fd2893 |
+ return (1);
|
|
|
fd2893 |
+ }
|
|
|
fd2893 |
+
|
|
|
fd2893 |
+ failed_call = "socket";
|
|
|
fd2893 |
+ nextres:
|
|
|
fd2893 |
+ if (s != INVALID_SOCKET)
|
|
|
fd2893 |
+ close(s);
|
|
|
fd2893 |
+ res = res->ai_next;
|
|
|
fd2893 |
}
|
|
|
fd2893 |
- *sock = s;
|
|
|
fd2893 |
- return (1);
|
|
|
fd2893 |
+ freeaddrinfo(res0);
|
|
|
fd2893 |
+ closesocket(s);
|
|
|
fd2893 |
+
|
|
|
fd2893 |
+ perror(failed_call);
|
|
|
fd2893 |
+ return (0);
|
|
|
fd2893 |
}
|
|
|
fd2893 |
|
|
|
fd2893 |
-int do_server(int port, int type, int *ret,
|
|
|
fd2893 |
+int do_server(char *port, int type, int *ret,
|
|
|
fd2893 |
int (*cb) (char *hostname, int s, int stype,
|
|
|
fd2893 |
unsigned char *context), unsigned char *context,
|
|
|
fd2893 |
int naccept)
|
|
|
fd2893 |
@@ -328,69 +327,89 @@ int do_server(int port, int type, int *r
|
|
|
fd2893 |
}
|
|
|
fd2893 |
}
|
|
|
fd2893 |
|
|
|
fd2893 |
-static int init_server_long(int *sock, int port, char *ip, int type)
|
|
|
fd2893 |
+static int init_server(int *sock, char *port, int type)
|
|
|
fd2893 |
{
|
|
|
fd2893 |
- int ret = 0;
|
|
|
fd2893 |
- struct sockaddr_in server;
|
|
|
fd2893 |
- int s = -1;
|
|
|
fd2893 |
+ struct addrinfo *res, *res0 = NULL, hints;
|
|
|
fd2893 |
+ char *failed_call = NULL;
|
|
|
fd2893 |
+ int s = INVALID_SOCKET;
|
|
|
fd2893 |
+ int e;
|
|
|
fd2893 |
|
|
|
fd2893 |
if (!ssl_sock_init())
|
|
|
fd2893 |
return (0);
|
|
|
fd2893 |
|
|
|
fd2893 |
- memset((char *)&server, 0, sizeof(server));
|
|
|
fd2893 |
- server.sin_family = AF_INET;
|
|
|
fd2893 |
- server.sin_port = htons((unsigned short)port);
|
|
|
fd2893 |
- if (ip == NULL)
|
|
|
fd2893 |
- server.sin_addr.s_addr = INADDR_ANY;
|
|
|
fd2893 |
- else
|
|
|
fd2893 |
-/* Added for T3E, address-of fails on bit field (beckman@acl.lanl.gov) */
|
|
|
fd2893 |
-# ifndef BIT_FIELD_LIMITS
|
|
|
fd2893 |
- memcpy(&server.sin_addr.s_addr, ip, 4);
|
|
|
fd2893 |
-# else
|
|
|
fd2893 |
- memcpy(&server.sin_addr, ip, 4);
|
|
|
fd2893 |
-# endif
|
|
|
fd2893 |
-
|
|
|
fd2893 |
- if (type == SOCK_STREAM)
|
|
|
fd2893 |
- s = socket(AF_INET, SOCK_STREAM, SOCKET_PROTOCOL);
|
|
|
fd2893 |
- else /* type == SOCK_DGRAM */
|
|
|
fd2893 |
- s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
|
|
fd2893 |
+ memset(&hints, '\0', sizeof(hints));
|
|
|
fd2893 |
+ hints.ai_family = AF_INET6;
|
|
|
fd2893 |
+ tryipv4:
|
|
|
fd2893 |
+ hints.ai_socktype = type;
|
|
|
fd2893 |
+ hints.ai_flags = AI_PASSIVE;
|
|
|
fd2893 |
+
|
|
|
fd2893 |
+ e = getaddrinfo(NULL, port, &hints, &res;;
|
|
|
fd2893 |
+ if (e) {
|
|
|
fd2893 |
+ if (hints.ai_family == AF_INET) {
|
|
|
fd2893 |
+ fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(e));
|
|
|
fd2893 |
+ if (e == EAI_SYSTEM)
|
|
|
fd2893 |
+ perror("getaddrinfo");
|
|
|
fd2893 |
+ return (0);
|
|
|
fd2893 |
+ } else
|
|
|
fd2893 |
+ res = NULL;
|
|
|
fd2893 |
+ }
|
|
|
fd2893 |
|
|
|
fd2893 |
- if (s == INVALID_SOCKET)
|
|
|
fd2893 |
- goto err;
|
|
|
fd2893 |
+ res0 = res;
|
|
|
fd2893 |
+ while (res) {
|
|
|
fd2893 |
+ s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
|
|
fd2893 |
+ if (s == INVALID_SOCKET) {
|
|
|
fd2893 |
+ failed_call = "socket";
|
|
|
fd2893 |
+ goto nextres;
|
|
|
fd2893 |
+ }
|
|
|
fd2893 |
+ if (hints.ai_family == AF_INET6) {
|
|
|
fd2893 |
+ int j = 0;
|
|
|
fd2893 |
+ setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&j, sizeof j);
|
|
|
fd2893 |
+ }
|
|
|
fd2893 |
# if defined SOL_SOCKET && defined SO_REUSEADDR
|
|
|
fd2893 |
- {
|
|
|
fd2893 |
- int j = 1;
|
|
|
fd2893 |
- setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *)&j, sizeof j);
|
|
|
fd2893 |
- }
|
|
|
fd2893 |
-# endif
|
|
|
fd2893 |
- if (bind(s, (struct sockaddr *)&server, sizeof(server)) == -1) {
|
|
|
fd2893 |
-# ifndef OPENSSL_SYS_WINDOWS
|
|
|
fd2893 |
- perror("bind");
|
|
|
fd2893 |
+ {
|
|
|
fd2893 |
+ int j = 1;
|
|
|
fd2893 |
+ setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *)&j, sizeof j);
|
|
|
fd2893 |
+ }
|
|
|
fd2893 |
# endif
|
|
|
fd2893 |
- goto err;
|
|
|
fd2893 |
+
|
|
|
fd2893 |
+ if (bind(s, (struct sockaddr *)res->ai_addr, res->ai_addrlen) == -1) {
|
|
|
fd2893 |
+ failed_call = "bind";
|
|
|
fd2893 |
+ goto nextres;
|
|
|
fd2893 |
+ }
|
|
|
fd2893 |
+ if (type == SOCK_STREAM && listen(s, 128) == -1) {
|
|
|
fd2893 |
+ failed_call = "listen";
|
|
|
fd2893 |
+ goto nextres;
|
|
|
fd2893 |
+ }
|
|
|
fd2893 |
+
|
|
|
fd2893 |
+ *sock = s;
|
|
|
fd2893 |
+ return (1);
|
|
|
fd2893 |
+
|
|
|
fd2893 |
+ nextres:
|
|
|
fd2893 |
+ if (s != INVALID_SOCKET)
|
|
|
fd2893 |
+ close(s);
|
|
|
fd2893 |
+ res = res->ai_next;
|
|
|
fd2893 |
}
|
|
|
fd2893 |
- /* Make it 128 for linux */
|
|
|
fd2893 |
- if (type == SOCK_STREAM && listen(s, 128) == -1)
|
|
|
fd2893 |
- goto err;
|
|
|
fd2893 |
- *sock = s;
|
|
|
fd2893 |
- ret = 1;
|
|
|
fd2893 |
- err:
|
|
|
fd2893 |
- if ((ret == 0) && (s != -1)) {
|
|
|
fd2893 |
- SHUTDOWN(s);
|
|
|
fd2893 |
+ if (res0)
|
|
|
fd2893 |
+ freeaddrinfo(res0);
|
|
|
fd2893 |
+
|
|
|
fd2893 |
+ if (s == INVALID_SOCKET) {
|
|
|
fd2893 |
+ if (hints.ai_family == AF_INET6) {
|
|
|
fd2893 |
+ hints.ai_family = AF_INET;
|
|
|
fd2893 |
+ goto tryipv4;
|
|
|
fd2893 |
+ }
|
|
|
fd2893 |
+ perror("socket");
|
|
|
fd2893 |
+ return (0);
|
|
|
fd2893 |
}
|
|
|
fd2893 |
- return (ret);
|
|
|
fd2893 |
-}
|
|
|
fd2893 |
|
|
|
fd2893 |
-static int init_server(int *sock, int port, int type)
|
|
|
fd2893 |
-{
|
|
|
fd2893 |
- return (init_server_long(sock, port, NULL, type));
|
|
|
fd2893 |
+ perror(failed_call);
|
|
|
fd2893 |
+ return (0);
|
|
|
fd2893 |
}
|
|
|
fd2893 |
|
|
|
fd2893 |
static int do_accept(int acc_sock, int *sock, char **host)
|
|
|
fd2893 |
{
|
|
|
fd2893 |
+ static struct sockaddr_storage from;
|
|
|
fd2893 |
+ char buffer[NI_MAXHOST];
|
|
|
fd2893 |
int ret;
|
|
|
fd2893 |
- struct hostent *h1, *h2;
|
|
|
fd2893 |
- static struct sockaddr_in from;
|
|
|
fd2893 |
int len;
|
|
|
fd2893 |
/* struct linger ling; */
|
|
|
fd2893 |
|
|
|
fd2893 |
@@ -432,134 +451,60 @@ static int do_accept(int acc_sock, int *
|
|
|
fd2893 |
ling.l_onoff=1;
|
|
|
fd2893 |
ling.l_linger=0;
|
|
|
fd2893 |
i=setsockopt(ret,SOL_SOCKET,SO_LINGER,(char *)&ling,sizeof(ling));
|
|
|
fd2893 |
- if (i < 0) { perror("linger"); return(0); }
|
|
|
fd2893 |
+ if (i < 0) { closesocket(ret); perror("linger"); return(0); }
|
|
|
fd2893 |
i=0;
|
|
|
fd2893 |
i=setsockopt(ret,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i));
|
|
|
fd2893 |
- if (i < 0) { perror("keepalive"); return(0); }
|
|
|
fd2893 |
+ if (i < 0) { closesocket(ret); perror("keepalive"); return(0); }
|
|
|
fd2893 |
*/
|
|
|
fd2893 |
|
|
|
fd2893 |
if (host == NULL)
|
|
|
fd2893 |
goto end;
|
|
|
fd2893 |
-# ifndef BIT_FIELD_LIMITS
|
|
|
fd2893 |
- /* I should use WSAAsyncGetHostByName() under windows */
|
|
|
fd2893 |
- h1 = gethostbyaddr((char *)&from.sin_addr.s_addr,
|
|
|
fd2893 |
- sizeof(from.sin_addr.s_addr), AF_INET);
|
|
|
fd2893 |
-# else
|
|
|
fd2893 |
- h1 = gethostbyaddr((char *)&from.sin_addr,
|
|
|
fd2893 |
- sizeof(struct in_addr), AF_INET);
|
|
|
fd2893 |
-# endif
|
|
|
fd2893 |
- if (h1 == NULL) {
|
|
|
fd2893 |
- BIO_printf(bio_err, "bad gethostbyaddr\n");
|
|
|
fd2893 |
+
|
|
|
fd2893 |
+ if (getnameinfo((struct sockaddr *)&from, sizeof(from),
|
|
|
fd2893 |
+ buffer, sizeof(buffer), NULL, 0, 0)) {
|
|
|
fd2893 |
+ BIO_printf(bio_err, "getnameinfo failed\n");
|
|
|
fd2893 |
*host = NULL;
|
|
|
fd2893 |
/* return(0); */
|
|
|
fd2893 |
} else {
|
|
|
fd2893 |
- if ((*host = (char *)OPENSSL_malloc(strlen(h1->h_name) + 1)) == NULL) {
|
|
|
fd2893 |
+ if ((*host = (char *)OPENSSL_malloc(strlen(buffer) + 1)) == NULL) {
|
|
|
fd2893 |
perror("OPENSSL_malloc");
|
|
|
fd2893 |
closesocket(ret);
|
|
|
fd2893 |
return (0);
|
|
|
fd2893 |
}
|
|
|
fd2893 |
- BUF_strlcpy(*host, h1->h_name, strlen(h1->h_name) + 1);
|
|
|
fd2893 |
-
|
|
|
fd2893 |
- h2 = GetHostByName(*host);
|
|
|
fd2893 |
- if (h2 == NULL) {
|
|
|
fd2893 |
- BIO_printf(bio_err, "gethostbyname failure\n");
|
|
|
fd2893 |
- closesocket(ret);
|
|
|
fd2893 |
- return (0);
|
|
|
fd2893 |
- }
|
|
|
fd2893 |
- if (h2->h_addrtype != AF_INET) {
|
|
|
fd2893 |
- BIO_printf(bio_err, "gethostbyname addr is not AF_INET\n");
|
|
|
fd2893 |
- closesocket(ret);
|
|
|
fd2893 |
- return (0);
|
|
|
fd2893 |
- }
|
|
|
fd2893 |
+ strcpy(*host, buffer);
|
|
|
fd2893 |
}
|
|
|
fd2893 |
end:
|
|
|
fd2893 |
*sock = ret;
|
|
|
fd2893 |
return (1);
|
|
|
fd2893 |
}
|
|
|
fd2893 |
|
|
|
fd2893 |
-int extract_host_port(char *str, char **host_ptr, unsigned char *ip,
|
|
|
fd2893 |
- short *port_ptr)
|
|
|
fd2893 |
+int extract_host_port(char *str, char **host_ptr, char **port_ptr)
|
|
|
fd2893 |
{
|
|
|
fd2893 |
- char *h, *p;
|
|
|
fd2893 |
+ char *h, *p, *x;
|
|
|
fd2893 |
|
|
|
fd2893 |
- h = str;
|
|
|
fd2893 |
- p = strchr(str, ':');
|
|
|
fd2893 |
+ x = h = str;
|
|
|
fd2893 |
+ if (*h == '[') {
|
|
|
fd2893 |
+ h++;
|
|
|
fd2893 |
+ p = strchr(h, ']');
|
|
|
fd2893 |
+ if (p == NULL) {
|
|
|
fd2893 |
+ BIO_printf(bio_err, "no ending bracket for IPv6 address\n");
|
|
|
fd2893 |
+ return (0);
|
|
|
fd2893 |
+ }
|
|
|
fd2893 |
+ *(p++) = '\0';
|
|
|
fd2893 |
+ x = p;
|
|
|
fd2893 |
+ }
|
|
|
fd2893 |
+ p = strchr(x, ':');
|
|
|
fd2893 |
if (p == NULL) {
|
|
|
fd2893 |
BIO_printf(bio_err, "no port defined\n");
|
|
|
fd2893 |
return (0);
|
|
|
fd2893 |
}
|
|
|
fd2893 |
*(p++) = '\0';
|
|
|
fd2893 |
|
|
|
fd2893 |
- if ((ip != NULL) && !host_ip(str, ip))
|
|
|
fd2893 |
- goto err;
|
|
|
fd2893 |
if (host_ptr != NULL)
|
|
|
fd2893 |
*host_ptr = h;
|
|
|
fd2893 |
+ if (port_ptr != NULL)
|
|
|
fd2893 |
+ *port_ptr = p;
|
|
|
fd2893 |
|
|
|
fd2893 |
- if (!extract_port(p, port_ptr))
|
|
|
fd2893 |
- goto err;
|
|
|
fd2893 |
- return (1);
|
|
|
fd2893 |
- err:
|
|
|
fd2893 |
- return (0);
|
|
|
fd2893 |
-}
|
|
|
fd2893 |
-
|
|
|
fd2893 |
-static int host_ip(char *str, unsigned char ip[4])
|
|
|
fd2893 |
-{
|
|
|
fd2893 |
- unsigned int in[4];
|
|
|
fd2893 |
- int i;
|
|
|
fd2893 |
-
|
|
|
fd2893 |
- if (sscanf(str, "%u.%u.%u.%u", &(in[0]), &(in[1]), &(in[2]), &(in[3])) ==
|
|
|
fd2893 |
- 4) {
|
|
|
fd2893 |
- for (i = 0; i < 4; i++)
|
|
|
fd2893 |
- if (in[i] > 255) {
|
|
|
fd2893 |
- BIO_printf(bio_err, "invalid IP address\n");
|
|
|
fd2893 |
- goto err;
|
|
|
fd2893 |
- }
|
|
|
fd2893 |
- ip[0] = in[0];
|
|
|
fd2893 |
- ip[1] = in[1];
|
|
|
fd2893 |
- ip[2] = in[2];
|
|
|
fd2893 |
- ip[3] = in[3];
|
|
|
fd2893 |
- } else { /* do a gethostbyname */
|
|
|
fd2893 |
- struct hostent *he;
|
|
|
fd2893 |
-
|
|
|
fd2893 |
- if (!ssl_sock_init())
|
|
|
fd2893 |
- return (0);
|
|
|
fd2893 |
-
|
|
|
fd2893 |
- he = GetHostByName(str);
|
|
|
fd2893 |
- if (he == NULL) {
|
|
|
fd2893 |
- BIO_printf(bio_err, "gethostbyname failure\n");
|
|
|
fd2893 |
- goto err;
|
|
|
fd2893 |
- }
|
|
|
fd2893 |
- /* cast to short because of win16 winsock definition */
|
|
|
fd2893 |
- if ((short)he->h_addrtype != AF_INET) {
|
|
|
fd2893 |
- BIO_printf(bio_err, "gethostbyname addr is not AF_INET\n");
|
|
|
fd2893 |
- return (0);
|
|
|
fd2893 |
- }
|
|
|
fd2893 |
- ip[0] = he->h_addr_list[0][0];
|
|
|
fd2893 |
- ip[1] = he->h_addr_list[0][1];
|
|
|
fd2893 |
- ip[2] = he->h_addr_list[0][2];
|
|
|
fd2893 |
- ip[3] = he->h_addr_list[0][3];
|
|
|
fd2893 |
- }
|
|
|
fd2893 |
- return (1);
|
|
|
fd2893 |
- err:
|
|
|
fd2893 |
- return (0);
|
|
|
fd2893 |
-}
|
|
|
fd2893 |
-
|
|
|
fd2893 |
-int extract_port(char *str, short *port_ptr)
|
|
|
fd2893 |
-{
|
|
|
fd2893 |
- int i;
|
|
|
fd2893 |
- struct servent *s;
|
|
|
fd2893 |
-
|
|
|
fd2893 |
- i = atoi(str);
|
|
|
fd2893 |
- if (i != 0)
|
|
|
fd2893 |
- *port_ptr = (unsigned short)i;
|
|
|
fd2893 |
- else {
|
|
|
fd2893 |
- s = getservbyname(str, "tcp");
|
|
|
fd2893 |
- if (s == NULL) {
|
|
|
fd2893 |
- BIO_printf(bio_err, "getservbyname failure for %s\n", str);
|
|
|
fd2893 |
- return (0);
|
|
|
fd2893 |
- }
|
|
|
fd2893 |
- *port_ptr = ntohs((unsigned short)s->s_port);
|
|
|
fd2893 |
- }
|
|
|
fd2893 |
return (1);
|
|
|
fd2893 |
}
|
|
|
fd2893 |
|