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

Subversion Repositories plasma

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

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
#define IPPRINTF
24
#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
   int length, ip_length, bytes, rc=0, notify=0;
665
   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
      //Ack data
877
      frameOut = IPFrameGet(FRAME_COUNT_SEND);
878
      if(frameOut)
879
      {
880
         frameOut->packet[TCP_FLAGS] = TCP_FLAGS_ACK;
881
         TCPSendPacket(socket, frameOut, TCP_DATA);
882
      }
883
 
884
      //Using frame
885
      rc = 1;
886
   }
887
   else if(bytes)
888
   {
889
      //Ack with current offset since data missing
890
      frameOut = IPFrameGet(FRAME_COUNT_SEND);
891
      if(frameOut)
892
      {
893
         frameOut->packet[TCP_FLAGS] = TCP_FLAGS_ACK;
894
         TCPSendPacket(socket, frameOut, TCP_DATA);
895
      }
896
   }
897
 
898
   //Check if FIN flag set
899
   if(packet[TCP_FLAGS] & TCP_FLAGS_FIN && socket->ack >= seq)
900
   {
901
      notify = 1;
902
      socket->timeout = SOCKET_TIMEOUT;
903
      if(IPVerbose)
904
         printf("F");
905
      frameOut = IPFrameGet(0);
906
      if(frameOut == NULL)
907
         return 0;
908
      packetOut = frameOut->packet;
909
      packetOut[TCP_FLAGS] = TCP_FLAGS_ACK;
910
      ++socket->ack;
911
      TCPSendPacket(socket, frameOut, TCP_DATA);
912
      if(socket->state == IP_FIN_SERVER)
913 382 rhoads
         socket->timeout = SOCKET_TIMEOUT;
914 353 rhoads
      else if(socket->state == IP_TCP)
915
         socket->state = IP_FIN_CLIENT;
916
   }
917
 
918
   //Notify application
919
   if(socket->funcPtr && notify)
920 400 rhoads
      OS_Job((JobFunc_t)socket->funcPtr, socket, 0, 0);
921 353 rhoads
   return rc;
922
}
923
 
924
 
