www.pudn.com > pppcode.rar > ip.c


/*///////////////////////////////////////////////////////////////////////////// 
File Name : IP.C 
Author : Rene Trenado 
Location : Motorola Applications Lab, Baja California 
Date Created : September 2000 
Current Revision : 0.0 
Notes : This file contains the Internet Protocol variables & support routines 
/////////////////////////////////////////////////////////////////////////////*/ 
#include "ip.h" 
#include "ppp.h" 
extern BYTE InBuffer [PPP_BUFFER_SIZE + 1]; // Input Buffer for PPP data 
extern BYTE OutBuffer[PPP_BUFFER_SIZE + 1]; 
BYTE IPAddress[4] = {220, 1, 141, 149}; // Default IP Address 
static volatile char IPAdapter = PPP; // Default interface for IP output 
IPDatagram *ip_in; // A pointer to received IP datagrams 
IPDatagram *ip_out; // Global buffer for IP packet output 
/*********************************************************************** 
Function : IPInit 
Parameters : None 
Date : September 2000 
Desc : Initializes the IP module pointers***********************************************************************/ 
void IPInit (void) { 
ip_in = (IPDatagram *)&InBuffer [4]; 
ip_out = (IPDatagram *)&OutBuffer [4]; 
} 
/*********************************************************************** 
Function : Bind adapter 
Parameters : Interface: A Byte ID 
Date : September 2000 
Desc : Selects the output format of an IP packet 
***********************************************************************/ 
void IPBindAdapter (INTERFACE Interface) { 
IPAdapter = Interface; // switch to different output interface 
} 
/*********************************************************************** 
Function : IPNetSend 
Parameters : ip: A pointer to a IP datagram to transmit 
Date : November 2000 
Desc : Sends a IP datagram over the interface specified 
***********************************************************************/ 
void IPNetSend (IPDatagram* ip) { 
static WORD Id = 0xF0; // ID to be used in IP datagrams 
ip_out->Version_HLen = 0x45; // Header Forma=IPv4, Length = 5 
ip_out->Service = 0; // Always zero 
ip_out->LengthUpper = 0; // High byte of datagram Length 
ip_out->ID = htons(Id++); // Merge IP ID 
ip_out->Frag = 0; // No flags nor enable fragmentation 
ip_out->TTL = 0x80; // Time to live set to default 
ip_out->Checksum = 0; // Clear checksum to avoid 
// miscalculations 
// Get checksum of entire datagram 
ip_out->Checksum = htons(IPCheckSum ((BYTE *)ip_out, 10)); 
switch (IPAdapter) { // Select the adapter to output the IP datagram 
case PPP: // Output through PPP adapter 
OutBuffer [0] = 0xff; // Frame PPP packet 
OutBuffer [1] = 0x03; 
OutBuffer [2] = 0x00; // This is a IP datagram, set 
// protocol type 
OutBuffer [3] = 0x21;ProcPPPSend (OutBuffer, OutBuffer [7] + 6); 
break; 
//case SLIP: // Output through SLIP interface 
//ProcSLIPSend ((BYTE *)ip_out, ip_out->Length); 
//break; 
case ETHERNET: // Send datagram over ethernet 
break; 
default: 
break; 
} 
} 
/*********************************************************************** 
Function : IPCompare 
Parameters : Ip: A pointer to a IP address to compare 
Date : November 2000 
Desc : Compares an IP address to the default IP address defined 
in this module 
***********************************************************************/ 
BYTE IPCompare (BYTE *IPOne) { 
if (IPOne [0] != IPAddress[0]) return (BYTE)0x00; 
if (IPOne [1] != IPAddress[1]) return (BYTE)0x00; 
if (IPOne [2] != IPAddress[2]) return (BYTE)0x00; 
if (IPOne [3] != IPAddress[3]) return (BYTE)0x00; 
return (BYTE) 0x01; 
} 
/*********************************************************************** 
Function : IPChecksum 
Parameters : Data: A pointer to an array of Words 
Size: Size of the array 
Date : August 2000 
Desc : Obtains the IP checksum of an array of 16-bit words of size "Size" 
***********************************************************************/ 
DWORD IPCheckSum (BYTE* Data, WORD Size) { 
unsigned long Sum = 0; 
while (Size-->0) { 
Sum += ((unsigned long)((*Data << 8) + *(Data+1)) & 0xFFFF);Data+=2; 
} 
Sum = (Sum >> 16) + (Sum & 0xFFFF); 
Sum += (Sum >> 16); 
return (WORD) ~Sum; 
}