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] - Diff between revs 405 and 406

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 405 Rev 406
Line 55... Line 55...
 *                        derived from our own IP address)
 *                        derived from our own IP address)
 *      We want:        - load the boot file
 *      We want:        - load the boot file
 *      Next step:      none
 *      Next step:      none
 */
 */
 
 
 
 
#include "common.h"
#include "common.h"
#include "support.h"
#include "support.h"
#include "net.h"
#include "net.h"
#include "bootp.h"
#include "bootp.h"
#include "tftp.h"
#include "tftp.h"
Line 83... Line 82...
IPaddr_t        NetOurGatewayIP=0;               /* Our gateways IP address      */
IPaddr_t        NetOurGatewayIP=0;               /* Our gateways IP address      */
IPaddr_t        NetOurDNSIP=0;                   /* Our DNS IP address           */
IPaddr_t        NetOurDNSIP=0;                   /* Our DNS IP address           */
char            NetOurNISDomain[32]={0,};        /* Our NIS domain               */
char            NetOurNISDomain[32]={0,};        /* Our NIS domain               */
char            NetOurHostName[32]={0,}; /* Our hostname                 */
char            NetOurHostName[32]={0,}; /* Our hostname                 */
char            NetOurRootPath[64]={0,}; /* Our bootpath                 */
char            NetOurRootPath[64]={0,}; /* Our bootpath                 */
 
 
unsigned short          NetBootFileSize=0;               /* Our bootfile size in blocks  */
unsigned short          NetBootFileSize=0;               /* Our bootfile size in blocks  */
 
 
/** END OF BOOTP EXTENTIONS **/
/** END OF BOOTP EXTENTIONS **/
 
 
unsigned long           NetBootFileXferSize;    /* The actual transferred size of the bootfile (in bytes) */
unsigned long NetBootFileXferSize;      /* The actual transferred size
 
                                           of the bootfile (in bytes)   */
unsigned char           NetOurEther[6];         /* Our ethernet address                 */
unsigned char           NetOurEther[6];         /* Our ethernet address                 */
unsigned char           NetServerEther[6] =     /* Boot server enet address             */
unsigned char           NetServerEther[6] =     /* Boot server enet address             */
  { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
 
 
IPaddr_t        NetOurIP;               /* Our IP addr (0 = unknown)            */
IPaddr_t        NetOurIP;               /* Our IP addr (0 = unknown)            */
IPaddr_t        NetServerIP;            /* Our IP addr (0 = unknown)            */
IPaddr_t        NetServerIP;            /* Our IP addr (0 = unknown)            */
volatile unsigned char *NetRxPkt;               /* Current receive packet               */
volatile unsigned char *NetRxPkt;               /* Current receive packet               */
int             NetRxPktLen;            /* Current rx packet length             */
int             NetRxPktLen;            /* Current rx packet length             */
unsigned        NetIPID;                /* IP packet ID                         */
unsigned        NetIPID;                /* IP packet ID                         */
unsigned char           NetBcastAddr[6] =       /* Ethernet bcast address               */
unsigned char           NetBcastAddr[6] =       /* Ethernet bcast address               */
  { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
 
 
int             NetState;               /* Network loop state                   */
int             NetState;               /* Network loop state                   */
 
 
char            BootFile[128];          /* Boot File name                       */
char            BootFile[128];          /* Boot File name                       */
 
 
volatile unsigned char  PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
volatile unsigned char  PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
Line 117... Line 120...
 
 
/**********************************************************************/
/**********************************************************************/
/*
/*
 *      Main network processing loop.
 *      Main network processing loop.
 */
 */
int
int NetLoop(proto_t protocol)
NetLoop(proto_t protocol)
 
{
{
 
 
  if (!NetTxPacket) {
  if (!NetTxPacket) {
    int i;
    int i;
    printf("NetTxPacket begin setup\n");
    printf("NetTxPacket begin setup\n");
Line 197... Line 199...
      BootpRequest ();
      BootpRequest ();
    }
    }
#endif
#endif
#ifdef RARP
#ifdef RARP
    if {
    if {
      RarpTry    = 0;
                        RarpTry
 
                                = 0;
      RarpRequest ();
      RarpRequest ();
    }
    }
#endif
#endif
  }
  }
 
 
Line 226... Line 229...
      eth_halt();
      eth_halt();
      printf("\nAbort\n");
      printf("\nAbort\n");
      return 0;
      return 0;
    }
    }
 
 
 
 
    /*
    /*
     *  Check for a timeout, and run the timeout handler
     *  Check for a timeout, and run the timeout handler
     *  if we have one.
     *  if we have one.
     */
     */
    /*
    /*
Line 267... Line 269...
 
 
}
}
 
 
/**********************************************************************/
/**********************************************************************/
 
 
 
 
#if 1
#if 1
void
void NetStartAgain(void)
NetStartAgain(void)
 
