author | mjm <mjm> | 2002-11-22 23:39:26 (UTC) |
---|---|---|
committer | mjm <mjm> | 2002-11-22 23:39:26 (UTC) |
commit | 5a346c71bf5255eb769c9fceb34358c06e60aef3 (patch) (unidiff) | |
tree | 6d600718317d56c411c2d91259e73780a99be60d /noncore/net/wellenreiter/libwellenreiter/source/sock.cc | |
parent | a71b9d79a05ab2b6bc891afe4df4d5fdcf641550 (diff) | |
download | opie-5a346c71bf5255eb769c9fceb34358c06e60aef3.zip opie-5a346c71bf5255eb769c9fceb34358c06e60aef3.tar.gz opie-5a346c71bf5255eb769c9fceb34358c06e60aef3.tar.bz2 |
ripped stuff from daemon and generalized it for libary usage
Diffstat (limited to 'noncore/net/wellenreiter/libwellenreiter/source/sock.cc') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/net/wellenreiter/libwellenreiter/source/sock.cc | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/noncore/net/wellenreiter/libwellenreiter/source/sock.cc b/noncore/net/wellenreiter/libwellenreiter/source/sock.cc new file mode 100644 index 0000000..2cc16eb --- a/dev/null +++ b/noncore/net/wellenreiter/libwellenreiter/source/sock.cc | |||
@@ -0,0 +1,73 @@ | |||
1 | /* | ||
2 | * Socket operations for wellenreiter | ||
3 | * | ||
4 | * $Id$ | ||
5 | */ | ||
6 | |||
7 | #include "sock.hh" | ||
8 | #include "log.hh" | ||
9 | |||
10 | /* Setup UDP Socket for incoming commands */ | ||
11 | int commsock(const char *host, int port) | ||
12 | { | ||
13 | struct sockaddr_in saddr; | ||
14 | int sock; | ||
15 | |||
16 | if((sock=socket(AF_INET, SOCK_DGRAM, 0)) < 0) | ||
17 | { | ||
18 | wl_logerr("Cannot set up socket: %s", strerror(errno)); | ||
19 | return -1; | ||
20 | } | ||
21 | |||
22 | memset(&saddr, 0, sizeof(saddr)); | ||
23 | saddr.sin_family = PF_INET; | ||
24 | saddr.sin_port = htons(port); | ||
25 | saddr.sin_addr.s_addr = inet_addr(host); | ||
26 | |||
27 | if(bind(sock,(struct sockaddr *)&saddr, sizeof(saddr)) < 0) | ||
28 | { | ||
29 | wl_logerr("Cannot bind socket: %s", strerror(errno)); | ||
30 | close(sock); | ||
31 | return -1; | ||
32 | } | ||
33 | |||
34 | return sock; | ||
35 | } | ||
36 | |||
37 | /* Send a string to commsock */ | ||
38 | int sendcomm(const char *host, int port, const char *string, ...) | ||
39 | { | ||
40 | int sock; | ||
41 | char buffer[4096]; | ||
42 | struct sockaddr_in saddr; | ||
43 | va_list ap; | ||
44 | |||
45 | /* Generate string */ | ||
46 | memset(buffer, 0, sizeof(buffer)); | ||
47 | va_start(ap, string); | ||
48 | vsnprintf(buffer, sizeof(buffer)-1, string, ap); | ||
49 | va_end(ap); | ||
50 | |||
51 | /* Setup socket */ | ||
52 | if((sock=socket(AF_INET, SOCK_DGRAM, 0)) < 0) | ||
53 | { | ||
54 | wl_logerr("Cannot set up socket: %s", strerror(errno)); | ||
55 | return -1; | ||
56 | } | ||
57 | |||
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) | ||
63 | { | ||
64 | wl_logerr("Cannot write to socket: %s", strerror(errno)); | ||
65 | close(sock); | ||
66 | return 0; | ||
67 | } | ||
68 | |||
69 | if(close(sock) < 0) | ||
70 | wl_logerr("Cannot close socket: %s", strerror(errno)); | ||
71 | |||
72 | return 1; | ||
73 | } | ||