00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
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;
00026
00027
00028 void ipSetConfig(uint32_t myIp, uint32_t netmask, uint32_t gatewayIp)
00029 {
00030 struct netEthAddr ethaddr;
00031
00032
00033 IpMyConfig.ip = myIp;
00034 IpMyConfig.netmask = netmask;
00035 IpMyConfig.gateway = gatewayIp;
00036
00037
00038 nicGetMacAddress(ethaddr.addr);
00039 arpSetAddress(ðaddr, myIp);
00040 }
00041
00042 struct ipConfig* ipGetConfig(void)
00043 {
00044 return &IpMyConfig;
00045 }
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 void ipSend(uint32_t dstIp, uint8_t protocol, uint16_t len, uint8_t* data)
00056 {
00057
00058 struct netEthIpHeader* ethIpHeader;
00059
00060
00061 data -= ETH_HEADER_LEN+IP_HEADER_LEN;
00062 ethIpHeader = (struct netEthIpHeader*)data;
00063
00064
00065
00066
00067 len += IP_HEADER_LEN;
00068
00069
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
00082
00083 ethIpHeader->ip.ipchksum = netChecksum(ðIpHeader->ip, IP_HEADER_LEN);
00084
00085
00086
00087 if( (dstIp & IpMyConfig.netmask) == (IpMyConfig.ip & IpMyConfig.netmask) )
00088 {
00089 arpIpOut(ethIpHeader,0);
00090
00091 }
00092 else
00093 {
00094 arpIpOut(ethIpHeader,IpMyConfig.gateway);
00095
00096 }
00097
00098
00099 len += ETH_HEADER_LEN;
00100
00101
00102
00103
00104
00105
00106 nicSend(len, data);
00107 }
00108
00109 void udpSend(uint32_t dstIp, uint16_t dstPort, uint16_t len, uint8_t* data)
00110 {
00111
00112 struct netUdpHeader* udpHeader;
00113
00114 data -= UDP_HEADER_LEN;
00115 udpHeader = (struct netUdpHeader*)data;
00116
00117 len += UDP_HEADER_LEN;
00118
00119 udpHeader->destport = HTONS(dstPort);
00120 udpHeader->srcport = HTONS(dstPort);
00121 udpHeader->udplen = htons(len);
00122 udpHeader->udpchksum = 0;
00123
00124
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 }