From: Alan T. DeKok Date: Thu, 16 Aug 2018 16:45:44 +0000 (-0400) Subject: move set name out of instantiate for RADIUS X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e7861c91aef5bc861c2e00a45d4e2e1cf053cea8;p=thirdparty%2Ffreeradius-server.git move set name out of instantiate for RADIUS and stop doing connect() here --- diff --git a/src/modules/proto_radius/proto_radius_udp.c b/src/modules/proto_radius/proto_radius_udp.c index e019a8414da..46f4537e803 100644 --- a/src/modules/proto_radius/proto_radius_udp.c +++ b/src/modules/proto_radius/proto_radius_udp.c @@ -134,7 +134,7 @@ static ssize_t mod_read(void *instance, void **packet_ctx, fr_time_t **recv_time &address->dst_ipaddr, &address->dst_port, &address->if_index, ×tamp); if (data_size < 0) { - DEBUG2("proto_radius_udp got read error %zd: %s", data_size, fr_strerror()); + DEBUG2("proto_radius_udp got read error: %s", fr_strerror()); return data_size; } @@ -321,6 +321,7 @@ static int mod_open(void *instance, UNUSED void const *master_instance) uint16_t port = inst->port; CONF_SECTION *server_cs; CONF_ITEM *ci; + char dst_buf[128]; sockfd = fr_socket_server_udp(&inst->ipaddr, &port, inst->port_name, true); if (sockfd < 0) { @@ -348,27 +349,6 @@ static int mod_open(void *instance, UNUSED void const *master_instance) goto error; } - /* - * Connect to the client for child sockets. - */ - if (inst->connection) { - socklen_t salen; - struct sockaddr_storage src; - - if (fr_ipaddr_to_sockaddr(&inst->connection->src_ipaddr, inst->connection->src_port, - &src, &salen) < 0) { - close(sockfd); - ERROR("Failed getting IP address"); - goto error; - } - - if (connect(sockfd, (struct sockaddr *) &src, salen) < 0) { - close(sockfd); - ERROR("Failed in connect: %s", fr_syserror(errno)); - goto error; - } - } - inst->sockfd = sockfd; ci = cf_parent(inst->cs); /* listen { ... } */ @@ -378,6 +358,23 @@ static int mod_open(void *instance, UNUSED void const *master_instance) server_cs = cf_item_to_section(ci); + /* + * Get our name. + */ + if (fr_ipaddr_is_inaddr_any(&inst->ipaddr)) { + if (inst->ipaddr.af == AF_INET) { + strlcpy(dst_buf, "*", sizeof(dst_buf)); + } else { + rad_assert(inst->ipaddr.af == AF_INET6); + strlcpy(dst_buf, "::", sizeof(dst_buf)); + } + } else { + fr_value_box_snprint(dst_buf, sizeof(dst_buf), fr_box_ipaddr(inst->ipaddr), 0); + } + + inst->name = talloc_typed_asprintf(inst, "proto udp ipaddr %s port %u", + dst_buf, inst->port); + // @todo - also print out auth / acct / coa, etc. DEBUG("Listening on radius address %s bound to virtual server %s", inst->name, cf_section_name2(server_cs)); @@ -397,6 +394,42 @@ static int mod_fd(void const *instance) return inst->sockfd; } +/** Set the file descriptor for this socket. + * + * @param[in] instance of the RADIUS UDP I/O path. + * @param[in] fd the FD to set + */ +static int mod_fd_set(void *instance, int fd) +{ + proto_radius_udp_t *inst = talloc_get_type_abort(instance, proto_radius_udp_t); + char dst_buf[128], src_buf[128]; + + inst->sockfd = fd; + + /* + * Get our name. + */ + if (fr_ipaddr_is_inaddr_any(&inst->ipaddr)) { + if (inst->ipaddr.af == AF_INET) { + strlcpy(dst_buf, "*", sizeof(dst_buf)); + } else { + rad_assert(inst->ipaddr.af == AF_INET6); + strlcpy(dst_buf, "::", sizeof(dst_buf)); + } + } else { + fr_value_box_snprint(dst_buf, sizeof(dst_buf), fr_box_ipaddr(inst->ipaddr), 0); + } + + fr_value_box_snprint(src_buf, sizeof(src_buf), fr_box_ipaddr(inst->connection->src_ipaddr), 0); + + inst->name = talloc_typed_asprintf(inst, "proto udp from client %s port %u to ipaddr %s port %u", + src_buf, inst->connection->src_port, dst_buf, inst->port); + + ERROR("UDP is %s", inst->name); + + return 0; +} + static int mod_compare(UNUSED void const *instance, void const *one, void const *two) { int rcode; @@ -425,41 +458,6 @@ static char const *mod_name(void *instance) return inst->name; } -static int mod_instantiate(void *instance, UNUSED CONF_SECTION *cs) -{ - proto_radius_udp_t *inst = talloc_get_type_abort(instance, proto_radius_udp_t); - char dst_buf[128]; - - /* - * Get our name. - */ - if (fr_ipaddr_is_inaddr_any(&inst->ipaddr)) { - if (inst->ipaddr.af == AF_INET) { - strlcpy(dst_buf, "*", sizeof(dst_buf)); - } else { - rad_assert(inst->ipaddr.af == AF_INET6); - strlcpy(dst_buf, "::", sizeof(dst_buf)); - } - } else { - fr_value_box_snprint(dst_buf, sizeof(dst_buf), fr_box_ipaddr(inst->ipaddr), 0); - } - - if (!inst->connection) { - inst->name = talloc_typed_asprintf(inst, "proto udp server %s port %u", - dst_buf, inst->port); - - } else { - char src_buf[128]; - - fr_value_box_snprint(src_buf, sizeof(src_buf), fr_box_ipaddr(inst->connection->src_ipaddr), 0); - - inst->name = talloc_typed_asprintf(inst, "proto udp from client %s port %u to server %s port %u", - src_buf, inst->connection->src_port, dst_buf, inst->port); - } - - return 0; -} - static int mod_bootstrap(void *instance, CONF_SECTION *cs) { @@ -585,7 +583,6 @@ fr_app_io_t proto_radius_udp = { .inst_size = sizeof(proto_radius_udp_t), // .detach = mod_detach, .bootstrap = mod_bootstrap, - .instantiate = mod_instantiate, .default_message_size = 4096, .track_duplicates = true, @@ -595,6 +592,7 @@ fr_app_io_t proto_radius_udp = { .write = mod_write, .close = mod_close, .fd = mod_fd, + .fd_set = mod_fd_set, .compare = mod_compare, .connection_set = mod_connection_set, .network_get = mod_network_get,