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

ip.c

Go to the documentation of this file.
00001 /*! \file ip.c \brief IP (Internet Protocol) Library. */
00002 //*****************************************************************************
00003 //
00004 // File Name    : 'ip.c'
00005 // Title        : IP (Internet Protocol) 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 
00016 #include <inttypes.h>
00017 #include "global.h"
00018 #include "rprintf.h"
00019 
00020 #include "net.h"
00021 #include "nic.h"
00022 #include "arp.h"
00023 #include "ip.h"
00024 
00025 uint32_t IpMyAddress;   ///< Local IP address
00026 uint32_t IpNetmask;     ///< Local netmask
00027 uint32_t IpGateway;     ///< Local Gateway IP address
00028 
00029 void ipSetAddress(uint32_t myIp, uint32_t netmask, uint32_t gatewayIp)
00030 {
00031     struct netEthAddr ethaddr;
00032 
00033     IpMyAddress = myIp;
00034     IpNetmask = netmask;
00035     IpGateway = gatewayIp;
00036 
00037     // set ARP association
00038     nicGetMacAddress(ethaddr.addr);
00039     arpSetAddress(&ethaddr, myIp);
00040 }
00041 
00042 uint32_t ipGetMyAddress(void)
00043 {
00044     return IpMyAddress;
00045 }
00046 
00047 void ipSend(uint32_t dstIp, uint8_t protocol, uint16_t len, uint8_t* data)
00048 {
00049     // make pointer to ethernet/IP header
00050     struct netEthIpHeader* ethIpHeader;
00051 
00052     // move data pointer to make room for headers
00053     data -= ETH_HEADER_LEN+IP_HEADER_LEN;
00054     ethIpHeader = (struct netEthIpHeader*)data;
00055 
00056 //  debugPrintHexTable(len+ETH_HEADER_LEN+IP_HEADER_LEN, data);
00057 
00058     // adjust length to add IP header
00059     len += IP_HEADER_LEN;
00060 
00061     // fill IP header
00062     ethIpHeader->ip.destipaddr = HTONL(dstIp);
00063     ethIpHeader->ip.srcipaddr = HTONL(IpMyAddress);
00064     ethIpHeader->ip.proto = protocol;
00065     ethIpHeader->ip.len = htons(len);
00066     ethIpHeader->ip.vhl = 0x45;
00067     ethIpHeader->ip.tos = 0;
00068     ethIpHeader->ip.ipid = 0;
00069     ethIpHeader->ip.ipoffset = 0;
00070     ethIpHeader->ip.ttl = IP_TIME_TO_LIVE;
00071     ethIpHeader->ip.ipchksum = 0;
00072 
00073     // calculate and apply IP checksum
00074     // DO THIS ONLY AFTER ALL CHANGES HAVE BEEN MADE TO IP HEADER
00075     ethIpHeader->ip.ipchksum = netChecksum(&ethIpHeader->ip, IP_HEADER_LEN);
00076 
00077     // add ethernet routing
00078     // check if we need to send to gateway
00079     if( (dstIp & IpNetmask) == (IpMyAddress & IpNetmask) )
00080     {
00081         arpIpOut(ethIpHeader,0);            // local send
00082         //rprintf("Sending IP packet on local net\r\n");
00083     }
00084     else
00085     {
00086         arpIpOut(ethIpHeader,IpGateway);    // gateway send
00087         //rprintf("Sending IP packet to gateway\r\n");
00088     }
00089 
00090     // adjust length to add ethernet header
00091     len += ETH_HEADER_LEN;
00092 
00093     // debug
00094     //debugPrintHexTable(ETH_HEADER_LEN, &data[0]);
00095     //debugPrintHexTable(len-ETH_HEADER_LEN, &data[ETH_HEADER_LEN]);
00096 
00097     // send it
00098     nicSend(len, data);
00099 }
00100 
00101 
00102 void udpSend(uint32_t dstIp, uint16_t dstPort, uint16_t len, uint8_t* data)
00103 {
00104     // make pointer to UDP header
00105     struct netUdpHeader* udpHeader;
00106     // move data pointer to make room for UDP header
00107     data -= UDP_HEADER_LEN;
00108     udpHeader = (struct netUdpHeader*)data;
00109     // adjust length to add UDP header
00110     len += UDP_HEADER_LEN;
00111     // fill UDP header
00112     udpHeader->destport = HTONS(dstPort);
00113     udpHeader->srcport  = HTONS(dstPort);
00114     udpHeader->udplen = htons(len);
00115     udpHeader->udpchksum = 0;
00116 
00117 //  debugPrintHexTable(UDP_HEADER_LEN, (uint8_t*)udpPacket);
00118 
00119     ipSend(dstIp, IP_PROTO_UDP, len, (uint8_t*)udpHeader);
00120 }

Generated on Tue Sep 20 03:11:43 2005 for Procyon AVRlib by  doxygen 1.4.2