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

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 "ip.h"
00023 
00024 uint32_t IpMyAddress;
00025 uint32_t IpNetmask;
00026 uint32_t IpGateway;
00027 
00028 void ipSetAddress(uint32_t myIp, uint32_t netmask, uint32_t gatewayIp)
00029 {
00030     IpMyAddress = myIp;
00031     IpNetmask = netmask;
00032     IpGateway = gatewayIp;
00033 }
00034 
00035 uint32_t ipGetMyAddress(void)
00036 {
00037     return IpMyAddress;
00038 }
00039 
00040 void ipSend(uint32_t dstIp, uint8_t protocol, uint16_t len, uint8_t* data)
00041 {
00042     // make pointer to ethernet/IP header
00043     struct netEthIpHeader* ethIpHeader;
00044 
00045     // move data pointer to make room for headers
00046     data -= ETH_HEADER_LEN+IP_HEADER_LEN;
00047     ethIpHeader = (struct netEthIpHeader*)data;
00048 
00049 //  debugPrintHexTable(len+ETH_HEADER_LEN+IP_HEADER_LEN, data);
00050 
00051     // adjust length to add IP header
00052     len += IP_HEADER_LEN;
00053 
00054     // fill IP header
00055     ethIpHeader->ip.destipaddr = HTONL(dstIp);
00056     ethIpHeader->ip.srcipaddr = HTONL(IpMyAddress);
00057     ethIpHeader->ip.proto = protocol;
00058     ethIpHeader->ip.len = htons(len);
00059     ethIpHeader->ip.vhl = 0x45;
00060     ethIpHeader->ip.tos = 0;
00061     ethIpHeader->ip.ipid = 0;
00062     ethIpHeader->ip.ipoffset = 0;
00063     ethIpHeader->ip.ttl = IP_TIME_TO_LIVE;
00064     ethIpHeader->ip.ipchksum = 0;
00065 
00066     // calculate and apply IP checksum
00067     // DO THIS ONLY AFTER ALL CHANGES HAVE BEEN MADE TO IP HEADER
00068     ethIpHeader->ip.ipchksum = netChecksum(&ethIpHeader->ip, IP_HEADER_LEN);
00069 
00070     // add ethernet routing
00071     // check if we need to send to gateway
00072     if( (dstIp & IpNetmask) == (IpMyAddress & IpNetmask) )
00073         arpIpOut(ethIpHeader,0);            // local send
00074     else
00075         arpIpOut(ethIpHeader,IpGateway);    // gateway send
00076 
00077     // adjust length to add ethernet header
00078     len += ETH_HEADER_LEN;
00079 
00080     // debug
00081     //debugPrintHexTable(ETH_HEADER_LEN, &data[0]);
00082     //debugPrintHexTable(len-ETH_HEADER_LEN, &data[ETH_HEADER_LEN]);
00083 
00084     // send it
00085     nicSend(len, data);
00086 }
00087 
00088 
00089 void udpSend(uint32_t dstIp, uint16_t dstPort, uint16_t len, uint8_t* data)
00090 {
00091     // make pointer to UDP header
00092     struct netUdpHeader* udpHeader;
00093     // move data pointer to make room for UDP header
00094     data -= UDP_HEADER_LEN;
00095     udpHeader = (struct netUdpHeader*)data;
00096     // adjust length to add UDP header
00097     len += UDP_HEADER_LEN;
00098     // fill UDP header
00099     udpHeader->destport = HTONS(dstPort);
00100     udpHeader->srcport  = HTONS(dstPort);
00101     udpHeader->udplen = htons(len);
00102     udpHeader->udpchksum = 0;
00103 
00104 //  debugPrintHexTable(UDP_HEADER_LEN, (uint8_t*)udpPacket);
00105 
00106     ipSend(dstIp, IP_PROTO_UDP, len, (uint8_t*)udpHeader);
00107 }

Generated on Mon Aug 22 04:29:27 2005 for Procyon AVRlib by  doxygen 1.4.2