Alessio Caiazza avatar Alessio Caiazza committed ad6a178

OSX support

Comments (0)

Files changed (2)

 DEPS = 
 OBJ = socket.o main.o gs105e.o shell.o shell_ip.o shell_vlan.o shell_sys.o shell_port.o
 
+ifeq ($(OS),Windows_NT)
+	CFLAGS += -D WIN32
+	ifeq ($(PROCESSOR_ARCHITECTURE),AMD64)
+		CFLAGS += -D AMD64
+	endif
+	ifeq ($(PROCESSOR_ARCHITECTURE),x86)
+		CFLAGS += -D IA32
+	endif
+else
+	UNAME_S := $(shell uname -s)
+	ifeq ($(UNAME_S),Linux)
+		CFLAGS += -D LINUX
+	endif
+	ifeq ($(UNAME_S),Darwin)
+		CFLAGS += -D OSX
+	endif
+	UNAME_P := $(shell uname -p)
+	ifeq ($(UNAME_P),x86_64)
+		CFLAGS += -D AMD64
+	endif
+	ifneq ($(filter %86,$(UNAME_P)),)
+		CFLAGS += -D IA32
+	endif
+	ifneq ($(filter arm%,$(UNAME_P)),)
+		CFLAGS += -D ARM
+	endif
+endif
+
+
+
 %.o: %.c $(DEPS)
 	$(CC) -c -o $@ $< $(CFLAGS)
 
+#ifdef OSX
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#include <net/if_dl.h>
+#endif
 #include <arpa/inet.h>
 #include <sys/socket.h>
 #include <stdlib.h>
 #include <net/if.h>
 #include "socket.h"
 #include <sys/select.h>
+#include <errno.h>
+
 int sock;
 
 
 struct sockaddr_in sockAddr;
 
-unsigned char * get_mac(const char *intf) {
+#ifdef LINUX
+unsigned char * linux_get_mac(const char *intf) {
         unsigned char * mac = (unsigned char *) malloc(sizeof(unsigned char *) * 6);
         struct ifreq iface;
         unsigned char i;
         
         return mac;
 }
+#endif
+
+#ifdef OSX
+unsigned char * bsd_get_mac(const char *intf) {
+        int32_t     mib[6];
+        size_t     len;
+        char      *buf;
+        unsigned char   *mac;
+        struct if_msghdr  *ifm;
+        struct sockaddr_dl  *sdl;
+
+        mib[0] = CTL_NET;
+        mib[1] = AF_ROUTE;
+        mib[2] = 0;
+        mib[3] = AF_LINK;
+        mib[4] = NET_RT_IFLIST;
+        if ((mib[5] = if_nametoindex(intf)) == 0) {
+                perror("if_nametoindex error");
+                exit(2);
+        }
+
+        if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
+                perror("sysctl 1 error");
+                exit(3);
+        }
+
+        if ((buf = malloc(len)) == NULL) {
+                perror("malloc error");
+                exit(4);
+        }
+
+        if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
+                perror("sysctl 2 error");
+                exit(5);
+        }
+
+        ifm = (struct if_msghdr *)buf;
+        sdl = (struct sockaddr_dl *)(ifm + 1);
+        mac = (unsigned char *)LLADDR(sdl);
+        return mac;
+}
+#endif
+
+unsigned char * get_mac(const char *intf) {
+#ifdef LINUX
+        return linux_get_mac(intf);
+#endif
+#ifdef OSX
+        return bsd_get_mac(intf);
+#endif
+}
 
 void init_socket() {
         sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.