From 0970b1cad049a9f01b85df7ab1420e68c5573223 Mon Sep 17 00:00:00 2001 From: Gerrit Renker Date: Sun, 13 Jun 2010 11:30:47 +0200 Subject: [PATCH] sender: miscellaneous cleanups This collects various minor syntactical changes into a single patch: * replace indirection 'ut->sc->xxx' by 'sc->xxx' where possible; * more elegant way of duplicating sc->name string; * udp_delete_target() now takes sender_client as argument; * added IPv4-address:port example for udp_help(); * revised the definition of E_ADDRESS_LOOKUP (it means what it says) and replaced two inappropriate uses by assert(): in each case this error value was used to point to an unsupported adress family. But since the only supported network protocol is IP, if the case happens, it means a bug and should be marked as such. -- error.h | 2 - para_audioc |binary error.h | 2 - send_common.c | 2 - udp_recv.c | 4 --- udp_send.c | 64 +++++++++++++++++++++++++++++----------------------------- 4 files changed, 35 insertions(+), 37 deletions(-) --- error.h | 2 +- send_common.c | 2 +- udp_recv.c | 4 +--- udp_send.c | 64 +++++++++++++++++++++++++-------------------------- 4 files changed, 35 insertions(+), 37 deletions(-) diff --git a/error.h b/error.h index f687a4cb..94a44854 100644 --- a/error.h +++ b/error.h @@ -222,7 +222,7 @@ extern const char **para_errlist[]; #define NET_ERRORS \ PARA_ERROR(NAME_TOO_LONG, "name too long for struct sockaddr_un"), \ - PARA_ERROR(ADDRESS_LOOKUP, "address lookup / socket creation failed"), \ + PARA_ERROR(ADDRESS_LOOKUP, "can not resolve requested address"),\ PARA_ERROR(CHMOD, "failed to set socket mode"), \ PARA_ERROR(SENDMSG, "sendmsg() failed"), \ PARA_ERROR(RECVMSG, "recvmsg() failed"), \ diff --git a/send_common.c b/send_common.c index dea96081..c9ba5907 100644 --- a/send_common.c +++ b/send_common.c @@ -309,7 +309,7 @@ struct sender_client *accept_sender_client(struct sender_status *ss, fd_set *rfd ss->num_clients++; sc = para_calloc(sizeof(*sc)); sc->fd = fd; - sc->name = make_message("%s", remote_name(fd)); + sc->name = para_strdup(remote_name(fd)); sc->cq = cq_new(MAX_CQ_BYTES); para_list_add(&sc->node, &ss->client_list); add_close_on_fork_list(fd); diff --git a/udp_recv.c b/udp_recv.c index 5520c6fb..8e2fcf2f 100644 --- a/udp_recv.c +++ b/udp_recv.c @@ -142,6 +142,7 @@ static int mcast_receiver_setup(int fd, const char *iface) if (getsockname(fd, (struct sockaddr *)&ss, &sslen) < 0) goto err; + assert(ss.ss_family == AF_INET || ss.ss_family == AF_INET6); if (iface != NULL && id == 0) PARA_WARNING_LOG("could not resolve interface %s, using default", iface); @@ -179,9 +180,6 @@ static int mcast_receiver_setup(int fd, const char *iface) break; } return 0; - default: - PARA_ERROR_LOG("address family %d not supported", ss.ss_family); - return -E_ADDRESS_LOOKUP; } err: return -ERRNO_TO_PARA_ERROR(errno); diff --git a/udp_send.c b/udp_send.c index 71e85b79..880c83cf 100644 --- a/udp_send.c +++ b/udp_send.c @@ -63,14 +63,16 @@ static void udp_close_target(struct sender_client *sc) } } -static void udp_delete_target(struct udp_target *ut, const char *msg) +static void udp_delete_target(struct sender_client *sc, const char *msg) { - PARA_NOTICE_LOG("deleting %s (%s) from list\n", ut->sc->name, msg); - udp_close_target(ut->sc); + struct udp_target *ut = sc->private_data; + + PARA_NOTICE_LOG("deleting %s (%s) from list\n", sc->name, msg); + udp_close_target(sc); vss_del_fec_client(ut->fc); - list_del(&ut->sc->node); - free(ut->sc->name); - free(ut->sc); + list_del(&sc->node); + free(sc->name); + free(sc); free(ut); } @@ -99,6 +101,7 @@ static int mcast_sender_setup(struct sender_client *sc) if (getpeername(sc->fd, (struct sockaddr *)&ss, &sslen) < 0) goto err; + assert(ss.ss_family == AF_INET || ss.ss_family == AF_INET6); /* RFC 3493, 5.2: -1 means 'use kernel default' */ if (ttl < 0 || ttl > 255) @@ -144,9 +147,6 @@ static int mcast_sender_setup(struct sender_client *sc) if (setsockopt(sc->fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0) break; return 0; - default: - PARA_ERROR_LOG("address family %d not supported", ss.ss_family); - return -E_ADDRESS_LOOKUP; } err: return -ERRNO_TO_PARA_ERROR(errno); @@ -234,11 +234,10 @@ static struct sender_client *udp_lookup_target(struct sender_command_data *scd) static int udp_com_delete(struct sender_command_data *scd) { - struct udp_target *ut = sc->private_data; struct sender_client *sc = udp_lookup_target(scd); if (sc) { - udp_delete_target(ut, "com_delete"); + udp_delete_target(sc, "com_delete"); return 1; } PARA_NOTICE_LOG("not deleting non-existing target '%s'\n", scd->host); @@ -257,12 +256,13 @@ static int udp_init_fec(struct sender_client *sc) } /** Check and clear socket error if any. */ -static int udp_check_socket_state(struct udp_target *ut) +static int udp_check_socket_state(struct sender_client *sc) { + struct udp_target *ut = sc->private_data; int ret; socklen_t errlen = sizeof(ret); - if (getsockopt(ut->sc->fd, SOL_SOCKET, SO_ERROR, &ret, &errlen) < 0) { + if (getsockopt(sc->fd, SOL_SOCKET, SO_ERROR, &ret, &errlen) < 0) { PARA_ERROR_LOG("SO_ERROR failed: %s\n", strerror(ret)); return 0; } else if (ret == 0) { @@ -282,7 +282,7 @@ static int udp_check_socket_state(struct udp_target *ut) */ PARA_NOTICE_LOG("Evicting %s after %d seconds " "of connection errors.\n", - ut->sc->name, (int)dist); + sc->name, (int)dist); } } return -ERRNO_TO_PARA_ERROR(ret); @@ -290,14 +290,13 @@ static int udp_check_socket_state(struct udp_target *ut) static int udp_send_fec(struct sender_client *sc, char *buf, size_t len) { - struct udp_target *ut = sc->private_data; int ret; if (sender_status == SENDER_OFF) return 0; - if (len == 0 && !cq_peek(ut->sc->cq)) + if (len == 0 && !cq_peek(sc->cq)) return 0; - ret = udp_check_socket_state(ut); + ret = udp_check_socket_state(sc); if (ret < 0) goto fail; ret = send_queued_chunks(sc->fd, sc->cq); @@ -324,7 +323,7 @@ static int udp_send_fec(struct sender_client *sc, char *buf, size_t len) } return 1; fail: - udp_delete_target(ut, para_strerror(-ret)); + udp_delete_target(sc, para_strerror(-ret)); return ret; } @@ -347,30 +346,30 @@ static int udp_com_add(struct sender_command_data *scd) ut->fcp.init_fec = udp_init_fec; ut->fcp.send_fec = udp_send_fec; - ut->sc->private_data = ut; - ut->sc->fd = -1; + sc->private_data = ut; + sc->fd = -1; ret = para_connect_simple(IPPROTO_UDP, scd->host, scd->port); if (ret < 0) goto err; - ut->sc->fd = ret; + sc->fd = ret; - ret = mcast_sender_setup(ut->sc); + ret = mcast_sender_setup(sc); if (ret < 0) goto err; - ret = mark_fd_nonblocking(ut->sc->fd); + ret = mark_fd_nonblocking(sc->fd); if (ret < 0) goto err; - ut->sc->name = para_strdup(remote_name(ut->sc->fd)); - PARA_INFO_LOG("adding to target list (%s)\n", ut->sc->name); - ut->fc = vss_add_fec_client(ut->sc, &ut->fcp); - para_list_add(&ut->sc->node, &targets); + sc->name = para_strdup(remote_name(sc->fd)); + PARA_INFO_LOG("adding to target list (%s)\n", sc->name); + ut->fc = vss_add_fec_client(sc, &ut->fcp); + para_list_add(&sc->node, &targets); return 1; err: - if (ut->sc->fd >= 0) - close(ut->sc->fd); + if (sc->fd >= 0) + close(sc->fd); PARA_NOTICE_LOG("failed to set up %s:%d (%s)- not adding it\n", scd->host, scd->port, para_strerror(-ret)); - free(ut->sc); + free(sc); free(ut); return ret; } @@ -382,8 +381,8 @@ static char *udp_info(void) list_for_each_entry(sc, &targets, node) { struct udp_target *ut = sc->private_data; - char *tmp = make_message("%s%s/%u:%u:%u ", tgts ? : "", - ut->sc->name, + char *tmp = make_message("%s%s/%u:%u:%u ", + tgts ? : "", sc->name, ut->fcp.max_slice_bytes, ut->fcp.data_slices_per_group, ut->fcp.slices_per_group @@ -432,6 +431,7 @@ static char *udp_help(void) " add [FF05::42]:1500 (IPv6 multicast)\n" " add [::1] (IPv6 localhost/default port)\n" " delete myhost.net (host with port wildcard)\n" + " delete 10.1.2.3:456 (IPv4 with explicit port)\n" " delete [badc0de::1] (IPv6 with port wildcard)\n" ); } -- 2.39.5