l2tpd_socket: complete unix socket implementation

This commit is contained in:
Alexander Couzens 2016-11-02 20:51:30 +01:00 committed by Harald Welte
parent bcd9772fe5
commit 0204667e06
2 changed files with 55 additions and 13 deletions

View File

@ -1,3 +1,24 @@
/* generic unix socket interface
*
* (C) 2016 by Alexander Couzens <lynxis@fe80.eu>
*
* All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include <errno.h>
#include <stdio.h>
@ -13,8 +34,33 @@
#include "l2tpd_socket.h"
static int l2tp_sock_write(struct osmo_fd *bfd, struct msgb *msg)
{
int rc;
rc = write(bfd->fd, msg->data, msg->len);
if (rc != msg->len)
LOGP(DLCTRL, LOGL_ERROR, "Failed to write message to the unix connection.\n");
return rc;
}
int l2tp_sock_cleanup(struct osmo_fd *bfd)
{
int rc;
struct osmo_wqueue *wq = container_of(bfd, struct osmo_wqueue, bfd);
osmo_wqueue_clear(wq);
rc = close(bfd->fd);
osmo_fd_unregister(bfd);
bfd->fd = -1;
return rc;
}
/* accept a new connection */
static int mncc_sock_accept(struct osmo_fd *bfd, unsigned int flags)
static int l2tp_sock_accept(struct osmo_fd *bfd, unsigned int flags)
{
struct l2tp_socket_state *state = container_of(bfd, struct l2tp_socket_state, listen_bfd);
struct osmo_fd *conn_bfd = &state->wqueue.bfd;
@ -31,13 +77,12 @@ static int mncc_sock_accept(struct osmo_fd *bfd, unsigned int flags)
if (conn_bfd->fd >= 0) {
LOGP(state->log_class, LOGL_NOTICE, "There is already one connection to the socket\n");
osmo_fd_unregister(conn_bfd);
close(conn_bfd->fd);
l2tp_sock_cleanup(conn_bfd);
return 0;
}
conn_bfd->fd = rc;
conn_bfd->when = BSC_FD_READ | BSC_FD_WRITE;
conn_bfd->when = BSC_FD_READ;
if (osmo_fd_register(conn_bfd) != 0) {
LOGP(state->log_class, LOGL_ERROR, "Failed to register new connection fd\n");
@ -46,7 +91,10 @@ static int mncc_sock_accept(struct osmo_fd *bfd, unsigned int flags)
return -1;
}
LOGP(state->log_class, LOGL_NOTICE, "MNCC Socket has connection with external "
state->wqueue.write_cb = l2tp_sock_write;
state->wqueue.except_cb = l2tp_sock_cleanup;
LOGP(state->log_class, LOGL_NOTICE, "Unix Socket has connection with external "
"call control application\n");
return 0;
@ -57,12 +105,6 @@ void l2tp_set_read_callback(struct l2tp_socket_state *state, int (*read_cb)(stru
state->wqueue.read_cb = read_cb;
}
void l2tp_set_write_callback(struct l2tp_socket_state *state, int (*write_cb)(struct osmo_fd *fd, struct msgb *msg))
{
state->wqueue.write_cb = write_cb;
}
/*!
* \brief l2tp_enqueue_data
* \param sock
@ -99,7 +141,7 @@ int l2tp_socket_init(struct l2tp_socket_state *state, const char *sock_path, int
}
bfd->when = BSC_FD_READ;
bfd->cb = mncc_sock_accept;
bfd->cb = l2tp_sock_accept;
rc = osmo_fd_register(bfd);
if (rc < 0) {

View File

@ -18,4 +18,4 @@ int l2tp_socket_init(struct l2tp_socket_state *state, const char *sock_path, int
int l2tp_socket_enqueue(struct l2tp_socket_state *state, struct msgb *msg);
void l2tp_set_read_callback(struct l2tp_socket_state *state, int (*read_cb)(struct osmo_fd *fd));
void l2tp_set_write_callback(struct l2tp_socket_state *state, int (*write_cb)(struct osmo_fd *fd, struct msgb *msg));
int l2tp_sock_cleanup(struct osmo_fd *bfd);