summaryrefslogtreecommitdiff
path: root/devices.c
diff options
context:
space:
mode:
authorHåkon Nessjøen <haakon.nessjoen@gmail.com>2010-09-28 00:07:49 +0200
committerHåkon Nessjøen <haakon.nessjoen@gmail.com>2010-09-28 00:07:49 +0200
commitc90ce96f3f86404ee85b390a3ed4b575a257423d (patch)
treeb779d3773e2a69a4b46a2c58e72a8e5971bed73f /devices.c
parent86460e9be93df399286e63b8142853d6e5aee5a9 (diff)
downloadMAC-Telnet-c90ce96f3f86404ee85b390a3ed4b575a257423d.tar.gz
MAC-Telnet-c90ce96f3f86404ee85b390a3ed4b575a257423d.zip
Comment cleanup
Diffstat (limited to 'devices.c')
-rw-r--r--devices.c34
1 files changed, 32 insertions, 2 deletions
diff --git a/devices.c b/devices.c
index 9293783..b7c1254 100644
--- a/devices.c
+++ b/devices.c
@@ -1,3 +1,21 @@
+/*
+ Mac-Telnet - Connect to RouterOS clients via MAC address
+ Copyright (C) 2010, Håkon Nessjøen <haakon.nessjoen@gmail.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along
+ with this program; if not, write to the Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
@@ -7,6 +25,8 @@
#include <sys/ioctl.h>
#include <net/if.h>
+/* Functions using NETDEVICE api */
+
int getDeviceIndex(int sockfd, unsigned char *deviceName) {
struct ifreq ifr;
@@ -15,6 +35,7 @@ int getDeviceIndex(int sockfd, unsigned char *deviceName) {
return -1;
}
+ /* Return interface index */
return ifr.ifr_ifindex;
}
@@ -26,8 +47,8 @@ int getDeviceMAC(const int sockfd, const unsigned char *deviceName, unsigned cha
return -1;
}
+ /* Fetch mac address */
memcpy(mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
-
return 1;
}
@@ -36,26 +57,35 @@ int getDeviceIp(const int sockfd, const unsigned char *deviceName, struct sockad
struct ifreq *ifr;
int i,numDevices;
+ /*
+ * Do a initial query without allocating ifreq structs
+ * to count the number of ifreq structs to allocate memory for
+ */
memset(&ifc, 0, sizeof(ifc));
if (ioctl(sockfd, SIOCGIFCONF, &ifc) != 0) {
return -1;
}
+ /*
+ * Allocate memory for interfaces, multiply by two in case
+ * the number of interfaces has increased since last ioctl
+ */
if ((ifr = malloc(ifc.ifc_len * 2)) == NULL) {
perror("malloc");
exit(1);
}
ifc.ifc_req = ifr;
-
if (ioctl(sockfd, SIOCGIFCONF, &ifc) != 0) {
free(ifr);
return -1;
}
+ /* Iterate through all devices, searching for interface */
numDevices = ifc.ifc_len / sizeof(struct ifreq);
for (i = 0; i < numDevices; ++i) {
if (strcmp(ifr[i].ifr_name, deviceName) == 0) {
+ /* Fetch IP for found interface */
memcpy(ip, &(ifr[i].ifr_addr), sizeof(ip));
free(ifr);
return 1;