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 struct ipConfig IpMyConfig; ///< Local IP address/config structure
00026 
00027 
00028 void ipSetConfig(uint32_t myIp, uint32_t netmask, uint32_t gatewayIp)
00029 {
00030     struct netEthAddr ethaddr;
00031 
00032     // set local addressing
00033     IpMyConfig.ip = myIp;
00034     IpMyConfig.netmask = netmask;
00035     IpMyConfig.gateway = gatewayIp;
00036 
00037     // set ARP association
00038     nicGetMacAddress(ethaddr.addr);
00039     arpSetAddress(&ethaddr, myIp);
00040 }
00041 
00042 struct ipConfig* ipGetConfig(void)
00043 {
00044     return &IpMyConfig;
00045 }
00046 
00047 // deprecated
00048 /*
00049 uint32_t ipGetMyAddress(void)
00050 {
00051     return IpMyConfig.ip;
00052 }
00053 */
00054 
00055 void ipSend(uint32_t dstIp, uint8_t protocol, uint16_t len, uint8_t* data)
00056 {
00057     // make pointer to ethernet/IP header
00058     struct netEthIpHeader* ethIpHeader;
00059 
00060     // move data pointer to make room for headers
00061     data -= ETH_HEADER_LEN+IP_HEADER_LEN;
00062     ethIpHeader = (struct netEthIpHeader*)data;
00063 
00064 //  debugPrintHexTable(len+ETH_HEADER_LEN+IP_HEADER_LEN, data);
00065 
00066     // adjust length to add IP header
00067     len += IP_HEADER_LEN;
00068 
00069     // fill IP header
00070     ethIpHeader->ip.destipaddr = HTONL(dstIp);
00071     ethIpHeader->ip.srcipaddr = HTONL(IpMyConfig.ip);
00072     ethIpHeader->ip.proto = protocol;
00073     ethIpHeader->ip.len = htons(len);
00074     ethIpHeader->ip.vhl = 0x45;
00075     ethIpHeader->ip.tos = 0;
00076     ethIpHeader->ip.ipid = 0;
00077     ethIpHeader->ip.ipoffset = 0;
00078     ethIpHeader->ip.ttl = IP_TIME_TO_LIVE;
00079     ethIpHeader->ip.ipchksum = 0;
00080 
00081     // calculate and apply IP checksum
00082     // DO THIS ONLY AFTER ALL CHANGES HAVE BEEN MADE TO IP HEADER
00083     ethIpHeader->ip.ipchksum = netChecksum(&ethIpHeader->ip, IP_HEADER_LEN);
00084 
00085     // add ethernet routing
00086     // check if we need to send to gateway
00087     if( (dstIp & IpMyConfig.netmask) == (IpMyConfig.ip & IpMyConfig.netmask) )
00088     {
00089         arpIpOut(ethIpHeader,0);                    // local send
00090         //rprintf("Sending IP packet on local net\r\n");
00091     }
00092     else
00093     {
00094         arpIpOut(ethIpHeader,IpMyConfig.gateway);   // gateway send
00095         //rprintf("Sending IP packet to gateway\r\n");
00096     }
00097 
00098     // adjust length to add ethernet header
00099     len += ETH_HEADER_LEN;
00100 
00101     // debug
00102     //debugPrintHexTable(ETH_HEADER_LEN, &data[0]);
00103     //debugPrintHexTable(len-ETH_HEADER_LEN, &data[ETH_HEADER_LEN]);
00104 
00105     // send it
00106     nicSend(len, data);
00107 }
00108 
00109 void udpSend(uint32_t dstIp, uint16_t dstPort, uint16_t len, uint8_t* data)
00110 {
00111     // make pointer to UDP header
00112     struct netUdpHeader* udpHeader;
00113     // move data pointer to make room for UDP header
00114     data -= UDP_HEADER_LEN;
00115     udpHeader = (struct netUdpHeader*)data;
00116     // adjust length to add UDP header
00117     len += UDP_HEADER_LEN;
00118     // fill UDP header
00119     udpHeader->destport = HTONS(dstPort);
00120     udpHeader->srcport  = HTONS(dstPort);
00121     udpHeader->udplen = htons(len);
00122     udpHeader->udpchksum = 0;
00123 
00124 //  debugPrintHexTable(UDP_HEADER_LEN, (uint8_t*)udpPacket);
00125 
00126     ipSend(dstIp, IP_PROTO_UDP, len, (uint8_t*)udpHeader);
00127 }
00128 
00129 void ipPrintConfig(struct ipConfig* config)
00130 {
00131     rprintfProgStrM("IP Addr : "); netPrintIPAddr(config->ip);      rprintfCRLF();
00132     rprintfProgStrM("Netmask : "); netPrintIPAddr(config->netmask); rprintfCRLF();
00133     rprintfProgStrM("Gateway : "); netPrintIPAddr(config->gateway); rprintfCRLF();
00134 }

Generated on Sun Oct 29 03:41:07 2006 for Procyon AVRlib by  doxygen 1.4.2