The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
packet.c
Go to the documentation of this file.
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15  */
16 
17 /** fr_packet_t alloc/free functions
18  *
19  * @file src/lib/util/packet.c
20  *
21  * @copyright 2000,2006 The FreeRADIUS server project
22  */
23 RCSID("$Id: 62da5ac014009284fdc84801e84423fcd33e8319 $")
24 
25 #include <freeradius-devel/util/misc.h>
26 #include <freeradius-devel/util/packet.h>
27 #include <freeradius-devel/util/rand.h>
28 
29 /** Allocate a new fr_packet_t
30  *
31  * @param ctx the context in which the packet is allocated. May be NULL if
32  * the packet is not associated with a request_t.
33  * @param new_vector if true a new request authenticator will be generated.
34  * @return
35  * - New fr_packet_t.
36  * - NULL on error.
37  */
38 fr_packet_t *fr_packet_alloc(TALLOC_CTX *ctx, bool new_vector)
39 {
40  fr_packet_t *rp;
41 
42  rp = talloc_zero(ctx, fr_packet_t);
43  if (!rp) {
44  fr_strerror_const("out of memory");
45  return NULL;
46  }
47  rp->id = -1;
48 
49  if (new_vector) fr_rand_buffer(rp->vector, sizeof(rp->vector));
50 
51  return rp;
52 }
53 
54 /** Allocate a new fr_packet_t response
55  *
56  * @param ctx the context in which the packet is allocated. May be NULL if
57  * the packet is not associated with a request_t.
58  * @param packet The request packet.
59  * @return
60  * - New fr_packet_t.
61  * - NULL on error.
62  */
63 fr_packet_t *fr_packet_alloc_reply(TALLOC_CTX *ctx, fr_packet_t *packet)
64 {
65  fr_packet_t *reply;
66 
67  if (!packet) return NULL;
68 
69  reply = fr_packet_alloc(ctx, false);
70  if (!reply) return NULL;
71 
72  /*
73  * Initialize the fields from the request.
74  */
75  fr_socket_addr_swap(&reply->socket, &packet->socket);
76  reply->id = packet->id;
77  reply->code = 0; /* UNKNOWN code */
78  memset(reply->vector, 0, sizeof(reply->vector));
79  reply->data = NULL;
80  reply->data_len = 0;
81 
82  return reply;
83 }
84 
85 
86 /** Free a fr_packet_t
87  *
88  */
89 void fr_packet_free(fr_packet_t **packet_p)
90 {
91  fr_packet_t *packet;
92 
93  if (!packet_p || !*packet_p) return;
94  packet = *packet_p;
95 
96  PACKET_VERIFY(packet);
97 
98  talloc_free(packet);
99  *packet_p = NULL;
100 }
#define RCSID(id)
Definition: build.h:444
talloc_free(reap)
fr_packet_t * fr_packet_alloc_reply(TALLOC_CTX *ctx, fr_packet_t *packet)
Allocate a new fr_packet_t response.
Definition: packet.c:63
fr_packet_t * fr_packet_alloc(TALLOC_CTX *ctx, bool new_vector)
Allocate a new fr_packet_t.
Definition: packet.c:38
void fr_packet_free(fr_packet_t **packet_p)
Free a fr_packet_t.
Definition: packet.c:89
void fr_rand_buffer(void *start, size_t length)
Definition: rand.c:126
unsigned int code
Packet code (type).
Definition: packet.h:61
fr_socket_t socket
This packet was received on.
Definition: packet.h:57
int id
Packet ID (used to link requests/responses).
Definition: packet.h:60
#define PACKET_VERIFY(_x)
Definition: packet.h:42
uint8_t * data
Packet data (body).
Definition: packet.h:63
size_t data_len
Length of packet data.
Definition: packet.h:64
uint8_t vector[RADIUS_AUTH_VECTOR_LENGTH]
RADIUS authentication vector.
Definition: packet.h:69
static void fr_socket_addr_swap(fr_socket_t *dst, fr_socket_t const *src)
Swap src/dst information of a fr_socket_t.
Definition: socket.h:121
#define fr_strerror_const(_msg)
Definition: strerror.h:223