-
Notifications
You must be signed in to change notification settings - Fork 4
/
erlang_listener.c
371 lines (352 loc) · 11.6 KB
/
erlang_listener.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include "erlang_mod.h"
#include "erlang_listener.h"
#include "erlang_cmd.h"
#include "../../sr_module.h"
#include "../../dprint.h"
#include "../../shm_init.h"
#include "../../cfg/cfg_struct.h"
struct erlang_cmd *pending_cmds = 0;
//forward decl
int send_erlang(struct erlang_cmd *erl_cmd);
void child_loop(int data_pipe)
{
fd_set fdset;
int selret,maxfd;
struct nodes_list *node;
struct erlang_cmd *erl_cmd, **cmd_p;
time_t cur_ticks;
struct timeval tv;
int readcount;
int erl_retcode=-1;
while(1) {
erl_cmd=NULL;
cmd_p=&pending_cmds;
cur_ticks=get_ticks_raw();
while(*cmd_p) {
//timeout ==0 means we did not send it yet
if (((*cmd_p)->timeout !=0) &&((*cmd_p)->timeout < cur_ticks)) {
LM_DBG("removing stalled request %p\n",*cmd_p);
erl_cmd=*cmd_p;
erl_cmd->retcode=-5;
//sending worked expects this to be freed
shm_free(erl_cmd->erlbuf);
erl_cmd->erlbuf=NULL;
erl_cmd->erlbuf_len=0;
lock_release(&(erl_cmd->lock));
*cmd_p=(*cmd_p)->next;
break;
}
cmd_p=&((*cmd_p)->next);
}
FD_ZERO(&fdset);
FD_SET(data_pipe, &fdset);
// LM_DBG("child_loop: datapipe FD_SET(%d)\n",data_pipe);
maxfd=data_pipe;
tv.tv_sec=1; tv.tv_usec=0;
// LM_DBG("erlang_child_loop: loopstart\n");
for(node=nodes_lst; node; node=node->next){
// LM_DBG("child_loop: checking %s fd=%d\n",node->node,node->fd);
if(node_reconnect(node)<0) {
// LM_DBG("child_loop: skipping %s\n",node->node);
continue;
}
// LM_DBG("child_loop: Setting FD_SET(%d) %s\n",node->fd,node->node);
FD_SET(node->fd, &fdset);
maxfd=(node->fd > maxfd) ? node->fd : maxfd;
}
selret = select(maxfd+1, &fdset, NULL, NULL, &tv);
// LM_DBG("erlang_child_loop: after select\n");
// cfg_update();
if(selret < 0) {
LM_ERR("erlang_child_loop: error in select(): %d %s\n",errno,strerror(errno));
continue;
}
if(selret==0) {
// LM_DBG("erlang_child_loop: timeout\n");
continue;
}
for(node=nodes_lst; node; node=node->next){
if(node->fd<=0) //skip disconected nodes
continue;
if(FD_ISSET(node->fd, &fdset)) {
node_receive(node);
}
}
if(FD_ISSET(data_pipe, &fdset)) {
int unlock=0;
readcount=read(data_pipe, &erl_cmd, sizeof(erl_cmd));
if (readcount==0) {
// LM_DBG("erlang_child_loop: readcount=0\n");
continue;
}
LM_DBG("erlang_child_loop: read from worker %d %d %s %d %p %p\n",readcount,
erl_cmd->cmd,erl_cmd->reg_name,erl_cmd->erlbuf_len,erl_cmd->erlbuf,erl_cmd->node);
node=erl_cmd->node;
erl_cmd->timeout = get_ticks_raw()+MS_TO_TICKS(5000);
switch(erl_cmd->cmd) {
case ERLANG_INFO:
case ERLANG_CAST:
erl_retcode=send_erlang(erl_cmd);
unlock=1;
break;
case ERLANG_CALL:
case ERLANG_CALL_ROUTE:
case ERLANG_REX:
erl_retcode=send_erlang(erl_cmd);
break;
default:
LM_ERR("erlang_child_loop: unknown cmd_pipe command: %d\n",erl_cmd->cmd);
}
//not needed anymore, will be malloced again when response arrives
shm_free(erl_cmd->erlbuf);
erl_cmd->erlbuf=NULL;
erl_cmd->erlbuf_len=0;
if(erl_retcode<0) {
LM_ERR("erlang_child_loop: erl_send failed: %d (unlocking)\n",erl_retcode);
erl_cmd->retcode=erl_retcode;
lock_release(&(erl_cmd->lock)); //unlock and let it fail in worker
continue;
}
LM_DBG("erlang_child_loop: erl_send success: %d\n",erl_retcode);
if(unlock) {
lock_release(&(erl_cmd->lock));
} else {
//no unlock, hold worker until reply comes
erl_cmd->next=pending_cmds;
pending_cmds=erl_cmd;
}
continue;
}
}
}
int node_reconnect(struct nodes_list *node)
{
time_t t;
if(node->fd >0) return 0;
t=time(NULL);
if(node->timeout > t) {
// LM_DBG("reconnect not yet %d sec left\n",(int)(node->timeout) - (int)t);
return -1;
}
// LM_DBG("erlang conncting to %s %s %s\n",node->name, node->cookie,node->node);
if (ei_connect_init(&(node->ec), node->name, node->cookie, 1) < 0) {
LM_ERR("node_reconnect_loop: ei_connect_init error %s sheulding for sleep\n",node->name);
node->timeout=t+30;
node->fd=-1;
return -1;
}
// LM_DBG("erlang_child_init: ei_connect_init sucseded %s\n",node->node);
node->fd = ei_connect(&(node->ec), node->node);
if(node->fd <0) {
LM_ERR("erlang_new_connection ei_connect error %s %d sleeping for a while\n", node->name, node->fd);
node->timeout=t+30;
return -1;
}
LM_INFO("erlang_child_init: ei_connect connected to %s fd=%d\n",node->node, node->fd);
return 0;
}
void node_receive(struct nodes_list *node)
{
int status = 1;
char *pbuf= NULL;
char name[MAXATOMLEN];
int i=0,j=0, decode_index=0;
struct erlang_cmd *current_cmd;
erlang_ref ref;
erlang_msg msg;
ei_x_buff buf;
ei_x_new(&buf);
// LM_DBG("node_receive %s fd=%d \n",node->node, node->fd);
memset(&msg,0,sizeof(msg));
status = ei_xreceive_msg_tmo(node->fd, &msg, &buf, 1000);
// LM_DBG("node_receive %s fd=%d status=%d\n",node->node, node->fd, status);
switch (status) {
case ERL_TICK:
// LM_DBG("node_receive ERL_TICK\n");
break;
case ERL_MSG:
LM_DBG("node_receive ERL_MSG\n");
switch (msg.msgtype) {
case ERL_SEND:
LM_DBG("node_receive ERL_MSG erl_send from %s:<%d.%d.%d> to %s,<%d.%d.%d> to %s (cookie %s)\n",
msg.from.node, msg.from.num, msg.from.serial, msg.from.creation,
msg.to.node,msg.to.num,msg.to.serial,msg.to.creation,
msg.toname,msg.cookie);
decode_index=0;
ei_decode_version(buf.buff,&decode_index,&j);
//debug
i=decode_index;
LM_DBG("node_receive: buf.index=%d decode_index=%d i=%d j=%d\n", buf.index, decode_index,i,j );
ei_s_print_term(&pbuf, buf.buff, &i);
LM_DBG("node_receive: message is pbuf='%s' buf.buffsz=%d buf.index=%d decode_index=%d i=%d j=%d\n", pbuf, buf.buffsz,buf.index, decode_index,i,j );
free(pbuf);pbuf=NULL;
//end debug
ei_get_type(buf.buff, &decode_index, &i, &j); //i is type, j is size
LM_DBG("node_receive: buf.index=%d decode_index=%d i=%d j=%d\n", buf.index, decode_index,i,j );
if((i==ERL_SMALL_TUPLE_EXT || i==ERL_LARGE_TUPLE_EXT) && j==2) {
ei_decode_tuple_header(buf.buff, &decode_index, &i); // ignoring i value we know it is 2
ei_get_type(buf.buff, &decode_index, &i, &j); //i is type, j is size
switch(i) {
case ERL_ATOM_EXT:
ei_decode_atom(buf.buff, &decode_index, name);
if(strcmp("rex",name)==0) {
LM_DBG("got rex!\n");
current_cmd=find_pending_by_pid(msg.to.num,msg.to.serial);
if(current_cmd!=NULL) {
LM_DBG("node_receive: got rex response unlcoking %p\n",current_cmd);
current_cmd->retcode=0;
current_cmd->erlbuf=shm_malloc(buf.buffsz);
memcpy(current_cmd->erlbuf, buf.buff, buf.buffsz);
current_cmd->erlbuf_len=buf.buffsz;
current_cmd->decode_index=decode_index;
lock_release(&(current_cmd->lock));
} else {
i=decode_index;
LM_DBG("node_receive: buf.index=%d decode_index=%d i=%d j=%d\n", buf.index, decode_index,i,j );
ei_s_print_term(&pbuf, buf.buff, &i);
LM_ERR("node_receive: Unexpected message pbuf='%s' buf.index=%d decode_index=%d i=%d j=%d\n", pbuf, buf.index, decode_index,i,j );
free(pbuf);pbuf=NULL;
}
} else {
LM_ERR("Don't know how to handle msg with {%s,...}\n",name);
}
break;
// case ERL_PID_EXT:
// ei_decode_pid(buf.buff, &decode_index, &pid);
//
// break;
case ERL_NEW_REFERENCE_EXT:
case ERL_REFERENCE_EXT:
ei_decode_ref(buf.buff, &decode_index, &ref);
current_cmd=find_pending_by_ref(ref.n[0], ref.n[1], ref.n[2]);
if(current_cmd!=NULL) {
LM_DBG("node_receive: got call or call_route response unlcoking %p\n",current_cmd);
current_cmd->retcode=0;
current_cmd->erlbuf=shm_malloc(buf.buffsz);
memcpy(current_cmd->erlbuf, buf.buff, buf.buffsz);
current_cmd->erlbuf_len=buf.buffsz;
current_cmd->decode_index=decode_index;
lock_release(&(current_cmd->lock));
}else {
i=decode_index;
LM_DBG("node_receive: buf.index=%d decode_index=%d i=%d j=%d\n", buf.index, decode_index,i,j );
ei_s_print_term(&pbuf, buf.buff, &i);
LM_ERR("node_receive: Unexpected message pbuf='%s' buf.index=%d decode_index=%d i=%d j=%d\n", pbuf, buf.index, decode_index,i,j );
free(pbuf);pbuf=NULL;
}
break;
default:
LM_ERR("node_receive: expected atom or pid as 1st element of tuple (GOT %c)\n",i);
}
LM_DBG("end of node_receive case ERL_SEND!\n");
} else {
LM_DBG("node_receive: not ERL_SMALL_TUPLE nor ERL_LARGE_TUPLE_EXT i=%d j=%d\n", i, j );
}
break;
case ERL_REG_SEND:
LM_DBG("erl_reg_send to %s from %s:<%d.%d.%d> to %s:<%d.%d.%d>\n", msg.toname,
msg.from.node,msg.from.num,msg.from.serial,msg.from.creation,
msg.to.node,msg.to.num,msg.to.serial,msg.to.creation);
decode_index=0;
ei_decode_version(buf.buff,&decode_index,&j);
i=decode_index;
//debug
ei_s_print_term(&pbuf, buf.buff, &i);
LM_DBG("erl_send: message is '%s' %d %d %d\n", pbuf, buf.index, i,j );
free(pbuf);pbuf=NULL;
//end debug
break;
case ERL_LINK:
LM_DBG("erl_link\n");
break;
case ERL_UNLINK:
LM_DBG("erl_unlink\n");
break;
case ERL_EXIT:
LM_DBG("erl_exit from %s <%d.%d.%d>\n", msg.from.node, msg.from.creation, msg.from.num,msg.from.serial);
// handle_exit(node, &msg.from);
break;
default:
LM_DBG("unexpected msg type %d\n", (int) (msg.msgtype));
break;
}
break;
case ERL_ERROR:
LM_DBG("node_receive ERL_ERROR\n");
if (erl_errno != ETIMEDOUT && erl_errno != EAGAIN) {
LM_DBG("erl_error\n");
if(close(node->fd)) {
LM_DBG("Error while closing fd after erl_error\n");
}
node->fd=-1;
}
break;
default:
LM_DBG("node_receive unexpected status %d \n", status);
break;
}
ei_x_free(&buf);
}
char *shm_strdup(str *src)
{
char *res;
if (!src || !src->s)
return NULL;
if (!(res = (char *) shm_malloc(src->len + 1)))
return NULL;
strncpy(res, src->s, src->len);
res[src->len] = 0;
return res;
}
struct nodes_list *parse_connect_param(char *s, int l)
{
struct nodes_list *node;
str st;
param_t* params_list = NULL;
param_hooks_t phooks;
param_t *pit=NULL;
st.s = s;
st.len = l;
if(st.s[st.len-1]==';')
st.len--;
if (parse_params(&st, CLASS_ANY, &phooks, &(params_list))<0) {
LM_ERR("ERROR:parse_connect_param: cant parse parameters\n");
return 0;
}
node=shm_malloc(sizeof(struct nodes_list));
memset(node,0,sizeof(struct nodes_list));
if (node==0){
LM_ERR("ERROR:parse_connect_param: out of memory\n");
return 0;
}
for (pit = params_list; pit; pit=pit->next) {
if (pit->name.len==4 && strncasecmp(pit->name.s, "name", 4)==0) {
node->name = shm_strdup(&(pit->body));
} else if(pit->name.len==6 && strncasecmp(pit->name.s, "cookie", 6)==0) {
node->cookie= shm_strdup(&(pit->body));
} else if(pit->name.len==4 && strncasecmp(pit->name.s, "node", 4)==0) {
node->node = shm_strdup(&(pit->body));
}
}
if(node->name==NULL) { LM_ERR("invalid node name\n");goto error;}
if(node->cookie==NULL) { LM_ERR("cookie is not set\n");goto error;}
if(node->node==NULL) { LM_ERR("node is not set\n"); goto error;}
free_params(params_list);
LM_DBG("parsed: name=%s cookie=%s node=%s\n",node->name,node->cookie,node->node);
return node;
error:
if (node) {
if(node->name) shm_free(node->name);
if(node->cookie) shm_free(node->cookie);
if(node->node) shm_free(node->node);
shm_free(node);
}
free_params(params_list);
return 0;
}
int send_erlang(struct erlang_cmd *erl_cmd) {
struct nodes_list *node;
node=erl_cmd->node;
return ei_reg_send(&(node->ec), node->fd, erl_cmd->reg_name,
erl_cmd->erlbuf, erl_cmd->erlbuf_len);
}