Fix compilation on OSX.

- /proc/net/route is Linux only.
- lsif() will work anywhere SIOCGIFCONF is defined.
- Abstract namespace UNIX domain sockets are Linux only.
- Rework code to reduce indent levels.
This commit is contained in:
Daniel O'Connor 2012-05-28 14:54:33 +09:30
parent 36389d2b78
commit 8882515d03
4 changed files with 57 additions and 42 deletions

4
lsif.c
View File

@ -61,6 +61,7 @@
#define SIOCGIFBRDADDR OSIOCGIFBRDADDR #define SIOCGIFBRDADDR OSIOCGIFBRDADDR
#endif #endif
#ifdef linux
/* for when all other options fail, as can happen on Android, /* for when all other options fail, as can happen on Android,
if the permissions for the socket-based method are broken. if the permissions for the socket-based method are broken.
Down side is that it while it gets the interface name and Down side is that it while it gets the interface name and
@ -97,8 +98,9 @@ int scrapeProcNetRoute()
fclose(f); fclose(f);
return 0; return 0;
} }
#endif
#ifdef ANDROID #ifdef SIOCGIFCONF
int lsif(void) int lsif(void)
{ {
char buf[8192] = {0}; char buf[8192] = {0};

View File

@ -70,50 +70,61 @@ int monitor_setup_sockets()
struct sockaddr_un name; struct sockaddr_un name;
int len; int len;
bzero(&name, sizeof(name));
name.sun_family = AF_UNIX; name.sun_family = AF_UNIX;
if (monitor_named_socket==-1) { if (monitor_named_socket!=-1)
/* ignore SIGPIPE so that we don't explode */ return 0;
signal(SIGPIPE, SIG_IGN);
/* ignore SIGPIPE so that we don't explode */
name.sun_path[0]=0; signal(SIGPIPE, SIG_IGN);
snprintf(&name.sun_path[1],100,"org.servalproject.servald.monitor.socket"); if ((monitor_named_socket = socket(AF_UNIX, SOCK_STREAM, 0))==-1) {
if (name.sun_path[0]) unlink(&name.sun_path[0]); WHY_perror("socket");
/* DONT include the null if forming an abstract socket, but DO include it goto error;
if forming a file-system */
len = 1+strlen(&name.sun_path[1]) + sizeof(name.sun_family);
monitor_named_socket = socket(AF_UNIX, SOCK_STREAM, 0);
if (monitor_named_socket>-1) {
int dud=0;
int r=bind(monitor_named_socket, (struct sockaddr *)&name, len);
if (r) { dud=1; r=0; WHY_perror("bind"); }
r=listen(monitor_named_socket,MAX_MONITOR_SOCKETS);
if (r) { dud=1; r=0; WHY_perror("listen");
}
if (dud) {
close(monitor_named_socket);
monitor_named_socket=-1;
return -1;
}
int reuseP=1;
if(setsockopt( monitor_named_socket, SOL_SOCKET, SO_REUSEADDR,
&reuseP, sizeof(reuseP)) < 0)
{
WHY("Could not indicate reuse addresses. Not necessarily a problem (yet)");
WHY_perror("setsockopt");
}
int send_buffer_size=64*1024;
int res = setsockopt(monitor_named_socket, SOL_SOCKET, SO_RCVBUF,
&send_buffer_size, sizeof(send_buffer_size));
if (res) WHY_perror("setsockopt");
else if (debug&(DEBUG_IO|DEBUG_VERBOSE_IO)) WHY("Monitor server socket setup");
}
} }
#ifdef linux
/* Use abstract namespace as Android has no writable FS which supports sockets */
name.sun_path[0]=0;
/* XXX: 104 comes from OSX sys/un.h - no #define (note Linux has UNIX_PATH_MAX and it's 108(!)) */
snprintf(&name.sun_path[1],104-2,"org.servalproject.servald.monitor.socket");
/* Doesn't include trailing nul */
len = 1+strlen(&name.sun_path[1]) + sizeof(name.sun_family);
#else
snprintf(name.sun_path,104-1,"%s/org.servalproject.servald.monitor.socket",serval_instancepath());
unlink(name.sun_path);
/* Includes trailing nul */
len = 1+strlen(&name.sun_path) + sizeof(name.sun_family);
#endif
if(bind(monitor_named_socket, (struct sockaddr *)&name, len)==-1) {
WHY_perror("bind");
goto error;
}
if(listen(monitor_named_socket,MAX_MONITOR_SOCKETS)==-1) {
WHY_perror("listen");
goto error;
}
int reuseP=1;
if(setsockopt(monitor_named_socket, SOL_SOCKET, SO_REUSEADDR,
&reuseP, sizeof(reuseP)) < 0) {
WHY_perror("setsockopt");
WHY("Could not indicate reuse addresses. Not necessarily a problem (yet)");
}
int send_buffer_size=64*1024;
if(setsockopt(monitor_named_socket, SOL_SOCKET, SO_RCVBUF,
&send_buffer_size, sizeof(send_buffer_size))==-1)
WHY_perror("setsockopt");
if (debug&(DEBUG_IO|DEBUG_VERBOSE_IO)) WHY("Monitor server socket setup");
return 0; return 0;
error:
close(monitor_named_socket);
monitor_named_socket=-1;
return -1;
} }
int monitor_get_fds(struct pollfd *fds,int *fdcount,int fdmax) int monitor_get_fds(struct pollfd *fds,int *fdcount,int fdmax)

View File

@ -596,12 +596,13 @@ int overlay_interface_discover()
r=r->next; r=r->next;
} }
#ifdef ANDROID #ifdef SIOCGIFCONF
/* Use alternative linux-only method to find and register interfaces. */
lsif(); lsif();
#endif #endif
/* /proc based approach */
#ifdef linux
scrapeProcNetRoute(); scrapeProcNetRoute();
#endif
#ifdef HAVE_IFADDRS_H #ifdef HAVE_IFADDRS_H
struct ifaddrs *ifaddr,*ifa; struct ifaddrs *ifaddr,*ifa;

View File

@ -1499,6 +1499,7 @@ int encodeAndDispatchRecordedAudio(int fd,int callSessionToken,
unsigned char *sampleData, unsigned char *sampleData,
int sampleBytes); int sampleBytes);
int scrapeProcNetRoute(); int scrapeProcNetRoute();
int lsif();
int bufferAudioForPlayback(int codec,long long start_time,long long end_time, int bufferAudioForPlayback(int codec,long long start_time,long long end_time,
unsigned char *data,int dataLen); unsigned char *data,int dataLen);
int startAudio(); int startAudio();