Skip to content

Commit

Permalink
proto: always populate default values for protocols name and port
Browse files Browse the repository at this point in the history
This allows you to print the protocol you received, and its associated port
  • Loading branch information
razvancrainea committed Jun 23, 2016
1 parent 030efef commit 0ae5ba2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 54 deletions.
9 changes: 0 additions & 9 deletions main.c
Expand Up @@ -955,15 +955,6 @@ int main(int argc, char** argv)
yydebug = 1;
#endif

/*
* initializes transport interfaces - we initialize them here because we
* can have listening interfaces declared in the command line
*/
if (trans_init() < 0) {
LM_ERR("cannot initialize transport interface\n");
goto error;
}

/* get uid/gid */
if (user){
if (user2uid(&uid, &gid, user)<0){
Expand Down
50 changes: 20 additions & 30 deletions net/trans.c
Expand Up @@ -33,38 +33,25 @@
#include "proto_udp/proto_udp_handler.h"


/* we alocate this dynamically because we don't know how when new protocols
* are developed. Since this is done only once, it's not that bad */
struct proto_info *protos;
/*
* we need to always populate this structure at startup, at least the SIP
* protocols, because we never know what kind of traffic we receive and have
* to print its name
*/
struct proto_info protos[PROTO_LAST - PROTO_NONE] = {

{ }, /* PROTO_NONE */
{ .name = "udp", .port = 5060 }, /* PROTO_UDP */
{ .name = "tcp", .port = 5060 }, /* PROTO_TCP */
{ .name = "tls", .port = 5061 }, /* PROTO_TLS */
{ .name = "sctp", .port = 5060 }, /* PROTO_SCTP */
{ .name = "ws", .port = 80 }, /* PROTO_WS */
{ .name = "wss", .port = 443 }, /* PROTO_WSS */
/* populate here for other protos - not necessary right now */
};

static struct socket_id *cmd_listeners;


int trans_init(void)
{
unsigned int proto_nr;

proto_nr = PROTO_LAST - PROTO_NONE;
protos = pkg_malloc(proto_nr * sizeof(struct proto_info));
if (!protos) {
LM_ERR("no more memory to allocate protocols\n");
return -1;
}

memset(protos, 0, proto_nr * sizeof(struct proto_info));

return 0;
}

void trans_destroy(void)
{
int i;
for (i = PROTO_FIRST; i < PROTO_LAST; i++)
if (protos[i].id != PROTO_NONE)
pkg_free(protos[i].name);
pkg_free(protos);
}

#define PROTO_PREFIX_LEN (sizeof(PROTO_PREFIX) - 1)

int trans_load(void)
Expand Down Expand Up @@ -117,10 +104,13 @@ int trans_load(void)
found_proto = 1;
/* copy necessary info */
protos[pi.id].id = pi.id;
protos[pi.id].name = pi.name;
protos[pi.id].default_port = pi.default_port;
protos[pi.id].tran = pi.tran;
protos[pi.id].net = pi.net;
/*
* XXX: make sure you never overwrite port or name
* should we put it in a separate structure?
*/
}
}
if (found_proto)
Expand Down
23 changes: 8 additions & 15 deletions net/trans.h
Expand Up @@ -31,13 +31,16 @@
#include "api_proto_net.h"

struct proto_info {
/* proto as ID */
enum sip_protos id;

/* the name of the protocol */
char *name;

/* the default protocol */
/* the default port according to RFC */
unsigned short port;

/* proto as ID */
enum sip_protos id;

/* the default port, in case it is missing in the listener */
unsigned short default_port;

/* bindings for the transport interface */
Expand All @@ -59,7 +62,7 @@ struct proto_info {
* listeners */
typedef int (*api_proto_init)(struct proto_info *pi);

extern struct proto_info *protos;
extern struct proto_info protos[];

#define is_tcp_based_proto(_p) \
(protos[_p].net.flags&PROTO_NET_USE_TCP)
Expand All @@ -75,16 +78,6 @@ extern struct proto_info *protos;
protos[(_rcv)->proto].tran.dst_attr(_rcv,_attr,_val);\
}while(0)

/*
* initializes transport interface structures
*/
int trans_init(void);

/*
* destroys the transport interface structures
*/
void trans_destroy(void);

/*
* loads the transport protocol
*/
Expand Down

0 comments on commit 0ae5ba2

Please sign in to comment.