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 RADIUS_PACKET *fr_tcp_recv(int sockfd, int flags)
31 RADIUS_PACKET *packet = rad_alloc(NULL, false);
33 if (!packet) return NULL;
35 packet->sockfd = sockfd;
37 if (fr_tcp_read_packet(packet, flags) != 1) {
47 * Receives a packet, assuming that the RADIUS_PACKET structure
48 * has been filled out already.
50 * This ASSUMES that the packet is allocated && fields
53 * This ASSUMES that the socket is marked as O_NONBLOCK, which
54 * the function above does set, if your system supports it.
56 * Calling this function MAY change sockfd,
57 * if src_ipaddr.af == AF_UNSPEC.
59 int fr_tcp_read_packet(RADIUS_PACKET *packet, int flags)
64 * No data allocated. Read the 4-byte header into
70 len = recv(packet->sockfd, packet->vector + packet->data_len,
71 4 - packet->data_len, 0);
72 if (len == 0) return -2; /* clean close */
75 if ((len < 0) && (errno == ECONNRESET)) { /* forced */
81 fr_strerror_printf("Error receiving packet: %s",
86 packet->data_len += len;
87 if (packet->data_len < 4) { /* want more data */
91 packet_len = (packet->vector[2] << 8) | packet->vector[3];
93 if (packet_len < RADIUS_HDR_LEN) {
94 fr_strerror_printf("Discarding packet: Smaller than RFC minimum of 20 bytes");
99 * If the packet is too big, then the socket is bad.
101 if (packet_len > MAX_PACKET_LEN) {
102 fr_strerror_printf("Discarding packet: Larger than RFC limitation of 4096 bytes");
106 packet->data = talloc_array(packet, uint8_t, packet_len);
108 fr_strerror_printf("Out of memory");
112 packet->data_len = packet_len;
114 memcpy(packet->data, packet->vector, 4);
118 * Try to read more data.
120 len = recv(packet->sockfd, packet->data + packet->partial,
121 packet->data_len - packet->partial, 0);
122 if (len == 0) return -2; /* clean close */
125 if ((len < 0) && (errno == ECONNRESET)) { /* forced */
131 fr_strerror_printf("Error receiving packet: %s", fr_syserror(errno));
135 packet->partial += len;
137 if (packet->partial < packet->data_len) {
142 * See if it's a well-formed RADIUS packet.
144 if (!rad_packet_ok(packet, flags, NULL)) {
149 * Explicitly set the VP list to empty.
154 char ip_buf[128], buffer[256];
156 if (packet->src_ipaddr.af != AF_UNSPEC) {
157 inet_ntop(packet->src_ipaddr.af,
158 &packet->src_ipaddr.ipaddr,
159 ip_buf, sizeof(ip_buf));
160 snprintf(buffer, sizeof(buffer), "host %s port %d",
161 ip_buf, packet->src_port);
163 snprintf(buffer, sizeof(buffer), "socket %d",
169 return 1; /* done reading the packet */
172 #endif /* WITH_TCP */