OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [bootloaders/] [orpmon/] [services/] [net.c] - Blame information for rev 343

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

Line No. Rev Author Line
1 2 marcus.erl
/*
2
 *      Copied from Linux Monitor (LiMon) - Networking.
3
 *
4
 *      Copyright 1994 - 2000 Neil Russell.
5
 *      (See License)
6
 *      Copyright 2000 Roland Borde
7
 *      Copyright 2000 Paolo Scaffardi
8
 *      Copyright 2000, 2001 Wolfgang Denk
9
 */
10
 
11
/*
12
 * General Desription:
13
 *
14
 * The user interface supports commands for BOOTP, RARP, and TFTP.
15
 * Also, we support ARP internally. Depending on available data,
16
 * these interact as follows:
17
 *
18
 * BOOTP:
19
 *
20
 *      Prerequisites:  - own ethernet address
21
 *      We want:        - own IP address
22
 *                      - TFTP server IP address
23
 *                      - name of bootfile
24
 *      Next step:      ARP
25
 *
26
 * RARP:
27
 *
28
 *      Prerequisites:  - own ethernet address
29
 *      We want:        - own IP address
30
 *                      - TFTP server IP address
31
 *      Next step:      ARP
32
 *
33
 * ARP:
34
 *
35
 *      Prerequisites:  - own ethernet address
36
 *                      - own IP address
37
 *                      - TFTP server IP address
38
 *      We want:        - TFTP server ethernet address
39
 *      Next step:      TFTP
40
 *
41
 * DHCP:
42
 *
43
 *     Prerequisites:   - own ethernet address
44
 *     We want:         - IP, Netmask, ServerIP, Gateway IP
45
 *                      - bootfilename, lease time
46
 *     Next step:       - TFTP
47
 *
48
 * TFTP:
49
 *
50
 *      Prerequisites:  - own ethernet address
51
 *                      - own IP address
52
 *                      - TFTP server IP address
53
 *                      - TFTP server ethernet address
54
 *                      - name of bootfile (if unknown, we use a default name
55
 *                        derived from our own IP address)
56
 *      We want:        - load the boot file
57
 *      Next step:      none
58
 */
59
 
60
 
61
#include "common.h"
62
#include "support.h"
63
#include "net.h"
64
#include "bootp.h"
65
#include "tftp.h"
66
#include "rarp.h"
67
#include "arp.h"
68
#if OC_LAN==1
69 140 julius
#include "eth.h"
70 246 julius
#else 
71
# if SMC91111_LAN==1
72
#  include "smc91111.h"
73
# endif
74 2 marcus.erl
#endif
75
 
76
#if 0
77
#define ET_DEBUG
78
#endif
79
 
80
/** BOOTP EXTENTIONS **/
81
 
82
IPaddr_t        NetOurSubnetMask=0;              /* Our subnet mask (0=unknown)  */
83
IPaddr_t        NetOurGatewayIP=0;               /* Our gateways IP address      */
84
IPaddr_t        NetOurDNSIP=0;                   /* Our DNS IP address           */
85
char            NetOurNISDomain[32]={0,};        /* Our NIS domain               */
86
char            NetOurHostName[32]={0,}; /* Our hostname                 */
87
char            NetOurRootPath[64]={0,}; /* Our bootpath                 */
88
unsigned short          NetBootFileSize=0;               /* Our bootfile size in blocks  */
89
 
90
/** END OF BOOTP EXTENTIONS **/
91
 
