Line 17... |
Line 17... |
* IPSendPacket()
|
* IPSendPacket()
|
* IPChecksum()
|
* IPChecksum()
|
* IPSendFrame()
|
* IPSendFrame()
|
* FrameInsert()
|
* FrameInsert()
|
*--------------------------------------------------------------------*/
|
*--------------------------------------------------------------------*/
|
#ifdef WIN32
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <assert.h>
|
|
#define _LIBC
|
|
#endif
|
|
#include "rtos.h"
|
#include "rtos.h"
|
#define IPPRINTF
|
#define IPPRINTF
|
#include "tcpip.h"
|
#include "tcpip.h"
|
|
|
|
|
//ETHER FIELD OFFSET LENGTH VALUE
|
//ETHER FIELD OFFSET LENGTH VALUE
|
#define ETHERNET_DEST 0 //6
|
#define ETHERNET_DEST 0 //6
|
#define ETHERNET_SOURCE 6 //6
|
#define ETHERNET_SOURCE 6 //6
|
#define ETHERNET_FRAME_TYPE 12 //2 IP=0x0800; ARP=0x0806
|
#define ETHERNET_FRAME_TYPE 12 //2 IP=0x0800; ARP=0x0806
|
|
|
Line 179... |
Line 171... |
static IPFrame *FrameResendHead;
|
static IPFrame *FrameResendHead;
|
static IPFrame *FrameResendTail;
|
static IPFrame *FrameResendTail;
|
static IPSocket *SocketHead;
|
static IPSocket *SocketHead;
|
static uint32 Seconds;
|
static uint32 Seconds;
|
static int DhcpRetrySeconds;
|
static int DhcpRetrySeconds;
|
static IPFuncPtr FrameSendFunc;
|
static IPSendFuncPtr FrameSendFunc;
|
static OS_MQueue_t *IPMQueue;
|
static OS_MQueue_t *IPMQueue;
|
static OS_Thread_t *IPThread;
|
static OS_Thread_t *IPThread;
|
int IPVerbose=1;
|
int IPVerbose=1;
|
|
|
static const unsigned char dhcpDiscover[] = {
|
static const unsigned char dhcpDiscover[] = {
|
Line 330... |
Line 322... |
pseudo[PSEUDO_ZERO] = 0;
|
pseudo[PSEUDO_ZERO] = 0;
|
pseudo[PSEUDO_IP_PROTOCOL] = packet[IP_PROTOCOL];
|
pseudo[PSEUDO_IP_PROTOCOL] = packet[IP_PROTOCOL];
|
memcpy(pseudo+PSEUDO_LENGTH, packet+UDP_LENGTH, 2);
|
memcpy(pseudo+PSEUDO_LENGTH, packet+UDP_LENGTH, 2);
|
checksum = IPChecksum(0xffff, pseudo, 12);
|
checksum = IPChecksum(0xffff, pseudo, 12);
|
length2 = (packet[UDP_LENGTH] << 8) + packet[UDP_LENGTH+1];
|
length2 = (packet[UDP_LENGTH] << 8) + packet[UDP_LENGTH+1];
|
checksum = IPChecksum(checksum, packet+UDP_SOURCE_PORT, length);
|
checksum = IPChecksum(checksum, packet+UDP_SOURCE_PORT, length2);
|
}
|
}
|
else if(packet[IP_PROTOCOL] == 0x06) //TCP
|
else if(packet[IP_PROTOCOL] == 0x06) //TCP
|
{
|
{
|
memcpy(pseudo+PSEUDO_IP_SOURCE, packet+IP_SOURCE, 4);
|
memcpy(pseudo+PSEUDO_IP_SOURCE, packet+IP_SOURCE, 4);
|
memcpy(pseudo+PSEUDO_IP_DEST, packet+IP_DEST, 4);
|
memcpy(pseudo+PSEUDO_IP_DEST, packet+IP_DEST, 4);
|
Line 746... |
Line 738... |
if(SocketHead)
|
if(SocketHead)
|
SocketHead->prev = socketNew;
|
SocketHead->prev = socketNew;
|
SocketHead = socketNew;
|
SocketHead = socketNew;
|
OS_MutexPost(IPMutex);
|
OS_MutexPost(IPMutex);
|
if(socketNew->funcPtr)
|
if(socketNew->funcPtr)
|
OS_Job(socketNew->funcPtr, socketNew, 0, 0);
|
OS_Job((JobFunc_t)socketNew->funcPtr, socketNew, 0, 0);
|
return 0;
|
return 0;
|
}
|
}
|
}
|
}
|
|
|
//Send reset
|
//Send reset
|
Line 849... |
Line 841... |
{
|
{
|
frameOut->packet[TCP_FLAGS] = TCP_FLAGS_ACK;
|
frameOut->packet[TCP_FLAGS] = TCP_FLAGS_ACK;
|
TCPSendPacket(socket, frameOut, TCP_DATA);
|
TCPSendPacket(socket, frameOut, TCP_DATA);
|
}
|
}
|
if(socket->funcPtr)
|
if(socket->funcPtr)
|
OS_Job(socket->funcPtr, socket, 0, 0);
|
OS_Job((JobFunc_t)socket->funcPtr, socket, 0, 0);
|
return 0;
|
return 0;
|
}
|
}
|
if(packet[TCP_HEADER_LENGTH] != 0x50)
|
if(packet[TCP_HEADER_LENGTH] != 0x50)
|
{
|
{
|
if(IPVerbose)
|
if(IPVerbose)
|
Line 923... |
Line 915... |
socket->state = IP_FIN_CLIENT;
|
socket->state = IP_FIN_CLIENT;
|
}
|
}
|
|
|
//Notify application
|
//Notify application
|
if(socket->funcPtr && notify)
|
if(socket->funcPtr && notify)
|
OS_Job(socket->funcPtr, socket, 0, 0);
|
OS_Job((JobFunc_t)socket->funcPtr, socket, 0, 0);
|
return rc;
|
return rc;
|
}
|
}
|
|
|
|
|
int IPProcessEthernetPacket(IPFrame *frameIn, int length)
|
int IPProcessEthernetPacket(IPFrame *frameIn, int length)
|
Line 1021... |
Line 1013... |
for(socket = SocketHead; socket; socket = socket->next)
|
for(socket = SocketHead; socket; socket = socket->next)
|
{
|
{
|
if(socket->state == IP_PING &&
|
if(socket->state == IP_PING &&
|
memcmp(packet+IP_SOURCE, socket->headerSend+IP_DEST, 4) == 0)
|
memcmp(packet+IP_SOURCE, socket->headerSend+IP_DEST, 4) == 0)
|
{
|
{
|
OS_Job(socket->funcPtr, socket, 0, 0);
|
OS_Job((JobFunc_t)socket->funcPtr, socket, 0, 0);
|
return 0;
|
return 0;
|
}
|
}
|
}
|
}
|
}
|
}
|
if(packet[PING_TYPE] != 8)
|
if(packet[PING_TYPE] != 8)
|
Line 1077... |
Line 1069... |
if(socket)
|
if(socket)
|
{
|
{
|
if(IPVerbose)
|
if(IPVerbose)
|
printf("U");
|
printf("U");
|
FrameInsert(&socket->frameReadHead, &socket->frameReadTail, frameIn);
|
FrameInsert(&socket->frameReadHead, &socket->frameReadTail, frameIn);
|
OS_Job(socket->funcPtr, socket, 0, 0);
|
OS_Job((JobFunc_t)socket->funcPtr, socket, 0, 0);
|
return 1;
|
return 1;
|
}
|
}
|
}
|
}
|
return 0;
|
return 0;
|
}
|
}
|
Line 1156... |
Line 1148... |
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, uint8 macAddress[6], char name[6])
|
void IPInit(IPSendFuncPtr frameSendFunction, uint8 macAddress[6], char name[6])
|
{
|
{
|
int i;
|
int i;
|
IPFrame *frame;
|
IPFrame *frame;
|
|
|
if(macAddress)
|
if(macAddress)
|
Line 1187... |
Line 1179... |
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, IPSockFuncPtr funcPtr)
|
{
|
{
|
IPSocket *socket;
|
IPSocket *socket;
|
uint8 *ptrSend, *ptrRcv;
|
uint8 *ptrSend, *ptrRcv;
|
IPFrame *frame;
|
IPFrame *frame;
|
static int portSource=0x1007;
|
static int portSource=0x1007;
|
Line 1682... |
Line 1674... |
ipAddress = (ptr[10] << 24) | (ptr[11] << 16) | (ptr[12] << 8) | ptr[13];
|
ipAddress = (ptr[10] << 24) | (ptr[11] << 16) | (ptr[12] << 8) | ptr[13];
|
printf("ipAddress = %d.%d.%d.%d\n", ptr[10], ptr[11], ptr[12], ptr[13]);
|
printf("ipAddress = %d.%d.%d.%d\n", ptr[10], ptr[11], ptr[12], ptr[13]);
|
socket->userData = ipAddress;
|
socket->userData = ipAddress;
|
if(socket->userFunc)
|
if(socket->userFunc)
|
{
|
{
|
socket->userFunc(socket, ipAddress, socket->userPtr);
|
socket->userFunc(socket, socket->userPtr, ipAddress);
|
}
|
}
|
break;
|
break;
|
}
|
}
|
}
|
}
|
}
|
}
|
IPClose(socket);
|
IPClose(socket);
|
}
|
}
|
|
|
|
|
void IPResolve(char *name, IPFuncPtr resolvedFunc, void *arg)
|
void IPResolve(char *name, IPCallbackPtr resolvedFunc, void *arg)
|
{
|
{
|
uint8 buf[200], *ptr;
|
uint8 buf[200], *ptr;
|
int length, i;
|
int length, i;
|
IPSocket *socket;
|
IPSocket *socket;
|
|
|
Line 1727... |
Line 1719... |
ptr[3] = DNS_QUERY_CLASS;
|
ptr[3] = DNS_QUERY_CLASS;
|
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 = resolvedFunc;
|
socket->userPtr = arg;
|
socket->userPtr = arg;
|
socket->userData = 0;
|
socket->userData = 0;
|
IPWrite(socket, buf, length);
|
IPWrite(socket, buf, length);
|
}
|
}
|
|
|