The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
tcp.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program 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
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: 18ec854a3e030e93f95722f9333e171cc6b6c4f7 $
19 *
20 * @file protocols/radius/packet.c
21 * @brief TCP-specific functions.
22 *
23 * @copyright (C) 2009 Dante http://dante.net
24 */
25RCSID("$Id: 18ec854a3e030e93f95722f9333e171cc6b6c4f7 $")
26
27#include <freeradius-devel/radius/radius.h>
28#include <freeradius-devel/util/syserror.h>
29#include "tcp.h"
30
31/*
32 * This ASSUMES that the socket is marked as O_NONBLOCK, which
33 * the function above does set, if your system supports it.
34 */
35ssize_t fr_tcp_read_packet(int sockfd, uint8_t *buffer, size_t size, size_t *total, uint32_t max_attributes, bool require_message_authenticator)
36{
37 ssize_t slen;
38 size_t packet_len, hdr_len;
39 uint8_t *start, *end;
40
41 fr_assert(*total < size);
42 start = buffer + *total;
43 end = buffer + size;
44
45 slen = recv(sockfd, start, (size_t) (end - start), 0);
46 if (slen <= 0) return -1;
47
48 packet_len = *total + slen;
49
50 /*
51 * Not enough for a header, die.
52 */
53 if (packet_len < 4) return 0;
54
55 hdr_len = fr_nbo_to_uint16(buffer + 2);
56 if ((hdr_len < RADIUS_HEADER_LENGTH) || (hdr_len > RADIUS_MAX_PACKET_SIZE)) return -1;
57
58 if (packet_len < hdr_len) {
59 *total = packet_len;
60 return 0;
61 }
62
63 /*
64 * See if it's a well-formed RADIUS packet.
65 */
66 if (!fr_radius_ok(buffer, &hdr_len, max_attributes, require_message_authenticator, NULL)) {
67 return -1;
68 }
69
70 return hdr_len;
71}
static int const char char buffer[256]
Definition acutest.h:578
#define RCSID(id)
Definition build.h:487
static int sockfd
Definition dhcpclient.c:56
bool fr_radius_ok(uint8_t const *packet, size_t *packet_len_p, uint32_t max_attributes, bool require_message_authenticator, decode_fail_t *reason)
unsigned int uint32_t
long int ssize_t
unsigned char uint8_t
static uint16_t fr_nbo_to_uint16(uint8_t const data[static sizeof(uint16_t)])
Read an unsigned 16bit integer from wire format (big endian)
Definition nbo.h:146
#define RADIUS_HEADER_LENGTH
Definition net.h:80
#define fr_assert(_expr)
Definition rad_assert.h:38
#define RADIUS_MAX_PACKET_SIZE
Definition radius.h:41
ssize_t fr_tcp_read_packet(int sockfd, uint8_t *buffer, size_t size, size_t *total, uint32_t max_attributes, bool require_message_authenticator)
Definition tcp.c:35
RADIUS over TCP.