summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjsing <jsing@openbsd.org>2014-06-24 17:42:54 +0000
committerjsing <jsing@openbsd.org>2014-06-24 17:42:54 +0000
commit4463abb8fec17413bc617454f913355216b5437a (patch)
tree7a6bfe869fcd4afa485f9446320d37a3bacf7d96
parentActually make BIO_set_tcp_ndelay() work - TCP_NODELAY will not magically (diff)
downloadwireguard-openbsd-4463abb8fec17413bc617454f913355216b5437a.tar.xz
wireguard-openbsd-4463abb8fec17413bc617454f913355216b5437a.zip
Replace 48 lines of code with a single inet_pton() call. The previous
handrolled version could not even make use of sscanf(), since that would not work with a certain antiquated compiler. It is worth noting that there is a tiny change in behaviour - previously calling BIO_get_host_ip() with something that looked like it might be a valid IP address (for example, "1." or even ".") would result in it returning failure rather than trying a BIO_gethostbyname() - now we'll always try a BIO_gethostbyname() if it was not a valid IPv4 address. ok beck@ miod@ deraadt@
-rw-r--r--lib/libcrypto/bio/b_sock.c51
-rw-r--r--lib/libssl/src/crypto/bio/b_sock.c51
2 files changed, 6 insertions, 96 deletions
diff --git a/lib/libcrypto/bio/b_sock.c b/lib/libcrypto/bio/b_sock.c
index 62d545a1299..5f8b1e052fe 100644
--- a/lib/libcrypto/bio/b_sock.c
+++ b/lib/libcrypto/bio/b_sock.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: b_sock.c,v 1.42 2014/06/24 17:30:00 jsing Exp $ */
+/* $OpenBSD: b_sock.c,v 1.43 2014/06/24 17:42:54 jsing Exp $ */
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
* All rights reserved.
*
@@ -59,6 +59,7 @@
#include <sys/ioctl.h>
#include <sys/socket.h>
+#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
@@ -72,8 +73,6 @@
#include "cryptlib.h"
-static int get_ip(const char *str, unsigned char *ip);
-
int
BIO_get_host_ip(const char *str, unsigned char *ip)
{
@@ -82,15 +81,7 @@ BIO_get_host_ip(const char *str, unsigned char *ip)
int locked = 0;
struct hostent *he;
- i = get_ip(str, ip);
- if (i < 0) {
- BIOerr(BIO_F_BIO_GET_HOST_IP, BIO_R_INVALID_IP_ADDRESS);
- goto err;
- }
-
- /* If the string actually contained an IP address, we need not do
- anything more */
- if (i > 0)
+ if (inet_pton(AF_INET, str, ip) == 1)
return (1);
/* do a gethostbyname */
@@ -228,42 +219,6 @@ BIO_socket_ioctl(int fd, long type, void *arg)
return (i);
}
-/* The reason I have implemented this instead of using sscanf is because
- * Visual C 1.52c gives an unresolved external when linking a DLL :-( */
-static int
-get_ip(const char *str, unsigned char ip[4])
-{
- unsigned int tmp[4];
- int num = 0, c, ok = 0;
-
- tmp[0] = tmp[1] = tmp[2] = tmp[3] = 0;
-
- for (;;) {
- c= *(str++);
- if ((c >= '0') && (c <= '9')) {
- ok = 1;
- tmp[num] = tmp[num]*10 + c-'0';
- if (tmp[num] > 255)
- return (0);
- } else if (c == '.') {
- if (!ok)
- return (-1);
- if (num == 3)
- return (0);
- num++;
- ok = 0;
- } else if (c == '\0' && (num == 3) && ok)
- break;
- else
- return (0);
- }
- ip[0] = tmp[0];
- ip[1] = tmp[1];
- ip[2] = tmp[2];
- ip[3] = tmp[3];
- return (1);
-}
-
int
BIO_get_accept_socket(char *host, int bind_mode)
{
diff --git a/lib/libssl/src/crypto/bio/b_sock.c b/lib/libssl/src/crypto/bio/b_sock.c
index 62d545a1299..5f8b1e052fe 100644
--- a/lib/libssl/src/crypto/bio/b_sock.c
+++ b/lib/libssl/src/crypto/bio/b_sock.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: b_sock.c,v 1.42 2014/06/24 17:30:00 jsing Exp $ */
+/* $OpenBSD: b_sock.c,v 1.43 2014/06/24 17:42:54 jsing Exp $ */
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
* All rights reserved.
*
@@ -59,6 +59,7 @@
#include <sys/ioctl.h>
#include <sys/socket.h>
+#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
@@ -72,8 +73,6 @@
#include "cryptlib.h"
-static int get_ip(const char *str, unsigned char *ip);
-
int
BIO_get_host_ip(const char *str, unsigned char *ip)
{
@@ -82,15 +81,7 @@ BIO_get_host_ip(const char *str, unsigned char *ip)
int locked = 0;
struct hostent *he;
- i = get_ip(str, ip);
- if (i < 0) {
- BIOerr(BIO_F_BIO_GET_HOST_IP, BIO_R_INVALID_IP_ADDRESS);
- goto err;
- }
-
- /* If the string actually contained an IP address, we need not do
- anything more */
- if (i > 0)
+ if (inet_pton(AF_INET, str, ip) == 1)
return (1);
/* do a gethostbyname */
@@ -228,42 +219,6 @@ BIO_socket_ioctl(int fd, long type, void *arg)
return (i);
}
-/* The reason I have implemented this instead of using sscanf is because
- * Visual C 1.52c gives an unresolved external when linking a DLL :-( */
-static int
-get_ip(const char *str, unsigned char ip[4])
-{
- unsigned int tmp[4];
- int num = 0, c, ok = 0;
-
- tmp[0] = tmp[1] = tmp[2] = tmp[3] = 0;
-
- for (;;) {
- c= *(str++);
- if ((c >= '0') && (c <= '9')) {
- ok = 1;
- tmp[num] = tmp[num]*10 + c-'0';
- if (tmp[num] > 255)
- return (0);
- } else if (c == '.') {
- if (!ok)
- return (-1);
- if (num == 3)
- return (0);
- num++;
- ok = 0;
- } else if (c == '\0' && (num == 3) && ok)
- break;
- else
- return (0);
- }
- ip[0] = tmp[0];
- ip[1] = tmp[1];
- ip[2] = tmp[2];
- ip[3] = tmp[3];
- return (1);
-}
-
int
BIO_get_accept_socket(char *host, int bind_mode)
{