2 * tcp.c TCP-specific functions.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright (C) 2009 Dante http://dante.net
25 #include <freeradius-devel/libradius.h>
29 /* FIXME: into common RADIUS header? */
30 #define MAX_PACKET_LEN 4096
32 RADIUS_PACKET *fr_tcp_recv(int sockfd, int flags)
34 RADIUS_PACKET *packet = rad_alloc(NULL, false);
36 if (!packet) return NULL;
38 packet->sockfd = sockfd;
40 if (fr_tcp_read_packet(packet, flags) != 1) {
50 * Receives a packet, assuming that the RADIUS_PACKET structure
51 * has been filled out already.
53 * This ASSUMES that the packet is allocated && fields
56 * This ASSUMES that the socket is marked as O_NONBLOCK, which
57 * the function above does set, if your system supports it.
59 * Calling this function MAY change sockfd,
60 * if src_ipaddr.af == AF_UNSPEC.
62 int fr_tcp_read_packet(RADIUS_PACKET *packet, int flags)
67 * No data allocated. Read the 4-byte header into
73 len = recv(packet->sockfd, packet->vector + packet->data_len,
74 4 - packet->data_len, 0);
75 if (len == 0) return -2; /* clean close */
78 if ((len < 0) && (errno == ECONNRESET)) { /* forced */
84 fr_strerror_printf("Error receiving packet: %s",
89 packet->data_len += len;
90 if (packet->data_len < 4) { /* want more data */
94 packet_len = (packet->vector[2] << 8) | packet->vector[3];
96 if (packet_len < RADIUS_HDR_LEN) {
97 fr_strerror_printf("Discarding packet: Smaller than RFC minimum of 20 bytes");
102 * If the packet is too big, then the socket is bad.
104 if (packet_len > MAX_PACKET_LEN) {
105 fr_strerror_printf("Discarding packet: Larger than RFC limitation of 4096 bytes");
109 packet->data = talloc_array(packet, uint8_t, packet_len);
111 fr_strerror_printf("Out of memory");
115 packet->data_len = packet_len;
117 memcpy(packet->data, packet->vector, 4);
121 * Try to read more data.
123 len = recv(packet->sockfd, packet->data + packet->partial,
124 packet->data_len - packet->partial, 0);
125 if (len == 0) return -2; /* clean close */
128 if ((len < 0) && (errno == ECONNRESET)) { /* forced */
134 fr_strerror_printf("Error receiving packet: %s", fr_syserror(errno));
138 packet->partial += len;
140 if (packet->partial < packet->data_len) {
145 * See if it's a well-formed RADIUS packet.
147 if (!rad_packet_ok(packet, flags, NULL)) {
152 * Explicitly set the VP list to empty.
157 char ip_buf[128], buffer[256];
159 if (packet->src_ipaddr.af != AF_UNSPEC) {
160 inet_ntop(packet->src_ipaddr.af,
161 &packet->src_ipaddr.ipaddr,
162 ip_buf, sizeof(ip_buf));
163 snprintf(buffer, sizeof(buffer), "host %s port %d",
164 ip_buf, packet->src_port);
166 snprintf(buffer, sizeof(buffer), "socket %d",
171 if (is_radius_code(packet->code)) {
172 DEBUG("Received %s packet from %s",
173 fr_packet_codes[packet->code], buffer);
175 DEBUG("Received packet from %s code %d",
176 buffer, packet->code);
178 DEBUG(", id=%d, length=%zu\n", packet->id, packet->data_len);
181 return 1; /* done reading the packet */
184 #endif /* WITH_TCP */