Skip to content

Commit

Permalink
mi_datagram: adhere to the new JSON-RPC mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
rvlad-patrascu committed Jan 15, 2019
1 parent 9fbcbce commit 725b8ef
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 1,073 deletions.
456 changes: 178 additions & 278 deletions modules/mi_datagram/datagram_fnc.c

Large diffs are not rendered by default.

30 changes: 3 additions & 27 deletions modules/mi_datagram/datagram_fnc.h
Expand Up @@ -39,42 +39,18 @@

#include "mi_datagram.h"

#define DATAGRAM_SOCK_BUF_SIZE 65457

#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)

#define MI_ERR( CODE, REASON) TOSTRING(CODE##_ERR_CODE) " " REASON##_REASON "\n"

#define MI_INTERNAL_ERR_CODE 500
#define MI_PARSE_ERR_CODE 400

#define MI_COMMAND_FAILED_REASON "command failed"

#define MI_COMMAND_FAILED MI_ERR(MI_INTERNAL, MI_COMMAND_FAILED)
#define MI_COMMAND_FAILED_LEN (sizeof(MI_COMMAND_FAILED)-1)

#define MI_COMMAND_NOT_AVAILABLE_REASON "command not available"
#define MI_COMMAND_NOT_AVAILABLE MI_ERR(MI_INTERNAL, MI_COMMAND_NOT_AVAILABLE)
#define MI_COMMAND_AVAILABLE_LEN (sizeof(MI_COMMAND_NOT_AVAILABLE)-1)

#define MI_PARSE_ERROR_REASON "parse_error"
#define MI_PARSE_ERROR MI_ERR(MI_PARSE, MI_PARSE_ERROR)
#define MI_PARSE_ERROR "400 Parse_error"
#define MI_PARSE_ERROR_LEN (sizeof(MI_PARSE_ERROR)-1)

#define MI_INTERNAL_ERROR_REASON "Internal server error"
#define MI_INTERNAL_ERROR MI_ERR(MI_INTERNAL, MI_INTERNAL_ERROR)
#define MI_INTERNAL_ERROR "500 Internal error"
#define MI_INTERNAL_ERROR_LEN (sizeof(MI_INTERNAL_ERROR)-1)

typedef struct datagram_str{
char * start, * current;
int len;
}datagram_stream;

typedef struct rx_tx{
int rx_sock, tx_sock;
}rx_tx_sockets;


