lintian doesn't like orphan packages with uploaders...
[debian/amanda] / common-src / sockaddr-util.c
index 84655ac7c7371420a6c70a8df2988af629e700ca..28da531239f1a3807a19ae004c11df5514b1971a 100644 (file)
@@ -1,9 +1,10 @@
 /*
- * Copyright (c) 2005-2008 Zmanda Inc.  All Rights Reserved.
+ * Copyright (c) 2007-2012 Zmanda, Inc.  All Rights Reserved.
  *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 as published
- * by the Free Software Foundation.
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
@@ -14,7 +15,7 @@
  * with this program; if not, write to the Free Software Foundation, Inc.,
  * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  *
- * Contact information: Zmanda Inc, 465 S Mathlida Ave, Suite 300
+ * Contact information: Zmanda Inc, 465 S. Mathilda Ave., Suite 300
  * Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
  *
  * Author: Dustin J. Mitchell <dustin@zmanda.com>
@@ -23,6 +24,7 @@
  * Utility routines for handling sockaddrs
  */
 
+#include "amanda.h"
 #include "sockaddr-util.h"
 
 void
@@ -84,12 +86,58 @@ str_sockaddr(
     {
        inet_ntop(AF_INET, &sa->sin.sin_addr.s_addr, ipstr, sizeof(ipstr));
     }
-    g_snprintf(mystr_sockaddr,sizeof(mystr_sockaddr),"%s.%d", ipstr, port);
+    g_snprintf(mystr_sockaddr,sizeof(mystr_sockaddr),"%s:%d", ipstr, port);
     mystr_sockaddr[sizeof(mystr_sockaddr)-1] = '\0';
 
     return mystr_sockaddr;
 }
 
+char *
+str_sockaddr_no_port(
+    sockaddr_union *sa)
+{
+#ifdef WORKING_IPV6
+    char ipstr[INET6_ADDRSTRLEN];
+#else
+    char ipstr[INET_ADDRSTRLEN];
+#endif
+
+#ifdef WORKING_IPV6
+    if ( SU_GET_FAMILY(sa) == AF_INET6) {
+       inet_ntop(AF_INET6, &sa->sin6.sin6_addr, ipstr, sizeof(ipstr));
+    } else
+#endif
+    {
+       inet_ntop(AF_INET, &sa->sin.sin_addr.s_addr, ipstr, sizeof(ipstr));
+    }
+    g_snprintf(mystr_sockaddr,sizeof(mystr_sockaddr),"%s", ipstr);
+    mystr_sockaddr[sizeof(mystr_sockaddr)-1] = '\0';
+
+    return mystr_sockaddr;
+}
+
+int
+str_to_sockaddr(
+       const char *src,
+       sockaddr_union *dst)
+{
+    int result;
+
+    g_debug("parsing %s", src);
+    /* try AF_INET first */
+    SU_INIT(dst, AF_INET);
+    if ((result = inet_pton(AF_INET, src, &dst->sin.sin_addr)) == 1)
+       return result;
+
+    /* otherwise try AF_INET6, if supported */
+#ifdef WORKING_IPV6
+    SU_INIT(dst, AF_INET6);
+    return inet_pton(AF_INET6, src, &dst->sin6.sin6_addr);
+#else
+    return result;
+#endif
+}
+
 /* Unmap a V4MAPPED IPv6 address into its equivalent IPv4 address.  The location
  * TMP is used to store the rewritten address, if necessary.  Returns a pointer
  * to the unmapped address.