summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHåkon Nessjøen <haakon.nessjoen@gmail.com>2011-11-13 06:03:15 +0100
committerHåkon Nessjøen <haakon.nessjoen@gmail.com>2011-11-13 06:03:15 +0100
commit3084b5ec02f72c7c420a5c6687a61df637005ceb (patch)
tree6f4a26605c36030a9aae85863f0594064275a000
parent632bc61784657ca10da047c38596bde9d336998f (diff)
downloadMAC-Telnet-3084b5ec02f72c7c420a5c6687a61df637005ceb.tar.gz
MAC-Telnet-3084b5ec02f72c7c420a5c6687a61df637005ceb.zip
Simplified checking of mac address (code cleanup)
-rw-r--r--interfaces.c5
-rw-r--r--interfaces.h1
-rw-r--r--macping.c3
-rw-r--r--mactelnet.c2
-rw-r--r--mactelnetd.c13
5 files changed, 10 insertions, 14 deletions
diff --git a/interfaces.c b/interfaces.c
index b37a162..86b9aab 100644
--- a/interfaces.c
+++ b/interfaces.c
@@ -82,6 +82,7 @@ static void net_update_mac(struct net_interface *interfaces, int max_devices) {
if (ioctl(tmpsock, SIOCGIFHWADDR, &ifr) == 0) {
/* Fetch mac address */
memcpy(interfaces[i].mac_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
+ interfaces[i].has_mac = 1;
}
}
@@ -136,11 +137,13 @@ int net_get_interfaces(struct net_interface *interfaces, int max_devices) {
}
#ifndef __linux__
{
+ unsigned char emptymac[] = {0, 0, 0, 0, 0, 0};
struct sockaddr_dl *sdl = (struct sockaddr_dl *)int_cursor->ifa_addr;
if (sdl->sdl_alen == ETH_ALEN) {
struct net_interface *interface = net_get_interface_ptr(interfaces, max_devices, int_cursor->ifa_name, 1);
- if (interface != NULL) {
+ if (interface != NULL && memcmp(interfaces->mac_addr, emptymac, ETH_ALEN) != 0) {
memcpy(interface->mac_addr, LLADDR(sdl), ETH_ALEN);
+ interface->has_mac = 1;
}
}
}
diff --git a/interfaces.h b/interfaces.h
index 8d556f1..afb3e72 100644
--- a/interfaces.h
+++ b/interfaces.h
@@ -32,6 +32,7 @@ struct net_interface {
#ifdef __linux__
int ifindex;
#endif
+ int has_mac;
int in_use;
};
diff --git a/macping.c b/macping.c
index a276701..63ca5eb 100644
--- a/macping.c
+++ b/macping.c
@@ -223,7 +223,6 @@ int main(int argc, char **argv) {
int waitforpacket;
struct timeval timestamp;
unsigned char pingdata[1500];
- unsigned char emptymac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
gettimeofday(&timestamp, NULL);
memcpy(pingdata, &timestamp, sizeof(timestamp));
@@ -238,7 +237,7 @@ int main(int argc, char **argv) {
break;
}
- if (memcmp(emptymac, interface->mac_addr, ETH_ALEN) == 0) {
+ if (!interface->has_mac) {
continue;
}
diff --git a/mactelnet.c b/mactelnet.c
index c3ed71d..ca27f15 100644
--- a/mactelnet.c
+++ b/mactelnet.c
@@ -357,7 +357,7 @@ static int find_interface() {
}
/* Ensure that we have mac-address for this interface */
- if (memcmp(interfaces[i].mac_addr, emptymac, ETH_ALEN) == 0) {
+ if (!interfaces[i].has_mac) {
close(testsocket);
continue;
}
diff --git a/mactelnetd.c b/mactelnetd.c
index 3380115..3690d38 100644
--- a/mactelnetd.c
+++ b/mactelnetd.c
@@ -200,18 +200,15 @@ static int find_socket(unsigned char *mac) {
}
static void setup_sockets() {
- unsigned char emptymac[ETH_ALEN];
int i;
- bzero(emptymac, ETH_ALEN);
-
if (net_get_interfaces(interfaces, MAX_INTERFACES) <= 0) {
fprintf(stderr, "Error: No suitable devices found\n");
exit(1);
}
for (i = 0; i < MAX_INTERFACES; ++i) {
- if (interfaces[i].in_use == 0 || memcmp(interfaces[i].mac_addr, emptymac, ETH_ALEN) == 0) {
+ if (interfaces[i].in_use == 0 || !interfaces[i].has_mac) {
continue;
}
@@ -746,7 +743,6 @@ static void print_version() {
void mndp_broadcast() {
struct mt_packet pdata;
struct utsname s_uname;
- unsigned char emptymac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
int i;
unsigned int uptime;
#ifdef __linux__
@@ -774,7 +770,7 @@ void mndp_broadcast() {
struct net_interface *interface = &interfaces[i];
struct mt_mndp_hdr *header = (struct mt_mndp_hdr *)&(pdata.data);
- if (interfaces[i].in_use == 0 || memcmp(emptymac, interfaces[i].mac_addr, ETH_ALEN) == 0) {
+ if (interfaces[i].in_use == 0 || interfaces[i].has_mac == 0) {
continue;
}
@@ -932,10 +928,7 @@ int main (int argc, char **argv) {
syslog(LOG_NOTICE, "Bound to %s:%d", inet_ntoa(si_me.sin_addr), sourceport);
for (i = 0; i < MAX_INTERFACES; ++i) {
- unsigned char emptymac[ETH_ALEN];
- bzero(emptymac, ETH_ALEN);
-
- if (interfaces[i].in_use && memcmp(interfaces[i].mac_addr, emptymac, ETH_ALEN) != 0) {
+ if (interfaces[i].in_use && interfaces[i].has_mac) {
struct ether_addr *mac = (struct ether_addr *)&(interfaces[i].mac_addr);
syslog(LOG_NOTICE, "Listening on %s for %16s\n", interfaces[i].name, ether_ntoa(mac));
interface_count++;