This is the mail archive of the cygwin mailing list for the Cygwin 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]

Anamoly with ioctl() in cygwin 1.7.10


Hello,

I'm new to cygwin and ran into an anamoly with calling ioctl() that
I've not experienced on Linux. It appears that ioctl() behaves as
expected when it is called from the main thread; however, it does not
when called from a thread created by the main thread. Is this expected
behavior when using cygwin?

I've added a sample program to demonstrate the anomaly. The call to
listInterfaces() from main() will function properly. The call to
listInterfaces() from handlePackets() will fail at ioctl(sck,
SIOCGIFCONF, &ifc); however, the errno result reported by perror is
"No error".

Regards,
LC

#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <pthread.h>

int listInterfaces(void);
void* handlePackets (void* data);

int main(void)
{
   pthread_t pktHandlerThreadId;
   pthread_mutex_t pktMutex;

   printf("Result from main thread:\n");
   listInterfaces();
   pthread_mutex_lock(&pktMutex);
   pthread_create(&pktHandlerThreadId, NULL, &handlePackets, &pktMutex);
   pthread_mutex_unlock(&pktMutex);
   pthread_join(pktHandlerThreadId, NULL); //Wait until Event Handler
thread exits..
   printf("\nProgram is exiting.\n");
   return 0;
}


void * handlePackets(void* data)
{
  	pthread_mutex_t *pktMutex;
  	pktMutex = (pthread_mutex_t *)data;
  	pthread_mutex_lock(pktMutex);
  	printf("\nResult from packet handler thread:\n");
  	pktMutex = (pthread_mutex_t *)data;
    listInterfaces();
  	pthread_mutex_unlock(pktMutex);
    return NULL;
}

int listInterfaces(void)
{
	int retVal =0;
    /*
      Example code to obtain IP and MAC for all available interfaces on Linux.
      by Adam Pierce <adam@doctort.org>

    http://www.doctort.org/adam/
    */
    char          buf[1024];
    struct ifconf ifc;
    struct ifreq *ifr;
    int           sck;
    int           nInterfaces;
    int           i;

    /* Get a socket handle. */
    sck = socket(AF_INET, SOCK_DGRAM, 0);
    if(sck < 0)
    {
    	perror("socket");
    	return -1;
    }

    /* Query available interfaces. */
    ifc.ifc_len = sizeof(buf);
    ifc.ifc_buf = buf;
    if(ioctl(sck, SIOCGIFCONF, &ifc) < 0)
    {
    	perror("ioctl(SIOCGIFCONF)");
		return -1;
    }

    /* Iterate through the list of interfaces. */
    ifr         = ifc.ifc_req;
    nInterfaces = ifc.ifc_len / sizeof(struct ifreq);
    for(i = 0; i < nInterfaces; i++)
    {
    	struct ifreq *item = &ifr[i];
		/* Show the device name and IP address */
		printf("%s: IP %s",
			   item->ifr_name,
			   inet_ntoa(((struct sockaddr_in *)&item->ifr_addr)->sin_addr));

		/* Get the MAC address */
		if(ioctl(sck, SIOCGIFHWADDR, item) < 0)
		{
			perror("ioctl(SIOCGIFHWADDR)");
			return -1;
		}

		/* Get the broadcast address (added by Eric) */
		if(ioctl(sck, SIOCGIFBRDADDR, item) >= 0)
				printf(", BROADCAST %s", inet_ntoa(((struct sockaddr_in
*)&item->ifr_broadaddr)->sin_addr));
		printf("\n");
    }
	return retVal;
}

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple


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