1 /*
2  * Copyright (c) 2014 Sippy Software, Inc., http://www.sippysoft.com
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27 
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <errno.h>
31 #include <stdint.h>
32 #include <string.h>
33 #include <unistd.h>
34 
35 #include "config.h"
36 
37 #include "rtpp_log.h"
38 #include "rtpp_types.h"
39 #include "rtpp_log_obj.h"
40 #include "rtpp_cfg_stable.h"
41 #include "rtpp_defines.h"
42 #include "rtpp_command.h"
43 #include "rtpp_command_private.h"
44 #include "rtpp_command_parse.h"
45 #include "rtpp_command_stream.h"
46 #include "rtpp_util.h"
47 
48 static void
rtpp_command_stream_compact(struct rtpp_cmd_connection * rcs)49 rtpp_command_stream_compact(struct rtpp_cmd_connection *rcs)
50 {
51     char *cp;
52     int clen;
53 
54     if (rcs->inbuf_ppos == 0 || rcs->inbuf_epos == 0)
55         return;
56     if (rcs->inbuf_ppos == rcs->inbuf_epos) {
57         rcs->inbuf_ppos = 0;
58         rcs->inbuf_epos = 0;
59         return;
60     }
61     cp = &rcs->inbuf[rcs->inbuf_ppos];
62     clen = rcs->inbuf_epos - rcs->inbuf_ppos;
63     memcpy(rcs->inbuf, cp, clen);
64     rcs->inbuf_epos = clen;
65     rcs->inbuf_ppos = 0;
66 }
67 
68 int
rtpp_command_stream_doio(struct cfg * cf,struct rtpp_cmd_connection * rcs)69 rtpp_command_stream_doio(struct cfg *cf, struct rtpp_cmd_connection *rcs)
70 {
71     int len, blen;
72     char *cp;
73 
74     rtpp_command_stream_compact(rcs);
75     cp = &(rcs->inbuf[rcs->inbuf_epos]);
76     blen = sizeof(rcs->inbuf) - rcs->inbuf_epos;
77 
78     for (;;) {
79         len = read(rcs->controlfd_in, cp, blen);
80         if (len != -1 || (errno != EAGAIN && errno != EINTR))
81             break;
82     }
83     if (len == -1) {
84         if (errno != EAGAIN && errno != EINTR)
85             RTPP_ELOG(cf->stable->glog, RTPP_LOG_ERR, "can't read from control socket");
86         return (-1);
87     }
88     rcs->inbuf_epos += len;
89     return (len);
90 }
91 
92 struct rtpp_command *
rtpp_command_stream_get(struct cfg * cf,struct rtpp_cmd_connection * rcs,int * rval,double dtime,struct rtpp_command_stats * csp)93 rtpp_command_stream_get(struct cfg *cf, struct rtpp_cmd_connection *rcs,
94   int *rval, double dtime, struct rtpp_command_stats *csp)
95 {
96     char **ap;
97     char *cp, *cp1;
98     int len;
99     struct rtpp_command *cmd;
100 
101     if (rcs->inbuf_epos == rcs->inbuf_ppos) {
102         *rval = EAGAIN;
103         return (NULL);
104     }
105     cp = &(rcs->inbuf[rcs->inbuf_ppos]);
106     len = rcs->inbuf_epos - rcs->inbuf_ppos;
107     cp1 = memchr(cp, '\n', len);
108     if (cp1 == NULL) {
109         *rval = EAGAIN;
110         return (NULL);
111     }
112 
113     cmd = rtpp_command_ctor(cf, rcs->controlfd_out, dtime, rval, csp, 0);
114     if (cmd == NULL) {
115         return (NULL);
116     }
117 
118     if (rcs->rlen > 0) {
119         cmd->rlen = rcs->rlen;
120         memcpy(&cmd->raddr, &rcs->raddr, rcs->rlen);
121     }
122 
123     len = cp1 - cp;
124     memcpy(cmd->buf, cp, len);
125     cmd->buf[len] = '\0';
126     rcs->inbuf_ppos += len + 1;
127 
128     RTPP_LOG(cf->stable->glog, RTPP_LOG_DBUG, "received command \"%s\"", cmd->buf);
129     csp->ncmds_rcvd.cnt++;
130 
131     cp = cmd->buf;
132     for (ap = cmd->argv; (*ap = rtpp_strsep(&cp, "\r\n\t ")) != NULL;) {
133         if (**ap != '\0') {
134             cmd->argc++;
135             if (++ap >= &cmd->argv[RTPC_MAX_ARGC])
136                 break;
137         }
138     }
139 
140     if (cmd->argc < 1) {
141         RTPP_LOG(cf->stable->glog, RTPP_LOG_ERR, "command syntax error");
142         reply_error(cmd, ECODE_PARSE_1);
143         *rval = EINVAL;
144         free_command(cmd);
145         return (NULL);
146     }
147 
148     /* Step I: parse parameters that are common to all ops */
149     if (rtpp_command_pre_parse(cf, cmd) != 0) {
150         /* Error reply is handled by the rtpp_command_pre_parse() */
151         *rval = 0;
152         free_command(cmd);
153         return (NULL);
154     }
155 
156     return (cmd);
157 }
158