{
{
  NetState = NETLOOP_RESTART;
  NetState = NETLOOP_RESTART;
}
}
 
 
/**********************************************************************/
/**********************************************************************/
/*
/*
 *      Miscelaneous bits.
 *      Miscelaneous bits.
 */
 */
 
 
void
void NetSetHandler(rxhand_f * f)
NetSetHandler(rxhand_f * f)
 
{
{
  packetHandler = f;
  packetHandler = f;
}
}
 
 
 
void NetSetTimeout(int iv, thand_f * f)
void
 
NetSetTimeout(int iv, thand_f * f)
 
{
{
  if (iv == 0) {
  if (iv == 0) {
    timeHandler = (thand_f *)0;
    timeHandler = (thand_f *)0;
  } else {
  } else {
    timeHandler = f;
    timeHandler = f;
    timeValue = get_timer(0) + iv;
    timeValue = get_timer(0) + iv;
  }
  }
}
}
 
 
 
void NetSendPacket(volatile unsigned char *pkt, int len)
void
 
NetSendPacket(volatile unsigned char * pkt, int len)
 
{
{
 
 
#if OC_LAN==1
#if OC_LAN==1
  unsigned char *p = (unsigned char *)0;
  unsigned char *p = (unsigned char *)0;
  while (p == (unsigned char*) 0)
  while (p == (unsigned char*) 0)
Line 317... Line 312...
  eth_send(pkt, len);
  eth_send(pkt, len);
# endif
# endif
#endif
#endif
}
}
 
 
 
void NetReceive(volatile unsigned char *pkt, int len)
 
 
void
 
NetReceive(volatile unsigned char * pkt, int len)
 
