Inside the uIP Stack

by Drew Barnett and Anthony J. Massa



Listing One



(a)

typedef struct {

  /* IP header. */

  u8_t vhl,

    tos,          

    len[2],       

    ipid[2],        

    ipoffset[2],  

    ttl,          

    proto;     

  u16_t ipchksum;

  u16_t srcipaddr[2], 

    destipaddr[2];

  /* TCP header. */

  u16_t srcport,

    destport;

  u8_t seqno[4],  

    ackno[4],

    tcpoffset,

    flags,

    wnd[2];     

  u16_t tcpchksum;

  u8_t urgp[2];

  u8_t optdata[4];

} uip_tcpip_hdr;





(b)



typedef struct {

  /* IP header. */

  u8_t vhl,

    tos,          

    len[2],       

    ipid[2],        

    ipoffset[2],  

    ttl,          

    proto;     

  u16_t ipchksum[2];

  u16_t srcipaddr[4],

    destipaddr[4];

  /* TCP header. */

  u16_t srcport[2],

    destport[2];

  u8_t seqno[4],  

    ackno[4],

    tcpoffset,

    flags,

    wnd[2];     

  u16_t tcpchksum[2];

  u8_t urgp[2];

  u8_t optdata[4];

} uip_tcpip_hdr;





Listing Two

(a)

/* Swap port numbers. */

tmp16 = BUF->srcport;

BUF->srcport = BUF->destport;

BUF->destport = tmp16;



(b)  

/* Swap port numbers. */

tmp16 = ((BUF->srcport[1] << 8) | BUF->srcport[0]);

BUF->srcport[0] = BUF->destport[0];

BUF->srcport[1] = BUF->destport[1];

BUF->destport[0] = (tmp16 & 0x00FF);

BUF->destport[1] = ((tmp16 >> 8) & 0x00FF);





Listing Three



// Initialize and start the uIP timer to fire every 1 ms.

timer_init();

// Initialize the serial port for use with the uIP stack.

serial_init();

// uIP networking stack related initialization.

slipdev_init();

uip_init();

httpd_init();

// Main processing loop.

while (1)

{

    // If we get a length greater than zero, we know a packet is ready for

    // processing. Otherwise, we call the periodic function once per second.

    uip_len = slipdev_poll();

    if (uip_len == 0)

    {

        // Call the periodic function once per second and loop through all

        // connections.

        if (ulTimer0Count >= 1000)

        {

            ulTimer0Count = 0;

            for (uiConn = 0; uiConn < UIP_CONNS; uiConn++)

            {

               uip_periodic( uiConn );

               // If the periodic function resulted in data that needs to

               // to be sent out, length is set to a value greater than zero.

               if (uip_len > 0)

                  slipdev_send();

            }

        }

    }

    else

    {

        // Process the incoming data.

        uip_input();



        // If the input function resulted in data that needs to

        // to be sent out, the length is set to a value greater than zero.

        if (uip_len > 0)

            slipdev_send();

    }

} // End of main loop.













1



