summaryrefslogtreecommitdiffstats
path: root/src/util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2004-06-23 23:17:30 +0000
committerLennart Poettering <lennart@poettering.net>2004-06-23 23:17:30 +0000
commitacb25b35102dfca08f66e155560f6c99cb8fa841 (patch)
tree2ae84c77727548a15eabbe5ad624dc1fd29af30b /src/util.c
parenteecf602476ff5b51bdc08f8fd0e4aa70d2b0ef5a (diff)
main part of the native protocol
git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@31 fefdeb5f-60dc-0310-8127-8f9354f1896f
Diffstat (limited to 'src/util.c')
-rw-r--r--src/util.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c
new file mode 100644
index 00000000..0383a0ad
--- /dev/null
+++ b/src/util.c
@@ -0,0 +1,62 @@
+#include <assert.h>
+#include <string.h>
+#include <stdio.h>
+#include <sys/un.h>
+#include <netinet/in.h>
+#include <fcntl.h>
+
+#include "util.h"
+
+void make_nonblock_fd(int fd) {
+ int v;
+
+ if ((v = fcntl(fd, F_GETFL)) >= 0)
+ if (!(v & O_NONBLOCK))
+ fcntl(fd, F_SETFL, v|O_NONBLOCK);
+}
+
+void peer_to_string(char *c, size_t l, int fd) {
+ struct stat st;
+
+ assert(c && l && fd >= 0);
+
+ if (fstat(fd, &st) < 0) {
+ snprintf(c, l, "Invalid client fd");
+ return;
+ }
+
+ if (S_ISSOCK(st.st_mode)) {
+ union {
+ struct sockaddr sa;
+ struct sockaddr_in in;
+ struct sockaddr_un un;
+ } sa;
+ socklen_t sa_len = sizeof(sa);
+
+ if (getpeername(fd, &sa.sa, &sa_len) >= 0) {
+
+ if (sa.sa.sa_family == AF_INET) {
+ uint32_t ip = ntohl(sa.in.sin_addr.s_addr);
+
+ snprintf(c, l, "TCP/IP client from %i.%i.%i.%i:%u",
+ ip >> 24,
+ (ip >> 16) & 0xFF,
+ (ip >> 8) & 0xFF,
+ ip & 0xFF,
+ ntohs(sa.in.sin_port));
+ return;
+ } else if (sa.sa.sa_family == AF_LOCAL) {
+ snprintf(c, l, "UNIX client for %s", sa.un.sun_path);
+ return;
+ }
+
+ }
+ snprintf(c, l, "Unknown network client");
+ return;
+ } else if (S_ISCHR(st.st_mode) && (fd == 0 || fd == 1)) {
+ snprintf(c, l, "STDIN/STDOUT client");
+ return;
+ }
+
+ snprintf(c, l, "Unknown client");
+}