Added copy packet functionality
authorAlan T. DeKok <aland@freeradius.org>
Mon, 7 Apr 2014 12:41:01 +0000 (08:41 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Mon, 7 Apr 2014 15:18:37 +0000 (11:18 -0400)
Mainly for CoA.  May be useful elsewhere

src/include/libradius.h
src/lib/radius.c

index 1e1b656..aba8f40 100644 (file)
@@ -524,6 +524,8 @@ int         rad_sign(RADIUS_PACKET *packet, RADIUS_PACKET const *original,
 int rad_digest_cmp(uint8_t const *a, uint8_t const *b, size_t length);
 RADIUS_PACKET  *rad_alloc(TALLOC_CTX *ctx, int newvector);
 RADIUS_PACKET  *rad_alloc_reply(TALLOC_CTX *ctx, RADIUS_PACKET *);
+RADIUS_PACKET *rad_copy_packet(TALLOC_CTX *ctx, RADIUS_PACKET const *in);
+
 void           rad_free(RADIUS_PACKET **);
 int            rad_pwencode(char *encpw, size_t *len, char const *secret,
                             uint8_t const *vector);
index 8add96b..1ad86cc 100644 (file)
@@ -4681,3 +4681,36 @@ void rad_free(RADIUS_PACKET **radius_packet_ptr)
        talloc_free(radius_packet);
        *radius_packet_ptr = NULL;
 }
+
+/** Duplicate a 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 packet The packet to copy
+ * @return a new RADIUS_PACKET or NULL on error.
+ */
+RADIUS_PACKET *rad_copy_packet(TALLOC_CTX *ctx, RADIUS_PACKET const *in)
+{
+       RADIUS_PACKET *out;
+
+       out = rad_alloc(ctx, 0);
+       if (!out) return NULL;
+
+       /*
+        *      Bootstrap by copying everything.
+        */
+       memcpy(out, in, sizeof(*out));
+
+       /*
+        *      Then reset necessary fields
+        */
+       out->sockfd = -1;
+
+       out->data = NULL;
+       out->data_len = 0;
+
+       out->vps = paircopy(out, in->vps);
+       out->offset = 0;
+
+       return out;
+}