Updated coding style.

This commit is contained in:
Dan Sumorok 2013-05-04 20:36:11 -04:00
parent 94b790728e
commit 01ba04139f
3 changed files with 130 additions and 133 deletions

View File

@ -43,160 +43,160 @@
#include <Carbon/Carbon.h> #include <Carbon/Carbon.h>
static int openBpf(char *ifname); static int open_bpf(char *ifname);
static int retreiveAuthInfo(void); static int retreive_auth_info(void);
static int mainLoop(int sd); static int main_loop(int sd);
int main(int argc, char **argv) { int main(int argc, char **argv) {
char *ifName; char *if_name;
int ret; int ret;
int sd; int sd;
if(argc != 2) { if (argc != 2) {
return 255; return 255;
} }
ifName = argv[1]; if_name = argv[1];
ret = retreiveAuthInfo(); ret = retreive_auth_info();
if(ret != 0) { if (ret != 0) {
return 254; return 254;
} }
fflush(stdout); fflush(stdout);
sd = openBpf(ifName); sd = open_bpf(if_name);
if(sd < 0) { if (sd < 0) {
return 253; return 253;
} }
fflush(stdout); fflush(stdout);
ret = mainLoop(sd); ret = main_loop(sd);
close(sd); close(sd);
if(ret < 0) { if (ret < 0) {
return 252; return 252;
} }
return 0; return 0;
} }
static int mainLoop(int sd) { static int main_loop(int sd) {
fd_set readSet; fd_set readSet;
char *outgoing, *incoming; char *outgoing, *incoming;
unsigned short *outLen; unsigned short *out_len;
unsigned short *inLen; unsigned short *in_len;
int inIndex, outIndex; int in_index, out_index;
u_int blen = 0; u_int blen = 0;
int ret; int ret;
int fret = 0; int fret = 0;
struct bpf_hdr *hdr; struct bpf_hdr *hdr;
int pktLen; int pkt_len;
int frameLen; int frame_len;
int pad; int pad;
if(ioctl(sd, BIOCGBLEN, &blen) < 0) { if (ioctl(sd, BIOCGBLEN, &blen) < 0) {
return -1; return -1;
} }
incoming = malloc(blen); incoming = malloc(blen);
if(incoming == NULL) { if (incoming == NULL) {
return -2; return -2;
} }
outgoing = malloc(blen); outgoing = malloc(blen);
if(outgoing == NULL) { if (outgoing == NULL) {
free(outgoing); free(outgoing);
return -3; return -3;
} }
inIndex = 0; in_index = 0;
outIndex = 0; out_index = 0;
outLen = (unsigned short *)outgoing; out_len = (unsigned short *)outgoing;
while(1) { while (1) {
int i; int i;
FD_ZERO(&readSet); FD_ZERO(&readSet);
FD_SET(0, &readSet); FD_SET(0, &readSet);
FD_SET(sd, &readSet); FD_SET(sd, &readSet);
ret = select(sd + 1, &readSet, NULL, NULL, NULL); ret = select(sd + 1, &readSet, NULL, NULL, NULL);
if(ret < 0) { if (ret < 0) {
fret = -4; fret = -4;
break; break;
} }
if(FD_ISSET(0, &readSet)) { if (FD_ISSET(0, &readSet)) {
if(outIndex < 2) { if (out_index < 2) {
ret = read(0, outgoing + outIndex, 2-outIndex); ret = read(0, outgoing + out_index, 2-out_index);
} else { } 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; fret = -5;
break; break;
} }
outIndex += ret; out_index += ret;
if(outIndex > 1) { if (out_index > 1) {
fflush(stdout); fflush(stdout);
if((*outLen + 2) > blen) { if ((*out_len + 2) > blen) {
fret = -6; fret = -6;
break; break;
} }
if(outIndex == (*outLen + 2)) { if (out_index == (*out_len + 2)) {
ret = write(sd, outLen + 1, *outLen); ret = write(sd, out_len + 1, *out_len);
if(ret != *outLen) { if (ret != *out_len) {
fret = -7; fret = -7;
break; break;
} }
outIndex = 0; out_index = 0;
} }
} }
} }
if(FD_ISSET(sd, &readSet)) { if (FD_ISSET(sd, &readSet)) {
int i; int i;
ret = read(sd, incoming, blen); ret = read(sd, incoming, blen);
if(ret < 1) { if (ret < 1) {
fret = -8; fret = -8;
break; break;
} }
hdr = (struct bpf_hdr *)incoming; hdr = (struct bpf_hdr *)incoming;
inLen = (unsigned short *)(incoming + 16); in_len = (unsigned short *)(incoming + 16);
do { do {
pktLen = hdr->bh_caplen; pkt_len = hdr->bh_caplen;
frameLen = pktLen + 18; frame_len = pkt_len + 18;
if((pktLen < 0) || (frameLen > ret) || (frameLen < 0)) { if ((pkt_len < 0) || (frame_len > ret) || (frame_len < 0)) {
fret = -9; fret = -9;
break; break;
} }
*inLen = pktLen; *in_len = pkt_len;
write(0, inLen, pktLen + 2); write(0, in_len, pkt_len + 2);
if((frameLen & 0x03) == 0) { if ((frame_len & 0x03) == 0) {
pad = 0; pad = 0;
} else { } else {
pad = 4 - (frameLen & 0x03); pad = 4 - (frame_len & 0x03);
} }
ret -= (frameLen + pad); ret -= (frame_len + pad);
hdr = (struct bpf_hdr *)((unsigned char *)hdr + frameLen + pad); hdr = (struct bpf_hdr *)((unsigned char *)hdr + frame_len + pad);
inLen = (unsigned short *)((unsigned char *)hdr + 16); in_len = (unsigned short *)((unsigned char *)hdr + 16);
} while (ret > 0); } while (ret > 0);
if(fret != 0) { if (fret != 0) {
break; break;
} }
} }
@ -208,7 +208,7 @@ static int mainLoop(int sd) {
return fret; return fret;
} }
static int retreiveAuthInfo(void) { static int retreive_auth_info(void) {
AuthorizationRef aRef; AuthorizationRef aRef;
OSStatus status; OSStatus status;
AuthorizationRights myRights; AuthorizationRights myRights;
@ -219,12 +219,12 @@ static int retreiveAuthInfo(void) {
int i; int i;
status = AuthorizationCopyPrivilegedReference(&aRef, kAuthorizationFlagDefaults); status = AuthorizationCopyPrivilegedReference(&aRef, kAuthorizationFlagDefaults);
if(status != errAuthorizationSuccess) { if (status != errAuthorizationSuccess) {
return -1; return -1;
} }
status = AuthorizationCopyInfo(aRef, NULL, &mySet); status = AuthorizationCopyInfo(aRef, NULL, &mySet);
if(status != errAuthorizationSuccess) { if (status != errAuthorizationSuccess) {
AuthorizationFree(aRef, kAuthorizationFlagDestroyRights); AuthorizationFree(aRef, kAuthorizationFlagDestroyRights);
return -1; return -1;
} }
@ -240,7 +240,7 @@ static int retreiveAuthInfo(void) {
status = AuthorizationCopyRights(aRef, &myRights, NULL, status = AuthorizationCopyRights(aRef, &myRights, NULL,
kAuthorizationFlagExtendRights, kAuthorizationFlagExtendRights,
&newRights); &newRights);
if(status != errAuthorizationSuccess) { if (status != errAuthorizationSuccess) {
AuthorizationFreeItemSet(mySet); AuthorizationFreeItemSet(mySet);
AuthorizationFree(aRef, kAuthorizationFlagDestroyRights); AuthorizationFree(aRef, kAuthorizationFlagDestroyRights);
return -2; return -2;
@ -253,18 +253,18 @@ static int retreiveAuthInfo(void) {
return 0; return 0;
} }
static int openBpf(char *ifname) { static int open_bpf(char *ifname) {
u_int blen = 0; u_int blen = 0;
struct ifreq ifreq; struct ifreq ifreq;
u_int arg; u_int arg;
int sd = open("/dev/bpf2", O_RDWR); int sd = open("/dev/bpf2", O_RDWR);
if(sd < 0) { if (sd < 0) {
return -1; return -1;
} }
if(ioctl(sd, BIOCGBLEN, &blen) < 0) { if (ioctl(sd, BIOCGBLEN, &blen) < 0) {
close(sd); close(sd);
return -2; return -2;
} }
@ -273,25 +273,25 @@ static int openBpf(char *ifname) {
strncpy(ifreq.ifr_name, ifname, IFNAMSIZ); strncpy(ifreq.ifr_name, ifname, IFNAMSIZ);
arg = 0; arg = 0;
if(ioctl(sd, BIOCSETIF, &ifreq) < 0) { if (ioctl(sd, BIOCSETIF, &ifreq) < 0) {
close(sd); close(sd);
return -3; return -3;
} }
arg = 0; arg = 0;
if(ioctl(sd, BIOCSSEESENT, &arg) < 0) { if (ioctl(sd, BIOCSSEESENT, &arg) < 0) {
close(sd); close(sd);
return -4; return -4;
} }
arg = 1; arg = 1;
if(ioctl(sd, BIOCPROMISC, &arg) < 0) { if (ioctl(sd, BIOCPROMISC, &arg) < 0) {
close(sd); close(sd);
return -5; return -5;
} }
arg = 1; arg = 1;
if(ioctl(sd, BIOCIMMEDIATE, &arg) < 0) { if (ioctl(sd, BIOCIMMEDIATE, &arg) < 0) {
close(sd); close(sd);
return -6; return -6;
} }

View File

@ -40,58 +40,57 @@
#include <Carbon/Carbon.h> #include <Carbon/Carbon.h>
FILE * runTool(const char *ifName); FILE * run_tool(const char *ifName);
FILE * runTool(const char *ifName) { FILE * run_tool(const char *ifName) {
OSStatus authStatus; OSStatus auth_status;
FILE *fp; FILE *fp;
char *args[] = {"etherslavetool", NULL, NULL}; char *args[] = {"etherslavetool", NULL, NULL};
int ret; int ret;
const char *path; const char *path;
AuthorizationFlags auth_flags;
AuthorizationRef auth_ref;
AuthorizationItem auth_items[1];
AuthorizationRights auth_rights;
path = [[[NSBundle mainBundle] path = [[[NSBundle mainBundle]
pathForResource:@"etherslavetool" ofType: nil] UTF8String]; pathForResource:@"etherslavetool" ofType: nil] UTF8String];
if(path == NULL) { if (path == NULL) {
return NULL; return NULL;
} }
AuthorizationFlags authFlags;
AuthorizationRef authRef;
AuthorizationItem authItems[1];
AuthorizationRights authRights;
args[1] = (char *)ifName; args[1] = (char *)ifName;
authFlags = kAuthorizationFlagExtendRights | auth_flags = kAuthorizationFlagExtendRights |
kAuthorizationFlagInteractionAllowed | kAuthorizationFlagInteractionAllowed |
kAuthorizationFlagPreAuthorize; kAuthorizationFlagPreAuthorize;
authItems[0].name = "system.privilege.admin"; auth_items[0].name = "system.privilege.admin";
authItems[0].valueLength = 0; auth_items[0].valueLength = 0;
authItems[0].value = NULL; auth_items[0].value = NULL;
authItems[0].flags = 0; auth_items[0].flags = 0;
authRights.count = sizeof (authItems) / sizeof (authItems[0]); auth_rights.count = sizeof (auth_items) / sizeof (auth_items[0]);
authRights.items = authItems; auth_rights.items = auth_items;
authStatus = AuthorizationCreate(&authRights, auth_status = AuthorizationCreate(&auth_rights,
kAuthorizationEmptyEnvironment, kAuthorizationEmptyEnvironment,
authFlags, auth_flags,
&authRef); &auth_ref);
if(authStatus != errAuthorizationSuccess) { if (auth_status != errAuthorizationSuccess) {
fprintf(stderr, "%s: AuthorizationCreate() failed.\n", __func__); fprintf(stderr, "%s: AuthorizationCreate() failed.\n", __func__);
return NULL; return NULL;
} }
authStatus = AuthorizationExecuteWithPrivileges(authRef, auth_status = AuthorizationExecuteWithPrivileges(auth_ref,
path, path,
kAuthorizationFlagDefaults, kAuthorizationFlagDefaults,
args + 1, args + 1,
&fp); &fp);
if(authStatus != errAuthorizationSuccess) { if (auth_status != errAuthorizationSuccess) {
fprintf(stderr, "%s: AuthorizationExecWithPrivileges() failed.\n", __func__); fprintf(stderr, "%s: AuthorizationExecWithPrivileges() failed.\n", __func__);
return NULL; return NULL;
} }

View File

@ -106,7 +106,7 @@ enum {
#ifdef ENABLE_MACOSX_ETHERSLAVE #ifdef ENABLE_MACOSX_ETHERSLAVE
extern "C" { extern "C" {
extern FILE * runTool(const char *ifName); extern FILE * run_tool(const char *if_name);
} }
#endif #endif
@ -155,9 +155,9 @@ static void slirp_add_redirs();
static int slirp_add_redir(const char *redir_str); static int slirp_add_redir(const char *redir_str);
#ifdef ENABLE_MACOSX_ETHERSLAVE #ifdef ENABLE_MACOSX_ETHERSLAVE
static int getmacaddress(const char* dev, unsigned char *addr); static int get_mac_address(const char* dev, unsigned char *addr);
static bool openEtherSlave(const char *ifName); static bool open_ether_slave(const char *if_name);
static int readpacket(void); static int read_packet(void);
#endif #endif
/* /*
@ -260,7 +260,7 @@ bool ether_init(void)
// Do nothing if no Ethernet device specified // Do nothing if no Ethernet device specified
const char *name = PrefsFindString("ether"); const char *name = PrefsFindString("ether");
#ifdef ENABLE_MACOSX_ETHERSLAVE #ifdef ENABLE_MACOSX_ETHERSLAVE
const char *slaveDev = PrefsFindString("etherslavedev"); const char *slave_dev = PrefsFindString("etherslavedev");
#endif #endif
if (name == NULL) if (name == NULL)
return false; return false;
@ -333,11 +333,11 @@ bool ether_init(void)
break; break;
#ifdef ENABLE_MACOSX_ETHERSLAVE #ifdef ENABLE_MACOSX_ETHERSLAVE
case NET_IF_ETHERSLAVE: case NET_IF_ETHERSLAVE:
if(slaveDev == NULL) { if (slave_dev == NULL) {
WarningAlert("etherslavedev not defined in preferences."); WarningAlert("etherslavedev not defined in preferences.");
return false; return false;
} }
return openEtherSlave(slaveDev); return open_ether_slave(slave_dev);
#endif #endif
} }
if (net_if_type != NET_IF_SLIRP) { if (net_if_type != NET_IF_SLIRP) {
@ -792,14 +792,14 @@ static int16 ether_do_write(uint32 arg)
#endif #endif
#ifdef ENABLE_MACOSX_ETHERSLAVE #ifdef ENABLE_MACOSX_ETHERSLAVE
if (net_if_type == NET_IF_ETHERSLAVE) { if (net_if_type == NET_IF_ETHERSLAVE) {
unsigned short pktlen; unsigned short pkt_len;
pktlen = len; pkt_len = len;
if(write(fd, &pktlen, 2) < 2) { if (write(fd, &pkt_len, 2) < 2) {
return excessCollsns; return excessCollsns;
} }
if(write(fd, packet, len) < len) { if (write(fd, packet, len) < len) {
return excessCollsns; return excessCollsns;
} }
return noErr; return noErr;
@ -940,7 +940,7 @@ static void *receive_func(void *arg)
#ifdef ENABLE_MACOSX_ETHERSLAVE #ifdef ENABLE_MACOSX_ETHERSLAVE
if (net_if_type == NET_IF_ETHERSLAVE) { if (net_if_type == NET_IF_ETHERSLAVE) {
if(readpacket() < 1) { if (read_packet() < 1) {
break; break;
} }
} }
@ -987,12 +987,12 @@ void ether_do_interrupt(void)
#endif #endif
#ifdef ENABLE_MACOSX_ETHERSLAVE #ifdef ENABLE_MACOSX_ETHERSLAVE
if (net_if_type == NET_IF_ETHERSLAVE) { if (net_if_type == NET_IF_ETHERSLAVE) {
unsigned short *pktlen; unsigned short *pkt_len;
uint32 p = packet; uint32 p = packet;
pktlen = (unsigned short *)packet_buffer; pkt_len = (unsigned short *)packet_buffer;
length = *pktlen; length = *pkt_len;
memcpy(Mac2HostAddr(packet), pktlen + 1, length); memcpy(Mac2HostAddr(packet), pkt_len + 1, length);
ether_dispatch_packet(p, length); ether_dispatch_packet(p, length);
break; break;
} else } else
@ -1124,22 +1124,22 @@ static int slirp_add_redir(const char *redir_str)
} }
#ifdef ENABLE_MACOSX_ETHERSLAVE #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; struct ifaddrs *ifaddrs, *next;
int ret = -1; int ret = -1;
struct sockaddr_dl *sa; struct sockaddr_dl *sa;
if(getifaddrs(&ifaddrs) != 0) { if (getifaddrs(&ifaddrs) != 0) {
perror("getifaddrs"); perror("getifaddrs");
return -1; return -1;
} }
next = ifaddrs; next = ifaddrs;
while(next != NULL) { while (next != NULL) {
switch(next->ifa_addr->sa_family) { switch (next->ifa_addr->sa_family) {
case AF_LINK: case AF_LINK:
if(!strcmp(dev, next->ifa_name)) { if (!strcmp(dev, next->ifa_name)) {
sa = (struct sockaddr_dl *)next->ifa_addr; sa = (struct sockaddr_dl *)next->ifa_addr;
memcpy(addr, LLADDR(sa), 6); memcpy(addr, LLADDR(sa), 6);
ret = 0; ret = 0;
@ -1156,23 +1156,21 @@ static int getmacaddress(const char* dev, unsigned char *addr)
return ret; return ret;
} }
static bool openEtherSlave(const char *ifName) static bool open_ether_slave(const char *if_name)
{ {
FILE *fp; FILE *fp;
char str[64]; char str[64];
str[sizeof(str)-1] = '\0'; if (get_mac_address(if_name, ether_addr) != 0) {
snprintf(str, sizeof(str), "Unable to find interface %s.",
if(getmacaddress(ifName, ether_addr) != 0) { if_name);
snprintf(str, sizeof(str)-1, "Unable to find interface %s.",
ifName);
WarningAlert(str); WarningAlert(str);
return false; return false;
} }
fp = runTool(ifName); fp = run_tool(if_name);
if(fp == NULL) { if (fp == NULL) {
snprintf(str, sizeof(str)-1, "Unable to run ether slave helper tool."); snprintf(str, sizeof(str), "Unable to run ether slave helper tool.");
WarningAlert(str); WarningAlert(str);
return false; return false;
} }
@ -1180,7 +1178,7 @@ static bool openEtherSlave(const char *ifName)
fd = dup(fileno(fp)); fd = dup(fileno(fp));
fclose(fp); fclose(fp);
if(start_thread() == false) { if (start_thread() == false) {
close(fd); close(fd);
fd = -1; fd = -1;
return false; return false;
@ -1189,37 +1187,37 @@ static bool openEtherSlave(const char *ifName)
return true; return true;
} }
static int readpacket() static int read_packet()
{ {
int index; int index;
unsigned short *pktLen; unsigned short *pkt_len;
int ret = -1; int ret = -1;
pktLen = (unsigned short *)packet_buffer; pkt_len = (unsigned short *)packet_buffer;
index = 0; index = 0;
while(1) { while (1) {
if(index < 2) { if (index < 2) {
ret = read(fd, packet_buffer + index, 2 - index); ret = read(fd, packet_buffer + index, 2 - index);
} else { } 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); fprintf(stderr, "%s: read() returned %d.\n", __func__, ret);
break; break;
} }
index += ret; index += ret;
if(index > 1) { if (index > 1) {
if(*pktLen > (sizeof(packet_buffer) + 2)) { if (*pkt_len > (sizeof(packet_buffer) + 2)) {
fprintf(stderr, "%s: pktLen (%d) too large.\n", __func__, *pktLen); fprintf(stderr, "%s: pkt_len (%d) too large.\n", __func__, *pkt_len);
break; break;
} }
if(index == (*pktLen + 2)) { if (index == (*pkt_len + 2)) {
ret = *pktLen; ret = *pkt_len;
break; break;
} }
} }