-
Notifications
You must be signed in to change notification settings - Fork 564
/
trans.c
267 lines (235 loc) · 6.98 KB
/
trans.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*
* history:
* ---------
* 2015-01-xx created (razvanc)
*/
#include <string.h>
#include "trans.h"
#include "api_proto.h"
#include "../mem/mem.h"
#include "../sr_module.h"
#include "../socket_info.h"
#include "proto_tcp/proto_tcp_handler.h"
#include "proto_udp/proto_udp_handler.h"
/*
* 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] = {
{ .name = NULL, .default_rfc_port = 0 }, /* PROTO_NONE */
{ .name = "udp", .default_rfc_port = 5060 }, /* PROTO_UDP */
{ .name = "tcp", .default_rfc_port = 5060 }, /* PROTO_TCP */
{ .name = "tls", .default_rfc_port = 5061 }, /* PROTO_TLS */
{ .name = "sctp", .default_rfc_port = 5060 }, /* PROTO_SCTP */
{ .name = "ws", .default_rfc_port = 80 }, /* PROTO_WS */
{ .name = "wss", .default_rfc_port = 443 }, /* PROTO_WSS */
/* populate here for other protos - not necessary right now */
};
static struct socket_id *cmd_listeners;
#define PROTO_PREFIX_LEN (sizeof(PROTO_PREFIX) - 1)
int trans_load(void)
{
int id;
struct sr_module *mod;
cmd_export_t *cmd;
int found_all = 0;
int found_proto;
api_proto_init abind;
struct proto_info pi;
/* go through all protocol modules loaded and load only the ones
* that are prefixed with the PROTO_PREFIX token */
for (mod = modules; mod; mod = mod->next) {
if (strncmp(PROTO_PREFIX, mod->exports->name, PROTO_PREFIX_LEN) != 0)
continue;
found_proto = 0;
/* we have a transport module here - check for protocols */
for (cmd = mod->exports->cmds; cmd && cmd->name; cmd++) {
if (strcmp("proto_init", cmd->name)!=0)
continue;
abind = (api_proto_init)cmd->function;
memset(&pi, 0, sizeof(pi));
if (abind(&pi) < 0) {
LM_ERR("cannot load protocol's functions for %s\n",
cmd->name);
return -1;
}
/* double check if it is a known/valid proto */
if (pi.id < PROTO_FIRST || pi.id >= PROTO_OTHER) {
LM_ERR("Unknown protocol id %d; check sip_protos structure!\n", pi.id);
return -1;
}
/* double check the name of the proto */
if (parse_proto((unsigned char *)pi.name, strlen(pi.name), &id) < 0) {
LM_ERR("Cannot parse protocol %s\n", pi.name);
return -1;
}
if (id != pi.id) {
LM_ERR("Protocol ID mismatch %d != %d\n", id, pi.id);
return -1;
}
found_proto = 1;
/* check if there is any listener for this protocol */
if (!protos[pi.id].listeners) {
LM_DBG("No listener defined for proto %s\n", pi.name);
continue;
}
/* check if already added */
if (protos[id].id != PROTO_NONE) {
LM_ERR("Protocol already loaded %s\n", pi.name);
return -1;
}
/* all good now */
found_all++;
/* 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;
}
if (found_proto)
continue;
LM_ERR("No binding found for protocol %s\n", mod->exports->name);
return -1;
}
/* return whether we found any protocol or not */
return found_all;
}
#undef PROTO_PREFIX_LEN
int add_listener(struct socket_id *sock, enum si_flags flags)
{
/*
* XXX: using the new version, the protocol _MUST_ be specified
* otherwise UDP will be assumed
*/
enum sip_protos proto = sock->proto;
/* validate the protocol */
if (proto < PROTO_FIRST || proto >= PROTO_LAST) {
LM_BUG("invalid protocol number %d\n", proto);
return -1;
}
/* convert to socket_info */
if (new_sock2list(sock->name, sock->port, sock->proto, sock->adv_name, sock->adv_port,
sock->children, flags, &protos[proto].listeners) < 0) {
LM_ERR("cannot add socket to the list\n");
return -1;
}
return 0;
}
int add_cmd_listener(char *name, int port, int proto)
{
struct socket_id *tmp = pkg_malloc(sizeof(struct socket_id));
if (!tmp) {
fprintf(stderr, "no more pkg memory\n");
return -1;
}
memset(tmp, 0, sizeof(struct socket_id));
tmp->name = name;
tmp->port = port;
tmp->proto = proto;
tmp->next = cmd_listeners;
cmd_listeners = tmp;
return 0;
}
int fix_cmd_listeners(void)
{
struct socket_id *si, *prev;
for (si = cmd_listeners; si;) {
if (si->proto == PROTO_NONE)
si->proto = PROTO_UDP;
if (add_listener(si, 0) < 0)
LM_ERR("Cannot add socket <%s>, skipping...\n", si->name);
prev = si;
si = si->next;
pkg_free(prev);
}
return 0;
}
/*
* return 0 on success, -1 on error */
int fix_all_socket_lists(void)
{
int i;
int found = 0;
static char buf[PROTO_NAME_MAX_SIZE /* currently we shouldn't hardcode that much */];
char *p;
for (i = PROTO_FIRST; i < PROTO_LAST; i++) {
if (protos[i].id != PROTO_NONE) {
if (fix_socket_list(&protos[i].listeners)!=0) {
LM_ERR("fix_socket_list for %d failed\n", protos[i].id);
goto error;
}
found++;
} else if (protos[i].listeners) {
p = proto2str(i, buf);
if (p == NULL)
goto error;
*p = '\0';
LM_ERR("listeners found for protocol %s, but no module "
"can handle it, %d, %d \n", buf,i,protos[i].id);
goto error;
}
}
if (!found){
LM_ERR("no listening sockets\n");
goto error;
}
return 0;
error:
return -1;
}
int trans_init_all_listeners(void)
{
struct socket_info *si;
int i;
for (i = PROTO_FIRST; i < PROTO_LAST; i++)
if (protos[i].id != PROTO_NONE)
for( si=protos[i].listeners ; si ; si=si->next ) {
if (protos[i].tran.init_listener(si)<0) {
LM_ERR("failed to init listener [%.*s], proto %s\n",
si->name.len, si->name.s,
protos[i].name );
return -1;
}
/* set first IPv4 and IPv6 listeners for this proto */
if ((si->address.af==AF_INET) &&
(!protos[i].sendipv4 || (protos[i].sendipv4->flags&SI_IS_LO)))
protos[i].sendipv4=si;
if (!protos[i].sendipv6 && (si->address.af==AF_INET6))
protos[i].sendipv6=si;
}
return 0;
}
void print_all_socket_lists(void)
{
struct socket_info *si;
int i;
for (i = PROTO_FIRST; i < PROTO_LAST; i++) {
if (protos[i].id == PROTO_NONE)
continue;
for (si = protos[i].listeners; si; si = si->next)
printf(" %s: %s [%s]:%s%s\n", protos[i].name,
si->name.s, si->address_str.s, si->port_no_str.s,
si->flags & SI_IS_MCAST ? " mcast" : "");
}
}