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 178

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

powered by: WebSVN 2.1.0

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