| Line No. | Rev | Author | Line | 
|---|---|---|---|
| 1 | 6 | kaklik | /*! \file ip.c \brief IP (Internet Protocol) Library. */ | 
| 2 | //***************************************************************************** | ||
| 3 | // | ||
| 4 | // File Name	: 'ip.c' | ||
| 5 | // Title		: IP (Internet Protocol) Library | ||
| 6 | // Author		: Pascal Stang | ||
| 7 | // Created		: 8/30/2004 | ||
| 8 | // Revised		: 7/3/2005 | ||
| 9 | // Version		: 0.1 | ||
| 10 | // Target MCU	: Atmel AVR series | ||
| 11 | // Editor Tabs	: 4 | ||
| 12 | // | ||
| 13 | //***************************************************************************** | ||
| 14 | |||
| 15 | |||
| 16 | #include <inttypes.h> | ||
| 17 | #include "global.h" | ||
| 18 | #include "rprintf.h" | ||
| 19 | |||
| 20 | #include "net.h" | ||
| 21 | #include "nic.h" | ||
| 22 | #include "arp.h" | ||
| 23 | #include "ip.h" | ||
| 24 | |||
| 25 | struct ipConfig IpMyConfig;	///< Local IP address/config structure | ||
| 26 | |||
| 27 | |||
| 28 | void ipSetConfig(uint32_t myIp, uint32_t netmask, uint32_t gatewayIp) | ||
| 29 | { | ||
| 30 | 	struct netEthAddr ethaddr; | ||
| 31 | |||
| 32 | 	// set local addressing | ||
| 33 | 	IpMyConfig.ip = myIp; | ||
| 34 | 	IpMyConfig.netmask = netmask; | ||
| 35 | 	IpMyConfig.gateway = gatewayIp; | ||
| 36 | |||
| 37 | 	// set ARP association | ||
| 38 | 	nicGetMacAddress(ethaddr.addr); | ||
| 39 | 	arpSetAddress(ðaddr, myIp); | ||
| 40 | } | ||
| 41 | |||
| 42 | struct ipConfig* ipGetConfig(void) | ||
| 43 | { | ||
| 44 | 	return &IpMyConfig; | ||
| 45 | } | ||
| 46 | |||
| 47 | // deprecated | ||
| 48 | /* | ||
| 49 | uint32_t ipGetMyAddress(void) | ||
| 50 | { | ||
| 51 | 	return IpMyConfig.ip; | ||
| 52 | } | ||
| 53 | */ | ||
| 54 | |||
| 55 | void ipSend(uint32_t dstIp, uint8_t protocol, uint16_t len, uint8_t* data) | ||
| 56 | { | ||
| 57 | 	// make pointer to ethernet/IP header | ||
| 58 | 	struct netEthIpHeader* ethIpHeader; | ||
| 59 | |||
| 60 | 	// move data pointer to make room for headers | ||
| 61 | 	data -= ETH_HEADER_LEN+IP_HEADER_LEN; | ||
| 62 | 	ethIpHeader = (struct netEthIpHeader*)data; | ||
| 63 | |||
| 64 | //	debugPrintHexTable(len+ETH_HEADER_LEN+IP_HEADER_LEN, data); | ||
| 65 | |||
| 66 | 	// adjust length to add IP header | ||
| 67 | 	len += IP_HEADER_LEN; | ||
| 68 | |||
| 69 | 	// fill IP header | ||
| 70 | 	ethIpHeader->ip.destipaddr = HTONL(dstIp); | ||
| 71 | 	ethIpHeader->ip.srcipaddr = HTONL(IpMyConfig.ip); | ||
| 72 | 	ethIpHeader->ip.proto = protocol; | ||
| 73 | 	ethIpHeader->ip.len = htons(len); | ||
| 74 | 	ethIpHeader->ip.vhl = 0x45; | ||
| 75 | 	ethIpHeader->ip.tos = 0; | ||
| 76 | 	ethIpHeader->ip.ipid = 0; | ||
| 77 | 	ethIpHeader->ip.ipoffset = 0; | ||
| 78 | 	ethIpHeader->ip.ttl = IP_TIME_TO_LIVE; | ||
| 79 | 	ethIpHeader->ip.ipchksum = 0; | ||
| 80 | |||
| 81 | 	// calculate and apply IP checksum | ||
| 82 | 	// DO THIS ONLY AFTER ALL CHANGES HAVE BEEN MADE TO IP HEADER | ||
| 83 | 	ethIpHeader->ip.ipchksum = netChecksum(ðIpHeader->ip, IP_HEADER_LEN); | ||
| 84 | |||
| 85 | 	// add ethernet routing | ||
| 86 | 	// check if we need to send to gateway | ||
| 87 | 	if( (dstIp & IpMyConfig.netmask) == (IpMyConfig.ip & IpMyConfig.netmask) ) | ||
| 88 | 	{ | ||
| 89 | 		arpIpOut(ethIpHeader,0);					// local send | ||
| 90 | 		//rprintf("Sending IP packet on local net\r\n"); | ||
| 91 | 	} | ||
| 92 | 	else | ||
| 93 | 	{ | ||
| 94 | 		arpIpOut(ethIpHeader,IpMyConfig.gateway);	// gateway send | ||
| 95 | 		//rprintf("Sending IP packet to gateway\r\n"); | ||
| 96 | 	} | ||
| 97 | |||
| 98 | 	// adjust length to add ethernet header | ||
| 99 | 	len += ETH_HEADER_LEN; | ||
| 100 | |||
| 101 | 	// debug | ||
| 102 | 	//debugPrintHexTable(ETH_HEADER_LEN, &data[0]); | ||
| 103 | 	//debugPrintHexTable(len-ETH_HEADER_LEN, &data[ETH_HEADER_LEN]); | ||
| 104 | |||
| 105 | 	// send it | ||
| 106 | 	nicSend(len, data); | ||
| 107 | } | ||
| 108 | |||
| 109 | void udpSend(uint32_t dstIp, uint16_t dstPort, uint16_t len, uint8_t* data) | ||
| 110 | { | ||
| 111 | 	// make pointer to UDP header | ||
| 112 | 	struct netUdpHeader* udpHeader; | ||
| 113 | 	// move data pointer to make room for UDP header | ||
| 114 | 	data -= UDP_HEADER_LEN; | ||
| 115 | 	udpHeader = (struct netUdpHeader*)data; | ||
| 116 | 	// adjust length to add UDP header | ||
| 117 | 	len += UDP_HEADER_LEN; | ||
| 118 | 	// fill UDP header | ||
| 119 | 	udpHeader->destport = HTONS(dstPort); | ||
| 120 | 	udpHeader->srcport  = HTONS(dstPort); | ||
| 121 | 	udpHeader->udplen = htons(len); | ||
| 122 | 	udpHeader->udpchksum = 0; | ||
| 123 | |||
| 124 | //	debugPrintHexTable(UDP_HEADER_LEN, (uint8_t*)udpPacket); | ||
| 125 | |||
| 126 | 	ipSend(dstIp, IP_PROTO_UDP, len, (uint8_t*)udpHeader); | ||
| 127 | } | ||
| 128 | |||
| 129 | void ipPrintConfig(struct ipConfig* config) | ||
| 130 | { | ||
| 131 | 	rprintfProgStrM("IP Addr : "); netPrintIPAddr(config->ip);		rprintfCRLF(); | ||
| 132 | 	rprintfProgStrM("Netmask : "); netPrintIPAddr(config->netmask);	rprintfCRLF(); | ||
| 133 | 	rprintfProgStrM("Gateway : "); netPrintIPAddr(config->gateway);	rprintfCRLF(); | ||
| 134 | } | 
Powered by WebSVN v2.8.3