[sr-dev] git:master: ctl: init binrpc callbacks in mod init and implement '[' specifier

Daniel-Constantin Mierla miconda at gmail.com
Wed Apr 23 23:11:32 CEST 2014


Module: sip-router
Branch: master
Commit: 9a404279c9e4d07795c82848795622f5f0da0ca2
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=9a404279c9e4d07795c82848795622f5f0da0ca2

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date:   Wed Apr 23 23:03:10 2014 +0200

ctl: init binrpc callbacks in mod init and implement '[' specifier

- '[' allows adding an array
- implemented rpc api member array_add

---

 modules/ctl/binrpc_run.c |   89 +++++++++++++++++++++++++++++++++++++++------
 modules/ctl/binrpc_run.h |    2 +
 modules/ctl/ctl.c        |    3 ++
 3 files changed, 82 insertions(+), 12 deletions(-)

diff --git a/modules/ctl/binrpc_run.c b/modules/ctl/binrpc_run.c
index f0ee510..216b203 100644
--- a/modules/ctl/binrpc_run.c
+++ b/modules/ctl/binrpc_run.c
@@ -118,24 +118,28 @@ static int rpc_add(struct binrpc_ctx* ctx, char* fmt, ...);
 static int rpc_scan(struct binrpc_ctx* ctx, char* fmt, ...);
 static int rpc_printf(struct binrpc_ctx* ctx, char* fmt, ...);
 static int rpc_struct_add(struct rpc_struct_l* s, char* fmt, ...);
+static int rpc_array_add(struct rpc_struct_l* s, char* fmt, ...);
 static int rpc_struct_scan(struct rpc_struct_l* s, char* fmt, ...);
 /* struct scan */
 static int rpc_struct_printf(struct rpc_struct_l *s, char* name,
 								char* fmt, ...);
 
 
-static rpc_t binrpc_callbacks={
-	(rpc_fault_f)			rpc_fault,
-	(rpc_send_f)			rpc_send,
-	(rpc_add_f)				rpc_add,
-	(rpc_scan_f)			rpc_scan,
-	(rpc_printf_f)			rpc_printf,
-	(rpc_struct_add_f)		rpc_struct_add,
-	(rpc_struct_scan_f)		rpc_struct_scan,
-	(rpc_struct_printf_f)	rpc_struct_printf
-};
-
+static rpc_t binrpc_callbacks;
 
+void binrpc_callbacks_init(void)
+{
+	memset(&binrpc_callbacks, 0, sizeof(binrpc_callbacks));
+	binrpc_callbacks.fault         = (rpc_fault_f)rpc_fault;
+	binrpc_callbacks.send          = (rpc_send_f)rpc_send;
+	binrpc_callbacks.add           = (rpc_add_f)rpc_add;
+	binrpc_callbacks.scan          = (rpc_scan_f)rpc_scan;
+	binrpc_callbacks.printf        = (rpc_printf_f)rpc_printf;
+	binrpc_callbacks.struct_add    = (rpc_struct_add_f)rpc_struct_add;
+	binrpc_callbacks.array_add     = (rpc_struct_add_f)rpc_array_add;
+	binrpc_callbacks.struct_scan   = (rpc_struct_scan_f)rpc_struct_scan;
+	binrpc_callbacks.struct_printf = (rpc_struct_printf_f)rpc_struct_printf;
+}
 
 /** mark a pointer for freeing when the ctx is destroyed.
  * @return 0 on success, -1 on error
@@ -977,6 +981,7 @@ static int rpc_add(struct binrpc_ctx* ctx, char* fmt, ...)
 				if (err<0) goto error_add;
 				break;
 			case '{':
+			case '[':
 				err=binrpc_start_struct(&ctx->out.pkt);
 				if (err<0) goto error_add;
 				rs=new_rpc_struct();
@@ -1080,6 +1085,7 @@ static int rpc_struct_add(struct rpc_struct_l* s, char* fmt, ...)
 				avp.u.strval=*(va_arg(ap, str*));
 				break;
 			case '{':
+			case '[':
 				avp.type=BINRPC_T_STRUCT;
 				err=binrpc_addavp(&s->pkt, &avp);
 				if (err<0) goto error_add;
@@ -1113,7 +1119,66 @@ error:
 	return -1;
 }
 
-
+/* returns  0 on success, -1 on error */
+static int rpc_array_add(struct rpc_struct_l* s, char* fmt, ...)
+{
+	va_list ap;
+	int err;
+	char* sv;
+	str* st;
+	struct rpc_struct_l* rs;
+	
+	va_start(ap, fmt);
+	for (;*fmt; fmt++){
+		switch(*fmt){
+			case 'd':
+			case 't':
+			case 'b':
+				err=binrpc_addint(&s->pkt, va_arg(ap, int));
+				if (err<0) goto error_add;
+				break;
+			case 's': /* asciiz */
+				sv=va_arg(ap, char*);
+				if (sv==0) /* fix null strings */
+					sv="<null string>"; 
+				err=binrpc_addstr(&s->pkt, sv, strlen(sv));
+				if (err<0) goto error_add;
+				break;
+			case 'S': /* str */
+				st=va_arg(ap, str*);
+				err=binrpc_addstr(&s->pkt, st->s, st->len);
+				if (err<0) goto error_add;
+				break;
+			case '{':
+			case '[':
+				err=binrpc_start_struct(&s->pkt);
+				if (err<0) goto error_add;
+				rs=new_rpc_struct();
+				if (rs==0) goto error_mem;
+				rs->offset=binrpc_pkt_len(&s->pkt);
+				err=binrpc_end_struct(&s->pkt);
+				if (err<0) goto error_add;
+				clist_append(&s->substructs, rs, next, prev);
+				*(va_arg(ap, void**))=rs;
+				break;
+			case 'f': 
+				err=binrpc_adddouble(&s->pkt, va_arg(ap, double));
+				if (err<0) goto error_add;
+				break;
+			default: 
+				LOG(L_CRIT, "BUG: binrpc: rpc_add: formatting char \'%c\'"
+							" not supported\n", *fmt);
+				goto error;
+		}
+	}
+	va_end(ap);
+	return 0;
+error_mem:
+error_add:
+error:
+	va_end(ap);
+	return -1;
+}
 
 /* returns  0 on success, -1 on error */
 static int rpc_struct_printf(struct rpc_struct_l *s, char* name,
diff --git a/modules/ctl/binrpc_run.h b/modules/ctl/binrpc_run.h
index 6224934..680ea5f 100644
--- a/modules/ctl/binrpc_run.h
+++ b/modules/ctl/binrpc_run.h
@@ -36,4 +36,6 @@
 int process_rpc_req(unsigned char* buf, int size, int* bytes_needed,
 					void* sh, void** saved_state);
 
+void binrpc_callbacks_init(void);
+
 #endif
diff --git a/modules/ctl/ctl.c b/modules/ctl/ctl.c
index bd675f8..b476ea5 100644
--- a/modules/ctl/ctl.c
+++ b/modules/ctl/ctl.c
@@ -50,6 +50,7 @@
 MODULE_VERSION
 
 #include "ctl_defaults.h"
+#include "binrpc_run.h"
 #ifdef USE_FIFO
 #include "fifo_server.h"
 #endif
@@ -229,6 +230,8 @@ static int mod_init(void)
 {
 	struct id_list* l;
 
+	binrpc_callbacks_init();
+
 	if(binrpc_max_body_size<=0)
 		binrpc_max_body_size = 4;
 	if(binrpc_struct_max_body_size<=0)




More information about the sr-dev mailing list