925
int IPProcessEthernetPacket(IPFrame *frameIn, int length)
926
{
927
   int ip_length, rc;
928
   IPSocket *socket;
929
   IPFrame *frameOut;
930
   uint8 *packet, *packetOut;
931
 
932
   packet = frameIn->packet;
933
   frameIn->length = (uint16)length;
934
 
935
   if(packet[ETHERNET_FRAME_TYPE] != 0x08 || frameIn->length > PACKET_SIZE)
936
      return 0;  //wrong ethernet type, packet not used
937
 
938
   //ARP?
939
   if(packet[ETHERNET_FRAME_TYPE+1] == 0x06)
940
   {
941
      //Check if ARP reply
942
      if(memcmp(packet+ETHERNET_DEST, ethernetAddressPlasma, 6) == 0 &&
943 382 rhoads
         packet[ARP_OP+1] == 2)
944 353 rhoads
      {
945 382 rhoads
         memcpy(ArpCache[ArpCacheIndex].ip, packet+ARP_IP_SENDER, 4);
946
         memcpy(ArpCache[ArpCacheIndex].mac, packet+ARP_ETHERNET_SENDER, 6);
947
         if(++ArpCacheIndex >= sizeof(ArpCache) / sizeof(ArpCache_t))
948
            ArpCacheIndex = 0;
949
         if(memcmp(packet+ARP_IP_SENDER, ipAddressGateway, 4) == 0)
950
         {
951
            //Found MAC address for gateway
952
            memcpy(ethernetAddressGateway, packet+ARP_ETHERNET_SENDER, 6);
953
         }
954 353 rhoads
         return 0;
955
      }
956
 
957
      //Check if ARP request
958
      if(packet[ARP_OP] != 0 || packet[ARP_OP+1] != 1 ||
959
         memcmp(packet+ARP_IP_TARGET, ipAddressPlasma, 4))
960
         return 0;
961
      //Create ARP response
962
      frameOut = IPFrameGet(0);
963
      if(frameOut == NULL)
964
         return 0;
965
      packetOut = frameOut->packet;
966
      memcpy(packetOut, packet, frameIn->length);
967
      memcpy(packetOut+ETHERNET_DEST, packet+ETHERNET_SOURCE, 6);
968
      memcpy(packetOut+ETHERNET_SOURCE, ethernetAddressPlasma, 6);
969
      packetOut[ARP_OP+1] = 2; //ARP reply
970
      memcpy(packetOut+ARP_ETHERNET_SENDER, ethernetAddressPlasma, 6);
971
      memcpy(packetOut+ARP_IP_SENDER, packet+ARP_IP_TARGET, 4);
972
      memcpy(packetOut+ARP_ETHERNET_TARGET, packet+ARP_ETHERNET_SENDER, 6);
973
      memcpy(packetOut+ARP_IP_TARGET, packet+ARP_IP_SENDER, 4);
974
      IPSendPacket(NULL, frameOut, frameIn->length);
975
      return 0;
976
   }
977
 
978
   //Check if proper type of packet
979
   ip_length = (packet[IP_LENGTH] << 8) | packet[IP_LENGTH+1];
980
   if(frameIn->length < UDP_DATA || ip_length > frameIn->length - IP_VERSION_LENGTH)
981
      return 0;
982
   if(packet[ETHERNET_FRAME_TYPE+1] != 0x00 ||
983
      packet[IP_VERSION_LENGTH] != 0x45)
984
      return 0;
985
 
986
   //Check if DHCP reply
987
   if(packet[IP_PROTOCOL] == 0x11 &&
988
      packet[UDP_SOURCE_PORT] == 0 && packet[UDP_SOURCE_PORT+1] == 67 &&
989
      packet[UDP_DEST_PORT] == 0 && packet[UDP_DEST_PORT+1] == 68)
990
   {
991
      IPDhcp(packet, frameIn->length, 2);            //DHCP reply
992
      return 0;
993
   }
994
 
995
   //Check if correct destination address
996
   if(memcmp(packet+ETHERNET_DEST, ethernetAddressPlasma, 6) ||
997
      memcmp(packet+IP_DEST, ipAddressPlasma, 4))
998
      return 0;
999
   rc = EthernetVerifyChecksums(packet, frameIn->length);
1000 367 rhoads
#ifndef WIN32
1001
   if(rc && FrameSendFunc)
1002
   {
1003
      printf("C ");
1004
      return 0;
1005
   }
1006
#endif
1007 353 rhoads
 
1008
   //PING request?
1009
   if(packet[IP_PROTOCOL] == 1)
1010
   {
1011
      if(packet[PING_TYPE] == 0)  //PING reply
1012
      {
1013
         for(socket = SocketHead; socket; socket = socket->next)
1014
         {
1015
            if(socket->state == IP_PING &&
1016
               memcmp(packet+IP_SOURCE, socket->headerSend+IP_DEST, 4) == 0)
1017
            {
1018 400 rhoads
               OS_Job((JobFunc_t)socket->funcPtr, socket, 0, 0);
1019 353 rhoads
               return 0;
1020
            }
1021
         }
1022
      }
1023
      if(packet[PING_TYPE] != 8)
1024
         return 0;
1025
      frameOut = IPFrameGet(FRAME_COUNT_SEND);
1026
      if(frameOut == NULL)
1027
         return 0;
1028
      packetOut = frameOut->packet;
1029
      EthernetCreateResponse(packetOut, packet, frameIn->length);
1030
      frameOut->packet[PING_TYPE] = 0;       //PING reply
1031
      IPSendPacket(NULL, frameOut, frameIn->length);
1032
      return 0;
1033
   }
1034
 
1035
   //TCP packet?
1036
   if(packet[IP_PROTOCOL] == 0x06)
1037
   {
1038
      return IPProcessTCPPacket(frameIn);
1039
   }
1040
 
1041
   //UDP packet?
1042
   if(packet[IP_PROTOCOL] == 0x11)
1043
   {
1044
      //Find open socket
1045
      for(socket = SocketHead; socket; socket = socket->next)
1046
      {
1047
         if(packet[IP_PROTOCOL] == socket->headerRcv[IP_PROTOCOL] &&
1048
            memcmp(packet+IP_SOURCE, socket->headerRcv+IP_SOURCE, 8) == 0 &&
1049
            memcmp(packet+UDP_SOURCE_PORT, socket->headerRcv+UDP_SOURCE_PORT, 2) == 0)
1050
         {
1051
            break;
1052
         }
1053
      }
1054
 
1055
      if(socket == NULL)
1056
      {
1057
         //Find listening socket
1058
         for(socket = SocketHead; socket; socket = socket->next)
1059
         {
1060
            if(packet[IP_PROTOCOL] == socket->headerRcv[IP_PROTOCOL] &&
1061
               memcmp(packet+UDP_DEST_PORT, socket->headerRcv+UDP_DEST_PORT, 2) == 0)
1062
            {
1063
               EthernetCreateResponse(socket->headerSend, packet, UDP_DATA);
1064
               break;
1065
            }
1066
         }
1067
      }
1068
 
1069
      if(socket)
1070
      {
1071
         if(IPVerbose)
1072
            printf("U");
1073
         FrameInsert(&socket->frameReadHead, &socket->frameReadTail, frameIn);
1074 400 rhoads
         OS_Job((JobFunc_t)socket->funcPtr, socket, 0, 0);
1075 353 rhoads
         return 1;
1076
      }
1077
   }
1078
   return 0;
1079
}
1080
 
