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