92
unsigned long           NetBootFileXferSize;    /* The actual transferred size of the bootfile (in bytes) */
93
unsigned char           NetOurEther[6];         /* Our ethernet address                 */
94
unsigned char           NetServerEther[6] =     /* Boot server enet address             */
95 140 julius
  { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
96 2 marcus.erl
IPaddr_t        NetOurIP;               /* Our IP addr (0 = unknown)            */
97
IPaddr_t        NetServerIP;            /* Our IP addr (0 = unknown)            */
98
volatile unsigned char *NetRxPkt;               /* Current receive packet               */
99
int             NetRxPktLen;            /* Current rx packet length             */
100
unsigned        NetIPID;                /* IP packet ID                         */
101
unsigned char           NetBcastAddr[6] =       /* Ethernet bcast address               */
102 140 julius
  { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
103 2 marcus.erl
int             NetState;               /* Network loop state                   */
104
 
105
char            BootFile[128];          /* Boot File name                       */
106
 
107
volatile unsigned char  PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
108
 
109
volatile unsigned char *NetRxPackets[PKTBUFSRX]; /* Receive packets                     */
110
 
111
static rxhand_f *packetHandler;         /* Current RX packet handler            */
112
static thand_f *timeHandler;            /* Current timeout handler              */
113
static unsigned long    timeValue;              /* Current timeout value                */
114
volatile unsigned char *NetTxPacket = 0; /* THE transmit packet                  */
115
 
116
static int net_check_prereq (proto_t protocol);
117
 
118
/**********************************************************************/
119
/*
120
 *      Main network processing loop.
121
 */
122
int
123
NetLoop(proto_t protocol)
124
{
125
#if 1
126 140 julius
  if (!NetTxPacket) {
127
    int i;
128
    printf("NetTxPacket begin setup\n");
129
    /*
130
     *  Setup packet buffers, aligned correctly.
131
     */
132
    NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
133
    NetTxPacket -= (unsigned long)NetTxPacket % PKTALIGN;
134
    for (i = 0; i < PKTBUFSRX; i++) {
135
      NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
136
    }
137
  }
138 2 marcus.erl
 
139 140 julius
  eth_halt();
140
  eth_init(NetReceive);
141 2 marcus.erl
 
142 140 julius
 restart:
143 2 marcus.erl
 
144 140 julius
  NetCopyEther(NetOurEther, global.eth_add);
145 2 marcus.erl
 
146 140 julius
  NetState = NETLOOP_CONTINUE;
147 2 marcus.erl
 
148 140 julius
  /*
149
   *    Start the ball rolling with the given start function.  From
150
   *    here on, this code is a state machine driven by received
151
   *    packets and timer events.
152
   */
153 2 marcus.erl
 
154 140 julius
  if (protocol == TFTP) {                       /* TFTP */
155
    NetOurIP    = global.ip;
156
    NetServerIP = global.srv_ip;
157
    NetOurGatewayIP = global.gw_ip;
158
    NetOurSubnetMask= global.mask;
159 2 marcus.erl
 
160 140 julius
    if (net_check_prereq (protocol) != 0) {
161
      return 0;
162
    }
163 2 marcus.erl
 
164 140 julius
    /* always use ARP to get server ethernet address */
165
    ArpTry = 0;
166 2 marcus.erl
 
167 140 julius
    ArpRequest ();
168 2 marcus.erl
 
169
#if (CONFIG_COMMANDS & CFG_CMD_DHCP)
170 140 julius
  } else if (protocol == DHCP) {
171
    if (net_check_prereq (protocol) != 0) {
172
      return 0;
173
    }
174 2 marcus.erl
 
175 140 julius
    /* Start with a clean slate... */
176
    NetOurIP = 0;
177
    NetServerIP = 0;
178
    DhcpRequest();              /* Basically same as BOOTP */
179 2 marcus.erl
 
180
#endif  /* CFG_CMD_DHCP */
181
 
182 140 julius
  } else {                              /* BOOTP or RARP */
183 2 marcus.erl
 
184 140 julius
    /*
185
     * initialize our IP addr to 0 in order to accept ANY
186
     * IP addr assigned to us by the BOOTP / RARP server
187
     */
188
    NetOurIP = 0;
189
    NetServerIP = 0;
190 2 marcus.erl
 
191 140 julius
    if (net_check_prereq (protocol) != 0) {
192
      return 0;
193
    }
194 2 marcus.erl
#ifdef BOOTP
195 140 julius
    if (protocol == BOOTP) {
196
      BootpTry = 0;
197
      BootpRequest ();
198
    }
199 2 marcus.erl
#endif
200
#ifdef RARP
201
    if {
202 140 julius
      RarpTry    = 0;
203
      RarpRequest ();
204
    }
205 2 marcus.erl
#endif
206 140 julius
  }
207 2 marcus.erl
 
208 140 julius
  NetBootFileXferSize = 0;
209 2 marcus.erl
 
210 140 julius
  /*
211
   *    Main packet reception loop.  Loop receiving packets until
212
   *    someone sets `NetQuit'.
213
   */
214
  for (;;) {
215
    //          WATCHDOG_RESET();
216
    /*
217
     *  Check the ethernet for a new packet.  The ethernet
218
     *  receive routine will process it.
219
     */
220
    eth_rx();
221 2 marcus.erl
 
222 140 julius
    /*
223
     *  Abort if ctrl-c was pressed.
224
     */
225
    if (ctrlc()) {
226
      eth_halt();
227
      printf("\nAbort\n");
228
      return 0;
229
    }
230 2 marcus.erl
 
231
 
232 140 julius
    /*
233
     *  Check for a timeout, and run the timeout handler
234
     *  if we have one.
235
     */
236
    /*
237
      if (timeHandler && (get_timer(0) > timeValue)) {
238
      thand_f *x;
239 2 marcus.erl
 
240 140 julius
      x = timeHandler;
241
      timeHandler = (thand_f *)0;
242
      (*x)();
243
      }
244
    */
245
 
246
    switch (NetState) {
247 2 marcus.erl
 
248 140 julius
    case NETLOOP_RESTART:
249
      goto restart;
250 2 marcus.erl
 
251 140 julius
    case NETLOOP_SUCCESS:
252
      if (NetBootFileXferSize > 0) {
253
        printf("Bytes transferred = %ld (0x%lx)\n",
254
               NetBootFileXferSize,
255
               NetBootFileXferSize);
256
#ifdef TFTP_CALC_CHKSUM
257
        printf("CHKSUM: 0x%lx\n", TFTP_CHKSUM);
258
#endif
259
      }
260
      eth_halt();
261
      return NetBootFileXferSize;
262 2 marcus.erl
 
263 140 julius
    case NETLOOP_FAIL:
264
      return 0;
265
    }
266
  }
267 2 marcus.erl
#endif
268
}
269
 
270
/**********************************************************************/
271
 
272
 
273
#if 1
274
void
275
NetStartAgain(void)
276
{
277 140 julius
  NetState = NETLOOP_RESTART;
278 2 marcus.erl
}
279
 
280
/**********************************************************************/
281
/*
282
 *      Miscelaneous bits.
283
 */
284
 
285
void
286
NetSetHandler(rxhand_f * f)
287
{
288 140 julius
  packetHandler = f;
289 2 marcus.erl
}
290
 
291
 
292
void
293
NetSetTimeout(int iv, thand_f * f)
294
{
295 140 julius
  if (iv == 0) {
296
    timeHandler = (thand_f *)0;
297
  } else {
298
    timeHandler = f;
299
    timeValue = get_timer(0) + iv;
300
  }
301 2 marcus.erl
}
302
 
303
 
304
void
305
NetSendPacket(volatile unsigned char * pkt, int len)
306
{
307
 
308
#if OC_LAN==1
309 140 julius
  unsigned char *p = (unsigned char *)0;
310
  while (p == (unsigned char*) 0)
311
    p = eth_get_tx_buf();
312
 
313 2 marcus.erl
  memcpy(p, (void *)pkt, len);
314
  eth_send(p, len);
315 246 julius
#else
316
# if  SMC91111_LAN==1
317 2 marcus.erl
  eth_send(pkt, len);
318 246 julius
# endif
319 2 marcus.erl
#endif
320
}
321
 
322
 
323
 
324
void
325
NetReceive(volatile unsigned char * pkt, int len)
326
{
327 140 julius
  Ethernet_t *et;
328
  IP_t  *ip;
329
  ARP_t *arp;
330
  int   x;
331
  IPaddr_t ip_to_check; // Used as a temp variable to check IP
332 2 marcus.erl
 
333 140 julius
  NetRxPkt = pkt;
334
  NetRxPktLen = len;
335
  et = (Ethernet_t *)pkt;
336 2 marcus.erl
 
337 140 julius
  x = SWAP16(et->et_protlen);
338 2 marcus.erl
 
339 140 julius
  if (x < 1514) {
340
    /*
341
     *  Got a 802 packet.  Check the other protocol field.
342
     */
343
    x = SWAP16(et->et_prot);
344
    ip = (IP_t *)(pkt + E802_HDR_SIZE);
345
    len -= E802_HDR_SIZE;
346
  } else {
347
    ip = (IP_t *)(pkt + ETHER_HDR_SIZE);
348
    len -= ETHER_HDR_SIZE;
349
  }
350 2 marcus.erl
 
351
#ifdef ET_DEBUG
352 140 julius
  printf("Receive from protocol 0x%x\n", x);
353 2 marcus.erl
#endif
354
 
355 140 julius
  switch (x) {
356 2 marcus.erl
 
357 140 julius
  case PROT_ARP:
358
    /*
359
     * We have to deal with two types of ARP packets:
360
     * - REQUEST packets will be answered by sending  our
361
     *   IP address - if we know it.
362
     * - REPLY packates are expected only after we asked
363
     *   for the TFTP server's or the gateway's ethernet
364
     *   address; so if we receive such a packet, we set
365
     *   the server ethernet address
366
     */
367 2 marcus.erl
#ifdef ET_DEBUG
368 140 julius
    printf("Got ARP\n");
369 2 marcus.erl
#endif
370 140 julius
    arp = (ARP_t *)ip;
371
    if (len < ARP_HDR_SIZE) {
372
      printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
373
      return;
374
    }
375
    if (SWAP16(arp->ar_hrd) != ARP_ETHER) {
376
      return;
377
    }
378
    if (SWAP16(arp->ar_pro) != PROT_IP) {
379
      return;
380
    }
381
    if (arp->ar_hln != 6) {
382
      return;
383
    }
384
    if (arp->ar_pln != 4) {
385
      return;
386
    }
387
 
388
    memcpy((void*) &ip_to_check, (void*)&arp->ar_data[16],
389
           sizeof(IPaddr_t));
390
    if (NetOurIP == 0 ||
391
        ip_to_check != NetOurIP) {
392
      return;
393
    }
394 2 marcus.erl
 
395 140 julius
    switch (SWAP16(arp->ar_op)) {
396
    case ARPOP_REQUEST:         /* reply with our IP address  */
397 2 marcus.erl
#ifdef ET_DEBUG
398 140 julius
      printf("Got ARP REQUEST, return our IP\n");
399 2 marcus.erl
#endif
400 140 julius
      NetSetEther((unsigned char *)et, et->et_src, PROT_ARP);
401
      arp->ar_op = SWAP16(ARPOP_REPLY);
402
      NetCopyEther(&arp->ar_data[10], &arp->ar_data[0]);
403
      NetCopyEther(&arp->ar_data[0], NetOurEther);
404
      //*(IPaddr_t *)(&arp->ar_data[16]) = *(IPaddr_t *)(&arp->ar_data[6]);
405
      memcpy((void*)&arp->ar_data[16],(void*) &arp->ar_data[6],
406
             sizeof(IPaddr_t));
407
      //*(IPaddr_t *)(&arp->ar_data[6]) = NetOurIP;
408
      memcpy((void*)&arp->ar_data[6],(void*) &NetOurIP,
409
             sizeof(IPaddr_t));
410
 
411
      NetSendPacket((unsigned char *)et,
412
                    ((unsigned char *)arp-pkt)+ARP_HDR_SIZE);
413
      return;
414
    case ARPOP_REPLY:           /* set TFTP server eth addr     */
415 2 marcus.erl
#ifdef ET_DEBUG
416 140 julius
      printf("Got ARP REPLY, set server/gtwy eth addr\n");
417 2 marcus.erl
#endif
418 140 julius
      NetCopyEther(NetServerEther, &arp->ar_data[0]);
419
      (*packetHandler)(0,0,0,0);    /* start TFTP */
420
      return;
421
    default:
422 2 marcus.erl
#ifdef ET_DEBUG
423 140 julius
      printf("Unexpected ARP opcode 0x%x\n", SWAP16(arp->ar_op));
424 2 marcus.erl
#endif
425 140 julius
      return;
426
    }
427 2 marcus.erl
 
428 140 julius
  case PROT_RARP:
429 2 marcus.erl
#ifdef ET_DEBUG
430 140 julius
    printf("Got RARP\n");
431 2 marcus.erl
#endif
432 140 julius
    arp = (ARP_t *)ip;
433
    if (len < ARP_HDR_SIZE) {
434
      printf("bad length %d < %d\n", len, ARP_HDR_SIZE);
435
      return;
436
    }
437 2 marcus.erl
 
438 140 julius
    if ((SWAP16(arp->ar_op) != RARPOP_REPLY) ||
439
        (SWAP16(arp->ar_hrd) != ARP_ETHER)   ||
440
        (SWAP16(arp->ar_pro) != PROT_IP)     ||
441
        (arp->ar_hln != 6) || (arp->ar_pln != 4)) {
442 2 marcus.erl
 
443 140 julius
      printf("invalid RARP header\n");
444
    } else {
445
      //NetOurIP = *((IPaddr_t *)&arp->ar_data[16]);
446
      memcpy((void*) &NetOurIP, (void*) &arp->ar_data[16],
447
             sizeof(IPaddr_t));
448
      //NetServerIP = *((IPaddr_t *)&arp->ar_data[6]);
449
      memcpy((void*) &NetServerIP,(void*) &arp->ar_data[6],
450
             sizeof(IPaddr_t));
451 2 marcus.erl
 
452 140 julius
      NetCopyEther(NetServerEther, &arp->ar_data[0]);
453 2 marcus.erl
 
454 140 julius
      (*packetHandler)(0,0,0,0);
455
    }
456
    break;
457
 
458
  case PROT_IP:
459 2 marcus.erl
#ifdef ET_DEBUG
460 140 julius
    printf("Got IP\n");
461 2 marcus.erl
#endif
462 140 julius
    if (len < IP_HDR_SIZE) {
463
      debug ("len bad %d < %d\n", len, IP_HDR_SIZE);
464
      return;
465
    }
466
    if (len < SWAP16(ip->ip_len)) {
467
      printf("len bad %d < %d\n", len, SWAP16(ip->ip_len));
468
      return;
469
    }
470
    len = SWAP16(ip->ip_len);
471 2 marcus.erl
#ifdef ET_DEBUG
472 140 julius
    printf("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
473 2 marcus.erl
#endif
474 140 julius
    if ((ip->ip_hl_v & 0xf0) != 0x40) {
475
      return;
476
    }
477
    if (ip->ip_off & SWAP16c(0x1fff)) { /* Can't deal w/ fragments */
478
      return;
479
    }
480
    if (!NetCksumOk((unsigned char *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
481
      //printf("checksum bad\n");
482
      return;
483
    }
484 2 marcus.erl
 
485 140 julius
    memcpy((void*)&ip_to_check,(void*)&ip->ip_dst, sizeof (IPaddr_t));
486 2 marcus.erl
 
487 140 julius
    if (NetOurIP &&
488
        ip_to_check != NetOurIP &&
489
        ip_to_check != 0xFFFFFFFF) {
490
      return;
491
    }
492
    /*
493
     * watch for ICMP host redirects
494
     *
495
     * There is no real handler code (yet). We just watch
496
     * for ICMP host redirect messages. In case anybody
497
     * sees these messages: please contact me
498
     * (wd@denx.de), or - even better - send me the
499
     * necessary fixes :-)
500
     *
501
     * Note: in all cases where I have seen this so far
502
     * it was a problem with the router configuration,
503
     * for instance when a router was configured in the
504
     * BOOTP reply, but the TFTP server was on the same
505
     * subnet. So this is probably a warning that your
506
     * configuration might be wrong. But I'm not really
507
     * sure if there aren't any other situations.
508
     */
509
    if (ip->ip_p == IPPROTO_ICMP) {
510
      ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
511 2 marcus.erl
 
512 140 julius
      if (icmph->type != ICMP_REDIRECT)
513
        return;
514
      if (icmph->code != ICMP_REDIR_HOST)
515
        return;
516
      printf (" ICMP Host Redirect to ");
517
      print_IPaddr(icmph->un.gateway);
518
      putc(' ');
519
    } else if (ip->ip_p != IPPROTO_UDP) {       /* Only UDP packets */
520
      return;
521
    }
522
 
523
    /*
524
     *  IP header OK.  Pass the packet to the current handler.
525
     */
526
    (*packetHandler)((unsigned char *)ip +IP_HDR_SIZE,
527
                     SWAP16(ip->udp_dst),
528
                     SWAP16(ip->udp_src),
529
                     SWAP16(ip->udp_len) - 8);
530
 
531
    break;
532
  }
533 2 marcus.erl
}
534
 
535
 
536
/**********************************************************************/
537
 
538
static int net_check_prereq (proto_t protocol)
539
{
540 140 julius
  switch (protocol) {
541
  case ARP:     /* nothing to do */
542
    break;
543 2 marcus.erl
 
544 140 julius
  case TFTP:
545
    if (NetServerIP == 0) {
546
      printf     ("*** ERROR: `serverip' not set\n");
547
      return (1);
548
    }
549 2 marcus.erl
 
550 140 julius
    if (NetOurIP == 0) {
551
      printf ("*** ERROR: `ipaddr' not set\n");
552
      return (1);
553
    }
554
    /* Fall through */
555 2 marcus.erl
 
556 140 julius
  case DHCP:
557
  case RARP:
558
  case BOOTP:
559
    if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
560
      printf ("*** ERROR: `ethaddr' not set\n");
561
      return (1);
562
    }
563
    /* Fall through */
564
  }
565
  return (0);    /* OK */
566 2 marcus.erl
}
567
/**********************************************************************/
568
 
569
int
570
NetCksumOk(unsigned char * ptr, int len)
571
{
572 140 julius
  return !((NetCksum(ptr, len) + 1) & 0xfffe);
573 2 marcus.erl
}
574
 
575
 
576
unsigned
577
NetCksum(unsigned char * ptr, int len)
578
{
579 140 julius
  unsigned long xsum;
580 2 marcus.erl
 
581 140 julius
  xsum = 0;
582
  while (len-- > 0)
583
    {
584
      xsum += (*((unsigned short *)ptr));
585
      ptr += sizeof(short);
586
    }
587
 
588
  xsum = (xsum & 0xffff) + (xsum >> 16);
589
  xsum = (xsum & 0xffff) + (xsum >> 16);
590
  return (xsum & 0xffff);
591 2 marcus.erl
}
592
 
593
 
594
void
595
NetCopyEther(volatile unsigned char * to, unsigned char * from)
596
{
597 140 julius
  int   i;
598 2 marcus.erl
 
599 140 julius
  for (i = 0; i < 6; i++)
600
    *to++ = *from++;
601 2 marcus.erl
}
602
 
603
 
604
void
605
NetSetEther(volatile unsigned char * xet, unsigned char * addr, unsigned long prot)
606
{
607 140 julius
  volatile Ethernet_t *et = (Ethernet_t *)xet;
608 2 marcus.erl
 
609 140 julius
  NetCopyEther(et->et_dest, addr);
610
  NetCopyEther(et->et_src, NetOurEther);
611
  et->et_protlen = SWAP16(prot);
612 2 marcus.erl
}
613
 
614
 
615
void
616
NetSetIP(volatile unsigned char * xip, IPaddr_t dest, int dport, int sport, int len)
617
{
618 140 julius
  volatile IP_t *ip = (IP_t *)xip;
619 2 marcus.erl
 
620 140 julius
  /*
621
   *    If the data is an odd number of bytes, zero the
622
   *    byte after the last byte so that the checksum
623
   *    will work.
624
   */
625
  if (len & 1)
626
    xip[IP_HDR_SIZE + len] = 0;
627 2 marcus.erl
 
628 140 julius
  /*
629
   *    Construct an IP and UDP header.
630
   (need to set no fragment bit - XXX)
631
  */
632
  ip->ip_hl_v  = 0x45;          /* IP_HDR_SIZE / 4 (not including UDP) */
633
  ip->ip_tos   = 0;
634
  ip->ip_len   = SWAP16(IP_HDR_SIZE + len);
635
  ip->ip_id    = SWAP16(NetIPID++);
636
  ip->ip_off   = SWAP16c(0x4000);       /* No fragmentation */
637
  ip->ip_ttl   = 255;
638
  ip->ip_p     = 17;            /* UDP */
639
  ip->ip_sum   = 0;
640
  //ip->ip_src   = NetOurIP;
641
  memcpy((void*)&ip->ip_src,(void*) &NetOurIP,
642
        sizeof(IPaddr_t));
643
  //ip->ip_dst   = dest;
644
  memcpy((void*)&ip->ip_dst,(void*) &dest,
645
        sizeof(IPaddr_t));
646
  ip->udp_src  = SWAP16(sport);
647
  ip->udp_dst  = SWAP16(dport);
648
  ip->udp_len  = SWAP16(8 + len);
649
  ip->udp_xsum = 0;
650
  ip->ip_sum   = ~NetCksum((unsigned char *)ip, IP_HDR_SIZE_NO_UDP / 2);
651 2 marcus.erl
}
652
 
653
void copy_filename (unsigned char *dst, unsigned char *src, int size)
654
{
655 140 julius
  if (*src && (*src == '"')) {
656
    ++src;
657
    --size;
658
  }
659 2 marcus.erl
 
660 140 julius
  while ((--size > 0) && *src && (*src != '"')) {
661
    *dst++ = *src++;
662
  }
663
  *dst = '\0';
664 2 marcus.erl
}
665
 
666
void ip_to_string (IPaddr_t x, char *s)
667
{
668 140 julius
  char num[] = "0123456789ABCDEF";
669
  int i;
670 2 marcus.erl
 
671 140 julius
  x = SWAP32(x);
672 2 marcus.erl
 
673 140 julius
  for(i = 28; i >= 0; i -= 4)
674
    *s++ = num[((x >> i) & 0x0f)];
675
  *s = 0;
676 2 marcus.erl
}
677
 
678
void print_IPaddr (IPaddr_t x)
679
{
680 140 julius
  char tmp[12];
681 2 marcus.erl
 
682 140 julius
  ip_to_string(x, tmp);
683 2 marcus.erl
 
684 140 julius
  printf(tmp);
685 2 marcus.erl
}
686
 
687
static unsigned int i2a(char* dest,unsigned int x) {
688
  register unsigned int tmp=x;
689
  register unsigned int len=0;
690
  if (x>=100) { *dest++=tmp/100+'0'; tmp=tmp%100; ++len; }
691
  if (x>=10) { *dest++=tmp/10+'0'; tmp=tmp%10; ++len; }
692
  *dest++=tmp+'0';
693
  return len+1;
694
}
695
 
696
char *inet_ntoa(unsigned long in) {
697
  static char buf[20];
698
  unsigned int len;
699
  unsigned char *ip=(unsigned char*)&in;
700
 
701
  len=i2a(buf,ip[0]); buf[len]='.'; ++len;
702
  len+=i2a(buf+ len,ip[1]); buf[len]='.'; ++len;
703
  len+=i2a(buf+ len,ip[2]); buf[len]='.'; ++len;
704
  len+=i2a(buf+ len,ip[3]); buf[len]=0;
705
  return buf;
706
}
707
 
708
unsigned long inet_aton(const char *cp)
709
{
710
  unsigned long a[4];
711
  unsigned long ret;
712
  char *p = (char *)cp;
713
  int i,d;
714
  if (strcmp(cp, "255.255.255.255") == 0)
715
    return -1;
716
 
717
  for(i = 0; i < 4; i++) {
718
    a[i] = strtoul(p, 0, 0);
719
    for(d=1; (p[d] != '.') && (i < 3); d++);
720
    p = &p[d+1];
721
  }
722
 
723
  ret = (a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3];
724
  return ret;
725
}
726
 
727
#endif

powered by: WebSVN 2.1.0

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