1081
 
1082
#ifndef WIN32
1083
static void IPMainThread(void *arg)
1084
{
1085
   uint32 message[4];
1086
   int rc;
1087
   IPFrame *frame, *frameOut=NULL;
1088
   uint32 ticks, ticksLast;
1089
   (void)arg;
1090
 
1091
   ticksLast = OS_ThreadTime();
1092
   memset(message, 0, sizeof(message));
1093
 
1094
   for(;;)
1095
   {
1096 368 rhoads
      Led(7, 0);
1097 353 rhoads
      rc = OS_MQueueGet(IPMQueue, message, 10);
1098
      if(rc == 0)
1099
      {
1100
         frame = (IPFrame*)message[1];
1101
         if(message[0] == 0)       //frame received
1102
         {
1103 368 rhoads
            Led(7, 1);
1104 353 rhoads
            frame->length = (uint16)message[2];
1105
            rc = IPProcessEthernetPacket(frame, frame->length);
1106
            if(rc == 0)
1107
               FrameFree(frame);
1108
         }
1109
         else if(message[0] == 1)  //frame sent
1110
         {
1111 368 rhoads
            Led(7, 2);
1112 353 rhoads
            assert(frame == frameOut);
1113
            IPFrameReschedule(frame);
1114
            frameOut = NULL;
1115
         }
1116
         else if(message[0] == 2)  //frame ready to send
1117
         {
1118
         }
1119
      }
1120
 
1121
      if(frameOut == NULL)
1122
      {
1123
         OS_MutexPend(IPMutex);
1124
         frameOut = FrameSendTail;
1125
         if(frameOut)
1126
            FrameRemove(&FrameSendHead, &FrameSendTail, frameOut);
1127
         OS_MutexPost(IPMutex);
1128
         if(frameOut)
1129
         {
1130 368 rhoads
            Led(7, 4);
1131 353 rhoads
            UartPacketSend(frameOut->packet, frameOut->length);
1132
         }
1133
      }
1134
 
1135
      ticks = OS_ThreadTime();
1136
      if(ticks - ticksLast > 100)
1137
      {
1138
         IPTick();
1139
         ticksLast = ticks;
1140
      }
1141
   }
1142
}
1143
#endif
1144
 
1145
 
1146
uint8 *MyPacketGet(void)
1147
{
1148
   return (uint8*)IPFrameGet(FRAME_COUNT_RCV);
1149
}
1150
 
1151
 
1152
//Set FrameSendFunction only if single threaded
1153 400 rhoads
void IPInit(IPSendFuncPtr frameSendFunction, uint8 macAddress[6], char name[6])
1154 353 rhoads
{
1155
   int i;
1156
   IPFrame *frame;
1157
 
1158
   if(macAddress)
1159
      memcpy(ethernetAddressPlasma, macAddress, 6);
1160
   if(name)
1161
      memcpy(dhcpOptions+18, name, 6);
1162
   FrameSendFunc = frameSendFunction;
1163
   IPMutex = OS_MutexCreate("IPSem");
1164
   IPMQueue = OS_MQueueCreate("IPMQ", FRAME_COUNT*2, 32);
1165
   for(i = 0; i < FRAME_COUNT; ++i)
1166
   {
1167
      frame = (IPFrame*)malloc(sizeof(IPFrame));
1168
      memset(frame, 0, sizeof(IPFrame));
1169
      frame->next = FrameFreeHead;
1170
      frame->prev = NULL;
1171
      FrameFreeHead = frame;
1172
   }
1173
   FrameFreeCount = FRAME_COUNT;
1174
#ifndef WIN32
1175
   UartPacketConfig(MyPacketGet, PACKET_SIZE, IPMQueue);
1176
   if(frameSendFunction == NULL)
1177
      IPThread = OS_ThreadCreate("TCP/IP", IPMainThread, NULL, 240, 6000);
1178
#endif
1179
   IPDhcp(NULL, 360, 1);        //Send DHCP request
1180
}
1181
 
1182
 
