Diffstat (limited to 'noncore/net/wellenreiter/libwellenreiter/source/wl_sock.cc') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/net/wellenreiter/libwellenreiter/source/wl_sock.cc | 92 |
1 files changed, 0 insertions, 92 deletions
diff --git a/noncore/net/wellenreiter/libwellenreiter/source/wl_sock.cc b/noncore/net/wellenreiter/libwellenreiter/source/wl_sock.cc deleted file mode 100644 index 6e26cb8..0000000 --- a/noncore/net/wellenreiter/libwellenreiter/source/wl_sock.cc +++ b/dev/null | |||
@@ -1,92 +0,0 @@ | |||
1 | /* | ||
2 | * Socket operations for wellenreiter | ||
3 | * | ||
4 | * $Id$ | ||
5 | */ | ||
6 | |||
7 | #include "wl_sock.hh" | ||
8 | #include "wl_log.hh" | ||
9 | |||
10 | /* Setup UDP Socket for incoming commands */ | ||
11 | int wl_setupsock(const char *host, int port, struct sockaddr_in saddr) | ||
12 | { | ||
13 | int sock; | ||
14 | |||
15 | if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) | ||
16 | { | ||
17 | wl_logerr("Cannot set up socket: %s", strerror(errno)); | ||
18 | return -1; | ||
19 | } | ||
20 | |||
21 | memset(&saddr, 0, sizeof(saddr)); | ||
22 | saddr.sin_family = PF_INET; | ||
23 | saddr.sin_port = htons(port); | ||
24 | saddr.sin_addr.s_addr = inet_addr(host); | ||
25 | |||
26 | if(bind(sock,(struct sockaddr *)&saddr, sizeof(saddr)) < 0) | ||
27 | { | ||
28 | wl_logerr("Cannot bind socket: %s", strerror(errno)); | ||
29 | close(sock); | ||
30 | return -1; | ||
31 | } | ||
32 | |||
33 | return sock; | ||
34 | } | ||
35 | |||
36 | /* Send a string to commsock */ | ||
37 | int wl_send(const char *host, int port, const char *string, ...) | ||
38 | { | ||
39 | int sock; | ||
40 | char buffer[4096]; | ||
41 | struct sockaddr_in saddr; | ||
42 | va_list ap; | ||
43 | |||
44 | /* Generate string */ | ||
45 | memset(buffer, 0, sizeof(buffer)); | ||
46 | va_start(ap, string); | ||
47 | vsnprintf(buffer, sizeof(buffer)-1, string, ap); | ||
48 | va_end(ap); | ||
49 | |||
50 | saddr.sin_family = AF_INET; | ||
51 | saddr.sin_port = htons(port); | ||
52 | saddr.sin_addr.s_addr = inet_addr(host); | ||
53 | |||
54 | /* Setup socket */ | ||
55 | if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) | ||
56 | { | ||
57 | wl_logerr("Cannot set up socket: %s", strerror(errno)); | ||
58 | return 0; | ||
59 | } | ||
60 | |||
61 | if(sendto(sock, buffer, sizeof(buffer), 0, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) | ||
62 | { | ||
63 | wl_logerr("Cannot write to socket: %s", strerror(errno)); | ||
64 | close(sock); | ||
65 | return 0; | ||
66 | } | ||
67 | |||
68 | if(close(sock) < 0) | ||
69 | wl_logerr("Cannot close socket: %s", strerror(errno)); | ||
70 | |||
71 | return 1; | ||
72 | } | ||
73 | |||
74 | /* Check for new messages on commsock */ | ||
75 | int wl_recv(int *sock, struct sockaddr_in cliaddr, char *out, int maxlen) | ||
76 | { | ||
77 | socklen_t len = sizeof(struct sockaddr); | ||
78 | char retval[3]; | ||
79 | |||
80 | memset(out, 0, maxlen); | ||
81 | if(recvfrom(*sock, out, maxlen - 1, 0, (struct sockaddr *)&cliaddr, &len) < 0) | ||
82 | { | ||
83 | wl_logerr("Cannot receive from socket: %s", strerror(errno)); | ||
84 | return -1; | ||
85 | } | ||
86 | |||
87 | /* Get packet type and return it */ | ||
88 | memset(retval, 0, sizeof(retval)); | ||
89 | memcpy(retval, out, 2); | ||
90 | |||
91 | return atoi(retval); | ||
92 | } | ||