summaryrefslogtreecommitdiff
path: root/common/sock_any.c
diff options
context:
space:
mode:
authorStef Walter <stef@memberwebs.com>2004-07-22 16:55:14 +0000
committerStef Walter <stef@memberwebs.com>2004-07-22 16:55:14 +0000
commit6be6d1dd25f2e7f2f1de6c0091e9aeae2ea1918c (patch)
tree1454a6bb0ec09fb6b105171d7aab29a217b30a39 /common/sock_any.c
parent166f69df6dd704626c1b09ae60145956435b67e1 (diff)
- Fixed uninitialized memory bug
- Imported updated sock_any and hash code
Diffstat (limited to 'common/sock_any.c')
-rw-r--r--common/sock_any.c91
1 files changed, 84 insertions, 7 deletions
diff --git a/common/sock_any.c b/common/sock_any.c
index acac8ee..0018318 100644
--- a/common/sock_any.c
+++ b/common/sock_any.c
@@ -1,3 +1,40 @@
+/*
+ * Copyright (c) 2004, Nate Nielsen
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above
+ * copyright notice, this list of conditions and the
+ * following disclaimer.
+ * * Redistributions in binary form must reproduce the
+ * above copyright notice, this list of conditions and
+ * the following disclaimer in the documentation and/or
+ * other materials provided with the distribution.
+ * * The names of contributors to this software may not be
+ * used to endorse or promote products derived from this
+ * software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+ * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ *
+ *
+ * CONTRIBUTORS
+ * Nate Nielsen <nielsen@memberwebs.com>
+ *
+ */
#include <stdlib.h>
#include <errno.h>
@@ -10,12 +47,15 @@
#include <arpa/inet.h>
-int sock_any_pton(const char* addr, struct sockaddr_any* any, int defport)
+#define LOCALHOST_ADDR 0x7F000001
+
+int sock_any_pton(const char* addr, struct sockaddr_any* any, int opts)
{
size_t l;
- char buf[256]; /* TODO: Use a constant */
+ char buf[256];
char* t;
char* t2;
+ int defport = (opts & 0xFFFF);
memset(any, 0, sizeof(*any));
@@ -32,13 +72,35 @@ int sock_any_pton(const char* addr, struct sockaddr_any* any, int defport)
if(l < PORT_MIN || l > PORT_MAX || addr[l] != 0)
break;
- port = strtol(t, &t2, 10);
+ port = strtol(addr, &t2, 10);
if(*t2 || port <= 0 || port >= 65536)
break;
- any->s.in.sin_family = AF_INET;
- any->s.in.sin_port = htons((unsigned short)(port <= 0 ? defport : port));
- any->s.in.sin_addr.s_addr = 0;
+ any->s.in.sin_port = htons(port);
+
+ /* Fill in the type based on defaults */
+#ifdef HAVE_INET6
+ if(opts & SANY_OPT_DEFINET6)
+ any->s.in.sin_family = AF_INET6;
+ else
+#endif
+ any->s.in.sin_family = AF_INET;
+
+ /* Fill in the address based on defaults */
+ if(opts & SANY_OPT_DEFLOCAL)
+ {
+#ifdef HAVE_INET6
+ if(opts & SANY_OPT_DEFINET6)
+ memcpy(&(any->s.in.sin6_addr), &in6addr_loopback, sizeof(struct in6_addr));
+ else
+#endif
+ any->s.in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ }
+
+ /*
+ * Note the 'any' option is the default since we zero out
+ * the entire structure above.
+ */
any->namelen = sizeof(any->s.in);
return AF_INET;
@@ -237,9 +299,10 @@ int sock_any_pton(const char* addr, struct sockaddr_any* any, int defport)
return -1;
}
-int sock_any_ntop(struct sockaddr_any* any, char* addr, size_t addrlen)
+int sock_any_ntop(struct sockaddr_any* any, char* addr, size_t addrlen, int opts)
{
int len = 0;
+ int port = 0;
switch(any->s.a.sa_family)
{
@@ -257,12 +320,14 @@ int sock_any_ntop(struct sockaddr_any* any, char* addr, size_t addrlen)
case AF_INET:
if(inet_ntop(any->s.a.sa_family, &(any->s.in.sin_addr), addr, addrlen) == NULL)
return -1;
+ port = ntohs(any->s.in.sin_port);
break;
#ifdef HAVE_INET6
case AF_INET6:
if(inet_ntop(any->s.a.sa_family, &(any->s.in6.sin6_addr), addr, addrlen) == NULL)
return -1;
+ port = ntohs(any->s.in6.sin6_port);
break;
#endif
@@ -271,5 +336,17 @@ int sock_any_ntop(struct sockaddr_any* any, char* addr, size_t addrlen)
return -1;
}
+ if(!(opts & SANY_OPT_NOPORT) && port != 0)
+ {
+ strncat(addr, ":", addrlen);
+ addr[addrlen - 1] = 0;
+
+ len = strlen(addr);
+ addr += len;
+ addrlen -= len;
+
+ snprintf(addr, addrlen, "%d", port);
+ }
+
return 0;
}