1183
//To open a socket for listen set ipAddress to 0
1184 400 rhoads
IPSocket *IPOpen(IPMode_e mode, uint32 ipAddress, uint32 port, IPSockFuncPtr funcPtr)
1185 353 rhoads
{
1186
   IPSocket *socket;
1187
   uint8 *ptrSend, *ptrRcv;
1188
   IPFrame *frame;
1189
   static int portSource=0x1007;
1190
 
1191
   socket = (IPSocket*)malloc(sizeof(IPSocket));
1192
   if(socket == NULL)
1193
      return socket;
1194
   memset(socket, 0, sizeof(IPSocket));
1195
   socket->prev = NULL;
1196
   socket->state = IP_LISTEN;
1197
   socket->timeout = 0;
1198
   socket->timeoutReset = SOCKET_TIMEOUT;
1199
   socket->frameReadHead = NULL;
1200
   socket->frameReadTail = NULL;
1201
   socket->readOffset = 0;
1202
   socket->funcPtr = funcPtr;
1203
   socket->userData = 0;
1204
   socket->userFunc = NULL;
1205
   socket->userPtr = NULL;
1206
   socket->seqWindow = 2048;
1207
   ptrSend = socket->headerSend;
1208
   ptrRcv = socket->headerRcv;
1209
 
1210
   if(ipAddress == 0)
1211
   {
1212
      //Setup listing port
1213
      socket->headerRcv[TCP_DEST_PORT] = (uint8)(port >> 8);
1214
      socket->headerRcv[TCP_DEST_PORT+1] = (uint8)port;
1215
   }
1216
   else
1217
   {
1218
      //Setup sending packet
1219
      memset(ptrSend, 0, UDP_LENGTH);
1220
      memset(ptrRcv, 0, UDP_LENGTH);
1221
 
1222
      //Setup Ethernet
1223
      if(ipAddress != IPAddressSelf())
1224
         memcpy(ptrSend+ETHERNET_DEST, ethernetAddressGateway, 6);
1225
      else
1226
         memcpy(ptrSend+ETHERNET_DEST, ethernetAddressPlasma, 6);
1227
      memcpy(ptrSend+ETHERNET_SOURCE, ethernetAddressPlasma, 6);
1228
      ptrSend[ETHERNET_FRAME_TYPE] = 0x08;
1229
 
1230
      //Setup IP
1231
      ptrSend[IP_VERSION_LENGTH] = 0x45;
1232
      ptrSend[IP_TIME_TO_LIVE] = 0x80;
1233
 
1234
      //Setup IP addresses
1235
      memcpy(ptrSend+IP_SOURCE, ipAddressPlasma, 4);
1236
      ptrSend[IP_DEST] = (uint8)(ipAddress >> 24);
1237
      ptrSend[IP_DEST+1] = (uint8)(ipAddress >> 16);
1238
      ptrSend[IP_DEST+2] = (uint8)(ipAddress >> 8);
1239
      ptrSend[IP_DEST+3] = (uint8)ipAddress;
1240
      ptrRcv[IP_SOURCE] = (uint8)(ipAddress >> 24);
1241
      ptrRcv[IP_SOURCE+1] = (uint8)(ipAddress >> 16);
1242
      ptrRcv[IP_SOURCE+2] = (uint8)(ipAddress >> 8);
1243
      ptrRcv[IP_SOURCE+3] = (uint8)ipAddress;
1244
      memcpy(ptrRcv+IP_DEST, ipAddressPlasma, 4);
1245
 
1246
      //Setup ports
1247
      ptrSend[TCP_SOURCE_PORT] = (uint8)(portSource >> 8);
1248
      ptrSend[TCP_SOURCE_PORT+1] = (uint8)portSource;
1249
      ptrSend[TCP_DEST_PORT] = (uint8)(port >> 8);
1250
      ptrSend[TCP_DEST_PORT+1] = (uint8)port;
1251
      ptrRcv[TCP_SOURCE_PORT] = (uint8)(port >> 8);
1252
      ptrRcv[TCP_SOURCE_PORT+1] = (uint8)port;
1253
      ptrRcv[TCP_DEST_PORT] = (uint8)(portSource >> 8);
1254
      ptrRcv[TCP_DEST_PORT+1] = (uint8)portSource;
1255
      ++portSource;
1256
   }
1257
 
1258
   if(mode == IP_MODE_TCP)
1259
   {
1260
      if(ipAddress)
1261
         socket->state = IP_TCP;
1262
      else
1263
         socket->state = IP_LISTEN;
1264
      ptrSend[IP_PROTOCOL] = 0x06;  //TCP
1265
      ptrRcv[IP_PROTOCOL] = 0x06;
1266
   }
1267
   else if(mode == IP_MODE_UDP)
1268
   {
1269
      socket->state = IP_UDP;
1270
      ptrSend[IP_PROTOCOL] = 0x11;  //UDP
1271
      ptrRcv[IP_PROTOCOL] = 0x11;
1272
   }
1273
   else if(mode == IP_MODE_PING)
1274
   {
1275
      socket->state = IP_PING;
1276
      ptrSend[IP_PROTOCOL] = 0x01;  //PING
1277
      memset(ptrSend+PING_TYPE, 0, 8);
1278
      ptrSend[PING_TYPE] = 8;       //SEND
1279
   }
1280
 
1281
   //Add socket to linked list
1282
   OS_MutexPend(IPMutex);
1283
   socket->next = SocketHead;
1284
   socket->prev = NULL;
1285
   if(SocketHead)
1286
      SocketHead->prev = socket;
1287
   SocketHead = socket;
1288
   OS_MutexPost(IPMutex);
1289
 
1290
   if(mode == IP_MODE_TCP && ipAddress)
1291
   {
1292
      //Send TCP SYN
1293
      socket->seq = 0x01234567;
1294
      frame = IPFrameGet(0);
1295
      if(frame)
1296
      {
1297
         frame->packet[TCP_FLAGS] = TCP_FLAGS_SYN;
1298
         frame->packet[TCP_DATA] = 2;    //maximum segment size = 536
1299
         frame->packet[TCP_DATA+1] = 4;
1300
         frame->packet[TCP_DATA+2] = 2;
1301
         frame->packet[TCP_DATA+3] = 24;
1302
         TCPSendPacket(socket, frame, TCP_DATA+4);
1303
         ++socket->seq;
1304
      }
1305
   }
1306
   return socket;
1307
}
1308
 
