Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

# net.c

Go to the documentation of this file.

    00001 /*! \file net.c \brief Network support library. */
    00002 //*****************************************************************************
    00003 //
    00004 // File Name    : 'net.c'
    00005 // Title        : Network support library
    00006 // Author       : Pascal Stang
    00007 // Created      : 8/30/2004
    00008 // Revised      : 7/3/2005
    00009 // Version      : 0.1
    00010 // Target MCU   : Atmel AVR series
    00011 // Editor Tabs  : 4
    00012 //
    00013 //*****************************************************************************
    00014
    00015 #include <stdint.h>
    00016 #include "global.h"
    00017 #include "rprintf.h"
    00018
    00019 #include "net.h"
    00020
    00021 uint16_t htons(uint16_t val)
    00022 {
    00023     return ((val<<8) | ((val>>8)&0xFF))&0x0000FFFF;
    00024 /*
    00025     int tmp;
    00026
    00027     __asm __volatile(
    00028         "and    %1, %2, #0xff\n"
    00029         "mov    %0, %2, lsr #8\n"
    00030         "orr    %0, %0, %1, lsl #8"
    00031     : "=r" (v), "=r" (tmp)
    00032     : "r" (v));
    00033
    00034     return (v);
    00035 */
    00036 }
    00037
    00038 uint32_t htonl(uint32_t val)
    00039 {
    00040 //  return (htons(val>>16) | (uint32_t)htons(val&0x0000FFFF)<<16);
    00041 /*  return (((val&0xFF000000)>>24) |
    00042             ((val&0x00FF0000)>>8)  |
    00043             ((val&0x0000FF00)<<8)  |
    00044             ((val&0x000000FF)<<24) );
    00045 */
    00046     int tmp;
    00047
    00048     __asm __volatile(
    00049         "eor    %1, %2, %2, ror #16\n"
    00050         "bic    %1, %1, #0x00ff0000\n"
    00051         "mov    %0, %2, ror #8\n"
    00052         "eor    %0, %0, %1, lsr #8"
    00053     : "=r" (val), "=r" (tmp)
    00054     : "r" (val));
    00055
    00056     return (val);
    00057 }
    00058
    00059
    00060 uint16_t netChecksum(void *data, uint16_t len)
    00061 {
    00062     register uint32_t sum = 0;
    00063
    00064     for (;;) {
    00065         if (len < 2)
    00066             break;
    00067         //sum += *((uint16_t *)data)++;
    00068         sum += *((uint16_t *)data);
    00069         data+=2;
    00070         len -= 2;
    00071     }
    00072     if (len)
    00073         sum += *(uint8_t *) data;
    00074
    00075     while ((len = (uint16_t) (sum >> 16)) != 0)
    00076         sum = (uint16_t) sum + len;
    00077
    00078     return (uint16_t) sum ^ 0xFFFF;
    00079 }
    00080
    00081 void netPrintEthAddr(struct netEthAddr* ethaddr)
    00082 {
    00083     rprintfu08(ethaddr->addr[0]);
    00084     rprintfChar(':');
    00085     rprintfu08(ethaddr->addr[1]);
    00086     rprintfChar(':');
    00087     rprintfu08(ethaddr->addr[2]);
    00088     rprintfChar(':');
    00089     rprintfu08(ethaddr->addr[3]);
    00090     rprintfChar(':');
    00091     rprintfu08(ethaddr->addr[4]);
    00092     rprintfChar(':');
    00093     rprintfu08(ethaddr->addr[5]);
    00094 }
    00095
    00096 void netPrintIPAddr(uint32_t ipaddr)
    00097 {
    00098     rprintf("%d.%d.%d.%d",
    00099         ((unsigned char*)&ipaddr)[3],
    00100         ((unsigned char*)&ipaddr)[2],
    00101         ((unsigned char*)&ipaddr)[1],
    00102         ((unsigned char*)&ipaddr)[0]);
    00103 }
    00104
    00105 /*
    00106 void netPrintEthHeader(struct netEthHeader* eth_hdr)
    00107 {
    00108     rprintfProgStrM("Eth Packet Type: 0x");
    00109     rprintfu16(htons(eth_hdr->type));
    00110
    00111     rprintfProgStrM(" SRC:");
    00112     netPrintEthAddr(&eth_hdr->src);
    00113     rprintfProgStrM("->DST:");
    00114     netPrintEthAddr(&eth_hdr->dest);
    00115     rprintfCRLF();
    00116 }
    00117
    00118 void netPrintIpHeader(struct netIpHeader* ipheader)
    00119 {
    00120     rprintfProgStrM("IP Header\r\n");
    00121     rprintf("Ver     : %d\r\n", (ipheader->vhl)>>4);
    00122     rprintf("Length  : %d\r\n", htons(ipheader->len));
    00123     if(ipheader->proto == IP_PROTO_ICMP)
    00124         rprintfProgStrM("Protocol: ICMP\r\n");
    00125     else if(ipheader->proto == IP_PROTO_TCP)
    00126         rprintfProgStrM("Protocol: TCP\r\n");
    00127     else if(ipheader->proto == IP_PROTO_UDP)
    00128         rprintfProgStrM("Protocol: UDP\r\n");
    00129     else
    00130         rprintf("Protocol: %d\r\n", ipheader->proto);
    00131
    00132     rprintfProgStrM("SourceIP: "); netPrintIPAddr(htonl(ipheader->srcipaddr));  rprintfCRLF();
    00133     rprintfProgStrM("Dest  IP: "); netPrintIPAddr(htonl(ipheader->destipaddr)); rprintfCRLF();
    00134 }
    00135
    00136 void netPrintTcpHeader(struct netTcpHeader* tcpheader)
    00137 {
    00138     rprintfProgStrM("TCP Header\r\n");
    00139     rprintf("Src Port: %d\r\n", htons(tcpheader->srcport));
    00140     rprintf("Dst Port: %d\r\n", htons(tcpheader->destport));
    00141     rprintfProgStrM("Seq Num : 0x"); rprintfu32(htonl(tcpheader->seqno));   rprintfCRLF();
    00142     rprintfProgStrM("Ack Num : 0x"); rprintfu32(htonl(tcpheader->ackno));   rprintfCRLF();
    00143     rprintfProgStrM("Flags   : ");
    00144     if(tcpheader->flags & TCP_FLAGS_FIN)
    00145         rprintfProgStrM("FIN ");
    00146     if(tcpheader->flags & TCP_FLAGS_SYN)
    00147         rprintfProgStrM("SYN ");
    00148     if(tcpheader->flags & TCP_FLAGS_RST)
    00149         rprintfProgStrM("RST ");
    00150     if(tcpheader->flags & TCP_FLAGS_PSH)
    00151         rprintfProgStrM("PSH ");
    00152     if(tcpheader->flags & TCP_FLAGS_ACK)
    00153         rprintfProgStrM("ACK ");
    00154     if(tcpheader->flags & TCP_FLAGS_URG)
    00155         rprintfProgStrM("URG ");
    00156     rprintfCRLF();
    00157 }
    00158 */
    00159