summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorho <ho@openbsd.org>2001-06-27 00:10:35 +0000
committerho <ho@openbsd.org>2001-06-27 00:10:35 +0000
commit255c8f770ffa753bec5eb2511ea89e2791de46a0 (patch)
treee0ee694f663c1cb0c2f11eb00a655b8740d939d0
parentAdd ancontrol too, since it is supported. (diff)
downloadwireguard-openbsd-255c8f770ffa753bec5eb2511ea89e2791de46a0.tar.xz
wireguard-openbsd-255c8f770ffa753bec5eb2511ea89e2791de46a0.zip
A few more functions to help with IPv6 support. By Niklas and me.
-rw-r--r--sbin/isakmpd/util.c98
-rw-r--r--sbin/isakmpd/util.h7
2 files changed, 101 insertions, 4 deletions
diff --git a/sbin/isakmpd/util.c b/sbin/isakmpd/util.c
index d6bb669944d..212e79172d8 100644
--- a/sbin/isakmpd/util.c
+++ b/sbin/isakmpd/util.c
@@ -1,9 +1,9 @@
-/* $OpenBSD: util.c,v 1.12 2001/04/05 23:02:02 ho Exp $ */
+/* $OpenBSD: util.c,v 1.13 2001/06/27 00:10:35 ho Exp $ */
/* $EOM: util.c,v 1.23 2000/11/23 12:22:08 niklas Exp $ */
/*
- * Copyright (c) 1998, 1999 Niklas Hallqvist. All rights reserved.
- * Copyright (c) 2000 Håkan Olsson. All rights reserved.
+ * Copyright (c) 1998, 1999, 2001 Niklas Hallqvist. All rights reserved.
+ * Copyright (c) 2000, 2001 Håkan Olsson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -36,7 +36,10 @@
*/
#include <sys/types.h>
+#include <sys/socket.h>
#include <sys/stat.h>
+#include <netinet/in.h>
+#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
@@ -51,6 +54,12 @@
#include "util.h"
/*
+ * Set if -N is given, allowing name lookups to be done, possibly stalling
+ * the daemon for quite a while.
+ */
+int allow_name_lookups = 0;
+
+/*
* This is set to true in case of regression-test mode, when it will
* cause predictable random numbers be generated.
*/
@@ -231,6 +240,89 @@ hex2raw (char *s, u_int8_t *buf, size_t sz)
return 0;
}
+int
+text2sockaddr (char *address, char *port, struct sockaddr **sa)
+{
+#ifdef HAVE_GETNAMEINFO
+ struct addrinfo *ai, hints;
+
+ memset (&hints, 0, sizeof hints);
+ if (!allow_name_lookups)
+ hints.ai_flags = AI_NUMERICHOST;
+ hints.ai_family = PF_UNSPEC;
+ hints.ai_socktype = SOCK_DGRAM;
+ hints.ai_protocol = IPPROTO_UDP;
+
+ if (getaddrinfo (address, port, &hints, &ai))
+ return -1;
+
+ *sa = malloc (ai->ai_addr->sa_len);
+ if (!sa)
+ return -1;
+
+ memcpy (*sa, ai->ai_addr, ai->ai_addr->sa_len);
+ freeaddrinfo (ai);
+ return 0;
+#else
+ return -1;
+#endif
+}
+
+int
+sockaddr2text (struct sockaddr *sa, char **address)
+{
+#ifdef HAVE_GETNAMEINFO
+ char buf[NI_MAXHOST];
+
+ if (getnameinfo (sa, sa->sa_len, buf, sizeof buf, 0, 0,
+ allow_name_lookups ? 0 : NI_NUMERICHOST))
+ return -1;
+
+ *address = malloc (strlen (buf) + 1);
+ if (!address)
+ return -1;
+
+ strcpy (*address, buf);
+ return 0;
+#else
+ return -1;
+#endif
+}
+
+/*
+ * sockaddr_len and sockaddr_data return the relevant sockaddr info depending
+ * on address family. Useful to keep other code shorter(/clearer?).
+ */
+int
+sockaddr_len (struct sockaddr *sa)
+{
+ switch (sa->sa_family)
+ {
+ case AF_INET6:
+ return sizeof ((struct sockaddr_in6 *)sa)->sin6_addr.s6_addr;
+ case AF_INET:
+ return sizeof ((struct sockaddr_in *)sa)->sin_addr.s_addr;
+ default:
+ log_print ("sockaddr_len: unsupported protocol family %d",
+ sa->sa_family);
+ return 0;
+ }
+}
+
+u_int8_t *
+sockaddr_data (struct sockaddr *sa)
+{
+ switch (sa->sa_family)
+ {
+ case AF_INET6:
+ return (u_int8_t *)&((struct sockaddr_in6 *)sa)->sin6_addr.s6_addr;
+ case AF_INET:
+ return (u_int8_t *)&((struct sockaddr_in *)sa)->sin_addr.s_addr;
+ default:
+ return 0; /* XXX */
+ }
+}
+
/*
* Perform sanity check on files containing secret information.
* Returns -1 on failure, 0 otherwise.
diff --git a/sbin/isakmpd/util.h b/sbin/isakmpd/util.h
index 41f4e4307e8..84b25d084f0 100644
--- a/sbin/isakmpd/util.h
+++ b/sbin/isakmpd/util.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: util.h,v 1.7 2000/11/23 12:56:15 niklas Exp $ */
+/* $OpenBSD: util.h,v 1.8 2001/06/27 00:10:35 ho Exp $ */
/* $EOM: util.h,v 1.10 2000/10/24 13:33:39 niklas Exp $ */
/*
@@ -41,6 +41,7 @@
#define ROUNDUP_32(x) (((x) + 3) & ~4)
+extern int allow_name_lookups;
extern int regrand;
struct message;
@@ -59,6 +60,10 @@ extern void encode_128 (u_int8_t *, u_int8_t *);
#endif
extern u_int8_t *getrandom (u_int8_t *, size_t);
extern int hex2raw (char *, u_int8_t *, size_t);
+extern int sockaddr2text (struct sockaddr *, char **);
+extern int text2sockaddr (char *, char *, struct sockaddr **);
+extern int sockaddr_len (struct sockaddr *);
+extern u_int8_t *sockaddr_data (struct sockaddr *);
extern int zero_test (const u_int8_t *, size_t);
extern int check_file_secrecy (char *, off_t *);