SmartOS/TinyIP/TinyIP.h

121 lines
3.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef _TinyIP_H_
#define _TinyIP_H_
// 模块开发使用说明见后
#include "Sys.h"
#include "List.h"
#include "Stream.h"
#include "Net\ITransport.h"
#include "Net\Net.h"
class TinyIP;
// 网络数据处理Socket基类
class Socket
{
public:
TinyIP* Tip; // TinyIP控制器
ushort Type; // 类型
bool Enable; // 启用
Socket(TinyIP* tip);
virtual ~Socket();
// 处理数据包
virtual bool Process(MemoryStream* ms) = 0;
};
// Socket列表
class SocketList : public List<Socket*>
{
public:
Socket* FindByType(ushort type);
};
class TinyIP;
typedef bool (*LoopFilter)(TinyIP* tip, void* param, MemoryStream& ms);
// 精简以太网协议。封装以太网帧以及IP协议不包含其它协议实现仅提供底层支持。
class TinyIP
{
private:
ITransport* _port;
ulong _StartTime;
// 循环调度的任务,捕获数据包,返回长度
uint Fetch(byte* buf = NULL, uint len = 0);
void Init();
public:
byte* Buffer; // 缓冲区
// 任务函数
static void Work(void* param);
// 带过滤器的轮询
bool LoopWait(LoopFilter filter, void* param, uint msTimeout);
// 处理数据包
void Process(MemoryStream* ms);
// 修正IP包负载数据的长度。物理层送来的长度可能有误一般超长
void FixPayloadLength(IP_HEADER* ip, MemoryStream* ms);
public:
IPAddress IP; // 本地IP地址
IPAddress Mask; // 子网掩码
MacAddress Mac; // 本地Mac地址
//ushort Port; // 本地端口
bool EnableBroadcast; // 使用广播
//bool EnableArp; // 启用Arp
//MacAddress LocalMac;// 本地目标Mac地址
//IPAddress LocalIP; // 本地目标IP地址
//ushort LocalPort; // 本地目标端口
MacAddress RemoteMac;// 远程Mac地址
IPAddress RemoteIP; // 远程IP地址
//ushort RemotePort; // 远程端口
ushort BufferSize; // 缓冲区大小
IPAddress DHCPServer;
IPAddress DNSServer;
IPAddress Gateway;
// Arp套接字
Socket* Arp;
// 套接字列表。套接字根据类型来识别
//Socket* Sockets[0x20];
//uint SocketCount;
SocketList Sockets;
TinyIP();
TinyIP(ITransport* port);
virtual ~TinyIP();
void Init(ITransport* port);
bool Open();
void ShowInfo();
static void ShowIP(IPAddress ip);
static void ShowMac(const MacAddress& mac);
ushort CheckSum(const byte* buf, uint len, byte type);
void SendEthernet(ETH_TYPE type, const byte* buf, uint len);
void SendIP(IP_TYPE type, const byte* buf, uint len);
bool IsMyIP(IPAddress ip); // 是否发给我的IP地址
bool IsBroadcast(IPAddress ip); // 是否广播地址
};
/*
TinyIP作为精简以太网协议每次只处理一个收发数据包目标定位是超简单的使用场合
ARP是必须有的子网内计算机发数据包给它之前必须先通过ARP得到它的MAC地址
ARP还必须有一个表记录部分IP和MAC的对照关系子网内通讯时需要子网外通讯也需要网关MAC
ICMP提供一个简单的Ping服务可要可不要用户习惯通过Ping来识别网络设备是否在线因此默认使用该模块
TCP可作为服务端处理任意端口请求并响应这里不需要记录连接状态因为客户端的每一次请求过来都有IP地址和MAC地址
TCP也可作为客户端但只能使用固定几个连接每次发送数据之前还需要设置远程IP和端口
UDP可作为服务端处理任意端口请求并响应
UDP也可作为客户端向任意地址端口发送数据
因为只有一个缓冲区,所有收发数据包不能过大,并且只能有一包数据
*/
#endif