Convert rad_alloc and rad_free to use talloc
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Mon, 18 Feb 2013 22:12:38 +0000 (17:12 -0500)
committerAlan T. DeKok <aland@freeradius.org>
Tue, 19 Feb 2013 22:04:56 +0000 (17:04 -0500)
17 files changed:
src/include/libradius.h
src/lib/dhcp.c
src/lib/radius.c
src/lib/tcp.c
src/lib/vqp.c
src/main/command.c
src/main/detail.c
src/main/dhclient.c
src/main/listen.c
src/main/process.c
src/main/radclient.c
src/main/radsniff.c
src/main/realms.c
src/main/tls_listen.c
src/main/util.c
src/modules/rlm_eap/radeapclient.c
src/modules/rlm_replicate/rlm_replicate.c

index e22fe19..afecdab 100644 (file)
@@ -376,8 +376,8 @@ int         rad_sign(RADIUS_PACKET *packet, const RADIUS_PACKET *original,
                         const char *secret);
 
 int rad_digest_cmp(const uint8_t *a, const uint8_t *b, size_t length);
-RADIUS_PACKET  *rad_alloc(int newvector);
-RADIUS_PACKET  *rad_alloc_reply(RADIUS_PACKET *);
+RADIUS_PACKET  *rad_alloc(TALLOC_CTX *ctx, int newvector);
+RADIUS_PACKET  *rad_alloc_reply(TALLOC_CTX *ctx, RADIUS_PACKET *);
 void           rad_free(RADIUS_PACKET **);
 int            rad_pwencode(char *encpw, size_t *len, const char *secret,
                             const uint8_t *vector);
index 994f002..486e865 100644 (file)
@@ -224,7 +224,7 @@ RADIUS_PACKET *fr_dhcp_recv(int sockfd)
        int port;
        uint8_t                 *code;
 
