Skip to content

Commit c14fc8c

Browse files
committed
Untangle L subcommand processing a bit.
1 parent 8167799 commit c14fc8c

9 files changed

Lines changed: 78 additions & 108 deletions

File tree

modules/acct_csv/rtpp_acct_csv.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*/
2727

2828
#include <sys/param.h>
29+
#include <sys/socket.h>
2930
#include <sys/stat.h>
3031
#include <sys/types.h>
3132
#include <errno.h>

src/commands/rpcpv1_ul.c

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ static int rtpp_command_ul_pre_parse(const struct rtpp_command *,
124124
}
125125

126126
void
127-
ul_reply_port(struct rtpp_command *cmd, struct ul_reply *ulr)
127+
ul_reply_port(const struct rtpp_command *cmd, struct ul_reply *ulr)
128128
{
129129
int rport, r;
130130
const char subc_err[] = " && -1";
@@ -535,19 +535,20 @@ handle_nomem(struct rtpp_command *cmd, int ecode, struct rtpp_session *spa)
535535
CALL_SMETHOD(cmd->reply, deliver_error, ecode);
536536
}
537537

538-
static int
539-
format_ul_reply_result(const struct ul_reply *ulr, struct rtpp_subc_resp *rsp)
538+
int
539+
format_ul_reply_result(const struct ul_opts *ulop, char *buf_t, size_t blen)
540540
{
541541
const rtpp_str_const_t *sap;
542542
rtpp_str_const_t sad;
543543
char saddr[MAX_ADDR_STRLEN];
544544
const char *at;
545545
int plen;
546+
const struct ul_reply *ulr = &ulop->reply;
546547

547548
if (ulr == NULL || ulr->ia == NULL || ishostnull(ulr->ia->addr)) {
548-
plen = snprintf(rsp->buf_t, sizeof(rsp->buf_t), "%d",
549+
plen = snprintf(buf_t, blen, "%d",
549550
(ulr != NULL) ? ulr->port : 0);
550-
return (plen > -1 && plen < sizeof(rsp->buf_t)) ? 0 : -1;
551+
return (plen > -1 && plen < blen) ? 0 : -1;
551552
}
552553
sap = &ulr->ia->params.advaddr;
553554
if (sap->s == NULL) {
@@ -557,15 +558,14 @@ format_ul_reply_result(const struct ul_reply *ulr, struct rtpp_subc_resp *rsp)
557558
sap = &sad;
558559
}
559560
at = (ulr->ia->addr->sa_family == AF_INET) ? "" : " 6";
560-
plen = snprintf(rsp->buf_t, sizeof(rsp->buf_t), "%d %.*s%s",
561+
plen = snprintf(buf_t, blen, "%d %.*s%s",
561562
ulr->port, FMTSTR(sap), at);
562-
return (plen > -1 && plen < sizeof(rsp->buf_t)) ? 0 : -1;
563+
return (plen > -1 && plen < blen) ? 0 : -1;
563564
}
564565

565566
int
566567
rtpp_command_ul_handle_impl(const struct rtpp_cfg *cfsp,
567-
struct rtpp_command *cmd, int sidx, struct rtpp_subc_resp *rsp,
568-
struct rtpp_command_stats *csp, const struct rtpp_sockaddr *raddrp)
568+
struct rtpp_command *cmd, int sidx)
569569
{
570570
int pidx, lport, sessions_active;
571571
struct rtpp_socket *fds[2];
@@ -582,7 +582,7 @@ rtpp_command_ul_handle_impl(const struct rtpp_cfg *cfsp,
582582
ulop = cmd->cca.opts.ul;
583583

584584
#define UL_FAIL(_ecode) do { \
585-
if (rsp == NULL) \
585+
if (cmd->reply != NULL) \
586586
CALL_SMETHOD(cmd->reply, deliver_error, (_ecode)); \
587587
goto err_undo_0; \
588588
} while (0)
@@ -626,8 +626,8 @@ rtpp_command_ul_handle_impl(const struct rtpp_cfg *cfsp,
626626
spa->rtp->stream[sidx]->port = lport;
627627
spa->rtcp->stream[sidx]->port = lport + 1;
628628
if (spa->complete == 0) {
629-
if (csp != NULL)
630-
csp->nsess_complete.cnt++;
629+
if (cmd->csp != NULL)
630+
cmd->csp->nsess_complete.cnt++;
631631
CALL_SMETHOD(spa->rtp->stream[0]->ttl, reset_with,
632632
spa->rtp->stream[0]->stream_ttl);
633633
CALL_SMETHOD(spa->rtp->stream[1]->ttl, reset_with,
@@ -693,8 +693,8 @@ rtpp_command_ul_handle_impl(const struct rtpp_cfg *cfsp,
693693
UL_FAIL(ECODE_LSTFAIL_2);
694694
}
695695

696-
if (csp != NULL)
697-
csp->nsess_created.cnt++;
696+
if (cmd->csp != NULL)
697+
cmd->csp->nsess_created.cnt++;
698698

699699
hte = CALL_SMETHOD(cfsp->sessions_ht, append_str_refcnt, spa->call_id,
700700
spa->rcnt, NULL);
@@ -744,11 +744,11 @@ rtpp_command_ul_handle_impl(const struct rtpp_cfg *cfsp,
744744
}
745745
if (ulop->notify_socket != NULL) {
746746
struct rtpp_tnotify_target *rttp;
747-
struct rtpp_sockaddr raddr = (raddrp != NULL) ? *raddrp :
748-
(struct rtpp_sockaddr){.l = 0};
747+
RTPP_DBG_ASSERT(cmd->raddr != NULL);
749748

750749
rttp = CALL_METHOD(cfsp->rtpp_tnset_cf, lookup, ulop->notify_socket->s,
751-
(raddr.l > 0) ? sstosa(raddr.a) : NULL, (raddr.l > 0) ? cmd->laddr : NULL);
750+
(cmd->raddr->l > 0) ? sstosa(cmd->raddr->a) : NULL,
751+
(cmd->raddr->l > 0) ? cmd->laddr : NULL);
752752
if (rttp == NULL) {
753753
RTPP_LOG(spa->log, RTPP_LOG_ERR, "invalid socket name %.*s",
754754
FMTSTR(ulop->notify_socket));
@@ -833,26 +833,18 @@ rtpp_command_ul_handle_impl(const struct rtpp_cfg *cfsp,
833833
if (rsc.resp->result != 0)
834834
break;
835835
}
836-
if (rsp == NULL) {
836+
if (cmd->reply != NULL) {
837837
ul_reply_port(cmd, &ulop->reply);
838-
} else if (format_ul_reply_result(&ulop->reply, rsp) != 0) {
839-
goto err_undo_0;
840838
}
841-
#undef UL_FAIL
842839
return (0);
843840

844841
err_undo_0:
845-
#undef UL_FAIL
846842
return (-1);
847843
}
848844

849845
int
850846
rtpp_command_ul_handle(const struct rtpp_cfg *cfsp, struct rtpp_command *cmd, int sidx)
851847
{
852-
struct rtpp_command_stats *csp;
853-
struct rtpp_sockaddr raddr;
854848

855-
csp = rtpp_command_get_stats(cmd);
856-
raddr = rtpp_command_get_raddr(cmd);
857-
return (rtpp_command_ul_handle_impl(cfsp, cmd, sidx, NULL, csp, &raddr));
849+
return (rtpp_command_ul_handle_impl(cfsp, cmd, sidx));
858850
}

src/commands/rpcpv1_ul.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ struct ul_opts;
3535
struct ul_reply;
3636
struct rtpp_command;
3737
struct rtpp_session;
38-
struct rtpp_subc_ctx;
39-
struct rtpp_subc_resp;
4038
struct rtpp_command_args;
4139
struct after_success_h;
4240
struct rtpp_command_stats;
@@ -62,10 +60,9 @@ struct ul_opts *rtpp_command_ul_opts_parse_inner(const struct rtpp_cfg *,
6260
void rtpp_command_ul_opts_free(struct ul_opts *ulop);
6361
int rtpp_command_ul_handle(const struct rtpp_cfg *, struct rtpp_command *,
6462
int);
63+
int format_ul_reply_result(const struct ul_opts *, char *, size_t);
6564
int rtpp_command_ul_handle_impl(const struct rtpp_cfg *,
66-
struct rtpp_command *, int, struct rtpp_subc_resp *,
67-
struct rtpp_command_stats *, const struct rtpp_sockaddr *);
68-
void ul_reply_port(struct rtpp_command *cmd,
69-
struct ul_reply *ulr);
65+
struct rtpp_command *, int);
66+
void ul_reply_port(const struct rtpp_command *cmd, struct ul_reply *ulr);
7067

7168
#endif

src/commands/rpcpv1_ul_subc.c

Lines changed: 33 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,7 @@
5959

6060
struct rtpp_subcommand_ul_lstate {
6161
struct rtpp_refcnt *rcnt;
62-
int op;
63-
struct ul_opts *ulop;
64-
struct rtpp_log *glog;
65-
struct sockaddr *laddr;
66-
const struct rtpp_timestamp *dtime;
67-
const rtpp_str_t *to_tag;
68-
struct rtpp_command_stats *csp;
69-
struct rtpp_sockaddr raddr;
62+
struct rtpp_command scmd;
7063
};
7164

7265
static int
@@ -77,7 +70,7 @@ rtpp_command_ul_subc_pre_parse(const rtpp_str_t *call_id,
7770

7871
if (call_id == NULL || from_tag == NULL || to_tag == NULL)
7972
return (-1);
80-
if (subc_args->c != 3 && subc_args->c != 5)
73+
if (subc_args->c != 3)
8174
return (-1);
8275
if (subc_args->v[0].len < 1)
8376
return (-1);
@@ -86,17 +79,12 @@ rtpp_command_ul_subc_pre_parse(const rtpp_str_t *call_id,
8679

8780
memset(pcmd, '\0', sizeof(*pcmd));
8881
pcmd->cmods = subc_args->v[0].s + 1;
89-
pcmd->op = (subc_args->c == 5) ? UPDATE : LOOKUP;
82+
pcmd->op = LOOKUP;
9083
pcmd->call_id = call_id;
9184
pcmd->from_tag = from_tag;
9285
pcmd->to_tag = to_tag;
9386
pcmd->addr = rtpp_str_fix(&subc_args->v[1]);
9487
pcmd->port = rtpp_str_fix(&subc_args->v[2]);
95-
if (subc_args->c == 5) {
96-
pcmd->has_notify = 1;
97-
pcmd->notify_socket = rtpp_str_fix(&subc_args->v[3]);
98-
pcmd->notify_tag = subc_args->v[4];
99-
}
10088
return (0);
10189
}
10290

@@ -105,37 +93,26 @@ rtpp_command_ul_as_subc(const struct after_success_h_args *ap,
10593
const struct rtpp_subc_ctx *scp)
10694
{
10795
const struct rtpp_cfg *cfsp;
108-
const struct rtpp_subcommand_ul_lstate *lsp;
109-
struct rtpp_command scmd;
96+
struct rtpp_subcommand_ul_lstate *lsp;
11097
int sidx, rval;
11198

112-
lsp = (const struct rtpp_subcommand_ul_lstate *)ap->dyn;
99+
lsp = (struct rtpp_subcommand_ul_lstate *)ap->dyn;
113100
RTPP_DBG_ASSERT(scp->env->sessp != NULL);
114101
RTPP_DBG_ASSERT(lsp != NULL);
115-
RTPP_DBG_ASSERT(lsp->ulop != NULL);
116-
RTPP_DBG_ASSERT(lsp->to_tag != NULL);
117102
cfsp = (const struct rtpp_cfg *)ap->stat;
118-
memset(&scmd, '\0', sizeof(scmd));
119-
scmd.glog = lsp->glog;
120-
scmd.reply = NULL;
121-
scmd.dtime = lsp->dtime;
122-
scmd.laddr = lsp->laddr;
123-
scmd.cca.op = lsp->op;
124-
scmd.cca.call_id = scp->env->sessp->call_id;
125-
scmd.cca.from_tag = scp->env->sessp->from_tag;
126-
scmd.cca.to_tag = lsp->to_tag;
127-
scmd.cca.opts.ul = lsp->ulop;
128103
if (scp->env->sessp->rtp->stream[0] == scp->env->strmp_in) {
129104
sidx = 0;
130105
} else if (scp->env->sessp->rtp->stream[1] == scp->env->strmp_in) {
131106
sidx = 1;
132107
} else {
133108
return (-1);
134109
}
135-
scmd.sp = scp->env->sessp;
136-
rval = rtpp_command_ul_handle_impl(cfsp, &scmd, sidx, scp->resp, lsp->csp,
137-
&lsp->raddr);
110+
lsp->scmd.sp = scp->env->sessp;
111+
rval = rtpp_command_ul_handle_impl(cfsp, &lsp->scmd, sidx);
138112
if (rval == 0) {
113+
if (format_ul_reply_result(lsp->scmd.cca.opts.ul,
114+
scp->resp->buf_t, sizeof(scp->resp->buf_t)) != 0)
115+
return -1;
139116
struct rtpp_stream *t = scp->env->strmp_in;
140117
scp->env->strmp_in = scp->env->strmp_out;
141118
scp->env->strmp_out = t;
@@ -150,33 +127,32 @@ rtpp_command_ul_look_subc_parse(const struct rtpp_cfg *cfsp,
150127
{
151128
struct rtpp_subcommand_ul_lstate *lsp;
152129
struct rtpp_command_ul_pcmd pcmd;
153-
struct rtpp_command scmd = {.glog = cmd->glog, .reply = cmd->reply};
154130
struct ul_opts *ulop;
155131
int ecode = 0;
156132

157133
if (cmd->cca.op != UPDATE)
158-
return (-1);
134+
goto e0;
159135
if (rtpp_command_ul_subc_pre_parse(cmd->cca.call_id, cmd->cca.from_tag,
160-
cmd->cca.to_tag, subc_args, &pcmd) != 0) {
161-
return (-1);
162-
}
163-
ulop = rtpp_command_ul_opts_parse_inner(cfsp, &scmd, &pcmd, &ecode);
164-
if (ulop == NULL)
165-
return (-1);
136+
cmd->cca.to_tag, subc_args, &pcmd) != 0)
137+
goto e0;
166138
lsp = rtpp_rzmalloc(sizeof(struct rtpp_subcommand_ul_lstate),
167139
offsetof(struct rtpp_subcommand_ul_lstate, rcnt));
168-
if (lsp == NULL) {
169-
rtpp_command_ul_opts_free(ulop);
170-
return (-1);
171-
}
172-
lsp->op = pcmd.op;
173-
lsp->ulop = ulop;
174-
lsp->glog = cmd->glog;
175-
lsp->laddr = cmd->laddr;
176-
lsp->dtime = cmd->dtime;
177-
lsp->to_tag = cmd->cca.to_tag;
178-
lsp->csp = rtpp_command_get_stats(cmd);
179-
lsp->raddr = rtpp_command_get_raddr(cmd);
140+
if (lsp == NULL)
141+
goto e0;
142+
lsp->scmd = (struct rtpp_command){.glog = cmd->glog, .reply = cmd->reply};
143+
ulop = rtpp_command_ul_opts_parse_inner(cfsp, &lsp->scmd, &pcmd, &ecode);
144+
if (ulop == NULL)
145+
goto e1;
146+
lsp->scmd.reply = NULL;
147+
lsp->scmd.laddr = cmd->laddr;
148+
lsp->scmd.dtime = cmd->dtime;
149+
lsp->scmd.cca.op = pcmd.op;
150+
lsp->scmd.cca.opts.ul = ulop;
151+
lsp->scmd.cca.call_id = cmd->cca.call_id;
152+
lsp->scmd.cca.from_tag = cmd->cca.from_tag;
153+
lsp->scmd.cca.to_tag = cmd->cca.to_tag;
154+
lsp->scmd.raddr = cmd->raddr;
155+
lsp->scmd.csp = cmd->csp;
180156
RTPP_OBJ_DTOR_ATTACH_s(lsp, (rtpp_refcnt_dtor_t)&rtpp_command_ul_opts_free,
181157
ulop);
182158
asp->args.stat = (void *)cfsp;
@@ -187,6 +163,10 @@ rtpp_command_ul_look_subc_parse(const struct rtpp_cfg *cfsp,
187163
return -1;
188164
}
189165
return (0);
166+
e1:
167+
free(lsp);
168+
e0:
169+
return (-1);
190170
}
191171

192172
#if ENABLE_MODULE_IF

src/rtpp_command.c

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,12 @@ rtpp_command_ctor(const struct rtpp_cfg *cfsp, int controlfd,
217217
pvt->ctx.dtime.wall = dtime->wall;
218218
pvt->ctx.dtime.mono = dtime->mono;
219219
cmd->dtime = &pvt->ctx.dtime;
220-
pvt->ctx.csp = csp;
220+
cmd->csp = pvt->ctx.csp = csp;
221221
cmd->glog = cfsp->glog;
222222
pvt->ctx.umode = umode;
223223
cmd->reply = rtpc_reply_ctor(&pvt->ctx);
224+
pvt->ctx.raddr.a = &pvt->ctx._raddr;
225+
cmd->raddr = &pvt->ctx.raddr;
224226
if (cmd->reply == NULL) {
225227
RTPP_OBJ_DECREF(cmd);
226228
return (NULL);
@@ -235,29 +237,24 @@ rtpp_command_set_raddr(struct rtpp_command *cmd, const struct sockaddr *raddr, s
235237
struct rtpp_command_priv *pvt;
236238

237239
PUB2PVT(cmd, pvt);
238-
memcpy(&pvt->ctx.raddr, raddr, rlen);
239-
pvt->ctx.rlen = rlen;
240+
memcpy(&pvt->ctx._raddr, raddr, rlen);
241+
pvt->ctx.raddr.l = rlen;
240242
}
241243

242244
struct rtpp_sockaddr
243245
rtpp_command_get_raddr(const struct rtpp_command *cmd)
244246
{
245-
struct rtpp_sockaddr raddr;
246247
const struct rtpp_command_priv *pvt;
247248

248249
PUB2PVT(cmd, pvt);
249-
raddr.a = &pvt->ctx.raddr;
250-
raddr.l = pvt->ctx.rlen;
251-
return (raddr);
250+
return (pvt->ctx.raddr);
252251
}
253252

254253
struct rtpp_command_stats *
255254
rtpp_command_get_stats(const struct rtpp_command *cmd)
256255
{
257-
const struct rtpp_command_priv *pvt;
258256

259-
PUB2PVT(cmd, pvt);
260-
return (pvt->ctx.csp);
257+
return (cmd->csp);
261258
}
262259

263260
struct rtpp_command *
@@ -303,9 +300,9 @@ get_command(const struct rtpp_cfg *cfsp, struct rtpp_ctrl_sock *rcsp, int contro
303300
lp = &asize;
304301
raddr = sstosa(&rcsp->emrg.addr);
305302
} else {
306-
pvt->ctx.rlen = sizeof(pvt->ctx.raddr);
307-
lp = &pvt->ctx.rlen;
308-
raddr = sstosa(&pvt->ctx.raddr);
303+
pvt->ctx.raddr.l = sizeof(pvt->ctx._raddr);
304+
lp = &pvt->ctx.raddr.l;
305+
raddr = sstosa(pvt->ctx.raddr.a);
309306
}
310307
len = recvfrom(controlfd, bp, bsize - 1, 0, raddr, lp);
311308
}
@@ -354,11 +351,11 @@ rtpp_command_guard_retrans(struct rtpp_command *cmd,
354351
}
355352
len = cres->reply->len;
356353
int r = rtpp_anetio_sendto_na(pvt->ctx.cfs->rtpp_proc_cf->netio, pvt->ctx.controlfd,
357-
cres->reply->s, len, 0, sstosa(&pvt->ctx.raddr), pvt->ctx.rlen, cres->rcnt);
354+
cres->reply->s, len, 0, sstosa(pvt->ctx.raddr.a), pvt->ctx.raddr.l, cres->rcnt);
358355
if (r != 0)
359356
RTPP_OBJ_DECREF(cres);
360-
pvt->ctx.csp->ncmds_rcvd.cnt--;
361-
pvt->ctx.csp->ncmds_rcvd_ndups.cnt++;
357+
cmd->csp->ncmds_rcvd.cnt--;
358+
cmd->csp->ncmds_rcvd_ndups.cnt++;
362359
return (1);
363360
}
364361

@@ -379,7 +376,7 @@ rtpp_command_split(struct rtpp_command *cmd, int len, int *rval,
379376
RTPP_LOG(pvt->ctx.cfs->glog, RTPP_LOG_DBUG, "received command \"%s\"",
380377
cmd->buf);
381378
}
382-
pvt->ctx.csp->ncmds_rcvd.cnt++;
379+
cmd->csp->ncmds_rcvd.cnt++;
383380

384381
cap = &cmd->args;
385382
rtpp_strsplit(cmd->buf, mbuf, len, sizeof(mbuf));

0 commit comments

Comments
 (0)