6 files changed, 209 insertions, 0 deletions
diff --git a/noncore/net/wellenreiter/libwellenreiter/docs/quickintro b/noncore/net/wellenreiter/libwellenreiter/docs/quickintro new file mode 100644 index 0000000..e4978a1 --- a/dev/null +++ b/noncore/net/wellenreiter/libwellenreiter/docs/quickintro @@ -0,0 +1,35 @@ + +Name: libwellenreiter +Author: Martin J. Muench + +NOTE: This is just a very short summary of the functions included +in libwellenreiter, written because a lack of time. The whole +library will be rewritten with extended documentation and +test programs. + +Included functions: + +Log to syslog/info: +void wl_loginfo(const char *, ...); + +Log to syslog/err: +void wl_logerr(const char *, ...); + +Use like printf() function with format strings and so on. + + +Setup udp socket for incoming commands: +int commsock(const char *, int); + +Fist argument is the host where it should listen and second +is the port. + +Return values: + +Send command to udp socket: +int sendcomm(const char *, int, const char *, ...); + +First two arguments are the host and port where the command should +be send to, followed by buffer that should be send. + +Thats it for the moment. diff --git a/noncore/net/wellenreiter/libwellenreiter/source/Makefile b/noncore/net/wellenreiter/libwellenreiter/source/Makefile new file mode 100644 index 0000000..05a5bd3 --- a/dev/null +++ b/noncore/net/wellenreiter/libwellenreiter/source/Makefile @@ -0,0 +1,17 @@ +# $Id$ + +INCLUDES = +LIBRARIES = +LIBOBJ = sock.o log.o +CXX = g++ -Wall -pedantic -g $(INCLUDES) -DDEBUG + +static: libwellenreiter.a +libwellenreiter.a: $(LIBOBJ) + ar -cr libwellenreiter.a $(LIBOBJ) + +shared: libwellenreiter.so +libwellenreiter.so: $(LIBOBJ) + $(CXX) -shared -o libwellenreiter.so $(LIBOBJ) + +clean: + rm -f *.o *.a *.so *~ diff --git a/noncore/net/wellenreiter/libwellenreiter/source/log.cc b/noncore/net/wellenreiter/libwellenreiter/source/log.cc new file mode 100644 index 0000000..af7f909 --- a/dev/null +++ b/noncore/net/wellenreiter/libwellenreiter/source/log.cc @@ -0,0 +1,51 @@ +/* + * Small functions to log to syslog + * + * $Id$ + */ + +#include "log.hh" + +/* Log to syslog INFO */ +void wl_loginfo(const char *fmt,...) +{ + + char buffer[4096]; + va_list ap; + + memset(buffer, 0, sizeof(buffer)); + va_start(ap, fmt); + vsnprintf(buffer, sizeof(buffer)-1, fmt, ap); + va_end(ap); + + openlog("libwellenreiter", LOG_PID, LOG_SYSLOG); + syslog(LOG_INFO, "(info) %s", buffer); + closelog(); + +#ifdef DEBUG + fprintf(stderr, "(info) %s\n", buffer); +#endif + +} + +/* Log to syslog ERR */ +void wl_logerr(const char *fmt,...) +{ + + char buffer[4096]; + va_list ap; + + memset(buffer, 0, sizeof(buffer)); + va_start(ap, fmt); + vsnprintf(buffer, sizeof(buffer)-1, fmt, ap); + va_end(ap); + + openlog("libwellenreiter", LOG_PID, LOG_SYSLOG); + syslog(LOG_INFO, "(err) %s", buffer); + closelog(); + +#ifdef DEBUG + fprintf(stderr, "(err) %s\n", buffer); +#endif + +} diff --git a/noncore/net/wellenreiter/libwellenreiter/source/log.hh b/noncore/net/wellenreiter/libwellenreiter/source/log.hh new file mode 100644 index 0000000..8f6e543 --- a/dev/null +++ b/noncore/net/wellenreiter/libwellenreiter/source/log.hh @@ -0,0 +1,14 @@ +/* $Id$ */ + +#ifndef WLLOG_HH +#define WLLOG_HH + +#include <stdio.h> +#include <syslog.h> +#include <stdarg.h> +#include <string.h> + +void wl_loginfo(const char *, ...); +void wl_logerr(const char *, ...); + +#endif /* WLLOG_HH */ 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; +} diff --git a/noncore/net/wellenreiter/libwellenreiter/source/sock.hh b/noncore/net/wellenreiter/libwellenreiter/source/sock.hh new file mode 100644 index 0000000..afde641 --- a/dev/null +++ b/noncore/net/wellenreiter/libwellenreiter/source/sock.hh @@ -0,0 +1,19 @@ +/* $id */ + +#ifndef WLSOCK_HH +#define WLSOCK_HH + +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <signal.h> +#include <string.h> +#include <unistd.h> +#include <stdlib.h> +#include <errno.h> + +int commsock(const char *, int); +int sendcomm(const char *, int, const char *, ...); + +#endif /* WLSOCK_HH */ |