{
{
  Ethernet_t *et;
  Ethernet_t *et;
  IP_t  *ip;
  IP_t  *ip;
  ARP_t *arp;
  ARP_t *arp;
  int   x;
  int   x;
Line 385... Line 377...
      return;
      return;
    }
    }
 
 
    memcpy((void*) &ip_to_check, (void*)&arp->ar_data[16],
    memcpy((void*) &ip_to_check, (void*)&arp->ar_data[16],
           sizeof(IPaddr_t));
           sizeof(IPaddr_t));
    if (NetOurIP == 0 ||
                if (NetOurIP == 0 || ip_to_check != NetOurIP) {
        ip_to_check != NetOurIP) {
 
      return;
      return;
    }
    }
 
 
    switch (SWAP16(arp->ar_op)) {
    switch (SWAP16(arp->ar_op)) {
    case ARPOP_REQUEST:         /* reply with our IP address  */
    case ARPOP_REQUEST:         /* reply with our IP address  */
Line 400... Line 391...
      NetSetEther((unsigned char *)et, et->et_src, PROT_ARP);
      NetSetEther((unsigned char *)et, et->et_src, PROT_ARP);
      arp->ar_op = SWAP16(ARPOP_REPLY);
      arp->ar_op = SWAP16(ARPOP_REPLY);
      NetCopyEther(&arp->ar_data[10], &arp->ar_data[0]);
      NetCopyEther(&arp->ar_data[10], &arp->ar_data[0]);
      NetCopyEther(&arp->ar_data[0], NetOurEther);
      NetCopyEther(&arp->ar_data[0], NetOurEther);
      //*(IPaddr_t *)(&arp->ar_data[16]) = *(IPaddr_t *)(&arp->ar_data[6]);
      //*(IPaddr_t *)(&arp->ar_data[16]) = *(IPaddr_t *)(&arp->ar_data[6]);
      memcpy((void*)&arp->ar_data[16],(void*) &arp->ar_data[6],
                        memcpy((void *)&arp->ar_data[16],
             sizeof(IPaddr_t));
                               (void *)&arp->ar_data[6], sizeof(IPaddr_t));
      //*(IPaddr_t *)(&arp->ar_data[6]) = NetOurIP;
      //*(IPaddr_t *)(&arp->ar_data[6]) = NetOurIP;
      memcpy((void*)&arp->ar_data[6],(void*) &NetOurIP,
      memcpy((void*)&arp->ar_data[6],(void*) &NetOurIP,
             sizeof(IPaddr_t));
             sizeof(IPaddr_t));
 
 
      NetSendPacket((unsigned char *)et,
      NetSendPacket((unsigned char *)et,
                    ((unsigned char *)arp-pkt)+ARP_HDR_SIZE);
                                      ((unsigned char *)arp - pkt) +
 
                                      ARP_HDR_SIZE);
      return;
      return;
    case ARPOP_REPLY:           /* set TFTP server eth addr     */
    case ARPOP_REPLY:           /* set TFTP server eth addr     */
#ifdef ET_DEBUG
#ifdef ET_DEBUG
      printf("Got ARP REPLY, set server/gtwy eth addr\n");
      printf("Got ARP REPLY, set server/gtwy eth addr\n");
#endif
#endif
      NetCopyEther(NetServerEther, &arp->ar_data[0]);
      NetCopyEther(NetServerEther, &arp->ar_data[0]);
      (*packetHandler)(0,0,0,0);    /* start TFTP */
      (*packetHandler)(0,0,0,0);    /* start TFTP */
      return;
      return;
    default:
    default:
#ifdef ET_DEBUG
#ifdef ET_DEBUG
      printf("Unexpected ARP opcode 0x%x\n", SWAP16(arp->ar_op));
                        printf("Unexpected ARP opcode 0x%x\n",
 
                               SWAP16(arp->ar_op));
#endif
#endif
      return;
      return;
    }
    }
 
 
  case PROT_RARP:
  case PROT_RARP:
Line 462... Line 455...
    if (len < IP_HDR_SIZE) {
    if (len < IP_HDR_SIZE) {
      debug ("ip header len bad %d < %d\n", len, IP_HDR_SIZE);
      debug ("ip header len bad %d < %d\n", len, IP_HDR_SIZE);
      return;
      return;
    }
    }
    if (len < SWAP16(ip->ip_len)) {
    if (len < SWAP16(ip->ip_len)) {
      printf("ip header (swap) len bad %d < %d\n", len, SWAP16(ip->ip_len));
                        printf("ip header (swap) len bad %d < %d\n", len,
 
                               SWAP16(ip->ip_len));
      return;
      return;
    }
    }
    len = SWAP16(ip->ip_len);
    len = SWAP16(ip->ip_len);
