OpenCores
URL https://opencores.org/ocsvn/mlite/mlite/trunk

Subversion Repositories mlite

[/] [mlite/] [trunk/] [kernel/] [tcpip.c] - Blame information for rev 350

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 155 rhoads
/*--------------------------------------------------------------------
2
 * TITLE: Plasma TCP/IP Protocol Stack
3
 * AUTHOR: Steve Rhoads (rhoadss@yahoo.com)
4
 * DATE CREATED: 4/22/06
5
 * FILENAME: tcpip.c
6
 * PROJECT: Plasma CPU core
7
 * COPYRIGHT: Software placed into the public domain by the author.
8
 *    Software 'as is' without warranty.  Author liable for nothing.
9
 * DESCRIPTION:
10
 *    Plasma TCP/IP Protocol Stack
11
 *
12
 *    Possible call stack when receiving a packet:
13
 *       IPMainThread()
14
 *          IPProcessEthernetPacket()
15
 *             IPProcessTCPPacket()
16
 *                TCPSendPacket()
17
 *                   IPSendPacket()
18
 *                      IPChecksum()
19
 *                      IPSendFrame()
20
 *                         FrameInsert()
21
 *--------------------------------------------------------------------*/
22
#ifdef WIN32
23
#include <stdio.h>
24
#include <stdlib.h>
25
#include <string.h>
26
#include <assert.h>
27
#define _LIBC
28
#endif
29
#include "rtos.h"
30 317 rhoads
#define IPPRINTF
31 155 rhoads
#include "tcpip.h"
32
 
33
 
34
//ETHER FIELD                 OFFSET   LENGTH   VALUE
35
#define ETHERNET_DEST         0        //6
36
#define ETHERNET_SOURCE       6        //6
37
#define ETHERNET_FRAME_TYPE   12       //2      IP=0x0800; ARP=0x0806
38
 
39
//ARP   FIELD                 OFFSET   LENGTH   VALUE
40
#define ARP_HARD_TYPE         14       //2      0x0001
41
#define ARP_PROT_TYPE         16       //2      0x0800
42
#define ARP_HARD_SIZE         18       //1      0x06
43
#define ARP_PROT_SIZE         19       //1      0x04
44
#define ARP_OP                20       //2      ARP=1;ARPreply=2
45
#define ARP_ETHERNET_SENDER   22       //6
46
#define ARP_IP_SENDER         28       //4
47
#define ARP_ETHERNET_TARGET   32       //6
48
#define ARP_IP_TARGET         38       //4
49
#define ARP_PAD               42       //18     0
50
 
51
//IP    FIELD                 OFFSET   LENGTH   VALUE
52
#define IP_VERSION_LENGTH     14       //1      0x45
53
#define IP_TYPE_OF_SERVICE    15       //1      0x00
54
#define IP_LENGTH             16       //2
55
#define IP_ID16               18       //2
56
#define IP_FRAG_OFFSET        20       //2
57
#define IP_TIME_TO_LIVE       22       //1      0x80
58
#define IP_PROTOCOL           23       //1      TCP=0x06;PING=0x01;UDP=0x11
59
#define IP_CHECKSUM           24       //2
60
#define IP_SOURCE             26       //4
61
#define IP_DEST               30       //4
62
 
63
//PSEUDO FIELD                OFFSET   LENGTH   VALUE
64
#define PSEUDO_IP_SOURCE      0        //4
65
#define PSEUDO_IP_DEST        4        //4
66
#define PSEUDO_ZERO           8        //1      0
67
#define PSEUDO_IP_PROTOCOL    9        //1
68
#define PSEUDO_LENGTH         10       //2
69
 
70
//UDP   FIELD                 OFFSET   LENGTH   VALUE
71
#define UDP_SOURCE_PORT       34       //2
72
#define UDP_DEST_PORT         36       //2
73
#define UDP_LENGTH            38       //2
74
#define UDP_CHECKSUM          40       //2
75
#define UDP_DATA              42
76
 
77
//DHCP  FIELD                 OFFSET   LENGTH   VALUE
78
#define DHCP_OPCODE           42       //1      REQUEST=1;REPLY=2
79
#define DHCP_HW_TYPE          43       //1      1
80
#define DHCP_HW_LEN           44       //1      6
81
#define DHCP_HOP_COUNT        45       //1      0
82
#define DHCP_TRANS_ID         46       //4
83
#define DHCP_NUM_SEC          50       //2      0
84
#define DHCP_UNUSED           52       //2
85
#define DHCP_CLIENT_IP        54       //4
86
#define DHCP_YOUR_IP          58       //4
87
#define DHCP_SERVER_IP        62       //4
88
#define DHCP_GATEWAY_IP       66       //4
89
#define DHCP_CLIENT_ETHERNET  70       //16
90
#define DHCP_SERVER_NAME      86       //64
91
#define DHCP_BOOT_FILENAME    150      //128
92
#define DHCP_MAGIC_COOKIE     278      //4      0x63825363
93
#define DHCP_OPTIONS          282      //N
94
 
95
#define DHCP_MESSAGE_TYPE     53       //1 type
96
#define DHCP_DISCOVER         1
97
#define DHCP_OFFER            2
98
#define DHCP_REQUEST          3
99
#define DHCP_ACK              5
100
#define DHCP_REQUEST_IP       50       //4 ip
101
#define DHCP_REQUEST_SERV_IP  54       //4 ip
102
#define DHCP_CLIENT_ID        61       //7 1 ethernet
103
#define DHCP_HOST_NAME        12       //6 plasma
104
#define DHCP_PARAMS           55       //4 1=subnet; 15=domain_name; 3=router; 6=dns
105
#define DHCP_PARAM_SUBNET     1
106
#define DHCP_PARAM_ROUTER     3
107
#define DHCP_PARAM_DNS        6
108
#define DHCP_END_OPTION       0xff
109
 
110
//DHCP  FIELD                 OFFSET   LENGTH   VALUE
111 206 rhoads
#define DNS_ID                0        //2    
112
#define DNS_FLAGS             2        //2      
113
#define DNS_NUM_QUESTIONS     4        //2      1 
114
#define DNS_NUM_ANSWERS_RR    6        //2      0/1
115
#define DNS_NUM_AUTHORITY_RR  8        //2      0 
116 155 rhoads
#define DNS_NUM_ADDITIONAL_RR 10       //2      0
117
#define DNS_QUESTIONS         12       //2   
118
 
119
#define DNS_FLAGS_RESPONSE    0x8000
120
#define DNS_FLAGS_RECURSIVE   0x0100
121
#define DNS_FLAGS_ERROR       0x0003
122
#define DNS_FLAGS_OK          0x0000
123
#define DNS_QUERY_TYPE_IP     1
124
#define DNS_QUERY_CLASS       1
125
#define DNS_PORT              53
126
 
127
//TCP   FIELD                 OFFSET   LENGTH   VALUE
128
#define TCP_SOURCE_PORT       34       //2
129
#define TCP_DEST_PORT         36       //2
130
#define TCP_SEQ               38       //4
131
#define TCP_ACK               42       //4
132
#define TCP_HEADER_LENGTH     46       //1      0x50
133
#define TCP_FLAGS             47       //1      SYNC=0x2;ACK=0x10;FIN=0x1
134
#define TCP_WINDOW_SIZE       48       //2
135
#define TCP_CHECKSUM          50       //2
136
#define TCP_URGENT_POINTER    52       //2
137
#define TCP_DATA              54       //length-N
138
 
139
#define TCP_FLAGS_FIN         1
140
#define TCP_FLAGS_SYN         2
141
#define TCP_FLAGS_RST         4
142 209 rhoads
#define TCP_FLAGS_PSH         8
143 155 rhoads
#define TCP_FLAGS_ACK         16
144
 
145
//PING  FIELD                 OFFSET   LENGTH   VALUE
146
#define PING_TYPE             34       //1      SEND=8;REPLY=0
147
#define PING_CODE             35       //1      0
148
#define PING_CHECKSUM         36       //2
149
#define PING_ID               38       //2
150
#define PING_SEQUENCE         40       //2
151
#define PING_DATA             44
152
 
153
static void IPClose2(IPSocket *Socket);
154
 
