author | mjm <mjm> | 2002-12-27 16:35:28 (UTC) |
---|---|---|
committer | mjm <mjm> | 2002-12-27 16:35:28 (UTC) |
commit | 3cb928900ffb1d2799bd7e8e1871aaf8d89b56cf (patch) (side-by-side diff) | |
tree | 180016a8feae9e40c3e6b8be62b518bdf8ef18fc /noncore/net/wellenreiter/libwellenreiter/source/wl_sock.cc | |
parent | e6aaae74f39e3aed4dc3c186c56f92cef8c7da09 (diff) | |
download | opie-3cb928900ffb1d2799bd7e8e1871aaf8d89b56cf.zip opie-3cb928900ffb1d2799bd7e8e1871aaf8d89b56cf.tar.gz opie-3cb928900ffb1d2799bd7e8e1871aaf8d89b56cf.tar.bz2 |
cleanup, new protocol, renaming of some functions, GUI has to update!
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 | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/noncore/net/wellenreiter/libwellenreiter/source/wl_sock.cc b/noncore/net/wellenreiter/libwellenreiter/source/wl_sock.cc new file mode 100644 index 0000000..5714afb --- a/dev/null +++ b/noncore/net/wellenreiter/libwellenreiter/source/wl_sock.cc @@ -0,0 +1,91 @@ +/* + * Socket operations for wellenreiter + * + * $Id$ + */ + +#include "wl_sock.hh" +#include "wl_log.hh" + +/* Setup UDP Socket for incoming commands */ +int wl_setupsock(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 wl_send(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; + } + + 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 -1; + } + + if(close(sock) < 0) + wl_logerr("Cannot close socket: %s", strerror(errno)); + + return 0; +} + +/* Check for new messages on commsock */ +int wl_recv(int *sock, char *out, int maxlen) +{ + struct sockaddr_in *cliaddr; + socklen_t len=sizeof(struct sockaddr); + char retval[3]; + + memset(out, 0, maxlen); + if(recvfrom(*sock, out, maxlen - 1, 0, (struct sockaddr *)cliaddr, &len) < 0) + return -1; + + /* Get packet type and return it */ + memset(retval, 0, sizeof(retval)); + memcpy(retval, out, 2); + + return atoi(retval); +} |