author | mjm <mjm> | 2002-11-22 23:39:26 (UTC) |
---|---|---|
committer | mjm <mjm> | 2002-11-22 23:39:26 (UTC) |
commit | 5a346c71bf5255eb769c9fceb34358c06e60aef3 (patch) (side-by-side diff) | |
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) (show 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 @@ +/* + * Socket operations for wellenreiter + * + * $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); + + /* 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; +} |