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>
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;
}

View File

@ -40,58 +40,57 @@
#include <Carbon/Carbon.h>
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;
}

View File

@ -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;
}
}