This is the mail archive of the ecos-discuss@sources.redhat.com mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: ARP Support in eCos Applications


On Wednesday 13 October 2004 06:38, Manoj Abraham wrote:
> hello,
>          We are trying to compile an application code having ARP related
> system calls by replacing VxWorks calls by equivalent  eCos calls .But we
> didnt find any equivalent eCos system calls for arpAdd()add an entry to arp
> table  and mRouteAdd(). Though there is etharp_ip_input() in 
> packages/net/src/netif/etharp.c for ARP updation, that support is not
> provided when we build the eCos image for 'all packages' .That build
> includes FreeBSD suport rather than LWIP.When we tried to build eCos image
> with LWIP package added and FreeBSD packege unloaded,the build will stop
> with so many redefinition and undeclared warnings.Can anyone help me out to
> track this problem?Which all packages we need to include for arp support
> while building eCos image with configuartion tool?
>
> Thank you all in advance.
>
> Manoj K Abraham
> --
> India.com free e-mail - www.india.com.
> Check out our value-added Premium features, such as an extra 20MB for mail
> storage, POP3, e-mail forwarding, and ads-free mailboxes!
>
> Powered by Outblaze

Hello All,
May be you could have a look at the two following functions I wrote for eCOS 
builded with the FreeBSD stack port.
The first one add MAC adress 'dest' for IP adress set in 'gateway' on 
interface number 'index' in ARP table.
The second one remove it.

 ----------------------------------
note : IPSTRING and MACSTRING are my own defined as follows :
#define MACSTRING 18
#define IPSTRING 16

ether_print is a simple conversion fonction for mac adresses as shown :