-       packet = rad_alloc(0);
+       packet = rad_alloc(NULL, 0);
        if (!packet) {
                fr_strerror_printf("Failed allocating packet");
                return NULL;
index 35a60ca..51fe280 100644 (file)
@@ -2531,11 +2531,11 @@ RADIUS_PACKET *rad_recv(int fd, int flags)
        /*
         *      Allocate the new request data structure
         */
-       if ((packet = malloc(sizeof(*packet))) == NULL) {
+       packet = rad_alloc(NULL, 0);
+       if (!packet) {
                fr_strerror_printf("out of memory");
                return NULL;
        }
-       memset(packet, 0, sizeof(*packet));
 
        if (flags & 0x02) {
                sock_flags = MSG_PEEK;
@@ -2552,7 +2552,7 @@ RADIUS_PACKET *rad_recv(int fd, int flags)
        if (data_len < 0) {
                fr_strerror_printf("Error receiving packet: %s", strerror(errno));
                /* packet->data is NULL */
-               free(packet);
+               rad_free(&packet);
                return NULL;
        }
        packet->data_len = data_len; /* unsigned vs signed */
@@ -2565,7 +2565,7 @@ RADIUS_PACKET *rad_recv(int fd, int flags)
        if (packet->data_len > MAX_PACKET_LEN) {
                fr_strerror_printf("Discarding packet: Larger than RFC limitation of 4096 bytes.");
                /* packet->data is NULL */
-               free(packet);
+               rad_free(&packet);
                return NULL;
        }
 
@@ -2577,7 +2577,7 @@ RADIUS_PACKET *rad_recv(int fd, int flags)
         */
        if ((packet->data_len == 0) || !packet->data) {
                fr_strerror_printf("Empty packet: Socket is not ready.");
-               free(packet);
+               rad_free(&packet);
                return NULL;
        }
 
@@ -4242,18 +4242,22 @@ uint32_t fr_rand(void)
 }
 
 
-/**
- * @brief Allocate a new RADIUS_PACKET
+/** Allocate a new RADIUS_PACKET
+ *
+ * @param ctx the context in which the packet is allocated. May be NULL if
+ *     the packet is not associated with a REQUEST.
+ * @param newvector if TRUE a new request authenticator will be generated.
+ * @return a new RADIUS_PACKET or NULL on error.
  */
-RADIUS_PACKET *rad_alloc(int newvector)
+RADIUS_PACKET *rad_alloc(TALLOC_CTX *ctx, int newvector)
 {
        RADIUS_PACKET   *rp;
 
-       if ((rp = malloc(sizeof(RADIUS_PACKET))) == NULL) {
+       rp = talloc_zero(ctx, RADIUS_PACKET);
+       if (!rp) {
                fr_strerror_printf("out of memory");
                return NULL;
        }
-       memset(rp, 0, sizeof(*rp));
        rp->id = -1;
        rp->offset = -1;
 
@@ -4276,13 +4280,20 @@ RADIUS_PACKET *rad_alloc(int newvector)
        return rp;
 }
 
-RADIUS_PACKET *rad_alloc_reply(RADIUS_PACKET *packet)
+/** Allocate a new RADIUS_PACKET response
+ *
+ * @param ctx the context in which the packet is allocated. May be NULL if
+ *     the packet is not associated with a REQUEST.
+ * @param newvector if TRUE a new request authenticator will be generated.
+ * @return a new RADIUS_PACKET or NULL on error.
+ */
+RADIUS_PACKET *rad_alloc_reply(TALLOC_CTX *ctx, RADIUS_PACKET *packet)
 {
        RADIUS_PACKET *reply;
 
        if (!packet) return NULL;
 
-       reply = rad_alloc(0);
+       reply = rad_alloc(ctx, 0);
        if (!reply) return NULL;
 
        /*
@@ -4319,7 +4330,6 @@ void rad_free(RADIUS_PACKET **radius_packet_ptr)
 
        pairfree(&radius_packet->vps);
 
-       free(radius_packet);
-
+       talloc_free(radius_packet);
        *radius_packet_ptr = NULL;
 }
index ff79d82..8ce9f31 100644 (file)
@@ -190,7 +190,7 @@ int fr_tcp_client_socket(fr_ipaddr_t *src_ipaddr,
 
 RADIUS_PACKET *fr_tcp_recv(int sockfd, int flags)
 {
-       RADIUS_PACKET *packet = rad_alloc(0);
+       RADIUS_PACKET *packet = rad_alloc(NULL, 0);
 
        if (!packet) return NULL;
 
@@ -356,7 +356,7 @@ RADIUS_PACKET *fr_tcp_accept(int sockfd)
                 */
 #ifdef EWOULDBLOCK
                if (errno == EWOULDBLOCK) {
-                       packet = rad_alloc(0);
+                       packet = rad_alloc(NULL, 0);
                        if (!packet) return NULL;
 
                        packet->sockfd = sockfd;
@@ -368,7 +368,7 @@ RADIUS_PACKET *fr_tcp_accept(int sockfd)
                return NULL;
        }
                
-       packet = rad_alloc(0);
+       packet = rad_alloc(NULL, 0);
        if (!packet) {
                close(newfd);
                return NULL;
index fbd213c..1b20d39 100644 (file)
@@ -282,11 +282,11 @@ RADIUS_PACKET *vqp_recv(int sockfd)
        /*
         *      Allocate the new request data structure
         */
-       if ((packet = malloc(sizeof(*packet))) == NULL) {
+       packet = rad_alloc(NULL, 0);
+       if (!packet) {
                fr_strerror_printf("out of memory");
                return NULL;
        }
-       memset(packet, 0, sizeof(*packet));
 
        length = vqp_recvfrom(sockfd, &packet->data, 0,
                                        &packet->src_ipaddr, &packet->src_port,
@@ -298,7 +298,7 @@ RADIUS_PACKET *vqp_recv(int sockfd)
        if (length < 0) {
                fr_strerror_printf("Error receiving packet: %s", strerror(errno));
                /* packet->data is NULL */
-               free(packet);
+               rad_free(&packet);
                return NULL;
        }
        packet->data_len = length; /* unsigned vs signed */
index dc9456f..252691a 100644 (file)
@@ -1294,7 +1294,7 @@ static int command_inject_file(rad_listen_t *listener, int argc, char *argv[])
        fake->decode = null_socket_dencode;
        fake->send = null_socket_send;
 
-       packet = rad_alloc(0);
+       packet = rad_alloc(NULL, 0);
        packet->src_ipaddr = sock->src_ipaddr;
        packet->src_port = 0;
 
index 1981d1c..afd2eb2 100644 (file)
@@ -627,7 +627,7 @@ int detail_recv(rad_listen_t *listener)
         *      Allocate the packet.  If we fail, it's a serious
         *      problem.
         */
-       packet = rad_alloc(1);
+       packet = rad_alloc(NULL, 1);
        if (!packet) {
                radlog(L_ERR, "FATAL: Failed allocating memory for detail");
                exit(1);
index 68615f1..23bc863 100644 (file)
@@ -107,7 +107,7 @@ static int request_init(const char *filename)
                fp = stdin;
        }
 
-       request = rad_alloc(0);
+       request = rad_alloc(NULL, 0);
 
        /*
         *      Read the VP's.
index dcd53a6..dabec1b 100644 (file)
@@ -260,7 +260,7 @@ RADCLIENT *client_listener_find(rad_listen_t *listener,
                request_free(&request);
                goto unknown;
        }
-       request->reply = rad_alloc_reply(request->packet);
+       request->reply = rad_alloc_reply(request, request->packet);
        if (!request->reply) {
                request_free(&request);
                goto unknown;
@@ -438,7 +438,7 @@ static int dual_tcp_recv(rad_listen_t *listener)
         *      Allocate a packet for partial reads.
         */
        if (!sock->packet) {
-               sock->packet = rad_alloc(0);
+               sock->packet = rad_alloc(NULL, 0);
                if (!sock->packet) return 0;
 
                sock->packet->sockfd = listener->fd;
index b78e1a4..1c0b581 100644 (file)
@@ -1359,8 +1359,8 @@ int request_insert(rad_listen_t *listener, RADIUS_PACKET *packet,
         *      Create and initialize the new request.
         */
        request = request_alloc(); /* never fails */
-       
-       if ((request->reply = rad_alloc(0)) == NULL) {
+       request->reply = rad_alloc(request, 0);
+       if (!request->reply) {
                radlog(L_ERR, "No memory");
                request_free(&request);
                return 1;
@@ -2585,7 +2585,7 @@ static void ping_home_server(void *ctx)
        request->child_pid = NO_SUCH_CHILD_PID;
 #endif
 
-       request->proxy = rad_alloc(1);
+       request->proxy = rad_alloc(request, 1);
        rad_assert(request->proxy != NULL);
 
        if (home->ping_check == HOME_PING_CHECK_STATUS_SERVER) {
index fc437d5..3c599dc 100644 (file)
@@ -239,7 +239,7 @@ static int radclient_init(const char *filename)
                }
                memset(radclient, 0, sizeof(*radclient));
 
-               radclient->request = rad_alloc(1);
+               radclient->request = rad_alloc(NULL, 1);
                if (!radclient->request) {
                        goto oom;
                }
index 01c1a7d..5eeef50 100644 (file)
@@ -128,7 +128,7 @@ static int filter_packet(RADIUS_PACKET *packet)
                /*
                 *      This swaps the various fields.
                 */
-               reply = rad_alloc_reply(packet);
+               reply = rad_alloc_reply(NULL, packet);
                if (!reply) goto oom;
                
                compare = 1;
@@ -242,13 +242,12 @@ static void got_packet(UNUSED uint8_t *args, const struct pcap_pkthdr *header, c
        udp = (const struct udp_header*)(((const uint8_t *) ip) + size_ip);
        payload = (const uint8_t *)(((const uint8_t *) udp) + size_udp);
 
-       packet = malloc(sizeof(*packet));
+       packet = rad_alloc(NULL, 0);
        if (!packet) {
                fprintf(stderr, "Out of memory\n");
                return;
        }
 
-       memset(packet, 0, sizeof(*packet));
        packet->src_ipaddr.af = AF_INET;
        packet->src_ipaddr.ipaddr.ip4addr.s_addr = ip->ip_src.s_addr;
        packet->src_port = ntohs(udp->udp_sport);
@@ -266,7 +265,7 @@ static void got_packet(UNUSED uint8_t *args, const struct pcap_pkthdr *header, c
                DEBUG(log_dst, "  To:      %s:%d\n", inet_ntoa(ip->ip_dst), ntohs(udp->udp_dport));
                DEBUG(log_dst, "  Type:    %s\n", fr_packet_codes[packet->code]);
 
-               free(packet);
+               rad_free(&packet);
                return;
        }
        
@@ -281,7 +280,7 @@ static void got_packet(UNUSED uint8_t *args, const struct pcap_pkthdr *header, c
                break;
        case PW_AUTHENTICATION_REQUEST:
                /* save the request for later matching */
-               original = rad_alloc_reply(packet);
+               original = rad_alloc_reply(NULL, packet);
                if (original) { /* just ignore allocation failures */
                        rbtree_deletebydata(request_tree, original);
                        rbtree_insert(request_tree, original);
@@ -296,7 +295,7 @@ static void got_packet(UNUSED uint8_t *args, const struct pcap_pkthdr *header, c
         *  Decode the data without bothering to check the signatures.
         */
        if (rad_decode(packet, original, radius_secret) != 0) {
-               free(packet);
+               rad_free(&packet);
                fr_perror("decode");
                return;
        }
@@ -308,7 +307,7 @@ static void got_packet(UNUSED uint8_t *args, const struct pcap_pkthdr *header, c
                rbtree_deletebydata(request_tree, original);
 
        if (filter_vps && filter_packet(packet)) {
-               free(packet);
+               rad_free(&packet);
                DEBUG(log_dst, "Packet number %d doesn't match\n", count++);
                return;
        }
@@ -363,7 +362,7 @@ static void got_packet(UNUSED uint8_t *args, const struct pcap_pkthdr *header, c
        if (!filter_vps ||
            ((packet->code != PW_AUTHENTICATION_REQUEST) &&
             (packet->code != PW_ACCOUNTING_REQUEST))) {
-               free(packet);
+               rad_free(&packet);
        }
 }
 
@@ -566,7 +565,7 @@ int main(int argc, char *argv[])
        /*
         *  Allocate a null packet for decrypting attributes in CoA requests
         */
-       nullpacket = rad_alloc(0);
+       nullpacket = rad_alloc(NULL, 0);
        if (!nullpacket) {
                fprintf(stderr, "radsniff: Out of memory\n");
                exit(1);
index 5668f26..56df3b8 100644 (file)
@@ -2096,7 +2096,8 @@ void home_server_update_request(home_server *home, REQUEST *request)
         *      module, and encapsulated into an EAP packet.
         */
        if (!request->proxy) {
-               if ((request->proxy = rad_alloc(TRUE)) == NULL) {
+               request->proxy = rad_alloc(request, TRUE);
+               if (!request->proxy) {
                        radlog(L_ERR, "no memory");
                        exit(1);
                }
index 3d48054..56d6939 100644 (file)
@@ -140,7 +140,7 @@ static int tls_socket_recv(rad_listen_t *listener)
        RADCLIENT *client = sock->client;
 
        if (!sock->packet) {
-               sock->packet = rad_alloc(0);
+               sock->packet = rad_alloc(NULL, 0);
                if (!sock->packet) return 0;
 
                sock->packet->sockfd = listener->fd;
@@ -149,7 +149,10 @@ static int tls_socket_recv(rad_listen_t *listener)
                sock->packet->dst_ipaddr = sock->my_ipaddr;
                sock->packet->dst_port = sock->my_port;
 
-               if (sock->request) sock->request->packet = sock->packet;
+               if (sock->request) {
+                       talloc_steal(sock->request, sock->packet);
+                       sock->request->packet = sock->packet;
+               }
        }
 
        /*
@@ -172,7 +175,7 @@ static int tls_socket_recv(rad_listen_t *listener)
                /*
                 *      Not sure if we should do this on every packet...
                 */
-               request->reply = rad_alloc(0);
+               request->reply = rad_alloc(request, 0);
                if (!request->reply) return 0;
 
                request->options = RAD_REQUEST_OPTION_DEBUG2;
@@ -533,7 +536,7 @@ redo:
        }
        PTHREAD_MUTEX_UNLOCK(&sock->mutex);
 
-       packet = rad_alloc(0);
+       packet = rad_alloc(NULL, 0);
        packet->sockfd = listener->fd;
        packet->src_ipaddr = sock->other_ipaddr;
        packet->src_port = sock->other_port;
index 72ba7e3..a5777d3 100644 (file)
@@ -473,13 +473,13 @@ REQUEST *request_alloc_fake(REQUEST *request)
    */
   fake->server = request->server;
 
-  fake->packet = rad_alloc(1);
+  fake->packet = rad_alloc(request, 1);
   if (!fake->packet) {
          request_free(&fake);
          return NULL;
   }
 
-  fake->reply = rad_alloc(0);
+  fake->reply = rad_alloc(request, 0);
   if (!fake->reply) {
          request_free(&fake);
          return NULL;
@@ -547,7 +547,7 @@ REQUEST *request_alloc_coa(REQUEST *request)
 
        request->coa->packet->code = 0; /* unknown, as of yet */
        request->coa->child_state = REQUEST_RUNNING;
-       request->coa->proxy = rad_alloc(0);
+       request->coa->proxy = rad_alloc(request->coa, 0);
        if (!request->coa->proxy) {
                request_free(&request->coa);
                return NULL;
index 75b2c11..de4fbd7 100644 (file)
@@ -1097,7 +1097,8 @@ int main(int argc, char **argv)
                return 1;
        }
 
-       if ((req = rad_alloc(1)) == NULL) {
+       req = rad_alloc(NULL, 1);
+       if (!req) {
                fr_perror("radclient");
                exit(1);
        }
@@ -1461,12 +1462,14 @@ main(int argc, char *argv[])
                return 1;
        }
 
-       if ((req = rad_alloc(1)) == NULL) {
+       req = rad_alloc(NULL, 1)
+       if (!req) {
                fr_perror("radclient");
                exit(1);
        }
 
-       if ((req2 = rad_alloc(1)) == NULL) {
+       req2 = rad_alloc(NULL, 1);
+       if (!req2) {
                fr_perror("radclient");
                exit(1);
        }
index 575d830..d715a11 100644 (file)
@@ -126,7 +126,7 @@ static int replicate_packet(void *instance, REQUEST *request,
                 *      we built here.
                 */
                if (!packet) {
-                       packet = rad_alloc(1);
+                       packet = rad_alloc(NULL, 1);
                        if (!packet) return RLM_MODULE_FAIL;
                        packet->sockfd = -1;
                        packet->code = code;