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

Subversion Repositories mlite

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

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

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

powered by: WebSVN 2.1.0

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