Skip to content

Commit

Permalink
issue zeromq#25 try to guess wifi nic
Browse files Browse the repository at this point in the history
  • Loading branch information
miniway committed Nov 5, 2012
1 parent 8a836b4 commit 5eecebb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
6 changes: 6 additions & 0 deletions C/configure.in
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ esac
AC_HEADER_STDC
AC_CHECK_HEADERS(errno.h arpa/inet.h netinet/tcp.h netinet/in.h stddef.h \
stdlib.h string.h sys/socket.h sys/time.h unistd.h limits.h)
AC_CHECK_HEADERS([net/if.h net/if_media.h linux/wireless.h], [], [],
[
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
])

# Checks for typedefs, structures, and compiler characteristics.
AC_HEADER_STDBOOL
Expand Down
44 changes: 44 additions & 0 deletions C/src/zre_udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@

#include <czmq.h>
#include "../include/zre.h"
#include "platform.h"

#ifdef HAVE_LINUX_WIRELESS_H
# include <linux/wireless.h>
#else
# ifdef HAVE_NET_IF_H
# include <net/if.h>
# endif
# ifdef HAVE_NET_IF_MEDIA_H
# include <net/if_media.h>
# endif
#endif

// -----------------------------------------------------------------
// UDP instance
Expand Down Expand Up @@ -81,6 +93,35 @@ s_handle_io_error (char *reason)
}
}

// check if given NIC name is wireless
static bool
s_wireless_nic (const char* name)
{
int sock = 0;
bool result = FALSE;

if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
return FALSE;
}
# ifdef SIOCGIFMEDIA
struct ifmediareq ifmr;

memset (&ifmr, 0, sizeof (struct ifmediareq));
strlcpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
if (ioctl(sock, SIOCGIFMEDIA, (caddr_t) &ifmr) != -1) {
result = IFM_TYPE (ifmr.ifm_current) == IFM_IEEE80211;
}
# elif defined(SIOCGIWNAME)
struct iwreq wrq;

strncpy (wrq.ifr_name, name, IFNAMSIZ);
if (ioctl(sock, SIOCGIWNAME, (caddr_t) &wrq) != -1) {
result = TRUE;
}
# endif
close(sock);
return result;
}

// -----------------------------------------------------------------
// Constructor
Expand Down Expand Up @@ -132,6 +173,9 @@ zre_udp_new (int port_nbr)
self->address = *(struct sockaddr_in *) interface->ifa_addr;
self->broadcast = *(struct sockaddr_in *) interface->ifa_broadaddr;
self->broadcast.sin_port = htons (self->port_nbr);

if (s_wireless_nic (interface->ifa_name))
break;
}
interface = interface->ifa_next;
}
Expand Down

0 comments on commit 5eecebb

Please sign in to comment.