#ifdef ET_DEBUG
#ifdef ET_DEBUG
    printf("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
    printf("len=%d, v=%02x\n", len, ip->ip_hl_v & 0xff);
Line 480... Line 474...
    if (!NetCksumOk((unsigned char *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
    if (!NetCksumOk((unsigned char *)ip, IP_HDR_SIZE_NO_UDP / 2)) {
      //printf("checksum bad\n");
      //printf("checksum bad\n");
      return;
      return;
    }
    }
 
 
    memcpy((void*)&ip_to_check,(void*)&ip->ip_dst, sizeof (IPaddr_t));
                memcpy((void *)&ip_to_check, (void *)&ip->ip_dst,
 
                       sizeof(IPaddr_t));
 
 
    if (NetOurIP &&
    if (NetOurIP &&
        ip_to_check != NetOurIP &&
                    ip_to_check != NetOurIP && ip_to_check != 0xFFFFFFFF) {
        ip_to_check != 0xFFFFFFFF) {
 
      return;
      return;
    }
    }
    /*
    /*
     * watch for ICMP host redirects
     * watch for ICMP host redirects
     *
     *
Line 523... Line 517...
    /*
    /*
     *  IP header OK.  Pass the packet to the current handler.
     *  IP header OK.  Pass the packet to the current handler.
     */
     */
    (*packetHandler)((unsigned char *)ip +IP_HDR_SIZE,
    (*packetHandler)((unsigned char *)ip +IP_HDR_SIZE,
                     SWAP16(ip->udp_dst),
                     SWAP16(ip->udp_dst),
                     SWAP16(ip->udp_src),
                                  SWAP16(ip->udp_src), SWAP16(ip->udp_len) - 8);
                     SWAP16(ip->udp_len) - 8);
 
 
 
    break;
    break;
  }
  }
}
}
 
 
 
 
/**********************************************************************/
/**********************************************************************/
 
 
static int net_check_prereq (proto_t protocol)
static int net_check_prereq (proto_t protocol)
{
{
  switch (protocol) {
  switch (protocol) {
Line 562... Line 554...
    }
    }
    /* Fall through */
    /* Fall through */
  }
  }
  return (0);    /* OK */
  return (0);    /* OK */
}
}
 
 
/**********************************************************************/
/**********************************************************************/
 
 
int
int NetCksumOk(unsigned char *ptr, int len)
NetCksumOk(unsigned char * ptr, int len)
 
{
{
  return !((NetCksum(ptr, len) + 1) & 0xfffe);
  return !((NetCksum(ptr, len) + 1) & 0xfffe);
}
}
 
 
 
unsigned NetCksum(unsigned char *ptr, int len)
unsigned
 
NetCksum(unsigned char * ptr, int len)
 
{
{
  unsigned long xsum;
  unsigned long xsum;
 
 
  xsum = 0;
  xsum = 0;
  while (len-- > 0)
        while (len-- > 0) {
    {
 
      xsum += (*((unsigned short *)ptr));
      xsum += (*((unsigned short *)ptr));
      ptr += sizeof(short);
      ptr += sizeof(short);
    }
    }
 
 
  xsum = (xsum & 0xffff) + (xsum >> 16);
  xsum = (xsum & 0xffff) + (xsum >> 16);
  xsum = (xsum & 0xffff) + (xsum >> 16);
  xsum = (xsum & 0xffff) + (xsum >> 16);
  return (xsum & 0xffff);
  return (xsum & 0xffff);
}
}
 
 
 
void NetCopyEther(volatile unsigned char *to, unsigned char *from)
void
 
NetCopyEther(volatile unsigned char * to, unsigned char * from)
 