char* ether_print(const u_char cp[ETHER_ADDR_LEN], char *etheraddr,const 
unsigned int len)
{
 snprintf(etheraddr,len,"%02x:%02x:%02x:%02x:%02x:
%02x",cp[0],cp[1],cp[2],cp[3],cp[4],cp[5]);
 return(etheraddr);
}
  ----------------------------------


Sorry, many comments are in french, hope it will be usefull anyway.


int AddMAC(const char* dest,const char* gateway,const int index)
{
 struct sockaddr_dl gway;  // Adresse passerelle
 struct sockaddr_in dst;  // Réseau/hote cible

 char ds[IPSTRING];
 struct ecos_rtentry *rt=NULL;  // Entrée dans la table de routage
 int s=0;                            // Descripteur de socket
 u_char cp[MACSTRING];   // Adresse MAC sous forme chaine

 memset(&gway,0,sizeof(struct sockaddr_dl));
 gway.sdl_family=AF_LINK; 
 gway.sdl_len=sizeof(struct sockaddr_dl);
 gway.sdl_index=index;
 gway.sdl_type=IFT_ETHER; 
 gway.sdl_alen=ETHER_ADDR_LEN;
 memcpy(gway.sdl_data,gateway,ETHER_ADDR_LEN);

 memset(&dst,0,sizeof(struct sockaddr_in));
 dst.sin_family=AF_INET;
 dst.sin_port=0;
 dst.sin_len=sizeof(struct sockaddr_in);
 dst.sin_addr.s_addr=inet_addr(dest);

 rt=(struct ecos_rtentry*)malloc(sizeof(struct ecos_rtentry));
 if (!rt)
 {
  LOG("AddMAC",LOG_ERR,"Erreur d'allocation d'une 
route :'%s'",strerror(errno));
  return(-1);
 }
 memset(rt,0,sizeof(struct ecos_rtentry));
 rt->rt_flags|=RTF_LLINFO;
 rt->rt_flags|=RTF_HOST; // Host entry
 memcpy(&(rt->rt_gateway), &gway, sizeof(struct sockaddr));
 memcpy(&(rt->rt_dst), &dst, sizeof(struct sockaddr_in));

 rt->rt_flags|=RTF_UP; // Route utilisable
// rt->rt_flags|=RTF_WAS_CLONED;
 //rt->rt_use=0;
 rt->rt_dev=NULL;
 rt->rt_metric=1;  // Reseau local
 strncpy(ds,inet_ntoa(dst.sin_addr),IPSTRING);
 ether_print(gateway,cp,MACSTRING);
 LOG("AddMAC",LOG_INFO,"Ajout d'une adresse MAC  pour '%s' :'%s' sur interface 
eth%d",ds,cp,index-1);
 s=socket(AF_INET,SOCK_DGRAM,0);
 if (s<0)
 {
  LOG("AddMAC",LOG_ERR,"Socket error :'%s'",strerror(errno));
  free(rt);
  return(-1);
 }
 if (ioctl(s,SIOCADDRT,rt)<0)
 {
  LOG("AddMAC",LOG_ERR,"Ioctl error :'%s'",strerror(errno));
  switch (errno)
  {
   case EINVAL:LOG("AddMAC",LOG_ERR,"Invalid Command or agument");
   break;
   case ENOTTY:LOG("AddMAC",LOG_ERR,"Not a device");
   break;
   case EBADF:LOG("AddMAC",LOG_ERR,"Bad File descriptor");
   break;
   case EEXIST:LOG("AddMAC",LOG_ERR,"Already existing");
    break;
   default:LOG("AddMAC",LOG_ERR,"Unknown error 
%d:'%s'",errno,strerror(errno));
  }
  free(rt);
  close(s);
  return (-1);
 }
 LOG("AddMAC",LOG_INFO,"ARP entry added");
 free(rt);
 close(s);
 return(0);
}

//--------------------------------------------------------------------------------------------
// Permet de supprimer une adresse MAC de la table ARP
//--------------------------------------------------------------------------------------------
int DelMAC(const char* dest,const char* gateway,const int index)
{
 struct sockaddr_dl gway;   // Adresse passerelle
 struct sockaddr_in dst;   // Réseau/hote cible

 char ds[IPSTRING];
 struct ecos_rtentry *rt=NULL;  // Entrée dans la table de routage
 int s=0;    // Descripteur de socket
 u_char cp[MACSTRING];

 memset(&gway,0,sizeof(struct sockaddr_dl));
 gway.sdl_family=AF_LINK; 
 gway.sdl_len=sizeof(struct sockaddr_dl);
 gway.sdl_index=index;
 gway.sdl_type=IFT_ETHER;
 gway.sdl_alen=ETHER_ADDR_LEN;
 memcpy(gway.sdl_data,gateway,ETHER_ADDR_LEN);

 memset(&dst,0,sizeof(struct sockaddr_in));
 dst.sin_family=AF_INET;
 dst.sin_port=0;
 dst.sin_len=sizeof(struct sockaddr_in);
 dst.sin_addr.s_addr=inet_addr(dest);

 rt=(struct ecos_rtentry*)malloc(sizeof(struct ecos_rtentry));
 if (!rt)
 {
  LOG("DelMAC",LOG_ERR,"Erreur d'allocation d'une 
route :'%s'",strerror(errno));
  return(-1);
 }
 memset(rt,0,sizeof(struct ecos_rtentry));
 rt->rt_flags|=RTF_LLINFO;
 rt->rt_flags|=RTF_HOST; // Host entry
 memcpy(&(rt->rt_gateway), &gway, sizeof(struct sockaddr));
 memcpy(&(rt->rt_dst), &dst, sizeof(struct sockaddr_in));

 rt->rt_flags|=RTF_UP; // Route utilisable
 //rt->rt_use=0;
 rt->rt_dev=NULL;
 rt->rt_metric=1;  // Reseau local
 strncpy(ds,inet_ntoa(dst.sin_addr),IPSTRING);
 ether_print(gateway,cp,MACSTRING);
 LOG("DelMAC",LOG_INFO,"Suppression d'une adresse MAC  pour '%s' :'%s' de 
l'interface eth%d",ds,cp,index-1);
 s=socket(AF_INET,SOCK_DGRAM,0);
 if (s<0)
 {
  LOG("DelMAC",LOG_ERR,"Socket error %s",strerror(errno));
  free(rt);
  return(-1);
 }
 if (ioctl(s,SIOCDELRT,rt)<0)
 {
  LOG("DelMAC",LOG_ERR,"Ioctl error %s",strerror(errno));
  switch (errno)
  {
   case EINVAL:LOG("DelMAC",LOG_ERR,"Invalid Command or agument");
   break;
   case ENOTTY:LOG("DelMAC",LOG_ERR,"Not a device");
   break;
   case EBADF:LOG("DelMAC",LOG_ERR,"Bad File descriptor");
   break;
   case EEXIST:LOG("DelMAC",LOG_ERR,"Already existing");
    break;
   default:LOG("DelMAC",LOG_ERR,"Unknown error %d:%s",errno,strerror(errno));
  }
  free(rt);
  close(s);
  return (-1);
 }
 LOG("DelMAC",LOG_INFO,"ARP entry deleted");
 free(rt);
 close(s);
 return(0);
}


--
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]