From a250b40c80e2ec055091291a5a753b0e28c4d893 Mon Sep 17 00:00:00 2001 From: Dan Sumorok Date: Tue, 30 Apr 2013 20:46:31 -0400 Subject: [PATCH 1/5] Added new etherlave network option for OS X. --- BasiliskII/src/MacOSX/etherslavetool.c | 277 +++++++++++++++++++++++++ BasiliskII/src/MacOSX/runtool.m | 79 +++++++ BasiliskII/src/Unix/.gitignore | 1 + BasiliskII/src/Unix/Makefile.in | 7 + BasiliskII/src/Unix/configure.ac | 11 + BasiliskII/src/Unix/ether_unix.cpp | 182 +++++++++++++++- BasiliskII/src/Unix/prefs_unix.cpp | 3 + 7 files changed, 559 insertions(+), 1 deletion(-) create mode 100644 BasiliskII/src/MacOSX/etherslavetool.c create mode 100644 BasiliskII/src/MacOSX/runtool.m diff --git a/BasiliskII/src/MacOSX/etherslavetool.c b/BasiliskII/src/MacOSX/etherslavetool.c new file mode 100644 index 00000000..08a18d97 --- /dev/null +++ b/BasiliskII/src/MacOSX/etherslavetool.c @@ -0,0 +1,277 @@ +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include + +#include + +#include + +static int openBpf(char *ifname); +static int retreiveAuthInfo(void); +static int mainLoop(int sd); + +int main(int argc, char **argv) { + char *ifName; + int ret; + int sd; + + if(argc != 2) { + return 255; + } + + ifName = argv[1]; + + ret = retreiveAuthInfo(); + if(ret != 0) { + return 254; + } + + fflush(stdout); + + sd = openBpf(ifName); + if(sd < 0) { + return 253; + } + + fflush(stdout); + + ret = mainLoop(sd); + + close(sd); + + if(ret < 0) { + return 252; + } + + return 0; +} + +static int mainLoop(int sd) { + fd_set readSet; + char *outgoing, *incoming; + unsigned short *outLen; + unsigned short *inLen; + int inIndex, outIndex; + u_int blen = 0; + int ret; + int fret = 0; + struct bpf_hdr *hdr; + int pktLen; + int frameLen; + int pad; + + if(ioctl(sd, BIOCGBLEN, &blen) < 0) { + return -1; + } + + incoming = malloc(blen); + if(incoming == NULL) { + return -2; + } + + outgoing = malloc(blen); + if(outgoing == NULL) { + free(outgoing); + return -3; + } + + inIndex = 0; + outIndex = 0; + + outLen = (unsigned short *)outgoing; + + while(1) { + int i; + FD_ZERO(&readSet); + FD_SET(0, &readSet); + FD_SET(sd, &readSet); + + ret = select(sd + 1, &readSet, NULL, NULL, NULL); + if(ret < 0) { + fret = -4; + break; + } + + if(FD_ISSET(0, &readSet)) { + if(outIndex < 2) { + ret = read(0, outgoing + outIndex, 2-outIndex); + } else { + ret = read(0, outgoing + outIndex, *outLen - outIndex + 2); + } + + if(ret < 1) { + fret = -5; + break; + } + + outIndex += ret; + if(outIndex > 1) { + fflush(stdout); + + if((*outLen + 2) > blen) { + fret = -6; + break; + } + + if(outIndex == (*outLen + 2)) { + ret = write(sd, outLen + 1, *outLen); + if(ret != *outLen) { + fret = -7; + break; + } + outIndex = 0; + } + } + + } + + if(FD_ISSET(sd, &readSet)) { + int i; + + ret = read(sd, incoming, blen); + if(ret < 1) { + fret = -8; + break; + } + + hdr = (struct bpf_hdr *)incoming; + inLen = (unsigned short *)(incoming + 16); + + do { + pktLen = hdr->bh_caplen; + frameLen = pktLen + 18; + + if((pktLen < 0) || (frameLen > ret) || (frameLen < 0)) { + fret = -9; + break; + } + *inLen = pktLen; + + write(0, inLen, pktLen + 2); + if((frameLen & 0x03) == 0) { + pad = 0; + } else { + pad = 4 - (frameLen & 0x03); + } + + ret -= (frameLen + pad); + hdr = (struct bpf_hdr *)((unsigned char *)hdr + frameLen + pad); + inLen = (unsigned short *)((unsigned char *)hdr + 16); + } while (ret > 0); + + if(fret != 0) { + break; + } + } + } + + free(incoming); + free(outgoing); + + return fret; +} + +static int retreiveAuthInfo(void) { + AuthorizationRef aRef; + OSStatus status; + AuthorizationRights myRights; + AuthorizationRights *newRights; + AuthorizationItem *myItem; + AuthorizationItem myItems[1]; + AuthorizationItemSet *mySet; + int i; + + status = AuthorizationCopyPrivilegedReference(&aRef, kAuthorizationFlagDefaults); + if(status != errAuthorizationSuccess) { + return -1; + } + + status = AuthorizationCopyInfo(aRef, NULL, &mySet); + if(status != errAuthorizationSuccess) { + AuthorizationFree(aRef, kAuthorizationFlagDestroyRights); + return -1; + } + + myItems[0].name = "system.privilege.admin"; + myItems[0].valueLength = 0; + myItems[0].value = NULL; + myItems[0].flags = 0; + + myRights.count = sizeof (myItems) / sizeof (myItems[0]); + myRights.items = myItems; + + status = AuthorizationCopyRights(aRef, &myRights, NULL, + kAuthorizationFlagExtendRights, + &newRights); + if(status != errAuthorizationSuccess) { + AuthorizationFreeItemSet(mySet); + AuthorizationFree(aRef, kAuthorizationFlagDestroyRights); + return -2; + } + + AuthorizationFreeItemSet(newRights); + AuthorizationFreeItemSet(mySet); + AuthorizationFree(aRef, kAuthorizationFlagDestroyRights); + + return 0; +} + +static int openBpf(char *ifname) { + u_int blen = 0; + struct ifreq ifreq; + u_int arg; + + int sd = open("/dev/bpf2", O_RDWR); + + if(sd < 0) { + return -1; + } + + if(ioctl(sd, BIOCGBLEN, &blen) < 0) { + close(sd); + return -2; + } + + bzero(&ifreq, sizeof(ifreq)); + strncpy(ifreq.ifr_name, ifname, IFNAMSIZ); + + arg = 0; + if(ioctl(sd, BIOCSETIF, &ifreq) < 0) { + close(sd); + return -3; + } + + arg = 0; + if(ioctl(sd, BIOCSSEESENT, &arg) < 0) { + close(sd); + return -4; + } + + arg = 1; + if(ioctl(sd, BIOCPROMISC, &arg) < 0) { + close(sd); + return -5; + } + + arg = 1; + if(ioctl(sd, BIOCIMMEDIATE, &arg) < 0) { + close(sd); + return -6; + } + + return sd; +} diff --git a/BasiliskII/src/MacOSX/runtool.m b/BasiliskII/src/MacOSX/runtool.m new file mode 100644 index 00000000..64f39f9d --- /dev/null +++ b/BasiliskII/src/MacOSX/runtool.m @@ -0,0 +1,79 @@ +#include +#include + +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include + +#include + +#include + +FILE * runTool(const char *ifName); + +FILE * runTool(const char *ifName) { + OSStatus authStatus; + FILE *fp; + char *args[] = {"ethsheeptool", NULL, NULL}; + int ret; + const char *path; + + path = [[[NSBundle mainBundle] + pathForResource:@"etherslavetool" ofType: nil] UTF8String]; + + if(path == NULL) { + return NULL; + } + + AuthorizationFlags authFlags; + AuthorizationRef authRef; + AuthorizationItem authItems[1]; + AuthorizationRights authRights; + + args[1] = (char *)ifName; + + authFlags = kAuthorizationFlagExtendRights | + kAuthorizationFlagInteractionAllowed | + kAuthorizationFlagPreAuthorize; + + authItems[0].name = "system.privilege.admin"; + authItems[0].valueLength = 0; + authItems[0].value = NULL; + authItems[0].flags = 0; + + authRights.count = sizeof (authItems) / sizeof (authItems[0]); + authRights.items = authItems; + + authStatus = AuthorizationCreate(&authRights, + kAuthorizationEmptyEnvironment, + authFlags, + &authRef); + + if(authStatus != errAuthorizationSuccess) { + fprintf(stderr, "%s: AuthorizationCreate() failed.\n", __func__); + return NULL; + } + + authStatus = AuthorizationExecuteWithPrivileges(authRef, + path, + kAuthorizationFlagDefaults, + args + 1, + &fp); + + if(authStatus != errAuthorizationSuccess) { + fprintf(stderr, "%s: AuthorizationExecWithPrivileges() failed.\n", __func__); + return NULL; + } + + return fp; +} diff --git a/BasiliskII/src/Unix/.gitignore b/BasiliskII/src/Unix/.gitignore index cc9e93f6..6d074222 100644 --- a/BasiliskII/src/Unix/.gitignore +++ b/BasiliskII/src/Unix/.gitignore @@ -1,6 +1,7 @@ # Object files obj/* BasiliskII +etherslavetool # Autotools generated files Makefile diff --git a/BasiliskII/src/Unix/Makefile.in b/BasiliskII/src/Unix/Makefile.in index e50326a8..57a1c9d9 100644 --- a/BasiliskII/src/Unix/Makefile.in +++ b/BasiliskII/src/Unix/Makefile.in @@ -74,6 +74,10 @@ CXXFLAGS += $(GUI_CFLAGS) LIBS += $(GUI_LIBS) endif +ifeq (@MACOSX_ETHERSLAVE@,yes) +PROGS += etherslavetool +endif + ## Rules .PHONY: modules install installdirs uninstall mostlyclean clean distclean depend dep .SUFFIXES: @@ -138,6 +142,9 @@ $(GUI_APP)_app: $(GUI_APP) ../MacOSX/Info.plist ../MacOSX/$(APP).icns mkdir -p $(GUI_APP_APP)/Contents/Resources ./cpr.sh ../MacOSX/$(APP).icns $(GUI_APP_APP)/Contents/Resources/$(GUI_APP).icns +etherslavetool: ../MacOSX/etherslavetool.c + $(CC) $(CPPFLAGS) $(DEFS) $(CFLAGS) $(LIBS) $< -o $@ + modules: cd Linux/NetDriver; make diff --git a/BasiliskII/src/Unix/configure.ac b/BasiliskII/src/Unix/configure.ac index 62d45f57..93ff7aac 100644 --- a/BasiliskII/src/Unix/configure.ac +++ b/BasiliskII/src/Unix/configure.ac @@ -21,6 +21,9 @@ AC_ARG_ENABLE(standalone-gui,[ --enable-standalone-gui enable a standalone GUI dnl Mac OS X GUI. AC_ARG_ENABLE(macosx-gui, [ --enable-macosx-gui enable Mac OS X GUI [default=no]], [WANT_MACOSX_GUI=$enableval], [WANT_MACOSX_GUI=no]) +dnl Mac OS X etherslave support +AC_ARG_ENABLE(macosx-etherslave, [ --enable-macosx-etherslave enable Mac OS X Sound [default=no]], [WANT_MACOSX_ETHERSLAVE=$enableval], [WANT_MACOSX_ETHERSLAVE=no]) + dnl Video options. AC_ARG_ENABLE(xf86-dga, [ --enable-xf86-dga use the XFree86 DGA extension [default=yes]], [WANT_XF86_DGA=$enableval], [WANT_XF86_DGA=yes]) AC_ARG_ENABLE(xf86-vidmode, [ --enable-xf86-vidmode use the XFree86 VidMode extension [default=yes]], [WANT_XF86_VIDMODE=$enableval], [WANT_XF86_VIDMODE=yes]) @@ -724,6 +727,14 @@ else EXTRASYSSRCS="$EXTRASYSSRCS main_unix.cpp prefs_unix.cpp" fi +if [[ "x$WANT_MACOSX_ETHERSLAVE" = "xyes" ]]; then + EXTRASYSSRCS="$EXTRASYSSRCS ../MacOSX/runtool.m" + LIBS="$LIBS -framework Security" + AC_DEFINE(ENABLE_MACOSX_ETHERSLAVE, 1, [Define if supporting "etherslave" network device.]) +fi + +AC_SUBST(MACOSX_ETHERSLAVE, $WANT_MACOSX_ETHERSLAVE) + dnl SDL overrides if [[ "x$WANT_SDL" = "xyes" ]]; then AC_DEFINE(USE_SDL, 1, [Define to enble SDL support]) diff --git a/BasiliskII/src/Unix/ether_unix.cpp b/BasiliskII/src/Unix/ether_unix.cpp index 526ee29c..a2c07d2a 100644 --- a/BasiliskII/src/Unix/ether_unix.cpp +++ b/BasiliskII/src/Unix/ether_unix.cpp @@ -41,6 +41,12 @@ #endif #include #include + +#ifdef ENABLE_MACOSX_ETHERSLAVE +#include +#include +#endif + #include #include #include @@ -93,9 +99,17 @@ enum { NET_IF_SHEEPNET, NET_IF_ETHERTAP, NET_IF_TUNTAP, - NET_IF_SLIRP + NET_IF_SLIRP, + NET_IF_ETHERSLAVE }; + +#ifdef ENABLE_MACOSX_ETHERSLAVE +extern "C" { + extern FILE * runTool(const char *ifName); +} +#endif + // Constants #if ENABLE_TUNTAP static const char ETHERCONFIG_FILE_NAME[] = DATADIR "/tunconfig"; @@ -122,6 +136,11 @@ static uint8 ether_addr[6]; // Our Ethernet address const bool ether_driver_opened = true; // Flag: is the MacOS driver opened? #endif + +#ifdef ENABLE_MACOSX_ETHERSLAVE +static uint8 packet_buffer[2048]; +#endif + // Attached network protocols, maps protocol type to MacOS handler address static map net_protocols; @@ -135,6 +154,11 @@ static void ether_do_interrupt(void); static void slirp_add_redirs(); static int slirp_add_redir(const char *redir_str); +#ifdef ENABLE_MACOSX_ETHERSLAVE +static int getmacaddress(const char* dev, unsigned char *addr); +static bool openEtherSlave(const char *ifName); +static int readpacket(void); +#endif /* * Start packet reception thread @@ -235,6 +259,9 @@ bool ether_init(void) // Do nothing if no Ethernet device specified const char *name = PrefsFindString("ether"); +#ifdef ENABLE_MACOSX_ETHERSLAVE + const char *slaveDev = PrefsFindString("etherslavedev"); +#endif if (name == NULL) return false; @@ -249,6 +276,10 @@ bool ether_init(void) #ifdef HAVE_SLIRP else if (strcmp(name, "slirp") == 0) net_if_type = NET_IF_SLIRP; +#endif +#ifdef ENABLE_MACOSX_ETHERSLAVE + else if (strcmp(name, "etherslave") == 0) + net_if_type = NET_IF_ETHERSLAVE; #endif else net_if_type = NET_IF_SHEEPNET; @@ -300,6 +331,14 @@ bool ether_init(void) case NET_IF_SHEEPNET: strcpy(dev_name, "/dev/sheep_net"); break; +#ifdef ENABLE_MACOSX_ETHERSLAVE + case NET_IF_ETHERSLAVE: + if(slaveDev == NULL) { + WarningAlert("etherslavedev not defined in preferences."); + return false; + } + return openEtherSlave(slaveDev); +#endif } if (net_if_type != NET_IF_SLIRP) { fd = open(dev_name, O_RDWR); @@ -750,6 +789,21 @@ static int16 ether_do_write(uint32 arg) write(slirp_input_fd, packet, len); return noErr; } else +#endif +#ifdef ENABLE_MACOSX_ETHERSLAVE + if (net_if_type == NET_IF_ETHERSLAVE) { + unsigned short pktlen; + + pktlen = len; + if(write(fd, &pktlen, 2) < 2) { + return excessCollsns; + } + + if(write(fd, packet, len) < len) { + return excessCollsns; + } + return noErr; + } else #endif if (write(fd, packet, len) < 0) { D(bug("WARNING: Couldn't transmit packet\n")); @@ -884,6 +938,13 @@ static void *receive_func(void *arg) if (res <= 0) break; +#ifdef ENABLE_MACOSX_ETHERSLAVE + if (net_if_type == NET_IF_ETHERSLAVE) { + if(readpacket() < 1) { + break; + } + } +#endif if (ether_driver_opened) { // Trigger Ethernet interrupt D(bug(" packet received, triggering Ethernet interrupt\n")); @@ -923,6 +984,18 @@ void ether_do_interrupt(void) ether_udp_read(packet, length, &from); } else +#endif +#ifdef ENABLE_MACOSX_ETHERSLAVE + if (net_if_type == NET_IF_ETHERSLAVE) { + unsigned short *pktlen; + uint32 p = packet; + + pktlen = (unsigned short *)packet_buffer; + length = *pktlen; + memcpy(Mac2HostAddr(packet), pktlen + 1, length); + ether_dispatch_packet(p, length); + break; + } else #endif { @@ -1049,3 +1122,110 @@ static int slirp_add_redir(const char *redir_str) WarningAlert(str); return -1; } + +#ifdef ENABLE_MACOSX_ETHERSLAVE +static int getmacaddress(const char* dev, unsigned char *addr) +{ + struct ifaddrs *ifaddrs, *next; + int ret = -1; + struct sockaddr_dl *sa; + + if(getifaddrs(&ifaddrs) != 0) { + perror("getifaddrs"); + return -1; + } + + next = ifaddrs; + while(next != NULL) { + switch(next->ifa_addr->sa_family) { + case AF_LINK: + if(!strcmp(dev, next->ifa_name)) { + sa = (struct sockaddr_dl *)next->ifa_addr; + memcpy(addr, LLADDR(sa), 6); + ret = 0; + } + break; + default: + break; + } + next = next->ifa_next; + } + + freeifaddrs(ifaddrs); + + return ret; +} + +static bool openEtherSlave(const char *ifName) +{ + FILE *fp; + char str[64]; + + str[sizeof(str)-1] = '\0'; + + if(getmacaddress(ifName, ether_addr) != 0) { + snprintf(str, sizeof(str)-1, "Unable to find interface %s.", + ifName); + WarningAlert(str); + return false; + } + + fp = runTool(ifName); + if(fp == NULL) { + snprintf(str, sizeof(str)-1, "Unable to run ether slave helper tool."); + WarningAlert(str); + return false; + } + + fd = dup(fileno(fp)); + fclose(fp); + + if(start_thread() == false) { + close(fd); + fd = -1; + return false; + } + + return true; +} + +static int readpacket() +{ + int index; + unsigned short *pktLen; + int ret = -1; + + pktLen = (unsigned short *)packet_buffer; + + index = 0; + while(1) { + if(index < 2) { + ret = read(fd, packet_buffer + index, 2 - index); + } else { + ret = read(fd, packet_buffer + index, *pktLen - index + 2); + } + + if(ret < 1) { + fprintf(stderr, "%s: read() returned %d.\n", __func__, ret); + break; + } + + index += ret; + + if(index > 1) { + if(*pktLen > (sizeof(packet_buffer) + 2)) { + fprintf(stderr, "%s: pktLen (%d) too large.\n", __func__, *pktLen); + break; + } + + if(index == (*pktLen + 2)) { + ret = *pktLen; + break; + } + } + } + + return ret; +} + +#endif diff --git a/BasiliskII/src/Unix/prefs_unix.cpp b/BasiliskII/src/Unix/prefs_unix.cpp index a92cd640..43f70fb6 100644 --- a/BasiliskII/src/Unix/prefs_unix.cpp +++ b/BasiliskII/src/Unix/prefs_unix.cpp @@ -40,6 +40,9 @@ prefs_desc platform_prefs_items[] = { {"mixer", TYPE_STRING, false, "audio mixer device name"}, #ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION {"ignoresegv", TYPE_BOOLEAN, false, "ignore illegal memory accesses"}, +#endif +#ifdef ENABLE_MACOSX_ETHERSLAVE + {"etherslavedev", TYPE_STRING, false, "ethernet device for etherslave ethernet"}, #endif {"idlewait", TYPE_BOOLEAN, false, "sleep when idle"}, {NULL, TYPE_END, false, NULL} // End of list From 94b790728e192f12e7b8a023dcfeff44779ce35b Mon Sep 17 00:00:00 2001 From: Dan Sumorok Date: Wed, 1 May 2013 06:54:20 -0400 Subject: [PATCH 2/5] Added file-level comments. --- BasiliskII/src/MacOSX/etherslavetool.c | 23 +++++++++++++++++++++++ BasiliskII/src/MacOSX/runtool.m | 23 ++++++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/BasiliskII/src/MacOSX/etherslavetool.c b/BasiliskII/src/MacOSX/etherslavetool.c index 08a18d97..dfc4bec0 100644 --- a/BasiliskII/src/MacOSX/etherslavetool.c +++ b/BasiliskII/src/MacOSX/etherslavetool.c @@ -1,3 +1,26 @@ +/* + * etherslavetool.c - Reads and writes raw ethernet packets usng bpf + * interface. + * + * Copyright (C) 2010, Daniel Sumorok + * + * Basilisk II (C) 1997-2008 Christian Bauer + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + #include #include diff --git a/BasiliskII/src/MacOSX/runtool.m b/BasiliskII/src/MacOSX/runtool.m index 64f39f9d..925e50fc 100644 --- a/BasiliskII/src/MacOSX/runtool.m +++ b/BasiliskII/src/MacOSX/runtool.m @@ -1,3 +1,24 @@ +/* + * runtool.m - Run an external program as root for networking + * Copyright (C) 2010, Daniel Sumorok + * + * Basilisk II (C) 1997-2008 Christian Bauer + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + #include #include @@ -24,7 +45,7 @@ FILE * runTool(const char *ifName); FILE * runTool(const char *ifName) { OSStatus authStatus; FILE *fp; - char *args[] = {"ethsheeptool", NULL, NULL}; + char *args[] = {"etherslavetool", NULL, NULL}; int ret; const char *path; From 01ba04139fda8602e7c6daf85aa2f7624ed1e3a8 Mon Sep 17 00:00:00 2001 From: Dan Sumorok Date: Sat, 4 May 2013 20:36:11 -0400 Subject: [PATCH 3/5] Updated coding style. --- BasiliskII/src/MacOSX/etherslavetool.c | 126 ++++++++++++------------- BasiliskII/src/MacOSX/runtool.m | 53 +++++------ BasiliskII/src/Unix/ether_unix.cpp | 84 ++++++++--------- 3 files changed, 130 insertions(+), 133 deletions(-) diff --git a/BasiliskII/src/MacOSX/etherslavetool.c b/BasiliskII/src/MacOSX/etherslavetool.c index dfc4bec0..766c7968 100644 --- a/BasiliskII/src/MacOSX/etherslavetool.c +++ b/BasiliskII/src/MacOSX/etherslavetool.c @@ -43,160 +43,160 @@ #include -static int openBpf(char *ifname); -static int retreiveAuthInfo(void); -static int mainLoop(int sd); +static int open_bpf(char *ifname); +static int retreive_auth_info(void); +static int main_loop(int sd); int main(int argc, char **argv) { - char *ifName; + char *if_name; int ret; int sd; - if(argc != 2) { + if (argc != 2) { return 255; } - ifName = argv[1]; + if_name = argv[1]; - ret = retreiveAuthInfo(); - if(ret != 0) { + ret = retreive_auth_info(); + if (ret != 0) { return 254; } fflush(stdout); - sd = openBpf(ifName); - if(sd < 0) { + sd = open_bpf(if_name); + if (sd < 0) { return 253; } fflush(stdout); - ret = mainLoop(sd); + ret = main_loop(sd); close(sd); - if(ret < 0) { + if (ret < 0) { return 252; } return 0; } -static int mainLoop(int sd) { +static int main_loop(int sd) { fd_set readSet; char *outgoing, *incoming; - unsigned short *outLen; - unsigned short *inLen; - int inIndex, outIndex; + unsigned short *out_len; + unsigned short *in_len; + int in_index, out_index; u_int blen = 0; int ret; int fret = 0; struct bpf_hdr *hdr; - int pktLen; - int frameLen; + int pkt_len; + int frame_len; int pad; - if(ioctl(sd, BIOCGBLEN, &blen) < 0) { + if (ioctl(sd, BIOCGBLEN, &blen) < 0) { return -1; } incoming = malloc(blen); - if(incoming == NULL) { + if (incoming == NULL) { return -2; } outgoing = malloc(blen); - if(outgoing == NULL) { + if (outgoing == NULL) { free(outgoing); return -3; } - inIndex = 0; - outIndex = 0; + in_index = 0; + out_index = 0; - outLen = (unsigned short *)outgoing; + out_len = (unsigned short *)outgoing; - while(1) { + while (1) { int i; FD_ZERO(&readSet); FD_SET(0, &readSet); FD_SET(sd, &readSet); ret = select(sd + 1, &readSet, NULL, NULL, NULL); - if(ret < 0) { + if (ret < 0) { fret = -4; break; } - if(FD_ISSET(0, &readSet)) { - if(outIndex < 2) { - ret = read(0, outgoing + outIndex, 2-outIndex); + if (FD_ISSET(0, &readSet)) { + if (out_index < 2) { + ret = read(0, outgoing + out_index, 2-out_index); } else { - ret = read(0, outgoing + outIndex, *outLen - outIndex + 2); + ret = read(0, outgoing + out_index, *out_len - out_index + 2); } - if(ret < 1) { + if (ret < 1) { fret = -5; break; } - outIndex += ret; - if(outIndex > 1) { + out_index += ret; + if (out_index > 1) { fflush(stdout); - if((*outLen + 2) > blen) { + if ((*out_len + 2) > blen) { fret = -6; break; } - if(outIndex == (*outLen + 2)) { - ret = write(sd, outLen + 1, *outLen); - if(ret != *outLen) { + if (out_index == (*out_len + 2)) { + ret = write(sd, out_len + 1, *out_len); + if (ret != *out_len) { fret = -7; break; } - outIndex = 0; + out_index = 0; } } } - if(FD_ISSET(sd, &readSet)) { + if (FD_ISSET(sd, &readSet)) { int i; ret = read(sd, incoming, blen); - if(ret < 1) { + if (ret < 1) { fret = -8; break; } hdr = (struct bpf_hdr *)incoming; - inLen = (unsigned short *)(incoming + 16); + in_len = (unsigned short *)(incoming + 16); do { - pktLen = hdr->bh_caplen; - frameLen = pktLen + 18; + pkt_len = hdr->bh_caplen; + frame_len = pkt_len + 18; - if((pktLen < 0) || (frameLen > ret) || (frameLen < 0)) { + if ((pkt_len < 0) || (frame_len > ret) || (frame_len < 0)) { fret = -9; break; } - *inLen = pktLen; + *in_len = pkt_len; - write(0, inLen, pktLen + 2); - if((frameLen & 0x03) == 0) { + write(0, in_len, pkt_len + 2); + if ((frame_len & 0x03) == 0) { pad = 0; } else { - pad = 4 - (frameLen & 0x03); + pad = 4 - (frame_len & 0x03); } - ret -= (frameLen + pad); - hdr = (struct bpf_hdr *)((unsigned char *)hdr + frameLen + pad); - inLen = (unsigned short *)((unsigned char *)hdr + 16); + ret -= (frame_len + pad); + hdr = (struct bpf_hdr *)((unsigned char *)hdr + frame_len + pad); + in_len = (unsigned short *)((unsigned char *)hdr + 16); } while (ret > 0); - if(fret != 0) { + if (fret != 0) { break; } } @@ -208,7 +208,7 @@ static int mainLoop(int sd) { return fret; } -static int retreiveAuthInfo(void) { +static int retreive_auth_info(void) { AuthorizationRef aRef; OSStatus status; AuthorizationRights myRights; @@ -219,12 +219,12 @@ static int retreiveAuthInfo(void) { int i; status = AuthorizationCopyPrivilegedReference(&aRef, kAuthorizationFlagDefaults); - if(status != errAuthorizationSuccess) { + if (status != errAuthorizationSuccess) { return -1; } status = AuthorizationCopyInfo(aRef, NULL, &mySet); - if(status != errAuthorizationSuccess) { + if (status != errAuthorizationSuccess) { AuthorizationFree(aRef, kAuthorizationFlagDestroyRights); return -1; } @@ -240,7 +240,7 @@ static int retreiveAuthInfo(void) { status = AuthorizationCopyRights(aRef, &myRights, NULL, kAuthorizationFlagExtendRights, &newRights); - if(status != errAuthorizationSuccess) { + if (status != errAuthorizationSuccess) { AuthorizationFreeItemSet(mySet); AuthorizationFree(aRef, kAuthorizationFlagDestroyRights); return -2; @@ -253,18 +253,18 @@ static int retreiveAuthInfo(void) { return 0; } -static int openBpf(char *ifname) { +static int open_bpf(char *ifname) { u_int blen = 0; struct ifreq ifreq; u_int arg; int sd = open("/dev/bpf2", O_RDWR); - if(sd < 0) { + if (sd < 0) { return -1; } - if(ioctl(sd, BIOCGBLEN, &blen) < 0) { + if (ioctl(sd, BIOCGBLEN, &blen) < 0) { close(sd); return -2; } @@ -273,25 +273,25 @@ static int openBpf(char *ifname) { strncpy(ifreq.ifr_name, ifname, IFNAMSIZ); arg = 0; - if(ioctl(sd, BIOCSETIF, &ifreq) < 0) { + if (ioctl(sd, BIOCSETIF, &ifreq) < 0) { close(sd); return -3; } arg = 0; - if(ioctl(sd, BIOCSSEESENT, &arg) < 0) { + if (ioctl(sd, BIOCSSEESENT, &arg) < 0) { close(sd); return -4; } arg = 1; - if(ioctl(sd, BIOCPROMISC, &arg) < 0) { + if (ioctl(sd, BIOCPROMISC, &arg) < 0) { close(sd); return -5; } arg = 1; - if(ioctl(sd, BIOCIMMEDIATE, &arg) < 0) { + if (ioctl(sd, BIOCIMMEDIATE, &arg) < 0) { close(sd); return -6; } diff --git a/BasiliskII/src/MacOSX/runtool.m b/BasiliskII/src/MacOSX/runtool.m index 925e50fc..261f3054 100644 --- a/BasiliskII/src/MacOSX/runtool.m +++ b/BasiliskII/src/MacOSX/runtool.m @@ -40,58 +40,57 @@ #include -FILE * runTool(const char *ifName); +FILE * run_tool(const char *ifName); -FILE * runTool(const char *ifName) { - OSStatus authStatus; +FILE * run_tool(const char *ifName) { + OSStatus auth_status; FILE *fp; char *args[] = {"etherslavetool", NULL, NULL}; int ret; const char *path; + AuthorizationFlags auth_flags; + AuthorizationRef auth_ref; + AuthorizationItem auth_items[1]; + AuthorizationRights auth_rights; path = [[[NSBundle mainBundle] pathForResource:@"etherslavetool" ofType: nil] UTF8String]; - if(path == NULL) { + if (path == NULL) { return NULL; } - AuthorizationFlags authFlags; - AuthorizationRef authRef; - AuthorizationItem authItems[1]; - AuthorizationRights authRights; - args[1] = (char *)ifName; - authFlags = kAuthorizationFlagExtendRights | + auth_flags = kAuthorizationFlagExtendRights | kAuthorizationFlagInteractionAllowed | kAuthorizationFlagPreAuthorize; - authItems[0].name = "system.privilege.admin"; - authItems[0].valueLength = 0; - authItems[0].value = NULL; - authItems[0].flags = 0; + auth_items[0].name = "system.privilege.admin"; + auth_items[0].valueLength = 0; + auth_items[0].value = NULL; + auth_items[0].flags = 0; - authRights.count = sizeof (authItems) / sizeof (authItems[0]); - authRights.items = authItems; + auth_rights.count = sizeof (auth_items) / sizeof (auth_items[0]); + auth_rights.items = auth_items; - authStatus = AuthorizationCreate(&authRights, - kAuthorizationEmptyEnvironment, - authFlags, - &authRef); + auth_status = AuthorizationCreate(&auth_rights, + kAuthorizationEmptyEnvironment, + auth_flags, + &auth_ref); - if(authStatus != errAuthorizationSuccess) { + if (auth_status != errAuthorizationSuccess) { fprintf(stderr, "%s: AuthorizationCreate() failed.\n", __func__); return NULL; } - authStatus = AuthorizationExecuteWithPrivileges(authRef, - path, - kAuthorizationFlagDefaults, - args + 1, - &fp); + auth_status = AuthorizationExecuteWithPrivileges(auth_ref, + path, + kAuthorizationFlagDefaults, + args + 1, + &fp); - if(authStatus != errAuthorizationSuccess) { + if (auth_status != errAuthorizationSuccess) { fprintf(stderr, "%s: AuthorizationExecWithPrivileges() failed.\n", __func__); return NULL; } diff --git a/BasiliskII/src/Unix/ether_unix.cpp b/BasiliskII/src/Unix/ether_unix.cpp index a2c07d2a..2c030c7b 100644 --- a/BasiliskII/src/Unix/ether_unix.cpp +++ b/BasiliskII/src/Unix/ether_unix.cpp @@ -106,7 +106,7 @@ enum { #ifdef ENABLE_MACOSX_ETHERSLAVE extern "C" { - extern FILE * runTool(const char *ifName); + extern FILE * run_tool(const char *if_name); } #endif @@ -155,9 +155,9 @@ static void slirp_add_redirs(); static int slirp_add_redir(const char *redir_str); #ifdef ENABLE_MACOSX_ETHERSLAVE -static int getmacaddress(const char* dev, unsigned char *addr); -static bool openEtherSlave(const char *ifName); -static int readpacket(void); +static int get_mac_address(const char* dev, unsigned char *addr); +static bool open_ether_slave(const char *if_name); +static int read_packet(void); #endif /* @@ -260,7 +260,7 @@ bool ether_init(void) // Do nothing if no Ethernet device specified const char *name = PrefsFindString("ether"); #ifdef ENABLE_MACOSX_ETHERSLAVE - const char *slaveDev = PrefsFindString("etherslavedev"); + const char *slave_dev = PrefsFindString("etherslavedev"); #endif if (name == NULL) return false; @@ -333,11 +333,11 @@ bool ether_init(void) break; #ifdef ENABLE_MACOSX_ETHERSLAVE case NET_IF_ETHERSLAVE: - if(slaveDev == NULL) { + if (slave_dev == NULL) { WarningAlert("etherslavedev not defined in preferences."); return false; } - return openEtherSlave(slaveDev); + return open_ether_slave(slave_dev); #endif } if (net_if_type != NET_IF_SLIRP) { @@ -792,14 +792,14 @@ static int16 ether_do_write(uint32 arg) #endif #ifdef ENABLE_MACOSX_ETHERSLAVE if (net_if_type == NET_IF_ETHERSLAVE) { - unsigned short pktlen; + unsigned short pkt_len; - pktlen = len; - if(write(fd, &pktlen, 2) < 2) { + pkt_len = len; + if (write(fd, &pkt_len, 2) < 2) { return excessCollsns; } - if(write(fd, packet, len) < len) { + if (write(fd, packet, len) < len) { return excessCollsns; } return noErr; @@ -940,7 +940,7 @@ static void *receive_func(void *arg) #ifdef ENABLE_MACOSX_ETHERSLAVE if (net_if_type == NET_IF_ETHERSLAVE) { - if(readpacket() < 1) { + if (read_packet() < 1) { break; } } @@ -987,12 +987,12 @@ void ether_do_interrupt(void) #endif #ifdef ENABLE_MACOSX_ETHERSLAVE if (net_if_type == NET_IF_ETHERSLAVE) { - unsigned short *pktlen; + unsigned short *pkt_len; uint32 p = packet; - pktlen = (unsigned short *)packet_buffer; - length = *pktlen; - memcpy(Mac2HostAddr(packet), pktlen + 1, length); + pkt_len = (unsigned short *)packet_buffer; + length = *pkt_len; + memcpy(Mac2HostAddr(packet), pkt_len + 1, length); ether_dispatch_packet(p, length); break; } else @@ -1124,22 +1124,22 @@ static int slirp_add_redir(const char *redir_str) } #ifdef ENABLE_MACOSX_ETHERSLAVE -static int getmacaddress(const char* dev, unsigned char *addr) +static int get_mac_address(const char* dev, unsigned char *addr) { struct ifaddrs *ifaddrs, *next; int ret = -1; struct sockaddr_dl *sa; - if(getifaddrs(&ifaddrs) != 0) { + if (getifaddrs(&ifaddrs) != 0) { perror("getifaddrs"); return -1; } next = ifaddrs; - while(next != NULL) { - switch(next->ifa_addr->sa_family) { + while (next != NULL) { + switch (next->ifa_addr->sa_family) { case AF_LINK: - if(!strcmp(dev, next->ifa_name)) { + if (!strcmp(dev, next->ifa_name)) { sa = (struct sockaddr_dl *)next->ifa_addr; memcpy(addr, LLADDR(sa), 6); ret = 0; @@ -1156,23 +1156,21 @@ static int getmacaddress(const char* dev, unsigned char *addr) return ret; } -static bool openEtherSlave(const char *ifName) +static bool open_ether_slave(const char *if_name) { FILE *fp; char str[64]; - str[sizeof(str)-1] = '\0'; - - if(getmacaddress(ifName, ether_addr) != 0) { - snprintf(str, sizeof(str)-1, "Unable to find interface %s.", - ifName); + if (get_mac_address(if_name, ether_addr) != 0) { + snprintf(str, sizeof(str), "Unable to find interface %s.", + if_name); WarningAlert(str); return false; } - fp = runTool(ifName); - if(fp == NULL) { - snprintf(str, sizeof(str)-1, "Unable to run ether slave helper tool."); + fp = run_tool(if_name); + if (fp == NULL) { + snprintf(str, sizeof(str), "Unable to run ether slave helper tool."); WarningAlert(str); return false; } @@ -1180,7 +1178,7 @@ static bool openEtherSlave(const char *ifName) fd = dup(fileno(fp)); fclose(fp); - if(start_thread() == false) { + if (start_thread() == false) { close(fd); fd = -1; return false; @@ -1189,37 +1187,37 @@ static bool openEtherSlave(const char *ifName) return true; } -static int readpacket() +static int read_packet() { int index; - unsigned short *pktLen; + unsigned short *pkt_len; int ret = -1; - pktLen = (unsigned short *)packet_buffer; + pkt_len = (unsigned short *)packet_buffer; index = 0; - while(1) { - if(index < 2) { + while (1) { + if (index < 2) { ret = read(fd, packet_buffer + index, 2 - index); } else { - ret = read(fd, packet_buffer + index, *pktLen - index + 2); + ret = read(fd, packet_buffer + index, *pkt_len - index + 2); } - if(ret < 1) { + if (ret < 1) { fprintf(stderr, "%s: read() returned %d.\n", __func__, ret); break; } index += ret; - if(index > 1) { - if(*pktLen > (sizeof(packet_buffer) + 2)) { - fprintf(stderr, "%s: pktLen (%d) too large.\n", __func__, *pktLen); + if (index > 1) { + if (*pkt_len > (sizeof(packet_buffer) + 2)) { + fprintf(stderr, "%s: pkt_len (%d) too large.\n", __func__, *pkt_len); break; } - if(index == (*pktLen + 2)) { - ret = *pktLen; + if (index == (*pkt_len + 2)) { + ret = *pkt_len; break; } } From f1c78e659c6aad962536a43f18c4260f38698f21 Mon Sep 17 00:00:00 2001 From: Dan Sumorok Date: Sat, 4 May 2013 20:37:29 -0400 Subject: [PATCH 4/5] More coding style updates. --- BasiliskII/src/MacOSX/etherslavetool.c | 12 ++++++++---- BasiliskII/src/MacOSX/runtool.m | 3 ++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/BasiliskII/src/MacOSX/etherslavetool.c b/BasiliskII/src/MacOSX/etherslavetool.c index 766c7968..57642774 100644 --- a/BasiliskII/src/MacOSX/etherslavetool.c +++ b/BasiliskII/src/MacOSX/etherslavetool.c @@ -47,7 +47,8 @@ static int open_bpf(char *ifname); static int retreive_auth_info(void); static int main_loop(int sd); -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ char *if_name; int ret; int sd; @@ -83,7 +84,8 @@ int main(int argc, char **argv) { return 0; } -static int main_loop(int sd) { +static int main_loop(int sd) +{ fd_set readSet; char *outgoing, *incoming; unsigned short *out_len; @@ -208,7 +210,8 @@ static int main_loop(int sd) { return fret; } -static int retreive_auth_info(void) { +static int retreive_auth_info(void) +{ AuthorizationRef aRef; OSStatus status; AuthorizationRights myRights; @@ -253,7 +256,8 @@ static int retreive_auth_info(void) { return 0; } -static int open_bpf(char *ifname) { +static int open_bpf(char *ifname) +{ u_int blen = 0; struct ifreq ifreq; u_int arg; diff --git a/BasiliskII/src/MacOSX/runtool.m b/BasiliskII/src/MacOSX/runtool.m index 261f3054..b866599c 100644 --- a/BasiliskII/src/MacOSX/runtool.m +++ b/BasiliskII/src/MacOSX/runtool.m @@ -42,7 +42,8 @@ FILE * run_tool(const char *ifName); -FILE * run_tool(const char *ifName) { +FILE * run_tool(const char *ifName) +{ OSStatus auth_status; FILE *fp; char *args[] = {"etherslavetool", NULL, NULL}; From e91a03f40f02474bc03d752f2ab3f24be3c0b117 Mon Sep 17 00:00:00 2001 From: Dan Sumorok Date: Sun, 5 May 2013 14:20:33 -0400 Subject: [PATCH 5/5] Added clean rule for etherslavetool. --- BasiliskII/src/Unix/Makefile.in | 1 + 1 file changed, 1 insertion(+) diff --git a/BasiliskII/src/Unix/Makefile.in b/BasiliskII/src/Unix/Makefile.in index 57a1c9d9..0094fae7 100644 --- a/BasiliskII/src/Unix/Makefile.in +++ b/BasiliskII/src/Unix/Makefile.in @@ -175,6 +175,7 @@ mostlyclean: clean: mostlyclean rm -f cpuemu.cpp cpudefs.cpp cputmp*.s cpufast*.s cpustbl.cpp cputbl.h compemu.cpp compstbl.cpp comptbl.h + rm -rf etherslavetool.dSYM distclean: clean rm -rf $(OBJ_DIR)