parent
92d4cdef58
commit
cc8ca04a85
|
@ -16,6 +16,8 @@ ArpSocket::ArpSocket(TinyIP* tip) : Socket(tip)
|
|||
Count = 64;
|
||||
#endif
|
||||
_Arps = NULL;
|
||||
|
||||
Enable = true;
|
||||
}
|
||||
|
||||
ArpSocket::~ArpSocket()
|
||||
|
|
|
@ -6,6 +6,7 @@ Dhcp::Dhcp(TinyIP* tip) : UdpSocket(tip)
|
|||
{
|
||||
Type = IP_UDP;
|
||||
Port = 68;
|
||||
RemotePort = 67;
|
||||
|
||||
Running = false;
|
||||
Result = false;
|
||||
|
@ -36,8 +37,10 @@ void Dhcp::SendDhcp(DHCP_HEADER* dhcp, uint len)
|
|||
|
||||
Tip->RemoteMac = MAC_FULL;
|
||||
Tip->RemoteIP = IP_FULL;
|
||||
Tip->Port = 68;
|
||||
Tip->RemotePort = 67;
|
||||
|
||||
//UDP_HEADER* udp = dhcp->Prev();
|
||||
//udp->SrcPort = 68;
|
||||
//udp->DestPort = 67;
|
||||
|
||||
// 如果最后一个字节不是DHCP_OPT_End,则增加End
|
||||
//byte* p = (byte*)dhcp + sizeof(DHCP_HEADER);
|
||||
|
@ -149,13 +152,18 @@ void Dhcp::Start()
|
|||
taskID = Sys.AddTask(SendDiscover, this, 0, 1000000);
|
||||
|
||||
// 通过DHCP获取IP期间,关闭Arp响应
|
||||
Tip->EnableArp = false;
|
||||
//Tip->EnableArp = false;
|
||||
if(Tip->Arp) Tip->Arp->Enable = false;
|
||||
|
||||
Running = true;
|
||||
|
||||
Open();
|
||||
}
|
||||
|
||||
void Dhcp::Stop()
|
||||
{
|
||||
Close();
|
||||
|
||||
Running = false;
|
||||
if(taskID)
|
||||
{
|
||||
|
@ -165,7 +173,8 @@ void Dhcp::Stop()
|
|||
taskID = 0;
|
||||
|
||||
// 通过DHCP获取IP期间,关闭Arp响应
|
||||
Tip->EnableArp = true;
|
||||
//Tip->EnableArp = true;
|
||||
if(Tip->Arp) Tip->Arp->Enable = true;
|
||||
|
||||
debug_printf("Dhcp::Stop Result=%d DhcpID=0x%08x\r\n", Result, dhcpid);
|
||||
|
||||
|
|
|
@ -9,6 +9,9 @@ TcpSocket::TcpSocket(TinyIP* tip) : Socket(tip)
|
|||
Port = 0;
|
||||
RemoteIP = 0;
|
||||
RemotePort = 0;
|
||||
LocalIP = 0;
|
||||
LocalPort = 0;
|
||||
|
||||
seqnum = 0xa;
|
||||
|
||||
Status = Closed;
|
||||
|
@ -33,8 +36,9 @@ bool TcpSocket::Process(MemoryStream* ms)
|
|||
// 不能修改主监听Socket的端口,否则可能导致收不到后续连接数据
|
||||
//Port = port;
|
||||
//RemotePort = remotePort;
|
||||
Tip->Port = port;
|
||||
Tip->RemotePort = remotePort;
|
||||
//
|
||||
//Tip->Port = port;
|
||||
//Tip->RemotePort = remotePort;
|
||||
|
||||
// 第一次同步应答
|
||||
if (tcp->Flags & TCP_FLAGS_SYN && !(tcp->Flags & TCP_FLAGS_ACK)) // SYN连接请求标志位,为1表示发起连接的请求数据包
|
||||
|
@ -125,8 +129,8 @@ void TcpSocket::Send(TCP_HEADER* tcp, uint len, byte flags)
|
|||
{
|
||||
Tip->RemoteIP = RemoteIP;
|
||||
|
||||
tcp->SrcPort = __REV16(Port > 0 ? Port : Tip->Port);
|
||||
tcp->DestPort = __REV16(RemotePort > 0 ? RemotePort : Tip->RemotePort);
|
||||
tcp->SrcPort = __REV16(Port);
|
||||
tcp->DestPort = __REV16(RemotePort);
|
||||
tcp->Flags = flags;
|
||||
if(tcp->Length < sizeof(TCP_HEADER) / 4) tcp->Length = sizeof(TCP_HEADER) / 4;
|
||||
|
||||
|
|
|
@ -20,9 +20,11 @@ public:
|
|||
Established = 2,
|
||||
}TCP_STATUS;
|
||||
|
||||
ushort Port; // 本地端口
|
||||
ushort Port; // 本地端口。0表示接收所有端口的数据包
|
||||
IPAddress RemoteIP; // 远程地址
|
||||
ushort RemotePort; // 远程端口
|
||||
IPAddress LocalIP; // 本地IP地址
|
||||
ushort LocalPort; // 本地端口
|
||||
TCP_STATUS Status; // 状态
|
||||
|
||||
TCP_HEADER* Header;
|
||||
|
|
|
@ -32,7 +32,7 @@ TinyIP::TinyIP(ITransport* port)
|
|||
Buffer = NULL;
|
||||
BufferSize = 1500;
|
||||
EnableBroadcast = true;
|
||||
EnableArp = true;
|
||||
//EnableArp = true;
|
||||
|
||||
Sockets.SetCapacity(0x10);
|
||||
// 必须有Arp,否则无法响应别人的IP询问
|
||||
|
@ -79,13 +79,14 @@ void TinyIP::Process(MemoryStream* ms)
|
|||
|
||||
// 只处理发给本机MAC的数据包。此时不能进行目标Mac地址过滤,因为可能是广播包
|
||||
//if(eth->DestMac != Mac) return;
|
||||
LocalMac = eth->DestMac;
|
||||
//LocalMac = eth->DestMac;
|
||||
RemoteMac = eth->SrcMac;
|
||||
|
||||
// 处理ARP
|
||||
if(eth->Type == ETH_ARP)
|
||||
{
|
||||
if(EnableArp && Arp) Arp->Process(ms);
|
||||
//if(EnableArp && Arp) Arp->Process(ms);
|
||||
if(Arp && Arp->Enable) Arp->Process(ms);
|
||||
|
||||
return;
|
||||
}
|
||||
|
@ -104,7 +105,7 @@ void TinyIP::Process(MemoryStream* ms)
|
|||
#endif
|
||||
|
||||
// 记录远程信息
|
||||
LocalIP = ip->DestIP;
|
||||
//LocalIP = ip->DestIP;
|
||||
RemoteIP = ip->SrcIP;
|
||||
|
||||
// 移交给ARP处理,为了让它更新ARP表
|
||||
|
@ -132,7 +133,7 @@ void TinyIP::Process(MemoryStream* ms)
|
|||
for(int i=count-1; i>=0; i--)
|
||||
{
|
||||
Socket* socket = Sockets[i];
|
||||
if(socket)
|
||||
if(socket && socket->Enable)
|
||||
{
|
||||
// 必须类型匹配
|
||||
if(socket->Type == ip->Protocol)
|
||||
|
@ -219,6 +220,10 @@ bool TinyIP::Init()
|
|||
assert_param(Sys.CheckMemory());
|
||||
}
|
||||
|
||||
// 必须有Arp,否则无法响应别人的IP询问
|
||||
if(!Arp) Arp = new ArpSocket(this);
|
||||
Arp->Enable = true;
|
||||
|
||||
if(!Open()) return false;
|
||||
|
||||
ShowInfo();
|
||||
|
@ -422,6 +427,7 @@ Socket::Socket(TinyIP* tip)
|
|||
assert_param(tip);
|
||||
|
||||
Tip = tip;
|
||||
Enable = false;
|
||||
// 除了ARP以外,加入到列表
|
||||
if(this->Type != ETH_ARP) tip->Sockets.Add(this);
|
||||
}
|
||||
|
@ -430,6 +436,7 @@ Socket::~Socket()
|
|||
{
|
||||
assert_param(Tip);
|
||||
|
||||
Enable = false;
|
||||
// 从TinyIP中删除当前Socket
|
||||
Tip->Sockets.Remove(this);
|
||||
}
|
||||
|
|
|
@ -15,8 +15,9 @@ class TinyIP;
|
|||
class Socket
|
||||
{
|
||||
public:
|
||||
TinyIP* Tip;
|
||||
ushort Type;
|
||||
TinyIP* Tip; // TinyIP控制器
|
||||
ushort Type; // 类型
|
||||
bool Enable; // 启用
|
||||
|
||||
Socket(TinyIP* tip);
|
||||
virtual ~Socket();
|
||||
|
@ -35,8 +36,8 @@ public:
|
|||
class TinyIP;
|
||||
typedef bool (*LoopFilter)(TinyIP* tip, void* param);
|
||||
|
||||
// 精简IP类
|
||||
class TinyIP //: protected IEthernetAdapter
|
||||
// 精简以太网协议。封装以太网帧以及IP协议,不包含其它协议实现,仅提供底层支持。
|
||||
class TinyIP
|
||||
{
|
||||
private:
|
||||
ITransport* _port;
|
||||
|
@ -59,16 +60,16 @@ public:
|
|||
IPAddress IP; // 本地IP地址
|
||||
IPAddress Mask; // 子网掩码
|
||||
MacAddress Mac; // 本地Mac地址
|
||||
ushort Port; // 本地端口
|
||||
//ushort Port; // 本地端口
|
||||
bool EnableBroadcast; // 使用广播
|
||||
bool EnableArp; // 启用Arp
|
||||
//bool EnableArp; // 启用Arp
|
||||
|
||||
MacAddress LocalMac;// 本地目标Mac地址
|
||||
IPAddress LocalIP; // 本地目标IP地址
|
||||
//MacAddress LocalMac;// 本地目标Mac地址
|
||||
//IPAddress LocalIP; // 本地目标IP地址
|
||||
//ushort LocalPort; // 本地目标端口
|
||||
MacAddress RemoteMac;// 远程Mac地址
|
||||
IPAddress RemoteIP; // 远程IP地址
|
||||
ushort RemotePort; // 远程端口
|
||||
//ushort RemotePort; // 远程端口
|
||||
|
||||
ushort BufferSize; // 缓冲区大小
|
||||
IPAddress DHCPServer;
|
||||
|
|
|
@ -3,9 +3,38 @@
|
|||
UdpSocket::UdpSocket(TinyIP* tip) : Socket(tip)
|
||||
{
|
||||
Type = IP_UDP;
|
||||
Port = 0;
|
||||
RemoteIP = 0;
|
||||
RemotePort = 0;
|
||||
LocalIP = 0;
|
||||
LocalPort = 0;
|
||||
|
||||
// 累加端口
|
||||
static ushort g_udp_port = 1024;
|
||||
Port = g_udp_port++;
|
||||
}
|
||||
|
||||
string UdpSocket::ToString()
|
||||
{
|
||||
static string name = "UDP_65535";
|
||||
sprintf(name, "UDP_%d", Port);
|
||||
return name;
|
||||
}
|
||||
|
||||
bool UdpSocket::OnOpen()
|
||||
{
|
||||
if(Port)
|
||||
debug_printf("Udp::Open %d\r\n", Port);
|
||||
else
|
||||
debug_printf("Udp::Open %d 监听所有端口UDP数据包\r\n", Port);
|
||||
|
||||
Enable = true;
|
||||
return Enable;
|
||||
}
|
||||
|
||||
void UdpSocket::OnClose()
|
||||
{
|
||||
debug_printf("Udp::Close %d\r\n", Port);
|
||||
Enable = false;
|
||||
}
|
||||
|
||||
bool UdpSocket::Process(MemoryStream* ms)
|
||||
|
@ -19,8 +48,11 @@ bool UdpSocket::Process(MemoryStream* ms)
|
|||
// 仅处理本连接的IP和端口
|
||||
if(Port != 0 && port != Port) return false;
|
||||
|
||||
IP_HEADER* ip = udp->Prev();
|
||||
RemotePort = remotePort;
|
||||
RemoteIP = udp->Prev()->SrcIP;
|
||||
RemoteIP = ip->SrcIP;
|
||||
LocalPort = port;
|
||||
LocalIP = ip->DestIP;
|
||||
|
||||
#if NET_DEBUG
|
||||
byte* data = udp->Next();
|
||||
|
@ -54,10 +86,10 @@ void UdpSocket::OnProcess(UDP_HEADER* udp, MemoryStream& ms)
|
|||
{
|
||||
#if NET_DEBUG
|
||||
debug_printf("UDP ");
|
||||
Tip->ShowIP(Tip->RemoteIP);
|
||||
debug_printf(":%d => ", Tip->RemotePort);
|
||||
Tip->ShowIP(Tip->LocalIP);
|
||||
debug_printf(":%d Payload=%d udp_len=%d \r\n", Tip->Port, len, __REV16(udp->Length));
|
||||
Tip->ShowIP(RemoteIP);
|
||||
debug_printf(":%d => ", RemotePort);
|
||||
Tip->ShowIP(LocalIP);
|
||||
debug_printf(":%d Payload=%d udp_len=%d \r\n", LocalPort, len, __REV16(udp->Length));
|
||||
|
||||
Sys.ShowString(data, len);
|
||||
debug_printf(" \r\n");
|
||||
|
@ -72,8 +104,8 @@ void UdpSocket::Send(UDP_HEADER* udp, uint len, bool checksum)
|
|||
//UDP_HEADER* udp = (UDP_HEADER*)(buf - sizeof(UDP_HEADER));
|
||||
assert_param(udp);
|
||||
|
||||
udp->SrcPort = __REV16(Port > 0 ? Port : Tip->Port);
|
||||
udp->DestPort = __REV16(RemotePort > 0 ? RemotePort : Tip->RemotePort);
|
||||
udp->SrcPort = __REV16(Port > 0 ? Port : LocalPort);
|
||||
udp->DestPort = __REV16(RemotePort);
|
||||
udp->Length = __REV16(sizeof(UDP_HEADER) + len);
|
||||
|
||||
// 网络序是大端
|
||||
|
|
12
TinyIP/Udp.h
12
TinyIP/Udp.h
|
@ -4,15 +4,17 @@
|
|||
#include "TinyIP.h"
|
||||
|
||||
// Udp会话
|
||||
class UdpSocket : public Socket, ITransport
|
||||
class UdpSocket : public Socket, public ITransport
|
||||
{
|
||||
private:
|
||||
UDP_HEADER* Create();
|
||||
|
||||
public:
|
||||
ushort Port; // 本地端口
|
||||
ushort Port; // 本地端口。0表示接收所有端口的数据包
|
||||
IPAddress RemoteIP; // 远程地址
|
||||
ushort RemotePort; // 远程端口
|
||||
IPAddress LocalIP; // 本地IP地址
|
||||
ushort LocalPort; // 本地端口
|
||||
|
||||
UdpSocket(TinyIP* tip);
|
||||
|
||||
|
@ -26,12 +28,14 @@ public:
|
|||
// 发送UDP数据到目标地址
|
||||
void Send(const byte* buf, uint len, IPAddress ip = 0, ushort port = 0);
|
||||
|
||||
virtual string ToString();
|
||||
|
||||
protected:
|
||||
void Send(UDP_HEADER* udp, uint len, bool checksum = true);
|
||||
virtual void OnProcess(UDP_HEADER* udp, MemoryStream& ms);
|
||||
|
||||
//virtual bool OnOpen() { return _port->Open(); }
|
||||
//virtual void OnClose() { _port->Close(); }
|
||||
virtual bool OnOpen();
|
||||
virtual void OnClose();
|
||||
|
||||
virtual bool OnWrite(const byte* buf, uint len);
|
||||
virtual uint OnRead(byte* buf, uint len);
|
||||
|
|
Loading…
Reference in New Issue