1309
 
1310
void IPWriteFlush(IPSocket *socket)
1311
{
1312
   uint8 *packetOut;
1313
   if(socket->frameSend && socket->state != IP_UDP &&
1314
      socket->state != IP_PING)
1315
   {
1316
      packetOut = socket->frameSend->packet;
1317
      packetOut[TCP_FLAGS] = TCP_FLAGS_ACK | TCP_FLAGS_PSH;
1318
      TCPSendPacket(socket, socket->frameSend, TCP_DATA + socket->sendOffset);
1319
      socket->seq += socket->sendOffset;
1320
      socket->frameSend = NULL;
1321
      socket->sendOffset = 0;
1322
   }
1323
}
1324
 
1325
 
1326
uint32 IPWrite(IPSocket *socket, const uint8 *buf, uint32 length)
1327
{
1328
   IPFrame *frameOut;
1329
   uint8 *packetOut;
1330
   uint32 bytes, count=0, tries=0;
1331
   int offset;
1332
   OS_Thread_t *self;
1333
 
1334 372 rhoads
   if(socket->state > IP_TCP)
1335
      return 0;
1336
 
1337 353 rhoads
   if(socket->timeout)
1338
      socket->timeout = socket->timeoutReset;
1339
 
1340 361 rhoads
#ifdef INCLUDE_FILESYS
1341
   if(socket->fileOut)   //override stdout
1342
      return fwrite((char*)buf, 1, length, socket->fileOut);
1343
#endif
1344
 
1345 353 rhoads
   //printf("IPWrite(0x%x, %d)", Socket, Length);
1346
   self = OS_ThreadSelf();
1347
   while(length)
1348
   {
1349
      //Rate limit output
1350
      if(socket->seq - socket->seqReceived >= SEND_WINDOW)
1351
      {
1352
         //printf("l(%d,%d,%d) ", socket->seq - socket->seqReceived, socket->seq, socket->seqReceived);
1353
         if(self != IPThread && ++tries < 200)
1354
         {
1355
            OS_ThreadSleep(1);
1356
            continue;
1357
         }
1358
      }
1359
      tries = 0;
1360
      while(socket->frameSend == NULL)
1361
      {
1362
         socket->frameSend = IPFrameGet(FRAME_COUNT_SEND);
1363
         socket->sendOffset = 0;
1364
         if(socket->frameSend == NULL)
1365
         {
1366
            //printf("L");
1367
            if(self == IPThread || ++tries > 200)
1368
               break;
1369
            else
1370
               OS_ThreadSleep(1);
1371
         }
1372
      }
1373
      frameOut = socket->frameSend;
1374
      offset = socket->sendOffset;
1375
      if(frameOut == NULL)
1376
         break;
1377
      packetOut = frameOut->packet;
1378
 
1379
      if(socket->state == IP_PING)
1380
      {
1381
         bytes = length;
1382
         memcpy(packetOut, socket->headerSend, PING_DATA);
1383
         memcpy(packetOut+PING_DATA, buf, bytes);
1384
         IPSendPacket(socket, socket->frameSend, PING_DATA + bytes);
1385
         socket->frameSend = NULL;
1386
      }
1387
      else if(socket->state != IP_UDP)
1388
      {
1389
         bytes = 512 - offset;
1390
         if(bytes > length)
1391
            bytes = length;
1392
         socket->sendOffset += bytes;
1393
         memcpy(packetOut+TCP_DATA+offset, buf, bytes);
1394
         if(socket->sendOffset >= 512)
1395
            IPWriteFlush(socket);
1396
         //if(Socket->seq - Socket->seqReceived > Socket->seqWindow)
1397
         //{
1398
         //   printf("W");
1399
         //   OS_ThreadSleep(10);
1400
         //}
1401
      }
1402
      else  //UDP
1403
      {
1404
         bytes = length;
1405
         memcpy(packetOut+UDP_DATA+offset, buf, bytes);
1406
         memcpy(packetOut, socket->headerSend, UDP_LENGTH);
1407
         IPSendPacket(socket, socket->frameSend, UDP_DATA + bytes);
1408
         socket->frameSend = NULL;
1409
      }
1410
      count += bytes;
1411
      buf += bytes;
1412
      length -= bytes;
1413
   }
1414
   return count;
1415
}
1416
 
