]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
don't malloc / memcpy the packet if we're replicating
authorAlan T. DeKok <aland@freeradius.org>
Wed, 2 Aug 2017 13:58:18 +0000 (15:58 +0200)
committerAlan T. DeKok <aland@freeradius.org>
Wed, 2 Aug 2017 15:06:33 +0000 (17:06 +0200)
src/modules/rlm_radius/rlm_radius_udp.c

index d0adddf12da3a8fc0928a606f226b8384e149a91..14b70e92d20e26a3591082101485df1a48d9ead0 100644 (file)
@@ -407,6 +407,12 @@ static void conn_writable(fr_event_list_t *el, int fd, UNUSED int flags, void *u
                                              request->packet->vps);
                if (packet_len <= 0) break;
 
+               /*
+                *      Might have been sent and then given up
+                *      on... free the raw data.
+                */
+               if (u->packet) TALLOC_FREE(u->packet);
+
                /*
                 *      Ad Proxy-State to the tail end of the packet.
                 *      We need to add it here, and NOT in
@@ -474,17 +480,22 @@ static void conn_writable(fr_event_list_t *el, int fd, UNUSED int flags, void *u
 
                // @todo - if debug >= 3, print out hex of the packet.
 
-               MEM(u->packet = talloc_memdup(u, c->buffer, packet_len));
-               u->packet_len = packet_len;
-
                /*
                 *      Write the packet to the socket.  If it blocks,
                 *      stop dequeueing packets.
                 */
-               rcode = write(fd, u->packet, u->packet_len);
+               rcode = write(fd, c->buffer, packet_len);
                if (rcode < 0) {
-                       if (errno == EWOULDBLOCK) break;
+                       if (errno == EWOULDBLOCK) {
+                               MEM(u->packet = talloc_memdup(u, c->buffer, packet_len));
+                               u->packet_len = packet_len;
+                               break;
+                       }
 
+                       /*
+                        *      We have to re-encode the packet, so
+                        *      don't bother copying it to 'u'.
+                        */
                        conn_error(el, fd, 0, errno, c);
                        return;
                }
@@ -499,6 +510,12 @@ static void conn_writable(fr_event_list_t *el, int fd, UNUSED int flags, void *u
                        continue;
                }
 
+               /*
+                *      Only copy the packet if we're not replicating
+                */
+               MEM(u->packet = talloc_memdup(u, c->buffer, packet_len));
+               u->packet_len = packet_len;
+
                /*
                 *      Start the retransmission timers.
                 */