summaryrefslogtreecommitdiff
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/net/wellenreiter/libwellenreiter/source/sock.cc28
-rw-r--r--noncore/net/wellenreiter/libwellenreiter/source/sock.hh1
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
@@ -1,73 +1,93 @@
1/* 1/*
2 * Socket operations for wellenreiter 2 * Socket operations for wellenreiter
3 * 3 *
4 * $Id$ 4 * $Id$
5 */ 5 */
6 6
7#include "sock.hh" 7#include "sock.hh"
8#include "log.hh" 8#include "log.hh"
9 9
10/* Setup UDP Socket for incoming commands */ 10/* Setup UDP Socket for incoming commands */
11int commsock(const char *host, int port) 11int commsock(const char *host, int port)
12{ 12{
13 struct sockaddr_in saddr; 13 struct sockaddr_in saddr;
14 int sock; 14 int sock;
15 15
16 if((sock=socket(AF_INET, SOCK_DGRAM, 0)) < 0) 16 if((sock=socket(AF_INET, SOCK_DGRAM, 0)) < 0)
17 { 17 {
18 wl_logerr("Cannot set up socket: %s", strerror(errno)); 18 wl_logerr("Cannot set up socket: %s", strerror(errno));
19 return -1; 19 return -1;
20 } 20 }
21 21
22 memset(&saddr, 0, sizeof(saddr)); 22 memset(&saddr, 0, sizeof(saddr));
23 saddr.sin_family = PF_INET; 23 saddr.sin_family = PF_INET;
24 saddr.sin_port = htons(port); 24 saddr.sin_port = htons(port);
25 saddr.sin_addr.s_addr = inet_addr(host); 25 saddr.sin_addr.s_addr = inet_addr(host);
26 26
27 if(bind(sock,(struct sockaddr *)&saddr, sizeof(saddr)) < 0) 27 if(bind(sock,(struct sockaddr *)&saddr, sizeof(saddr)) < 0)
28 { 28 {
29 wl_logerr("Cannot bind socket: %s", strerror(errno)); 29 wl_logerr("Cannot bind socket: %s", strerror(errno));
30 close(sock); 30 close(sock);
31 return -1; 31 return -1;
32 } 32 }
33 33
34 return sock; 34 return sock;
35} 35}
36 36
37/* Send a string to commsock */ 37/* Send a string to commsock */
38int sendcomm(const char *host, int port, const char *string, ...) 38int sendcomm(const char *host, int port, const char *string, ...)
39{ 39{
40 int sock; 40 int sock;
41 char buffer[4096]; 41 char buffer[4096];
42 struct sockaddr_in saddr; 42 struct sockaddr_in saddr;
43 va_list ap; 43 va_list ap;
44 44
45 /* Generate string */ 45 /* Generate string */
46 memset(buffer, 0, sizeof(buffer)); 46 memset(buffer, 0, sizeof(buffer));
47 va_start(ap, string); 47 va_start(ap, string);
48 vsnprintf(buffer, sizeof(buffer)-1, string, ap); 48 vsnprintf(buffer, sizeof(buffer)-1, string, ap);
49 va_end(ap); 49 va_end(ap);
50 50
51 saddr.sin_family = AF_INET;
52 saddr.sin_port = htons(port);
53 saddr.sin_addr.s_addr = inet_addr(host);
54
51 /* Setup socket */ 55 /* Setup socket */
52 if((sock=socket(AF_INET, SOCK_DGRAM, 0)) < 0) 56 if((sock=socket(AF_INET, SOCK_DGRAM, 0)) < 0)
53 { 57 {
54 wl_logerr("Cannot set up socket: %s", strerror(errno)); 58 wl_logerr("Cannot set up socket: %s", strerror(errno));
55 return -1; 59 return -1;
56 } 60 }
57 61
58 saddr.sin_family = AF_INET;
59 saddr.sin_port = htons(port);
60 saddr.sin_addr.s_addr = inet_addr(host);
61
62 if(sendto(sock, buffer, sizeof(buffer), 0, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) 62 if(sendto(sock, buffer, sizeof(buffer), 0, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
63 { 63 {
64 wl_logerr("Cannot write to socket: %s", strerror(errno)); 64 wl_logerr("Cannot write to socket: %s", strerror(errno));
65 close(sock); 65 close(sock);
66 return 0; 66 return 0;
67 } 67 }
68 68
69 if(close(sock) < 0) 69 if(close(sock) < 0)
70 wl_logerr("Cannot close socket: %s", strerror(errno)); 70 wl_logerr("Cannot close socket: %s", strerror(errno));
71 71
72 return 1; 72 return 1;
73} 73}
74
75/* Check for new messages on commsock */
76int recvcomm(int *sock, char *out, int maxlen)
77{
78 struct sockaddr_in *cliaddr;
79 socklen_t len=sizeof(struct sockaddr);
80 char buffer[128], retval[3];
81
82 memset(buffer, 0, sizeof(buffer));
83 if(recvfrom(*sock, buffer, sizeof(buffer)-1, 0, (struct sockaddr *)cliaddr, &len) < 0)
84 return -1;
85
86 memset(out, 0, maxlen);
87 memcpy(out, buffer, maxlen - 1);
88
89 memset(retval, 0, sizeof(retval));
90 memcpy(retval, out, 2);
91
92 return atoi(retval);
93}
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 @@
1/* $id */ 1/* $id */
2 2
3#ifndef WLSOCK_HH 3#ifndef WLSOCK_HH
4#define WLSOCK_HH 4#define WLSOCK_HH
5 5
6#include <sys/types.h> 6#include <sys/types.h>
7#include <sys/socket.h> 7#include <sys/socket.h>
8#include <netinet/in.h> 8#include <netinet/in.h>
9#include <arpa/inet.h> 9#include <arpa/inet.h>
10#include <signal.h> 10#include <signal.h>
11#include <string.h> 11#include <string.h>
12#include <unistd.h> 12#include <unistd.h>
13#include <stdlib.h> 13#include <stdlib.h>
14#include <errno.h> 14#include <errno.h>
15 15
16int commsock(const char *, int); 16int commsock(const char *, int);
17int sendcomm(const char *, int, const char *, ...); 17int sendcomm(const char *, int, const char *, ...);
18int recvcomm(int *, char *, int);
18 19
19#endif /* WLSOCK_HH */ 20#endif /* WLSOCK_HH */