1417
 
1418
uint32 IPRead(IPSocket *socket, uint8 *buf, uint32 length)
1419
{
1420
   IPFrame *frame, *frame2;
1421
   int count=0, bytes, offset;
1422
 
1423 361 rhoads
#ifdef INCLUDE_FILESYS
1424
   if(socket->fileIn)   //override stdin
1425
   {
1426
      bytes = fread(buf, 1, 1, socket->fileIn);
1427
      if(bytes == 0)
1428
      {
1429
         buf[0] = 0;
1430
         fclose(socket->fileIn);
1431
         socket->fileIn = NULL;
1432
         bytes = 1;
1433
      }
1434
      return bytes;
1435
   }
1436
#endif
1437
 
1438 353 rhoads
   if(socket->state == IP_UDP)
1439
      offset = UDP_DATA;
1440
   else
1441
      offset = TCP_DATA;
1442
 
1443
   OS_MutexPend(IPMutex);
1444
   for(frame = socket->frameReadTail; length && frame; )
1445
   {
1446
      bytes = frame->length - offset - socket->readOffset;
1447
      if(bytes > (int)length)
1448
         bytes = length;
1449
      memcpy(buf, frame->packet + offset + socket->readOffset, bytes);
1450
      buf += bytes;
1451
      socket->readOffset += bytes;
1452
      length -= bytes;
1453
      count += bytes;
1454
 
1455
      //Check if done with packet
1456
      frame2 = frame;
1457
      frame = frame->prev;
1458
      if(socket->readOffset == frame2->length - offset)
1459
      {
1460
         //Remove packet from socket linked list
1461
         socket->readOffset = 0;
1462
         FrameRemove(&socket->frameReadHead, &socket->frameReadTail, frame2);
1463
         socket->ackProcessed += frame2->length - offset;
1464
         if(socket->state == IP_TCP &&
1465
            socket->ack - socket->ackProcessed > RECEIVE_WINDOW - 2048)
1466
         {
1467
            //Update receive window for flow control
1468
            frame2->packet[TCP_FLAGS] = TCP_FLAGS_ACK;
1469
            TCPSendPacket(socket, frame2, TCP_DATA);
1470
         }
1471
         else
1472
            FrameFree(frame2);
1473
      }
1474
   }
1475
   OS_MutexPost(IPMutex);
1476
   return count;
1477
}
1478
 
1479
 
1480
static void IPClose2(IPSocket *socket)
1481
{
1482
   IPFrame *frame, *framePrev;
1483
 
1484 372 rhoads
   //printf("IPClose2(%x) ", (int)socket);
1485
 
1486 353 rhoads
   OS_MutexPend(IPMutex);
1487
 
1488 372 rhoads
   //Remove pending packets
1489
   for(frame = FrameSendHead; frame; )
1490 353 rhoads
   {
1491 372 rhoads
      framePrev = frame;
1492
      frame = frame->next;
1493
      if(framePrev->socket == socket)
1494
      {
1495
         FrameRemove(&FrameResendHead, &FrameResendTail, framePrev);
1496
         FrameFree(framePrev);
1497
      }
1498 353 rhoads
   }
1499
 
1500
   //Remove packets from retransmision list
1501
   for(frame = FrameResendHead; frame; )
1502
   {
1503
      framePrev = frame;
1504
      frame = frame->next;
1505
      if(framePrev->socket == socket)
1506
      {
1507
         FrameRemove(&FrameResendHead, &FrameResendTail, framePrev);
1508
         FrameFree(framePrev);
1509
      }
1510
   }
1511
 
1512
   //Remove packets from socket read linked list
1513
   for(frame = socket->frameReadHead; frame; )
1514
   {
1515
      framePrev = frame;
1516
      frame = frame->next;
1517
      FrameRemove(&socket->frameReadHead, &socket->frameReadTail, framePrev);
1518
      FrameFree(framePrev);
1519
   }
1520
 
1521 382 rhoads
   //Give application time to stop using socket
1522
   socket->timeout = SOCKET_TIMEOUT;
1523 372 rhoads
   socket->state = IP_CLOSED;
1524
 
1525 353 rhoads
   OS_MutexPost(IPMutex);
1526
}
1527
 
