]> code.delx.au - pulseaudio/blobdiff - polyp/socket-util.c
Make the whole stuff LGPL only
[pulseaudio] / polyp / socket-util.c
index f9d0febfa2d1979475ddb8c2190bc8b167132a11..1800710f7e25cffee1fdef301a09477c2a757933 100644 (file)
@@ -4,7 +4,7 @@
   This file is part of polypaudio.
  
   polypaudio is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published
+  it under the terms of the GNU Lesser General Public License as published
   by the Free Software Foundation; either version 2 of the License,
   or (at your option) any later version.
  
@@ -13,7 +13,7 @@
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   General Public License for more details.
  
-  You should have received a copy of the GNU General Public License
+  You should have received a copy of the GNU Lesser General Public License
   along with polypaudio; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
   USA.
 #include <assert.h>
 #include <string.h>
 #include <stdio.h>
+#include <sys/types.h>
 #include <sys/un.h>
 #include <netinet/in.h>
 #include <fcntl.h>
 #include <unistd.h>
-#include <sys/types.h>
+#include <netinet/in_systm.h>
 #include <netinet/tcp.h>
 #include <netinet/ip.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <netdb.h>
 
 #include "socket-util.h"
 #include "util.h"
 #include "xmalloc.h"
+#include "log.h"
 
 void pa_socket_peer_to_string(int fd, char *c, size_t l) {
     struct stat st;
@@ -92,27 +97,43 @@ int pa_socket_low_delay(int fd) {
     int priority;
     assert(fd >= 0);
 
+#ifdef SO_PRIORITY
     priority = 7;
     if (setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &priority, sizeof(priority)) < 0)
         return -1;
+#endif
 
     return 0;
 }
 
 int pa_socket_tcp_low_delay(int fd) {
-    int ret, tos;
+    int ret, tos, on;
 
     assert(fd >= 0);
 
     ret = pa_socket_low_delay(fd);
     
-/*     on = 1; */
-/*     if (setsockopt(fd, SOL_TCP, TCP_NODELAY, &on, sizeof(on)) < 0) */
-/*         ret = -1; */
+    on = 1;
+
+#if defined(SOL_TCP) || defined(IPPROTO_TCP)
+#if defined(SOL_TCP)
+    if (setsockopt(fd, SOL_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
+#else
+    if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
+#endif
+        ret = -1;
+#endif
 
+#if defined(IPTOS_LOWDELAY) && defined(IP_TOS) && (defined(SOL_IP) || \
+       defined(IPPROTO_IP))
     tos = IPTOS_LOWDELAY;
+#ifdef SOL_IP
     if (setsockopt(fd, SOL_IP, IP_TOS, &tos, sizeof(tos)) < 0)
+#else
+    if (setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
+#endif
         ret = -1;
+#endif
 
     return ret;
 
@@ -121,10 +142,10 @@ int pa_socket_tcp_low_delay(int fd) {
 int pa_socket_set_rcvbuf(int fd, size_t l) {
     assert(fd >= 0);
 
-    if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &l, sizeof(l)) < 0) {
-        fprintf(stderr, "SO_RCVBUF: %s\n", strerror(errno));
-        return -1;
-    }
+/*     if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &l, sizeof(l)) < 0) { */
+/*         pa_log(__FILE__": SO_RCVBUF: %s\n", strerror(errno)); */
+/*         return -1; */
+/*     } */
 
     return 0;
 }
@@ -132,10 +153,10 @@ int pa_socket_set_rcvbuf(int fd, size_t l) {
 int pa_socket_set_sndbuf(int fd, size_t l) {
     assert(fd >= 0);
 
-    if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &l, sizeof(l)) < 0) {
-        fprintf(stderr, "SO_SNDBUF: %s\n", strerror(errno));
-        return -1;
-    }
+/*     if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &l, sizeof(l)) < 0) { */
+/*         pa_log(__FILE__": SO_SNDBUF: %s\n", strerror(errno)); */
+/*         return -1; */
+/*     } */
 
     return 0;
 }
@@ -145,7 +166,7 @@ int pa_unix_socket_is_stale(const char *fn) {
     int fd = -1, ret = -1;
 
     if ((fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
-        fprintf(stderr, "socket(): %s\n", strerror(errno));
+        pa_log(__FILE__": socket(): %s\n", strerror(errno));
         goto finish;
     }
 
@@ -182,38 +203,35 @@ int pa_unix_socket_remove_stale(const char *fn) {
     return 0;
 }
 
-int pa_unix_socket_make_secure_dir(const char *fn) {
-    int ret = -1;
-    char *slash, *dir = pa_xstrdup(fn);
+struct sockaddr *pa_resolve_server(const char *server, size_t *len, uint16_t nport) {
+    struct sockaddr *sa;
+    struct addrinfo hints, *result = NULL;
+    char *port, host[256], tmp[16];
+    assert(server && len);
+
+    snprintf(host, sizeof(host), "%s", server);
+    host[strcspn(host, ":")] = 0;
     
-    if (!(slash = strrchr(dir, '/')))
-        goto finish;
-    *slash = 0;
+    if ((port = strrchr(server, ':')))
+        port++;
     
-    if (pa_make_secure_dir(dir) < 0)
-        goto finish;
+    if (!port) 
+        snprintf(port = tmp, sizeof(tmp), "%u", nport);
 
-    ret = 0;
-    
-finish:
-    pa_xfree(dir);
-    return ret;
-}
+    memset(&hints, 0, sizeof(hints));
+    hints.ai_family = PF_UNSPEC;
+    hints.ai_socktype = SOCK_STREAM;
+    hints.ai_protocol = 0;
 
-int pa_unix_socket_remove_secure_dir(const char *fn) {
-    int ret = -1;
-    char *slash, *dir = pa_xstrdup(fn);
+    if (getaddrinfo(host, port, &hints, &result) != 0)
+        return NULL;
+    assert(result);
     
-    if (!(slash = strrchr(dir, '/')))
-        goto finish;
-    *slash = 0;
+    sa = pa_xmalloc(*len = result->ai_addrlen);
+    memcpy(sa, result->ai_addr, *len);
 
-    if (rmdir(dir) < 0)
-        goto finish;
-    
-    ret = 0;
+    freeaddrinfo(result);
     
-finish:
-    pa_xfree(dir);
-    return ret;
+    return sa;
 }
+