Rewrite from scratch a TCP/IP stack for mbed. So far the following parts are usable: Drivers: - EMAC driver (from CMSIS 2.0) Protocols: - Ethernet protocol - ARP over ethernet for IPv4 - IPv4 over Ethernet - ICMPv4 over IPv4 - UDPv4 over IPv4 APIs: - Sockets for UDPv4 The structure of this stack is designed to be very modular. Each protocol can register one or more protocol to handle its payload, and in each protocol, an API can be hooked (like Sockets for example). This is an early release.

Committer:
Benoit
Date:
Sun Jun 26 09:56:31 2011 +0000
Revision:
7:8e12f7357b9f
Parent:
5:3cd83fcb1467
Added IPv4 global broadcast address to processed frames inside IPv4 layer.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Benoit 1:f4040665bc61 1 /*
Benoit 1:f4040665bc61 2 * $Id: UDPv4.c 29 2011-06-11 14:53:08Z benoit $
Benoit 1:f4040665bc61 3 * $Author: benoit $
Benoit 1:f4040665bc61 4 * $Date: 2011-06-11 16:53:08 +0200 (sam., 11 juin 2011) $
Benoit 1:f4040665bc61 5 * $Rev: 29 $
Benoit 1:f4040665bc61 6 *
Benoit 1:f4040665bc61 7 *
Benoit 1:f4040665bc61 8 *
Benoit 1:f4040665bc61 9 *
Benoit 1:f4040665bc61 10 *
Benoit 1:f4040665bc61 11 */
Benoit 1:f4040665bc61 12
Benoit 1:f4040665bc61 13 #include "UDPv4.h"
Benoit 1:f4040665bc61 14 #include "NetIF.h"
Benoit 1:f4040665bc61 15 #include "Debug.h"
Benoit 1:f4040665bc61 16 #include <string.h>
Benoit 1:f4040665bc61 17
Benoit 1:f4040665bc61 18 #define DEBUG_CURRENT_MODULE_NAME "UDPv4"
Benoit 1:f4040665bc61 19 #define DEBUG_CURRENT_MODULE_ID DEBUG_MODULE_UDPV4
Benoit 1:f4040665bc61 20
Benoit 1:f4040665bc61 21
Benoit 1:f4040665bc61 22 static void Init(void);
Benoit 5:3cd83fcb1467 23 static void Handler(NetIF_t *netIF, NetPacket_t *packet);
Benoit 1:f4040665bc61 24 static int32_t RegisterAPI(Net_API_t *api);
Benoit 1:f4040665bc61 25
Benoit 1:f4040665bc61 26
Benoit 1:f4040665bc61 27 Protocol_Handler_t udpv4 =
Benoit 1:f4040665bc61 28 {
Benoit 1:f4040665bc61 29 PROTOCOL_INDEX_NOT_INITIALIZED, /* Always PROTOCOL_INDEX_NOT_INITIALIZED at initialization */
Benoit 1:f4040665bc61 30 Protocol_ID_UDPv4, /* Protocol ID */
Benoit 1:f4040665bc61 31 IPV4_PROTO_UDPV4, /* Protocol number */
Benoit 1:f4040665bc61 32 Init, /* Protocol initialisation function */
Benoit 1:f4040665bc61 33 Handler, /* Protocol handler */
Benoit 1:f4040665bc61 34 NULL, /* Protocol registration function */
Benoit 1:f4040665bc61 35 RegisterAPI, /* API registration function */
Benoit 1:f4040665bc61 36 };
Benoit 1:f4040665bc61 37
Benoit 1:f4040665bc61 38
Benoit 1:f4040665bc61 39 static Net_API_t *netAPITable[NET_API_PER_PROTOCOL_MAX_COUNT];
Benoit 1:f4040665bc61 40 static int32_t netAPICount = 0;
Benoit 1:f4040665bc61 41
Benoit 1:f4040665bc61 42
Benoit 1:f4040665bc61 43 static void Init(void)
Benoit 1:f4040665bc61 44 {
Benoit 1:f4040665bc61 45 DEBUG_MODULE(DEBUG_LEVEL_INFO, ("Initializing UDPv4 layer"));
Benoit 1:f4040665bc61 46 memset(netAPITable, 0, sizeof(netAPITable));
Benoit 1:f4040665bc61 47 netAPICount = 0;
Benoit 1:f4040665bc61 48 }
Benoit 1:f4040665bc61 49
Benoit 1:f4040665bc61 50
Benoit 5:3cd83fcb1467 51 static void Handler(NetIF_t *netIF, NetPacket_t *packet)
Benoit 1:f4040665bc61 52 {
Benoit 1:f4040665bc61 53 IPv4_Header_t *ipv4Header;
Benoit 1:f4040665bc61 54 int32_t depth, index;
Benoit 1:f4040665bc61 55
Benoit 1:f4040665bc61 56 for (index = 0; index < netAPICount; index++)
Benoit 1:f4040665bc61 57 {
Benoit 1:f4040665bc61 58 netAPITable[index]->Hook(netIF, Protocol_ID_UDPv4, packet);
Benoit 1:f4040665bc61 59 }
Benoit 1:f4040665bc61 60
Benoit 1:f4040665bc61 61 depth = packet->depth;
Benoit 1:f4040665bc61 62 ipv4Header = (IPv4_Header_t *)packet->headerPtrTable[depth];
Benoit 1:f4040665bc61 63
Benoit 1:f4040665bc61 64 DEBUG_BLOCK(DEBUG_LEVEL_VERBOSE1)
Benoit 1:f4040665bc61 65 {
Benoit 1:f4040665bc61 66 UDPv4_DumpHeader(NULL, ipv4Header);
Benoit 1:f4040665bc61 67 }
Benoit 1:f4040665bc61 68 }
Benoit 1:f4040665bc61 69
Benoit 1:f4040665bc61 70
Benoit 1:f4040665bc61 71 static int32_t RegisterAPI(Net_API_t *netAPI)
Benoit 1:f4040665bc61 72 {
Benoit 1:f4040665bc61 73 DEBUG_MODULE(DEBUG_LEVEL_INFO, ("Registering %s API", api_IDNames[netAPI->apiID]));
Benoit 1:f4040665bc61 74 netAPI->InitAPI();
Benoit 1:f4040665bc61 75 netAPITable[netAPICount] = netAPI;
Benoit 1:f4040665bc61 76 netAPICount++;
Benoit 1:f4040665bc61 77 return -1;
Benoit 1:f4040665bc61 78 }
Benoit 1:f4040665bc61 79
Benoit 1:f4040665bc61 80
Benoit 1:f4040665bc61 81 void UDPv4_DumpHeader(const char *prefix, IPv4_Header_t *ipv4Header)
Benoit 1:f4040665bc61 82 {
Benoit 1:f4040665bc61 83 UDPv4_Header_t *udpv4Header = (UDPv4_Header_t *)(ipv4Header + 1);
Benoit 1:f4040665bc61 84
Benoit 1:f4040665bc61 85 DEBUG_RAW(("%sUDPv4 %d.%d.%d.%d:%d --> %d.%d.%d.%d:%d length:%d crc:%04X" ,
Benoit 1:f4040665bc61 86 prefix != NULL ? prefix : "",
Benoit 1:f4040665bc61 87 ipv4Header->source.IP0,
Benoit 1:f4040665bc61 88 ipv4Header->source.IP1,
Benoit 1:f4040665bc61 89 ipv4Header->source.IP2,
Benoit 1:f4040665bc61 90 ipv4Header->source.IP3,
Benoit 1:f4040665bc61 91 ntohs(udpv4Header->sourcePort),
Benoit 1:f4040665bc61 92 ipv4Header->dest.IP0,
Benoit 1:f4040665bc61 93 ipv4Header->dest.IP1,
Benoit 1:f4040665bc61 94 ipv4Header->dest.IP2,
Benoit 1:f4040665bc61 95 ipv4Header->dest.IP3,
Benoit 1:f4040665bc61 96 ntohs(udpv4Header->destPort),
Benoit 1:f4040665bc61 97 ntohs(udpv4Header->length),
Benoit 1:f4040665bc61 98 ntohs(udpv4Header->crc)
Benoit 1:f4040665bc61 99 ));
Benoit 1:f4040665bc61 100
Benoit 1:f4040665bc61 101 }
Benoit 1:f4040665bc61 102