int mi_init_datagram_server(sockaddr_dtgram * address, unsigned int domain,
rx_tx_sockets * socks,int mode,
int uid, int gid );
Expand Down
75 changes: 29 additions & 46 deletions modules/mi_datagram/doc/mi_datagram_admin.xml
Expand Up @@ -15,28 +15,12 @@
<section>
<title>DATAGRAM command syntax</title>
<para>
The external commands issued via DATAGRAM interface must follow the
following syntax:
<itemizedlist>
<listitem><para><emphasis>
request = first_line (argument '\n')*
</emphasis></para></listitem>
<listitem><para><emphasis>
first_line = ':'command_name':''\n'
</emphasis></para></listitem>
<listitem><para><emphasis>
argument = (arg_name '::' (arg_value)? ) | (arg_value)
</emphasis></para></listitem>
<listitem><para><emphasis>
arg_name = not-quoted_string
</emphasis></para></listitem>
<listitem><para><emphasis>
arg_value = not-quoted_string | '"' string '"'
</emphasis></para></listitem>
<listitem><para><emphasis>
not-quoted_string = string - {',",\n,\r}
</emphasis></para></listitem>
</itemizedlist>
The MI requests and replies follow the
<ulink url="http://www.jsonrpc.org/specification">JSON-RPC</ulink> syntax.
</para>
<para>
If case of an error generated by the MI engine, mostly internal
errors, an error message in plain text is sent back in the datagram.
</para>
</section>

Expand Down Expand Up @@ -206,26 +190,6 @@ modparam("mi_datagram", "socket_timeout", 2000)
</example>
</section>

<section id="param_reply_indent" xreflabel="reply_indent">
<title><varname>reply_indent</varname> (string)</title>
<para>
Strings to be used for line indentation. As the MI data structure
is tree oriendeted, the depth level will printed as indentation.
</para>
<para>
<emphasis>
Default value is <quote>"\t" (TAB)</quote>.
</emphasis>
</para>
<example>
<title>Set <varname>reply_indent</varname> parameter</title>
<programlisting format="linespecific">
...
modparam("mi_datagram", "reply_indent", " ")
...
</programlisting>
</example>
</section>
<section id="param_trace_destination" xreflabel="trace_destination">
<title><varname>trace_destination</varname> (string)</title>
<para>
Expand Down Expand Up @@ -299,6 +263,27 @@ modparam("mi_datagram", "trace_bwlist", "w: sip_trace")
</example>
</section>

<section id="param_pretty_printing" xreflabel="pretty_printing">
<title><varname>pretty_printing</varname> (int)</title>
<para>
Indicates whether the JSONRPC responses sent through MI should
be pretty-printed or not.
</para>
<para>
<emphasis>
Default value is <quote>0 - no pretty-printing</quote>.
</emphasis>
</para>
<example>
<title>Set <varname>pretty_printing</varname> parameter</title>
<programlisting format="linespecific">
...
modparam("mi_fifo", "pretty_printing", 1)
...
</programlisting>
</example>
</section>



</section>
Expand All @@ -316,15 +301,13 @@ modparam("mi_datagram", "trace_bwlist", "w: sip_trace")
<para>
This is an example showing the DATAGRAM format for the
<quote>get_statistics dialog: tm:</quote> MI commad:
response.
request.
</para>
<example>
<title>DATAGRAM request</title>
<programlisting format="linespecific">

:get_statistics:\n
dialog:\n
tm:\n
{"jsonrpc":"2.0","method":"get_statistics","id":"1065","params":["dialog:","tm:"]}

</programlisting>
</example>
Expand Down
30 changes: 2 additions & 28 deletions modules/mi_datagram/mi_datagram.c
Expand Up @@ -17,14 +17,8 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*
* History:
* ---------
* 2007-06-25 first version (ancuta)
*/



#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
Expand All @@ -40,7 +34,6 @@
#include <stdio.h>
#include <fcntl.h>


#include "../../sr_module.h"
#include "../../resolve.h"
#include "../../dprint.h"
Expand All @@ -51,11 +44,8 @@
#include "../../ip_addr.h"
#include "mi_datagram.h"
#include "datagram_fnc.h"
#include "mi_datagram_parser.h"
#include "mi_datagram_writer.h"
#include "../../mi/mi_trace.h"


/* AF_LOCAL is not defined on solaris */

#if !defined(AF_LOCAL)
Expand All @@ -65,7 +55,6 @@
#define PF_LOCAL PF_UNIX
#endif


#define MAX_CTIME_LEN 128
#define MAX_NB_PORT 65535

Expand All @@ -92,16 +81,14 @@ static int mi_unix_socket_gid = -1;
static char *mi_unix_socket_gid_s = 0;
static int mi_unix_socket_mode = S_IRUSR| S_IWUSR| S_IRGRP| S_IWGRP;

/* mi specific parameters */
static char *mi_reply_indent = DEFAULT_MI_REPLY_IDENT;

static str trace_destination_name = {NULL, 0};
trace_dest t_dst;

/* tracing is disabled by default */
int mi_trace_mod_id = -1;
char* mi_trace_bwlist_s;

int mi_datagram_pp;


static proc_export_t mi_procs[] = {
Expand All @@ -120,9 +107,9 @@ static param_export_t mi_params[] = {
{"unix_socket_group", INT_PARAM, &mi_unix_socket_gid },
{"unix_socket_user", STR_PARAM, &mi_unix_socket_uid_s },
{"unix_socket_user", INT_PARAM, &mi_unix_socket_uid },
{"reply_indent", STR_PARAM, &mi_reply_indent },
{"trace_destination", STR_PARAM, &trace_destination_name.s},
{"trace_bwlist", STR_PARAM, &mi_trace_bwlist_s },
{"pretty_printing", INT_PARAM, &mi_datagram_pp},
{0,0,0}
};

Expand Down Expand Up @@ -287,13 +274,6 @@ static int mi_mod_init(void)

static int mi_child_init(int rank)
{
if ( rank>PROC_MAIN ) {
if(mi_datagram_writer_init( DATAGRAM_SOCK_BUF_SIZE ,
mi_reply_indent )!= 0){
LM_CRIT("failed to initiate mi_datagram_writer\n");
return -1;
}
}
return 0;
}

Expand Down Expand Up @@ -330,12 +310,6 @@ static void datagram_process(int rank)
exit(-1);
}

if (mi_datagram_writer_init( DATAGRAM_SOCK_BUF_SIZE ,
mi_reply_indent )!= 0){
LM_CRIT("failed to initiate mi_datagram_writer\n");
exit(-1);
}

/* if tracing enabled init correlation id */
if ( t_dst ) {
if ( load_correlation_id() < 0 ) {
Expand Down
8 changes: 0 additions & 8 deletions modules/mi_datagram/mi_datagram.h
Expand Up @@ -28,14 +28,6 @@

#include <sys/socket.h>

#define DEFAULT_MI_REPLY_IDENT "\t"
#define MI_CMD_SEPARATOR ':'

/* the 2-chars separator between name and value */
#define MI_ATTR_VAL_SEP1 ':'
#define MI_ATTR_VAL_SEP2 ':'


/* maximum size for the socket reply name */
#define MAX_MI_FILENAME 128

Expand Down

0 comments on commit 725b8ef

Please sign in to comment.