Line 157... |
Line 157... |
#define TCP_DATA 54 //length-N
|
#define TCP_DATA 54 //length-N
|
|
|
#define TCP_FLAGS_FIN 1
|
#define TCP_FLAGS_FIN 1
|
#define TCP_FLAGS_SYN 2
|
#define TCP_FLAGS_SYN 2
|
#define TCP_FLAGS_RST 4
|
#define TCP_FLAGS_RST 4
|
|
#define TCP_FLAGS_PSH 8
|
#define TCP_FLAGS_ACK 16
|
#define TCP_FLAGS_ACK 16
|
|
|
//PING FIELD OFFSET LENGTH VALUE
|
//PING FIELD OFFSET LENGTH VALUE
|
#define PING_TYPE 34 //1 SEND=8;REPLY=0
|
#define PING_TYPE 34 //1 SEND=8;REPLY=0
|
#define PING_CODE 35 //1 0
|
#define PING_CODE 35 //1 0
|
Line 241... |
Line 242... |
if(frame)
|
if(frame)
|
{
|
{
|
assert(frame->state == 0);
|
assert(frame->state == 0);
|
frame->state = 1;
|
frame->state = 1;
|
}
|
}
|
//else if(IPVerbose)
|
|
// UartPrintfCritical(":");
|
|
return frame;
|
return frame;
|
}
|
}
|
|
|
|
|
static void FrameFree(IPFrame *frame)
|
static void FrameFree(IPFrame *frame)
|
Line 366... |
Line 365... |
int length;
|
int length;
|
length = frame->length - TCP_DATA;
|
length = frame->length - TCP_DATA;
|
if(frame->packet[TCP_FLAGS] & (TCP_FLAGS_FIN | TCP_FLAGS_SYN))
|
if(frame->packet[TCP_FLAGS] & (TCP_FLAGS_FIN | TCP_FLAGS_SYN))
|
++length;
|
++length;
|
if(frame->socket == NULL || frame->socket->state == IP_UDP || length == 0 ||
|
if(frame->socket == NULL || frame->socket->state == IP_UDP || length == 0 ||
|
++frame->retryCnt > 4)
|
frame->socket->state == IP_PING || ++frame->retryCnt > 4)
|
{
|
{
|
FrameFree(frame); //can't be ACK'ed
|
FrameFree(frame); //can't be ACK'ed
|
}
|
}
|
else
|
else
|
{
|
{
|
Line 479... |
Line 478... |
uint8 *packet = frame->packet;
|
uint8 *packet = frame->packet;
|
int flags, count;
|
int flags, count;
|
|
|
flags = packet[TCP_FLAGS];
|
flags = packet[TCP_FLAGS];
|
memcpy(packet, socket->headerSend, TCP_SEQ);
|
memcpy(packet, socket->headerSend, TCP_SEQ);
|
packet[TCP_FLAGS] = (uint8)flags;
|
packet[TCP_FLAGS] = (uint8)flags | (socket->headerSend[TCP_FLAGS] & TCP_FLAGS_PSH);
|
if(flags & TCP_FLAGS_SYN)
|
if(flags & TCP_FLAGS_SYN)
|
packet[TCP_HEADER_LENGTH] = 0x60; //set maximum segment size
|
packet[TCP_HEADER_LENGTH] = 0x60; //set maximum segment size
|
else
|
else
|
packet[TCP_HEADER_LENGTH] = 0x50;
|
packet[TCP_HEADER_LENGTH] = 0x50;
|
packet[TCP_SEQ] = (uint8)(socket->seq >> 24);
|
packet[TCP_SEQ] = (uint8)(socket->seq >> 24);
|
Line 632... |
Line 631... |
}
|
}
|
}
|
}
|
}
|
}
|
|
|
|
|
|
uint32 IPAddressSelf(void)
|
|
{
|
|
return (ipAddressPlasma[0] << 24) | (ipAddressPlasma[1] << 16) |
|
|
(ipAddressPlasma[2] << 8) | ipAddressPlasma[3];
|
|
}
|
|
|
|
|
static int IPProcessTCPPacket(IPFrame *frameIn)
|
static int IPProcessTCPPacket(IPFrame *frameIn)
|
{
|
{
|
uint32 seq, ack;
|
uint32 seq, ack;
|
int length, ip_length, bytes;
|
int length, ip_length, bytes;
|
IPSocket *socket, *socketNew;
|
IPSocket *socket, *socketNew;
|
Line 936... |
Line 942... |
//}
|
//}
|
|
|
//PING request?
|
//PING request?
|
if(packet[IP_PROTOCOL] == 1)
|
if(packet[IP_PROTOCOL] == 1)
|
{
|
{
|
|
if(packet[PING_TYPE] == 0) //PING reply
|
|
{
|
|
for(socket = SocketHead; socket; socket = socket->next)
|
|
{
|
|
if(socket->state == IP_PING &&
|
|
memcmp(packet+IP_SOURCE, socket->headerSend+IP_DEST, 4) == 0)
|
|
{
|
|
socket->funcPtr(socket);
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
if(packet[PING_TYPE] != 8)
|
if(packet[PING_TYPE] != 8)
|
return 0;
|
return 0;
|
frameOut = IPFrameGet(FRAME_COUNT_SEND);
|
frameOut = IPFrameGet(FRAME_COUNT_SEND);
|
if(frameOut == NULL)
|
if(frameOut == NULL)
|
return 0;
|
return 0;
|
Line 976... |
Line 994... |
for(socket = SocketHead; socket; socket = socket->next)
|
for(socket = SocketHead; socket; socket = socket->next)
|
{
|
{
|
if(packet[IP_PROTOCOL] == socket->headerRcv[IP_PROTOCOL] &&
|
if(packet[IP_PROTOCOL] == socket->headerRcv[IP_PROTOCOL] &&
|
memcmp(packet+UDP_DEST_PORT, socket->headerRcv+UDP_DEST_PORT, 2) == 0)
|
memcmp(packet+UDP_DEST_PORT, socket->headerRcv+UDP_DEST_PORT, 2) == 0)
|
{
|
{
|
|
EthernetCreateResponse(socket->headerSend, packet, UDP_DATA);
|
break;
|
break;
|
}
|
}
|
}
|
}
|
}
|
}
|
|
|
Line 994... |
Line 1013... |
}
|
}
|
return 0;
|
return 0;
|
}
|
}
|
|
|
|
|
void IPMainThread(void *Arg)
|
static void IPMainThread(void *arg)
|
{
|
{
|
uint32 message[4];
|
uint32 message[4];
|
int rc;
|
int rc;
|
IPFrame *frame, *frameOut=NULL;
|
IPFrame *frame, *frameOut=NULL;
|
uint32 ticks, ticksLast;
|
uint32 ticks, ticksLast;
|
(void)Arg;
|
(void)arg;
|
|
|
ticksLast = OS_ThreadTime();
|
ticksLast = OS_ThreadTime();
|
memset(message, 0, sizeof(message));
|
memset(message, 0, sizeof(message));
|
|
|
for(;;)
|
for(;;)
|
Line 1063... |
Line 1082... |
return (uint8*)IPFrameGet(FRAME_COUNT_RCV);
|
return (uint8*)IPFrameGet(FRAME_COUNT_RCV);
|
}
|
}
|
|
|
|
|
//Set FrameSendFunction only if single threaded
|
//Set FrameSendFunction only if single threaded
|
void IPInit(IPFuncPtr FrameSendFunction)
|
void IPInit(IPFuncPtr frameSendFunction)
|
{
|
{
|
int i;
|
int i;
|
IPFrame *frame;
|
IPFrame *frame;
|
|
|
FrameSendFunc = FrameSendFunction;
|
FrameSendFunc = frameSendFunction;
|
IPMutex = OS_MutexCreate("IPSem");
|
IPMutex = OS_MutexCreate("IPSem");
|
IPMQueue = OS_MQueueCreate("IPMQ", FRAME_COUNT*2, 32);
|
IPMQueue = OS_MQueueCreate("IPMQ", FRAME_COUNT*2, 32);
|
for(i = 0; i < FRAME_COUNT; ++i)
|
for(i = 0; i < FRAME_COUNT; ++i)
|
{
|
{
|
frame = (IPFrame*)malloc(sizeof(IPFrame));
|
frame = (IPFrame*)malloc(sizeof(IPFrame));
|
Line 1080... |
Line 1099... |
frame->next = FrameFreeHead;
|
frame->next = FrameFreeHead;
|
frame->prev = NULL;
|
frame->prev = NULL;
|
FrameFreeHead = frame;
|
FrameFreeHead = frame;
|
}
|
}
|
UartPacketConfig(MyPacketGet, PACKET_SIZE, IPMQueue);
|
UartPacketConfig(MyPacketGet, PACKET_SIZE, IPMQueue);
|
if(FrameSendFunction == NULL)
|
if(frameSendFunction == NULL)
|
IPThread = OS_ThreadCreate("TCP/IP", IPMainThread, NULL, 240, 6000);
|
IPThread = OS_ThreadCreate("TCP/IP", IPMainThread, NULL, 240, 6000);
|
|
|
IPDhcp(NULL, 360, 1); //Send DHCP request
|
IPDhcp(NULL, 360, 1); //Send DHCP request
|
}
|
}
|
|
|
|
|
//To open a socket for listen set IPAddress to 0
|
//To open a socket for listen set ipAddress to 0
|
IPSocket *IPOpen(IPMode_e Mode, uint32 IPAddress, uint32 Port, IPFuncPtr funcPtr)
|
IPSocket *IPOpen(IPMode_e mode, uint32 ipAddress, uint32 port, IPFuncPtr funcPtr)
|
{
|
{
|
IPSocket *socket;
|
IPSocket *socket;
|
uint8 *ptrSend, *ptrRcv;
|
uint8 *ptrSend, *ptrRcv;
|
IPFrame *frame;
|
IPFrame *frame;
|
static int portSource=0x1007;
|
static int portSource=0x1007;
|
(void)Mode;
|
|
(void)IPAddress;
|
|
|
|
socket = (IPSocket*)malloc(sizeof(IPSocket));
|
socket = (IPSocket*)malloc(sizeof(IPSocket));
|
if(socket == NULL)
|
if(socket == NULL)
|
return socket;
|
return socket;
|
memset(socket, 0, sizeof(IPSocket));
|
memset(socket, 0, sizeof(IPSocket));
|
Line 1114... |
Line 1131... |
socket->userFunc = NULL;
|
socket->userFunc = NULL;
|
socket->userPtr = NULL;
|
socket->userPtr = NULL;
|
ptrSend = socket->headerSend;
|
ptrSend = socket->headerSend;
|
ptrRcv = socket->headerRcv;
|
ptrRcv = socket->headerRcv;
|
|
|
if(IPAddress == 0)
|
if(ipAddress == 0)
|
{
|
{
|
//Setup listing port
|
//Setup listing port
|
socket->headerRcv[TCP_DEST_PORT] = (uint8)(Port >> 8);
|
socket->headerRcv[TCP_DEST_PORT] = (uint8)(port >> 8);
|
socket->headerRcv[TCP_DEST_PORT+1] = (uint8)Port;
|
socket->headerRcv[TCP_DEST_PORT+1] = (uint8)port;
|
}
|
}
|
else
|
else
|
{
|
{
|
//Setup sending packet
|
//Setup sending packet
|
memset(ptrSend, 0, UDP_LENGTH);
|
memset(ptrSend, 0, UDP_LENGTH);
|
memset(ptrRcv, 0, UDP_LENGTH);
|
memset(ptrRcv, 0, UDP_LENGTH);
|
|
|
//Setup Ethernet
|
//Setup Ethernet
|
|
if(ipAddress != IPAddressSelf())
|
memcpy(ptrSend+ETHERNET_DEST, ethernetAddressGateway, 6);
|
memcpy(ptrSend+ETHERNET_DEST, ethernetAddressGateway, 6);
|
|
else
|
|
memcpy(ptrSend+ETHERNET_DEST, ethernetAddressPlasma, 6);
|
memcpy(ptrSend+ETHERNET_SOURCE, ethernetAddressPlasma, 6);
|
memcpy(ptrSend+ETHERNET_SOURCE, ethernetAddressPlasma, 6);
|
ptrSend[ETHERNET_FRAME_TYPE] = 0x08;
|
ptrSend[ETHERNET_FRAME_TYPE] = 0x08;
|
|
|
//Setup IP
|
//Setup IP
|
ptrSend[IP_VERSION_LENGTH] = 0x45;
|
ptrSend[IP_VERSION_LENGTH] = 0x45;
|
ptrSend[IP_TIME_TO_LIVE] = 0x80;
|
ptrSend[IP_TIME_TO_LIVE] = 0x80;
|
|
|
//Setup IP addresses
|
//Setup IP addresses
|
memcpy(ptrSend+IP_SOURCE, ipAddressPlasma, 4);
|
memcpy(ptrSend+IP_SOURCE, ipAddressPlasma, 4);
|
ptrSend[IP_DEST] = (uint8)(IPAddress >> 24);
|
ptrSend[IP_DEST] = (uint8)(ipAddress >> 24);
|
ptrSend[IP_DEST+1] = (uint8)(IPAddress >> 16);
|
ptrSend[IP_DEST+1] = (uint8)(ipAddress >> 16);
|
ptrSend[IP_DEST+2] = (uint8)(IPAddress >> 8);
|
ptrSend[IP_DEST+2] = (uint8)(ipAddress >> 8);
|
ptrSend[IP_DEST+3] = (uint8)IPAddress;
|
ptrSend[IP_DEST+3] = (uint8)ipAddress;
|
ptrRcv[IP_SOURCE] = (uint8)(IPAddress >> 24);
|
ptrRcv[IP_SOURCE] = (uint8)(ipAddress >> 24);
|
ptrRcv[IP_SOURCE+1] = (uint8)(IPAddress >> 16);
|
ptrRcv[IP_SOURCE+1] = (uint8)(ipAddress >> 16);
|
ptrRcv[IP_SOURCE+2] = (uint8)(IPAddress >> 8);
|
ptrRcv[IP_SOURCE+2] = (uint8)(ipAddress >> 8);
|
ptrRcv[IP_SOURCE+3] = (uint8)IPAddress;
|
ptrRcv[IP_SOURCE+3] = (uint8)ipAddress;
|
memcpy(ptrRcv+IP_DEST, ipAddressPlasma, 4);
|
memcpy(ptrRcv+IP_DEST, ipAddressPlasma, 4);
|
|
|
//Setup ports
|
//Setup ports
|
ptrSend[TCP_SOURCE_PORT] = (uint8)(portSource >> 8);
|
ptrSend[TCP_SOURCE_PORT] = (uint8)(portSource >> 8);
|
ptrSend[TCP_SOURCE_PORT+1] = (uint8)portSource;
|
ptrSend[TCP_SOURCE_PORT+1] = (uint8)portSource;
|
ptrSend[TCP_DEST_PORT] = (uint8)(Port >> 8);
|
ptrSend[TCP_DEST_PORT] = (uint8)(port >> 8);
|
ptrSend[TCP_DEST_PORT+1] = (uint8)Port;
|
ptrSend[TCP_DEST_PORT+1] = (uint8)port;
|
ptrRcv[TCP_SOURCE_PORT] = (uint8)(Port >> 8);
|
ptrRcv[TCP_SOURCE_PORT] = (uint8)(port >> 8);
|
ptrRcv[TCP_SOURCE_PORT+1] = (uint8)Port;
|
ptrRcv[TCP_SOURCE_PORT+1] = (uint8)port;
|
ptrRcv[TCP_DEST_PORT] = (uint8)(portSource >> 8);
|
ptrRcv[TCP_DEST_PORT] = (uint8)(portSource >> 8);
|
ptrRcv[TCP_DEST_PORT+1] = (uint8)portSource;
|
ptrRcv[TCP_DEST_PORT+1] = (uint8)portSource;
|
++portSource;
|
++portSource;
|
}
|
}
|
|
|
if(Mode == IP_MODE_TCP)
|
if(mode == IP_MODE_TCP)
|
{
|
{
|
if(IPAddress)
|
if(ipAddress)
|
socket->state = IP_TCP;
|
socket->state = IP_TCP;
|
else
|
else
|
socket->state = IP_LISTEN;
|
socket->state = IP_LISTEN;
|
ptrSend[IP_PROTOCOL] = 0x06; //TCP
|
ptrSend[IP_PROTOCOL] = 0x06; //TCP
|
ptrRcv[IP_PROTOCOL] = 0x06;
|
ptrRcv[IP_PROTOCOL] = 0x06;
|
}
|
}
|
else if(Mode == IP_MODE_UDP)
|
else if(mode == IP_MODE_UDP)
|
{
|
{
|
socket->state = IP_UDP;
|
socket->state = IP_UDP;
|
ptrSend[IP_PROTOCOL] = 0x11; //UDP
|
ptrSend[IP_PROTOCOL] = 0x11; //UDP
|
ptrRcv[IP_PROTOCOL] = 0x11;
|
ptrRcv[IP_PROTOCOL] = 0x11;
|
}
|
}
|
|
else if(mode == IP_MODE_PING)
|
|
{
|
|
socket->state = IP_PING;
|
|
ptrSend[IP_PROTOCOL] = 0x01; //PING
|
|
memset(ptrSend+PING_TYPE, 0, 8);
|
|
ptrSend[PING_TYPE] = 8; //SEND
|
|
}
|
|
|
//Add socket to linked list
|
//Add socket to linked list
|
OS_MutexPend(IPMutex);
|
OS_MutexPend(IPMutex);
|
socket->next = SocketHead;
|
socket->next = SocketHead;
|
socket->prev = NULL;
|
socket->prev = NULL;
|
if(SocketHead)
|
if(SocketHead)
|
SocketHead->prev = socket;
|
SocketHead->prev = socket;
|
SocketHead = socket;
|
SocketHead = socket;
|
OS_MutexPost(IPMutex);
|
OS_MutexPost(IPMutex);
|
|
|
if(Mode == IP_MODE_TCP && IPAddress)
|
if(mode == IP_MODE_TCP && ipAddress)
|
{
|
{
|
//Send TCP SYN
|
//Send TCP SYN
|
socket->seq = 0x01234567;
|
socket->seq = 0x01234567;
|
frame = IPFrameGet(0);
|
frame = IPFrameGet(0);
|
if(frame)
|
if(frame)
|
Line 1204... |
Line 1231... |
}
|
}
|
return socket;
|
return socket;
|
}
|
}
|
|
|
|
|
void IPWriteFlush(IPSocket *Socket)
|
void IPWriteFlush(IPSocket *socket)
|
{
|
{
|
uint8 *packetOut;
|
uint8 *packetOut;
|
if(Socket->frameSend && Socket->state != IP_UDP)
|
if(socket->frameSend && socket->state != IP_UDP &&
|
|
socket->state != IP_PING)
|
{
|
{
|
packetOut = Socket->frameSend->packet;
|
packetOut = socket->frameSend->packet;
|
packetOut[TCP_FLAGS] = TCP_FLAGS_ACK;
|
packetOut[TCP_FLAGS] = TCP_FLAGS_ACK;
|
TCPSendPacket(Socket, Socket->frameSend, TCP_DATA + Socket->sendOffset);
|
TCPSendPacket(socket, socket->frameSend, TCP_DATA + socket->sendOffset);
|
Socket->seq += Socket->sendOffset;
|
socket->seq += socket->sendOffset;
|
Socket->frameSend = NULL;
|
socket->frameSend = NULL;
|
Socket->sendOffset = 0;
|
socket->sendOffset = 0;
|
}
|
}
|
}
|
}
|
|
|
|
|
uint32 IPWrite(IPSocket *Socket, const uint8 *Buf, uint32 Length)
|
uint32 IPWrite(IPSocket *socket, const uint8 *buf, uint32 length)
|
{
|
{
|
IPFrame *frameOut;
|
IPFrame *frameOut;
|
uint8 *packetOut;
|
uint8 *packetOut;
|
uint32 bytes, count=0, tries=0;
|
uint32 bytes, count=0, tries=0;
|
int offset;
|
int offset;
|
OS_Thread_t *self;
|
OS_Thread_t *self;
|
|
|
//printf("IPWrite(0x%x, %d)", Socket, Length);
|
//printf("IPWrite(0x%x, %d)", Socket, Length);
|
self = OS_ThreadSelf();
|
self = OS_ThreadSelf();
|
while(Length)
|
while(length)
|
{
|
{
|
//Rate limit output
|
//Rate limit output
|
if(Socket->seq - Socket->seqReceived >= 5120)
|
if(socket->seq - socket->seqReceived >= 5120)
|
{
|
{
|
if(self == IPThread || ++tries > 200)
|
if(self == IPThread || ++tries > 200)
|
break;
|
break;
|
else
|
else
|
{
|
{
|
OS_ThreadSleep(1);
|
OS_ThreadSleep(1);
|
continue;
|
continue;
|
}
|
}
|
}
|
}
|
tries = 0;
|
tries = 0;
|
while(Socket->frameSend == NULL)
|
while(socket->frameSend == NULL)
|
{
|
{
|
Socket->frameSend = IPFrameGet(FRAME_COUNT_SEND);
|
socket->frameSend = IPFrameGet(FRAME_COUNT_SEND);
|
Socket->sendOffset = 0;
|
socket->sendOffset = 0;
|
if(Socket->frameSend == NULL)
|
if(socket->frameSend == NULL)
|
{
|
{
|
if(self == IPThread || ++tries > 200)
|
if(self == IPThread || ++tries > 200)
|
break;
|
break;
|
else
|
else
|
OS_ThreadSleep(1);
|
OS_ThreadSleep(1);
|
}
|
}
|
}
|
}
|
frameOut = Socket->frameSend;
|
frameOut = socket->frameSend;
|
offset = Socket->sendOffset;
|
offset = socket->sendOffset;
|
if(frameOut == NULL)
|
if(frameOut == NULL)
|
break;
|
break;
|
packetOut = frameOut->packet;
|
packetOut = frameOut->packet;
|
|
|
|
if(socket->state == IP_PING)
|
|
{
|
|
bytes = length;
|
|
memcpy(packetOut, socket->headerSend, PING_DATA);
|
|
memcpy(packetOut+PING_DATA, buf, bytes);
|
|
IPSendPacket(socket, socket->frameSend, PING_DATA + bytes);
|
|
socket->frameSend = NULL;
|
|
}
|
|
else if(socket->state != IP_UDP)
|
|
{
|
bytes = 512 - offset;
|
bytes = 512 - offset;
|
if(bytes > Length)
|
if(bytes > length)
|
bytes = Length;
|
bytes = length;
|
Socket->sendOffset += bytes;
|
socket->sendOffset += bytes;
|
|
memcpy(packetOut+TCP_DATA+offset, buf, bytes);
|
if(Socket->state != IP_UDP)
|
if(socket->sendOffset >= 512)
|
{
|
IPWriteFlush(socket);
|
memcpy(packetOut+TCP_DATA+offset, Buf, bytes);
|
|
if(Socket->sendOffset >= 512)
|
|
IPWriteFlush(Socket);
|
|
//if(Socket->seq - Socket->seqReceived > Socket->seqWindow)
|
//if(Socket->seq - Socket->seqReceived > Socket->seqWindow)
|
//{
|
//{
|
// printf("W");
|
// printf("W");
|
// OS_ThreadSleep(10);
|
// OS_ThreadSleep(10);
|
//}
|
//}
|
}
|
}
|
else //UDP
|
else //UDP
|
{
|
{
|
memcpy(packetOut+UDP_DATA+offset, Buf, bytes);
|
bytes = length;
|
memcpy(packetOut, Socket->headerSend, UDP_LENGTH);
|
memcpy(packetOut+UDP_DATA+offset, buf, bytes);
|
IPSendPacket(Socket, Socket->frameSend, UDP_DATA + Socket->sendOffset);
|
memcpy(packetOut, socket->headerSend, UDP_LENGTH);
|
Socket->frameSend = NULL;
|
IPSendPacket(socket, socket->frameSend, UDP_DATA + bytes);
|
|
socket->frameSend = NULL;
|
}
|
}
|
count += bytes;
|
count += bytes;
|
Buf += bytes;
|
buf += bytes;
|
Length -= bytes;
|
length -= bytes;
|
}
|
}
|
return count;
|
return count;
|
}
|
}
|
|
|
|
|
uint32 IPRead(IPSocket *Socket, uint8 *Buf, uint32 Length)
|
uint32 IPRead(IPSocket *socket, uint8 *buf, uint32 length)
|
{
|
{
|
IPFrame *frame, *frame2;
|
IPFrame *frame, *frame2;
|
int count=0, bytes, offset;
|
int count=0, bytes, offset;
|
|
|
if(Socket->state == IP_TCP)
|
if(socket->state == IP_TCP)
|
offset = TCP_DATA;
|
offset = TCP_DATA;
|
else
|
else
|
offset = UDP_DATA;
|
offset = UDP_DATA;
|
|
|
OS_MutexPend(IPMutex);
|
OS_MutexPend(IPMutex);
|
for(frame = Socket->frameReadTail; Length && frame; )
|
for(frame = socket->frameReadTail; length && frame; )
|
{
|
{
|
bytes = frame->length - offset - Socket->readOffset;
|
bytes = frame->length - offset - socket->readOffset;
|
if(bytes > (int)Length)
|
if(bytes > (int)length)
|
bytes = Length;
|
bytes = length;
|
memcpy(Buf, frame->packet + offset + Socket->readOffset, bytes);
|
memcpy(buf, frame->packet + offset + socket->readOffset, bytes);
|
Buf += bytes;
|
buf += bytes;
|
Socket->readOffset += bytes;
|
socket->readOffset += bytes;
|
Length -= bytes;
|
length -= bytes;
|
count += bytes;
|
count += bytes;
|
|
|
//Check if done with packet
|
//Check if done with packet
|
frame2 = frame;
|
frame2 = frame;
|
frame = frame->prev;
|
frame = frame->prev;
|
if(Socket->readOffset == frame2->length - offset)
|
if(socket->readOffset == frame2->length - offset)
|
{
|
{
|
//Remove packet from socket linked list
|
//Remove packet from socket linked list
|
Socket->readOffset = 0;
|
socket->readOffset = 0;
|
FrameRemove(&Socket->frameReadHead, &Socket->frameReadTail, frame2);
|
FrameRemove(&socket->frameReadHead, &socket->frameReadTail, frame2);
|
FrameFree(frame2);
|
FrameFree(frame2);
|
}
|
}
|
}
|
}
|
OS_MutexPost(IPMutex);
|
OS_MutexPost(IPMutex);
|
return count;
|
return count;
|
}
|
}
|
|
|
|
|
static void IPClose2(IPSocket *Socket)
|
static void IPClose2(IPSocket *socket)
|
{
|
{
|
IPFrame *frame, *framePrev;
|
IPFrame *frame, *framePrev;
|
|
|
OS_MutexPend(IPMutex);
|
OS_MutexPend(IPMutex);
|
|
|
//Mark packets as don't retransmit
|
//Mark packets as don't retransmit
|
for(frame = FrameSendHead; frame; frame = frame->next)
|
for(frame = FrameSendHead; frame; frame = frame->next)
|
{
|
{
|
if(frame->socket == Socket)
|
if(frame->socket == socket)
|
frame->socket = NULL;
|
frame->socket = NULL;
|
}
|
}
|
|
|
//Remove packets from retransmision list
|
//Remove packets from retransmision list
|
for(frame = FrameResendHead; frame; )
|
for(frame = FrameResendHead; frame; )
|
{
|
{
|
framePrev = frame;
|
framePrev = frame;
|
frame = frame->next;
|
frame = frame->next;
|
if(framePrev->socket == Socket)
|
if(framePrev->socket == socket)
|
{
|
{
|
FrameRemove(&FrameResendHead, &FrameResendTail, framePrev);
|
FrameRemove(&FrameResendHead, &FrameResendTail, framePrev);
|
FrameFree(framePrev);
|
FrameFree(framePrev);
|
}
|
}
|
}
|
}
|
|
|
//Remove packets from socket read linked list
|
//Remove packets from socket read linked list
|
for(frame = Socket->frameReadHead; frame; )
|
for(frame = socket->frameReadHead; frame; )
|
{
|
{
|
framePrev = frame;
|
framePrev = frame;
|
frame = frame->next;
|
frame = frame->next;
|
FrameRemove(&Socket->frameReadHead, &Socket->frameReadTail, framePrev);
|
FrameRemove(&socket->frameReadHead, &socket->frameReadTail, framePrev);
|
FrameFree(framePrev);
|
FrameFree(framePrev);
|
}
|
}
|
|
|
//Remove socket
|
//Remove socket
|
if(Socket->prev == NULL)
|
if(socket->prev == NULL)
|
SocketHead = Socket->next;
|
SocketHead = socket->next;
|
else
|
else
|
Socket->prev->next = Socket->next;
|
socket->prev->next = socket->next;
|
if(Socket->next)
|
if(socket->next)
|
Socket->next->prev = Socket->prev;
|
socket->next->prev = socket->prev;
|
free(Socket);
|
free(socket);
|
OS_MutexPost(IPMutex);
|
OS_MutexPost(IPMutex);
|
}
|
}
|
|
|
|
|
void IPClose(IPSocket *Socket)
|
void IPClose(IPSocket *socket)
|
{
|
{
|
IPFrame *frameOut;
|
IPFrame *frameOut;
|
|
|
IPWriteFlush(Socket);
|
IPWriteFlush(socket);
|
if(Socket->state == IP_UDP)
|
if(socket->state <= IP_UDP)
|
{
|
{
|
IPClose2(Socket);
|
IPClose2(socket);
|
return;
|
return;
|
}
|
}
|
frameOut = IPFrameGet(0);
|
frameOut = IPFrameGet(0);
|
if(frameOut == 0)
|
if(frameOut == 0)
|
return;
|
return;
|
frameOut->packet[TCP_FLAGS] = TCP_FLAGS_FIN | TCP_FLAGS_ACK;
|
frameOut->packet[TCP_FLAGS] = TCP_FLAGS_FIN | TCP_FLAGS_ACK;
|
TCPSendPacket(Socket, frameOut, TCP_DATA);
|
TCPSendPacket(socket, frameOut, TCP_DATA);
|
++Socket->seq;
|
++socket->seq;
|
if(Socket->state == IP_FIN_CLIENT)
|
if(socket->state == IP_FIN_CLIENT)
|
IPClose2(Socket);
|
IPClose2(socket);
|
else
|
else
|
Socket->state = IP_FIN_SERVER;
|
socket->state = IP_FIN_SERVER;
|
}
|
}
|
|
|
|
|
//static void ShowIP(IPSocket *socket, uint32 ipAddress)
|
void IPPrintf(IPSocket *socket, char *message)
|
//{
|
{
|
// (void)socket;
|
IPWrite(socket, (uint8*)message, (int)strlen(message));
|
// printf("IP=0x%x\n", ipAddress);
|
IPWriteFlush(socket);
|
//}
|
}
|
|
|
|
|
void IPTick(void)
|
void IPTick(void)
|
{
|
{
|
IPFrame *frame, *frame2;
|
IPFrame *frame, *frame2;
|
Line 1465... |
Line 1502... |
|
|
static void DnsCallback(IPSocket *socket)
|
static void DnsCallback(IPSocket *socket)
|
{
|
{
|
uint8 buf[200], *ptr;
|
uint8 buf[200], *ptr;
|
uint32 ipAddress;
|
uint32 ipAddress;
|
int i, length;
|
int bytes;
|
|
|
memset(buf, 0, sizeof(buf));
|
memset(buf, 0, sizeof(buf));
|
IPRead(socket, buf, sizeof(buf));
|
bytes = IPRead(socket, buf, sizeof(buf));
|
if(buf[DNS_NUM_ANSWERS_RR+1])
|
if(buf[DNS_NUM_ANSWERS_RR+1])
|
{
|
{
|
ptr = buf + DNS_QUESTIONS;
|
ptr = buf + DNS_QUESTIONS;
|
while(*ptr)
|
for(ptr = buf + DNS_QUESTIONS; ptr + 14 <= buf + bytes; ++ptr)
|
++ptr;
|
|
++ptr;
|
|
ptr += 4;
|
|
for(i = 0; (int)(ptr - buf) < sizeof(buf) && i < buf[DNS_NUM_ANSWERS_RR+1]; ++i)
|
|
{
|
{
|
if(ptr[2] == 0 && ptr[3] == 1 && ptr[4] == 0 && ptr[5] == 1)
|
if(ptr[0] == 0 && ptr[1] == 0 && ptr[2] == 1 && ptr[3] == 0 &&
|
|
ptr[4] == 1 && ptr[9] == 0 && ptr[10] == 4)
|
{
|
{
|
ipAddress = (ptr[12] << 24) | (ptr[13] << 16) | (ptr[14] << 8) | ptr[15];
|
ipAddress = (ptr[11] << 24) | (ptr[12] << 16) | (ptr[13] << 8) | ptr[14];
|
printf("ipAddress = %d.%d.%d.%d\n", ptr[12], ptr[13], ptr[14], ptr[16]);
|
printf("ipAddress = %d.%d.%d.%d\n", ptr[11], ptr[12], ptr[13], ptr[14]);
|
socket->userData = ipAddress;
|
socket->userData = ipAddress;
|
if(socket->userFunc)
|
if(socket->userFunc)
|
{
|
{
|
socket->userFunc(socket, ipAddress);
|
socket->userFunc(socket, ipAddress, socket->userPtr);
|
}
|
}
|
break;
|
break;
|
}
|
}
|
length = (ptr[10] << 8) | ptr[11];
|
|
ptr += 12 + length;
|
|
}
|
}
|
}
|
}
|
if(FrameSendFunc)
|
if(FrameSendFunc)
|
IPClose(socket);
|
IPClose(socket);
|
}
|
}
|
|
|
|
|
uint32 IPResolve(char *Name, IPFuncPtr resolvedFunc)
|
uint32 IPResolve(char *name, IPFuncPtr resolvedFunc, void *arg)
|
{
|
{
|
uint8 buf[200], *ptr;
|
uint8 buf[200], *ptr;
|
int length, i;
|
int length, i;
|
IPSocket *socket;
|
IPSocket *socket;
|
uint32 ipAddress=0;
|
uint32 ipAddress=0;
|
Line 1513... |
Line 1545... |
buf[DNS_FLAGS] = 1;
|
buf[DNS_FLAGS] = 1;
|
buf[DNS_NUM_QUESTIONS+1] = 1;
|
buf[DNS_NUM_QUESTIONS+1] = 1;
|
|
|
//Setup name
|
//Setup name
|
ptr = buf + DNS_QUESTIONS;
|
ptr = buf + DNS_QUESTIONS;
|
strncpy((char*)ptr+1, Name, 100);
|
strncpy((char*)ptr+1, name, 100);
|
ptr[0] = 1;
|
ptr[0] = 1;
|
while(ptr[0])
|
while(ptr[0])
|
{
|
{
|
for(i = 0; i < 100; ++i)
|
for(i = 0; i < 100; ++i)
|
{
|
{
|
Line 1535... |
Line 1567... |
length = (int)(ptr - buf) + 4;
|
length = (int)(ptr - buf) + 4;
|
if(length < 60)
|
if(length < 60)
|
length = 60;
|
length = 60;
|
|
|
socket->userFunc = (IPFuncPtr)resolvedFunc;
|
socket->userFunc = (IPFuncPtr)resolvedFunc;
|
|
socket->userPtr = arg;
|
socket->userData = 0;
|
socket->userData = 0;
|
IPWrite(socket, buf, length);
|
IPWrite(socket, buf, length);
|
|
|
if(FrameSendFunc == NULL)
|
if(FrameSendFunc == NULL)
|
{
|
{
|