TCP and UDP Server - tcpd
#include <tcpinit.h>
#include <sys/socket.h>
#include <netinet/in.h>
The tcp and udp server or tcpd provides IPv4/IPv6 TCP, UDP and IP protocols support with low level drivers for specific chip support packages. It is a fast and easy to use TCP/UDP solution which requires a simple setup structure at creation time to get it up and running. It offers static IP addresses in IPv4 format or a DHCP client for IP address retrieval. It offers static routing tables, multiple interfaces and great small packet performance on processors with less than 32K of RAM. IPv6 protocol uses automaticaly configured link-local IP address based on host MAC.
The tcpd is an extensively modified version of the zero copy BSD implementation. It has been reduced in size and provides standard BSD socket services including support for select. It has been in the field in various incarnations for over 20 years.
The tcpd is initialized using the structure defined in tcpinit.h. It is passed to the tcpd from tcp_shell which starts the actual server passing in the parameter. The structure works this way for historical reasons - it could easily be restructured to take the parameter in the pthread_create call and start the server directly. For future reasons relating to parallelization of the tcpd, this change was not made.
The tcpd has several compile time options to reduce server size and several initialization options to reduce system size. Through recompilation, a udp only solution may be created reducing total system size to under 11K bytes. Initialization options which are geared to reducing the buffer sizes are tunable to trade off performance and size. Multiple interfaces are compile time options.
The tcpd also has several add on features that many will be interested in. These features include:
The tcpd compile time options can be found in the file <tcpdconfig.h> from server directory. All options can be defined as 0 (OFF) or 1 (ON):
The tcpd initiatization structure can be found in the file <tcpinit.h>:
//
// tcpd initialization structure
//
struct tcpinit {
uint_16 tcp_msgsize; /* size of message */
uint_32 tcp_default_gateway; /* default gateway */
int tcp_devport; /* device address */
int tcp_vector; /* device vector */
uint_32 tcp_IP_address; /* internet address */
unsigned char tcp_ether_address[6]; /* ethernet address */
unsigned char tcp_padding[2]; /* padding */
int tcp_level; /* device int level */
int tcp_unit; /* unit number */
char *tcp_pool_start; /* buffer address */
uint tcp_pool_size; /* buffer size */
char tcp_devname[8]; /* device name */
};
Structure fields description:
This is an example of the various setup parameters for the tcpd - in this case using SPI communication with a enc28J60.
#define GW_IP_ADDR 0xf010a8c0 //"192.168.16.240"
#define ETH_ADDR 0x100000 //enc28J60 chip address
#define ETH_INT_VECT 0 //Interrupt #0
#define ETH_INT_LEVEL 3 //Interrupt priority
static char tcpbuf[1024*8];
struct tcpinit etherinit = {
sizeof(struct tcpinit), /* message size */
GW_IP_ADDR, /* default gateway */
ETH_ADDR, /* address of ethernet chip */
ETH_INT_VECT, /* interrupt vector */
0xc710a8c0, /* internet address *///"192.168.16.199"
{0x00,0x04,0xa3,0x00,0x4a,0x19}, /* ethernet address */
{0,0}, /* padding */
ETH_INT_LEVEL, /* interrupt level */
0, /* unit number */
tcpbuf, /* buffer pointer */
sizeof(tcpbuf), /* size of buffer */
{'l','e',0}, /* interface name */
};
This partial example shows the creation and registration of the tcpd using tcp_shell.
THREAD Main()
{
pthread_t pid;
pthread_attr_t attr;
struct sched_param myNewPriority;
...
pthread_attr_init(&attr);
myNewPriority.sched_priority = 5;
pthread_attr_setschedparam(&attr, &myNewPriority);
pthread_attr_setstacksize(&attr, 2048);
if(pthread_create(&pid, &attr, &tcp_shell, 0)!=0)
{
xprintf("pthread_create = %d\n", errno);
pthread_exit(0);
}
if(dir_register("/dev/tcpd", pid, TYPE_SERVER)==0)
{
xprintf("dir_register = %d\n", errno);
pthread_exit(0);
}
...
}
THREAD tcp_shell(void *arg)
{
tcpd((char *)ðerinit, 0);
}
There is several demos available for the Unison and DSPnano tcpd which are found in installdir/demos which provide examples of using the server with blocking and non-blocking sockets, select, and a variety of services including tftp, telnet, and more.
socket(), bind(), accept(), recv(), listen(), send(), select(), dhcp client service, dhcp server, ppp server, Network Address Translation