-rw-r--r-- | noncore/net/wellenreiter/libwellenreiter/source/sock.cc | 28 | ||||
-rw-r--r-- | noncore/net/wellenreiter/libwellenreiter/source/sock.hh | 1 |
2 files changed, 25 insertions, 4 deletions
diff --git a/noncore/net/wellenreiter/libwellenreiter/source/sock.cc b/noncore/net/wellenreiter/libwellenreiter/source/sock.cc index 2cc16eb..90fc20a 100644 --- a/noncore/net/wellenreiter/libwellenreiter/source/sock.cc +++ b/noncore/net/wellenreiter/libwellenreiter/source/sock.cc @@ -3,71 +3,91 @@ * * $Id$ */ #include "sock.hh" #include "log.hh" /* Setup UDP Socket for incoming commands */ int commsock(const char *host, int port) { struct sockaddr_in saddr; int sock; if((sock=socket(AF_INET, SOCK_DGRAM, 0)) < 0) { wl_logerr("Cannot set up socket: %s", strerror(errno)); return -1; } memset(&saddr, 0, sizeof(saddr)); saddr.sin_family = PF_INET; saddr.sin_port = htons(port); saddr.sin_addr.s_addr = inet_addr(host); if(bind(sock,(struct sockaddr *)&saddr, sizeof(saddr)) < 0) { wl_logerr("Cannot bind socket: %s", strerror(errno)); close(sock); return -1; } return sock; } /* Send a string to commsock */ int sendcomm(const char *host, int port, const char *string, ...) { int sock; char buffer[4096]; struct sockaddr_in saddr; va_list ap; /* Generate string */ memset(buffer, 0, sizeof(buffer)); va_start(ap, string); vsnprintf(buffer, sizeof(buffer)-1, string, ap); va_end(ap); + saddr.sin_family = AF_INET; + saddr.sin_port = htons(port); + saddr.sin_addr.s_addr = inet_addr(host); + /* Setup socket */ if((sock=socket(AF_INET, SOCK_DGRAM, 0)) < 0) { wl_logerr("Cannot set up socket: %s", strerror(errno)); return -1; } - saddr.sin_family = AF_INET; - saddr.sin_port = htons(port); - saddr.sin_addr.s_addr = inet_addr(host); - if(sendto(sock, buffer, sizeof(buffer), 0, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) { wl_logerr("Cannot write to socket: %s", strerror(errno)); close(sock); return 0; } if(close(sock) < 0) wl_logerr("Cannot close socket: %s", strerror(errno)); return 1; } + +/* Check for new messages on commsock */ +int recvcomm(int *sock, char *out, int maxlen) +{ + struct sockaddr_in *cliaddr; + socklen_t len=sizeof(struct sockaddr); + char buffer[128], retval[3]; + + memset(buffer, 0, sizeof(buffer)); + if(recvfrom(*sock, buffer, sizeof(buffer)-1, 0, (struct sockaddr *)cliaddr, &len) < 0) + return -1; + + memset(out, 0, maxlen); + memcpy(out, buffer, maxlen - 1); + + memset(retval, 0, sizeof(retval)); + memcpy(retval, out, 2); + + return atoi(retval); +} diff --git a/noncore/net/wellenreiter/libwellenreiter/source/sock.hh b/noncore/net/wellenreiter/libwellenreiter/source/sock.hh index afde641..611e335 100644 --- a/noncore/net/wellenreiter/libwellenreiter/source/sock.hh +++ b/noncore/net/wellenreiter/libwellenreiter/source/sock.hh @@ -1,19 +1,20 @@ /* $id */ #ifndef WLSOCK_HH #define WLSOCK_HH #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <signal.h> #include <string.h> #include <unistd.h> #include <stdlib.h> #include <errno.h> int commsock(const char *, int); int sendcomm(const char *, int, const char *, ...); +int recvcomm(int *, char *, int); #endif /* WLSOCK_HH */ |