/src/freeradius-server/src/lib/util/packet.c
Line | Count | Source |
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 | 0 | { |
40 | 0 | fr_packet_t *rp; |
41 | |
|
42 | 0 | rp = talloc_zero(ctx, fr_packet_t); |
43 | 0 | if (!rp) { |
44 | 0 | fr_strerror_const("out of memory"); |
45 | 0 | return NULL; |
46 | 0 | } |
47 | 0 | rp->id = -1; |
48 | |
|
49 | 0 | if (new_vector) fr_rand_buffer(rp->vector, sizeof(rp->vector)); |
50 | |
|
51 | 0 | return rp; |
52 | 0 | } |
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 | 0 | { |
65 | 0 | fr_packet_t *reply; |
66 | |
|
67 | 0 | if (!packet) return NULL; |
68 | | |
69 | 0 | reply = fr_packet_alloc(ctx, false); |
70 | 0 | if (!reply) return NULL; |
71 | | |
72 | | /* |
73 | | * Initialize the fields from the request. |
74 | | */ |
75 | 0 | fr_socket_addr_swap(&reply->socket, &packet->socket); |
76 | 0 | reply->id = packet->id; |
77 | 0 | reply->code = 0; /* UNKNOWN code */ |
78 | 0 | memset(reply->vector, 0, sizeof(reply->vector)); |
79 | 0 | reply->data = NULL; |
80 | 0 | reply->data_len = 0; |
81 | |
|
82 | 0 | return reply; |
83 | 0 | } |
84 | | |
85 | | |
86 | | /** Free a fr_packet_t |
87 | | * |
88 | | */ |
89 | | void fr_packet_free(fr_packet_t **packet_p) |
90 | 0 | { |
91 | 0 | fr_packet_t *packet; |
92 | |
|
93 | 0 | if (!packet_p || !*packet_p) return; |
94 | 0 | packet = *packet_p; |
95 | |
|
96 | 0 | PACKET_VERIFY(packet); |
97 | |
|
98 | 0 | talloc_free(packet); |
99 | | *packet_p = NULL; |
100 | 0 | } |