{
{
  int   i;
  int   i;
 
 
  for (i = 0; i < 6; i++)
  for (i = 0; i < 6; i++)
    *to++ = *from++;
    *to++ = *from++;
}
}
 
 
 
 
void
void
NetSetEther(volatile unsigned char * xet, unsigned char * addr, unsigned long prot)
NetSetEther(volatile unsigned char *xet, unsigned char *addr,
 
            unsigned long prot)
{
{
  volatile Ethernet_t *et = (Ethernet_t *)xet;
  volatile Ethernet_t *et = (Ethernet_t *)xet;
 
 
  NetCopyEther(et->et_dest, addr);
  NetCopyEther(et->et_dest, addr);
  NetCopyEther(et->et_src, NetOurEther);
  NetCopyEther(et->et_src, NetOurEther);
  et->et_protlen = SWAP16(prot);
  et->et_protlen = SWAP16(prot);
}
}
 
 
 
 
void
void
NetSetIP(volatile unsigned char * xip, IPaddr_t dest, int dport, int sport, int len)
NetSetIP(volatile unsigned char *xip, IPaddr_t dest, int dport, int sport,
 
         int len)
{
{
  volatile IP_t *ip = (IP_t *)xip;
  volatile IP_t *ip = (IP_t *)xip;
 
 
  /*
  /*
   *    If the data is an odd number of bytes, zero the
   *    If the data is an odd number of bytes, zero the
Line 636... Line 623...
  ip->ip_off   = SWAP16c(0x4000);       /* No fragmentation */
  ip->ip_off   = SWAP16c(0x4000);       /* No fragmentation */
  ip->ip_ttl   = 255;
  ip->ip_ttl   = 255;
  ip->ip_p     = 17;            /* UDP */
  ip->ip_p     = 17;            /* UDP */
  ip->ip_sum   = 0;
  ip->ip_sum   = 0;
  //ip->ip_src   = NetOurIP;
  //ip->ip_src   = NetOurIP;
  memcpy((void*)&ip->ip_src,(void*) &NetOurIP,
        memcpy((void *)&ip->ip_src, (void *)&NetOurIP, sizeof(IPaddr_t));
        sizeof(IPaddr_t));
 
  //ip->ip_dst   = dest;
  //ip->ip_dst   = dest;
  memcpy((void*)&ip->ip_dst,(void*) &dest,
        memcpy((void *)&ip->ip_dst, (void *)&dest, sizeof(IPaddr_t));
        sizeof(IPaddr_t));
 
  ip->udp_src  = SWAP16(sport);
  ip->udp_src  = SWAP16(sport);
  ip->udp_dst  = SWAP16(dport);
  ip->udp_dst  = SWAP16(dport);
  ip->udp_len  = SWAP16(8 + len);
  ip->udp_len  = SWAP16(8 + len);
  ip->udp_xsum = 0;
  ip->udp_xsum = 0;
  ip->ip_sum   = ~NetCksum((unsigned char *)ip, IP_HDR_SIZE_NO_UDP / 2);
  ip->ip_sum   = ~NetCksum((unsigned char *)ip, IP_HDR_SIZE_NO_UDP / 2);
Line 682... Line 667...
  ip_to_string(x, tmp);
  ip_to_string(x, tmp);
 
 
  printf(tmp);
  printf(tmp);
}
}
 
 
static unsigned int i2a(char* dest,unsigned int x) {
static unsigned int i2a(char *dest, unsigned int x)
 
{
  register unsigned int tmp=x;
  register unsigned int tmp=x;
  register unsigned int len=0;
  register unsigned int len=0;
  if (x>=100) { *dest++=tmp/100+'0'; tmp=tmp%100; ++len; }
        if (x >= 100) {
  if (x>=10) { *dest++=tmp/10+'0'; tmp=tmp%10; ++len; }
                *dest++ = tmp / 100 + '0';
  *dest++=tmp+'0';
                tmp = tmp % 100;
 
                ++len;
 
        }
 
        if (x >= 10) {
 
                *dest++ = tmp / 10 + '0';
 
                tmp = tmp % 10;
 
                ++len;
 
        }
 
        *dest++ = tmp + '0';
  return len+1;
  return len+1;
}
}
 
 
char *inet_ntoa(unsigned long in) {
char *inet_ntoa(unsigned long in)
 
{
  static char buf[20];
  static char buf[20];
  unsigned int len;
  unsigned int len;
  unsigned char *ip=(unsigned char*)&in;
  unsigned char *ip=(unsigned char*)&in;
 
 
  len=i2a(buf,ip[0]); buf[len]='.'; ++len;
        len = i2a(buf, ip[0]);
  len+=i2a(buf+ len,ip[1]); buf[len]='.'; ++len;
        buf[len] = '.';
  len+=i2a(buf+ len,ip[2]); buf[len]='.'; ++len;
        ++len;
  len+=i2a(buf+ len,ip[3]); buf[len]=0;
        len += i2a(buf + len, ip[1]);
 
        buf[len] = '.';
 
        ++len;
 
        len += i2a(buf + len, ip[2]);
 
        buf[len] = '.';
 
        ++len;
 
        len += i2a(buf + len, ip[3]);
 
        buf[len] = 0;
  return buf;
  return buf;
}
}
 
 
unsigned long inet_aton(const char *cp)
unsigned long inet_aton(const char *cp)
{
{

powered by: WebSVN 2.1.0

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