Switch from AF_UNIX to UDP on localhost.
Dart does not support AF_UNIX sockets. There is a CL
which would add it, but it was never integrated.
Switch to UDP sockets, bound to localhost.
Also correct several signedness bugs, read() and recv()
return negative numbers on error.
Change-Id: Ib350c7d7459a12c4db20ca992c68e54da7756c7b
diff --git a/rcu_audio/gfrm-voice-demo.cc b/rcu_audio/gfrm-voice-demo.cc
index c097252..cda1db0 100644
--- a/rcu_audio/gfrm-voice-demo.cc
+++ b/rcu_audio/gfrm-voice-demo.cc
@@ -15,6 +15,7 @@
*/
#define _BSD_SOURCE
+#include <arpa/inet.h>
#include <endian.h>
#include <fcntl.h>
#include <stdint.h>
@@ -25,7 +26,6 @@
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
-#include <sys/un.h>
#include <unistd.h>
#include "rcu-audio.h"
@@ -64,7 +64,7 @@
{
mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
int fd;
- struct sockaddr_un sun;
+ struct sockaddr_in sin;
const char *outfile = "/tmp/audio.wav";
int outfd;
uint8_t buf[8192];
@@ -88,15 +88,17 @@
}
}
- if ((fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) {
- perror("socket(AF_UNIX) RCU_AUDIO_PATH");
+ if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
+ perror("socket(AF_INET) RCU_AUDIO");
exit(1);
}
- memset(&sun, 0, sizeof(sun));
- sun.sun_family = AF_UNIX;
- strncpy(&sun.sun_path[1], RCU_AUDIO_PATH, sizeof(sun.sun_path) - 2);
- if (bind(fd, (const struct sockaddr *) &sun, sizeof(sun)) < 0) {
- perror("bind(AF_UNIX) RCU_AUDIO_PATH");
+ memset(&sin, 0, sizeof(sin));
+ sin.sin_family = AF_INET;
+ sin.sin_port = htons(RCU_AUDIO_PORT);
+ sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+
+ if (bind(fd, (const struct sockaddr *) &sin, sizeof(sin)) < 0) {
+ perror("bind(AF_INET) RCU_AUDIO_PORT");
exit(1);
}
diff --git a/rcu_audio/gfrm100-rcu-audio.cc b/rcu_audio/gfrm100-rcu-audio.cc
index 6e55e93..31056ad 100644
--- a/rcu_audio/gfrm100-rcu-audio.cc
+++ b/rcu_audio/gfrm100-rcu-audio.cc
@@ -27,6 +27,7 @@
* applications using hidraw.
*/
+#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/hidraw.h>
@@ -40,7 +41,6 @@
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
-#include <sys/un.h>
#include <unistd.h>
#include <vector>
@@ -53,7 +53,7 @@
{
int in = -1, out = -1, connected = 0;
const char *device;
- struct sockaddr_un sun;
+ struct sockaddr_in sin;
char name[16];
char address[64];
@@ -68,9 +68,10 @@
exit(1);
}
- memset(&sun, 0, sizeof(sun));
- sun.sun_family = AF_UNIX;
- strncpy(&sun.sun_path[1], RCU_AUDIO_PATH, sizeof(sun.sun_path) - 2);
+ memset(&sin, 0, sizeof(sin));
+ sin.sin_family = AF_INET;
+ sin.sin_port = htons(RCU_AUDIO_PORT);
+ sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
if (ioctl(in, HIDIOCGRAWNAME(sizeof(name)), name) < 0) {
perror("HIDIOCGRAWNAME");
@@ -96,7 +97,7 @@
while (1) {
uint8_t data[2048];
- size_t len = read(in, data, sizeof(data));
+ ssize_t len = read(in, data, sizeof(data));
if (len < 0) {
fprintf(stderr, "GFRM100 has disconnected. Exiting.\n");
@@ -132,7 +133,7 @@
}
if (!connected) {
- if (connect(out, (const struct sockaddr *) &sun, sizeof(sun)) == 0) {
+ if (connect(out, (const struct sockaddr *) &sin, sizeof(sin)) == 0) {
connected = 1;
} else {
sleep(2); /* rate limit how often we retry. */
diff --git a/rcu_audio/rcu-audio.h b/rcu_audio/rcu-audio.h
index 725ca10..38b06d7 100644
--- a/rcu_audio/rcu-audio.h
+++ b/rcu_audio/rcu-audio.h
@@ -17,13 +17,13 @@
#ifndef RCU_AUDIO_H
#define RCU_AUDIO_H
-#define RCU_AUDIO_PATH "rcu_audio"
+#define RCU_AUDIO_PORT 1995
/* Return 1 if at least one second has passed since the
* last successful call to pacing(). */
extern int pacing();
-/* Return an AF_UNIX socket, or die trying. */
+/* Return an AF_INET socket, or die trying. */
extern int get_socket_or_die();
#endif /* RCU_AUDIO_H */
diff --git a/rcu_audio/rcu-utils.cc b/rcu_audio/rcu-utils.cc
index 2bd1e41..ad694c8 100644
--- a/rcu_audio/rcu-utils.cc
+++ b/rcu_audio/rcu-utils.cc
@@ -53,8 +53,8 @@
{
int s;
- if ((s = socket(AF_UNIX, SOCK_NONBLOCK | SOCK_DGRAM, 0)) < 0) {
- perror("socket(AF_UNIX)");
+ if ((s = socket(AF_INET, SOCK_NONBLOCK | SOCK_DGRAM, 0)) < 0) {
+ perror("socket(AF_INET)");
exit(1);
}
diff --git a/rcu_audio/ti-rcu-audio.cc b/rcu_audio/ti-rcu-audio.cc
index 76f1126..67dd869 100644
--- a/rcu_audio/ti-rcu-audio.cc
+++ b/rcu_audio/ti-rcu-audio.cc
@@ -17,6 +17,7 @@
* it exists in order to interface with the RAS_LIB.c implementation
* provided by TI which is not licensed under Apache. */
+#include <arpa/inet.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
@@ -39,6 +40,7 @@
{
int is = -1, os = -1, connected = 0;
struct sockaddr_un sun;
+ struct sockaddr_in sin;
uint8 prev = 0;
int msgs = 0, missed = 0, errors = 0;
@@ -51,13 +53,14 @@
exit(1);
}
- memset(&sun, 0, sizeof(sun));
- sun.sun_family = AF_UNIX;
- strncpy(&sun.sun_path[1], RCU_AUDIO_PATH, sizeof(sun.sun_path) - 2);
+ memset(&sin, 0, sizeof(sin));
+ sin.sin_family = AF_INET;
+ sin.sin_port = htons(RCU_AUDIO_PORT);
+ sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
while (1) {
uint8 ibuf[MAX_INPUT_BUF_SIZE + 6 + 1 + 4];
- size_t ilen;
+ ssize_t ilen;
ilen = recv(is, ibuf, sizeof(ibuf), 0);
if (ilen < 23) {
@@ -121,7 +124,7 @@
}
if (!connected) {
- if (connect(os, (const struct sockaddr *) &sun, sizeof(sun)) == 0) {
+ if (connect(os, (const struct sockaddr *) &sin, sizeof(sin)) == 0) {
connected = 1;
} else {
sleep(2); /* rate limit how often we try */