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 <inttypes.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);
    00024 }
    00025
    00026 uint32_t htonl(uint32_t val)
    00027 {
    00028     return (htons(val>>16) | (uint32_t)htons(val&0x0000FFFF)<<16);
    00029 }
    00030
    00031
    00032 uint16_t netChecksum(void *data, uint16_t len)
    00033 {
    00034     register uint32_t sum = 0;
    00035
    00036     for (;;) {
    00037         if (len < 2)
    00038             break;
    00039         //sum += *((uint16_t *)data)++;
    00040         sum += *((uint16_t *)data);
    00041         data+=2;
    00042         len -= 2;
    00043     }
    00044     if (len)
    00045         sum += *(uint8_t *) data;
    00046
    00047     while ((len = (uint16_t) (sum >> 16)) != 0)
    00048         sum = (uint16_t) sum + len;
    00049
    00050     return (uint16_t) sum ^ 0xFFFF;
    00051 }
    00052
    00053 void netPrintEthAddr(struct netEthAddr* ethaddr)
    00054 {
    00055     rprintfu08(ethaddr->addr[0]);
    00056     rprintfChar(':');
    00057     rprintfu08(ethaddr->addr[1]);
    00058     rprintfChar(':');
    00059     rprintfu08(ethaddr->addr[2]);
    00060     rprintfChar(':');
    00061     rprintfu08(ethaddr->addr[3]);
    00062     rprintfChar(':');
    00063     rprintfu08(ethaddr->addr[4]);
    00064     rprintfChar(':');
    00065     rprintfu08(ethaddr->addr[5]);
    00066 }
    00067
    00068 void netPrintIPAddr(uint32_t ipaddr)
    00069 {
    00070     rprintf("%d.%d.%d.%d",
    00071         ((unsigned char*)&ipaddr)[3],
    00072         ((unsigned char*)&ipaddr)[2],
    00073         ((unsigned char*)&ipaddr)[1],
    00074         ((unsigned char*)&ipaddr)[0]);
    00075 }
    00076
    00077 /*
    00078 void netPrintEthHeader(struct netEthHeader* eth_hdr)
    00079 {
    00080     rprintfProgStrM("Eth Packet Type: 0x");
    00081     rprintfu16(eth_hdr->type);
    00082
    00083     rprintfProgStrM(" SRC:");
    00084     netPrintEthAddr(&eth_hdr->src);
    00085     rprintfProgStrM("->DST:");
    00086     netPrintEthAddr(&eth_hdr->dest);
    00087     rprintfCRLF();
    00088 }
    00089
    00090 void netPrintIpHeader(struct netIpHeader* ipheader)
    00091 {
    00092     rprintfProgStrM("IP Header\r\n");
    00093     rprintf("Ver     : %d\r\n", (ipheader->vhl)>>4);
    00094     rprintf("Length  : %d\r\n", htons(ipheader->len));
    00095     if(ipheader->proto == IP_PROTO_ICMP)
    00096         rprintfProgStrM("Protocol: ICMP\r\n");
    00097     else if(ipheader->proto == IP_PROTO_TCP)
    00098         rprintfProgStrM("Protocol: TCP\r\n");
    00099     else if(ipheader->proto == IP_PROTO_UDP)
    00100         rprintfProgStrM("Protocol: UDP\r\n");
    00101     else
    00102         rprintf("Protocol: %d\r\n", ipheader->proto);
    00103
    00104     rprintfProgStrM("SourceIP: "); netPrintIPAddr(htonl(ipheader->srcipaddr));  rprintfCRLF();
    00105     rprintfProgStrM("Dest  IP: "); netPrintIPAddr(htonl(ipheader->destipaddr)); rprintfCRLF();
    00106 }
    00107
    00108 void netPrintTcpHeader(struct netTcpHeader* tcpheader)
    00109 {
    00110     rprintfProgStrM("TCP Header\r\n");
    00111     rprintf("Src Port: %d\r\n", htons(tcpheader->srcport));
    00112     rprintf("Dst Port: %d\r\n", htons(tcpheader->destport));
    00113     rprintfProgStrM("Seq Num : 0x"); rprintfu32(htonl(tcpheader->seqno));   rprintfCRLF();
    00114     rprintfProgStrM("Ack Num : 0x"); rprintfu32(htonl(tcpheader->ackno));   rprintfCRLF();
    00115     rprintfProgStrM("Flags   : ");
    00116     if(tcpheader->flags & TCP_FLAGS_FIN)
    00117         rprintfProgStrM("FIN ");
    00118     if(tcpheader->flags & TCP_FLAGS_SYN)
    00119         rprintfProgStrM("SYN ");
    00120     if(tcpheader->flags & TCP_FLAGS_RST)
    00121         rprintfProgStrM("RST ");
    00122     if(tcpheader->flags & TCP_FLAGS_PSH)
    00123         rprintfProgStrM("PSH ");
    00124     if(tcpheader->flags & TCP_FLAGS_ACK)
    00125         rprintfProgStrM("ACK ");
    00126     if(tcpheader->flags & TCP_FLAGS_URG)
    00127         rprintfProgStrM("URG ");
    00128     rprintfCRLF();
    00129 }
    00130 */
    00131