]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
add src_port parameter to fr_socket_client_udp()
authorAlan T. DeKok <aland@freeradius.org>
Tue, 22 Aug 2017 11:46:36 +0000 (13:46 +0200)
committerAlan T. DeKok <aland@freeradius.org>
Wed, 23 Aug 2017 11:34:05 +0000 (07:34 -0400)
so that we know what source port we're bound to

src/include/libradius.h
src/lib/util/socket.c
src/main/radsnmp.c
src/modules/rlm_linelog/rlm_linelog.c
src/modules/rlm_logtee/rlm_logtee.c
src/modules/rlm_radius/rlm_radius_udp.c

index 0ec28045a872bf255f8245020cfe1da78ce2a564..5482953083e77f4fb266fd07f4d88621dab115b3 100644 (file)
@@ -251,7 +251,7 @@ unsigned int        fr_fifo_num_elements(fr_fifo_t *fi);
 
 bool           fr_socket_is_valid_proto(int proto);
 int            fr_socket_client_unix(char const *path, bool async);
-int            fr_socket_client_udp(fr_ipaddr_t const *src_ipaddr, fr_ipaddr_t const *dst_ipaddr,
+int            fr_socket_client_udp(fr_ipaddr_t const *src_ipaddr, uint16_t *src_port, fr_ipaddr_t const *dst_ipaddr,
                                     uint16_t dst_port, bool async);
 int            fr_socket_client_tcp(fr_ipaddr_t const *src_ipaddr, fr_ipaddr_t const *dst_ipaddr,
                                     uint16_t dst_port, bool async);
index 9e32ef9ec06b59caf327c32a51ae6a40c1ce79f0..59a7cd6a2d2696252fb447978719f8aa986685ae 100644 (file)
@@ -338,7 +338,7 @@ int fr_socket_client_unix(UNUSED char const *path, UNUSED bool async)
  *
  * The following code demonstrates using this function with a connection timeout:
  @code {.c}
-   sockfd = fr_socket_client_udp(NULL, ipaddr, port, true);
+   sockfd = fr_socket_client_udp(NULL, NULL, ipaddr, port, true);
    if (sockfd < 0) {
        fr_perror();
        exit(1);
@@ -355,6 +355,7 @@ int fr_socket_client_unix(UNUSED char const *path, UNUSED bool async)
  *
  * @param src_ipaddr   to bind socket to, may be NULL if socket is not bound to any specific
  *                     address.
+ * @param[out] src_port        The source port we were bound to.
  * @param dst_ipaddr   Where to send datagrams.
  * @param dst_port     Where to send datagrams.
  * @param async                Whether to set the socket to nonblocking, allowing use of
@@ -363,11 +364,13 @@ int fr_socket_client_unix(UNUSED char const *path, UNUSED bool async)
  *     - FD on success.
  *     - -1 on failure.
  */
-int fr_socket_client_udp(fr_ipaddr_t const *src_ipaddr, fr_ipaddr_t const *dst_ipaddr, uint16_t dst_port, bool async)
+int fr_socket_client_udp(fr_ipaddr_t const *src_ipaddr, uint16_t *src_port, fr_ipaddr_t const *dst_ipaddr, uint16_t dst_port, bool async)
 {
        int                     sockfd;
        struct sockaddr_storage salocal;
        socklen_t               salen;
+       fr_ipaddr_t             my_ipaddr;
+       uint16_t                my_port;
 
        if (!dst_ipaddr) return -1;
 
@@ -425,6 +428,14 @@ int fr_socket_client_udp(fr_ipaddr_t const *src_ipaddr, fr_ipaddr_t const *dst_i
        }
 #endif
 
+       if (getsockname(sockfd, (struct sockaddr *) &salocal, &salen) < 0) {
+               fr_strerror_printf("Failed getting socket name: %s", fr_syserror(errno));
+               return -1;
+       }
+
+       if (fr_ipaddr_from_sockaddr(&salocal, salen, &my_ipaddr, &my_port) < 0) return -1;
+       if (src_port) *src_port = my_port;
+
        if (connect(sockfd, (struct sockaddr *) &salocal, salen) < 0) {
                /*
                 *      POSIX says the only time we will get this,
index d627ea396b144cd10f06eb3f55bc2cf88862e364..51487f25eb7ad4cb33f53127105b2ecc96f32a41 100644 (file)
@@ -1133,7 +1133,7 @@ int main(int argc, char **argv)
 
        default:
        case IPPROTO_UDP:
-               sockfd = fr_socket_client_udp(NULL, &conf->server_ipaddr, conf->server_port, true);
+               sockfd = fr_socket_client_udp(NULL, NULL, &conf->server_ipaddr, conf->server_port, true);
                break;
        }
        if (sockfd < 0) {
index 36811bff6b02885808b7de83e7318f554fe9ae9c..b66c78a8b1cf9103538ebcb3e00662ae4b77162d 100644 (file)
@@ -235,7 +235,7 @@ static void *mod_conn_create(TALLOC_CTX *ctx, void *instance, struct timeval con
                        DEBUG2("Opening UDP connection to %s:%u", buff, inst->udp.port);
                }
 
-               sockfd = fr_socket_client_udp(NULL, &inst->udp.dst_ipaddr, inst->udp.port, true);
+               sockfd = fr_socket_client_udp(NULL, NULL, &inst->udp.dst_ipaddr, inst->udp.port, true);
                if (sockfd < 0) {
                        PERROR("Failed opening UDP socket");
                        return NULL;
index 1edd7c0e60eecd382d501aa21b9a707a0761ad66..7e6465f64e6544a5821ca8fac656a6675322b14a 100644 (file)
@@ -376,7 +376,7 @@ static fr_connection_state_t _logtee_conn_init(int *fd_out, void *uctx)
        case LOGTEE_DST_UDP:
                DEBUG2("Opening UDP connection to %pV:%u",
                       fr_box_ipaddr(inst->udp.dst_ipaddr), inst->udp.port);
-               fd = fr_socket_client_udp(NULL, &inst->udp.dst_ipaddr, inst->udp.port, true);
+               fd = fr_socket_client_udp(NULL, NULL, &inst->udp.dst_ipaddr, inst->udp.port, true);
                if (fd < 0) return FR_CONNECTION_STATE_FAILED;
                break;
 
index 68ffd2620ae1ad1d1936b822cf92ddaf3fcac5b1..e3cef613787353e29801f5e781e934ffa4f968fa 100644 (file)
@@ -1423,25 +1423,14 @@ static fr_connection_state_t conn_init(int *fd_out, void *uctx)
 
        /*
         *      Open the outgoing socket.
-        *
-        *      @todo - pass src_port, and remove later call to fr_socket_bind()
-        *      which does return the src_port, but doesn't set the "don't fragment" bit.
         */
-       fd = fr_socket_client_udp(&c->src_ipaddr, &c->dst_ipaddr, c->dst_port, true);
+       fd = fr_socket_client_udp(&c->src_ipaddr, &c->src_port, &c->dst_ipaddr, c->dst_port, true);
        if (fd < 0) {
                DEBUG("%s failed opening socket: %s",
                      c->inst->parent->name, fr_strerror());
                return FR_CONNECTION_STATE_FAILED;
        }
 
-#if 0
-       if (fr_socket_bind(fd, &io->src_ipaddr, &io->src_port, inst->interface) < 0) {
-               DEBUG("Failed binding RADIUS client UDP socket: %s FD %d %pV port %u interface %s", fr_strerror(), fd, fr_box_ipaddr(io->src_ipaddr),
-                       io->src_port, inst->interface);
-               return FR_CONNECTION_STATE_FAILED;
-       }
-#endif
-
        /*
         *      Set the connection name.
         */