1528
 
1529
void IPClose(IPSocket *socket)
1530
{
1531
   IPFrame *frameOut;
1532
 
1533 372 rhoads
   //printf("IPClose(%x) ", (int)socket);
1534
 
1535 353 rhoads
   IPWriteFlush(socket);
1536
   if(socket->state <= IP_UDP)
1537
   {
1538
      IPClose2(socket);
1539
      return;
1540
   }
1541
   frameOut = IPFrameGet(0);
1542
   if(frameOut == NULL)
1543
      return;
1544
   frameOut->packet[TCP_FLAGS] = TCP_FLAGS_FIN | TCP_FLAGS_ACK;
1545
   TCPSendPacket(socket, frameOut, TCP_DATA);
1546
   ++socket->seq;
1547 382 rhoads
   socket->timeout = SOCKET_TIMEOUT;
1548
   socket->timeoutReset = SOCKET_TIMEOUT;
1549
   socket->state = IP_FIN_SERVER;
1550 353 rhoads
}
1551
 
1552
 
1553
void IPPrintf(IPSocket *socket, char *message,
1554
              int arg0, int arg1, int arg2, int arg3)
1555
{
1556
   char buf[500];
1557
   if(socket == NULL)
1558
   {
1559
      printf(message, arg0, arg1, arg2, arg3);
1560
      return;
1561
   }
1562
   if(strcmp(message, "%s") == 0)
1563
      IPWrite(socket, (uint8*)arg0, (int)strlen((char*)arg0));
1564
   else
1565
   {
1566
      sprintf(buf, message, arg0, arg1, arg2, arg3, 0, 0, 0, 0);
1567
      IPWrite(socket, (uint8*)buf, (int)strlen(buf));
1568
   }
1569
   if(socket->dontFlush == 0 || strstr(message, "\n"))
1570
      IPWriteFlush(socket);
1571
}
1572
 
1573
 
1574
void IPTick(void)
1575
{
1576
   IPFrame *frame, *frame2;
1577
   IPSocket *socket, *socket2;
1578
   unsigned long ticks;
1579
   static unsigned long ticksPrev=0, ticksPrev2=0;
1580
 
1581
   ticks = OS_ThreadTime();
1582
#ifdef WIN32
1583
   ticks = ticksPrev + 100;
1584
#endif
1585
   if(ticks - ticksPrev >= 95)
1586
   {
1587
      if(IPVerbose && (Seconds % 60) == 0)
1588
      {
1589
         if(FrameFreeCount >= FRAME_COUNT-1)
1590
            printf("T");
1591
         else
1592
            printf("T(%d)", FrameFreeCount);
1593
      }
1594
      ++Seconds;
1595
      if(--DhcpRetrySeconds <= 0)
1596
         IPDhcp(NULL, 400, 1);   //DHCP request
1597
   }
1598
 
1599
   OS_MutexPend(IPMutex);
1600
 
1601
   //Retransmit timeout packets
1602
   for(frame = FrameResendHead; frame; )
1603
   {
1604
      frame2 = frame;
1605
      frame = frame->next;
1606
      frame2->timeout = (short)(frame2->timeout - (ticks - ticksPrev2));
1607
      if(--frame2->timeout <= 0)
1608
      {
1609
         if(IPVerbose)
1610 380 rhoads
            printf("r" /*"(%x,%x,%d,%d,%d)"*/, (int)frame2, (int)frame2->socket,
1611
               frame2->retryCnt, frame2->length - TCP_DATA,
1612
               frame2->socket->state);
1613 353 rhoads
         FrameRemove(&FrameResendHead, &FrameResendTail, frame2);
1614 382 rhoads
         if(frame2->retryCnt < 4 && frame2->socket->state < IP_CLOSED)
1615 372 rhoads
            IPSendFrame(frame2);
1616
         else
1617
         {
1618
            if(frame2->socket->state == IP_TCP)
1619
               IPClose(frame2->socket);
1620
            FrameFree(frame2);
1621
         }
1622 353 rhoads
      }
1623
   }
1624
 
1625
   if(ticks - ticksPrev >= 95)
1626
   {
1627
      //Close timed out sockets
1628
      for(socket = SocketHead; socket; )
1629
      {
1630
         socket2 = socket;
1631
         socket = socket->next;
1632
         if(socket2->timeout && --socket2->timeout == 0)
1633
         {
1634 382 rhoads
            socket2->timeout = SOCKET_TIMEOUT;
1635
            if(socket2->state <= IP_TCP || socket2->state == IP_FIN_CLIENT)
1636
               IPClose(socket2);
1637
            else if(socket2->state != IP_CLOSED)
1638
               IPClose2(socket2);
1639
            else
1640 372 rhoads
            {
1641 378 rhoads
               if(socket2->prev == NULL)
1642
                  SocketHead = socket2->next;
1643 372 rhoads
               else
1644 378 rhoads
                  socket2->prev->next = socket2->next;
1645
               if(socket2->next)
1646
                  socket2->next->prev = socket2->prev;
1647
               //printf("freeSocket(%x) ", (int)socket2);
1648
               free(socket2);
1649 372 rhoads
            }
1650 353 rhoads
         }
1651
      }
1652
      ticksPrev = ticks;
1653
   }
1654
   OS_MutexPost(IPMutex);
1655
   ticksPrev2 = ticks;
1656
}
1657
 
