2011-10-03 20:09:45 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <arpa/inet.h>
|
2015-02-25 10:17:51 +00:00
|
|
|
#include <netinet/in.h>
|
2017-04-11 16:31:38 +00:00
|
|
|
#include <netinet/tcp.h>
|
2011-10-03 20:09:45 +00:00
|
|
|
|
2011-10-05 10:31:43 +00:00
|
|
|
#include <osmocom/core/timer.h>
|
2011-10-03 20:09:45 +00:00
|
|
|
#include <osmocom/core/select.h>
|
2017-04-08 20:06:37 +00:00
|
|
|
#include <osmocom/core/utils.h>
|
2011-10-03 20:09:45 +00:00
|
|
|
#include <osmocom/gsm/tlv.h>
|
|
|
|
#include <osmocom/core/msgb.h>
|
|
|
|
#include <osmocom/core/logging.h>
|
|
|
|
#include <osmocom/core/talloc.h>
|
|
|
|
#include <osmocom/core/socket.h>
|
|
|
|
|
|
|
|
#include <osmocom/netif/stream.h>
|
|
|
|
|
2015-12-21 19:54:19 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBSCTP
|
|
|
|
#include <netinet/sctp.h>
|
|
|
|
#endif
|
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \addtogroup stream Osmocom Stream Socket
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \file stream.c
|
|
|
|
* \brief Osmocom stream socket helpers
|
|
|
|
*/
|
|
|
|
|
2016-11-02 17:22:08 +00:00
|
|
|
/*
|
|
|
|
* Platforms that don't have MSG_NOSIGNAL (which disables SIGPIPE)
|
|
|
|
* usually have SO_NOSIGPIPE (set via setsockopt).
|
|
|
|
*/
|
|
|
|
#ifndef MSG_NOSIGNAL
|
|
|
|
#define MSG_NOSIGNAL 0
|
|
|
|
#endif
|
|
|
|
|
2015-12-21 19:54:19 +00:00
|
|
|
static int sctp_sock_activate_events(int fd)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_LIBSCTP
|
|
|
|
struct sctp_event_subscribe event;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* subscribe for all events */
|
|
|
|
memset((uint8_t *)&event, 1, sizeof(event));
|
|
|
|
rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS,
|
|
|
|
&event, sizeof(event));
|
|
|
|
|
2016-11-26 14:42:16 +00:00
|
|
|
if (rc < 0)
|
|
|
|
LOGP(DLINP, LOGL_ERROR, "coudldn't activate SCTP events "
|
|
|
|
"on FD %u\n", fd);
|
2015-12-21 19:54:19 +00:00
|
|
|
return rc;
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-04-11 16:31:38 +00:00
|
|
|
static int setsockopt_nodelay(int fd, int proto, int on)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
switch (proto) {
|
|
|
|
case IPPROTO_SCTP:
|
|
|
|
rc = setsockopt(fd, IPPROTO_SCTP, SCTP_NODELAY, &on, sizeof(on));
|
|
|
|
break;
|
|
|
|
case IPPROTO_TCP:
|
|
|
|
rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rc = -1;
|
|
|
|
LOGP(DLINP, LOGL_ERROR, "Unknown protocol %u, cannot set NODELAY\n",
|
|
|
|
proto);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-03 20:09:45 +00:00
|
|
|
/*
|
|
|
|
* Client side.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 19:14:38 +00:00
|
|
|
enum osmo_stream_cli_state {
|
|
|
|
STREAM_CLI_STATE_NONE = 0,
|
|
|
|
STREAM_CLI_STATE_CONNECTING = 1,
|
|
|
|
STREAM_CLI_STATE_CONNECTED = 2,
|
|
|
|
STREAM_CLI_STATE_MAX
|
2011-10-03 20:09:45 +00:00
|
|
|
};
|
|
|
|
|
2012-08-15 19:14:38 +00:00
|
|
|
#define OSMO_STREAM_CLI_F_RECONF (1 << 0)
|
2017-04-11 16:31:38 +00:00
|
|
|
#define OSMO_STREAM_CLI_F_NODELAY (1 << 1)
|
2011-10-09 18:18:49 +00:00
|
|
|
|
2012-08-15 19:14:38 +00:00
|
|
|
struct osmo_stream_cli {
|
2011-10-03 20:09:45 +00:00
|
|
|
struct osmo_fd ofd;
|
|
|
|
struct llist_head tx_queue;
|
|
|
|
struct osmo_timer_list timer;
|
2012-08-15 19:14:38 +00:00
|
|
|
enum osmo_stream_cli_state state;
|
2017-04-08 20:09:57 +00:00
|
|
|
char *addr;
|
2011-10-03 20:09:45 +00:00
|
|
|
uint16_t port;
|
2017-04-08 20:06:37 +00:00
|
|
|
char *local_addr;
|
2017-04-08 17:48:05 +00:00
|
|
|
uint16_t local_port;
|
2015-12-21 19:54:19 +00:00
|
|
|
uint16_t proto;
|
2012-08-15 19:14:38 +00:00
|
|
|
int (*connect_cb)(struct osmo_stream_cli *srv);
|
|
|
|
int (*read_cb)(struct osmo_stream_cli *srv);
|
|
|
|
int (*write_cb)(struct osmo_stream_cli *srv);
|
2011-10-03 20:09:45 +00:00
|
|
|
void *data;
|
2011-10-09 18:18:49 +00:00
|
|
|
int flags;
|
2011-11-08 09:10:10 +00:00
|
|
|
int reconnect_timeout;
|
2011-10-03 20:09:45 +00:00
|
|
|
};
|
|
|
|
|
2012-08-15 19:14:38 +00:00
|
|
|
void osmo_stream_cli_close(struct osmo_stream_cli *cli);
|
2011-10-03 20:09:45 +00:00
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Re-connect an Osmocom Stream Client
|
|
|
|
* If re-connection is enabled for this client, we close any existing
|
|
|
|
* connection (if any) and schedule a re-connect timer */
|
2017-03-17 11:02:52 +00:00
|
|
|
void osmo_stream_cli_reconnect(struct osmo_stream_cli *cli)
|
2011-10-03 20:09:45 +00:00
|
|
|
{
|
2012-08-15 19:14:38 +00:00
|
|
|
if (cli->reconnect_timeout < 0) {
|
2011-11-08 09:10:10 +00:00
|
|
|
LOGP(DLINP, LOGL_DEBUG, "not reconnecting, disabled.\n");
|
|
|
|
return;
|
|
|
|
}
|
2011-10-03 20:09:45 +00:00
|
|
|
LOGP(DLINP, LOGL_DEBUG, "connection closed\n");
|
2012-08-15 19:14:38 +00:00
|
|
|
osmo_stream_cli_close(cli);
|
2011-11-08 09:10:10 +00:00
|
|
|
LOGP(DLINP, LOGL_DEBUG, "retrying in %d seconds...\n",
|
2012-08-15 19:14:38 +00:00
|
|
|
cli->reconnect_timeout);
|
|
|
|
osmo_timer_schedule(&cli->timer, cli->reconnect_timeout, 0);
|
|
|
|
cli->state = STREAM_CLI_STATE_CONNECTING;
|
2011-10-03 20:09:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Close an Osmocom Stream Client
|
|
|
|
* \param[in] cli Osmocom Stream Client to be closed
|
|
|
|
* We unregister the socket fd from the osmocom select() loop
|
|
|
|
* abstraction and close the socket */
|
2012-08-15 19:14:38 +00:00
|
|
|
void osmo_stream_cli_close(struct osmo_stream_cli *cli)
|
2011-10-03 20:09:45 +00:00
|
|
|
{
|
2017-03-16 23:15:38 +00:00
|
|
|
if (cli->ofd.fd == -1)
|
|
|
|
return;
|
2012-08-15 19:14:38 +00:00
|
|
|
osmo_fd_unregister(&cli->ofd);
|
|
|
|
close(cli->ofd.fd);
|
2017-03-16 23:15:38 +00:00
|
|
|
cli->ofd.fd = -1;
|
2011-10-03 20:09:45 +00:00
|
|
|
}
|
|
|
|
|
2012-08-15 19:14:38 +00:00
|
|
|
static void osmo_stream_cli_read(struct osmo_stream_cli *cli)
|
2011-10-03 20:09:45 +00:00
|
|
|
{
|
|
|
|
LOGP(DLINP, LOGL_DEBUG, "message received\n");
|
|
|
|
|
2012-08-15 19:14:38 +00:00
|
|
|
if (cli->read_cb)
|
|
|
|
cli->read_cb(cli);
|
2011-10-03 20:09:45 +00:00
|
|
|
}
|
|
|
|
|
2012-08-15 19:14:38 +00:00
|
|
|
static int osmo_stream_cli_write(struct osmo_stream_cli *cli)
|
2011-10-03 20:09:45 +00:00
|
|
|
{
|
2015-12-21 19:54:19 +00:00
|
|
|
#ifdef HAVE_LIBSCTP
|
|
|
|
struct sctp_sndrcvinfo sinfo;
|
|
|
|
#endif
|
2011-10-03 20:09:45 +00:00
|
|
|
struct msgb *msg;
|
|
|
|
struct llist_head *lh;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
LOGP(DLINP, LOGL_DEBUG, "sending data\n");
|
|
|
|
|
2012-08-15 19:14:38 +00:00
|
|
|
if (llist_empty(&cli->tx_queue)) {
|
|
|
|
cli->ofd.when &= ~BSC_FD_WRITE;
|
2011-10-03 20:09:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-08-15 19:14:38 +00:00
|
|
|
lh = cli->tx_queue.next;
|
2011-10-03 20:09:45 +00:00
|
|
|
llist_del(lh);
|
|
|
|
msg = llist_entry(lh, struct msgb, list);
|
|
|
|
|
2012-08-15 19:14:38 +00:00
|
|
|
if (cli->state == STREAM_CLI_STATE_CONNECTING) {
|
2011-10-03 20:09:45 +00:00
|
|
|
LOGP(DLINP, LOGL_ERROR, "not connected, dropping data!\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-12-21 19:54:19 +00:00
|
|
|
switch (cli->proto) {
|
|
|
|
#ifdef HAVE_LIBSCTP
|
|
|
|
case IPPROTO_SCTP:
|
2015-12-25 16:47:19 +00:00
|
|
|
memset(&sinfo, 0, sizeof(sinfo));
|
2017-04-12 10:08:45 +00:00
|
|
|
sinfo.sinfo_ppid = htonl(msgb_sctp_ppid(msg));
|
2017-04-10 15:48:42 +00:00
|
|
|
sinfo.sinfo_stream = msgb_sctp_stream(msg);
|
2015-12-21 19:54:19 +00:00
|
|
|
ret = sctp_send(cli->ofd.fd, msg->data, msgb_length(msg),
|
|
|
|
&sinfo, MSG_NOSIGNAL);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case IPPROTO_TCP:
|
|
|
|
default:
|
|
|
|
ret = send(cli->ofd.fd, msg->data, msg->len, 0);
|
|
|
|
break;
|
|
|
|
}
|
2011-10-03 20:09:45 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
if (errno == EPIPE || errno == ENOTCONN) {
|
2012-08-15 19:14:38 +00:00
|
|
|
osmo_stream_cli_reconnect(cli);
|
2011-10-03 20:09:45 +00:00
|
|
|
}
|
|
|
|
LOGP(DLINP, LOGL_ERROR, "error to send\n");
|
|
|
|
}
|
|
|
|
msgb_free(msg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-15 19:14:38 +00:00
|
|
|
static int osmo_stream_cli_fd_cb(struct osmo_fd *ofd, unsigned int what)
|
2011-10-03 20:09:45 +00:00
|
|
|
{
|
2012-08-15 19:14:38 +00:00
|
|
|
struct osmo_stream_cli *cli = ofd->data;
|
2011-10-03 20:09:45 +00:00
|
|
|
int error, ret;
|
|
|
|
socklen_t len = sizeof(error);
|
|
|
|
|
2012-08-15 19:14:38 +00:00
|
|
|
switch(cli->state) {
|
|
|
|
case STREAM_CLI_STATE_CONNECTING:
|
2011-10-03 20:09:45 +00:00
|
|
|
ret = getsockopt(ofd->fd, SOL_SOCKET, SO_ERROR, &error, &len);
|
|
|
|
if (ret >= 0 && error > 0) {
|
2012-08-15 19:14:38 +00:00
|
|
|
osmo_stream_cli_reconnect(cli);
|
2011-10-03 20:09:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ofd->when &= ~BSC_FD_WRITE;
|
|
|
|
LOGP(DLINP, LOGL_DEBUG, "connection done.\n");
|
2012-08-15 19:14:38 +00:00
|
|
|
cli->state = STREAM_CLI_STATE_CONNECTED;
|
2016-11-02 17:22:08 +00:00
|
|
|
if (cli->proto == IPPROTO_SCTP) {
|
|
|
|
#ifdef SO_NOSIGPIPE
|
|
|
|
int val = 1;
|
|
|
|
|
|
|
|
ret = setsockopt(ofd->fd, SOL_SOCKET, SO_NOSIGPIPE, (void*)&val, sizeof(val));
|
|
|
|
if (ret < 0)
|
|
|
|
LOGP(DLINP, LOGL_DEBUG, "Failed setting SO_NOSIGPIPE: %s\n", strerror(errno));
|
|
|
|
#endif
|
2015-12-21 19:54:19 +00:00
|
|
|
sctp_sock_activate_events(ofd->fd);
|
2016-11-02 17:22:08 +00:00
|
|
|
}
|
2012-08-15 19:14:38 +00:00
|
|
|
if (cli->connect_cb)
|
|
|
|
cli->connect_cb(cli);
|
2011-10-03 20:09:45 +00:00
|
|
|
break;
|
2012-08-15 19:14:38 +00:00
|
|
|
case STREAM_CLI_STATE_CONNECTED:
|
2011-10-03 20:09:45 +00:00
|
|
|
if (what & BSC_FD_READ) {
|
|
|
|
LOGP(DLINP, LOGL_DEBUG, "connected read\n");
|
2012-08-15 19:14:38 +00:00
|
|
|
osmo_stream_cli_read(cli);
|
2011-10-03 20:09:45 +00:00
|
|
|
}
|
|
|
|
if (what & BSC_FD_WRITE) {
|
|
|
|
LOGP(DLINP, LOGL_DEBUG, "connected write\n");
|
2012-08-15 19:14:38 +00:00
|
|
|
osmo_stream_cli_write(cli);
|
2011-10-03 20:09:45 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-15 19:14:38 +00:00
|
|
|
static void cli_timer_cb(void *data);
|
2011-10-03 20:09:45 +00:00
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Create an Osmocom stream client
|
|
|
|
* \param[in] ctx talloc context from which to allocate memory
|
|
|
|
* This function allocates a new \ref osmo_stream_cli and initializes
|
|
|
|
* it with default values (5s reconnect timer, TCP protocol) */
|
2012-08-15 19:14:38 +00:00
|
|
|
struct osmo_stream_cli *osmo_stream_cli_create(void *ctx)
|
2011-10-03 20:09:45 +00:00
|
|
|
{
|
2012-08-15 19:14:38 +00:00
|
|
|
struct osmo_stream_cli *cli;
|
2011-10-03 20:09:45 +00:00
|
|
|
|
2012-08-15 19:14:38 +00:00
|
|
|
cli = talloc_zero(ctx, struct osmo_stream_cli);
|
|
|
|
if (!cli)
|
2011-10-03 20:09:45 +00:00
|
|
|
return NULL;
|
|
|
|
|
2015-12-21 19:54:19 +00:00
|
|
|
cli->proto = IPPROTO_TCP;
|
2012-08-15 19:14:38 +00:00
|
|
|
cli->ofd.fd = -1;
|
|
|
|
cli->ofd.when |= BSC_FD_READ | BSC_FD_WRITE;
|
|
|
|
cli->ofd.priv_nr = 0; /* XXX */
|
|
|
|
cli->ofd.cb = osmo_stream_cli_fd_cb;
|
|
|
|
cli->ofd.data = cli;
|
|
|
|
cli->state = STREAM_CLI_STATE_CONNECTING;
|
|
|
|
cli->timer.cb = cli_timer_cb;
|
2015-12-22 22:53:55 +00:00
|
|
|
cli->timer.data = cli;
|
2012-08-15 19:14:38 +00:00
|
|
|
cli->reconnect_timeout = 5; /* default is 5 seconds. */
|
|
|
|
INIT_LLIST_HEAD(&cli->tx_queue);
|
2011-10-03 20:09:45 +00:00
|
|
|
|
2012-08-15 19:14:38 +00:00
|
|
|
return cli;
|
2011-10-03 20:09:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Set the remote address to which we connect
|
|
|
|
* \param[in] cli Stream Client to modify
|
|
|
|
* \param[in] addr Remote IP address
|
|
|
|
*/
|
2011-10-03 20:09:45 +00:00
|
|
|
void
|
2012-08-15 19:14:38 +00:00
|
|
|
osmo_stream_cli_set_addr(struct osmo_stream_cli *cli, const char *addr)
|
2011-10-03 20:09:45 +00:00
|
|
|
{
|
2017-04-08 20:09:57 +00:00
|
|
|
osmo_talloc_replace_string(cli, &cli->addr, addr);
|
2012-08-15 19:14:38 +00:00
|
|
|
cli->flags |= OSMO_STREAM_CLI_F_RECONF;
|
2011-10-03 20:09:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Set the remote port number to which we connect
|
|
|
|
* \param[in] cli Stream Client to modify
|
|
|
|
* \param[in] port Remote port number
|
|
|
|
*/
|
2011-10-03 20:09:45 +00:00
|
|
|
void
|
2012-08-15 19:14:38 +00:00
|
|
|
osmo_stream_cli_set_port(struct osmo_stream_cli *cli, uint16_t port)
|
2011-10-03 20:09:45 +00:00
|
|
|
{
|
2012-08-15 19:14:38 +00:00
|
|
|
cli->port = port;
|
|
|
|
cli->flags |= OSMO_STREAM_CLI_F_RECONF;
|
2011-10-03 20:09:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-08 20:06:37 +00:00
|
|
|
/*! \brief Set the local port number for the socket (to be bound to)
|
2017-04-08 17:48:05 +00:00
|
|
|
* \param[in] cli Stream Client to modify
|
|
|
|
* \param[in] port Local port number
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
osmo_stream_cli_set_local_port(struct osmo_stream_cli *cli, uint16_t port)
|
|
|
|
{
|
|
|
|
cli->local_port = port;
|
|
|
|
cli->flags |= OSMO_STREAM_CLI_F_RECONF;
|
|
|
|
}
|
|
|
|
|
2017-04-08 20:06:37 +00:00
|
|
|
/*! \brief Set the local address for the socket (to be bound to)
|
|
|
|
* \param[in] cli Stream Client to modify
|
|
|
|
* \param[in] port Local host name
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
osmo_stream_cli_set_local_addr(struct osmo_stream_cli *cli, const char *addr)
|
|
|
|
{
|
|
|
|
osmo_talloc_replace_string(cli, &cli->local_addr, addr);
|
|
|
|
cli->flags |= OSMO_STREAM_CLI_F_RECONF;
|
|
|
|
}
|
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Set the protocol for the stream client socket
|
|
|
|
* \param[in] cli Stream Client to modify
|
|
|
|
* \param[in] proto Protocol (like IPPROTO_TCP (default), IPPROTO_SCTP, ...)
|
|
|
|
*/
|
2015-12-21 19:54:19 +00:00
|
|
|
void
|
|
|
|
osmo_stream_cli_set_proto(struct osmo_stream_cli *cli, uint16_t proto)
|
|
|
|
{
|
|
|
|
cli->proto = proto;
|
|
|
|
cli->flags |= OSMO_STREAM_CLI_F_RECONF;
|
|
|
|
}
|
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Set the reconnect time of the stream client socket
|
|
|
|
* \param[in] cli Stream Client to modify
|
|
|
|
* \param[in] timeout Re-connect timeout in seconds */
|
2012-08-15 19:14:38 +00:00
|
|
|
void
|
|
|
|
osmo_stream_cli_set_reconnect_timeout(struct osmo_stream_cli *cli, int timeout)
|
2011-11-08 09:10:10 +00:00
|
|
|
{
|
2012-08-15 19:14:38 +00:00
|
|
|
cli->reconnect_timeout = timeout;
|
2011-11-08 09:10:10 +00:00
|
|
|
}
|
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Set application private data of the stream client socket
|
|
|
|
* \param[in] cli Stream Client to modify
|
|
|
|
* \param[in] data User-specific data (available in call-back functions) */
|
2011-10-03 20:09:45 +00:00
|
|
|
void
|
2012-08-15 19:14:38 +00:00
|
|
|
osmo_stream_cli_set_data(struct osmo_stream_cli *cli, void *data)
|
2011-10-03 20:09:45 +00:00
|
|
|
{
|
2012-08-15 19:14:38 +00:00
|
|
|
cli->data = data;
|
2011-10-03 20:09:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Get application private data of the stream client socket
|
|
|
|
* \param[in] cli Stream Client to modif
|
|
|
|
* \returns Application private data, as set by \ref osmo_stream_cli_set_data() */
|
2012-08-15 19:14:38 +00:00
|
|
|
void *osmo_stream_cli_get_data(struct osmo_stream_cli *cli)
|
2011-10-09 19:19:13 +00:00
|
|
|
{
|
2012-08-15 19:14:38 +00:00
|
|
|
return cli->data;
|
2011-10-09 19:19:13 +00:00
|
|
|
}
|
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Get Osmocom File Descriptor of the stream client socket
|
|
|
|
* \param[in] cli Stream Client to modif
|
|
|
|
* \returns Pointer to \ref osmo_fd */
|
2011-10-17 17:49:33 +00:00
|
|
|
struct osmo_fd *
|
2012-08-15 19:14:38 +00:00
|
|
|
osmo_stream_cli_get_ofd(struct osmo_stream_cli *cli)
|
2011-10-17 17:49:33 +00:00
|
|
|
{
|
2012-08-15 19:14:38 +00:00
|
|
|
return &cli->ofd;
|
2011-10-17 17:49:33 +00:00
|
|
|
}
|
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Set the call-back function called on connect of the stream client socket
|
|
|
|
* \param[in] cli Stream Client to modif
|
|
|
|
* \param[in] connect_cb Call-back function to be called upon connect */
|
2011-10-03 20:09:45 +00:00
|
|
|
void
|
2012-08-15 19:14:38 +00:00
|
|
|
osmo_stream_cli_set_connect_cb(struct osmo_stream_cli *cli,
|
|
|
|
int (*connect_cb)(struct osmo_stream_cli *cli))
|
2011-10-03 20:09:45 +00:00
|
|
|
{
|
2012-08-15 19:14:38 +00:00
|
|
|
cli->connect_cb = connect_cb;
|
2011-10-03 20:09:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Set the call-back function called to read from the stream client socket
|
|
|
|
* \param[in] cli Stream Client to modif
|
|
|
|
* \param[in] read_cb Call-back function to be called when we want to read */
|
2011-10-03 20:09:45 +00:00
|
|
|
void
|
2012-08-15 19:14:38 +00:00
|
|
|
osmo_stream_cli_set_read_cb(struct osmo_stream_cli *cli,
|
|
|
|
int (*read_cb)(struct osmo_stream_cli *cli))
|
2011-10-03 20:09:45 +00:00
|
|
|
{
|
2012-08-15 19:14:38 +00:00
|
|
|
cli->read_cb = read_cb;
|
2011-10-03 20:09:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-08 20:17:19 +00:00
|
|
|
/*! \brief Destroy a Osmocom stream client (includes close)
|
2017-04-08 17:48:05 +00:00
|
|
|
* \param[in] cli Stream Client to destroy */
|
2012-08-15 19:14:38 +00:00
|
|
|
void osmo_stream_cli_destroy(struct osmo_stream_cli *cli)
|
2011-10-03 20:09:45 +00:00
|
|
|
{
|
2017-04-08 20:17:19 +00:00
|
|
|
osmo_stream_cli_close(cli);
|
2017-03-16 23:16:08 +00:00
|
|
|
osmo_timer_del(&cli->timer);
|
2015-12-22 22:40:47 +00:00
|
|
|
talloc_free(cli);
|
2011-10-03 20:09:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Open connection of an Osmocom stream client
|
|
|
|
* \param[in] cli Stream Client to connect
|
|
|
|
* \param[in] reconect 1 if we should not automatically reconnect
|
|
|
|
*/
|
2015-12-22 22:54:21 +00:00
|
|
|
int osmo_stream_cli_open2(struct osmo_stream_cli *cli, int reconnect)
|
2011-10-03 20:09:45 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2011-10-09 18:18:49 +00:00
|
|
|
/* we are reconfiguring this socket, close existing first. */
|
2012-08-15 19:14:38 +00:00
|
|
|
if ((cli->flags & OSMO_STREAM_CLI_F_RECONF) && cli->ofd.fd >= 0)
|
|
|
|
osmo_stream_cli_close(cli);
|
2011-10-09 18:18:49 +00:00
|
|
|
|
2012-08-15 19:14:38 +00:00
|
|
|
cli->flags &= ~OSMO_STREAM_CLI_F_RECONF;
|
2011-10-09 18:18:49 +00:00
|
|
|
|
2017-04-08 20:06:37 +00:00
|
|
|
ret = osmo_sock_init2(AF_INET, SOCK_STREAM, cli->proto,
|
|
|
|
cli->local_addr, cli->local_port,
|
|
|
|
cli->addr, cli->port,
|
|
|
|
OSMO_SOCK_F_CONNECT|OSMO_SOCK_F_BIND);
|
2017-03-16 23:14:38 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
if (reconnect && errno == ECONNREFUSED)
|
|
|
|
osmo_stream_cli_reconnect(cli);
|
2016-11-26 14:53:35 +00:00
|
|
|
return ret;
|
2017-03-16 23:14:38 +00:00
|
|
|
}
|
2016-11-26 14:53:35 +00:00
|
|
|
|
2017-04-11 16:31:38 +00:00
|
|
|
if (cli->flags & OSMO_STREAM_CLI_F_NODELAY)
|
|
|
|
setsockopt_nodelay(cli->ofd.fd, cli->proto, 1);
|
|
|
|
|
2012-08-15 19:14:38 +00:00
|
|
|
cli->ofd.fd = ret;
|
|
|
|
if (osmo_fd_register(&cli->ofd) < 0) {
|
2011-10-03 20:09:45 +00:00
|
|
|
close(ret);
|
2017-04-08 20:17:19 +00:00
|
|
|
cli->ofd.fd = -1;
|
2011-10-03 20:09:45 +00:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-11 16:31:38 +00:00
|
|
|
/*! \brief Set the NODELAY socket option to avoid Nagle-like behavior
|
|
|
|
* Setting this to nodelay=true will automatically set the NODELAY
|
|
|
|
* socket option on any socket established via \ref osmo_stream_cli_open
|
|
|
|
* or any re-connect. You have to set this _before_ opening the
|
|
|
|
* socket.
|
|
|
|
* \param[in] cli Stream client whose sockets are to be configured
|
|
|
|
* \param[in] nodelay whether to set (true) NODELAY before connect()
|
|
|
|
*/
|
|
|
|
void osmo_stream_cli_set_nodelay(struct osmo_stream_cli *cli, bool nodelay)
|
|
|
|
{
|
|
|
|
if (nodelay)
|
|
|
|
cli->flags |= OSMO_STREAM_CLI_F_NODELAY;
|
|
|
|
else
|
|
|
|
cli->flags &= ~OSMO_STREAM_CLI_F_NODELAY;
|
|
|
|
}
|
2015-12-22 22:54:21 +00:00
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Open connection of an Osmocom stream client
|
|
|
|
* \param[in] cli Stream Client to connect */
|
2015-12-22 22:54:21 +00:00
|
|
|
int osmo_stream_cli_open(struct osmo_stream_cli *cli)
|
|
|
|
{
|
|
|
|
return osmo_stream_cli_open2(cli, 0);
|
|
|
|
}
|
|
|
|
|
2012-08-15 19:14:38 +00:00
|
|
|
static void cli_timer_cb(void *data)
|
2011-10-03 20:09:45 +00:00
|
|
|
{
|
2012-08-15 19:14:38 +00:00
|
|
|
struct osmo_stream_cli *cli = data;
|
2011-10-03 20:09:45 +00:00
|
|
|
|
|
|
|
LOGP(DLINP, LOGL_DEBUG, "reconnecting.\n");
|
|
|
|
|
2012-08-15 19:14:38 +00:00
|
|
|
switch(cli->state) {
|
|
|
|
case STREAM_CLI_STATE_CONNECTING:
|
2017-03-19 18:12:12 +00:00
|
|
|
cli->ofd.when |= BSC_FD_READ | BSC_FD_WRITE;
|
2015-12-22 22:54:21 +00:00
|
|
|
osmo_stream_cli_open2(cli, 1);
|
2011-10-03 20:09:45 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Enqueue data to be sent via an Osmocom stream client
|
|
|
|
* \param[in] cli Stream Client through which we want to send
|
|
|
|
* \param[in] msg Message buffer to enqueue in transmit queue */
|
2012-08-15 19:14:38 +00:00
|
|
|
void osmo_stream_cli_send(struct osmo_stream_cli *cli, struct msgb *msg)
|
2011-10-03 20:09:45 +00:00
|
|
|
{
|
2012-08-15 19:14:38 +00:00
|
|
|
msgb_enqueue(&cli->tx_queue, msg);
|
|
|
|
cli->ofd.when |= BSC_FD_WRITE;
|
2011-10-03 20:09:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Receive data via an Osmocom stream client
|
|
|
|
* \param[in] cli Stream Client through which we want to send
|
|
|
|
* \param msg pre-allocate message buffer to which received data is appended
|
|
|
|
* \returns number of bytes read; <=0 in case of error */
|
2012-08-15 19:14:38 +00:00
|
|
|
int osmo_stream_cli_recv(struct osmo_stream_cli *cli, struct msgb *msg)
|
2011-10-17 10:50:58 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2012-08-15 19:14:38 +00:00
|
|
|
ret = recv(cli->ofd.fd, msg->data, msg->data_len, 0);
|
2011-10-17 10:50:58 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
if (errno == EPIPE || errno == ECONNRESET) {
|
|
|
|
LOGP(DLINP, LOGL_ERROR,
|
2012-08-15 19:14:38 +00:00
|
|
|
"lost connection with srv\n");
|
2011-10-17 10:50:58 +00:00
|
|
|
}
|
2012-08-15 19:14:38 +00:00
|
|
|
osmo_stream_cli_reconnect(cli);
|
2011-10-17 10:50:58 +00:00
|
|
|
return ret;
|
|
|
|
} else if (ret == 0) {
|
2012-08-15 19:14:38 +00:00
|
|
|
LOGP(DLINP, LOGL_ERROR, "connection closed with srv\n");
|
|
|
|
osmo_stream_cli_reconnect(cli);
|
2011-10-17 10:50:58 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
msgb_put(msg, ret);
|
2012-08-15 19:14:38 +00:00
|
|
|
LOGP(DLINP, LOGL_DEBUG, "received %d bytes from srv\n", ret);
|
2011-10-17 10:50:58 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-10-03 20:09:45 +00:00
|
|
|
/*
|
|
|
|
* Server side.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 19:14:38 +00:00
|
|
|
#define OSMO_STREAM_SRV_F_RECONF (1 << 0)
|
2017-04-11 16:31:38 +00:00
|
|
|
#define OSMO_STREAM_SRV_F_NODELAY (1 << 1)
|
2011-10-09 18:18:49 +00:00
|
|
|
|
2012-08-15 19:14:38 +00:00
|
|
|
struct osmo_stream_srv_link {
|
2011-10-03 20:09:45 +00:00
|
|
|
struct osmo_fd ofd;
|
2017-04-08 20:09:57 +00:00
|
|
|
char *addr;
|
2011-10-03 20:09:45 +00:00
|
|
|
uint16_t port;
|
2015-12-21 19:54:19 +00:00
|
|
|
uint16_t proto;
|
2012-08-15 19:14:38 +00:00
|
|
|
int (*accept_cb)(struct osmo_stream_srv_link *srv, int fd);
|
2011-10-03 20:09:45 +00:00
|
|
|
void *data;
|
2011-10-09 18:18:49 +00:00
|
|
|
int flags;
|
2011-10-03 20:09:45 +00:00
|
|
|
};
|
|
|
|
|
2012-08-15 19:14:38 +00:00
|
|
|
static int osmo_stream_srv_fd_cb(struct osmo_fd *ofd, unsigned int what)
|
2011-10-03 20:09:45 +00:00
|
|
|
{
|
|
|
|
int ret;
|
2016-11-26 13:51:45 +00:00
|
|
|
int sock_fd;
|
2011-10-03 20:09:45 +00:00
|
|
|
struct sockaddr_in sa;
|
|
|
|
socklen_t sa_len = sizeof(sa);
|
2012-08-15 19:14:38 +00:00
|
|
|
struct osmo_stream_srv_link *link = ofd->data;
|
2011-10-03 20:09:45 +00:00
|
|
|
|
|
|
|
ret = accept(ofd->fd, (struct sockaddr *)&sa, &sa_len);
|
|
|
|
if (ret < 0) {
|
|
|
|
LOGP(DLINP, LOGL_ERROR, "failed to accept from origin "
|
|
|
|
"peer, reason=`%s'\n", strerror(errno));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
LOGP(DLINP, LOGL_DEBUG, "accept()ed new link from %s to port %u\n",
|
|
|
|
inet_ntoa(sa.sin_addr), link->port);
|
2016-11-26 13:51:45 +00:00
|
|
|
sock_fd = ret;
|
2011-10-03 20:09:45 +00:00
|
|
|
|
2016-11-26 13:51:45 +00:00
|
|
|
if (link->proto == IPPROTO_SCTP) {
|
|
|
|
ret = sctp_sock_activate_events(sock_fd);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error_close_socket;
|
|
|
|
}
|
2015-12-21 19:54:19 +00:00
|
|
|
|
2016-11-26 13:51:45 +00:00
|
|
|
if (link->flags & OSMO_STREAM_SRV_F_NODELAY) {
|
|
|
|
ret = setsockopt_nodelay(ret, link->proto, 1);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error_close_socket;
|
|
|
|
}
|
2017-04-11 16:31:38 +00:00
|
|
|
|
2016-11-26 13:51:45 +00:00
|
|
|
if (!link->accept_cb) {
|
|
|
|
ret = -ENOTSUP;
|
|
|
|
goto error_close_socket;
|
|
|
|
}
|
2011-10-03 20:09:45 +00:00
|
|
|
|
2016-11-26 13:51:45 +00:00
|
|
|
ret = link->accept_cb(link, sock_fd);
|
|
|
|
if (ret)
|
|
|
|
goto error_close_socket;
|
2011-10-03 20:09:45 +00:00
|
|
|
return 0;
|
2016-11-26 13:51:45 +00:00
|
|
|
|
|
|
|
error_close_socket:
|
|
|
|
close(sock_fd);
|
|
|
|
return ret;
|
2011-10-03 20:09:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Create an Osmocom Stream Server Link
|
|
|
|
* A Stream Server Link is the listen()+accept() "parent" to individual
|
|
|
|
* Stream Servers
|
|
|
|
* \param[in] ctx talloc allocation context
|
|
|
|
* \returns Stream Server Link with default values (TCP)
|
|
|
|
*/
|
2012-08-15 19:14:38 +00:00
|
|
|
struct osmo_stream_srv_link *osmo_stream_srv_link_create(void *ctx)
|
2011-10-03 20:09:45 +00:00
|
|
|
{
|
2012-08-15 19:14:38 +00:00
|
|
|
struct osmo_stream_srv_link *link;
|
2011-10-03 20:09:45 +00:00
|
|
|
|
2012-08-15 19:14:38 +00:00
|
|
|
link = talloc_zero(ctx, struct osmo_stream_srv_link);
|
2011-10-03 20:09:45 +00:00
|
|
|
if (!link)
|
|
|
|
return NULL;
|
|
|
|
|
2015-12-21 19:54:19 +00:00
|
|
|
link->proto = IPPROTO_TCP;
|
2011-10-09 18:18:49 +00:00
|
|
|
link->ofd.fd = -1;
|
2011-10-03 20:09:45 +00:00
|
|
|
link->ofd.when |= BSC_FD_READ | BSC_FD_WRITE;
|
2012-08-15 19:14:38 +00:00
|
|
|
link->ofd.cb = osmo_stream_srv_fd_cb;
|
2011-10-03 20:09:45 +00:00
|
|
|
link->ofd.data = link;
|
|
|
|
|
|
|
|
return link;
|
|
|
|
}
|
|
|
|
|
2017-04-11 16:31:38 +00:00
|
|
|
/*! \brief Set the NODELAY socket option to avoid Nagle-like behavior
|
|
|
|
* Setting this to nodelay=true will automatically set the NODELAY
|
|
|
|
* socket option on any socket established via this server link, before
|
|
|
|
* calling the accept_cb()
|
|
|
|
* \param[in] link server link whose sockets are to be configured
|
|
|
|
* \param[in] nodelay whether to set (true) NODELAY after accept
|
|
|
|
*/
|
|
|
|
void osmo_stream_srv_link_set_nodelay(struct osmo_stream_srv_link *link, bool nodelay)
|
|
|
|
{
|
|
|
|
if (nodelay)
|
|
|
|
link->flags |= OSMO_STREAM_SRV_F_NODELAY;
|
|
|
|
else
|
|
|
|
link->flags &= ~OSMO_STREAM_SRV_F_NODELAY;
|
|
|
|
}
|
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Set the local address to which we bind
|
|
|
|
* \param[in] link Stream Server Link to modify
|
|
|
|
* \param[in] addr Local IP address
|
|
|
|
*/
|
2012-08-15 19:14:38 +00:00
|
|
|
void osmo_stream_srv_link_set_addr(struct osmo_stream_srv_link *link,
|
2011-10-09 16:00:53 +00:00
|
|
|
const char *addr)
|
2011-10-03 20:09:45 +00:00
|
|
|
{
|
2017-04-08 20:09:57 +00:00
|
|
|
osmo_talloc_replace_string(link, &link->addr, addr);
|
2012-08-15 19:14:38 +00:00
|
|
|
link->flags |= OSMO_STREAM_SRV_F_RECONF;
|
2011-10-03 20:09:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Set the local port number to which we bind
|
|
|
|
* \param[in] link Stream Server Link to modify
|
|
|
|
* \param[in] port Local port number
|
|
|
|
*/
|
2012-08-15 19:14:38 +00:00
|
|
|
void osmo_stream_srv_link_set_port(struct osmo_stream_srv_link *link,
|
2011-10-09 16:00:53 +00:00
|
|
|
uint16_t port)
|
2011-10-03 20:09:45 +00:00
|
|
|
{
|
|
|
|
link->port = port;
|
2012-08-15 19:14:38 +00:00
|
|
|
link->flags |= OSMO_STREAM_SRV_F_RECONF;
|
2011-10-03 20:09:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Set the protocol for the stream server link
|
|
|
|
* \param[in] link Stream Server Link to modify
|
|
|
|
* \param[in] proto Protocol (like IPPROTO_TCP (default), IPPROTO_SCTP, ...)
|
|
|
|
*/
|
2015-12-21 19:54:19 +00:00
|
|
|
void
|
|
|
|
osmo_stream_srv_link_set_proto(struct osmo_stream_srv_link *link,
|
|
|
|
uint16_t proto)
|
|
|
|
{
|
|
|
|
link->proto = proto;
|
|
|
|
link->flags |= OSMO_STREAM_SRV_F_RECONF;
|
|
|
|
}
|
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Set application private data of the stream server link
|
|
|
|
* \param[in] link Stream Server Link to modify
|
|
|
|
* \param[in] data User-specific data (available in call-back functions) */
|
2011-10-09 19:19:13 +00:00
|
|
|
void
|
2012-08-15 19:14:38 +00:00
|
|
|
osmo_stream_srv_link_set_data(struct osmo_stream_srv_link *link,
|
2011-10-09 19:19:13 +00:00
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
link->data = data;
|
|
|
|
}
|
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Get application private data of the stream server link
|
|
|
|
* \param[in] link Stream Server Link to modify
|
|
|
|
* \returns Application private data, as set by \ref osmo_stream_cli_set_data() */
|
2012-08-15 19:14:38 +00:00
|
|
|
void *osmo_stream_srv_link_get_data(struct osmo_stream_srv_link *link)
|
2011-10-09 19:19:13 +00:00
|
|
|
{
|
|
|
|
return link->data;
|
|
|
|
}
|
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Get Osmocom File Descriptor of the stream server link
|
|
|
|
* \param[in] link Stream Server Link
|
|
|
|
* \returns Pointer to \ref osmo_fd */
|
2011-10-17 17:49:33 +00:00
|
|
|
struct osmo_fd *
|
2012-08-15 19:14:38 +00:00
|
|
|
osmo_stream_srv_link_get_ofd(struct osmo_stream_srv_link *link)
|
2011-10-17 17:49:33 +00:00
|
|
|
{
|
|
|
|
return &link->ofd;
|
|
|
|
}
|
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Set the accept() call-back of the stream server link
|
|
|
|
* \param[in] link Stream Server Link
|
|
|
|
* \param[in] accept_cb Call-back function executed upon accept() */
|
2012-08-15 19:14:38 +00:00
|
|
|
void osmo_stream_srv_link_set_accept_cb(struct osmo_stream_srv_link *link,
|
|
|
|
int (*accept_cb)(struct osmo_stream_srv_link *link, int fd))
|
2011-10-03 20:09:45 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
link->accept_cb = accept_cb;
|
|
|
|
}
|
|
|
|
|
2017-04-08 20:17:19 +00:00
|
|
|
/*! \brief Destroy the stream server link. Closes + Releases Memory.
|
2017-04-08 17:48:05 +00:00
|
|
|
* \param[in] link Stream Server Link */
|
2012-08-15 19:14:38 +00:00
|
|
|
void osmo_stream_srv_link_destroy(struct osmo_stream_srv_link *link)
|
2011-10-03 20:09:45 +00:00
|
|
|
{
|
2017-04-08 20:17:19 +00:00
|
|
|
osmo_stream_srv_link_close(link);
|
2011-10-03 20:09:45 +00:00
|
|
|
talloc_free(link);
|
|
|
|
}
|
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Open the stream server link. This actually initializes the
|
|
|
|
* underlying socket and binds it to the configured ip/port
|
|
|
|
* \param[in] link Stream Server Link to open */
|
2012-08-15 19:14:38 +00:00
|
|
|
int osmo_stream_srv_link_open(struct osmo_stream_srv_link *link)
|
2011-10-03 20:09:45 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2011-10-09 18:18:49 +00:00
|
|
|
/* we are reconfiguring this socket, close existing first. */
|
2012-08-15 19:14:38 +00:00
|
|
|
if ((link->flags & OSMO_STREAM_SRV_F_RECONF) && link->ofd.fd >= 0)
|
|
|
|
osmo_stream_srv_link_close(link);
|
2011-10-09 18:18:49 +00:00
|
|
|
|
2012-08-15 19:14:38 +00:00
|
|
|
link->flags &= ~OSMO_STREAM_SRV_F_RECONF;
|
2011-10-09 18:18:49 +00:00
|
|
|
|
2015-12-21 19:54:19 +00:00
|
|
|
ret = osmo_sock_init(AF_INET, SOCK_STREAM, link->proto,
|
2011-10-03 20:09:45 +00:00
|
|
|
link->addr, link->port, OSMO_SOCK_F_BIND);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
link->ofd.fd = ret;
|
|
|
|
if (osmo_fd_register(&link->ofd) < 0) {
|
|
|
|
close(ret);
|
2017-04-08 20:17:19 +00:00
|
|
|
link->ofd.fd = -1;
|
2011-10-03 20:09:45 +00:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Close the stream server link and unregister from select loop
|
|
|
|
* Does not destroy the server link, merely closes it!
|
|
|
|
* \param[in] link Stream Server Link to close */
|
2012-08-15 19:14:38 +00:00
|
|
|
void osmo_stream_srv_link_close(struct osmo_stream_srv_link *link)
|
2011-10-03 20:09:45 +00:00
|
|
|
{
|
2017-03-16 23:15:38 +00:00
|
|
|
if (link->ofd.fd == -1)
|
|
|
|
return;
|
2011-10-03 20:09:45 +00:00
|
|
|
osmo_fd_unregister(&link->ofd);
|
|
|
|
close(link->ofd.fd);
|
2017-03-16 23:15:38 +00:00
|
|
|
link->ofd.fd = -1;
|
2011-10-03 20:09:45 +00:00
|
|
|
}
|
|
|
|
|
2012-08-15 19:14:38 +00:00
|
|
|
struct osmo_stream_srv {
|
|
|
|
struct osmo_stream_srv_link *srv;
|
2011-10-03 20:09:45 +00:00
|
|
|
struct osmo_fd ofd;
|
|
|
|
struct llist_head tx_queue;
|
2012-08-15 19:14:38 +00:00
|
|
|
int (*closed_cb)(struct osmo_stream_srv *peer);
|
|
|
|
int (*cb)(struct osmo_stream_srv *peer);
|
2011-10-03 20:09:45 +00:00
|
|
|
void *data;
|
|
|
|
};
|
|
|
|
|
2012-08-15 19:14:38 +00:00
|
|
|
static void osmo_stream_srv_read(struct osmo_stream_srv *conn)
|
2011-10-03 20:09:45 +00:00
|
|
|
{
|
|
|
|
LOGP(DLINP, LOGL_DEBUG, "message received\n");
|
|
|
|
|
|
|
|
if (conn->cb)
|
2011-10-17 10:50:58 +00:00
|
|
|
conn->cb(conn);
|
2011-10-03 20:09:45 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-15 19:14:38 +00:00
|
|
|
static void osmo_stream_srv_write(struct osmo_stream_srv *conn)
|
2011-10-03 20:09:45 +00:00
|
|
|
{
|
2015-12-21 19:54:19 +00:00
|
|
|
#ifdef HAVE_LIBSCTP
|
|
|
|
struct sctp_sndrcvinfo sinfo;
|
|
|
|
#endif
|
2011-10-03 20:09:45 +00:00
|
|
|
struct msgb *msg;
|
|
|
|
struct llist_head *lh;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
LOGP(DLINP, LOGL_DEBUG, "sending data\n");
|
|
|
|
|
|
|
|
if (llist_empty(&conn->tx_queue)) {
|
|
|
|
conn->ofd.when &= ~BSC_FD_WRITE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
lh = conn->tx_queue.next;
|
|
|
|
llist_del(lh);
|
|
|
|
msg = llist_entry(lh, struct msgb, list);
|
|
|
|
|
2015-12-21 19:54:19 +00:00
|
|
|
switch (conn->srv->proto) {
|
|
|
|
#ifdef HAVE_LIBSCTP
|
|
|
|
case IPPROTO_SCTP:
|
2015-12-25 16:47:19 +00:00
|
|
|
memset(&sinfo, 0, sizeof(sinfo));
|
2017-04-12 10:08:45 +00:00
|
|
|
sinfo.sinfo_ppid = htonl(msgb_sctp_ppid(msg));
|
2017-04-10 15:48:42 +00:00
|
|
|
sinfo.sinfo_stream = msgb_sctp_stream(msg);
|
2015-12-21 19:54:19 +00:00
|
|
|
ret = sctp_send(conn->ofd.fd, msg->data, msgb_length(msg),
|
|
|
|
&sinfo, MSG_NOSIGNAL);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case IPPROTO_TCP:
|
|
|
|
default:
|
|
|
|
ret = send(conn->ofd.fd, msg->data, msg->len, 0);
|
|
|
|
break;
|
|
|
|
}
|
2011-10-03 20:09:45 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
LOGP(DLINP, LOGL_ERROR, "error to send\n");
|
|
|
|
}
|
|
|
|
msgb_free(msg);
|
|
|
|
}
|
|
|
|
|
2012-08-15 19:14:38 +00:00
|
|
|
static int osmo_stream_srv_cb(struct osmo_fd *ofd, unsigned int what)
|
2011-10-03 20:09:45 +00:00
|
|
|
{
|
2012-08-15 19:14:38 +00:00
|
|
|
struct osmo_stream_srv *conn = ofd->data;
|
2011-10-03 20:09:45 +00:00
|
|
|
|
|
|
|
LOGP(DLINP, LOGL_DEBUG, "connected read/write\n");
|
|
|
|
if (what & BSC_FD_READ)
|
2012-08-15 19:14:38 +00:00
|
|
|
osmo_stream_srv_read(conn);
|
2011-10-03 20:09:45 +00:00
|
|
|
if (what & BSC_FD_WRITE)
|
2012-08-15 19:14:38 +00:00
|
|
|
osmo_stream_srv_write(conn);
|
2011-10-03 20:09:45 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Create a Stream Server inside the specified link
|
|
|
|
* \param[in] ctx talloc allocation context from which to allocate
|
|
|
|
* \param[in] link Stream Server Link to which we belong
|
|
|
|
* \returns Stream Server in case of success; NULL on error */
|
2012-08-15 19:14:38 +00:00
|
|
|
struct osmo_stream_srv *
|
|
|
|
osmo_stream_srv_create(void *ctx, struct osmo_stream_srv_link *link,
|
2011-10-09 16:00:53 +00:00
|
|
|
int fd,
|
2012-08-15 19:14:38 +00:00
|
|
|
int (*cb)(struct osmo_stream_srv *conn),
|
|
|
|
int (*closed_cb)(struct osmo_stream_srv *conn), void *data)
|
2011-10-03 20:09:45 +00:00
|
|
|
{
|
2012-08-15 19:14:38 +00:00
|
|
|
struct osmo_stream_srv *conn;
|
2011-10-03 20:09:45 +00:00
|
|
|
|
2012-08-15 19:14:38 +00:00
|
|
|
conn = talloc_zero(ctx, struct osmo_stream_srv);
|
2011-10-03 20:09:45 +00:00
|
|
|
if (conn == NULL) {
|
2012-08-15 19:14:38 +00:00
|
|
|
LOGP(DLINP, LOGL_ERROR, "cannot allocate new peer in srv, "
|
2011-10-03 20:09:45 +00:00
|
|
|
"reason=`%s'\n", strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-08-15 19:14:38 +00:00
|
|
|
conn->srv = link;
|
2011-10-03 20:09:45 +00:00
|
|
|
conn->ofd.fd = fd;
|
|
|
|
conn->ofd.data = conn;
|
2012-08-15 19:14:38 +00:00
|
|
|
conn->ofd.cb = osmo_stream_srv_cb;
|
2011-10-03 20:09:45 +00:00
|
|
|
conn->ofd.when = BSC_FD_READ;
|
|
|
|
conn->cb = cb;
|
|
|
|
conn->closed_cb = closed_cb;
|
|
|
|
conn->data = data;
|
|
|
|
INIT_LLIST_HEAD(&conn->tx_queue);
|
|
|
|
|
|
|
|
if (osmo_fd_register(&conn->ofd) < 0) {
|
|
|
|
LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
|
|
|
|
talloc_free(conn);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Set application private data of the stream server
|
|
|
|
* \param[in] conn Stream Server to modify
|
|
|
|
* \param[in] data User-specific data (available in call-back functions) */
|
2011-11-06 19:47:08 +00:00
|
|
|
void
|
2012-08-15 19:14:38 +00:00
|
|
|
osmo_stream_srv_set_data(struct osmo_stream_srv *conn,
|
2011-11-06 19:47:08 +00:00
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
conn->data = data;
|
|
|
|
}
|
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Get application private data of the stream server
|
|
|
|
* \param[in] conn Stream Server
|
|
|
|
* \returns Application private data, as set by \ref osmo_stream_srv_set_data() */
|
|
|
|
void *osmo_stream_srv_get_data(struct osmo_stream_srv *conn)
|
2011-10-09 19:19:13 +00:00
|
|
|
{
|
2017-04-08 17:48:05 +00:00
|
|
|
return conn->data;
|
2011-10-09 19:19:13 +00:00
|
|
|
}
|
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Get Osmocom File Descriptor of the stream server
|
|
|
|
* \param[in] conn Stream Server
|
|
|
|
* \returns Pointer to \ref osmo_fd */
|
2011-10-17 17:49:33 +00:00
|
|
|
struct osmo_fd *
|
2017-04-08 17:48:05 +00:00
|
|
|
osmo_stream_srv_get_ofd(struct osmo_stream_srv *conn)
|
2011-10-17 17:49:33 +00:00
|
|
|
{
|
2017-04-08 17:48:05 +00:00
|
|
|
return &conn->ofd;
|
2011-10-17 17:49:33 +00:00
|
|
|
}
|
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Get the master (Link) from a Stream Server
|
|
|
|
* \param[in] conn Stream Server of which we want to know the Link
|
|
|
|
* \returns Link through which the given Stream Server is established */
|
2012-08-19 16:35:10 +00:00
|
|
|
struct osmo_stream_srv_link *osmo_stream_srv_get_master(struct osmo_stream_srv *conn)
|
|
|
|
{
|
|
|
|
return conn->srv;
|
|
|
|
}
|
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Destroy given Stream Server
|
|
|
|
* This function closes the Stream Server socket, unregisters from
|
|
|
|
* select loop and de-allocates associated memory.
|
|
|
|
* \param[in] conn Stream Server to be destroyed */
|
2012-08-15 19:14:38 +00:00
|
|
|
void osmo_stream_srv_destroy(struct osmo_stream_srv *conn)
|
2011-10-03 20:09:45 +00:00
|
|
|
{
|
|
|
|
close(conn->ofd.fd);
|
|
|
|
osmo_fd_unregister(&conn->ofd);
|
|
|
|
if (conn->closed_cb)
|
|
|
|
conn->closed_cb(conn);
|
|
|
|
talloc_free(conn);
|
|
|
|
}
|
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Enqueue data to be sent via an Osmocom stream server
|
|
|
|
* \param[in] conn Stream Server through which we want to send
|
|
|
|
* \param[in] msg Message buffer to enqueue in transmit queue */
|
2012-08-15 19:14:38 +00:00
|
|
|
void osmo_stream_srv_send(struct osmo_stream_srv *conn, struct msgb *msg)
|
2011-10-03 20:09:45 +00:00
|
|
|
{
|
|
|
|
msgb_enqueue(&conn->tx_queue, msg);
|
|
|
|
conn->ofd.when |= BSC_FD_WRITE;
|
|
|
|
}
|
2011-10-17 10:50:58 +00:00
|
|
|
|
2017-04-08 17:48:05 +00:00
|
|
|
/*! \brief Receive data via Osmocom stream server
|
|
|
|
* \param[in] conn Stream Server from which to receive
|
|
|
|
* \param msg pre-allocate message buffer to which received data is appended
|
|
|
|
* \returns number of bytes read, negative on error.
|
|
|
|
*/
|
2012-08-15 19:14:38 +00:00
|
|
|
int osmo_stream_srv_recv(struct osmo_stream_srv *conn, struct msgb *msg)
|
2011-10-17 10:50:58 +00:00
|
|
|
{
|
2016-01-06 15:41:44 +00:00
|
|
|
#ifdef HAVE_LIBSCTP
|
|
|
|
struct sctp_sndrcvinfo sinfo;
|
|
|
|
int flags = 0;
|
|
|
|
#endif
|
2011-10-17 10:50:58 +00:00
|
|
|
int ret;
|
|
|
|
|
2016-01-06 15:41:44 +00:00
|
|
|
if (!msg)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
switch (conn->srv->proto) {
|
|
|
|
#ifdef HAVE_LIBSCTP
|
|
|
|
case IPPROTO_SCTP:
|
|
|
|
ret = sctp_recvmsg(conn->ofd.fd, msgb_data(msg), msgb_tailroom(msg),
|
|
|
|
NULL, NULL, &sinfo, &flags);
|
|
|
|
if (flags & MSG_NOTIFICATION) {
|
|
|
|
union sctp_notification *notif = (union sctp_notification *) msgb_data(msg);
|
|
|
|
LOGP(DLINP, LOGL_DEBUG, "NOTIFICATION %u flags=0x%x\n", notif->sn_header.sn_type, notif->sn_header.sn_flags);
|
|
|
|
switch (notif->sn_header.sn_type) {
|
|
|
|
case SCTP_ASSOC_CHANGE:
|
|
|
|
LOGP(DLINP, LOGL_DEBUG, "===> ASSOC CHANGE:");
|
|
|
|
switch (notif->sn_assoc_change.sac_state) {
|
|
|
|
case SCTP_COMM_UP:
|
|
|
|
LOGPC(DLINP, LOGL_DEBUG, " UP\n");
|
|
|
|
break;
|
|
|
|
case SCTP_COMM_LOST:
|
|
|
|
LOGPC(DLINP, LOGL_DEBUG, " LOST\n");
|
|
|
|
break;
|
|
|
|
case SCTP_RESTART:
|
|
|
|
LOGPC(DLINP, LOGL_DEBUG, " RESTART\n");
|
|
|
|
break;
|
|
|
|
case SCTP_SHUTDOWN_COMP:
|
|
|
|
LOGPC(DLINP, LOGL_DEBUG, " SHUTDOWN COMP\n");
|
|
|
|
break;
|
|
|
|
case SCTP_CANT_STR_ASSOC:
|
|
|
|
LOGPC(DLINP, LOGL_DEBUG, " CANT STR ASSOC\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SCTP_PEER_ADDR_CHANGE:
|
|
|
|
LOGP(DLINP, LOGL_DEBUG, "===> PEER ADDR CHANGE\n");
|
|
|
|
break;
|
|
|
|
case SCTP_SHUTDOWN_EVENT:
|
|
|
|
LOGP(DLINP, LOGL_DEBUG, "===> SHUTDOWN EVT\n");
|
|
|
|
/* Handle this like a regular disconnect */
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
2017-04-12 10:08:45 +00:00
|
|
|
msgb_sctp_ppid(msg) = ntohl(sinfo.sinfo_ppid);
|
2017-04-10 15:48:42 +00:00
|
|
|
msgb_sctp_stream(msg) = sinfo.sinfo_stream;
|
2016-01-06 15:41:44 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case IPPROTO_TCP:
|
|
|
|
default:
|
|
|
|
ret = recv(conn->ofd.fd, msgb_data(msg), msgb_tailroom(msg), 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-10-17 10:50:58 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
if (errno == EPIPE || errno == ECONNRESET) {
|
|
|
|
LOGP(DLINP, LOGL_ERROR,
|
2012-08-15 19:14:38 +00:00
|
|
|
"lost connection with srv\n");
|
2011-10-17 10:50:58 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
} else if (ret == 0) {
|
2012-08-15 19:14:38 +00:00
|
|
|
LOGP(DLINP, LOGL_ERROR, "connection closed with srv\n");
|
2011-10-17 10:50:58 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
msgb_put(msg, ret);
|
|
|
|
LOGP(DLINP, LOGL_DEBUG, "received %d bytes from client\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2017-04-08 17:48:05 +00:00
|
|
|
|
|
|
|
/*! @} */
|