]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - udp_send.c
URL/UDPv6 support for the UDP sender.
[paraslash.git] / udp_send.c
index 140458e7ed9b1d4e3a03d05c2e26a6b36d66db54..ca4d8cf291c60f38f64ed488716019c1635a75c7 100644 (file)
 #include "close_on_fork.h"
 #include "chunk_queue.h"
 
-/** Convert in_addr to ascii. */
-#define TARGET_ADDR(oc) inet_ntoa((oc)->addr)
-
 /** Describes one entry in the list of targets for the udp sender. */
 struct udp_target {
-       /** Address info. */
-       struct in_addr addr;
        /** The position of this target in the list of targets. */
        struct list_head node;
+       /** The hostname (DNS name or IPv4/v6 address string). */
+       char host[MAX_HOSTLEN];
        /** The UDP port. */
        int port;
        /** The socket fd. */
@@ -61,7 +58,7 @@ static void udp_close_target(struct udp_target *ut)
 
 static void udp_delete_target(struct udp_target *ut, const char *msg)
 {
-       PARA_NOTICE_LOG("deleting %s:%d (%s) from list\n", TARGET_ADDR(ut),
+       PARA_NOTICE_LOG("deleting %s#%d (%s) from list\n", ut->host,
                ut->port, msg);
        udp_close_target(ut);
        list_del(&ut->node);
@@ -155,7 +152,7 @@ static int udp_init_session(struct udp_target *ut)
        if (ut->fd >= 0) /* nothing to do */
                return 0;
 
-       ret = makesock(AF_UNSPEC, IPPROTO_UDP, 0, TARGET_ADDR(ut), ut->port);
+       ret = makesock(AF_UNSPEC, IPPROTO_UDP, 0, ut->host, ut->port);
        if (ret < 0)
                return ret;
        ut->fd = ret;
@@ -176,7 +173,7 @@ static int udp_init_session(struct udp_target *ut)
        }
        add_close_on_fork_list(ut->fd);
        ut->cq = cq_new(UDP_CQ_BYTES);
-       PARA_NOTICE_LOG("sending to udp %s:%d\n", TARGET_ADDR(ut), ut->port);
+       PARA_NOTICE_LOG("sending to udp %s#%d\n", ut->host, ut->port);
        return 1;
 }
 
@@ -306,33 +303,34 @@ static int udp_com_off(__a_unused struct sender_command_data *scd)
 
 static int udp_com_delete(struct sender_command_data *scd)
 {
-       char *a = para_strdup(inet_ntoa(scd->addr));
        struct udp_target *ut, *tmp;
+
        list_for_each_entry_safe(ut, tmp, &targets, node) {
-               if (scd->port != ut->port)
+               /* Unspecified port means wildcard port match */
+               if (scd->port > 0 && scd->port != ut->port)
                        continue;
-               if (strcmp(TARGET_ADDR(ut), a))
+               if (strcmp(ut->host, scd->host))
                        continue;
                udp_delete_target(ut, "com_delete");
        }
        return 1;
 }
 
-static void udp_add_target(int port, struct in_addr *addr)
+static void udp_add_target(const char *host, int port)
 {
        struct udp_target *ut = para_calloc(sizeof(struct udp_target));
-       ut->port = port;
-       ut->addr = *addr;
+
+       strncpy(ut->host, host, sizeof(ut->host));
+       ut->port = port > 0 ? port : conf.udp_default_port_arg;
        ut->fd = -1; /* not yet connected */
-       PARA_INFO_LOG("adding to target list (%s:%d)\n",
-               TARGET_ADDR(ut), ut->port);
+       PARA_INFO_LOG("adding to target list (%s#%d)\n",
+                     ut->host, ut->port);
        para_list_add(&ut->node, &targets);
 }
 
 static int udp_com_add(struct sender_command_data *scd)
 {
-       int port = (scd->port > 0)? scd->port : conf.udp_default_port_arg;
-       udp_add_target(port, &scd->addr);
+       udp_add_target(scd->host, scd->port);
        return 1;
 }
 
@@ -342,8 +340,10 @@ static char *udp_info(void)
        char *ret, *tgts = NULL;
 
        list_for_each_entry(ut, &targets, node) {
-               char *tmp = make_message("%s%s:%d ", tgts? tgts : "",
-                       TARGET_ADDR(ut), ut->port);
+               bool is_v6 = strchr(ut->host, ':') != NULL;
+               char *tmp = make_message("%s%s%s%s:%d ", tgts ? : "",
+                                        is_v6 ? "[" : "", ut->host,
+                                        is_v6 ? "]" : "", ut->port);
                free(tgts);
                tgts = tmp;
        }
@@ -362,40 +362,30 @@ static char *udp_info(void)
 
 static void udp_init_target_list(void)
 {
-       int i;
+       char    host[MAX_HOSTLEN];
+       int     port, i;
 
        INIT_LIST_HEAD(&targets);
-       for (i = 0; i < conf.udp_target_given; i++) {
-               char *arg = para_strdup(conf.udp_target_arg[i]);
-               char *p = strchr(arg, ':');
-               int port;
-               struct in_addr addr;
-
-               if (!p)
-                       goto err;
-               *p = '\0';
-               if (!inet_pton(AF_INET, arg, &addr))
-                       goto err;
-               port = atoi(++p);
-               if (port < 0 || port > 65535)
-                       port = conf.udp_default_port_arg;
-               udp_add_target(port, &addr);
-               goto success;
-err:
-               PARA_CRIT_LOG("syntax error for udp target option "
-                       "#%d, ignoring\n", i);
-success:
-               free(arg);
-               continue;
-       }
+       for (i = 0; i < conf.udp_target_given; i++)
+               if (parse_url(conf.udp_target_arg[i], host,
+                                       sizeof(host), &port) == NULL)
+                       PARA_CRIT_LOG("syntax error for udp target option "
+                                     "#%d, ignoring\n", i);
+               else
+                       udp_add_target(host, port);
 }
 
 static char *udp_help(void)
 {
        return make_message(
                "usage: {on|off}\n"
-               "usage: {add|delete} IP port\n"
-               "example: add 224.0.1.38 8000 (multicast streaming)\n"
+               "usage: {add|delete} host[:port]\n"
+               "examples: add 224.0.1.38:1500  (IPv4 multicast)\n"
+               "          add 10.10.1.42       (using default port)\n"
+               "          add [FF05::42]:1500  (IPv6 multicast)\n"
+               "          add [::1]            (IPv6 localhost/default port)\n"
+               "          delete myhost.net    (host with port wildcard)\n"
+               "          delete [badc0de::1]  (IPv6 with port wildcard)\n"
        );
 }