From d5cbb675a9f0c5f56d01401870e5eceb3eb3a6d8 Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Fri, 5 Aug 2011 18:02:47 +0200 Subject: pcap-client: Hacks to compile with an old libosmocore We are running a system where we do not want to upgrade libosmocore right now. Compile the osmo-pcap-client with a libosmocore from around January. --- configure.ac | 4 +- include/osmo-pcap/common.h | 2 +- include/osmo-pcap/osmo_pcap_client.h | 12 ++-- src/Makefile.am | 6 +- src/osmo_client_core.c | 8 +-- src/osmo_client_main.c | 48 +++++++++++---- src/osmo_client_network.c | 110 +++++++++++++++++++++++++++++++---- src/osmo_client_vty.c | 2 +- src/osmo_common.c | 2 +- 9 files changed, 154 insertions(+), 40 deletions(-) diff --git a/configure.ac b/configure.ac index 2f4de9d..39989da 100644 --- a/configure.ac +++ b/configure.ac @@ -15,8 +15,8 @@ AC_PROG_INSTALL AC_PROG_RANLIB dnl checks for libraries -PKG_CHECK_MODULES(LIBOSMOCORE, libosmocore >= 0.3.2) -PKG_CHECK_MODULES(LIBOSMOVTY, libosmovty >= 0.3.0) +PKG_CHECK_MODULES(LIBOSMOCORE, libosmocore < 0.2.0) +PKG_CHECK_MODULES(LIBOSMOVTY, libosmovty < 0.2.0) dnl checks for header files AC_HEADER_STDC diff --git a/include/osmo-pcap/common.h b/include/osmo-pcap/common.h index 9275f0a..22a0a08 100644 --- a/include/osmo-pcap/common.h +++ b/include/osmo-pcap/common.h @@ -23,7 +23,7 @@ #ifndef OSMO_PCAP_COMMON_H #define OSMO_PCAP_COMMON_H -#include +#include #include #include diff --git a/include/osmo-pcap/osmo_pcap_client.h b/include/osmo-pcap/osmo_pcap_client.h index 7aae7ca..0ba07d9 100644 --- a/include/osmo-pcap/osmo_pcap_client.h +++ b/include/osmo-pcap/osmo_pcap_client.h @@ -23,9 +23,9 @@ #include #include -#include -#include -#include +#include +#include +#include struct osmo_pcap_client { @@ -36,12 +36,12 @@ struct osmo_pcap_client { struct bpf_program bpf; char *filter_string; int filter_itself; - struct osmo_fd fd; + struct bsc_fd fd; char *srv_ip; int srv_port; - struct osmo_wqueue wqueue; - struct osmo_timer_list timer; + struct write_queue wqueue; + struct timer_list timer; }; extern struct osmo_pcap_client *pcap_client; diff --git a/src/Makefile.am b/src/Makefile.am index 402da22..30d6da6 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -2,12 +2,12 @@ INCLUDES = $(all_includes) -I$(top_srcdir)/include -I$(top_builddir)/ AM_CFLAGS = -Wall $(LIBOSMOCORE_CFLAGS) $(LIBOSMOVTY_CFLAGS) $(PCAP_CFLAGS) AM_LDFLAGS = $(LIBOSMOCORE_LIBS) $(LIBOSMOVTY_LIBS) -bin_PROGRAMS = osmo_pcap_client osmo_pcap_server +bin_PROGRAMS = osmo_pcap_client osmo_pcap_client_SOURCES = osmo_client_main.c osmo_common.c \ osmo_client_core.c osmo_client_vty.c \ osmo_client_network.c osmo_pcap_client_LDADD = $(PCAP_LIBS) -osmo_pcap_server_SOURCES = osmo_server_main.c osmo_common.c \ - osmo_server_vty.c osmo_server_network.c +#osmo_pcap_server_SOURCES = osmo_server_main.c osmo_common.c \ +# osmo_server_vty.c osmo_server_network.c diff --git a/src/osmo_client_core.c b/src/osmo_client_core.c index c8bd8bd..28f8a7e 100644 --- a/src/osmo_client_core.c +++ b/src/osmo_client_core.c @@ -23,7 +23,7 @@ #include #include -#include +#include #include @@ -32,7 +32,7 @@ #endif -static int pcap_read_cb(struct osmo_fd *fd, unsigned int what) +static int pcap_read_cb(struct bsc_fd *fd, unsigned int what) { struct osmo_pcap_client *client = fd->data; struct pcap_pkthdr hdr; @@ -86,7 +86,7 @@ static void free_all(struct osmo_pcap_client *client) pcap_freecode(&client->bpf); if (client->fd.fd >= 0) { - osmo_fd_unregister(&client->fd); + bsc_unregister_fd(&client->fd); client->fd.fd = -1; } @@ -127,7 +127,7 @@ int osmo_client_capture(struct osmo_pcap_client *client, const char *device) client->fd.when = BSC_FD_READ; client->fd.cb = pcap_read_cb; client->fd.data = client; - if (osmo_fd_register(&client->fd) != 0) { + if (bsc_register_fd(&client->fd) != 0) { LOGP(DCLIENT, LOGL_ERROR, "Failed to register the fd.\n"); client->fd.fd = -1; diff --git a/src/osmo_client_main.c b/src/osmo_client_main.c index 9ea0b66..91cfb0c 100644 --- a/src/osmo_client_main.c +++ b/src/osmo_client_main.c @@ -23,11 +23,10 @@ #include #include -#include -#include -#include -#include -#include +#include +#include +#include +#include #include #include @@ -43,14 +42,39 @@ #include "osmopcapconfig.h" + static const char *config_file = "osmo-pcap-client.cfg"; static int daemonize = 0; +static struct log_target *stderr_target = NULL; void *tall_bsc_ctx; struct osmo_pcap_client *pcap_client; extern void *tall_msgb_ctx; extern void *tall_ctr_ctx; + +/* drop in */ +static void osmo_init_ignore_signals(void) +{ + /* Signals that by default would terminate */ + signal(SIGPIPE, SIG_IGN); + signal(SIGALRM, SIG_IGN); + signal(SIGHUP, SIG_IGN); + signal(SIGIO, SIG_IGN); +} + +static int osmo_init_logging(const struct log_info *log_info) +{ + log_init(log_info); + stderr_target = log_target_create_stderr(); + if (!stderr_target) + return -1; + + log_add_target(stderr_target); + log_set_all_filter(stderr_target, 1); + return 0; +} + static struct vty_app_info vty_info = { .name = "OsmoPCAPClient", .version = PACKAGE_VERSION, @@ -104,16 +128,16 @@ static void handle_options(int argc, char **argv) daemonize = 1; break; case 'd': - log_parse_category_mask(osmo_stderr_target, optarg); + log_parse_category_mask(stderr_target, optarg); break; case 's': - log_set_use_color(osmo_stderr_target, 0); + log_set_use_color(stderr_target, 0); break; case 'T': - log_set_print_timestamp(osmo_stderr_target, 1); + log_set_print_timestamp(stderr_target, 1); break; case 'e': - log_set_log_level(osmo_stderr_target, atoi(optarg)); + log_set_log_level(stderr_target, atoi(optarg)); break; case 'c': config_file = strdup(optarg); @@ -161,7 +185,7 @@ int main(int argc, char **argv) vty_info.copyright = osmopcap_copyright; vty_init(&vty_info); - logging_vty_add_cmds(&log_info); + logging_vty_add_cmds(); /* parse options */ handle_options(argc, argv); @@ -188,7 +212,7 @@ int main(int argc, char **argv) vty_client_init(pcap_client); /* initialize the queue */ - osmo_wqueue_init(&pcap_client->wqueue, 10); + write_queue_init(&pcap_client->wqueue, 10); pcap_client->wqueue.bfd.fd = -1; @@ -210,7 +234,7 @@ int main(int argc, char **argv) } while (1) { - osmo_select_main(0); + bsc_select_main(0); } return(0); diff --git a/src/osmo_client_network.c b/src/osmo_client_network.c index bd1b9db..f6ada12 100644 --- a/src/osmo_client_network.c +++ b/src/osmo_client_network.c @@ -24,9 +24,8 @@ #include #include -#include -#include -#include +#include +#include #include @@ -37,6 +36,97 @@ #include #include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#define OSMO_SOCK_F_CONNECT (1 << 0) +#define OSMO_SOCK_F_BIND (1 << 1) +#define OSMO_SOCK_F_NONBLOCK (1 << 2) +int osmo_sock_init(uint16_t family, uint16_t type, uint8_t proto, + const char *host, uint16_t port, unsigned int flags) +{ + struct addrinfo hints, *result, *rp; + int sfd, rc, on = 1; + char portbuf[16]; + + if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == + (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) + return -EINVAL; + + sprintf(portbuf, "%u", port); + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_family = family; + hints.ai_socktype = type; + hints.ai_flags = 0; + hints.ai_protocol = proto; + + if (flags & OSMO_SOCK_F_BIND) + hints.ai_flags |= AI_PASSIVE; + + rc = getaddrinfo(host, portbuf, &hints, &result); + if (rc != 0) { + perror("getaddrinfo returned NULL"); + return -EINVAL; + } + + for (rp = result; rp != NULL; rp = rp->ai_next) { + sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); + if (sfd == -1) + continue; + if (flags & OSMO_SOCK_F_NONBLOCK) { + if (ioctl(sfd, FIONBIO, (unsigned char *)&on) < 0) { + perror("cannot set this socket unblocking"); + close(sfd); + return -EINVAL; + } + } + if (flags & OSMO_SOCK_F_CONNECT) { + rc = connect(sfd, rp->ai_addr, rp->ai_addrlen); + if (rc != -1 || (rc == -1 && errno == EINPROGRESS)) + break; + } else { + rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, + &on, sizeof(on)); + if (rc < 0) { + perror("cannot setsockopt socket"); + break; + } + if (bind(sfd, rp->ai_addr, rp->ai_addrlen) != -1) + break; + } + close(sfd); + } + freeaddrinfo(result); + + if (rp == NULL) { + perror("unable to connect/bind socket"); + return -ENODEV; + } + + setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); + + /* Make sure to call 'listen' on a bound, connection-oriented sock */ + if (flags & OSMO_SOCK_F_BIND) { + switch (type) { + case SOCK_STREAM: + case SOCK_SEQPACKET: + listen(sfd, 10); + break; + } + } + return sfd; +} + + static void _osmo_client_connect(void *_data) { osmo_client_connect((struct osmo_pcap_client *) _data); @@ -45,7 +135,7 @@ static void _osmo_client_connect(void *_data) static void lost_connection(struct osmo_pcap_client *client) { if (client->wqueue.bfd.fd >= 0) { - osmo_fd_unregister(&client->wqueue.bfd); + bsc_unregister_fd(&client->wqueue.bfd); close(client->wqueue.bfd.fd); client->wqueue.bfd.fd = -1; } @@ -53,19 +143,19 @@ static void lost_connection(struct osmo_pcap_client *client) client->timer.cb = _osmo_client_connect; client->timer.data = client; - osmo_timer_schedule(&client->timer, 2, 0); + bsc_schedule_timer(&client->timer, 2, 0); } static void write_data(struct osmo_pcap_client *client, struct msgb *msg) { - if (osmo_wqueue_enqueue(&client->wqueue, msg) != 0) { + if (write_queue_enqueue(&client->wqueue, msg) != 0) { LOGP(DCLIENT, LOGL_ERROR, "Failed to enqueue.\n"); msgb_free(msg); return; } } -static int read_cb(struct osmo_fd *fd) +static int read_cb(struct bsc_fd *fd) { char buf[4096]; int rc; @@ -81,7 +171,7 @@ static int read_cb(struct osmo_fd *fd) return 0; } -static int write_cb(struct osmo_fd *fd, struct msgb *msg) +static int write_cb(struct bsc_fd *fd, struct msgb *msg) { int rc; @@ -160,7 +250,7 @@ void osmo_client_connect(struct osmo_pcap_client *client) client->wqueue.write_cb = write_cb; client->wqueue.bfd.when = BSC_FD_READ; client->wqueue.bfd.data = client; - osmo_wqueue_clear(&client->wqueue); + write_queue_clear(&client->wqueue); fd = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, client->srv_ip, client->srv_port, OSMO_SOCK_F_CONNECT); @@ -173,7 +263,7 @@ void osmo_client_connect(struct osmo_pcap_client *client) } client->wqueue.bfd.fd = fd; - if (osmo_fd_register(&client->wqueue.bfd) != 0) { + if (bsc_register_fd(&client->wqueue.bfd) != 0) { LOGP(DCLIENT, LOGL_ERROR, "Failed to register to BFD.\n"); lost_connection(client); diff --git a/src/osmo_client_vty.c b/src/osmo_client_vty.c index 0b30eb7..bcf2e47 100644 --- a/src/osmo_client_vty.c +++ b/src/osmo_client_vty.c @@ -23,7 +23,7 @@ #include #include -#include +#include #include diff --git a/src/osmo_common.c b/src/osmo_common.c index b97394e..852be54 100644 --- a/src/osmo_common.c +++ b/src/osmo_common.c @@ -22,7 +22,7 @@ #include -#include +#include static const struct log_info_cat default_categories[] = { [DPCAP] = { -- cgit v1.2.3