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

Subversion Repositories plasma

[/] [plasma/] [trunk/] [kernel/] [tcpip.c] - Blame information for rev 416

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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