155 206 rhoads
static uint8 ethernetAddressGateway[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
156
#ifndef WIN32
157 155 rhoads
static uint8 ethernetAddressPlasma[] =  {0x00, 0x10, 0xdd, 0xce, 0x15, 0xd4};
158 206 rhoads
#else
159
static uint8 ethernetAddressPlasma[] =  {0x00, 0x10, 0xdd, 0xce, 0x15, 0xd5};
160
#endif
161 155 rhoads
 
162
static uint8 ipAddressPlasma[] =  {0x9d, 0xfe, 0x28, 10};   //changed by DHCP
163
static uint8 ipAddressGateway[] = {0xff, 0xff, 0xff, 0xff}; //changed by DHCP
164
static uint32 ipAddressDns;                                 //changed by DHCP
165
 
166 157 rhoads
static OS_Mutex_t *IPMutex;
167 296 rhoads
static int FrameFreeCount;
168 155 rhoads
static IPFrame *FrameFreeHead;
169
static IPFrame *FrameSendHead;
170
static IPFrame *FrameSendTail;
171
static IPFrame *FrameResendHead;
172
static IPFrame *FrameResendTail;
173
static IPSocket *SocketHead;
174
static uint32 Seconds;
175
static int DhcpRetrySeconds;
176
static IPFuncPtr FrameSendFunc;
177
static OS_MQueue_t *IPMQueue;
178 157 rhoads
static OS_Thread_t *IPThread;
179 155 rhoads
int IPVerbose=1;
180
 
181
static const unsigned char dhcpDiscover[] = {
182
   0xff, 0xff, 0xff, 0xff, 0xff, 0xff,             //dest
183
   0x00, 0x10, 0xdd, 0xce, 0x15, 0xd4,             //src
184
   0x08, 0x00,
185
   0x45, 0x00, 0x01, 0x48, 0x2e, 0xf5, 0x00, 0x00, //ip
186
   0x80, 0x11, 0x0a, 0xb1, 0x00, 0x00, 0x00, 0x00,
187
   0xff, 0xff, 0xff, 0xff,
188
   0x00, 0x44, 0x00, 0x43, 0x01, 0x34, 0x45, 0x66, //udp
189
   0x01, 0x01, 0x06, 0x00, 0x69, 0x26, 0xb5, 0x52  //dhcp
190
};
191
 
192 296 rhoads
static unsigned char dhcpOptions[] = {
193 155 rhoads
   0x63, 0x82, 0x53, 0x63,      //cookie
194
   0x35, 0x01, 0x01,            //DHCP Discover
195
   0x3d, 0x07, 0x01, 0x00, 0x10, 0xdd, 0xce, 0x15, 0xd4, //Client identifier
196 206 rhoads
#ifndef WIN32
197 155 rhoads
   0x0c, 0x06, 'p', 'l', 'a', 's', 'm', 'a',             //Host name
198 206 rhoads
#else
199
   0x0c, 0x06, 'p', 'l', 'a', 's', 'm', 'b',             //Host name
200
#endif
201 155 rhoads
   0x37, 0x03, DHCP_PARAM_SUBNET, DHCP_PARAM_ROUTER, DHCP_PARAM_DNS, //Parameters
202
   DHCP_END_OPTION
203
};
204
 
205
 
206
//Get a free frame; can be called from an ISR
207 157 rhoads
IPFrame *IPFrameGet(int freeCount)
208 155 rhoads
{
209
   IPFrame *frame=NULL;
210
   uint32 state;
211 157 rhoads
 
212
   state = OS_CriticalBegin();
213
   if(FrameFreeCount > freeCount)
214 155 rhoads
   {
215 157 rhoads
      frame = FrameFreeHead;
216
      if(FrameFreeHead)
217 296 rhoads
      {
218 157 rhoads
         FrameFreeHead = FrameFreeHead->next;
219 296 rhoads
         --FrameFreeCount;
220
      }
221 155 rhoads
   }
222 157 rhoads
   OS_CriticalEnd(state);
223 155 rhoads
   if(frame)
224
   {
225 157 rhoads
      assert(frame->state == 0);
226
      frame->state = 1;
227 155 rhoads
   }
228
   return frame;
229
}
230
 
231
 
232
static void FrameFree(IPFrame *frame)
233
{
234
   uint32 state;
235
 
236
   assert(frame->state == 1);
237
   frame->state = 0;
238
   state = OS_CriticalBegin();
239
   frame->next = FrameFreeHead;
240
   FrameFreeHead = frame;
241
   ++FrameFreeCount;
242
   OS_CriticalEnd(state);
243
}
244
 
245
 
246
static void FrameInsert(IPFrame **head, IPFrame **tail, IPFrame *frame)
247
{
248
   assert(frame->state == 1);
249
   frame->state = 2;
250 157 rhoads
   OS_MutexPend(IPMutex);
251 155 rhoads
   frame->prev = NULL;
252
   frame->next = *head;
253
   if(*head)
254
      (*head)->prev = frame;
255
   *head = frame;
256
   if(*tail == NULL)
257
      *tail = frame;
258 157 rhoads
   OS_MutexPost(IPMutex);
259 155 rhoads
}
260
 
261
 
262
static void FrameRemove(IPFrame **head, IPFrame **tail, IPFrame *frame)
263
{
264
   assert(frame->state == 2);
265
   frame->state = 1;
266
   if(frame->prev)
267
      frame->prev->next = frame->next;
268
   else
269
      *head = frame->next;
270
   if(frame->next)
271
      frame->next->prev = frame->prev;
272
   else
273
      *tail = frame->prev;
274
   frame->prev = NULL;
275
   frame->next = NULL;
276
}
277
 
278
 
279
static int IPChecksum(int checksum, const unsigned char *data, int length)
280
{
281
   int i;
282
   checksum = ~checksum & 0xffff;
283
   for(i = 0; i < length-1; i += 2)
284
   {
285
      checksum += (data[i] << 8) | data[i+1];
286
   }
287
   if(i < length)
288
      checksum += data[i] << 8;
289
   while(checksum >> 16)
290
      checksum = (checksum & 0xffff) + (checksum >> 16);
291
   checksum = ~checksum & 0xffff;
292
   return checksum;
293
}
294
 
295
 
296
static int EthernetVerifyChecksums(const unsigned char *packet, int length)
297
{
298
   int checksum, length2;
299
   unsigned char pseudo[12];
300
 
301
   //Calculate checksums
302
   if(packet[ETHERNET_FRAME_TYPE+1] == 0x00)  //IP
303
   {
304
      checksum = IPChecksum(0xffff, packet+IP_VERSION_LENGTH, 20);
305
      if(checksum)
306
         return -1;
307
      if(packet[IP_PROTOCOL] == 0x01)         //PING
308
      {
309
         checksum = IPChecksum(0xffff, packet+PING_TYPE, length-PING_TYPE);
310
      }
311
      else if(packet[IP_PROTOCOL] == 0x11)    //UDP
312
      {
313
         if(packet[UDP_CHECKSUM] == 0 && packet[UDP_CHECKSUM+1] == 0)
314
            return 0;
315
         memcpy(pseudo+PSEUDO_IP_SOURCE, packet+IP_SOURCE, 4);
316
         memcpy(pseudo+PSEUDO_IP_DEST, packet+IP_DEST, 4);
317
         pseudo[PSEUDO_ZERO] = 0;
318
         pseudo[PSEUDO_IP_PROTOCOL] = packet[IP_PROTOCOL];
319
         memcpy(pseudo+PSEUDO_LENGTH, packet+UDP_LENGTH, 2);
320
         checksum = IPChecksum(0xffff, pseudo, 12);
321
         length2 = (packet[UDP_LENGTH] << 8) + packet[UDP_LENGTH+1];
322
         checksum = IPChecksum(checksum, packet+UDP_SOURCE_PORT, length);
323
      }
324
      else if(packet[IP_PROTOCOL] == 0x06)    //TCP
325
      {
326
         memcpy(pseudo+PSEUDO_IP_SOURCE, packet+IP_SOURCE, 4);
327
         memcpy(pseudo+PSEUDO_IP_DEST, packet+IP_DEST, 4);
328
         pseudo[PSEUDO_ZERO] = 0;
329
         pseudo[PSEUDO_IP_PROTOCOL] = packet[IP_PROTOCOL];
330
         length = (packet[IP_LENGTH] << 8) + packet[IP_LENGTH+1];
331
         length2 = length - 20;
332
         pseudo[PSEUDO_LENGTH] = (unsigned char)(length2 >> 8);
333
         pseudo[PSEUDO_LENGTH+1] = (unsigned char)length2;
334
         checksum = IPChecksum(0xffff, pseudo, 12);
335
         checksum = IPChecksum(checksum, packet+TCP_SOURCE_PORT, length2);
336
      }
337
      if(checksum)
338
         return -1;
339
   }
340
   return 0;
341
}
342
 
343
 
344
static void IPFrameReschedule(IPFrame *frame)
345
{
346
   int length;
347
   length = frame->length - TCP_DATA;
348 160 rhoads
   if(frame->packet[TCP_FLAGS] & (TCP_FLAGS_FIN | TCP_FLAGS_SYN))
349 155 rhoads
      ++length;
350
   if(frame->socket == NULL || frame->socket->state == IP_UDP || length == 0 ||
351 209 rhoads
      frame->socket->state == IP_PING || ++frame->retryCnt > 4)
352 155 rhoads
   {
353
      FrameFree(frame);     //can't be ACK'ed
354
   }
355 341 rhoads
#ifdef WIN32
356
   else if(FrameFreeCount < FRAME_COUNT_SYNC)
357
   {
358
      FrameFree(frame);     //can't be ACK'ed
359
   }
360
#endif
361 155 rhoads
   else
362
   {
363
      //Put on resend list until TCP ACK'ed
364 317 rhoads
      frame->timeout = (short)(RETRANSMIT_TIME * frame->retryCnt);
365 155 rhoads
      FrameInsert(&FrameResendHead, &FrameResendTail, frame);
366
   }
367
}
368
 
369
 
370
static void IPSendFrame(IPFrame *frame)
371
{
372
   uint32 message[4];
373
 
374
   if(FrameSendFunc)
375
   {
376
      //Single threaded
377
      FrameSendFunc(frame->packet, frame->length);
378
      IPFrameReschedule(frame);
379
   }
380
   else
381
   {
382
      //Add Packet to send queue
383
      FrameInsert(&FrameSendHead, &FrameSendTail, frame);
384
 
385
      //Wakeup sender thread
386
      message[0] = 2;
387
      OS_MQueueSend(IPMQueue, message);
388
   }
389
}
390
 
391
 
392
static void IPSendPacket(IPSocket *socket, IPFrame *frame, int length)
393
{
394
   int checksum, length2=length;
395
   unsigned char pseudo[12], *packet=frame->packet;
396
 
397
   frame->length = (uint16)length;
398
 
399
   //Calculate checksums
400
   if(packet[ETHERNET_FRAME_TYPE+1] == 0x00)  //IP
401
   {
402
      length2 = length - IP_VERSION_LENGTH;
403
      packet[IP_LENGTH] = (uint8)(length2 >> 8);
404
      packet[IP_LENGTH+1] = (uint8)length2;
405
      memset(packet+IP_CHECKSUM, 0, 2);
406
      checksum = IPChecksum(0xffff, packet+IP_VERSION_LENGTH, 20);
407
      packet[IP_CHECKSUM] = (unsigned char)(checksum >> 8);
408
      packet[IP_CHECKSUM+1] = (unsigned char)checksum;
409 220 rhoads
      if(packet[IP_PROTOCOL] == 0x01)         //ICMP & PING
410 155 rhoads
      {
411
         memset(packet+PING_CHECKSUM, 0, 2);
412
         checksum = IPChecksum(0xffff, packet+PING_TYPE, length-PING_TYPE);
413
         packet[PING_CHECKSUM] = (unsigned char)(checksum >> 8);
414
         packet[PING_CHECKSUM+1] = (unsigned char)checksum;
415
      }
416
      else if(packet[IP_PROTOCOL] == 0x11)    //UDP
417
      {
418
         length2 = length - UDP_SOURCE_PORT;
419
         packet[UDP_LENGTH] = (uint8)(length2 >> 8);
420
         packet[UDP_LENGTH+1] = (uint8)length2;
421
         memcpy(pseudo+PSEUDO_IP_SOURCE, packet+IP_SOURCE, 4);
422
         memcpy(pseudo+PSEUDO_IP_DEST, packet+IP_DEST, 4);
423
         pseudo[PSEUDO_ZERO] = 0;
424
         pseudo[PSEUDO_IP_PROTOCOL] = packet[IP_PROTOCOL];
425
         memcpy(pseudo+PSEUDO_LENGTH, packet+UDP_LENGTH, 2);
426
         checksum = IPChecksum(0xffff, pseudo, 12);
427
         memset(packet+UDP_CHECKSUM, 0, 2);
428
         length2 = (packet[UDP_LENGTH] << 8) + packet[UDP_LENGTH+1];
429
         checksum = IPChecksum(checksum, packet+UDP_SOURCE_PORT, length2);
430
         packet[UDP_CHECKSUM] = (unsigned char)(checksum >> 8);
431
         packet[UDP_CHECKSUM+1] = (unsigned char)checksum;
432
      }
433
      else if(packet[IP_PROTOCOL] == 0x06)    //TCP
434
      {
435
         memcpy(pseudo+PSEUDO_IP_SOURCE, packet+IP_SOURCE, 4);
436
         memcpy(pseudo+PSEUDO_IP_DEST, packet+IP_DEST, 4);
437
         pseudo[PSEUDO_ZERO] = 0;
438
         pseudo[PSEUDO_IP_PROTOCOL] = packet[IP_PROTOCOL];
439
         length2 = (packet[IP_LENGTH] << 8) + packet[IP_LENGTH+1];
440
         length2 = length2 - 20;
441
         pseudo[PSEUDO_LENGTH] = (unsigned char)(length2 >> 8);
442
         pseudo[PSEUDO_LENGTH+1] = (unsigned char)length2;
443
         checksum = IPChecksum(0xffff, pseudo, 12);
444
         memset(packet+TCP_CHECKSUM, 0, 2);
445
         checksum = IPChecksum(checksum, packet+TCP_SOURCE_PORT, length2);
446
         packet[TCP_CHECKSUM] = (unsigned char)(checksum >> 8);
447
         packet[TCP_CHECKSUM+1] = (unsigned char)checksum;
448
      }
449
   }
450
 
451 160 rhoads
   length2 = length - TCP_DATA;
452
   if(socket && (packet[TCP_FLAGS] & (TCP_FLAGS_FIN | TCP_FLAGS_SYN)))
453 155 rhoads
      length2 = 1;
454
   frame->socket = socket;
455
   frame->timeout = 0;
456
   frame->retryCnt = 0;
457
   if(socket)
458
      frame->seqEnd = socket->seq + length2;
459
   IPSendFrame(frame);
460
}
461
 
462
 
463
static void TCPSendPacket(IPSocket *socket, IPFrame *frame, int length)
464
{
465
   uint8 *packet = frame->packet;
466 160 rhoads
   int flags, count;
467 155 rhoads
 
468
   flags = packet[TCP_FLAGS];
469
   memcpy(packet, socket->headerSend, TCP_SEQ);
470 220 rhoads
   packet[TCP_FLAGS] = (uint8)flags;
471 155 rhoads
   if(flags & TCP_FLAGS_SYN)
472
      packet[TCP_HEADER_LENGTH] = 0x60;  //set maximum segment size
473
   else
474
      packet[TCP_HEADER_LENGTH] = 0x50;
475
   packet[TCP_SEQ]   = (uint8)(socket->seq >> 24);
476
   packet[TCP_SEQ+1] = (uint8)(socket->seq >> 16);
477
   packet[TCP_SEQ+2] = (uint8)(socket->seq >> 8);
478
   packet[TCP_SEQ+3] = (uint8)socket->seq;
479
   packet[TCP_ACK]   = (uint8)(socket->ack >> 24);
480
   packet[TCP_ACK+1] = (uint8)(socket->ack >> 16);
481
   packet[TCP_ACK+2] = (uint8)(socket->ack >> 8);
482
   packet[TCP_ACK+3] = (uint8)socket->ack;
483 317 rhoads
   count = RECEIVE_WINDOW - (socket->ack - socket->ackProcessed);
484
   if(count < 0)
485
      count = 0;
486 160 rhoads
   packet[TCP_WINDOW_SIZE] = (uint8)(count >> 8);
487
   packet[TCP_WINDOW_SIZE+1] = (uint8)count;
488 155 rhoads
   packet[TCP_URGENT_POINTER] = 0;
489
   packet[TCP_URGENT_POINTER+1] = 0;
490
   IPSendPacket(socket, frame, length);
491
}
492
 
493
 
494
static void EthernetCreateResponse(unsigned char *packetOut,
495
                                   const unsigned char *packet,
496
                                   int length)
497
{
498
   //Swap destination and source fields
499
   memcpy(packetOut, packet, length);
500
   memcpy(packetOut+ETHERNET_DEST, packet+ETHERNET_SOURCE, 6);
501
   memcpy(packetOut+ETHERNET_SOURCE, packet+ETHERNET_DEST, 6);
502
   if(packet[ETHERNET_FRAME_TYPE+1] == 0x00)  //IP
503
   {
504
      memcpy(packetOut+IP_SOURCE, packet+IP_DEST, 4);
505
      memcpy(packetOut+IP_DEST, packet+IP_SOURCE, 4);
506
      if(packet[IP_PROTOCOL] == 0x06 || packet[IP_PROTOCOL] == 0x11)   //TCP/UDP
507
      {
508
         memcpy(packetOut+TCP_SOURCE_PORT, packet+TCP_DEST_PORT, 2);
509
         memcpy(packetOut+TCP_DEST_PORT, packet+TCP_SOURCE_PORT, 2);
510
      }
511
   }
512
}
513
 
514
 
515
static void IPDhcp(const unsigned char *packet, int length, int state)
516
{
517
   uint8 *packetOut, *ptr;
518
   const uint8 *ptr2;
519
   IPFrame *frame;
520
   static int request=0;
521
 
522
   if(state == 1)
523
   {
524
      //Create DHCP Discover
525 157 rhoads
      frame = IPFrameGet(0);
526 155 rhoads
      if(frame == NULL)
527
         return;
528
      packetOut = frame->packet;
529
      memset(packetOut, 0, 512);
530
      memcpy(packetOut, dhcpDiscover, sizeof(dhcpDiscover));
531
      memcpy(packetOut+ETHERNET_SOURCE, ethernetAddressPlasma, 6);
532
      memcpy(packetOut+DHCP_CLIENT_ETHERNET, ethernetAddressPlasma, 6);
533
      memcpy(packetOut+DHCP_MAGIC_COOKIE, dhcpOptions, sizeof(dhcpOptions));
534
      memcpy(packetOut+DHCP_MAGIC_COOKIE+10, ethernetAddressPlasma, 6);
535
      IPSendPacket(NULL, frame, 400);
536
      request = DHCP_DISCOVER;
537 296 rhoads
      DhcpRetrySeconds = 2;
538 155 rhoads
   }
539
   else if(state == 2 && memcmp(packet+DHCP_CLIENT_ETHERNET, ethernetAddressPlasma, 6) == 0)
540
   {
541
      if(packet[DHCP_MAGIC_COOKIE+6] == DHCP_OFFER && request == DHCP_DISCOVER)
542
      {
543
         //Process DHCP Offer and send DHCP Request
544 157 rhoads
         frame = IPFrameGet(0);
545 155 rhoads
         if(frame == NULL)
546
            return;
547
         packetOut = frame->packet;
548
         memset(packetOut, 0, 512);
549
         memcpy(packetOut, dhcpDiscover, sizeof(dhcpDiscover));
550
         memcpy(packetOut+ETHERNET_SOURCE, ethernetAddressPlasma, 6);
551
         memcpy(packetOut+DHCP_CLIENT_ETHERNET, ethernetAddressPlasma, 6);
552
         memcpy(packetOut+DHCP_MAGIC_COOKIE, dhcpOptions, sizeof(dhcpOptions));
553 206 rhoads
         memcpy(packetOut+DHCP_MAGIC_COOKIE+10, ethernetAddressPlasma, 6);
554 155 rhoads
         request = DHCP_REQUEST;
555
         packetOut[DHCP_MAGIC_COOKIE+6] = DHCP_REQUEST;
556
         ptr = packetOut+DHCP_MAGIC_COOKIE+sizeof(dhcpOptions)-1;
557
         ptr[0] = DHCP_REQUEST_IP;
558
         ptr[1] = 4;
559
         memcpy(ptr+2, packet+DHCP_YOUR_IP, 4);
560
         ptr[6] = DHCP_REQUEST_SERV_IP;
561
         ptr[7] = 4;
562
         memcpy(ptr+8, packet+DHCP_SERVER_IP, 4);
563
         ptr[12] = DHCP_END_OPTION;
564
         IPSendPacket(NULL, frame, 400);
565
      }
566
      else if(packet[DHCP_MAGIC_COOKIE+6] == DHCP_ACK && request == DHCP_REQUEST)
567
      {
568
         //Process DHCP Ack
569
         request = 0;
570
         DhcpRetrySeconds = 3600*4;
571
         memcpy(ipAddressPlasma, packet+DHCP_YOUR_IP, 4);
572
         printf("IP=%d.%d.%d.%d ", ipAddressPlasma[0], ipAddressPlasma[1],
573
            ipAddressPlasma[2], ipAddressPlasma[3]);
574
         memcpy(ipAddressGateway, packet+DHCP_GATEWAY_IP, 4);
575
         if(ipAddressGateway[0] == 0 && ipAddressGateway[1] == 0 &&
576
            ipAddressGateway[2] == 0 && ipAddressGateway[3] == 0)
577
            memcpy(ipAddressGateway, packet+DHCP_SERVER_IP, 4);
578
         printf("GW=%d.%d.%d.%d ", ipAddressGateway[0], ipAddressGateway[1],
579
            ipAddressGateway[2], ipAddressGateway[3]);
580
         memcpy(ethernetAddressGateway, packet+ETHERNET_SOURCE, 6);
581
         ptr2 = packet+DHCP_MAGIC_COOKIE+4;
582
         while(ptr2[0] != DHCP_END_OPTION && (int)(ptr2 - packet) < length)
583
         {
584
            if(ptr2[0] == DHCP_PARAM_DNS)
585
            {
586
               ipAddressDns = (ptr2[2] << 24) | (ptr2[3] << 16) | (ptr2[4] << 8) | ptr2[5];
587
               printf("DNS=%d.%d.%d.%d ", ptr2[2], ptr2[3], ptr2[4], ptr2[5]);
588
            }
589
            ptr2 += ptr2[1] + 2;
590
         }
591
 
592
         //Check if DHCP reply came from gateway
593
         if(memcmp(packet+IP_SOURCE, ipAddressGateway, 4))
594
         {
595
            //Send ARP to gateway
596 157 rhoads
            frame = IPFrameGet(0);
597 155 rhoads
            if(frame == NULL)
598
               return;
599
            packetOut = frame->packet;
600
            memset(packetOut, 0, 512);
601
            memset(packetOut+ETHERNET_DEST, 0xff, 6);
602
            memcpy(packetOut+ETHERNET_SOURCE, ethernetAddressPlasma, 6);
603
            packetOut[ETHERNET_FRAME_TYPE] = 0x08;
604
            packetOut[ETHERNET_FRAME_TYPE+1] = 0x06;
605
            packetOut[ARP_HARD_TYPE+1] = 0x01;
606
            packetOut[ARP_PROT_TYPE] = 0x08;
607
            packetOut[ARP_HARD_SIZE] = 0x06;
608
            packetOut[ARP_PROT_SIZE] = 0x04;
609
            packetOut[ARP_OP+1] = 1;
610
            memcpy(packetOut+ARP_ETHERNET_SENDER, ethernetAddressPlasma, 6);
611
            memcpy(packetOut+ARP_IP_SENDER, ipAddressPlasma, 4);
612
            memcpy(packetOut+ARP_IP_TARGET, ipAddressGateway, 4);
613
            IPSendPacket(NULL, frame, 60);
614
         }
615
      }
616
   }
617
}
618
 
619
 
620 209 rhoads
uint32 IPAddressSelf(void)
621
{
622
   return (ipAddressPlasma[0] << 24) | (ipAddressPlasma[1] << 16) |
623
          (ipAddressPlasma[2] << 8) | ipAddressPlasma[3];
624
}
625
 
626
 
627 155 rhoads
static int IPProcessTCPPacket(IPFrame *frameIn)
628
{
629
   uint32 seq, ack;
630 304 rhoads
   int length, ip_length, bytes, rc=0, notify=0;
631 155 rhoads
   IPSocket *socket, *socketNew;
632
   IPFrame *frameOut, *frame2, *framePrev;
633
   uint8 *packet, *packetOut;
634
 
635
   packet = frameIn->packet;
636
   length = frameIn->length;
637
 
638
   ip_length = (packet[IP_LENGTH] << 8) | packet[IP_LENGTH+1];
639
   seq = (packet[TCP_SEQ] << 24) | (packet[TCP_SEQ+1] << 16) |
640
         (packet[TCP_SEQ+2] << 8) | packet[TCP_SEQ+3];
641
   ack = (packet[TCP_ACK] << 24) | (packet[TCP_ACK+1] << 16) |
642
         (packet[TCP_ACK+2] << 8) | packet[TCP_ACK+3];
643
 
644
   //Check if start of connection
645 160 rhoads
   if((packet[TCP_FLAGS] & (TCP_FLAGS_SYN | TCP_FLAGS_ACK)) == TCP_FLAGS_SYN)
646 155 rhoads
   {
647
      if(IPVerbose)
648
         printf("S");
649
      //Check if duplicate SYN
650
      for(socket = SocketHead; socket; socket = socket->next)
651
      {
652
         if(socket->state != IP_LISTEN &&
653
            packet[IP_PROTOCOL] == socket->headerRcv[IP_PROTOCOL] &&
654
            memcmp(packet+IP_SOURCE, socket->headerRcv+IP_SOURCE, 8) == 0 &&
655
            memcmp(packet+TCP_SOURCE_PORT, socket->headerRcv+TCP_SOURCE_PORT, 4) == 0)
656
         {
657
            if(IPVerbose)
658
               printf("s");
659
            return 0;
660
         }
661
      }
662
 
663
      //Find an open port
664
      for(socket = SocketHead; socket; socket = socket->next)
665
      {
666
         if(socket->state == IP_LISTEN &&
667
            packet[IP_PROTOCOL] == socket->headerRcv[IP_PROTOCOL] &&
668
            memcmp(packet+TCP_DEST_PORT, socket->headerRcv+TCP_DEST_PORT, 2) == 0)
669
         {
670
            //Create a new socket
671 174 rhoads
            frameOut = IPFrameGet(FRAME_COUNT_SYNC);
672 155 rhoads
            if(frameOut == NULL)
673
               return 0;
674
            socketNew = (IPSocket*)malloc(sizeof(IPSocket));
675
            if(socketNew == NULL)
676
               return 0;
677
            memcpy(socketNew, socket, sizeof(IPSocket));
678
            socketNew->state = IP_TCP;
679 296 rhoads
            socketNew->timeout = SOCKET_TIMEOUT;
680 155 rhoads
            socketNew->ack = seq;
681 317 rhoads
            socketNew->ackProcessed = seq + 1;
682 155 rhoads
            socketNew->seq = socketNew->ack + 0x12345678;
683
            socketNew->seqReceived = socketNew->seq;
684 317 rhoads
            socketNew->seqWindow = (packet[TCP_WINDOW_SIZE] << 8) | packet[TCP_WINDOW_SIZE+1];
685 155 rhoads
 
686
            //Send ACK
687
            packetOut = frameOut->packet;
688
            EthernetCreateResponse(packetOut, packet, length);
689
            memcpy(socketNew->headerRcv, packet, TCP_SEQ);
690
            memcpy(socketNew->headerSend, packetOut, TCP_SEQ);
691
            packetOut[TCP_FLAGS] = TCP_FLAGS_SYN | TCP_FLAGS_ACK;
692
            ++socketNew->ack;
693
            packetOut[TCP_DATA] = 2;    //maximum segment size = 536
694
            packetOut[TCP_DATA+1] = 4;
695
            packetOut[TCP_DATA+2] = 2;
696
            packetOut[TCP_DATA+3] = 24;
697
            TCPSendPacket(socketNew, frameOut, TCP_DATA+4);
698
            ++socketNew->seq;
699
 
700
            //Add socket to linked list
701 157 rhoads
            OS_MutexPend(IPMutex);
702 155 rhoads
            socketNew->next = SocketHead;
703
            socketNew->prev = NULL;
704
            if(SocketHead)
705
               SocketHead->prev = socketNew;
706
            SocketHead = socketNew;
707 157 rhoads
            OS_MutexPost(IPMutex);
708 304 rhoads
            if(socketNew->funcPtr)
709
               OS_Job(socketNew->funcPtr, socketNew, 0, 0);
710 155 rhoads
            return 0;
711
         }
712
      }
713 220 rhoads
 
714
      //Send reset
715
      frameOut = IPFrameGet(0);
716
      if(frameOut == NULL)
717
         return 0;
718
      packetOut = frameOut->packet;
719
      EthernetCreateResponse(packetOut, packet, TCP_DATA);
720
      memset(packetOut+TCP_SEQ, 0, 4);
721
      ++seq;
722
      packetOut[TCP_ACK]   = (uint8)(seq >> 24);
723
      packetOut[TCP_ACK+1] = (uint8)(seq >> 16);
724
      packetOut[TCP_ACK+2] = (uint8)(seq >> 8);
725
      packetOut[TCP_ACK+3] = (uint8)seq;
726
      packetOut[TCP_HEADER_LENGTH] = 0x50;
727
      packetOut[TCP_FLAGS] = TCP_FLAGS_RST;
728
      IPSendPacket(NULL, frameOut, TCP_DATA);
729 155 rhoads
      return 0;
730
   }
731
 
732
   //Find an open socket
733
   for(socket = SocketHead; socket; socket = socket->next)
734
   {
735
      if(packet[IP_PROTOCOL] == socket->headerRcv[IP_PROTOCOL] &&
736
         memcmp(packet+IP_SOURCE, socket->headerRcv+IP_SOURCE, 8) == 0 &&
737
         memcmp(packet+TCP_SOURCE_PORT, socket->headerRcv+TCP_SOURCE_PORT, 4) == 0)
738
      {
739
         break;
740
      }
741
   }
742
   if(socket == NULL)
743 220 rhoads
   {
744 155 rhoads
      return 0;
745 220 rhoads
   }
746 155 rhoads
 
747 317 rhoads
   //Determine window
748
   socket->seqWindow = (packet[TCP_WINDOW_SIZE] << 8) | packet[TCP_WINDOW_SIZE+1];
749
   bytes = ip_length - (TCP_DATA - IP_VERSION_LENGTH);
750
 
751 225 rhoads
   //Check if packets can be removed from retransmition list
752
   if(packet[TCP_FLAGS] & TCP_FLAGS_ACK)
753 155 rhoads
   {
754
      if(ack != socket->seqReceived)
755
      {
756 157 rhoads
         OS_MutexPend(IPMutex);
757 155 rhoads
         for(frame2 = FrameResendHead; frame2; )
758
         {
759
            framePrev = frame2;
760
            frame2 = frame2->next;
761
            if(framePrev->socket == socket && (int)(ack - framePrev->seqEnd) >= 0)
762
            {
763
               //Remove packet from retransmition queue
764 160 rhoads
               if(socket->timeout)
765 231 rhoads
                  socket->timeout = socket->timeoutReset;
766 155 rhoads
               FrameRemove(&FrameResendHead, &FrameResendTail, framePrev);
767
               FrameFree(framePrev);
768
            }
769
         }
770 157 rhoads
         OS_MutexPost(IPMutex);
771 155 rhoads
         socket->seqReceived = ack;
772 318 rhoads
         socket->resentDone = 0;
773 155 rhoads
      }
774 317 rhoads
      else if(ack == socket->seqReceived && bytes == 0 &&
775 318 rhoads
         (packet[TCP_FLAGS] & (TCP_FLAGS_RST | TCP_FLAGS_FIN)) == 0 &&
776
         socket->resentDone == 0)
777 317 rhoads
      {
778
         //Detected that packet was lost, resend all
779
         if(IPVerbose)
780
            printf("A");
781
         OS_MutexPend(IPMutex);
782
         for(frame2 = FrameResendHead; frame2; )
783
         {
784
            framePrev = frame2;
785
            frame2 = frame2->next;
786
            if(framePrev->socket == socket)
787
            {
788
               //Remove packet from retransmition queue
789
               FrameRemove(&FrameResendHead, &FrameResendTail, framePrev);
790
               IPSendFrame(framePrev);
791
            }
792
         }
793
         OS_MutexPost(IPMutex);
794 318 rhoads
         socket->resentDone = 1;
795 317 rhoads
      }
796 225 rhoads
   }
797 155 rhoads
 
798 225 rhoads
   //Check if SYN/ACK
799
   if((packet[TCP_FLAGS] & (TCP_FLAGS_SYN | TCP_FLAGS_ACK)) ==
800
      (TCP_FLAGS_SYN | TCP_FLAGS_ACK))
801
   {
802
      //Ack SYN/ACK
803
      socket->ack = seq + 1;
804 317 rhoads
      socket->ackProcessed = seq + 1;
805 225 rhoads
      frameOut = IPFrameGet(FRAME_COUNT_SEND);
806
      if(frameOut)
807 160 rhoads
      {
808 225 rhoads
         frameOut->packet[TCP_FLAGS] = TCP_FLAGS_ACK;
809
         TCPSendPacket(socket, frameOut, TCP_DATA);
810 160 rhoads
      }
811 225 rhoads
      if(socket->funcPtr)
812
         OS_Job(socket->funcPtr, socket, 0, 0);
813
      return 0;
814
   }
815
   if(packet[TCP_HEADER_LENGTH] != 0x50)
816
   {
817
      if(IPVerbose)
818
         printf("length error\n");
819
      return 0;
820
   }
821
 
822 257 rhoads
   //Check if RST flag set
823
   if(packet[TCP_FLAGS] & TCP_FLAGS_RST)
824
   {
825 304 rhoads
      notify = 1;
826 257 rhoads
      if(socket->state == IP_FIN_SERVER)
827
         IPClose2(socket);
828
      else if(socket->state == IP_TCP)
829
         socket->state = IP_FIN_CLIENT;
830
   }
831 225 rhoads
   //Copy packet into socket
832 257 rhoads
   else if(socket->ack == seq && bytes > 0)
833 225 rhoads
   {
834
      //Insert packet into socket linked list
835 304 rhoads
      notify = 1;
836 225 rhoads
      if(socket->timeout)
837 231 rhoads
         socket->timeout = socket->timeoutReset;
838 225 rhoads
      if(IPVerbose)
839
         printf("D");
840
      if(frameIn->length > ip_length + IP_VERSION_LENGTH)
841 231 rhoads
         frameIn->length = (uint16)(ip_length + IP_VERSION_LENGTH);
842 225 rhoads
      FrameInsert(&socket->frameReadHead, &socket->frameReadTail, frameIn);
843
      socket->ack += bytes;
844
 
845
      //Ack data
846
      frameOut = IPFrameGet(FRAME_COUNT_SEND);
847
      if(frameOut)
848 160 rhoads
      {
849 225 rhoads
         frameOut->packet[TCP_FLAGS] = TCP_FLAGS_ACK;
850
         TCPSendPacket(socket, frameOut, TCP_DATA);
851 160 rhoads
      }
852
 
853 225 rhoads
      //Using frame
854
      rc = 1;
855
   }
856
   else if(bytes)
857
   {
858
      //Ack with current offset since data missing
859
      frameOut = IPFrameGet(FRAME_COUNT_SEND);
860
      if(frameOut)
861 155 rhoads
      {
862 225 rhoads
         frameOut->packet[TCP_FLAGS] = TCP_FLAGS_ACK;
863
         TCPSendPacket(socket, frameOut, TCP_DATA);
864
      }
865
   }
866 155 rhoads
 
867 225 rhoads
   //Check if FIN flag set
868
   if(packet[TCP_FLAGS] & TCP_FLAGS_FIN)
869
   {
870 304 rhoads
      notify = 1;
871 225 rhoads
      socket->timeout = SOCKET_TIMEOUT;
872
      if(IPVerbose)
873
         printf("F");
874
      frameOut = IPFrameGet(0);
875
      if(frameOut == NULL)
876
         return 0;
877
      packetOut = frameOut->packet;
878
      packetOut[TCP_FLAGS] = TCP_FLAGS_ACK;
879
      ++socket->ack;
880
      TCPSendPacket(socket, frameOut, TCP_DATA);
881
      if(socket->state == IP_FIN_SERVER)
882
         IPClose2(socket);
883 231 rhoads
      else if(socket->state == IP_TCP)
884 225 rhoads
         socket->state = IP_FIN_CLIENT;
885
   }
886 155 rhoads
 
887 225 rhoads
   //Notify application
888 304 rhoads
   if(socket->funcPtr && notify)
889 225 rhoads
      OS_Job(socket->funcPtr, socket, 0, 0);
890
   return rc;
891 155 rhoads
}
892
 
893
 
894 247 rhoads
int IPProcessEthernetPacket(IPFrame *frameIn, int length)
895 155 rhoads
{
896
   int ip_length, rc;
897
   IPSocket *socket;
898
   IPFrame *frameOut;
899
   uint8 *packet, *packetOut;
900
 
901
   packet = frameIn->packet;
902 247 rhoads
   frameIn->length = (uint16)length;
903 155 rhoads
 
904
   if(packet[ETHERNET_FRAME_TYPE] != 0x08 || frameIn->length > PACKET_SIZE)
905
      return 0;  //wrong ethernet type, packet not used
906
 
907
   //ARP?
908
   if(packet[ETHERNET_FRAME_TYPE+1] == 0x06)
909
   {
910
      //Check if ARP reply
911
      if(memcmp(packet+ETHERNET_DEST, ethernetAddressPlasma, 6) == 0 &&
912
         packet[ARP_OP+1] == 2 && memcmp(packet+ARP_IP_SENDER, ipAddressGateway, 4) == 0)
913
      {
914
         //Found MAC address for gateway
915
         memcpy(ethernetAddressGateway, packet+ARP_ETHERNET_SENDER, 6);
916
         return 0;
917
      }
918
 
919
      //Check if ARP request
920 326 rhoads
      if(packet[ARP_OP] != 0 || packet[ARP_OP+1] != 1 ||
921 155 rhoads
         memcmp(packet+ARP_IP_TARGET, ipAddressPlasma, 4))
922
         return 0;
923
      //Create ARP response
924 157 rhoads
      frameOut = IPFrameGet(0);
925 155 rhoads
      if(frameOut == NULL)
926
         return 0;
927
      packetOut = frameOut->packet;
928
      memcpy(packetOut, packet, frameIn->length);
929
      memcpy(packetOut+ETHERNET_DEST, packet+ETHERNET_SOURCE, 6);
930
      memcpy(packetOut+ETHERNET_SOURCE, ethernetAddressPlasma, 6);
931
      packetOut[ARP_OP+1] = 2; //ARP reply
932
      memcpy(packetOut+ARP_ETHERNET_SENDER, ethernetAddressPlasma, 6);
933
      memcpy(packetOut+ARP_IP_SENDER, packet+ARP_IP_TARGET, 4);
934
      memcpy(packetOut+ARP_ETHERNET_TARGET, packet+ARP_ETHERNET_SENDER, 6);
935
      memcpy(packetOut+ARP_IP_TARGET, packet+ARP_IP_SENDER, 4);
936
      IPSendPacket(NULL, frameOut, frameIn->length);
937
      return 0;
938
   }
939
 
940
   //Check if proper type of packet
941
   ip_length = (packet[IP_LENGTH] << 8) | packet[IP_LENGTH+1];
942
   if(frameIn->length < UDP_DATA || ip_length > frameIn->length - IP_VERSION_LENGTH)
943
      return 0;
944
   if(packet[ETHERNET_FRAME_TYPE+1] != 0x00 ||
945
      packet[IP_VERSION_LENGTH] != 0x45)
946
      return 0;
947
 
948
   //Check if DHCP reply
949
   if(packet[IP_PROTOCOL] == 0x11 &&
950
      packet[UDP_SOURCE_PORT] == 0 && packet[UDP_SOURCE_PORT+1] == 67 &&
951
      packet[UDP_DEST_PORT] == 0 && packet[UDP_DEST_PORT+1] == 68)
952
   {
953
      IPDhcp(packet, frameIn->length, 2);            //DHCP reply
954
      return 0;
955
   }
956
 
957
   //Check if correct destination address
958
   if(memcmp(packet+ETHERNET_DEST, ethernetAddressPlasma, 6) ||
959
      memcmp(packet+IP_DEST, ipAddressPlasma, 4))
960
      return 0;
961
   rc = EthernetVerifyChecksums(packet, frameIn->length);
962
   //if(rc)
963
   //{
964
   //   printf("C ");
965
   //   return;
966
   //}
967
 
968
   //PING request?
969
   if(packet[IP_PROTOCOL] == 1)
970
   {
971 209 rhoads
      if(packet[PING_TYPE] == 0)  //PING reply
972
      {
973
         for(socket = SocketHead; socket; socket = socket->next)
974
         {
975
            if(socket->state == IP_PING &&
976
               memcmp(packet+IP_SOURCE, socket->headerSend+IP_DEST, 4) == 0)
977
            {
978 225 rhoads
               OS_Job(socket->funcPtr, socket, 0, 0);
979 209 rhoads
               return 0;
980
            }
981
         }
982
      }
983 155 rhoads
      if(packet[PING_TYPE] != 8)
984
         return 0;
985 160 rhoads
      frameOut = IPFrameGet(FRAME_COUNT_SEND);
986 155 rhoads
      if(frameOut == NULL)
987
         return 0;
988
      packetOut = frameOut->packet;
989
      EthernetCreateResponse(packetOut, packet, frameIn->length);
990
      frameOut->packet[PING_TYPE] = 0;       //PING reply
991
      IPSendPacket(NULL, frameOut, frameIn->length);
992
      return 0;
993
   }
994
 
995
   //TCP packet?
996
   if(packet[IP_PROTOCOL] == 0x06)
997
   {
998
      return IPProcessTCPPacket(frameIn);
999
   }
1000
 
1001
   //UDP packet?
1002
   if(packet[IP_PROTOCOL] == 0x11)
1003
   {
1004
      //Find open socket
1005
      for(socket = SocketHead; socket; socket = socket->next)
1006
      {
1007
         if(packet[IP_PROTOCOL] == socket->headerRcv[IP_PROTOCOL] &&
1008
            memcmp(packet+IP_SOURCE, socket->headerRcv+IP_SOURCE, 8) == 0 &&
1009
            memcmp(packet+UDP_SOURCE_PORT, socket->headerRcv+UDP_SOURCE_PORT, 2) == 0)
1010
         {
1011
            break;
1012
         }
1013
      }
1014
 
1015
      if(socket == NULL)
1016
      {
1017
         //Find listening socket
1018
         for(socket = SocketHead; socket; socket = socket->next)
1019
         {
1020
            if(packet[IP_PROTOCOL] == socket->headerRcv[IP_PROTOCOL] &&
1021
               memcmp(packet+UDP_DEST_PORT, socket->headerRcv+UDP_DEST_PORT, 2) == 0)
1022
            {
1023 209 rhoads
               EthernetCreateResponse(socket->headerSend, packet, UDP_DATA);
1024 155 rhoads
               break;
1025
            }
1026
         }
1027
      }
1028
 
1029
      if(socket)
1030
      {
1031
         if(IPVerbose)
1032
            printf("U");
1033
         FrameInsert(&socket->frameReadHead, &socket->frameReadTail, frameIn);
1034 225 rhoads
         OS_Job(socket->funcPtr, socket, 0, 0);
1035 155 rhoads
         return 1;
1036
      }
1037
   }
1038
   return 0;
1039
}
1040
 
1041
 
1042 220 rhoads
#ifndef WIN32
1043 209 rhoads
static void IPMainThread(void *arg)
1044 155 rhoads
{
1045
   uint32 message[4];
1046
   int rc;
1047
   IPFrame *frame, *frameOut=NULL;
1048
   uint32 ticks, ticksLast;
1049 209 rhoads
   (void)arg;
1050 155 rhoads
 
1051
   ticksLast = OS_ThreadTime();
1052
   memset(message, 0, sizeof(message));
1053
 
1054
   for(;;)
1055
   {
1056
      Led(0);
1057
      rc = OS_MQueueGet(IPMQueue, message, 10);
1058
      if(rc == 0)
1059
      {
1060
         frame = (IPFrame*)message[1];
1061
         if(message[0] == 0)       //frame received
1062
         {
1063
            Led(1);
1064
            frame->length = (uint16)message[2];
1065 247 rhoads
            rc = IPProcessEthernetPacket(frame, frame->length);
1066 155 rhoads
            if(rc == 0)
1067
               FrameFree(frame);
1068
         }
1069
         else if(message[0] == 1)  //frame sent
1070
         {
1071
            Led(2);
1072
            assert(frame == frameOut);
1073
            IPFrameReschedule(frame);
1074
            frameOut = NULL;
1075
         }
1076
         else if(message[0] == 2)  //frame ready to send
1077
         {
1078
         }
1079
      }
1080
 
1081
      if(frameOut == NULL)
1082
      {
1083 157 rhoads
         OS_MutexPend(IPMutex);
1084 155 rhoads
         frameOut = FrameSendTail;
1085
         if(frameOut)
1086
            FrameRemove(&FrameSendHead, &FrameSendTail, frameOut);
1087 157 rhoads
         OS_MutexPost(IPMutex);
1088 155 rhoads
         if(frameOut)
1089
         {
1090
            Led(4);
1091
            UartPacketSend(frameOut->packet, frameOut->length);
1092
         }
1093
      }
1094
 
1095
      ticks = OS_ThreadTime();
1096
      if(ticks - ticksLast > 100)
1097
      {
1098
         IPTick();
1099
         ticksLast = ticks;
1100
      }
1101
   }
1102
}
1103 220 rhoads
#endif
1104 155 rhoads
 
1105
 
1106 157 rhoads
uint8 *MyPacketGet(void)
1107 155 rhoads
{
1108 160 rhoads
   return (uint8*)IPFrameGet(FRAME_COUNT_RCV);
1109 155 rhoads
}
1110
 
1111
 
1112
//Set FrameSendFunction only if single threaded
1113 296 rhoads
void IPInit(IPFuncPtr frameSendFunction, uint8 macAddress[6], char name[6])
1114 155 rhoads
{
1115
   int i;
1116
   IPFrame *frame;
1117
 
1118 296 rhoads
   if(macAddress)
1119
      memcpy(ethernetAddressPlasma, macAddress, 6);
1120
   if(name)
1121
      memcpy(dhcpOptions+18, name, 6);
1122 209 rhoads
   FrameSendFunc = frameSendFunction;
1123 157 rhoads
   IPMutex = OS_MutexCreate("IPSem");
1124 155 rhoads
   IPMQueue = OS_MQueueCreate("IPMQ", FRAME_COUNT*2, 32);
1125
   for(i = 0; i < FRAME_COUNT; ++i)
1126
   {
1127
      frame = (IPFrame*)malloc(sizeof(IPFrame));
1128
      memset(frame, 0, sizeof(IPFrame));
1129
      frame->next = FrameFreeHead;
1130
      frame->prev = NULL;
1131
      FrameFreeHead = frame;
1132
   }
1133 296 rhoads
   FrameFreeCount = FRAME_COUNT;
1134 302 rhoads
#ifndef WIN32
1135 155 rhoads
   UartPacketConfig(MyPacketGet, PACKET_SIZE, IPMQueue);
1136 209 rhoads
   if(frameSendFunction == NULL)
1137 157 rhoads
      IPThread = OS_ThreadCreate("TCP/IP", IPMainThread, NULL, 240, 6000);
1138 302 rhoads
#endif
1139 155 rhoads
   IPDhcp(NULL, 360, 1);        //Send DHCP request
1140
}
1141
 
1142
 
1143 209 rhoads
//To open a socket for listen set ipAddress to 0
1144
IPSocket *IPOpen(IPMode_e mode, uint32 ipAddress, uint32 port, IPFuncPtr funcPtr)
1145 155 rhoads
{
1146
   IPSocket *socket;
1147 160 rhoads
   uint8 *ptrSend, *ptrRcv;
1148
   IPFrame *frame;
1149
   static int portSource=0x1007;
1150 155 rhoads
 
1151
   socket = (IPSocket*)malloc(sizeof(IPSocket));
1152
   if(socket == NULL)
1153
      return socket;
1154
   memset(socket, 0, sizeof(IPSocket));
1155
   socket->prev = NULL;
1156
   socket->state = IP_LISTEN;
1157
   socket->timeout = 0;
1158 231 rhoads
   socket->timeoutReset = SOCKET_TIMEOUT;
1159 155 rhoads
   socket->frameReadHead = NULL;
1160
   socket->frameReadTail = NULL;
1161
   socket->readOffset = 0;
1162
   socket->funcPtr = funcPtr;
1163
   socket->userData = 0;
1164
   socket->userFunc = NULL;
1165
   socket->userPtr = NULL;
1166 317 rhoads
   socket->seqWindow = 2048;
1167 160 rhoads
   ptrSend = socket->headerSend;
1168
   ptrRcv = socket->headerRcv;
1169 155 rhoads
 
1170 209 rhoads
   if(ipAddress == 0)
1171 155 rhoads
   {
1172
      //Setup listing port
1173 209 rhoads
      socket->headerRcv[TCP_DEST_PORT] = (uint8)(port >> 8);
1174
      socket->headerRcv[TCP_DEST_PORT+1] = (uint8)port;
1175 155 rhoads
   }
1176
   else
1177
   {
1178 160 rhoads
      //Setup sending packet
1179
      memset(ptrSend, 0, UDP_LENGTH);
1180
      memset(ptrRcv, 0, UDP_LENGTH);
1181 155 rhoads
 
1182 160 rhoads
      //Setup Ethernet
1183 209 rhoads
      if(ipAddress != IPAddressSelf())
1184
         memcpy(ptrSend+ETHERNET_DEST, ethernetAddressGateway, 6);
1185
      else
1186
         memcpy(ptrSend+ETHERNET_DEST, ethernetAddressPlasma, 6);
1187 160 rhoads
      memcpy(ptrSend+ETHERNET_SOURCE, ethernetAddressPlasma, 6);
1188
      ptrSend[ETHERNET_FRAME_TYPE] = 0x08;
1189
 
1190
      //Setup IP
1191
      ptrSend[IP_VERSION_LENGTH] = 0x45;
1192
      ptrSend[IP_TIME_TO_LIVE] = 0x80;
1193
 
1194
      //Setup IP addresses
1195
      memcpy(ptrSend+IP_SOURCE, ipAddressPlasma, 4);
1196 209 rhoads
      ptrSend[IP_DEST] = (uint8)(ipAddress >> 24);
1197
      ptrSend[IP_DEST+1] = (uint8)(ipAddress >> 16);
1198
      ptrSend[IP_DEST+2] = (uint8)(ipAddress >> 8);
1199
      ptrSend[IP_DEST+3] = (uint8)ipAddress;
1200
      ptrRcv[IP_SOURCE] = (uint8)(ipAddress >> 24);
1201
      ptrRcv[IP_SOURCE+1] = (uint8)(ipAddress >> 16);
1202
      ptrRcv[IP_SOURCE+2] = (uint8)(ipAddress >> 8);
1203
      ptrRcv[IP_SOURCE+3] = (uint8)ipAddress;
1204 160 rhoads
      memcpy(ptrRcv+IP_DEST, ipAddressPlasma, 4);
1205
 
1206
      //Setup ports
1207
      ptrSend[TCP_SOURCE_PORT] = (uint8)(portSource >> 8);
1208
      ptrSend[TCP_SOURCE_PORT+1] = (uint8)portSource;
1209 209 rhoads
      ptrSend[TCP_DEST_PORT] = (uint8)(port >> 8);
1210
      ptrSend[TCP_DEST_PORT+1] = (uint8)port;
1211
      ptrRcv[TCP_SOURCE_PORT] = (uint8)(port >> 8);
1212
      ptrRcv[TCP_SOURCE_PORT+1] = (uint8)port;
1213 160 rhoads
      ptrRcv[TCP_DEST_PORT] = (uint8)(portSource >> 8);
1214
      ptrRcv[TCP_DEST_PORT+1] = (uint8)portSource;
1215 155 rhoads
      ++portSource;
1216
   }
1217
 
1218 209 rhoads
   if(mode == IP_MODE_TCP)
1219 155 rhoads
   {
1220 209 rhoads
      if(ipAddress)
1221 160 rhoads
         socket->state = IP_TCP;
1222
      else
1223
         socket->state = IP_LISTEN;
1224
      ptrSend[IP_PROTOCOL] = 0x06;  //TCP
1225
      ptrRcv[IP_PROTOCOL] = 0x06;
1226 155 rhoads
   }
1227 209 rhoads
   else if(mode == IP_MODE_UDP)
1228 155 rhoads
   {
1229
      socket->state = IP_UDP;
1230 160 rhoads
      ptrSend[IP_PROTOCOL] = 0x11;  //UDP
1231
      ptrRcv[IP_PROTOCOL] = 0x11;
1232 155 rhoads
   }
1233 209 rhoads
   else if(mode == IP_MODE_PING)
1234
   {
1235
      socket->state = IP_PING;
1236
      ptrSend[IP_PROTOCOL] = 0x01;  //PING
1237
      memset(ptrSend+PING_TYPE, 0, 8);
1238
      ptrSend[PING_TYPE] = 8;       //SEND
1239
   }
1240 155 rhoads
 
1241 160 rhoads
   //Add socket to linked list
1242 157 rhoads
   OS_MutexPend(IPMutex);
1243 155 rhoads
   socket->next = SocketHead;
1244 160 rhoads
   socket->prev = NULL;
1245
   if(SocketHead)
1246
      SocketHead->prev = socket;
1247 155 rhoads
   SocketHead = socket;
1248 157 rhoads
   OS_MutexPost(IPMutex);
1249 160 rhoads
 
1250 209 rhoads
   if(mode == IP_MODE_TCP && ipAddress)
1251 160 rhoads
   {
1252
      //Send TCP SYN
1253 206 rhoads
      socket->seq = 0x01234567;
1254 160 rhoads
      frame = IPFrameGet(0);
1255
      if(frame)
1256
      {
1257
         frame->packet[TCP_FLAGS] = TCP_FLAGS_SYN;
1258
         frame->packet[TCP_DATA] = 2;    //maximum segment size = 536
1259
         frame->packet[TCP_DATA+1] = 4;
1260
         frame->packet[TCP_DATA+2] = 2;
1261
         frame->packet[TCP_DATA+3] = 24;
1262
         TCPSendPacket(socket, frame, TCP_DATA+4);
1263
         ++socket->seq;
1264
      }
1265
   }
1266 155 rhoads
   return socket;
1267
}
1268
 
1269
 
1270 209 rhoads
void IPWriteFlush(IPSocket *socket)
1271 155 rhoads
{
1272
   uint8 *packetOut;
1273 209 rhoads
   if(socket->frameSend && socket->state != IP_UDP &&
1274
      socket->state != IP_PING)
1275 155 rhoads
   {
1276 209 rhoads
      packetOut = socket->frameSend->packet;
1277 220 rhoads
      packetOut[TCP_FLAGS] = TCP_FLAGS_ACK | TCP_FLAGS_PSH;
1278 209 rhoads
      TCPSendPacket(socket, socket->frameSend, TCP_DATA + socket->sendOffset);
1279
      socket->seq += socket->sendOffset;
1280
      socket->frameSend = NULL;
1281
      socket->sendOffset = 0;
1282 155 rhoads
   }
1283
}
1284
 
1285
 
1286 209 rhoads
uint32 IPWrite(IPSocket *socket, const uint8 *buf, uint32 length)
1287 155 rhoads
{
1288
   IPFrame *frameOut;
1289
   uint8 *packetOut;
1290 206 rhoads
   uint32 bytes, count=0, tries=0;
1291 155 rhoads
   int offset;
1292 165 rhoads
   OS_Thread_t *self;
1293 155 rhoads
 
1294 231 rhoads
   if(socket->timeout)
1295
      socket->timeout = socket->timeoutReset;
1296
 
1297 155 rhoads
   //printf("IPWrite(0x%x, %d)", Socket, Length);
1298 165 rhoads
   self = OS_ThreadSelf();
1299 209 rhoads
   while(length)
1300 155 rhoads
   {
1301 206 rhoads
      //Rate limit output
1302 317 rhoads
      if(socket->seq - socket->seqReceived >= SEND_WINDOW)
1303 206 rhoads
      {
1304 302 rhoads
         //printf("l(%d,%d,%d) ", socket->seq - socket->seqReceived, socket->seq, socket->seqReceived);
1305 341 rhoads
         if(self != IPThread && ++tries < 200)
1306 206 rhoads
         {
1307
            OS_ThreadSleep(1);
1308
            continue;
1309
         }
1310
      }
1311 165 rhoads
      tries = 0;
1312 209 rhoads
      while(socket->frameSend == NULL)
1313 155 rhoads
      {
1314 209 rhoads
         socket->frameSend = IPFrameGet(FRAME_COUNT_SEND);
1315
         socket->sendOffset = 0;
1316
         if(socket->frameSend == NULL)
1317 165 rhoads
         {
1318 302 rhoads
            //printf("L");
1319 165 rhoads
            if(self == IPThread || ++tries > 200)
1320
               break;
1321
            else
1322
               OS_ThreadSleep(1);
1323
         }
1324 155 rhoads
      }
1325 209 rhoads
      frameOut = socket->frameSend;
1326
      offset = socket->sendOffset;
1327 155 rhoads
      if(frameOut == NULL)
1328
         break;
1329
      packetOut = frameOut->packet;
1330
 
1331 209 rhoads
      if(socket->state == IP_PING)
1332 155 rhoads
      {
1333 209 rhoads
         bytes = length;
1334
         memcpy(packetOut, socket->headerSend, PING_DATA);
1335
         memcpy(packetOut+PING_DATA, buf, bytes);
1336
         IPSendPacket(socket, socket->frameSend, PING_DATA + bytes);
1337
         socket->frameSend = NULL;
1338
      }
1339
      else if(socket->state != IP_UDP)
1340
      {
1341
         bytes = 512 - offset;
1342
         if(bytes > length)
1343
            bytes = length;
1344
         socket->sendOffset += bytes;
1345
         memcpy(packetOut+TCP_DATA+offset, buf, bytes);
1346
         if(socket->sendOffset >= 512)
1347
            IPWriteFlush(socket);
1348 165 rhoads
         //if(Socket->seq - Socket->seqReceived > Socket->seqWindow)
1349
         //{
1350
         //   printf("W");
1351
         //   OS_ThreadSleep(10);
1352
         //}
1353 155 rhoads
      }
1354
      else  //UDP
1355
      {
1356 209 rhoads
         bytes = length;
1357
         memcpy(packetOut+UDP_DATA+offset, buf, bytes);
1358
         memcpy(packetOut, socket->headerSend, UDP_LENGTH);
1359
         IPSendPacket(socket, socket->frameSend, UDP_DATA + bytes);
1360
         socket->frameSend = NULL;
1361 155 rhoads
      }
1362
      count += bytes;
1363 209 rhoads
      buf += bytes;
1364
      length -= bytes;
1365 155 rhoads
   }
1366
   return count;
1367
}
1368
 
1369
 
1370 209 rhoads
uint32 IPRead(IPSocket *socket, uint8 *buf, uint32 length)
1371 155 rhoads
{
1372
   IPFrame *frame, *frame2;
1373
   int count=0, bytes, offset;
1374
 
1375 225 rhoads
   if(socket->state == IP_UDP)
1376
      offset = UDP_DATA;
1377
   else
1378 155 rhoads
      offset = TCP_DATA;
1379
 
1380 157 rhoads
   OS_MutexPend(IPMutex);
1381 209 rhoads
   for(frame = socket->frameReadTail; length && frame; )
1382 155 rhoads
   {
1383 209 rhoads
      bytes = frame->length - offset - socket->readOffset;
1384
      if(bytes > (int)length)
1385
         bytes = length;
1386
      memcpy(buf, frame->packet + offset + socket->readOffset, bytes);
1387
      buf += bytes;
1388
      socket->readOffset += bytes;
1389
      length -= bytes;
1390 155 rhoads
      count += bytes;
1391
 
1392
      //Check if done with packet
1393
      frame2 = frame;
1394
      frame = frame->prev;
1395 209 rhoads
      if(socket->readOffset == frame2->length - offset)
1396 155 rhoads
      {
1397
         //Remove packet from socket linked list
1398 209 rhoads
         socket->readOffset = 0;
1399
         FrameRemove(&socket->frameReadHead, &socket->frameReadTail, frame2);
1400 317 rhoads
         socket->ackProcessed += frame2->length - offset;
1401
         if(socket->state == IP_TCP &&
1402
            socket->ack - socket->ackProcessed > RECEIVE_WINDOW - 2048)
1403
         {
1404
            //Update receive window for flow control
1405
            frame2->packet[TCP_FLAGS] = TCP_FLAGS_ACK;
1406
            TCPSendPacket(socket, frame2, TCP_DATA);
1407
         }
1408
         else
1409
            FrameFree(frame2);
1410 155 rhoads
      }
1411
   }
1412 157 rhoads
   OS_MutexPost(IPMutex);
1413 155 rhoads
   return count;
1414
}
1415
 
1416
 
1417 209 rhoads
static void IPClose2(IPSocket *socket)
1418 155 rhoads
{
1419
   IPFrame *frame, *framePrev;
1420
 
1421 157 rhoads
   OS_MutexPend(IPMutex);
1422 155 rhoads
 
1423 157 rhoads
   //Mark packets as don't retransmit
1424
   for(frame = FrameSendHead; frame; frame = frame->next)
1425 155 rhoads
   {
1426 209 rhoads
      if(frame->socket == socket)
1427 157 rhoads
         frame->socket = NULL;
1428 155 rhoads
   }
1429
 
1430
   //Remove packets from retransmision list
1431
   for(frame = FrameResendHead; frame; )
1432
   {
1433
      framePrev = frame;
1434
      frame = frame->next;
1435 209 rhoads
      if(framePrev->socket == socket)
1436 155 rhoads
      {
1437
         FrameRemove(&FrameResendHead, &FrameResendTail, framePrev);
1438
         FrameFree(framePrev);
1439
      }
1440
   }
1441
 
1442
   //Remove packets from socket read linked list
1443 209 rhoads
   for(frame = socket->frameReadHead; frame; )
1444 155 rhoads
   {
1445
      framePrev = frame;
1446
      frame = frame->next;
1447 209 rhoads
      FrameRemove(&socket->frameReadHead, &socket->frameReadTail, framePrev);
1448 155 rhoads
      FrameFree(framePrev);
1449
   }
1450
 
1451
   //Remove socket
1452 317 rhoads
   if(socket->state == IP_CLOSED || socket->state <= IP_UDP)
1453 225 rhoads
   {
1454
      if(socket->prev == NULL)
1455
         SocketHead = socket->next;
1456
      else
1457
         socket->prev->next = socket->next;
1458
      if(socket->next)
1459
         socket->next->prev = socket->prev;
1460
      free(socket);
1461
   }
1462 155 rhoads
   else
1463 225 rhoads
   {
1464
      //Give application 10 seconds to stop using socket
1465 317 rhoads
      if(socket->state > IP_UDP)
1466
         socket->state = IP_CLOSED;
1467 225 rhoads
      socket->timeout = 10;
1468
   }
1469 157 rhoads
   OS_MutexPost(IPMutex);
1470 155 rhoads
}
1471
 
1472
 
1473 209 rhoads
void IPClose(IPSocket *socket)
1474 155 rhoads
{
1475
   IPFrame *frameOut;
1476
 
1477 209 rhoads
   IPWriteFlush(socket);
1478
   if(socket->state <= IP_UDP)
1479 157 rhoads
   {
1480 209 rhoads
      IPClose2(socket);
1481 157 rhoads
      return;
1482
   }
1483
   frameOut = IPFrameGet(0);
1484 231 rhoads
   if(frameOut == NULL)
1485 155 rhoads
      return;
1486
   frameOut->packet[TCP_FLAGS] = TCP_FLAGS_FIN | TCP_FLAGS_ACK;
1487 209 rhoads
   TCPSendPacket(socket, frameOut, TCP_DATA);
1488
   ++socket->seq;
1489
   if(socket->state == IP_FIN_CLIENT)
1490
      IPClose2(socket);
1491 157 rhoads
   else
1492 209 rhoads
      socket->state = IP_FIN_SERVER;
1493 155 rhoads
}
1494
 
1495
 
1496 317 rhoads
void IPPrintf(IPSocket *socket, char *message,
1497
              int arg0, int arg1, int arg2, int arg3)
1498 209 rhoads
{
1499 327 rhoads
   char buf[500];
1500 317 rhoads
   if(socket == NULL)
1501
   {
1502
      printf(message, arg0, arg1, arg2, arg3);
1503
      return;
1504
   }
1505 327 rhoads
   if(strcmp(message, "%s") == 0)
1506
      IPWrite(socket, (uint8*)arg0, (int)strlen((char*)arg0));
1507 317 rhoads
   else
1508
   {
1509
      sprintf(buf, message, arg0, arg1, arg2, arg3, 0, 0, 0, 0);
1510
      IPWrite(socket, (uint8*)buf, (int)strlen(buf));
1511
   }
1512 327 rhoads
   if(socket->dontFlush == 0 || strstr(message, "\n"))
1513 317 rhoads
      IPWriteFlush(socket);
1514 209 rhoads
}
1515 155 rhoads
 
1516
 
1517
void IPTick(void)
1518
{
1519
   IPFrame *frame, *frame2;
1520
   IPSocket *socket, *socket2;
1521 296 rhoads
   unsigned long ticks;
1522
   static unsigned long ticksPrev=0, ticksPrev2=0;
1523 155 rhoads
 
1524 296 rhoads
   ticks = OS_ThreadTime();
1525 326 rhoads
#ifdef WIN32
1526
   ticks = ticksPrev + 100;
1527
#endif
1528 296 rhoads
   if(ticks - ticksPrev >= 95)
1529 157 rhoads
   {
1530 296 rhoads
      if(IPVerbose && (Seconds % 60) == 0)
1531
      {
1532
         if(FrameFreeCount >= FRAME_COUNT-1)
1533
            printf("T");
1534
         else
1535
            printf("T(%d)", FrameFreeCount);
1536
      }
1537
      ++Seconds;
1538
      if(--DhcpRetrySeconds <= 0)
1539
         IPDhcp(NULL, 400, 1);   //DHCP request
1540 157 rhoads
   }
1541 155 rhoads
 
1542 157 rhoads
   OS_MutexPend(IPMutex);
1543 155 rhoads
 
1544
   //Retransmit timeout packets
1545
   for(frame = FrameResendHead; frame; )
1546
   {
1547
      frame2 = frame;
1548
      frame = frame->next;
1549 302 rhoads
      frame2->timeout = (short)(frame2->timeout - (ticks - ticksPrev2));
1550 296 rhoads
      if(--frame2->timeout <= 0)
1551 155 rhoads
      {
1552
         if(IPVerbose)
1553
            printf("r");
1554
         FrameRemove(&FrameResendHead, &FrameResendTail, frame2);
1555
         IPSendFrame(frame2);
1556
      }
1557
   }
1558
 
1559 296 rhoads
   if(ticks - ticksPrev >= 95)
1560 155 rhoads
   {
1561 296 rhoads
      //Close timed out sockets
1562
      for(socket = SocketHead; socket; )
1563 155 rhoads
      {
1564 296 rhoads
         socket2 = socket;
1565
         socket = socket->next;
1566
         if(socket2->timeout && --socket2->timeout == 0)
1567
         {
1568
            socket2->timeout = 10;
1569
            if(IPVerbose && socket2->state != IP_CLOSED &&
1570
                            socket2->state != IP_FIN_SERVER)
1571
               printf("t(%d,%d)", socket2->state, FrameFreeCount);
1572
            if(socket2->state == IP_TCP)
1573
               IPClose(socket2);
1574
            else if(socket2->state == IP_FIN_CLIENT)
1575
               IPClose(socket2);
1576
            else
1577
               IPClose2(socket2);
1578
         }
1579 155 rhoads
      }
1580 296 rhoads
      ticksPrev = ticks;
1581 155 rhoads
   }
1582 157 rhoads
   OS_MutexPost(IPMutex);
1583 296 rhoads
   ticksPrev2 = ticks;
1584 155 rhoads
}
1585
 
1586
 
1587
static void DnsCallback(IPSocket *socket)
1588
{
1589
   uint8 buf[200], *ptr;
1590
   uint32 ipAddress;
1591 209 rhoads
   int bytes;
1592 155 rhoads
 
1593
   memset(buf, 0, sizeof(buf));
1594 209 rhoads
   bytes = IPRead(socket, buf, sizeof(buf));
1595 155 rhoads
   if(buf[DNS_NUM_ANSWERS_RR+1])
1596
   {
1597 209 rhoads
      for(ptr = buf + DNS_QUESTIONS; ptr + 14 <= buf + bytes; ++ptr)
1598 155 rhoads
      {
1599 211 rhoads
         if(ptr[0] == 0 && ptr[1] == 1 && ptr[2] == 0 && ptr[3] == 1 &&
1600
            ptr[8] == 0 && ptr[9] == 4)
1601 155 rhoads
         {
1602 211 rhoads
            ipAddress = (ptr[10] << 24) | (ptr[11] << 16) | (ptr[12] << 8) | ptr[13];
1603
            printf("ipAddress = %d.%d.%d.%d\n", ptr[10], ptr[11], ptr[12], ptr[13]);
1604 155 rhoads
            socket->userData = ipAddress;
1605
            if(socket->userFunc)
1606
            {
1607 209 rhoads
               socket->userFunc(socket, ipAddress, socket->userPtr);
1608 155 rhoads
            }
1609
            break;
1610
         }
1611
      }
1612
   }
1613 220 rhoads
   IPClose(socket);
1614 155 rhoads
}
1615
 
1616
 
1617 220 rhoads
void IPResolve(char *name, IPFuncPtr resolvedFunc, void *arg)
1618 155 rhoads
{
1619
   uint8 buf[200], *ptr;
1620
   int length, i;
1621
   IPSocket *socket;
1622
 
1623
   socket = IPOpen(IP_MODE_UDP, ipAddressDns, DNS_PORT, DnsCallback);
1624
   memset(buf, 0, sizeof(buf));
1625
   buf[DNS_ID+1] = 1;
1626
   buf[DNS_FLAGS] = 1;
1627
   buf[DNS_NUM_QUESTIONS+1] = 1;
1628
 
1629
   //Setup name
1630
   ptr = buf + DNS_QUESTIONS;
1631 209 rhoads
   strncpy((char*)ptr+1, name, 100);
1632 155 rhoads
   ptr[0] = 1;
1633
   while(ptr[0])
1634
   {
1635
      for(i = 0; i < 100; ++i)
1636
      {
1637
         if(ptr[i+1] == '.' || ptr[i+1] == 0)
1638
         {
1639
            ptr[0] = (uint8)i;
1640
            ptr += i+1;
1641
            break;
1642
         }
1643
      }
1644
   }
1645
   ++ptr;
1646
   ptr[1] = DNS_QUERY_TYPE_IP;
1647
   ptr[3] = DNS_QUERY_CLASS;
1648
   length = (int)(ptr - buf) + 4;
1649
   if(length < 60)
1650
      length = 60;
1651
 
1652
   socket->userFunc = (IPFuncPtr)resolvedFunc;
1653 209 rhoads
   socket->userPtr = arg;
1654 155 rhoads
   socket->userData = 0;
1655
   IPWrite(socket, buf, length);
1656
}
1657
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.