1658
 
1659
static void DnsCallback(IPSocket *socket)
1660
{
1661
   uint8 buf[200], *ptr;
1662
   uint32 ipAddress;
1663
   int bytes;
1664
 
1665
   memset(buf, 0, sizeof(buf));
1666
   bytes = IPRead(socket, buf, sizeof(buf));
1667
   if(buf[DNS_NUM_ANSWERS_RR+1])
1668
   {
1669
      for(ptr = buf + DNS_QUESTIONS; ptr + 14 <= buf + bytes; ++ptr)
1670
      {
1671
         if(ptr[0] == 0 && ptr[1] == 1 && ptr[2] == 0 && ptr[3] == 1 &&
1672
            ptr[8] == 0 && ptr[9] == 4)
1673
         {
1674
            ipAddress = (ptr[10] << 24) | (ptr[11] << 16) | (ptr[12] << 8) | ptr[13];
1675
            printf("ipAddress = %d.%d.%d.%d\n", ptr[10], ptr[11], ptr[12], ptr[13]);
1676
            socket->userData = ipAddress;
1677
            if(socket->userFunc)
1678
            {
1679 400 rhoads
               socket->userFunc(socket, socket->userPtr, ipAddress);
1680 353 rhoads
            }
1681
            break;
1682
         }
1683
      }
1684
   }
1685
   IPClose(socket);
1686
}
1687
 
1688
 
1689 400 rhoads
void IPResolve(char *name, IPCallbackPtr resolvedFunc, void *arg)
1690 353 rhoads
{
1691
   uint8 buf[200], *ptr;
1692
   int length, i;
1693
   IPSocket *socket;
1694
 
1695
   socket = IPOpen(IP_MODE_UDP, ipAddressDns, DNS_PORT, DnsCallback);
1696
   memset(buf, 0, sizeof(buf));
1697
   buf[DNS_ID+1] = 1;
1698
   buf[DNS_FLAGS] = 1;
1699
   buf[DNS_NUM_QUESTIONS+1] = 1;
1700
 
1701
   //Setup name
1702
   ptr = buf + DNS_QUESTIONS;
1703
   strncpy((char*)ptr+1, name, 100);
1704
   ptr[0] = 1;
1705
   while(ptr[0])
1706
   {
1707
      for(i = 0; i < 100; ++i)
1708
      {
1709
         if(ptr[i+1] == '.' || ptr[i+1] == 0)
1710
         {
1711
            ptr[0] = (uint8)i;
1712
            ptr += i+1;
1713
            break;
1714
         }
1715
      }
1716
   }
1717
   ++ptr;
1718
   ptr[1] = DNS_QUERY_TYPE_IP;
1719
   ptr[3] = DNS_QUERY_CLASS;
1720
   length = (int)(ptr - buf) + 4;
1721
   if(length < 60)
1722
      length = 60;
1723
 
1724 400 rhoads
   socket->userFunc = resolvedFunc;
1725 353 rhoads
   socket->userPtr = arg;
1726
   socket->userData = 0;
1727
   IPWrite(socket, buf, length);
1728
}
1729
 

powered by: WebSVN 2.1.0

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