Skip to content

Commit

Permalink
add listener functions
Browse files Browse the repository at this point in the history
  • Loading branch information
razvancrainea committed Jan 22, 2015
1 parent 0b3cf88 commit 0b3ac29
Show file tree
Hide file tree
Showing 8 changed files with 244 additions and 8 deletions.
5 changes: 5 additions & 0 deletions cfg.y
Expand Up @@ -1177,6 +1177,11 @@ assign_stm: DEBUG EQUAL snumber {
| XLOG_BUF_SIZE EQUAL error { yyerror("number expected"); }
| XLOG_FORCE_COLOR EQUAL error { yyerror("boolean value expected"); }
| LISTEN EQUAL listen_def {
if (add_listener($3, 0)!=0){
LM_CRIT("cfg. parser: failed"
" to add listen address\n");
break;
}
}
| LISTEN EQUAL error { yyerror("ip address or hostname "
"expected (use quotes if the hostname includes"
Expand Down
44 changes: 44 additions & 0 deletions net/net.c
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2015 OpenSIPS Project
*
* This file is part of opensips, a free SIP server.
*
* opensips 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.
*
* opensips 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
* history:
* ---------
* 2015-01-xx created (razvanc)
*/

#include <string.h>
#include "net.h"
#include "../mem/mem.h"

struct proto_net *proto_net_binds;

int init_net_interface(int size)
{
proto_net_binds = pkg_malloc(size * sizeof(struct proto_net));
if (!proto_net_binds) {
LM_ERR("no more memory to allocate protocol bindings\n");
return -1;
}

memset(proto_net_binds, 0, size * sizeof(struct proto_net));

return 0;
}

37 changes: 37 additions & 0 deletions net/net.h
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2015 OpenSIPS Project
*
* This file is part of opensips, a free SIP server.
*
* opensips 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.
*
* opensips 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
* history:
* ---------
* 2015-01-xx created (razvanc)
*/

#ifndef _NET_TI_H_
#define _NET_TI_H_

#include "../ip_addr.h"
#include "proto_net.h"

extern struct proto_net *proto_net_binds;

int init_net_interface(int size);
int destroy_net_interface(int size);

#endif /* _NET_TI_H_ */
77 changes: 77 additions & 0 deletions net/net_tcp.c
@@ -0,0 +1,77 @@
/*
* Copyright (C) 2015 OpenSIPS Project
*
* This file is part of opensips, a free SIP server.
*
* opensips 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.
*
* opensips 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
* history:
* ---------
* 2015-01-xx created (razvanc)
*/

#include <string.h>
#include "net_tcp.h"


// initializes the TCP structures
int tcp_init(void)
{
// TODO: implementation
return -1;
}

// destroys the TCP data
void tcp_destroy(void)
{
// TODO: implementation
}

// looks for the transport protocol that knows how to handle the
// proto and calls the corresponding add_listener() function
int tcp_add_listener(char* host, int port, int type, void *ctx)
{
// TODO: implementation
return 0;
}

// used to return a listener
struct socket_info* tcp_find_listener(union sockaddr_union* to, int proto)
{
// TODO: implementation
return NULL;
}

// initializes the TCP listeners
int tcp_init_listeners(void)
{
// TODO: implementation
return -1;
}

// returns the connection identified by either the id or the destination to
struct connection* tcp_conn_get(int id, union sockaddr_union* to)
{
// TODO: implementation
return NULL;
}

// used to tune the connection attributes
int tcp_conn_fcntl(struct connection *conn, int attr, void *value)
{
// TODO: implementation
return -1;
}
9 changes: 7 additions & 2 deletions net/proto.h
Expand Up @@ -27,13 +27,18 @@
#define _PROTO_TI_H_

#include "../ip_addr.h"
#include "proto_net.h"

typedef int (*proto_init_f)(void);
typedef int (*proto_add_listener_f)(char *name, int port);

struct proto_funcs {
struct proto {
int default_port;
proto_init_f init;
proto_add_listener_f add_listener;
};

typedef int (*proto_bind_api)(struct proto_funcs *funcs);
typedef int (*proto_bind_api)(struct proto *proto_binds,
struct proto_net *net_binds);

#endif /* _PROTO_TI_H_ */
32 changes: 32 additions & 0 deletions net/proto_net.h
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2015 OpenSIPS Project
*
* This file is part of opensips, a free SIP server.
*
* opensips 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.
*
* opensips 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
* history:
* ---------
* 2015-01-xx created (razvanc)
*/

#ifndef _PROTO_NET_H_
#define _PROTO_NET_H_

struct proto_net {
};

#endif /*_PROTO_NET_H_ */
39 changes: 35 additions & 4 deletions net/trans.c
Expand Up @@ -26,6 +26,7 @@
#include <string.h>
#include "trans.h"
#include "proto.h"
#include "net.h"
#include "../mem/mem.h"
#include "../sr_module.h"

Expand All @@ -48,8 +49,12 @@ int init_trans_interface(void)
}

memset(protos, 0, proto_nr * sizeof(struct proto_info));
// for (i = 0; i < proto_nr; i++)
// protos[i].id = i + 1;

if (init_net_interface(proto_nr) < 0) {
LM_ERR("cannot init network interface\n");
pkg_free(protos);
return -1;
}

return 0;
}
Expand Down Expand Up @@ -127,13 +132,14 @@ enum sip_protos get_trans_proto(char *name)
LM_ERR("cannot find transport API for protocol %s\n", name);
goto end;
}
if (proto_api(&protos[proto - 1].funcs) < 0) {
if (proto_api(&protos[proto - 1].binds,
&proto_net_binds[proto - 1]) < 0) {
LM_ERR("cannot bind transport API for protocol %s\n", name);
goto end;
}

/* initialize the module */
if (protos[proto - 1].funcs.init && protos[proto - 1].funcs.init() < 0) {
if (protos[proto - 1].binds.init && protos[proto - 1].binds.init() < 0) {
LM_ERR("cannot initialzie protocol %s\n", name);
goto end;
}
Expand All @@ -146,3 +152,28 @@ enum sip_protos get_trans_proto(char *name)
end:
return PROTO_NONE;
}


int add_listener(struct socket_id *sock, enum si_flags flags)
{
enum sip_protos proto = sock->proto;
int port;

/* validate the protocol */
if (proto < 0 || proto >= proto_nr) {
LM_BUG("invalid protocol number %d\n", proto);
return -1;
}
if (protos[proto - 1].id == PROTO_NONE) {
LM_BUG("protocol %d not registered\n", proto);
return -1;
}
port = sock->port ? sock->port : protos[proto - 1].binds.default_port;
if (protos[proto - 1].binds.add_listener &&
protos[proto - 1].binds.add_listener(sock->name, port) < 0) {
LM_ERR("cannot add socket");
return -1;
}
return 0;
}

9 changes: 7 additions & 2 deletions net/trans.h
Expand Up @@ -36,8 +36,8 @@ struct proto_info {
/* listeners on this proto */
struct socket_info *listeners;

/* functions for this protocol */
struct proto_funcs funcs;
/* bindings for this protocol */
struct proto binds;
};

extern struct proto_info *protos;
Expand All @@ -53,4 +53,9 @@ int init_trans_interface(void);
*/
enum sip_protos get_trans_proto(char *name);

/*
* adds a new listener
*/
int add_listener(struct socket_id *sock, enum si_flags flags);

#endif /* _TRANS_TI_H_ */

0 comments on commit 0b3ac29

Please sign in to comment.