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.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers UDPv4.cpp Source File

UDPv4.cpp

00001 /*
00002  * $Id: UDPv4.c 29 2011-06-11 14:53:08Z benoit $
00003  * $Author: benoit $
00004  * $Date: 2011-06-11 16:53:08 +0200 (sam., 11 juin 2011) $
00005  * $Rev: 29 $
00006  * 
00007  * 
00008  * 
00009  * 
00010  * 
00011  */
00012  
00013 #include "UDPv4.h"
00014 #include "NetIF.h"
00015 #include "Debug.h"
00016 #include <string.h>
00017 
00018 #define    DEBUG_CURRENT_MODULE_NAME    "UDPv4"
00019 #define    DEBUG_CURRENT_MODULE_ID        DEBUG_MODULE_UDPV4
00020 
00021 
00022 static void Init(void);
00023 static void Handler(NetIF_t *netIF, NetPacket_t *packet);
00024 static int32_t RegisterAPI(Net_API_t *api);
00025 
00026 
00027 Protocol_Handler_t    udpv4 = 
00028 {
00029     PROTOCOL_INDEX_NOT_INITIALIZED,         /* Always PROTOCOL_INDEX_NOT_INITIALIZED at initialization */
00030     Protocol_ID_UDPv4,                         /* Protocol ID */
00031     IPV4_PROTO_UDPV4,                         /* Protocol number */
00032     Init,                                     /* Protocol initialisation function */
00033     Handler,                                /* Protocol handler */
00034     NULL,                                     /* Protocol registration function */
00035     RegisterAPI,                            /* API registration function */
00036 };
00037 
00038 
00039 static Net_API_t    *netAPITable[NET_API_PER_PROTOCOL_MAX_COUNT];
00040 static int32_t        netAPICount = 0;
00041 
00042 
00043 static void Init(void)
00044 {
00045     DEBUG_MODULE(DEBUG_LEVEL_INFO, ("Initializing UDPv4 layer"));
00046     memset(netAPITable, 0, sizeof(netAPITable));
00047     netAPICount = 0;
00048 }
00049 
00050 
00051 static void Handler(NetIF_t *netIF, NetPacket_t *packet)
00052 {
00053     IPv4_Header_t    *ipv4Header;
00054     int32_t            depth, index;
00055     
00056     for (index = 0; index < netAPICount; index++)
00057     {
00058         netAPITable[index]->Hook(netIF, Protocol_ID_UDPv4, packet);
00059     }
00060         
00061     depth = packet->depth;
00062     ipv4Header = (IPv4_Header_t *)packet->headerPtrTable[depth];
00063     
00064     DEBUG_BLOCK(DEBUG_LEVEL_VERBOSE1)
00065     {
00066         UDPv4_DumpHeader(NULL, ipv4Header);
00067     }
00068 }
00069 
00070 
00071 static int32_t RegisterAPI(Net_API_t *netAPI)
00072 {
00073     DEBUG_MODULE(DEBUG_LEVEL_INFO, ("Registering %s API", api_IDNames[netAPI->apiID]));
00074     netAPI->InitAPI();
00075     netAPITable[netAPICount] = netAPI;
00076     netAPICount++;
00077     return -1;
00078 }
00079 
00080 
00081 void UDPv4_DumpHeader(const char *prefix, IPv4_Header_t *ipv4Header)
00082 {
00083     UDPv4_Header_t    *udpv4Header = (UDPv4_Header_t *)(ipv4Header + 1);
00084 
00085     DEBUG_RAW(("%sUDPv4 %d.%d.%d.%d:%d --> %d.%d.%d.%d:%d  length:%d crc:%04X" ,
00086         prefix != NULL ? prefix : "",
00087         ipv4Header->source.IP0,
00088         ipv4Header->source.IP1,
00089         ipv4Header->source.IP2,
00090         ipv4Header->source.IP3,
00091         ntohs(udpv4Header->sourcePort),
00092         ipv4Header->dest.IP0,
00093         ipv4Header->dest.IP1,
00094         ipv4Header->dest.IP2,
00095         ipv4Header->dest.IP3,
00096         ntohs(udpv4Header->destPort),
00097         ntohs(udpv4Header->length),
00098         ntohs(udpv4Header->crc)
00099     ));
00100     
00101 }
00102