This is the mail archive of the ecos-discuss@sourceware.org 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]

network interfaces


Hi,

i have a function ( on Linux work well ) that get the list and the numbers
of network interfaces excluding the LOOPBACK INTERFACE, but on ecos this
dosn't work  :
for debugging i print the name of interface finded : printf ("scheda_lan :
%s\n",ifReq.ifr_name);
but "eth0" there isn't ...  and this function return me always 0 !

Ps: in Main () i do init_all_network_interfaces() !

************************************************  CODE
*********************************************

int ILibGetLocalIPAddressList(int** pp_int)
{
 char szBuffer[16*sizeof(struct ifreq)];
 struct ifconf ifConf;
 struct ifreq ifReq;
 int nResult;
 int LocalSock;
 struct sockaddr_in LocalAddr;
 int tempresults[16];
 int ctr=0;
 int i;

 // Create an unbound datagram socket to do the SIOCGIFADDR ioctl on.
 if ((LocalSock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
 {
  DEBUGSTATEMENT(printf("Can't do that\r\n"));
  //exit(1);
 }
 /// Get the interface configuration information...
 ifConf.ifc_len = sizeof szBuffer;
 ifConf.ifc_ifcu.ifcu_buf = (caddr_t)szBuffer;
 nResult = ioctl(LocalSock, SIOCGIFCONF, &ifConf);
 if (nResult < 0)
 {
  DEBUGSTATEMENT(printf("ioctl error\r\n"));
  //exit(1);
 }
 // Cycle through the list of interfaces looking for IP addresses.
 for (i = 0;(i < ifConf.ifc_len);)
 {
  struct ifreq *pifReq = (struct ifreq *)((caddr_t)ifConf.ifc_req + i);
  i += sizeof *pifReq;
  // See if this is the sort of interface we want to deal with.
  strcpy (ifReq.ifr_name, pifReq -> ifr_name);

  ------>>>>    printf ("scheda_lan : %s\n",ifReq.ifr_name);

  if (ioctl (LocalSock, SIOCGIFFLAGS, &ifReq) < 0)
  {
   DEBUGSTATEMENT(printf("Can't get flags\r\n"));
   //exit(1);
  }
  /* Skip loopback, point-to-point and down interfaces, */
  /* except don't skip down interfaces */
  /* if we're trying to get a list of configurable interfaces.
  if ((ifReq.ifr_flags & IFF_LOOPBACK) || (!(ifReq.ifr_flags & IFF_UP)))
  {
   continue;
  }
  if (pifReq -> ifr_addr.sa_family == AF_INET)
  {
   /* Get a pointer to the address...
   memcpy (&LocalAddr, &pifReq -> ifr_addr, sizeof pifReq -> ifr_addr);
   if (LocalAddr.sin_addr.s_addr != htonl (INADDR_LOOPBACK))
   {
    tempresults[ctr] = LocalAddr.sin_addr.s_addr;
    ++ctr;
   }
  }
 }

 close(LocalSock);
 *pp_int = (int*)malloc(sizeof(int)*(ctr));
 memcpy(*pp_int,tempresults,sizeof(int)*ctr);

 return(ctr);
#endif
}


Thanks Michele


-- 
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]