+/**
+ * Copyright (C) 2011-2012 Juho Vähä-Herttua
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ */
+
#include <stdlib.h>
#include <string.h>
#include <assert.h>
memset(&saddr, 0, sizeof(saddr));
if (use_ipv6) {
struct sockaddr_in6 *sin6ptr = (struct sockaddr_in6 *)&saddr;
+ int v6only = 1;
/* Initialize sockaddr for bind */
sin6ptr->sin6_family = family;
sin6ptr->sin6_addr = in6addr_any;
sin6ptr->sin6_port = htons(*port);
+#ifndef WIN32
+ /* Make sure we only listen to IPv6 addresses */
+ setsockopt(server_fd, IPPROTO_IPV6, IPV6_V6ONLY,
+ (char *) &v6only, sizeof(v6only));
+#endif
+
socklen = sizeof(*sin6ptr);
ret = bind(server_fd, (struct sockaddr *)sin6ptr, socklen);
if (ret == -1) {
return NULL;
}
+int
+netutils_parse_address(int family, const char *src, void *dst, int dstlen)
+{
+ struct addrinfo *result;
+ struct addrinfo *ptr;
+ struct addrinfo hints;
+ int length;
+ int ret;
+
+ if (family != AF_INET && family != AF_INET6) {
+ return -1;
+ }
+ if (!src || !dst) {
+ return -1;
+ }
+
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = family;
+ hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
+
+ ret = getaddrinfo(src, NULL, &hints, &result);
+ if (ret != 0) {
+ return -1;
+ }
+
+ length = -1;
+ for (ptr=result; ptr!=NULL; ptr=ptr->ai_next) {
+ if (family == ptr->ai_family && (unsigned int)dstlen >= ptr->ai_addrlen) {
+ memcpy(dst, ptr->ai_addr, ptr->ai_addrlen);
+ length = ptr->ai_addrlen;
+ break;
+ }
+ }
+ freeaddrinfo(result);
+ return length;
+}