diff --git a/examples/Makefile.am b/examples/Makefile.am index 415658d..ae718f3 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -15,7 +15,7 @@ libsxmp = ../lib/.libs/libsxmp.la bin_PROGRAMS = filelistd filelistc -filelistd_SOURCES = filelistd.c +filelistd_SOURCES = helpers.c filelistd.c filelistd_LDADD = $(LIBTDATA_LIBS) $(LIBSEXPR_LIBS) $(OPENSSL_LIBS) \ $(LIBUUID_LIBS) $(libsxmp) -lpthread diff --git a/examples/filelistd.c b/examples/filelistd.c index 8063b52..697d6dc 100644 --- a/examples/filelistd.c +++ b/examples/filelistd.c @@ -62,6 +62,7 @@ #include #include "filelist.h" +#include "helpers.h" /* * type used to stream a directory contents @@ -106,31 +107,6 @@ static inline void dump_dirent(struct dirent *d, char *buf) } } -/* just a stupid function to open listener on the specified port */ -static int __openlistener(int port) -{ - int sd; - struct sockaddr_in addr; - - sd = socket(PF_INET, SOCK_STREAM, 0); - memset(&addr, 0, sizeof(addr)); - - addr.sin_family = AF_INET; - addr.sin_port = htons(port); - addr.sin_addr.s_addr = INADDR_ANY; - - if(bind(sd, (struct sockaddr*)&addr, sizeof(addr)) != 0) { - perror("can't bind port"); - abort(); - } - if(listen(sd, 10) != 0) { - perror("Can't configure listening port"); - abort(); - } - - return sd; -} - /* firstly we need to take an rpc calls data, * sxmp uses usrtc from libtdata to store it */ @@ -421,7 +397,7 @@ int main(int argc, char **argv) signal(SIGPIPE, SIG_IGN); /* now we're ready to run the listen process */ - int srv = __openlistener(port); + int srv = openlistener_socket(port); while(1) { struct sockaddr_in addr; diff --git a/examples/helpers.c b/examples/helpers.c new file mode 100644 index 0000000..5ec229f --- /dev/null +++ b/examples/helpers.c @@ -0,0 +1,64 @@ +/* + * Secure X Message Passing Library v2 examples. + * + * (c) Originally written by somebody else ... + * (c) Alexander Vdolainen 2013-2015 + * + * libsxmp is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * libsxmp is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see ."; + * + * This is a simple helpers to make code more clean; + * + */ + +#include +#include +#define __USE_GNU +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int openlistener_socket(int port) +{ + int sd; + struct sockaddr_in addr; + + sd = socket(PF_INET, SOCK_STREAM, 0); + memset(&addr, 0, sizeof(addr)); + + addr.sin_family = AF_INET; + addr.sin_port = htons(port); + addr.sin_addr.s_addr = INADDR_ANY; + + if(bind(sd, (struct sockaddr*)&addr, sizeof(addr)) != 0) { + perror("can't bind port"); + abort(); + } + if(listen(sd, 10) != 0) { + perror("Can't configure listening port"); + abort(); + } + + return sd; +} + diff --git a/examples/helpers.h b/examples/helpers.h new file mode 100644 index 0000000..3b75d9c --- /dev/null +++ b/examples/helpers.h @@ -0,0 +1,27 @@ +/* + * Secure X Message Passing Library v2 examples. + * + * (c) Originally written by somebody else ... + * (c) Alexander Vdolainen 2013-2015 + * + * libsxmp is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * libsxmp is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see ."; + * + */ + +#ifndef __SXMP_EXAMPLES_HELPERS_H__ +#define __SXMP_EXAMPLES_HELPERS_H__ + +int openlistener_socket(int port); + +#endif /* __SXMP_EXAMPLES_HELPERS_H__ */