added more networking routines
This commit is contained in:
@@ -9,30 +9,22 @@
|
||||
*/
|
||||
#include "bas_types.h"
|
||||
#include "net.h"
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef DBUG_NETWORK
|
||||
|
||||
/********************************************************************/
|
||||
typedef struct
|
||||
{
|
||||
uint16_t port;
|
||||
void (*handler)(NIF *, NBUF *);
|
||||
} UDP_BOUND_PORT;
|
||||
|
||||
/********************************************************************/
|
||||
|
||||
#define UDP_MAX_PORTS (5) /* plenty for this implementation */
|
||||
|
||||
|
||||
static UDP_BOUND_PORT
|
||||
udp_port_table[UDP_MAX_PORTS];
|
||||
static UDP_BOUND_PORT udp_port_table[UDP_MAX_PORTS];
|
||||
|
||||
static uint16
|
||||
udp_port;
|
||||
static uint16_t udp_port;
|
||||
|
||||
/********************************************************************/
|
||||
void
|
||||
udp_init (void)
|
||||
void udp_init(void)
|
||||
{
|
||||
int index;
|
||||
|
||||
@@ -43,15 +35,13 @@ udp_init (void)
|
||||
|
||||
udp_port = DEFAULT_UDP_PORT; /* next free port */
|
||||
}
|
||||
/********************************************************************/
|
||||
void
|
||||
udp_prime_port (uint16 init_port)
|
||||
|
||||
void udp_prime_port(uint16_t init_port)
|
||||
{
|
||||
udp_port = init_port;
|
||||
}
|
||||
/********************************************************************/
|
||||
void
|
||||
udp_bind_port (uint16 port, void (*handler)(NIF *, NBUF *))
|
||||
|
||||
void udp_bind_port(uint16_t port, void (*handler)(NIF *, NBUF *))
|
||||
{
|
||||
int index;
|
||||
|
||||
@@ -65,9 +55,8 @@ udp_bind_port (uint16 port, void (*handler)(NIF *, NBUF *))
|
||||
}
|
||||
}
|
||||
}
|
||||
/********************************************************************/
|
||||
void
|
||||
udp_free_port (uint16 port)
|
||||
|
||||
void udp_free_port(uint16_t port)
|
||||
{
|
||||
int index;
|
||||
|
||||
@@ -80,9 +69,8 @@ udp_free_port (uint16 port)
|
||||
}
|
||||
}
|
||||
}
|
||||
/********************************************************************/
|
||||
static void *
|
||||
udp_port_handler (uint16 port)
|
||||
|
||||
static void *udp_port_handler(uint16_t port)
|
||||
{
|
||||
int index;
|
||||
|
||||
@@ -95,11 +83,10 @@ udp_port_handler (uint16 port)
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
/********************************************************************/
|
||||
uint16
|
||||
udp_obtain_free_port (void)
|
||||
|
||||
uint16_t udp_obtain_free_port(void)
|
||||
{
|
||||
uint16 port;
|
||||
uint16_t port;
|
||||
|
||||
port = udp_port;
|
||||
if (--udp_port <= 255)
|
||||
@@ -107,50 +94,47 @@ udp_obtain_free_port (void)
|
||||
|
||||
return port;
|
||||
}
|
||||
/********************************************************************/
|
||||
int
|
||||
udp_send ( NIF *nif, uint8 *dest, int sport, int dport, NBUF *pNbuf)
|
||||
|
||||
int udp_send(NIF *nif, uint8_t *dest, int sport, int dport, NBUF *pNbuf)
|
||||
{
|
||||
/*
|
||||
* This function takes data and creates a UDP frame and
|
||||
* passes it onto the IP layer
|
||||
*/
|
||||
udp_frame_hdr *udpframe;
|
||||
udp_frame_hdr *udpframe;
|
||||
|
||||
udpframe = (udp_frame_hdr *)&pNbuf->data[UDP_HDR_OFFSET];
|
||||
udpframe = (udp_frame_hdr *) &pNbuf->data[UDP_HDR_OFFSET];
|
||||
|
||||
/* Set UDP source port */
|
||||
udpframe->src_port = (uint16)sport;
|
||||
udpframe->src_port = (uint16_t) sport;
|
||||
|
||||
/* Set UDP destination port */
|
||||
udpframe->dest_port = (uint16)dport;
|
||||
udpframe->dest_port = (uint16_t) dport;
|
||||
|
||||
/* Set length */
|
||||
udpframe->length = (uint16)(pNbuf->length + UDP_HDR_SIZE);
|
||||
udpframe->length = (uint16_t) (pNbuf->length + UDP_HDR_SIZE);
|
||||
|
||||
/* No checksum calcualation needed */
|
||||
udpframe->chksum = (uint16)0;
|
||||
udpframe->chksum = (uint16_t) 0;
|
||||
|
||||
/* Add the length of the UDP packet to the total length of the packet */
|
||||
pNbuf->length += 8;
|
||||
|
||||
return (ip_send(nif,
|
||||
dest,
|
||||
ip_get_myip(nif_get_protocol_info(nif,ETH_FRM_IP)),
|
||||
return (ip_send(nif, dest,
|
||||
ip_get_myip(nif_get_protocol_info(nif, ETH_FRM_IP)),
|
||||
IP_PROTO_UDP,
|
||||
pNbuf));
|
||||
}
|
||||
/********************************************************************/
|
||||
void
|
||||
udp_handler (NIF *nif, NBUF *pNbuf)
|
||||
|
||||
void udp_handler(NIF *nif, NBUF *pNbuf)
|
||||
{
|
||||
/*
|
||||
* This function handles incoming UDP packets
|
||||
*/
|
||||
udp_frame_hdr *udpframe;
|
||||
udp_frame_hdr *udpframe;
|
||||
void (*handler)(NIF *, NBUF *);
|
||||
|
||||
udpframe = (udp_frame_hdr *)&pNbuf->data[pNbuf->offset];
|
||||
udpframe = (udp_frame_hdr *) &pNbuf->data[pNbuf->offset];
|
||||
|
||||
/*
|
||||
* Adjust the length and valid data offset of the packet we are
|
||||
@@ -163,18 +147,13 @@ udp_handler (NIF *nif, NBUF *pNbuf)
|
||||
* Traverse the list of bound ports to see if there is a higher
|
||||
* level protocol to pass the packet on to
|
||||
*/
|
||||
if ((handler = (void(*)(NIF*,NBUF*))udp_port_handler(UDP_DEST(udpframe))) != NULL)
|
||||
if ((handler = (void(*)(NIF*, NBUF*)) udp_port_handler(UDP_DEST(udpframe))) != NULL)
|
||||
handler(nif, pNbuf);
|
||||
else
|
||||
{
|
||||
#ifdef DBUG_PRINT
|
||||
printf("Received UDP packet for non-supported port\n");
|
||||
#endif
|
||||
xprintf("Received UDP packet for non-supported port\n");
|
||||
nbuf_free(pNbuf);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
/********************************************************************/
|
||||
|
||||
#endif /* #ifdef DBUG_NETWORK */
|
||||
|
||||
Reference in New Issue
Block a user