1 //==========================================================================
5 // Stand-alone UDP networking support for RedBoot
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004 Red Hat, Inc.
12 // Copyright (C) 2002, 2003 Gary Thomas
14 // eCos is free software; you can redistribute it and/or modify it under
15 // the terms of the GNU General Public License as published by the Free
16 // Software Foundation; either version 2 or (at your option) any later version.
18 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
19 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 // You should have received a copy of the GNU General Public License along
24 // with eCos; if not, write to the Free Software Foundation, Inc.,
25 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
27 // As a special exception, if other files instantiate templates or use macros
28 // or inline functions from this file, or you compile this file and link it
29 // with other works to produce a work based on this file, this file does not
30 // by itself cause the resulting work to be covered by the GNU General Public
31 // License. However the source code for this file must still be made available
32 // in accordance with section (3) of the GNU General Public License.
34 // This exception does not invalidate any other reasons why a work based on
35 // this file might be covered by the GNU General Public License.
37 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
38 // at http://sources.redhat.com/ecos/ecos-license/
39 // -------------------------------------------
40 //####ECOSGPLCOPYRIGHTEND####
41 //==========================================================================
42 //#####DESCRIPTIONBEGIN####
45 // Contributors: gthomas
50 // This code is part of RedBoot (tm).
52 //####DESCRIPTIONEND####
54 //==========================================================================
60 static int udp_rx_total;
61 static int udp_rx_handled;
62 static int udp_rx_cksum;
63 static int udp_rx_dropped;
66 #define MAX_UDP_DATA (ETH_MAX_PKTLEN - (ETH_HDR_SIZE + \
67 sizeof(ip_header_t) + \
68 sizeof(udp_header_t)))
71 * A major assumption is that only a very small number of sockets will
72 * active, so a simple linear search of those sockets is acceptible.
74 static udp_socket_t *udp_list;
78 * Install a handler for incoming udp packets.
79 * Caller provides the udp_socket_t structure.
80 * Returns zero if successful, -1 if socket is already used.
83 __udp_install_listener(udp_socket_t *s, word port, udp_handler_t handler)
88 * Make sure we only have one handler per port.
90 for (p = udp_list; p; p = p->next)
91 if (p->our_port == port)
94 s->our_port = htons(port);
104 * Remove the handler for the given socket.
107 __udp_remove_listener(word port)
109 udp_socket_t *prev, *s;
111 for (prev = NULL, s = udp_list; s; prev = s, s = s->next)
112 if (s->our_port == htons(port)) {
114 prev->next = s->next;
122 * Handle incoming UDP packets.
125 __udp_handler(pktbuf_t *pkt, ip_route_t *r)
127 udp_header_t *udp = pkt->udp_hdr;
128 ip_header_t *ip = pkt->ip_hdr;
131 if (udp->checksum == 0xffff)
134 /* copy length for pseudo sum calculation */
135 ip->length = udp->length;
137 if (__sum((word *)udp, ntohs(udp->length), __pseudo_sum(ip)) == 0) {
138 for (s = udp_list; s; s = s->next) {
139 if (s->our_port == udp->dest_port) {
140 (*s->handler)(s, ((char *)udp) + sizeof(udp_header_t),
141 ntohs(udp->length) - sizeof(udp_header_t),
142 r, ntohs(udp->src_port));
156 __udp_send(void *buf, int len, ip_route_t *dest_ip,
157 word dest_port, word src_port)
162 unsigned short cksum;
166 if (len > MAX_UDP_DATA)
169 /* just drop it if can't get a buffer */
170 if ((pkt = __pktbuf_alloc(ETH_MAX_PKTLEN)) == NULL)
176 pkt->pkt_bytes = len + sizeof(udp_header_t);
178 udp->src_port = htons(src_port);
179 udp->dest_port = htons(dest_port);
180 udp->length = htons(pkt->pkt_bytes);
183 memcpy(((char *)udp) + sizeof(udp_header_t), buf, len);
185 /* fill in some pseudo-header fields */
186 memcpy(ip->source, __local_ip_addr, sizeof(ip_addr_t));
187 memcpy(ip->destination, dest_ip->ip_addr, sizeof(ip_addr_t));
188 ip->protocol = IP_PROTO_UDP;
189 ip->length = udp->length;
191 cksum = __sum((word *)udp, pkt->pkt_bytes, __pseudo_sum(ip));
192 udp->checksum = htons(cksum);
194 ret = __ip_send(pkt, IP_PROTO_UDP, dest_ip);
200 __udp_sendto(void *data, int len, struct sockaddr_in *server,
201 struct sockaddr_in *local)
205 if (__arp_lookup((ip_addr_t *)&server->sin_addr, &rt) < 0) {
206 diag_printf("%s: Can't find address of server\n", __FUNCTION__);
209 __udp_send(data, len, &rt, ntohs(server->sin_port), ntohs(local->sin_port));
214 static char *recvfrom_buf;
215 static int recvfrom_len;
216 static struct sockaddr_in *recvfrom_server;
219 __udp_recvfrom_handler(udp_socket_t *skt, void *buf, int len,
220 ip_route_t *src_route, word src_port)
222 if (recvfrom_server == NULL || recvfrom_buf == NULL)
225 if (recvfrom_server->sin_port && recvfrom_server->sin_port != htons(src_port))
228 // Move data to waiting buffer
230 memcpy(recvfrom_buf, buf, len);
231 if (recvfrom_server) {
232 recvfrom_server->sin_port = htons(src_port);
233 memcpy(&recvfrom_server->sin_addr, &src_route->ip_addr, sizeof(src_route->ip_addr));
234 recvfrom_buf = NULL; // Tell reader we got a packet
236 diag_printf("udp_recvfrom - dropped packet of %d bytes\n", len);
241 __udp_recvfrom(void *data, int len, struct sockaddr_in *server,
242 struct sockaddr_in *local, struct timeval *timo)
244 int res, my_port, total_ms;
248 my_port = ntohs(local->sin_port);
249 if (__udp_install_listener(&skt, my_port, __udp_recvfrom_handler) < 0) {
254 recvfrom_server = server;
255 total_ms = (timo->tv_sec * 1000) + (timo->tv_usec / 1000);
259 __enet_poll(); // Handle the hardware
265 } while ((MS_TICKS_DELAY() - start) < total_ms);
266 __udp_remove_listener(my_port);