📄 ip.c
字号:
/*
* Copyright (C) 2003-2004 by Clive Moss. Email c.a.m@blueyonder.co.uk All rights reserved.
*
* Help & Contributions from D.J.Armstrong Email heli.pad@ntlworld.com
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY CLIVE MOSS 'AS IS' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED.
* IN NO EVENT SHALL CLIVE MOSS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
//#ifdef CPU_eZ8
// #pragma stkck // enable stack checking
//#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "ppp.h"
#include "ip.h"
#ifdef IncludeTCP
#include "tcp.h"
#endif
#ifdef IncludeICMP
#include "icmp.h"
#endif
#ifdef IncludeUDP
#include "udp.h"
#endif
#ifdef Debug
flash char ip_str1[] = "\nT";
flash char ip_str2[] = "\nR";
flash char ip_str2a[] = "X: Prot: PPP_IP ";
flash char ip_str3[] = " IPHdr: Ver:";
flash char ip_str4[] = " HdrLen:";
flash char ip_str5[] = " Tos:";
flash char ip_str6[] = " TotLen:";
flash char ip_str7[] = " ID:";
flash char ip_str8[] = " Frag:";
flash char ip_str9[] = " TTL:";
flash char ip_str10[] = " ICMP";
flash char ip_str11[] = " UDP";
flash char ip_str12[] = " TCP";
flash char ip_str13[] = " Chksum:";
flash char ip_str14[] = " option:";
flash char ip_str15[] = " DataBytes:";
flash char ip_str16[] = "*** IP chksum err\n";
flash char ip_str17[] = "*** Fragmented - ignoring\n";
#endif
flash char SubnetMask[4] = {255, 255, 255, 0};
flash char AllowedIP[10][4] = { // these are the ip's that are allowed to ping us and are allowed to ping & tcp in
// FIREWALL stuff
{192, 168, 254, 2}, // local lan ip
{192, 168, 254, 5}, // local lan ip
{192, 168, 254, 7}, // local lan ip
{0, 0, 0, 0}, // spare
{0, 0, 0, 0}, // spare
{0, 0, 0, 0}, // spare
{0, 0, 0, 0}, // spare
{0, 0, 0, 0}, // spare
{0, 0, 0, 0}, // spare
{0, 0, 0, 0} // leave as '0'
};
#ifdef CPU_eZ8
near u16 IP_ID;
#endif
#ifdef CPU_ATmega128
u16 IP_ID;
#endif
T_IP_Header *IP_Header;
// **************************************************************************
#ifdef Debug
void IP_DisplayProtocol(bool Tx, int TotalBytes)
{
if (Tx)
{
if (!SendDebugRStr(ip_str1)) return;
}
else
{
if (!SendDebugRStr(ip_str2)) return;
}
if (!SendDebugRStr(ip_str2a)) return;
sprintf((char*)ScratchPad, "[%04x] %d-bytes\n", PPP_IP, TotalBytes);
if (!SendDebugStr((char*)ScratchPad)) return;
}
void IP_DisplayHeader(int HeaderIdx, int TotalBytes)
{
int i, j;
u8 type, len;
/*
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Ver= 4 |IHL= 5 |Type of Service| Total Length = 21 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Identification = 111 |Flg=0| Fragment Offset = 0 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Time = 123 | Protocol = 1 | header checksum |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| source address |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| destination address |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Options | Padding | < optional
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
// display the header
if (!SendDebugRStr(ip_str3)) return;
sprintf((char*)ScratchPad, "%u", (IP_Header->VerLen & 0xf0) >> 4);
if (!SendDebugStr((char*)ScratchPad)) return;
if (!SendDebugRStr(ip_str4)) return;
sprintf((char*)ScratchPad, "%u", IP_Header->VerLen & 0x0f);
if (!SendDebugStr((char*)ScratchPad)) return;
if (!SendDebugRStr(ip_str5)) return;
sprintf((char*)ScratchPad, "%02X", IP_Header->TOS);
if (!SendDebugStr((char*)ScratchPad)) return;
if (!SendDebugRStr(ip_str6)) return;
sprintf((char*)ScratchPad, "%u", ntohs(IP_Header->TotalLength));
if (!SendDebugStr((char*)ScratchPad)) return;
if (!SendDebugRStr(ip_str7)) return;
sprintf((char*)ScratchPad, "%u", ntohs(IP_Header->ID));
if (!SendDebugStr((char*)ScratchPad)) return;
if (!SendDebugRStr(ip_str8)) return;
sprintf((char*)ScratchPad, "%04X", ntohs(IP_Header->Fragment));
if (!SendDebugStr((char*)ScratchPad)) return;
if (!SendDebugRStr(ip_str9)) return;
sprintf((char*)ScratchPad, "%u", IP_Header->TTL);
if (!SendDebugStr((char*)ScratchPad)) return;
switch (IP_Header->Protocol)
{
case IP_PROTO_ICMP : if (!SendDebugRStr(ip_str10)) return;
break;
case IP_PROTO_UDP : if (!SendDebugRStr(ip_str11)) return;
break;
case IP_PROTO_TCP : if (!SendDebugRStr(ip_str12)) return;
break;
}
sprintf((char*)ScratchPad, " [%04X]", IP_Header->Protocol);
if (!SendDebugStr((char*)ScratchPad)) return;
if (!SendDebugRStr(ip_str13)) return;
sprintf((char*)ScratchPad, "%04X ", ntohs(IP_Header->Checksum));
if (!SendDebugStr((char*)ScratchPad)) return;
IP_Str((char*)ScratchPad, IP_Header->SourceIP.ip32);
strcat((char*)ScratchPad, " > ");
IP_Str((char*)ScratchPad + strlen((char*)ScratchPad), IP_Header->DestIP.ip32);
if (!SendDebugStr((char*)ScratchPad)) return;
if (!SendDebugStr("\n")) return;
// display any header options
i = HeaderIdx; // index of header
j = (int)(IP_Header->VerLen & 0x0f); // j = length of header in 32-bit dwords
j <<= 2; // ... and now in bytes
j += i; // point to data (immediately follows the IP header)
i += sizeof(T_IP_Header); // point to header options
while (i < j) //
{ // go thru each IP header option
type = MainBuffer[i++]; // option type
len = 0; //
if (type >= 2) len = MainBuffer[i++]; // option length
if (!SendDebugRStr(ip_str14)) return;
if (type < 2)
sprintf((char*)ScratchPad, "%u\n", type);
else
sprintf((char*)ScratchPad, "%u len:%u\n", type, len);
if (!SendDebugStr((char*)ScratchPad)) return;
if (!type) break; // end of option list
if (type == 1) continue; // no length byte (NOP option)
//
if (len < 2) len = 2; //
//
//
i += (len - 2); //
} //
i = j;
// done
if (!SendDebugRStr(ip_str15)) return;
sprintf((char*)ScratchPad, "%u\n", TotalBytes - i);
if (!SendDebugStr((char*)ScratchPad)) return;
}
#endif
// **************************************************************************
// fire wall
bool IP_FireWalled(void)
{ // return TRUE is we are fire walling it
int i;
u32 dw1;
// u32 dw2;
if (PPP.THEIR_IP.ip32)
{
if (IP_Header->SourceIP.ip32 == PPP.THEIR_IP.ip32) return false; // it's the PPP server - allow it
}
if (IP_Header->SourceIP.ip32 == PPP.DNS1_IP.ip32) return false; // it's the primary dns server - allow it
if (IP_Header->SourceIP.ip32 == PPP.DNS2_IP.ip32) return false; // it's the secondary dns server - allow it
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -