From 4940fd2e0a13ef3438dbf93c981d9e8e4f475c56 Mon Sep 17 00:00:00 2001 From: Victor Seva Date: Wed, 17 May 2023 16:36:01 +0200 Subject: [PATCH] ctl: clang-format for coherent indentation and coding style --- src/modules/ctl/binrpc.c | 27 +- src/modules/ctl/binrpc.h | 767 ++++++++-------- src/modules/ctl/binrpc_run.c | 1104 ++++++++++++----------- src/modules/ctl/binrpc_run.h | 4 +- src/modules/ctl/ctl.c | 311 ++++--- src/modules/ctl/ctl.h | 1 - src/modules/ctl/ctl_defaults.h | 3 +- src/modules/ctl/ctrl_socks.c | 284 +++--- src/modules/ctl/ctrl_socks.h | 45 +- src/modules/ctl/fifo_server.c | 1501 +++++++++++++++++--------------- src/modules/ctl/fifo_server.h | 14 +- src/modules/ctl/init_socks.c | 207 ++--- src/modules/ctl/init_socks.h | 27 +- src/modules/ctl/io_listener.c | 493 +++++------ src/modules/ctl/io_listener.h | 19 +- 15 files changed, 2472 insertions(+), 2335 deletions(-) diff --git a/src/modules/ctl/binrpc.c b/src/modules/ctl/binrpc.c index 6239f6d2928..5004482b3c8 100644 --- a/src/modules/ctl/binrpc.c +++ b/src/modules/ctl/binrpc.c @@ -22,25 +22,18 @@ #include "binrpc.h" /* WARNING: keep in sync with the errors listed in binrpc.h */ -static const char* binrpc_str_errors[]={ - "no error", - "invalid function arguments", - "buffer too small (overflow)", - "corrupted packet", - "more data needed", - "end of packet encountered", - "binrpc parse context not initialized", - "record doesn't match type", - "bad record", - "bug -- internal error", - "unknown/invalid error code" -}; +static const char *binrpc_str_errors[] = {"no error", + "invalid function arguments", "buffer too small (overflow)", + "corrupted packet", "more data needed", "end of packet encountered", + "binrpc parse context not initialized", "record doesn't match type", + "bad record", "bug -- internal error", "unknown/invalid error code"}; - -const char* binrpc_error(int err) +const char *binrpc_error(int err) { - if (err<0) err=-err; - if (err>(-E_BINRPC_LAST)) err=-E_BINRPC_LAST; + if(err < 0) + err = -err; + if(err > (-E_BINRPC_LAST)) + err = -E_BINRPC_LAST; return binrpc_str_errors[err]; } diff --git a/src/modules/ctl/binrpc.h b/src/modules/ctl/binrpc.h index c3b8c4811e6..405de4c9a6e 100644 --- a/src/modules/ctl/binrpc.h +++ b/src/modules/ctl/binrpc.h @@ -21,7 +21,6 @@ /* binrpc is supposed to be a minimalist binary rpc implementation */ - /* packet header: * (big endian where it applies) * 4b 4b 4b 2b 2b @@ -98,89 +97,96 @@ #include #define BINRPC_MAGIC 0xA -#define BINRPC_VERS 1 +#define BINRPC_VERS 1 /* sizes & offsets */ -#define BINRPC_FIXED_HDR_SIZE 2 -#define BINRPC_TLEN_OFFSET BINRPC_FIXED_HDR_SIZE -#define BINRPC_MIN_HDR_SIZE (BINRPC_FIXED_HDR_SIZE+2) -#define BINRPC_MAX_HDR_SIZE (BINRPC_FIXED_HDR_SIZE+4+4) -#define BINRPC_MIN_RECORD_SIZE 1 +#define BINRPC_FIXED_HDR_SIZE 2 +#define BINRPC_TLEN_OFFSET BINRPC_FIXED_HDR_SIZE +#define BINRPC_MIN_HDR_SIZE (BINRPC_FIXED_HDR_SIZE + 2) +#define BINRPC_MAX_HDR_SIZE (BINRPC_FIXED_HDR_SIZE + 4 + 4) +#define BINRPC_MIN_RECORD_SIZE 1 /* min pkt size: min header + min. len (1) + min. cookie (1)*/ -#define BINRPC_MIN_PKT_SIZE BINRPC_MIN_HDR_SIZE +#define BINRPC_MIN_PKT_SIZE BINRPC_MIN_HDR_SIZE /* message types */ -#define BINRPC_REQ 0 -#define BINRPC_REPL 1 +#define BINRPC_REQ 0 +#define BINRPC_REPL 1 #define BINRPC_FAULT 3 /* values types */ -#define BINRPC_T_INT 0 -#define BINRPC_T_STR 1 /* 0 term, for easier parsing */ -#define BINRPC_T_DOUBLE 2 -#define BINRPC_T_STRUCT 3 -#define BINRPC_T_ARRAY 4 -#define BINRPC_T_AVP 5 /* allowed only in structs */ -#define BINRPC_T_BYTES 6 /* like STR, but not 0 term */ - -#define BINRPC_T_ALL 0xf /* wildcard type, will match any record type +#define BINRPC_T_INT 0 +#define BINRPC_T_STR 1 /* 0 term, for easier parsing */ +#define BINRPC_T_DOUBLE 2 +#define BINRPC_T_STRUCT 3 +#define BINRPC_T_ARRAY 4 +#define BINRPC_T_AVP 5 /* allowed only in structs */ +#define BINRPC_T_BYTES 6 /* like STR, but not 0 term */ + +#define BINRPC_T_ALL \ + 0xf /* wildcard type, will match any record type in the packet (not allowed inside the pkt)*/ /* errors */ -#define E_BINRPC_INVAL -1 /* invalid function call parameters */ -#define E_BINRPC_OVERFLOW -2 /* buffer overflow */ -#define E_BINRPC_BADPKT -3 /* something went really bad, the packet is +#define E_BINRPC_INVAL -1 /* invalid function call parameters */ +#define E_BINRPC_OVERFLOW -2 /* buffer overflow */ +#define E_BINRPC_BADPKT \ + -3 /* something went really bad, the packet is corrupted*/ -#define E_BINRPC_MORE_DATA -4 /* parsing error: more bytes are needed, +#define E_BINRPC_MORE_DATA \ + -4 /* parsing error: more bytes are needed, just repeat the failed op, when you have more bytes available */ -#define E_BINRPC_EOP -5 /* end of packet reached */ -#define E_BINRPC_NOTINIT -6 /* parse ctx not initialized */ -#define E_BINRPC_TYPE -7 /* unknown type for record, or requested +#define E_BINRPC_EOP -5 /* end of packet reached */ +#define E_BINRPC_NOTINIT -6 /* parse ctx not initialized */ +#define E_BINRPC_TYPE \ + -7 /* unknown type for record, or requested type doesn't match record type */ -#define E_BINRPC_RECORD -8 /* bad record (unexpected, bad struct a.s.o)*/ -#define E_BINRPC_BUG -9 /* internal error, bug */ -#define E_BINRPC_LAST -10 /* used to count the errors, keep always +#define E_BINRPC_RECORD -8 /* bad record (unexpected, bad struct a.s.o)*/ +#define E_BINRPC_BUG -9 /* internal error, bug */ +#define E_BINRPC_LAST \ + -10 /* used to count the errors, keep always last */ /* flags */ -#define BINRPC_F_INIT 1 +#define BINRPC_F_INIT 1 -struct binrpc_pkt{ /* binrpc body */ - unsigned char* body; - unsigned char* end; - unsigned char* crt; /*private */ +struct binrpc_pkt +{ /* binrpc body */ + unsigned char *body; + unsigned char *end; + unsigned char *crt; /*private */ }; -struct binrpc_parse_ctx{ +struct binrpc_parse_ctx +{ /* header */ unsigned int tlen; /* total len */ unsigned int cookie; int type; /* request, reply, error */ - + /* parsing info */ - unsigned int flags; /* parsing flags */ + unsigned int flags; /* parsing flags */ unsigned int offset; /* current offset (inside payload) */ unsigned int in_struct; unsigned int in_array; }; - -struct binrpc_val{ +struct binrpc_val +{ str name; /* used only in structs */ int type; - union{ + union + { str strval; double fval; int intval; int end; - }u; + } u; }; - /* helper functions */ /* return int size: minimum number of bytes needed to represent it @@ -188,73 +194,71 @@ struct binrpc_val{ inline static int binrpc_get_int_len(int i) { int size; - for (size=4; size && ((i & (0xffu<<24))==0); i<<=8, size--); + for(size = 4; size && ((i & (0xffu << 24)) == 0); i <<= 8, size--) + ; return size; } - /* adds a start or end tag (valid only for STRUCT or ARRAY for now */ -inline static int binrpc_add_tag(struct binrpc_pkt* pkt, int type, int end) +inline static int binrpc_add_tag(struct binrpc_pkt *pkt, int type, int end) { - if (pkt->crt>=pkt->end) return E_BINRPC_OVERFLOW; - *pkt->crt=(end<<7)|type; + if(pkt->crt >= pkt->end) + return E_BINRPC_OVERFLOW; + *pkt->crt = (end << 7) | type; pkt->crt++; return 0; } - /* writes a minimal long long, returns the new offset and sets * len to the number of bytes written (<=8) * to check for oveflow use: returned_value-p != *len * (Note: if *len==0 using the test above succeeds even if p>=end) */ -inline static unsigned char* binrpc_write_llong( unsigned char* p, - unsigned char* end, - long long i, int *len) +inline static unsigned char *binrpc_write_llong( + unsigned char *p, unsigned char *end, long long i, int *len) { int size; unsigned long long u; u = (unsigned long long)i; - for (size=8; size && ((u & (0xffull<<56))==0); u<<=8, size--); - *len=size; - for(; (p>56); - u<<=8; + for(size = 8; size && ((u & (0xffull << 56)) == 0); u <<= 8, size--) + ; + *len = size; + for(; (p < end) && (size); p++, size--) { + *p = (unsigned char)(u >> 56); + u <<= 8; } return p; } - /* writes a minimal int, returns the new offset and sets * len to the number of bytes written (<=4) * to check for oveflow use: returned_value-p != *len * (Note: if *len==0 using the test above succeeds even if p>=end) */ -inline static unsigned char* binrpc_write_int( unsigned char* p, - unsigned char* end, - int i, int *len) +inline static unsigned char *binrpc_write_int( + unsigned char *p, unsigned char *end, int i, int *len) { int size; unsigned int u; u = (unsigned int)i; - for (size=4; size && ((u & (0xffu<<24))==0); u<<=8, size--); - *len=size; - for(; (p>24); - u<<=8; + for(size = 4; size && ((u & (0xffu << 24)) == 0); u <<= 8, size--) + ; + *len = size; + for(; (p < end) && (size); p++, size--) { + *p = (unsigned char)(u >> 24); + u <<= 8; } return p; } - /* API functions */ /* initialize a binrpc_pkt structure, for packet creation @@ -269,36 +273,33 @@ inline static unsigned char* binrpc_write_int( unsigned char* p, * ... * bytes=binrpc_build_hdr(pkt, BINRPC_REQ, 0x123, hdr_buf, HDR_BUF_LEN); * writev(sock, {{ hdr, bytes}, {pkt->body, pkt->crt-pkt->body}} , 2)*/ -inline static int binrpc_init_pkt(struct binrpc_pkt *pkt, - unsigned char* buf, int b_len) +inline static int binrpc_init_pkt( + struct binrpc_pkt *pkt, unsigned char *buf, int b_len) { - if (b_lenbody=buf; - pkt->end=buf+b_len; - pkt->crt=pkt->body; + pkt->body = buf; + pkt->end = buf + b_len; + pkt->crt = pkt->body; return 0; }; - /* used to update internal contents if the original buffer * (from binrpc_init_pkt) was realloc'ed (and has grown) */ -inline static int binrpc_pkt_update_buf(struct binrpc_pkt *pkt, - unsigned char* new_buf, - int new_len) +inline static int binrpc_pkt_update_buf( + struct binrpc_pkt *pkt, unsigned char *new_buf, int new_len) { - if ((int)(pkt->crt-pkt->body)>new_len){ + if((int)(pkt->crt - pkt->body) > new_len) { return E_BINRPC_OVERFLOW; } - pkt->crt=new_buf+(pkt->crt-pkt->body); - pkt->body=new_buf; - pkt->end=new_buf+new_len; + pkt->crt = new_buf + (pkt->crt - pkt->body); + pkt->body = new_buf; + pkt->end = new_buf + new_len; return 0; } - /* builds a binrpc header for the binrpc pkt. body pkt and writes it in buf * params: * type - binrpc packet type (request, reply, fault) @@ -306,67 +307,68 @@ inline static int binrpc_pkt_update_buf(struct binrpc_pkt *pkt, * cookie - binrpc cookie value * buf,len - destination buffer & len * returns -1 on error, number of bytes written on success */ -inline static int binrpc_build_hdr( int type, int body_len, - unsigned int cookie, - unsigned char* buf, int b_len) +inline static int binrpc_build_hdr(int type, int body_len, unsigned int cookie, + unsigned char *buf, int b_len) { - unsigned char* p; + unsigned char *p; int len_len; int c_len; - - len_len=binrpc_get_int_len(body_len); - c_len=binrpc_get_int_len(cookie); - if (len_len==0) len_len=1; /* we can't have 0 len */ - if (c_len==0) c_len=1; /* we can't have 0 len */ + + len_len = binrpc_get_int_len(body_len); + c_len = binrpc_get_int_len(cookie); + if(len_len == 0) + len_len = 1; /* we can't have 0 len */ + if(c_len == 0) + c_len = 1; /* we can't have 0 len */ /* size check: 2 bytes header + len_len + cookie len*/ - if (b_len<(BINRPC_FIXED_HDR_SIZE+len_len+c_len)){ + if(b_len < (BINRPC_FIXED_HDR_SIZE + len_len + c_len)) { goto error_len; } - p=buf; - *p=(BINRPC_MAGIC << 4) | BINRPC_VERS; + p = buf; + *p = (BINRPC_MAGIC << 4) | BINRPC_VERS; p++; - *p=(type<<4)|((len_len-1)<<2)|(c_len-1); + *p = (type << 4) | ((len_len - 1) << 2) | (c_len - 1); p++; - for(;len_len>0; len_len--,p++){ - *p=(unsigned char)(body_len>>((len_len-1)*8)); + for(; len_len > 0; len_len--, p++) { + *p = (unsigned char)(body_len >> ((len_len - 1) * 8)); } - for(;c_len>0; c_len--,p++){ - *p=(unsigned char)(cookie>>((c_len-1)*8)); + for(; c_len > 0; c_len--, p++) { + *p = (unsigned char)(cookie >> ((c_len - 1) * 8)); } - return (int)(p-buf); + return (int)(p - buf); error_len: return E_BINRPC_OVERFLOW; } - -#define binrpc_pkt_len(pkt) ((int)((pkt)->crt-(pkt)->body)) - +#define binrpc_pkt_len(pkt) ((int)((pkt)->crt - (pkt)->body)) /* changes the length of a header (enough space must be available) */ -inline static int binrpc_hdr_change_len(unsigned char* hdr, int hdr_len, - int new_len) +inline static int binrpc_hdr_change_len( + unsigned char *hdr, int hdr_len, int new_len) { int len_len; - - binrpc_write_int(&hdr[BINRPC_TLEN_OFFSET], hdr+hdr_len, new_len, &len_len); + + binrpc_write_int( + &hdr[BINRPC_TLEN_OFFSET], hdr + hdr_len, new_len, &len_len); return 0; } /* int format: size TYPE */ -inline static int binrpc_add_llong_type(struct binrpc_pkt* pkt, long long i, int type) +inline static int binrpc_add_llong_type( + struct binrpc_pkt *pkt, long long i, int type) { - unsigned char* p; + unsigned char *p; int size; - p=binrpc_write_llong(pkt->crt+1, pkt->end, i, &size); - if ((pkt->crt>=pkt->end) || ((int)(p-pkt->crt-1)!=size)) + p = binrpc_write_llong(pkt->crt + 1, pkt->end, i, &size); + if((pkt->crt >= pkt->end) || ((int)(p - pkt->crt - 1) != size)) goto error_len; - *(pkt->crt)=(size<<4) | type; - pkt->crt=p; + *(pkt->crt) = (size << 4) | type; + pkt->crt = p; return 0; error_len: return E_BINRPC_OVERFLOW; @@ -374,45 +376,42 @@ inline static int binrpc_add_llong_type(struct binrpc_pkt* pkt, long long i, int /* int format: size BINRPC_T_INT */ -inline static int binrpc_add_int_type(struct binrpc_pkt* pkt, int i, int type) +inline static int binrpc_add_int_type(struct binrpc_pkt *pkt, int i, int type) { - - unsigned char* p; + + unsigned char *p; int size; - - p=binrpc_write_int(pkt->crt+1, pkt->end, i, &size); - if ((pkt->crt>=pkt->end) || ((int)(p-pkt->crt-1)!=size)) + + p = binrpc_write_int(pkt->crt + 1, pkt->end, i, &size); + if((pkt->crt >= pkt->end) || ((int)(p - pkt->crt - 1) != size)) goto error_len; - *(pkt->crt)=(size<<4) | type; - pkt->crt=p; + *(pkt->crt) = (size << 4) | type; + pkt->crt = p; return 0; error_len: return E_BINRPC_OVERFLOW; } - /* double format: FIXME: for now a hack: fixed point represented in * a long long (=> max 3 decimals, < MAX_LLONG/1000) */ -#define binrpc_add_double_type(pkt, f, type)\ +#define binrpc_add_double_type(pkt, f, type) \ binrpc_add_llong_type((pkt), (long long)((f)*1000), (type)) - /* skip bytes bytes (leaves an empty space, for possible future use) * WARNING: use with care, low level function */ -inline static int binrpc_add_skip(struct binrpc_pkt* pkt, int bytes) +inline static int binrpc_add_skip(struct binrpc_pkt *pkt, int bytes) { - if ((pkt->crt+bytes)>=pkt->end) + if((pkt->crt + bytes) >= pkt->end) return E_BINRPC_OVERFLOW; - pkt->crt+=bytes; + pkt->crt += bytes; return 0; } - /* * adds only the string mark and len, you'll have to memcpy the contents * manually later (and also use binrpc_add_skip(pkt, l) or increase @@ -423,199 +422,187 @@ inline static int binrpc_add_skip(struct binrpc_pkt* pkt, int bytes) * WARNING1: BINRPC_T_STR and BINRPC_T_AVP must be 0 term, the len passed to * this function, must include the \0 in this case. */ -inline static int binrpc_add_str_mark(struct binrpc_pkt* pkt, int type, - int l) +inline static int binrpc_add_str_mark(struct binrpc_pkt *pkt, int type, int l) { int size; - unsigned char* p; - - if (pkt->crt>=pkt->end) goto error_len; - if (l<8){ - size=l; - p=pkt->crt+1; - }else{ /* we need a separate len */ - p=binrpc_write_int(pkt->crt+1, pkt->end, l, &size); - if (((int)(p-pkt->crt-1)!=size)) + unsigned char *p; + + if(pkt->crt >= pkt->end) + goto error_len; + if(l < 8) { + size = l; + p = pkt->crt + 1; + } else { /* we need a separate len */ + p = binrpc_write_int(pkt->crt + 1, pkt->end, l, &size); + if(((int)(p - pkt->crt - 1) != size)) goto error_len; - size|=8; /* mark it as having external len */ + size |= 8; /* mark it as having external len */ } - *(pkt->crt)=(size)<<4|type; - pkt->crt=p; + *(pkt->crt) = (size) << 4 | type; + pkt->crt = p; return 0; error_len: return E_BINRPC_OVERFLOW; } - -inline static int binrpc_add_str_type(struct binrpc_pkt* pkt, char* s, int len, - int type) +inline static int binrpc_add_str_type( + struct binrpc_pkt *pkt, char *s, int len, int type) { int size; int l; int zero_term; /* whether or not to add an extra 0 at the end */ - unsigned char* p; - - zero_term=((type==BINRPC_T_STR)||(type==BINRPC_T_AVP)); - l=len+zero_term; - if (l<8){ - size=l; - p=pkt->crt+1; - }else{ /* we need a separate len */ - p=binrpc_write_int(pkt->crt+1, pkt->end, l, &size); + unsigned char *p; + + zero_term = ((type == BINRPC_T_STR) || (type == BINRPC_T_AVP)); + l = len + zero_term; + if(l < 8) { + size = l; + p = pkt->crt + 1; + } else { /* we need a separate len */ + p = binrpc_write_int(pkt->crt + 1, pkt->end, l, &size); /* if ((int)(p-pkt->crt)<(size+1)) goto error_len; - not needed, * caught by the next check */ - size|=8; /* mark it as having external len */ + size |= 8; /* mark it as having external len */ } - if ((p+l)>pkt->end) goto error_len; - *(pkt->crt)=(size)<<4|type; + if((p + l) > pkt->end) + goto error_len; + *(pkt->crt) = (size) << 4 | type; memcpy(p, s, len); - if (zero_term) p[len]=0; - pkt->crt=p+l; + if(zero_term) + p[len] = 0; + pkt->crt = p + l; return 0; error_len: return E_BINRPC_OVERFLOW; } - /* adds an avp (name, value) pair, useful to add structure members */ -inline static int binrpc_addavp(struct binrpc_pkt* pkt, struct binrpc_val* avp) +inline static int binrpc_addavp(struct binrpc_pkt *pkt, struct binrpc_val *avp) { int ret; - unsigned char* bak; + unsigned char *bak; - bak=pkt->crt; - ret=binrpc_add_str_type(pkt, avp->name.s, avp->name.len, BINRPC_T_AVP); - if (ret<0) return ret; - switch (avp->type){ + bak = pkt->crt; + ret = binrpc_add_str_type(pkt, avp->name.s, avp->name.len, BINRPC_T_AVP); + if(ret < 0) + return ret; + switch(avp->type) { case BINRPC_T_INT: - ret=binrpc_add_int_type(pkt, avp->u.intval, avp->type); + ret = binrpc_add_int_type(pkt, avp->u.intval, avp->type); break; case BINRPC_T_STR: case BINRPC_T_BYTES: - ret=binrpc_add_str_type(pkt, avp->u.strval.s, - avp->u.strval.len, - avp->type); + ret = binrpc_add_str_type( + pkt, avp->u.strval.s, avp->u.strval.len, avp->type); break; case BINRPC_T_STRUCT: case BINRPC_T_ARRAY: - ret=binrpc_add_tag(pkt, avp->type, 0); + ret = binrpc_add_tag(pkt, avp->type, 0); break; case BINRPC_T_DOUBLE: - ret=binrpc_add_double_type(pkt, avp->u.fval, avp->type); + ret = binrpc_add_double_type(pkt, avp->u.fval, avp->type); break; default: - ret=E_BINRPC_BUG; + ret = E_BINRPC_BUG; } - if (ret<0) - pkt->crt=bak; /* roll back */ + if(ret < 0) + pkt->crt = bak; /* roll back */ return ret; } +#define binrpc_addint(pkt, i) binrpc_add_int_type((pkt), (i), BINRPC_T_INT) -#define binrpc_addint(pkt, i) binrpc_add_int_type((pkt), (i), BINRPC_T_INT) - -#define binrpc_adddouble(pkt, f) \ +#define binrpc_adddouble(pkt, f) \ binrpc_add_double_type((pkt), (f), BINRPC_T_DOUBLE) -#define binrpc_addstr(pkt, s, len) \ - binrpc_add_str_type((pkt), (s), (len), BINRPC_T_STR) +#define binrpc_addstr(pkt, s, len) \ + binrpc_add_str_type((pkt), (s), (len), BINRPC_T_STR) -#define binrpc_addbytes(pkt, s, len) \ - binrpc_add_str_type((pkt), (s), (len), BINRPC_T_BYTES) +#define binrpc_addbytes(pkt, s, len) \ + binrpc_add_str_type((pkt), (s), (len), BINRPC_T_BYTES) /* struct type format: * start : 0000 | BINRPC_T_STRUCT * end: 1000 | BINRPC_T_STRUCT */ -#define binrpc_start_struct(pkt) binrpc_add_tag((pkt), BINRPC_T_STRUCT, 0) +#define binrpc_start_struct(pkt) binrpc_add_tag((pkt), BINRPC_T_STRUCT, 0) -#define binrpc_end_struct(pkt) binrpc_add_tag((pkt), BINRPC_T_STRUCT, 1) +#define binrpc_end_struct(pkt) binrpc_add_tag((pkt), BINRPC_T_STRUCT, 1) -#define binrpc_start_array(pkt) binrpc_add_tag((pkt), BINRPC_T_ARRAY, 0) +#define binrpc_start_array(pkt) binrpc_add_tag((pkt), BINRPC_T_ARRAY, 0) -#define binrpc_end_array(pkt) binrpc_add_tag((pkt), BINRPC_T_ARRAY, 1) +#define binrpc_end_array(pkt) binrpc_add_tag((pkt), BINRPC_T_ARRAY, 1) -static inline int binrpc_addfault( struct binrpc_pkt* pkt, - int code, - char* s, int len) +static inline int binrpc_addfault( + struct binrpc_pkt *pkt, int code, char *s, int len) { int ret; - unsigned char* bak; - - bak=pkt->crt; - if ((ret=binrpc_addint(pkt, code))<0) + unsigned char *bak; + + bak = pkt->crt; + if((ret = binrpc_addint(pkt, code)) < 0) return ret; - ret=binrpc_addstr(pkt, s, len); - if (ret<0) - pkt->crt=bak; /* roll back */ + ret = binrpc_addstr(pkt, s, len); + if(ret < 0) + pkt->crt = bak; /* roll back */ return ret; } /* parsing incoming messages */ -static inline unsigned char* binrpc_read_llong( long long* i, - int len, - unsigned char* s, - unsigned char* end, - int *err - ) +static inline unsigned char *binrpc_read_llong( + long long *i, int len, unsigned char *s, unsigned char *end, int *err) { - unsigned char* start; + unsigned char *start; unsigned long long u; - start=s; - *i=0; + start = s; + *i = 0; u = 0; - *err=0; - for(;len>0; len--, s++){ - if (s>=end){ - *err=E_BINRPC_MORE_DATA; + *err = 0; + for(; len > 0; len--, s++) { + if(s >= end) { + *err = E_BINRPC_MORE_DATA; *i = (long long)u; return start; } - u<<=8; - u|=*s; + u <<= 8; + u |= *s; }; *i = (long long)u; return s; } - -static inline unsigned char* binrpc_read_int( int* i, - int len, - unsigned char* s, - unsigned char* end, - int *err - ) +static inline unsigned char *binrpc_read_int( + int *i, int len, unsigned char *s, unsigned char *end, int *err) { - unsigned char* start; + unsigned char *start; unsigned int u; - start=s; - *i=0; + start = s; + *i = 0; u = 0; - *err=0; - for(;len>0; len--, s++){ - if (s>=end){ - *err=E_BINRPC_MORE_DATA; + *err = 0; + for(; len > 0; len--, s++) { + if(s >= end) { + *err = E_BINRPC_MORE_DATA; *i = (int)u; return start; } - u<<=8; - u|=*s; + u <<= 8; + u |= *s; }; *i = (int)u; return s; } - /* initialize parsing context, it tries to read the whole message header, * if there is not enough data, sets *err to E_BINRPC_MORE_DATA. In this * case just redo the call when more data is available (len is bigger) @@ -623,72 +610,67 @@ static inline unsigned char* binrpc_read_int( int* i, * (=> you can discard the content between buf & the returned value). * On error buf is returned back, and *err set. */ -static inline unsigned char* binrpc_parse_init( struct binrpc_parse_ctx* ctx, - unsigned char* buf, - int len, - int *err - ) +static inline unsigned char *binrpc_parse_init( + struct binrpc_parse_ctx *ctx, unsigned char *buf, int len, int *err) { int len_len, c_len; unsigned char *p; - *err=0; - ctx->tlen=0; /* init to 0 */ - ctx->cookie=0; /* init to 0 */ - if (lentlen = 0; /* init to 0 */ + ctx->cookie = 0; /* init to 0 */ + if(len < BINRPC_MIN_PKT_SIZE) { + *err = E_BINRPC_MORE_DATA; goto error; } - if (buf[0]!=((BINRPC_MAGIC<<4)|BINRPC_VERS)){ - *err=E_BINRPC_BADPKT; + if(buf[0] != ((BINRPC_MAGIC << 4) | BINRPC_VERS)) { + *err = E_BINRPC_BADPKT; goto error; } - ctx->type=buf[1]>>4; + ctx->type = buf[1] >> 4; /* type check */ - switch(ctx->type){ + switch(ctx->type) { case BINRPC_REQ: case BINRPC_REPL: case BINRPC_FAULT: break; default: - *err=E_BINRPC_BADPKT; + *err = E_BINRPC_BADPKT; goto error; } - len_len=((buf[1]>>2) & 3) + 1; - c_len=(buf[1]&3) + 1; - if ((BINRPC_TLEN_OFFSET+len_len+c_len)>len){ - *err=E_BINRPC_MORE_DATA; + len_len = ((buf[1] >> 2) & 3) + 1; + c_len = (buf[1] & 3) + 1; + if((BINRPC_TLEN_OFFSET + len_len + c_len) > len) { + *err = E_BINRPC_MORE_DATA; goto error; } - p=binrpc_read_int((int*)&ctx->tlen, len_len, &buf[BINRPC_TLEN_OFFSET], - &buf[len], err); + p = binrpc_read_int((int *)&ctx->tlen, len_len, &buf[BINRPC_TLEN_OFFSET], + &buf[len], err); /* empty packets (replies) are allowed if (ctx->tlen==0){ *err=E_BINRPC_BADPKT; goto error; } */ - p=binrpc_read_int((int*)&ctx->cookie, c_len, p, &buf[len], err); - ctx->offset=0; - ctx->flags|=BINRPC_F_INIT; + p = binrpc_read_int((int *)&ctx->cookie, c_len, p, &buf[len], err); + ctx->offset = 0; + ctx->flags |= BINRPC_F_INIT; return p; error: return buf; } - /* returns bytes needed (till the end of the packet) * on error (non. init ctx) returns < 0 */ inline static int binrpc_bytes_needed(struct binrpc_parse_ctx *ctx) { - if (ctx->flags & BINRPC_F_INIT) - return ctx->tlen-ctx->offset; + if(ctx->flags & BINRPC_F_INIT) + return ctx->tlen - ctx->offset; return E_BINRPC_NOTINIT; } - /* prefill v with the requested type, if type==BINRPC_T_ALL it * will be replaced by the actual record type * known problems: no support for arrays inside STRUCT @@ -696,286 +678,281 @@ inline static int binrpc_bytes_needed(struct binrpc_parse_ctx *ctx) * not-strict-formatted rpc responses) * returns position after the record and *err==0 if successful * original position and *err<0 if not */ -inline static unsigned char* binrpc_read_record(struct binrpc_parse_ctx* ctx, - unsigned char* buf, - unsigned char* end, - struct binrpc_val* v, - int smode, - int* err - ) +inline static unsigned char *binrpc_read_record(struct binrpc_parse_ctx *ctx, + unsigned char *buf, unsigned char *end, struct binrpc_val *v, int smode, + int *err) { int type; int len; int end_tag; int tmp; - unsigned char* p; + unsigned char *p; long long ll; - p=buf; - end_tag=0; - *err=0; - if (!(ctx->flags & BINRPC_F_INIT)){ - *err=E_BINRPC_NOTINIT; + p = buf; + end_tag = 0; + *err = 0; + if(!(ctx->flags & BINRPC_F_INIT)) { + *err = E_BINRPC_NOTINIT; goto error; } - if (ctx->offset>=ctx->tlen){ - *err=E_BINRPC_EOP; + if(ctx->offset >= ctx->tlen) { + *err = E_BINRPC_EOP; goto error; } - if (p>=end){ - *err=E_BINRPC_MORE_DATA; + if(p >= end) { + *err = E_BINRPC_MORE_DATA; goto error; } /* read type_len */ - type=*p & 0xf; - len=*p>>4; + type = *p & 0xf; + len = *p >> 4; p++; - if ((type!=BINRPC_T_DOUBLE) && (len & 8)){ - end_tag=1; /* possible end mark for array or structs */ + if((type != BINRPC_T_DOUBLE) && (len & 8)) { + end_tag = 1; /* possible end mark for array or structs */ /* we have to read len bytes and use them as the new len */ - p=binrpc_read_int(&len, len&7, p, end, err); - if (*err<0) + p = binrpc_read_int(&len, len & 7, p, end, err); + if(*err < 0) goto error; } - if ((p+len)>end){ - *err=E_BINRPC_MORE_DATA; + if((p + len) > end) { + *err = E_BINRPC_MORE_DATA; goto error; } - if ((v->type!=type) && (v->type !=BINRPC_T_ALL)){ + if((v->type != type) && (v->type != BINRPC_T_ALL)) { goto error_type; } - v->type=type; - switch(type){ + v->type = type; + switch(type) { case BINRPC_T_STRUCT: - if (ctx->in_struct){ - if (end_tag){ + if(ctx->in_struct) { + if(end_tag) { ctx->in_struct--; - v->u.end=1; - }else{ - if(smode==0) { + v->u.end = 1; + } else { + if(smode == 0) { goto error_record; } else { - v->u.end=0; + v->u.end = 0; ctx->in_struct++; } } } else { - if (end_tag) + if(end_tag) goto error_record; - v->u.end=0; + v->u.end = 0; ctx->in_struct++; } break; case BINRPC_T_AVP: /* name | value */ - if (ctx->in_struct){ - v->name.s=(char*)p; - v->name.len=(len-1); /* don't include 0 term */ - p+=len; - if (p>=end){ - *err=E_BINRPC_MORE_DATA; + if(ctx->in_struct) { + v->name.s = (char *)p; + v->name.len = (len - 1); /* don't include 0 term */ + p += len; + if(p >= end) { + *err = E_BINRPC_MORE_DATA; goto error; } /* avp value type */ - type=*p & 0xf; - if ((type!=BINRPC_T_AVP) && (type!=BINRPC_T_ARRAY)){ - tmp=ctx->in_struct; - ctx->in_struct=0; /* hack to parse a normal record */ - v->type=type; /* hack */ - p=binrpc_read_record(ctx, p, end, v, smode, err); - if (*err<0){ - ctx->in_struct=tmp; + type = *p & 0xf; + if((type != BINRPC_T_AVP) && (type != BINRPC_T_ARRAY)) { + tmp = ctx->in_struct; + ctx->in_struct = 0; /* hack to parse a normal record */ + v->type = type; /* hack */ + p = binrpc_read_record(ctx, p, end, v, smode, err); + if(*err < 0) { + ctx->in_struct = tmp; goto error; - }else{ - ctx->in_struct+=tmp; + } else { + ctx->in_struct += tmp; /* the offset is already updated => skip */ goto no_offs_update; } - }else{ - goto error_record; + } else { + goto error_record; } } else { goto error_type; } break; case BINRPC_T_INT: - if (ctx->in_struct && smode==0) goto error_record; - p=binrpc_read_int(&v->u.intval, len, p, end, err); + if(ctx->in_struct && smode == 0) + goto error_record; + p = binrpc_read_int(&v->u.intval, len, p, end, err); break; case BINRPC_T_STR: - if (ctx->in_struct && smode==0) goto error_record; - v->u.strval.s=(char*)p; - v->u.strval.len=(len-1); /* don't include terminating 0 */ - p+=len; + if(ctx->in_struct && smode == 0) + goto error_record; + v->u.strval.s = (char *)p; + v->u.strval.len = (len - 1); /* don't include terminating 0 */ + p += len; break; case BINRPC_T_BYTES: - if (ctx->in_struct && smode==0) goto error_record; - v->u.strval.s=(char*)p; - v->u.strval.len=len; - p+=len; + if(ctx->in_struct && smode == 0) + goto error_record; + v->u.strval.s = (char *)p; + v->u.strval.len = len; + p += len; break; case BINRPC_T_ARRAY: - if (ctx->in_struct && smode==0) goto error_record; - if (end_tag){ - if (ctx->in_array>0){ + if(ctx->in_struct && smode == 0) + goto error_record; + if(end_tag) { + if(ctx->in_array > 0) { ctx->in_array--; - v->u.end=1; - }else{ + v->u.end = 1; + } else { goto error_record; } - }else{ + } else { ctx->in_array++; - v->u.end=0; + v->u.end = 0; } break; case BINRPC_T_DOUBLE: /* FIXME: hack: represented as fixed point inside a long long */ - if (ctx->in_struct && smode==0) goto error_record; - p=binrpc_read_llong(&ll, len, p, end, err); - v->u.fval=((double)ll)/1000; + if(ctx->in_struct && smode == 0) + goto error_record; + p = binrpc_read_llong(&ll, len, p, end, err); + v->u.fval = ((double)ll) / 1000; break; default: - if (ctx->in_struct){ + if(ctx->in_struct) { goto error_record; } else { goto error_type; } } - ctx->offset+=(int)(p-buf); + ctx->offset += (int)(p - buf); no_offs_update: return p; error_type: - *err=E_BINRPC_TYPE; + *err = E_BINRPC_TYPE; return buf; error_record: - *err=E_BINRPC_RECORD; + *err = E_BINRPC_RECORD; error: return buf; } - /* reads/skips an entire struct * the struct start/end are saved in v->u.strval.s, v->u.strval.len * return: - new buffer position and set *err to 0 if successfull * - original buffer and *err<0 on error */ -inline static unsigned char* binrpc_read_struct(struct binrpc_parse_ctx* ctx, - unsigned char* buf, - unsigned char* end, - struct binrpc_val* v, - int* err - ) +inline static unsigned char *binrpc_read_struct(struct binrpc_parse_ctx *ctx, + unsigned char *buf, unsigned char *end, struct binrpc_val *v, int *err) { int type; int len; int end_tag; - unsigned char* p; + unsigned char *p; int in_struct; - - *err=0; - p=buf; - end_tag=0; - if (!(ctx->flags & BINRPC_F_INIT)){ - *err=E_BINRPC_NOTINIT; + + *err = 0; + p = buf; + end_tag = 0; + if(!(ctx->flags & BINRPC_F_INIT)) { + *err = E_BINRPC_NOTINIT; goto error; } - if (ctx->offset>=ctx->tlen){ - *err=E_BINRPC_EOP; + if(ctx->offset >= ctx->tlen) { + *err = E_BINRPC_EOP; goto error; } - if (p>=end){ - *err=E_BINRPC_MORE_DATA; + if(p >= end) { + *err = E_BINRPC_MORE_DATA; goto error; } /* read type_len */ - type=*p & 0xf; - len=*p>>4; + type = *p & 0xf; + len = *p >> 4; p++; - if (len & 8){ - end_tag=1; /* possible end mark for array or structs */ + if(len & 8) { + end_tag = 1; /* possible end mark for array or structs */ /* we have to read len bytes and use them as the new len */ - p=binrpc_read_int(&len, len&7, p, end, err); - if (*err<0) + p = binrpc_read_int(&len, len & 7, p, end, err); + if(*err < 0) goto error; } - if ((p+len)>=end){ - *err=E_BINRPC_MORE_DATA; + if((p + len) >= end) { + *err = E_BINRPC_MORE_DATA; goto error; } - if (type!=BINRPC_T_STRUCT){ + if(type != BINRPC_T_STRUCT) { goto error_type; } - if (end_tag){ + if(end_tag) { goto error_record; } - p+=len; /* len should be 0 for a struct tag */ - in_struct=1; - v->type=type; - v->u.strval.s=(char*)p; /* it will contain the inside of the struc */ - while(in_struct){ + p += len; /* len should be 0 for a struct tag */ + in_struct = 1; + v->type = type; + v->u.strval.s = (char *)p; /* it will contain the inside of the struc */ + while(in_struct) { /* read name */ - type=*p & 0xf; - len=*p>>4; + type = *p & 0xf; + len = *p >> 4; p++; - if (len & 8){ - end_tag=1; /* possible end mark for array or structs */ + if(len & 8) { + end_tag = 1; /* possible end mark for array or structs */ /* we have to read len bytes and use them as the new len */ - p=binrpc_read_int(&len, len&7, p, end, err); - if (*err<0) + p = binrpc_read_int(&len, len & 7, p, end, err); + if(*err < 0) goto error; } - if ((type==BINRPC_T_STRUCT) && end_tag){ + if((type == BINRPC_T_STRUCT) && end_tag) { in_struct--; - if (in_struct<0) + if(in_struct < 0) goto error_record; continue; - }else if (type!=BINRPC_T_AVP){ + } else if(type != BINRPC_T_AVP) { goto error_record; } /* skip over it */ - p+=len; - if (p>=end){ - *err=E_BINRPC_MORE_DATA; + p += len; + if(p >= end) { + *err = E_BINRPC_MORE_DATA; goto error; } /* read value */ - type=*p & 0xf; - len=*p>>4; + type = *p & 0xf; + len = *p >> 4; p++; - if (len & 8){ - end_tag=1; /* possible end mark for array or structs */ + if(len & 8) { + end_tag = 1; /* possible end mark for array or structs */ /* we have to read len bytes and use them as the new len */ - p=binrpc_read_int(&len, len&7, p, end, err); - if (*err<0) + p = binrpc_read_int(&len, len & 7, p, end, err); + if(*err < 0) goto error; } - if (type==BINRPC_T_STRUCT){ - if (end_tag) + if(type == BINRPC_T_STRUCT) { + if(end_tag) goto error_record; in_struct++; }; - p+=len; - if (p>=end){ - *err=E_BINRPC_MORE_DATA; + p += len; + if(p >= end) { + *err = E_BINRPC_MORE_DATA; goto error; } } /* don't include the end tag */; - v->u.strval.len=(int)(p-(unsigned char*)v->u.strval.s)-1; + v->u.strval.len = (int)(p - (unsigned char *)v->u.strval.s) - 1; return p; - + error_type: - *err=E_BINRPC_RECORD; + *err = E_BINRPC_RECORD; return buf; error_record: - *err=E_BINRPC_TYPE; + *err = E_BINRPC_TYPE; error: return buf; } - /* error code to string */ -const char* binrpc_error(int err); +const char *binrpc_error(int err); #endif diff --git a/src/modules/ctl/binrpc_run.c b/src/modules/ctl/binrpc_run.c index 3265a63c2ab..86606136f15 100644 --- a/src/modules/ctl/binrpc_run.c +++ b/src/modules/ctl/binrpc_run.c @@ -29,94 +29,101 @@ #include "io_listener.h" #include "ctl.h" -#include /* vsnprintf */ +#include /* vsnprintf */ #include /* strtod */ #include -#define DEFAULT_RPC_PRINTF_BUF_SIZE 1024 +#define DEFAULT_RPC_PRINTF_BUF_SIZE 1024 /* if set try to automatically convert values to the requested type in rpc->scan (default: not set) */ -int autoconvert=0; +int autoconvert = 0; -int binrpc_max_body_size = 32; /* multiplied by 1024 in mod init */ +int binrpc_max_body_size = 32; /* multiplied by 1024 in mod init */ int binrpc_struct_max_body_size = 8; /* multiplied by 1024 in mod init */ int binrpc_buffer_size = DEFAULT_RPC_PRINTF_BUF_SIZE; -#define BINRPC_MAX_BODY binrpc_max_body_size /* maximum body for send */ -#define STRUCT_MAX_BODY binrpc_struct_max_body_size -#define MAX_MSG_CHUNKS 96 +#define BINRPC_MAX_BODY binrpc_max_body_size /* maximum body for send */ +#define STRUCT_MAX_BODY binrpc_struct_max_body_size +#define MAX_MSG_CHUNKS 96 #define BINRPC_GC_IBSIZE 4 /* initial gc block size (pointers no.) */ -struct rpc_struct_head{ - struct rpc_struct_l* next; - struct rpc_struct_l* prev; +struct rpc_struct_head +{ + struct rpc_struct_l *next; + struct rpc_struct_l *prev; }; -struct rpc_struct_l{ - struct rpc_struct_l* next; - struct rpc_struct_l* prev; +struct rpc_struct_l +{ + struct rpc_struct_l *next; + struct rpc_struct_l *prev; struct binrpc_pkt pkt; struct rpc_struct_head substructs; /* head */ - int offset; /* byte offset in parent's pkt */ + int offset; /* byte offset in parent's pkt */ }; -struct binrpc_send_ctx{ - struct binrpc_pkt pkt; /* body */ +struct binrpc_send_ctx +{ + struct binrpc_pkt pkt; /* body */ struct rpc_struct_head structs; /* list head */ }; -struct binrpc_recv_ctx{ +struct binrpc_recv_ctx +{ struct binrpc_parse_ctx ctx; - unsigned char* s; /* current position in buffer */ - unsigned char* end; + unsigned char *s; /* current position in buffer */ + unsigned char *end; int record_no; int in_struct; }; -struct binrpc_gc_block{ +struct binrpc_gc_block +{ unsigned short p_no; /**< array size */ - unsigned short idx; /**< current/last used pos. in the array */ - struct binrpc_gc_block* next; - void* p[1]; /**< array of pointers that will be free'd */ + unsigned short idx; /**< current/last used pos. in the array */ + struct binrpc_gc_block *next; + void *p[1]; /**< array of pointers that will be free'd */ }; -struct binrpc_ctx{ +struct binrpc_ctx +{ struct binrpc_recv_ctx in; struct binrpc_send_ctx out; - void* send_h; /* send handle */ - char* method; - struct binrpc_gc_block* gc; /**< garbage collection */ + void *send_h; /* send handle */ + char *method; + struct binrpc_gc_block *gc; /**< garbage collection */ int replied; int err_code; - str err_phrase; /**< Leading zero must be included! */ + str err_phrase; /**< Leading zero must be included! */ }; -struct iovec_array{ - struct iovec* v; +struct iovec_array +{ + struct iovec *v; int idx; int len; void *ctx; }; /* send */ -static void rpc_fault(struct binrpc_ctx* ctx, int code, char* fmt, ...); -static int rpc_send(struct binrpc_ctx* ctx); +static void rpc_fault(struct binrpc_ctx *ctx, int code, char *fmt, ...); +static int rpc_send(struct binrpc_ctx *ctx); static int rpc_send_v(struct iovec_array *a); -static int rpc_add(struct binrpc_ctx* ctx, char* fmt, ...); -static int rpc_scan(struct binrpc_ctx* ctx, char* fmt, ...); -static int rpc_rpl_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, ...); +static int rpc_add(struct binrpc_ctx *ctx, char *fmt, ...); +static int rpc_scan(struct binrpc_ctx *ctx, char *fmt, ...); +static int rpc_rpl_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 int rpc_struct_printf( + struct rpc_struct_l *s, char *name, char *fmt, ...); static rpc_t binrpc_callbacks; @@ -124,75 +131,75 @@ 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.rpl_printf = (rpc_rpl_printf_f)rpc_rpl_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.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.rpl_printf = (rpc_rpl_printf_f)rpc_rpl_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 */ -inline static int binrpc_gc_track(struct binrpc_ctx* ctx, void* p) +inline static int binrpc_gc_track(struct binrpc_ctx *ctx, void *p) { - struct binrpc_gc_block* b; + struct binrpc_gc_block *b; int n; - - b=ctx->gc; - if (b==0 || (b->idx>=b->p_no)){ - n=(b==0)?BINRPC_GC_IBSIZE:b->p_no*2; - b=ctl_malloc(sizeof(*b)+n*sizeof(void*)-sizeof(b->p)); - if (b==0) + + b = ctx->gc; + if(b == 0 || (b->idx >= b->p_no)) { + n = (b == 0) ? BINRPC_GC_IBSIZE : b->p_no * 2; + b = ctl_malloc(sizeof(*b) + n * sizeof(void *) - sizeof(b->p)); + if(b == 0) return -1; - b->p_no=n; - b->idx=0; + b->p_no = n; + b->idx = 0; /* link in front */ - b->next=ctx->gc; - ctx->gc=b; + b->next = ctx->gc; + ctx->gc = b; } - b->p[b->idx]=p; + b->p[b->idx] = p; b->idx++; return 0; } - /** free all the tracked pointer from ctx->gc. */ -inline static void binrpc_gc_collect(struct binrpc_ctx* ctx) +inline static void binrpc_gc_collect(struct binrpc_ctx *ctx) { - struct binrpc_gc_block* b; - struct binrpc_gc_block* next; + struct binrpc_gc_block *b; + struct binrpc_gc_block *next; int i; - - for(b=ctx->gc; b; b=next){ - next=b->next; - for (i=0; iidx; i++) + + for(b = ctx->gc; b; b = next) { + next = b->next; + for(i = 0; i < b->idx; i++) ctl_free(b->p[i]); ctl_free(b); } - ctx->gc=0; + ctx->gc = 0; } -static struct rpc_struct_l* new_rpc_struct() +static struct rpc_struct_l *new_rpc_struct() { - struct rpc_struct_l* rs; - + struct rpc_struct_l *rs; + /* alloc everything in one chunk */ - rs=ctl_malloc(sizeof(struct rpc_struct_l)+STRUCT_MAX_BODY); - if (rs==0) + rs = ctl_malloc(sizeof(struct rpc_struct_l) + STRUCT_MAX_BODY); + if(rs == 0) goto error; memset(rs, 0, sizeof(struct rpc_struct_l)); clist_init(&rs->substructs, next, prev); - if (binrpc_init_pkt(&rs->pkt, - (unsigned char*)rs+sizeof(struct rpc_struct_l), - STRUCT_MAX_BODY)<0){ + if(binrpc_init_pkt(&rs->pkt, + (unsigned char *)rs + sizeof(struct rpc_struct_l), + STRUCT_MAX_BODY) + < 0) { ctl_free(rs); goto error; } @@ -202,7 +209,6 @@ static struct rpc_struct_l* new_rpc_struct() } - #if 0 /* not used yet */ /* doubles the size */ static struct rpc_struct_l* grow_rpc_struct(struct rpc_struct_l *rs) @@ -251,63 +257,61 @@ inline static int append_pkt_body(struct binrpc_pkt* p, unsigned char* buf, #endif -inline static int append_iovec(struct iovec_array* a, unsigned char* buf, - int len) +inline static int append_iovec( + struct iovec_array *a, unsigned char *buf, int len) { int ret; - if (a->idx >= a->len) { + if(a->idx >= a->len) { ret = rpc_send_v(a); - if (ret < 0) + if(ret < 0) return ret; } - a->v[a->idx].iov_base=buf; - a->v[a->idx].iov_len=len; + a->v[a->idx].iov_base = buf; + a->v[a->idx].iov_len = len; a->idx++; return 0; } - -static int body_get_len(struct binrpc_pkt* body, - struct rpc_struct_head* sl_head) +static int body_get_len( + struct binrpc_pkt *body, struct rpc_struct_head *sl_head) { - struct rpc_struct_l* l; + struct rpc_struct_l *l; int len; - - len=binrpc_pkt_len(body); - clist_foreach(sl_head, l, next){ - len+=body_get_len(&l->pkt, &l->substructs); + + len = binrpc_pkt_len(body); + clist_foreach(sl_head, l, next) + { + len += body_get_len(&l->pkt, &l->substructs); } return len; } - -static int body_fill_iovec(struct iovec_array* v_a, - struct binrpc_pkt* body, - struct rpc_struct_head* sl_head) +static int body_fill_iovec(struct iovec_array *v_a, struct binrpc_pkt *body, + struct rpc_struct_head *sl_head) { int offs; - struct rpc_struct_l* l; + struct rpc_struct_l *l; int ret; - - offs=0; - clist_foreach(sl_head, l, next){ - if ((ret=append_iovec(v_a, body->body+offs, l->offset-offs))<0) + + offs = 0; + clist_foreach(sl_head, l, next) + { + if((ret = append_iovec(v_a, body->body + offs, l->offset - offs)) < 0) goto error; - offs=l->offset; - if ((ret=body_fill_iovec(v_a, &l->pkt, &l->substructs))<0) + offs = l->offset; + if((ret = body_fill_iovec(v_a, &l->pkt, &l->substructs)) < 0) goto error; }; /* copy the rest */ - ret=append_iovec(v_a, body->body+offs, binrpc_pkt_len(body)-offs); + ret = append_iovec(v_a, body->body + offs, binrpc_pkt_len(body) - offs); error: return ret; } - #if 0 /* expects an initialized new_b */ static int build_structs(struct binrpc_pkt *new_b, struct binrpc_pkt* body, @@ -333,13 +337,13 @@ static int build_structs(struct binrpc_pkt *new_b, struct binrpc_pkt* body, #endif - -static void free_structs(struct rpc_struct_head* sl_head) +static void free_structs(struct rpc_struct_head *sl_head) { - struct rpc_struct_l* l; - struct rpc_struct_l* tmp; - - clist_foreach_safe(sl_head, l, tmp, next){ + struct rpc_struct_l *l; + struct rpc_struct_l *tmp; + + clist_foreach_safe(sl_head, l, tmp, next) + { free_structs(&l->substructs); memset(l, 0, sizeof(struct rpc_struct_l)); /* debugging */ ctl_free(l); @@ -347,40 +351,37 @@ static void free_structs(struct rpc_struct_head* sl_head) } - -inline static int init_binrpc_ctx( struct binrpc_ctx* ctx, - unsigned char* recv_buf, - int recv_buf_len, - void* send_handle - ) +inline static int init_binrpc_ctx(struct binrpc_ctx *ctx, + unsigned char *recv_buf, int recv_buf_len, void *send_handle) { int err; - unsigned char* send_buf; + unsigned char *send_buf; int send_buf_len; - + memset(ctx, 0, sizeof(struct binrpc_ctx)); clist_init(&ctx->out.structs, next, prev); - ctx->send_h=send_handle; - ctx->in.end=recv_buf+recv_buf_len; - ctx->in.s=binrpc_parse_init(&ctx->in.ctx, recv_buf, recv_buf_len, &err); - if (err<0) goto end; - if ((ctx->in.ctx.tlen+(int)(ctx->in.s-recv_buf))>recv_buf_len){ - err=E_BINRPC_MORE_DATA; + ctx->send_h = send_handle; + ctx->in.end = recv_buf + recv_buf_len; + ctx->in.s = binrpc_parse_init(&ctx->in.ctx, recv_buf, recv_buf_len, &err); + if(err < 0) + goto end; + if((ctx->in.ctx.tlen + (int)(ctx->in.s - recv_buf)) > recv_buf_len) { + err = E_BINRPC_MORE_DATA; goto end; } /* fix end value */ - ctx->in.end=ctx->in.s+ctx->in.ctx.tlen; - + ctx->in.end = ctx->in.s + ctx->in.ctx.tlen; + /* alloc temporary body buffer */ - send_buf_len=BINRPC_MAX_BODY; - send_buf=ctl_malloc(send_buf_len); - if (send_buf==0){ - err=E_BINRPC_LAST; + send_buf_len = BINRPC_MAX_BODY; + send_buf = ctl_malloc(send_buf_len); + if(send_buf == 0) { + err = E_BINRPC_LAST; goto end; } /* we'll keep only the body */ - err=binrpc_init_pkt(&ctx->out.pkt, send_buf, send_buf_len); - if(err!=0) { + err = binrpc_init_pkt(&ctx->out.pkt, send_buf, send_buf_len); + if(err != 0) { ctl_free(send_buf); } end: @@ -388,27 +389,25 @@ inline static int init_binrpc_ctx( struct binrpc_ctx* ctx, } - -static inline void destroy_binrpc_ctx(struct binrpc_ctx* ctx) +static inline void destroy_binrpc_ctx(struct binrpc_ctx *ctx) { free_structs(&ctx->out.structs); - if (ctx->out.pkt.body){ + if(ctx->out.pkt.body) { ctl_free(ctx->out.pkt.body); - ctx->out.pkt.body=0; + ctx->out.pkt.body = 0; } - if (ctx->err_phrase.s){ + if(ctx->err_phrase.s) { ctl_free(ctx->err_phrase.s); - ctx->err_phrase.s=NULL; + ctx->err_phrase.s = NULL; } binrpc_gc_collect(ctx); } - #define MAX_FAULT_LEN 256 -#define FAULT_START_BUF (3 /* maxint*/+2/*max str header*/) -static void _rpc_fault(struct binrpc_ctx* ctx, int code, - char *phrase, int phrase_len) +#define FAULT_START_BUF (3 /* maxint*/ + 2 /*max str header*/) +static void _rpc_fault( + struct binrpc_ctx *ctx, int code, char *phrase, int phrase_len) { static unsigned char fault_start[FAULT_START_BUF]; static unsigned char hdr[BINRPC_MAX_HDR_SIZE]; @@ -418,25 +417,27 @@ static void _rpc_fault(struct binrpc_ctx* ctx, int code, int hdr_len; int err; - if (ctx->replied){ - LOG(L_ERR, "ERROR: binrpc: rpc_send: rpc method %s tried to reply" - " more than once\n", ctx->method?ctx->method:""); + if(ctx->replied) { + LOG(L_ERR, + "ERROR: binrpc: rpc_send: rpc method %s tried to reply" + " more than once\n", + ctx->method ? ctx->method : ""); return; } - err=0; - err=binrpc_init_pkt(&body, fault_start, FAULT_START_BUF); - if (err<0){ + err = 0; + err = binrpc_init_pkt(&body, fault_start, FAULT_START_BUF); + if(err < 0) { LOG(L_ERR, "ERROR: binrpc_init_pkt error\n"); goto error; } /* adding a fault "manually" to avoid extra memcpys */ - err=binrpc_addint(&body, code); - if (err<0){ + err = binrpc_addint(&body, code); + if(err < 0) { LOG(L_ERR, "ERROR: _rpc_fault: addint error\n"); goto error; } - err=binrpc_add_str_mark(&body, BINRPC_T_STR, phrase_len); - if (err<0){ + err = binrpc_add_str_mark(&body, BINRPC_T_STR, phrase_len); + if(err < 0) { LOG(L_ERR, "ERROR: _rpc_fault: add_str_mark error\n"); goto error; } @@ -446,50 +447,52 @@ static void _rpc_fault(struct binrpc_ctx* ctx, int code, LOG(L_ERR, "ERROR: binrpc_addfault error\n"); goto error; }*/ - b_len=binrpc_pkt_len(&body); - err=hdr_len=binrpc_build_hdr(BINRPC_FAULT, b_len+phrase_len, - ctx->in.ctx.cookie, hdr, BINRPC_MAX_HDR_SIZE); - if (err<0){ + b_len = binrpc_pkt_len(&body); + err = hdr_len = binrpc_build_hdr(BINRPC_FAULT, b_len + phrase_len, + ctx->in.ctx.cookie, hdr, BINRPC_MAX_HDR_SIZE); + if(err < 0) { LOG(L_ERR, "ERROR: binrpc_build_hdr error\n"); goto error; } - v[0].iov_base=hdr; - v[0].iov_len=hdr_len; - v[1].iov_base=body.body; - v[1].iov_len=b_len; - v[2].iov_base=phrase; - v[2].iov_len=phrase_len; - if ((err=sock_send_v(ctx->send_h, v, 3))<0){ - if (err==-2){ + v[0].iov_base = hdr; + v[0].iov_len = hdr_len; + v[1].iov_base = body.body; + v[1].iov_len = b_len; + v[2].iov_base = phrase; + v[2].iov_len = phrase_len; + if((err = sock_send_v(ctx->send_h, v, 3)) < 0) { + if(err == -2) { LOG(L_ERR, "ERROR: _rpc_fault: send failed: " - "datagram too big\n"); + "datagram too big\n"); return; } LOG(L_ERR, "ERROR: _rpc_fault: send failed\n"); return; } - ctx->replied=1; + ctx->replied = 1; return; error: LOG(L_ERR, "ERROR: _rpc_fault: binrpc_* failed with: %s (%d)\n", binrpc_error(err), err); } -static void rpc_fault(struct binrpc_ctx* ctx, int code, char* fmt, ...) +static void rpc_fault(struct binrpc_ctx *ctx, int code, char *fmt, ...) { char buf[MAX_FAULT_LEN]; va_list ap; int len; - if (ctx->replied){ - LOG(L_ERR, "ERROR: binrpc: rpc_send: rpc method %s tried to reply" - " more than once\n", ctx->method?ctx->method:""); + if(ctx->replied) { + LOG(L_ERR, + "ERROR: binrpc: rpc_send: rpc method %s tried to reply" + " more than once\n", + ctx->method ? ctx->method : ""); return; } va_start(ap, fmt); - len=vsnprintf(buf, MAX_FAULT_LEN, fmt, ap); /* ignore trunc. errors */ - if ((len<0) || (len > MAX_FAULT_LEN)) - len=MAX_FAULT_LEN-1; + len = vsnprintf(buf, MAX_FAULT_LEN, fmt, ap); /* ignore trunc. errors */ + if((len < 0) || (len > MAX_FAULT_LEN)) + len = MAX_FAULT_LEN - 1; va_end(ap); len++; /* vnsprintf doesn't include the terminating 0 */ @@ -497,30 +500,32 @@ static void rpc_fault(struct binrpc_ctx* ctx, int code, char* fmt, ...) } /* Prepare the error reply without sending out the message */ -static int rpc_fault_prepare(struct binrpc_ctx* ctx, int code, char* fmt, ...) +static int rpc_fault_prepare(struct binrpc_ctx *ctx, int code, char *fmt, ...) { char buf[MAX_FAULT_LEN]; va_list ap; int len; - if (ctx->replied){ - LOG(L_ERR, "ERROR: binrpc: rpc_send: rpc method %s tried to reply" - " more than once\n", ctx->method?ctx->method:""); + if(ctx->replied) { + LOG(L_ERR, + "ERROR: binrpc: rpc_send: rpc method %s tried to reply" + " more than once\n", + ctx->method ? ctx->method : ""); return -1; } va_start(ap, fmt); - len=vsnprintf(buf, MAX_FAULT_LEN, fmt, ap); /* ignore trunc. errors */ - if ((len<0) || (len >= MAX_FAULT_LEN)) - len=MAX_FAULT_LEN-1; + len = vsnprintf(buf, MAX_FAULT_LEN, fmt, ap); /* ignore trunc. errors */ + if((len < 0) || (len >= MAX_FAULT_LEN)) + len = MAX_FAULT_LEN - 1; va_end(ap); len++; /* vnsprintf doesn't include the terminating 0 */ ctx->err_code = code; - if (ctx->err_phrase.s) + if(ctx->err_phrase.s) ctl_free(ctx->err_phrase.s); - ctx->err_phrase.s = (char*)ctl_malloc(sizeof(char)*len); - if (!ctx->err_phrase.s) { + ctx->err_phrase.s = (char *)ctl_malloc(sizeof(char) * len); + if(!ctx->err_phrase.s) { ctx->err_code = 0; ctx->err_phrase.len = 0; LOG(L_ERR, "ERROR: rpc_fault_prepare: not enough memory\n"); @@ -532,10 +537,10 @@ static int rpc_fault_prepare(struct binrpc_ctx* ctx, int code, char* fmt, ...) } /* Reset the saved error code */ -static void rpc_fault_reset(struct binrpc_ctx* ctx) +static void rpc_fault_reset(struct binrpc_ctx *ctx) { ctx->err_code = 0; - if (ctx->err_phrase.s) { + if(ctx->err_phrase.s) { ctl_free(ctx->err_phrase.s); ctx->err_phrase.s = NULL; ctx->err_phrase.len = 0; @@ -547,11 +552,11 @@ static int rpc_send_v(struct iovec_array *a) { int ret; - if (a->idx <= 0) + if(a->idx <= 0) return 0; ret = sock_send_v(a->ctx, a->v, a->idx); - if (ret < 0) + if(ret < 0) return ret; a->idx = 0; @@ -559,7 +564,7 @@ static int rpc_send_v(struct iovec_array *a) } /* build the reply from the current body */ -static int rpc_send(struct binrpc_ctx* ctx) +static int rpc_send(struct binrpc_ctx *ctx) { int b_len; int hdr_len; @@ -567,51 +572,54 @@ static int rpc_send(struct binrpc_ctx* ctx) struct iovec_array a; static unsigned char hdr[BINRPC_MAX_HDR_SIZE]; int err; - - err=0; - a.v=v; - a.idx=1; - a.len=MAX_MSG_CHUNKS; + + err = 0; + a.v = v; + a.idx = 1; + a.len = MAX_MSG_CHUNKS; a.ctx = ctx->send_h; - - if (ctx->replied){ - LOG(L_ERR, "ERROR: binrpc: rpc_send: rpc method %s tried to reply" - " more than once\n", ctx->method?ctx->method:""); + + if(ctx->replied) { + LOG(L_ERR, + "ERROR: binrpc: rpc_send: rpc method %s tried to reply" + " more than once\n", + ctx->method ? ctx->method : ""); goto error; } - b_len=body_get_len(&ctx->out.pkt, &ctx->out.structs); - err=hdr_len=binrpc_build_hdr( BINRPC_REPL, b_len, ctx->in.ctx.cookie, - hdr, BINRPC_MAX_HDR_SIZE); - if (err<0){ - LOG(L_ERR, "ERROR: binrpc: rpc_fault: binrpc_* failed with:" - " %s (%d)\n", binrpc_error(err), err); + b_len = body_get_len(&ctx->out.pkt, &ctx->out.structs); + err = hdr_len = binrpc_build_hdr( + BINRPC_REPL, b_len, ctx->in.ctx.cookie, hdr, BINRPC_MAX_HDR_SIZE); + if(err < 0) { + LOG(L_ERR, + "ERROR: binrpc: rpc_fault: binrpc_* failed with:" + " %s (%d)\n", + binrpc_error(err), err); goto error; } - v[0].iov_base=hdr; - v[0].iov_len=hdr_len; + v[0].iov_base = hdr; + v[0].iov_len = hdr_len; /* fill the rest of the iovecs */ - err=body_fill_iovec(&a, &ctx->out.pkt, &ctx->out.structs); - if (err<0){ + err = body_fill_iovec(&a, &ctx->out.pkt, &ctx->out.structs); + if(err < 0) { LOG(L_ERR, "ERROR: binrprc: rpc_send: too many message chunks\n"); goto error; } - if ((err = rpc_send_v(&a)) < 0){ - if (err==-2){ + if((err = rpc_send_v(&a)) < 0) { + if(err == -2) { LOG(L_ERR, "ERROR: binrpc: rpc_send: send failed: " - "datagram too big\n"); + "datagram too big\n"); goto error; } LOG(L_ERR, "ERROR: binrprc: rpc_send: send failed\n"); goto error; } - ctx->replied=1; + ctx->replied = 1; return 0; error: return -1; } - /* params: buf, size - buffer containing the packet * bytes_needed - int pointer, filled with how many bytes are still * needed (after bytes_needed new bytes received this @@ -623,14 +631,14 @@ static int rpc_send(struct binrpc_ctx* ctx) * returns: number of bytes processed on success/partial success * -1 on error */ -int process_rpc_req(unsigned char* buf, int size, int* bytes_needed, - void* sh, void** saved_state) +int process_rpc_req(unsigned char *buf, int size, int *bytes_needed, void *sh, + void **saved_state) { int err; struct binrpc_val val; - rpc_exportx_t* rpc_e; + rpc_exportx_t *rpc_e; struct binrpc_ctx f_ctx; - struct binrpc_parse_ctx* ctx; + struct binrpc_parse_ctx *ctx; unsigned int rdata; if(ksr_shutdown_phase()) { @@ -639,22 +647,22 @@ int process_rpc_req(unsigned char* buf, int size, int* bytes_needed, return -1; } - if (sizetlen+(int)(f_ctx.in.s-buf)-size; - }else{ - *bytes_needed=1; /* we don't really know how much */ + err = init_binrpc_ctx(&f_ctx, buf, size, sh); + ctx = &f_ctx.in.ctx; + if(err < 0) { + if(err == E_BINRPC_MORE_DATA) { + if(f_ctx.in.ctx.tlen) { + *bytes_needed = ctx->tlen + (int)(f_ctx.in.s - buf) - size; + } else { + *bytes_needed = 1; /* we don't really know how much */ } goto more_data; - }else if( err==E_BINRPC_LAST){ + } else if(err == E_BINRPC_LAST) { LOG(L_ERR, "ERROR: init_binrpc_ctx: out of memory\n"); rpc_fault(&f_ctx, 500, "internal server error: out of mem."); goto error; @@ -662,43 +670,43 @@ int process_rpc_req(unsigned char* buf, int size, int* bytes_needed, rpc_fault(&f_ctx, 400, "bad request: %s", binrpc_error(err)); goto error; } - err=E_BINRPC_BADPKT; - if (ctx->type!=BINRPC_REQ){ + err = E_BINRPC_BADPKT; + if(ctx->type != BINRPC_REQ) { rpc_fault(&f_ctx, 400, "bad request: %s", binrpc_error(err)); goto error; } /* now we have the entire packet */ /* get rpc method */ - val.type=BINRPC_T_STR; - f_ctx.in.s=binrpc_read_record(ctx, f_ctx.in.s, f_ctx.in.end, &val, 0, &err); - if (err<0){ + val.type = BINRPC_T_STR; + f_ctx.in.s = + binrpc_read_record(ctx, f_ctx.in.s, f_ctx.in.end, &val, 0, &err); + if(err < 0) { LOG(L_CRIT, "ERROR: bad rpc request method, binrpc error: %s (%d)\n", binrpc_error(err), err); - rpc_fault(&f_ctx, 400, "bad request method: %s", binrpc_error(err) ); + rpc_fault(&f_ctx, 400, "bad request method: %s", binrpc_error(err)); goto error; } rpc_e = rpc_lookupx(val.u.strval.s, val.u.strval.len, &rdata); - if ((rpc_e==NULL) || (rpc_e->r.function==NULL)){ + if((rpc_e == NULL) || (rpc_e->r.function == NULL)) { rpc_fault(&f_ctx, 500, "command %s not found", val.u.strval.s); goto end; } - if (rdata & RPC_EXEC_DELTA) { + if(rdata & RPC_EXEC_DELTA) { LM_ERR("execution of command [%.*s] is limited by delta [%d]\n", val.u.strval.len, val.u.strval.s, ksr_rpc_exec_delta); rpc_fault(&f_ctx, 500, "Command Executed Too Fast"); goto end; } - f_ctx.method=val.u.strval.s; + f_ctx.method = val.u.strval.s; rpc_e->r.function(&binrpc_callbacks, &f_ctx); - if (f_ctx.replied==0){ - if ((binrpc_pkt_len(&f_ctx.out.pkt)==0) - && f_ctx.err_code && f_ctx.err_phrase.s - ) { - _rpc_fault(&f_ctx, f_ctx.err_code, - f_ctx.err_phrase.s, f_ctx.err_phrase.len); - /* to get an error reply if the rpc handlers hasn't replied + if(f_ctx.replied == 0) { + if((binrpc_pkt_len(&f_ctx.out.pkt) == 0) && f_ctx.err_code + && f_ctx.err_phrase.s) { + _rpc_fault(&f_ctx, f_ctx.err_code, f_ctx.err_phrase.s, + f_ctx.err_phrase.len); + /* to get an error reply if the rpc handlers hasn't replied * uncomment the following code: * } else if (binrpc_pkt_len(&f_ctx.out.pkt)==0){ rpc_fault(&f_ctx, 500, "internal server error: no reply"); @@ -710,15 +718,15 @@ int process_rpc_req(unsigned char* buf, int size, int* bytes_needed, } } end: - *bytes_needed=0; /* full read */ + *bytes_needed = 0; /* full read */ destroy_binrpc_ctx(&f_ctx); - return (int)(f_ctx.in.end-buf); + return (int)(f_ctx.in.end - buf); error: - if (f_ctx.replied==0){ - rpc_fault(&f_ctx, 500, "internal server error"); - LOG(L_ERR, "ERROR: unknown rpc error\n"); + if(f_ctx.replied == 0) { + rpc_fault(&f_ctx, 500, "internal server error"); + LOG(L_ERR, "ERROR: unknown rpc error\n"); } - *bytes_needed=0; /* we don't need anymore crap */ + *bytes_needed = 0; /* we don't need anymore crap */ destroy_binrpc_ctx(&f_ctx); return -1; more_data: @@ -727,11 +735,9 @@ int process_rpc_req(unsigned char* buf, int size, int* bytes_needed, } - - -static char* rpc_type_name(int type) +static char *rpc_type_name(int type) { - switch(type){ + switch(type) { case BINRPC_T_INT: return "integer"; case BINRPC_T_STR: @@ -753,206 +759,212 @@ static char* rpc_type_name(int type) }; - /** converts a binrpc_val to int. *@return int val on success, 0 and sets err on error (E_BINRPC_TYPE) */ -inline static int binrpc_val_conv_int( struct binrpc_val* v, int* err) +inline static int binrpc_val_conv_int(struct binrpc_val *v, int *err) { int ret; - - *err=0; - switch(v->type){ + + *err = 0; + switch(v->type) { case BINRPC_T_INT: return v->u.intval; case BINRPC_T_DOUBLE: - return (int) v->u.fval; + return (int)v->u.fval; case BINRPC_T_STR: - if (str2sint(&v->u.strval, &ret)==0) + if(str2sint(&v->u.strval, &ret) == 0) return ret; } - *err=E_BINRPC_TYPE; + *err = E_BINRPC_TYPE; return 0; } - /** converts a binrpc_val to double. *@return double val on success, 0 and sets err on error (E_BINRPC_TYPE) */ -inline static double binrpc_val_conv_double( struct binrpc_val* v, int* err) +inline static double binrpc_val_conv_double(struct binrpc_val *v, int *err) { double ret; - char* end; - - *err=0; - switch(v->type){ + char *end; + + *err = 0; + switch(v->type) { case BINRPC_T_DOUBLE: return v->u.fval; case BINRPC_T_INT: return (double)v->u.intval; case BINRPC_T_STR: - ret=strtod(v->u.strval.s, &end); - if (end!=v->u.strval.s) + ret = strtod(v->u.strval.s, &end); + if(end != v->u.strval.s) return ret; } - *err=E_BINRPC_TYPE; + *err = E_BINRPC_TYPE; return 0; } - /** converts a binrpc_val to str. *@return str val pointer on success, 0 and sets err on error (E_BINRPC_TYPE)*/ -inline static str* binrpc_val_conv_str(struct binrpc_ctx* ctx, - struct binrpc_val* v, int* err) +inline static str *binrpc_val_conv_str( + struct binrpc_ctx *ctx, struct binrpc_val *v, int *err) { - str* ret; - char* s; + str *ret; + char *s; int len; - - *err=0; - switch(v->type){ + + *err = 0; + switch(v->type) { case BINRPC_T_STR: return &v->u.strval; case BINRPC_T_INT: - s=int2str(v->u.intval, &len); - ret=ctl_malloc(sizeof(*ret)+len+1); - if (ret==0 || binrpc_gc_track(ctx, ret)!=0){ - if(ret!=0) ctl_free(ret); - *err=E_BINRPC_OVERFLOW; + s = int2str(v->u.intval, &len); + ret = ctl_malloc(sizeof(*ret) + len + 1); + if(ret == 0 || binrpc_gc_track(ctx, ret) != 0) { + if(ret != 0) + ctl_free(ret); + *err = E_BINRPC_OVERFLOW; return 0; } - ret->s=(char*)ret+sizeof(*ret); - ret->len=len; + ret->s = (char *)ret + sizeof(*ret); + ret->len = len; memcpy(ret->s, s, len); - ret->s[len]=0; + ret->s[len] = 0; return ret; case BINRPC_T_DOUBLE: /* for now the double to string conversion is not supported*/ - *err=E_BINRPC_BUG; + *err = E_BINRPC_BUG; return 0; } - *err=E_BINRPC_TYPE; + *err = E_BINRPC_TYPE; return 0; } - /* rpc interface functions */ /* returns the number of parameters read * on error: - number of parameters read so far (<=0)*/ -static int rpc_scan(struct binrpc_ctx* ctx, char* fmt, ...) +static int rpc_scan(struct binrpc_ctx *ctx, char *fmt, ...) { va_list ap; struct binrpc_val v; int err; - char* orig_fmt; + char *orig_fmt; int nofault; int modifiers; int autoconv; int i; double d; - str* s; - + str *s; + /* clear the previously saved error code */ rpc_fault_reset(ctx); - orig_fmt=fmt; + orig_fmt = fmt; nofault = 0; - modifiers=0; - autoconv=autoconvert; + modifiers = 0; + autoconv = autoconvert; va_start(ap, fmt); - for (;*fmt; fmt++){ - switch(*fmt){ + for(; *fmt; fmt++) { + switch(*fmt) { case '*': /* start of optional parameters */ nofault = 1; modifiers++; continue; case '.': /* autoconv. on for the next parameter */ modifiers++; - autoconv=1; + autoconv = 1; continue; case 'b': /* bool */ case 't': /* time */ case 'd': /* int */ case 'u': /* uint */ - v.type=autoconv?BINRPC_T_ALL:BINRPC_T_INT; - ctx->in.s=binrpc_read_record(&ctx->in.ctx, ctx->in.s, - ctx->in.end, &v, 0, &err); - if (err<0 || ((i=binrpc_val_conv_int(&v, &err))==0 && err<0)) - goto error_read; - *(va_arg(ap, int*))=i; + v.type = autoconv ? BINRPC_T_ALL : BINRPC_T_INT; + ctx->in.s = binrpc_read_record( + &ctx->in.ctx, ctx->in.s, ctx->in.end, &v, 0, &err); + if(err < 0 + || ((i = binrpc_val_conv_int(&v, &err)) == 0 + && err < 0)) + goto error_read; + *(va_arg(ap, int *)) = i; break; case 'f': - v.type=autoconv?BINRPC_T_ALL:BINRPC_T_DOUBLE; - ctx->in.s=binrpc_read_record(&ctx->in.ctx, ctx->in.s, - ctx->in.end, &v, 0, &err); - if (err<0 || ((d=binrpc_val_conv_double(&v, &err))==0 && - err<0)) + v.type = autoconv ? BINRPC_T_ALL : BINRPC_T_DOUBLE; + ctx->in.s = binrpc_read_record( + &ctx->in.ctx, ctx->in.s, ctx->in.end, &v, 0, &err); + if(err < 0 + || ((d = binrpc_val_conv_double(&v, &err)) == 0 + && err < 0)) goto error_read; - *(va_arg(ap, double*))=d; + *(va_arg(ap, double *)) = d; break; case 'l': /* long - convert it from double */ - v.type=autoconv?BINRPC_T_ALL:BINRPC_T_DOUBLE; - ctx->in.s=binrpc_read_record(&ctx->in.ctx, ctx->in.s, - ctx->in.end, &v, 0, &err); - if (err<0 || ((d=binrpc_val_conv_double(&v, &err))==0 && - err<0)) + v.type = autoconv ? BINRPC_T_ALL : BINRPC_T_DOUBLE; + ctx->in.s = binrpc_read_record( + &ctx->in.ctx, ctx->in.s, ctx->in.end, &v, 0, &err); + if(err < 0 + || ((d = binrpc_val_conv_double(&v, &err)) == 0 + && err < 0)) goto error_read; - *(va_arg(ap, long*))=(long)d; + *(va_arg(ap, long *)) = (long)d; break; case 'j': /* unsigned long - convert it from double */ - v.type=autoconv?BINRPC_T_ALL:BINRPC_T_DOUBLE; - ctx->in.s=binrpc_read_record(&ctx->in.ctx, ctx->in.s, - ctx->in.end, &v, 0, &err); - if (err<0 || ((d=binrpc_val_conv_double(&v, &err))==0 && - err<0)) + v.type = autoconv ? BINRPC_T_ALL : BINRPC_T_DOUBLE; + ctx->in.s = binrpc_read_record( + &ctx->in.ctx, ctx->in.s, ctx->in.end, &v, 0, &err); + if(err < 0 + || ((d = binrpc_val_conv_double(&v, &err)) == 0 + && err < 0)) goto error_read; - *(va_arg(ap, unsigned long*))=(unsigned long)d; + *(va_arg(ap, unsigned long *)) = (unsigned long)d; break; case 'L': /* long long - convert it from double */ - v.type=autoconv?BINRPC_T_ALL:BINRPC_T_DOUBLE; - ctx->in.s=binrpc_read_record(&ctx->in.ctx, ctx->in.s, - ctx->in.end, &v, 0, &err); - if (err<0 || ((d=binrpc_val_conv_double(&v, &err))==0 && - err<0)) + v.type = autoconv ? BINRPC_T_ALL : BINRPC_T_DOUBLE; + ctx->in.s = binrpc_read_record( + &ctx->in.ctx, ctx->in.s, ctx->in.end, &v, 0, &err); + if(err < 0 + || ((d = binrpc_val_conv_double(&v, &err)) == 0 + && err < 0)) goto error_read; - *(va_arg(ap, long long*))=(long long)d; + *(va_arg(ap, long long *)) = (long long)d; break; case 'J': /* unsigned long long - convert it from double */ - v.type=autoconv?BINRPC_T_ALL:BINRPC_T_DOUBLE; - ctx->in.s=binrpc_read_record(&ctx->in.ctx, ctx->in.s, - ctx->in.end, &v, 0, &err); - if (err<0 || ((d=binrpc_val_conv_double(&v, &err))==0 && - err<0)) + v.type = autoconv ? BINRPC_T_ALL : BINRPC_T_DOUBLE; + ctx->in.s = binrpc_read_record( + &ctx->in.ctx, ctx->in.s, ctx->in.end, &v, 0, &err); + if(err < 0 + || ((d = binrpc_val_conv_double(&v, &err)) == 0 + && err < 0)) goto error_read; - *(va_arg(ap, unsigned long long*))=(unsigned long long)d; + *(va_arg(ap, unsigned long long *)) = (unsigned long long)d; break; case 's': /* asciiz */ case 'S': /* str */ - v.type=autoconv?BINRPC_T_ALL:BINRPC_T_STR; - ctx->in.s=binrpc_read_record(&ctx->in.ctx, ctx->in.s, - ctx->in.end, &v, 0, &err); - if (err<0 || ((s=binrpc_val_conv_str(ctx, &v, &err))==0 || - err<0)){ - v.u.strval.s="if you get this string, you don't" - "check rpc_scan return code !!! (very bad)"; - v.u.strval.len=strlen(v.u.strval.s); - s=&v.u.strval; + v.type = autoconv ? BINRPC_T_ALL : BINRPC_T_STR; + ctx->in.s = binrpc_read_record( + &ctx->in.ctx, ctx->in.s, ctx->in.end, &v, 0, &err); + if(err < 0 + || ((s = binrpc_val_conv_str(ctx, &v, &err)) == 0 + || err < 0)) { + v.u.strval.s = "if you get this string, you don't" + "check rpc_scan return code !!! (very bad)"; + v.u.strval.len = strlen(v.u.strval.s); + s = &v.u.strval; } - if (*fmt=='s'){ - *(va_arg(ap, char**))=s->s; /* 0 term by proto*/ - }else{ - *(va_arg(ap, str*))=*s; + if(*fmt == 's') { + *(va_arg(ap, char **)) = s->s; /* 0 term by proto*/ + } else { + *(va_arg(ap, str *)) = *s; } - if (err<0) goto error_read; + if(err < 0) + goto error_read; break; case '{': /* struct */ - v.type=BINRPC_T_STRUCT; + v.type = BINRPC_T_STRUCT; /* FIXME: structure reading doesn't work for now */ #if 0 ctx->in.s=binrpc_read_record(&ctx->in.ctx, ctx->in.s, @@ -966,132 +978,148 @@ static int rpc_scan(struct binrpc_ctx* ctx, char* fmt, ...) default: goto error_inv_param; } - autoconv=autoconvert; /* reset autoconv*/ + autoconv = autoconvert; /* reset autoconv*/ ctx->in.record_no++; } va_end(ap); - return (int)(fmt-orig_fmt)-modifiers; + return (int)(fmt - orig_fmt) - modifiers; error_read: /* Do not immediately send out the error message, the user might retry the scan with different parameters */ - if(nofault==0 || ((err!=E_BINRPC_MORE_DATA) && (err!=E_BINRPC_EOP))) { - rpc_fault_prepare(ctx, 400, "error at parameter %d: expected %s type but" - " %s", ctx->in.record_no, rpc_type_name(v.type), - binrpc_error(err)); - /* + if(nofault == 0 || ((err != E_BINRPC_MORE_DATA) && (err != E_BINRPC_EOP))) { + rpc_fault_prepare(ctx, 400, + "error at parameter %d: expected %s type but" + " %s", + ctx->in.record_no, rpc_type_name(v.type), binrpc_error(err)); + /* rpc_fault(ctx, 400, "invalid record %d, offset %d (expected %d type)" ": %s", ctx->in.record_no, ctx->in.ctx.offset, v.type, binrpc_error(err)); */ } - if(nofault==1 && (err==E_BINRPC_MORE_DATA || err==E_BINRPC_EOP)) { + if(nofault == 1 && (err == E_BINRPC_MORE_DATA || err == E_BINRPC_EOP)) { va_end(ap); - return (int)(fmt-orig_fmt)-modifiers; + return (int)(fmt - orig_fmt) - modifiers; } goto error_ret; error_not_supported: - rpc_fault(ctx, 500, "internal server error, type %d not supported", - v.type); - LOG(L_CRIT, "BUG: binrpc: rpc_scan: formatting char \'%c\'" - " not supported\n", *fmt); -goto error_ret; + rpc_fault(ctx, 500, "internal server error, type %d not supported", v.type); + LOG(L_CRIT, + "BUG: binrpc: rpc_scan: formatting char \'%c\'" + " not supported\n", + *fmt); + goto error_ret; error_inv_param: rpc_fault(ctx, 500, "internal server error, invalid format char \'%c\'", - *fmt); + *fmt); error_ret: va_end(ap); - return -((int)(fmt-orig_fmt)-modifiers); + return -((int)(fmt - orig_fmt) - modifiers); } - /* returns 0 on success, -1 on error */ -static int rpc_add(struct binrpc_ctx* ctx, char* fmt, ...) +static int rpc_add(struct binrpc_ctx *ctx, char *fmt, ...) { va_list ap; int err; str st; - str* sp; - struct rpc_struct_l* rs; + str *sp; + struct rpc_struct_l *rs; str null_value = str_init(""); double d; va_start(ap, fmt); - for (;*fmt; fmt++){ - switch(*fmt){ + for(; *fmt; fmt++) { + switch(*fmt) { case 'd': case 't': case 'b': case 'u': - err=binrpc_addint(&ctx->out.pkt, va_arg(ap, int)); - if (err<0) goto error_add; + err = binrpc_addint(&ctx->out.pkt, va_arg(ap, int)); + if(err < 0) + goto error_add; break; case 's': /* asciiz */ - st.s=va_arg(ap, char*); - if (st.s==0) { + st.s = va_arg(ap, char *); + if(st.s == 0) { /* fix null strings */ - st=null_value; + st = null_value; } else { - st.len=strlen(st.s); + st.len = strlen(st.s); } - err=binrpc_addstr(&ctx->out.pkt, st.s, st.len); - if (err<0) goto error_add; + err = binrpc_addstr(&ctx->out.pkt, st.s, st.len); + if(err < 0) + goto error_add; break; case 'S': /* str */ - sp=va_arg(ap, str*); - if(sp!=NULL && sp->s!=NULL) { - st=*sp; + sp = va_arg(ap, str *); + if(sp != NULL && sp->s != NULL) { + st = *sp; } else { - st=null_value; + st = null_value; } - err=binrpc_addstr(&ctx->out.pkt, st.s, st.len); - if (err<0) goto error_add; + err = binrpc_addstr(&ctx->out.pkt, st.s, st.len); + 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(); - if (rs==0) goto error_mem; - rs->offset=binrpc_pkt_len(&ctx->out.pkt); - err=binrpc_end_struct(&ctx->out.pkt); - if (err<0) goto error_add; + err = binrpc_start_struct(&ctx->out.pkt); + if(err < 0) + goto error_add; + rs = new_rpc_struct(); + if(rs == 0) + goto error_mem; + rs->offset = binrpc_pkt_len(&ctx->out.pkt); + err = binrpc_end_struct(&ctx->out.pkt); + if(err < 0) + goto error_add; clist_append(&ctx->out.structs, rs, next, prev); - *(va_arg(ap, void**))=rs; + *(va_arg(ap, void **)) = rs; break; case 'f': - err=binrpc_adddouble(&ctx->out.pkt, va_arg(ap, double)); - if (err<0) goto error_add; + err = binrpc_adddouble(&ctx->out.pkt, va_arg(ap, double)); + if(err < 0) + goto error_add; break; case 'l': /* long - store in a double */ - d = (double)va_arg(ap, long); - err=binrpc_adddouble(&ctx->out.pkt, d); - if (err<0) goto error_add; + d = (double)va_arg(ap, long); + err = binrpc_adddouble(&ctx->out.pkt, d); + if(err < 0) + goto error_add; break; case 'j': /* unsigned long - store in a double */ - d = (double)va_arg(ap, unsigned long); - err=binrpc_adddouble(&ctx->out.pkt, d); - if (err<0) goto error_add; + d = (double)va_arg(ap, unsigned long); + err = binrpc_adddouble(&ctx->out.pkt, d); + if(err < 0) + goto error_add; break; case 'L': /* long long - store in a double */ - d = (double)va_arg(ap, long long); - err=binrpc_adddouble(&ctx->out.pkt, d); - if (err<0) goto error_add; + d = (double)va_arg(ap, long long); + err = binrpc_adddouble(&ctx->out.pkt, d); + if(err < 0) + goto error_add; break; case 'J': /* unsigned long long - store in a double */ - d = (double)va_arg(ap, unsigned long long); - err=binrpc_adddouble(&ctx->out.pkt, d); - if (err<0) goto error_add; + d = (double)va_arg(ap, unsigned long long); + err = binrpc_adddouble(&ctx->out.pkt, d); + if(err < 0) + goto error_add; break; default: - rpc_fault(ctx, 500, "Internal server error: " - "invalid formatting character \'%c\'", *fmt); - LOG(L_CRIT, "BUG: binrpc: rpc_add: formatting char \'%c\'" - " not supported\n", *fmt); + rpc_fault(ctx, 500, + "Internal server error: " + "invalid formatting character \'%c\'", + *fmt); + LOG(L_CRIT, + "BUG: binrpc: rpc_add: formatting char \'%c\'" + " not supported\n", + *fmt); goto error; } } @@ -1103,142 +1131,144 @@ static int rpc_add(struct binrpc_ctx* ctx, char* fmt, ...) goto error; error_add: rpc_fault(ctx, 500, "Internal server error processing \'%c\': %s (%d)", - *fmt, binrpc_error(err), err); + *fmt, binrpc_error(err), err); error: va_end(ap); return -1; } - -#define RPC_PRINTF_BUF_SIZE binrpc_buffer_size +#define RPC_PRINTF_BUF_SIZE binrpc_buffer_size /* returns 0 on success, -1 on error */ -static int rpc_rpl_printf(struct binrpc_ctx* ctx, char* fmt, ...) +static int rpc_rpl_printf(struct binrpc_ctx *ctx, char *fmt, ...) { va_list ap; - char* buf; + char *buf; int len; int err; - - buf=ctl_malloc(RPC_PRINTF_BUF_SIZE); - if (buf==0) goto error; + + buf = ctl_malloc(RPC_PRINTF_BUF_SIZE); + if(buf == 0) + goto error; va_start(ap, fmt); - len=vsnprintf(buf, RPC_PRINTF_BUF_SIZE, fmt, ap); + len = vsnprintf(buf, RPC_PRINTF_BUF_SIZE, fmt, ap); va_end(ap); - if ((len<0) || (len> RPC_PRINTF_BUF_SIZE)){ + if((len < 0) || (len > RPC_PRINTF_BUF_SIZE)) { LOG(L_ERR, "ERROR: binrpc: rpc_rpl_printf: buffer size exceeded(%d)\n", RPC_PRINTF_BUF_SIZE); goto error; } - if ((err=binrpc_addstr(&ctx->out.pkt, buf, len))<0){ - LOG(L_ERR, "ERROR: binrpc: rpc_rpl_printf: binrpc_addstr failed:" - " %s (%d)\n", binrpc_error(err), err); + if((err = binrpc_addstr(&ctx->out.pkt, buf, len)) < 0) { + LOG(L_ERR, + "ERROR: binrpc: rpc_rpl_printf: binrpc_addstr failed:" + " %s (%d)\n", + binrpc_error(err), err); goto error; } ctl_free(buf); return 0; error: - if (buf) ctl_free(buf); + if(buf) + ctl_free(buf); return -1; } - /* returns 0 on success, -1 on error */ -static int rpc_struct_add(struct rpc_struct_l* s, char* fmt, ...) +static int rpc_struct_add(struct rpc_struct_l *s, char *fmt, ...) { va_list ap; int err; struct binrpc_val avp; - struct rpc_struct_l* rs; + struct rpc_struct_l *rs; str *sp; str null_value = str_init(""); va_start(ap, fmt); - for (;*fmt; fmt++){ + for(; *fmt; fmt++) { memset(&avp, 0, sizeof(struct binrpc_val)); - avp.name.s=va_arg(ap, char*); - if (avp.name.s) - avp.name.len=strlen(avp.name.s); - switch(*fmt){ + avp.name.s = va_arg(ap, char *); + if(avp.name.s) + avp.name.len = strlen(avp.name.s); + switch(*fmt) { case 'd': case 't': case 'b': case 'u': - avp.type=BINRPC_T_INT; - avp.u.intval=va_arg(ap, int); + avp.type = BINRPC_T_INT; + avp.u.intval = va_arg(ap, int); break; case 's': /* asciiz */ - avp.type=BINRPC_T_STR; - avp.u.strval.s=va_arg(ap, char*); - if (avp.u.strval.s==NULL) { + avp.type = BINRPC_T_STR; + avp.u.strval.s = va_arg(ap, char *); + if(avp.u.strval.s == NULL) { /* fix null strings */ - avp.u.strval=null_value; + avp.u.strval = null_value; } else { - avp.u.strval.len=strlen(avp.u.strval.s); + avp.u.strval.len = strlen(avp.u.strval.s); } break; case 'S': /* str */ - avp.type=BINRPC_T_STR; - sp = va_arg(ap, str*); - if(sp!=NULL && sp->s!=NULL) { - avp.u.strval=*sp; + avp.type = BINRPC_T_STR; + sp = va_arg(ap, str *); + if(sp != NULL && sp->s != NULL) { + avp.u.strval = *sp; } else { - avp.u.strval=null_value; + avp.u.strval = null_value; } break; case '{': case '[': - avp.type=BINRPC_T_STRUCT; - err=binrpc_addavp(&s->pkt, &avp); - if (err<0){ + avp.type = BINRPC_T_STRUCT; + err = binrpc_addavp(&s->pkt, &avp); + if(err < 0) { LM_ERR("failed to add attribute-value (%c)\n", *fmt); goto error_add; } - rs=new_rpc_struct(); - if (rs==0){ + rs = new_rpc_struct(); + if(rs == 0) { LM_ERR("not enough memory (%c)\n", *fmt); goto error_mem; } - rs->offset=binrpc_pkt_len(&s->pkt); - err=binrpc_end_struct(&s->pkt); - if (err<0) { + rs->offset = binrpc_pkt_len(&s->pkt); + err = binrpc_end_struct(&s->pkt); + if(err < 0) { LM_ERR("failed to end struct (%c)\n", *fmt); goto error_add; } clist_append(&s->substructs, rs, next, prev); - *(va_arg(ap, void**))=rs; + *(va_arg(ap, void **)) = rs; goto end; case 'f': - avp.type=BINRPC_T_DOUBLE; - avp.u.fval=va_arg(ap, double); + avp.type = BINRPC_T_DOUBLE; + avp.u.fval = va_arg(ap, double); break; case 'l': /* long - store in a double */ - avp.type=BINRPC_T_DOUBLE; - avp.u.fval=(double)va_arg(ap, long); + avp.type = BINRPC_T_DOUBLE; + avp.u.fval = (double)va_arg(ap, long); break; case 'j': /* unsigned long - store in a double */ - avp.type=BINRPC_T_DOUBLE; - avp.u.fval=(double)va_arg(ap, unsigned long); + avp.type = BINRPC_T_DOUBLE; + avp.u.fval = (double)va_arg(ap, unsigned long); break; case 'L': /* long long - store in a double */ - avp.type=BINRPC_T_DOUBLE; - avp.u.fval=(double)va_arg(ap, long long); + avp.type = BINRPC_T_DOUBLE; + avp.u.fval = (double)va_arg(ap, long long); break; case 'J': /* unsigned long long - store in a double */ - avp.type=BINRPC_T_DOUBLE; - avp.u.fval=(double)va_arg(ap, unsigned long long); + avp.type = BINRPC_T_DOUBLE; + avp.u.fval = (double)va_arg(ap, unsigned long long); break; default: LM_ERR("formatting char \'%c\' not supported\n", *fmt); goto error; } - err=binrpc_addavp(&s->pkt, &avp); - if (err<0) { + err = binrpc_addavp(&s->pkt, &avp); + if(err < 0) { LM_ERR("failed to add attribute-value (%c)\n", *fmt); goto error; } @@ -1254,90 +1284,103 @@ static int rpc_struct_add(struct rpc_struct_l* s, char* fmt, ...) } /* returns 0 on success, -1 on error */ -static int rpc_array_add(struct rpc_struct_l* s, char* fmt, ...) +static int rpc_array_add(struct rpc_struct_l *s, char *fmt, ...) { va_list ap; int err; str st; str *sp; - struct rpc_struct_l* rs; + struct rpc_struct_l *rs; str null_value = str_init(""); double d; va_start(ap, fmt); - for (;*fmt; fmt++){ - switch(*fmt){ + for(; *fmt; fmt++) { + switch(*fmt) { case 'd': case 't': case 'b': case 'u': - err=binrpc_addint(&s->pkt, va_arg(ap, int)); - if (err<0) goto error_add; + err = binrpc_addint(&s->pkt, va_arg(ap, int)); + if(err < 0) + goto error_add; break; case 's': /* asciiz */ - st.s=va_arg(ap, char*); - if (st.s==0) { + st.s = va_arg(ap, char *); + if(st.s == 0) { /* fix null strings */ - st=null_value; + st = null_value; } else { st.len = strlen(st.s); } - err=binrpc_addstr(&s->pkt, st.s, st.len); - if (err<0) goto error_add; + err = binrpc_addstr(&s->pkt, st.s, st.len); + if(err < 0) + goto error_add; break; case 'S': /* str */ - sp=va_arg(ap, str*); - if(sp!=NULL && sp->s!=NULL) { - st=*sp; + sp = va_arg(ap, str *); + if(sp != NULL && sp->s != NULL) { + st = *sp; } else { - st=null_value; + st = null_value; } - err=binrpc_addstr(&s->pkt, st.s, st.len); - if (err<0) goto error_add; + 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; + 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; + *(va_arg(ap, void **)) = rs; break; case 'f': - err=binrpc_adddouble(&s->pkt, va_arg(ap, double)); - if (err<0) goto error_add; + err = binrpc_adddouble(&s->pkt, va_arg(ap, double)); + if(err < 0) + goto error_add; break; case 'l': /* long - store in a double */ d = (double)va_arg(ap, long); - err=binrpc_adddouble(&s->pkt, d); - if (err<0) goto error_add; + err = binrpc_adddouble(&s->pkt, d); + if(err < 0) + goto error_add; break; case 'j': /* unsigned long - store in a double */ d = (double)va_arg(ap, unsigned long); - err=binrpc_adddouble(&s->pkt, d); - if (err<0) goto error_add; + err = binrpc_adddouble(&s->pkt, d); + if(err < 0) + goto error_add; break; case 'L': /* long long - store in a double */ d = (double)va_arg(ap, long long); - err=binrpc_adddouble(&s->pkt, d); - if (err<0) goto error_add; + err = binrpc_adddouble(&s->pkt, d); + if(err < 0) + goto error_add; break; case 'J': /* unsigned long long - store in a double */ d = (double)va_arg(ap, unsigned long long); - err=binrpc_adddouble(&s->pkt, d); - if (err<0) goto error_add; + err = binrpc_adddouble(&s->pkt, d); + if(err < 0) + goto error_add; break; default: - LOG(L_CRIT, "BUG: binrpc: rpc_add: formatting char \'%c\'" - " not supported\n", *fmt); + LOG(L_CRIT, + "BUG: binrpc: rpc_add: formatting char \'%c\'" + " not supported\n", + *fmt); goto error; } } @@ -1351,48 +1394,51 @@ static int rpc_array_add(struct rpc_struct_l* s, char* fmt, ...) } /* returns 0 on success, -1 on error */ -static int rpc_struct_printf(struct rpc_struct_l *s, char* name, - char* fmt, ...) +static int rpc_struct_printf(struct rpc_struct_l *s, char *name, char *fmt, ...) { va_list ap; - char* buf; + char *buf; int len; int err; struct binrpc_val avp; - - buf=ctl_malloc(RPC_PRINTF_BUF_SIZE); - if (buf==0) goto error; + + buf = ctl_malloc(RPC_PRINTF_BUF_SIZE); + if(buf == 0) + goto error; va_start(ap, fmt); - len=vsnprintf(buf, RPC_PRINTF_BUF_SIZE, fmt, ap); + len = vsnprintf(buf, RPC_PRINTF_BUF_SIZE, fmt, ap); va_end(ap); - if ((len<0) || (len> RPC_PRINTF_BUF_SIZE)){ - LOG(L_ERR, "ERROR: binrpc: rpc_struct_printf:" - " buffer size exceeded(%d)\n", RPC_PRINTF_BUF_SIZE); + if((len < 0) || (len > RPC_PRINTF_BUF_SIZE)) { + LOG(L_ERR, + "ERROR: binrpc: rpc_struct_printf:" + " buffer size exceeded(%d)\n", + RPC_PRINTF_BUF_SIZE); goto error; } - avp.name.s=name; - avp.name.len=strlen(name); - avp.type=BINRPC_T_STR; - avp.u.strval.s=buf; - avp.u.strval.len=strlen(buf); - - if ((err=binrpc_addavp(&s->pkt, &avp))<0){ - LOG(L_ERR, "ERROR: binrpc: rpc_printf: binrpc_addavp failed:" - " %s (%d)\n", binrpc_error(err), err); + avp.name.s = name; + avp.name.len = strlen(name); + avp.type = BINRPC_T_STR; + avp.u.strval.s = buf; + avp.u.strval.len = strlen(buf); + + if((err = binrpc_addavp(&s->pkt, &avp)) < 0) { + LOG(L_ERR, + "ERROR: binrpc: rpc_printf: binrpc_addavp failed:" + " %s (%d)\n", + binrpc_error(err), err); goto error; } ctl_free(buf); return 0; error: - if (buf) ctl_free(buf); + if(buf) + ctl_free(buf); return -1; } - -static int rpc_struct_scan(struct rpc_struct_l* s, char* fmt, ...) +static int rpc_struct_scan(struct rpc_struct_l *s, char *fmt, ...) { LOG(L_CRIT, "ERROR: binrpc:rpc_struct_scan: not implemented\n"); return -1; }; - diff --git a/src/modules/ctl/binrpc_run.h b/src/modules/ctl/binrpc_run.h index bbc985b4598..9fe23a6251b 100644 --- a/src/modules/ctl/binrpc_run.h +++ b/src/modules/ctl/binrpc_run.h @@ -22,8 +22,8 @@ #define _binrpc_run_h -int process_rpc_req(unsigned char* buf, int size, int* bytes_needed, - void* sh, void** saved_state); +int process_rpc_req(unsigned char *buf, int size, int *bytes_needed, void *sh, + void **saved_state); void binrpc_callbacks_init(void); diff --git a/src/modules/ctl/ctl.c b/src/modules/ctl/ctl.c index 65cb10589eb..87654c8534c 100644 --- a/src/modules/ctl/ctl.c +++ b/src/modules/ctl/ctl.c @@ -29,9 +29,9 @@ #include #include /* socketpair */ -#include /* close, fork, getpid */ -#include /* snprintf */ -#include /* strerror */ +#include /* close, fork, getpid */ +#include /* snprintf */ +#include /* strerror */ #include MODULE_VERSION @@ -46,14 +46,12 @@ static int mod_init(void); static int mod_child(int rank); static void mod_destroy(void); -static cmd_export_t cmds[]={ - {0,0,0,0,0,0} -}; +static cmd_export_t cmds[] = {{0, 0, 0, 0, 0, 0}}; -static int usock_mode=0600; /* permissions, default rw-------*/ -static int usock_uid=-1; /* username and group for the unix sockets*/ -static int usock_gid=-1; +static int usock_mode = 0600; /* permissions, default rw-------*/ +static int usock_uid = -1; /* username and group for the unix sockets*/ +static int usock_gid = -1; /* if set try to automatically convert values to the requested type in rpc->scan (default: not set) */ @@ -62,110 +60,108 @@ extern int binrpc_max_body_size; extern int binrpc_struct_max_body_size; extern int binrpc_buffer_size; -static int add_binrpc_socket(modparam_t type, void * val); +static int add_binrpc_socket(modparam_t type, void *val); #ifdef USE_FIFO -static int add_fifo_socket(modparam_t type, void * val); +static int add_fifo_socket(modparam_t type, void *val); #endif -static int fix_user(modparam_t type, void * val); -static int fix_group(modparam_t type, void * val); +static int fix_user(modparam_t type, void *val); +static int fix_group(modparam_t type, void *val); -static void ctrl_listen_ls_rpc(rpc_t* rpc, void* ctx); -void io_listen_who_rpc(rpc_t* rpc, void* ctx); -void io_listen_conn_rpc(rpc_t* rpc, void* ctx); +static void ctrl_listen_ls_rpc(rpc_t *rpc, void *ctx); +void io_listen_who_rpc(rpc_t *rpc, void *ctx); +void io_listen_conn_rpc(rpc_t *rpc, void *ctx); -static char* io_listen_who_doc[]={ "list open connections", 0 }; -static char* io_listen_conn_doc[]={ "returns number of open connections", 0 }; -static char* ctl_listen_ls_doc[]={ "list ctl listen sockets", 0 }; +static char *io_listen_who_doc[] = {"list open connections", 0}; +static char *io_listen_conn_doc[] = {"returns number of open connections", 0}; +static char *ctl_listen_ls_doc[] = {"list ctl listen sockets", 0}; -static rpc_export_t ctl_rpc[]={ - {"ctl.who", io_listen_who_rpc, (const char**)io_listen_who_doc, 0}, - {"ctl.connections", io_listen_conn_rpc,(const char**)io_listen_conn_doc,0}, - {"ctl.listen", ctrl_listen_ls_rpc,(const char**)ctl_listen_ls_doc, 0}, - { 0, 0, 0, 0} -}; +static rpc_export_t ctl_rpc[] = { + {"ctl.who", io_listen_who_rpc, (const char **)io_listen_who_doc, 0}, + {"ctl.connections", io_listen_conn_rpc, + (const char **)io_listen_conn_doc, 0}, + {"ctl.listen", ctrl_listen_ls_rpc, (const char **)ctl_listen_ls_doc, 0}, + {0, 0, 0, 0}}; -static param_export_t params[]={ +static param_export_t params[] = { #ifdef USE_FIFO - {"fifo", PARAM_STRING|PARAM_USE_FUNC, (void*) add_fifo_socket }, + {"fifo", PARAM_STRING | PARAM_USE_FUNC, (void *)add_fifo_socket}, #endif - {"binrpc", PARAM_STRING|PARAM_USE_FUNC, (void*) add_binrpc_socket}, - {"mode", PARAM_INT, &usock_mode }, - {"user", PARAM_STRING|PARAM_USE_FUNC, fix_user }, - {"group", PARAM_STRING|PARAM_USE_FUNC, fix_group }, - {"autoconversion", PARAM_INT, &autoconvert }, - {"binrpc_max_body_size", PARAM_INT, &binrpc_max_body_size }, - {"binrpc_struct_max_body_size", PARAM_INT, &binrpc_struct_max_body_size }, - {"binrpc_buffer_size", PARAM_INT, &binrpc_buffer_size }, - {0,0,0} -}; /* no params */ + {"binrpc", PARAM_STRING | PARAM_USE_FUNC, (void *)add_binrpc_socket}, + {"mode", PARAM_INT, &usock_mode}, + {"user", PARAM_STRING | PARAM_USE_FUNC, fix_user}, + {"group", PARAM_STRING | PARAM_USE_FUNC, fix_group}, + {"autoconversion", PARAM_INT, &autoconvert}, + {"binrpc_max_body_size", PARAM_INT, &binrpc_max_body_size}, + {"binrpc_struct_max_body_size", PARAM_INT, + &binrpc_struct_max_body_size}, + {"binrpc_buffer_size", PARAM_INT, &binrpc_buffer_size}, + {0, 0, 0}}; /* no params */ struct module_exports exports = { - "ctl", /* module name */ - DEFAULT_DLFLAGS, /* dlopen flags */ - cmds, /* cmd (cfg function) exports */ - params, /* param exports */ - ctl_rpc, /* RPC method exports */ - 0, /* pseudo-variables exports */ - 0, /* response handling function */ - mod_init, /* module init function */ - mod_child, /* per-child init function */ - mod_destroy /* module destroy function */ + "ctl", /* module name */ + DEFAULT_DLFLAGS, /* dlopen flags */ + cmds, /* cmd (cfg function) exports */ + params, /* param exports */ + ctl_rpc, /* RPC method exports */ + 0, /* pseudo-variables exports */ + 0, /* response handling function */ + mod_init, /* module init function */ + mod_child, /* per-child init function */ + mod_destroy /* module destroy function */ }; -static struct id_list* listen_lst=0; /* binrpc sockets name list */ -static struct ctrl_socket* ctrl_sock_lst=0; -static int fd_no=0; /* number of fd used */ +static struct id_list *listen_lst = 0; /* binrpc sockets name list */ +static struct ctrl_socket *ctrl_sock_lst = 0; +static int fd_no = 0; /* number of fd used */ -static int add_binrpc_socket(modparam_t type, void * val) +static int add_binrpc_socket(modparam_t type, void *val) { char *s; - struct id_list* id; + struct id_list *id; - if ((type & PARAM_STRING)==0){ + if((type & PARAM_STRING) == 0) { LOG(L_CRIT, "BUG: ctl: add_binrpc_socket: bad parameter type %d\n", - type); + type); goto error; } - s=(char*)val; - id=parse_listen_id(s, strlen(s), UDP_SOCK); /* default udp proto */ - if (id==0){ + s = (char *)val; + id = parse_listen_id(s, strlen(s), UDP_SOCK); /* default udp proto */ + if(id == 0) { LOG(L_ERR, "ERROR: ctl: bad listen socket: \"%s\"\n", s); goto error; } - id->data_proto=P_BINRPC; - id->next=listen_lst; - listen_lst=id; + id->data_proto = P_BINRPC; + id->next = listen_lst; + listen_lst = id; return 0; error: return -1; } - #ifdef USE_FIFO -static int add_fifo_socket(modparam_t type, void * val) +static int add_fifo_socket(modparam_t type, void *val) { char *s; - struct id_list* id; + struct id_list *id; - if ((type & PARAM_STRING)==0){ - LOG(L_CRIT, "BUG: ctl: add_fifo: bad parameter type %d\n", - type); + if((type & PARAM_STRING) == 0) { + LOG(L_CRIT, "BUG: ctl: add_fifo: bad parameter type %d\n", type); goto error; } - s=(char*)val; - id=parse_listen_id(s, strlen(s), FIFO_SOCK); /* default udp proto */ - if (id==0){ + s = (char *)val; + id = parse_listen_id(s, strlen(s), FIFO_SOCK); /* default udp proto */ + if(id == 0) { LOG(L_ERR, "ERROR: ctl: bad fifo: \"%s\"\n", s); goto error; } - id->data_proto=P_FIFO; - id->next=listen_lst; - listen_lst=id; + id->data_proto = P_FIFO; + id->next = listen_lst; + listen_lst = id; return 0; error: return -1; @@ -173,18 +169,16 @@ static int add_fifo_socket(modparam_t type, void * val) #endif - -static int fix_user(modparam_t type, void * val) +static int fix_user(modparam_t type, void *val) { - char* s; + char *s; - if ((type & PARAM_STRING)==0){ - LOG(L_CRIT, "BUG: ctl: fix_user: bad parameter type %d\n", - type); + if((type & PARAM_STRING) == 0) { + LOG(L_CRIT, "BUG: ctl: fix_user: bad parameter type %d\n", type); goto error; } - s=(char*)val; - if (user2uid(&usock_uid, 0, s)<0){ + s = (char *)val; + if(user2uid(&usock_uid, 0, s) < 0) { LOG(L_ERR, "ERROR: ctl: bad user name/uid number %s\n", s); goto error; } @@ -194,18 +188,16 @@ static int fix_user(modparam_t type, void * val) } - -static int fix_group(modparam_t type, void * val) +static int fix_group(modparam_t type, void *val) { - char* s; + char *s; - if ((type & PARAM_STRING)==0){ - LOG(L_CRIT, "BUG: ctl: fix_group: bad parameter type %d\n", - type); + if((type & PARAM_STRING) == 0) { + LOG(L_CRIT, "BUG: ctl: fix_group: bad parameter type %d\n", type); goto error; } - s=(char*)val; - if (group2gid(&usock_gid, s)<0){ + s = (char *)val; + if(group2gid(&usock_gid, s) < 0) { LOG(L_ERR, "ERROR: ctl: bad group name/gid number %s\n", s); goto error; } @@ -214,29 +206,30 @@ static int fix_group(modparam_t type, void * val) return -1; } -#define CTL_SOCKET_PATH_SIZE 128 +#define CTL_SOCKET_PATH_SIZE 128 static int mod_init(void) { - struct id_list* l; + struct id_list *l; char ctl_socket_path[CTL_SOCKET_PATH_SIZE]; binrpc_callbacks_init(); - if(binrpc_max_body_size<=0) + if(binrpc_max_body_size <= 0) binrpc_max_body_size = 4; - if(binrpc_struct_max_body_size<=0) + if(binrpc_struct_max_body_size <= 0) binrpc_struct_max_body_size = 1; binrpc_max_body_size *= 1024; binrpc_struct_max_body_size *= 1024; - if (listen_lst==0) { - if(strcmp(runtime_dir, RUN_DIR)==0) { + if(listen_lst == 0) { + if(strcmp(runtime_dir, RUN_DIR) == 0) { add_binrpc_socket(PARAM_STRING, DEFAULT_CTL_SOCKET); } else { if(sizeof(DEFAULT_CTL_SOCKET_PROTO) - + sizeof(DEFAULT_CTL_SOCKET_NAME) - + strlen(runtime_dir) + 4 > CTL_SOCKET_PATH_SIZE) { + + sizeof(DEFAULT_CTL_SOCKET_NAME) + + strlen(runtime_dir) + 4 + > CTL_SOCKET_PATH_SIZE) { LM_ERR("ctl socket path is too big\n"); return -1; } @@ -248,9 +241,9 @@ static int mod_init(void) } } DBG("listening on:\n"); - for (l=listen_lst; l; l=l->next){ + for(l = listen_lst; l; l = l->next) { fd_no++; - switch(l->proto){ + switch(l->proto) { case UNIXD_SOCK: DBG(" [%s:unix dgram] %s\n", payload_proto_name(l->data_proto), l->name); @@ -262,12 +255,12 @@ static int mod_init(void) case UDP_SOCK: DBG(" [%s:udp] %s:%d\n", payload_proto_name(l->data_proto), l->name, - l->port?l->port:DEFAULT_CTL_PORT); + l->port ? l->port : DEFAULT_CTL_PORT); break; case TCP_SOCK: DBG(" [%s:tcp] %s:%d\n", payload_proto_name(l->data_proto), l->name, - l->port?l->port:DEFAULT_CTL_PORT); + l->port ? l->port : DEFAULT_CTL_PORT); break; #ifdef USE_FIFO case FIFO_SOCK: @@ -280,18 +273,21 @@ static int mod_init(void) LOG(L_CRIT, "BUG: ctrl: listen protocol %d not supported\n", l->proto); goto error; - } + } } /* get the uid/gid from core if not set for the module */ - if(usock_uid==-1 && sock_uid!=-1) usock_uid = sock_uid; - if(usock_gid==-1 && sock_gid!=-1) usock_gid = sock_gid; + if(usock_uid == -1 && sock_uid != -1) + usock_uid = sock_uid; + if(usock_gid == -1 && sock_gid != -1) + usock_gid = sock_gid; /* open socket now, before suid */ - if (init_ctrl_sockets(&ctrl_sock_lst, listen_lst, DEFAULT_CTL_PORT, - usock_mode, usock_uid, usock_gid)<0){ + if(init_ctrl_sockets(&ctrl_sock_lst, listen_lst, DEFAULT_CTL_PORT, + usock_mode, usock_uid, usock_gid) + < 0) { LOG(L_ERR, "ERROR: ctl: mod_init: init ctrl. sockets failed\n"); goto error; } - if (ctrl_sock_lst){ + if(ctrl_sock_lst) { /* we will fork */ register_procs(1); /* we will be creating an extra process */ register_fds(fd_no); @@ -307,58 +303,57 @@ static int mod_init(void) } - static int mod_child(int rank) { int pid; - struct ctrl_socket* cs; - static int rpc_handler=0; + struct ctrl_socket *cs; + static int rpc_handler = 0; /* do nothing from PROC_INIT, is the same as PROC_MAIN */ - if (rank==PROC_INIT) + if(rank == PROC_INIT) return 0; /* we want to fork(), but only from one process */ - if ((rank == PROC_MAIN ) && (ctrl_sock_lst)){ /* FIXME: no fork ?? */ + if((rank == PROC_MAIN) && (ctrl_sock_lst)) { /* FIXME: no fork ?? */ DBG("ctl: mod_child(%d), ctrl_sock_lst=%p\n", rank, ctrl_sock_lst); /* fork, but make sure we know not to close our own sockets when * ctl child_init will be called for the new child */ - rpc_handler=1; + rpc_handler = 1; /* child should start with a correct estimated used fds number*/ register_fds(MAX_IO_READ_CONNECTIONS); - pid=fork_process(PROC_RPC, "ctl handler", 1); - DBG("ctl: mod_child(%d), fork_process=%d, csl=%p\n", - rank, pid, ctrl_sock_lst); - if (pid<0){ + pid = fork_process(PROC_RPC, "ctl handler", 1); + DBG("ctl: mod_child(%d), fork_process=%d, csl=%p\n", rank, pid, + ctrl_sock_lst); + if(pid < 0) { goto error; } - if (pid == 0){ /* child */ - _ksr_is_main=0; - DBG("ctl: %d io_listen_loop(%d, %p)\n", - rank, fd_no, ctrl_sock_lst); + if(pid == 0) { /* child */ + _ksr_is_main = 0; + DBG("ctl: %d io_listen_loop(%d, %p)\n", rank, fd_no, ctrl_sock_lst); io_listen_loop(fd_no, ctrl_sock_lst); - }else{ /* parent */ + } else { /* parent */ /* not used in parent */ register_fds(-MAX_IO_READ_CONNECTIONS); - rpc_handler=0; + rpc_handler = 0; } } - if (rank!=PROC_RPC || !rpc_handler){ + if(rank != PROC_RPC || !rpc_handler) { /* close all the opened fds, we don't need them here */ - for (cs=ctrl_sock_lst; cs; cs=cs->next){ - if(cs->fd>=0) close(cs->fd); - cs->fd=-1; - if (cs->write_fd!=-1){ + for(cs = ctrl_sock_lst; cs; cs = cs->next) { + if(cs->fd >= 0) + close(cs->fd); + cs->fd = -1; + if(cs->write_fd != -1) { close(cs->write_fd); - cs->write_fd=-1; + cs->write_fd = -1; } } - if (rank!=PROC_MAIN){ /* we need the lists in main for on_exit cleanup, + if(rank != PROC_MAIN) { /* we need the lists in main for on_exit cleanup, see mod_destroy */ /* free memory, we don't need the lists anymore */ free_ctrl_socket_list(ctrl_sock_lst); - ctrl_sock_lst=0; + ctrl_sock_lst = 0; free_id_list(listen_lst); - listen_lst=0; + listen_lst = 0; } } return 0; @@ -367,27 +362,28 @@ static int mod_child(int rank) } - static void mod_destroy(void) { - struct ctrl_socket* cs; + struct ctrl_socket *cs; /* close all the opened fds & unlink the files */ - for (cs=ctrl_sock_lst; cs; cs=cs->next){ - switch(cs->transport){ + for(cs = ctrl_sock_lst; cs; cs = cs->next) { + switch(cs->transport) { case UNIXS_SOCK: case UNIXD_SOCK: - if(cs->fd>=0) close(cs->fd); - cs->fd=-1; - if (cs->write_fd!=-1){ + if(cs->fd >= 0) + close(cs->fd); + cs->fd = -1; + if(cs->write_fd != -1) { close(cs->write_fd); - cs->write_fd=-1; + cs->write_fd = -1; } - if (cs->name){ - if (unlink(cs->name)<0){ - LOG(L_ERR, "ERROR: ctl: could not delete unix" - " socket %s: %s (%d)\n", - cs->name, strerror(errno), errno); + if(cs->name) { + if(unlink(cs->name) < 0) { + LOG(L_ERR, + "ERROR: ctl: could not delete unix" + " socket %s: %s (%d)\n", + cs->name, strerror(errno), errno); } } break; @@ -397,32 +393,33 @@ static void mod_destroy(void) break; #endif default: - if(cs->fd>=0) close(cs->fd); - cs->fd=-1; - if (cs->write_fd!=-1){ + if(cs->fd >= 0) + close(cs->fd); + cs->fd = -1; + if(cs->write_fd != -1) { close(cs->write_fd); - cs->write_fd=-1; + cs->write_fd = -1; } } } - if (listen_lst){ + if(listen_lst) { free_id_list(listen_lst); - listen_lst=0; + listen_lst = 0; } - if (ctrl_sock_lst){ + if(ctrl_sock_lst) { free_ctrl_socket_list(ctrl_sock_lst); - ctrl_sock_lst=0; + ctrl_sock_lst = 0; } } -static void ctrl_listen_ls_rpc(rpc_t* rpc, void* ctx) +static void ctrl_listen_ls_rpc(rpc_t *rpc, void *ctx) { - struct ctrl_socket* cs; + struct ctrl_socket *cs; - for (cs=ctrl_sock_lst; cs; cs=cs->next){ + for(cs = ctrl_sock_lst; cs; cs = cs->next) { rpc->add(ctx, "ssss", payload_proto_name(cs->p_proto), - socket_proto_name(cs->transport), - cs->name, (cs->port)?int2str(cs->port, 0):""); + socket_proto_name(cs->transport), cs->name, + (cs->port) ? int2str(cs->port, 0) : ""); } } diff --git a/src/modules/ctl/ctl.h b/src/modules/ctl/ctl.h index e17227bcaae..21893abe27d 100644 --- a/src/modules/ctl/ctl.h +++ b/src/modules/ctl/ctl.h @@ -34,4 +34,3 @@ #endif #endif - diff --git a/src/modules/ctl/ctl_defaults.h b/src/modules/ctl/ctl_defaults.h index c20fd17c7c0..e7da9febcaf 100644 --- a/src/modules/ctl/ctl_defaults.h +++ b/src/modules/ctl/ctl_defaults.h @@ -13,7 +13,8 @@ #define DEFAULT_CTL_SOCKET_NAME NAME "_ctl" #endif -#define DEFAULT_CTL_SOCKET DEFAULT_CTL_SOCKET_PROTO RUN_DIR "/" DEFAULT_CTL_SOCKET_NAME +#define DEFAULT_CTL_SOCKET \ + DEFAULT_CTL_SOCKET_PROTO RUN_DIR "/" DEFAULT_CTL_SOCKET_NAME /* port used by default for tcp/udp if no port is explicitely specified */ #define DEFAULT_CTL_PORT 2049 diff --git a/src/modules/ctl/ctrl_socks.c b/src/modules/ctl/ctrl_socks.c index f9d559564bd..fa6620394e2 100644 --- a/src/modules/ctl/ctrl_socks.c +++ b/src/modules/ctl/ctrl_socks.c @@ -43,257 +43,271 @@ * where host_name=string, ipv4 address, [ipv6 address], * unix socket path (starts with '/') */ -struct id_list* parse_listen_id(char* l, int len, enum socket_protos def) +struct id_list *parse_listen_id(char *l, int len, enum socket_protos def) { - char* p; + char *p; enum socket_protos proto; - char* name; - char* port_str; + char *name; + char *port_str; int port; int err; - struct servent* se; - char* s; - struct id_list* id; - - s=ctl_malloc((len+1)*sizeof(char)); - if (s==0){ + struct servent *se; + char *s; + struct id_list *id; + + s = ctl_malloc((len + 1) * sizeof(char)); + if(s == 0) { LOG(L_ERR, "ERROR:parse_listen_id: out of memory\n"); goto error; } memcpy(s, l, len); - s[len]=0; /* null terminate */ - + s[len] = 0; /* null terminate */ + /* duplicate */ - proto=UNKNOWN_SOCK; - port=0; - name=0; - port_str=0; - p=s; - - if ((*p)=='[') goto ipv6; + proto = UNKNOWN_SOCK; + port = 0; + name = 0; + port_str = 0; + p = s; + + if((*p) == '[') + goto ipv6; /* find proto or name */ - for (; *p; p++){ - if (*p==':'){ - *p=0; - if (strcasecmp("tcp", s)==0){ - proto=TCP_SOCK; + for(; *p; p++) { + if(*p == ':') { + *p = 0; + if(strcasecmp("tcp", s) == 0) { + proto = TCP_SOCK; goto find_host; - }else if (strcasecmp("udp", s)==0){ - proto=UDP_SOCK; + } else if(strcasecmp("udp", s) == 0) { + proto = UDP_SOCK; goto find_host; - }else if (strcasecmp("unixd", s)==0){ - proto=UNIXD_SOCK; + } else if(strcasecmp("unixd", s) == 0) { + proto = UNIXD_SOCK; goto find_host; - }else if ((strcasecmp("unix", s)==0)||(strcasecmp("unixs", s)==0)){ - proto=UNIXS_SOCK; + } else if((strcasecmp("unix", s) == 0) + || (strcasecmp("unixs", s) == 0)) { + proto = UNIXS_SOCK; goto find_host; #ifdef USE_FIFO - }else if (strcasecmp("fifo", s)==0){ - proto=FIFO_SOCK; + } else if(strcasecmp("fifo", s) == 0) { + proto = FIFO_SOCK; goto find_host; #endif - }else{ - proto=UNKNOWN_SOCK; + } else { + proto = UNKNOWN_SOCK; /* this might be the host */ - name=s; + name = s; goto find_port; } } } - name=s; + name = s; goto end; /* only name found */ find_host: p++; - if (*p=='[') goto ipv6; - name=p; - for (; *p; p++){ - if ((*p)==':'){ - *p=0; + if(*p == '[') + goto ipv6; + name = p; + for(; *p; p++) { + if((*p) == ':') { + *p = 0; goto find_port; } } goto end; /* nothing after name */ ipv6: - name=p; + name = p; p++; - for(;*p;p++){ - if(*p==']'){ - if(*(p+1)==':'){ - p++; *p=0; + for(; *p; p++) { + if(*p == ']') { + if(*(p + 1) == ':') { + p++; + *p = 0; goto find_port; - }else if (*(p+1)==0) goto end; - }else{ + } else if(*(p + 1) == 0) + goto end; + } else { goto error; } } - + find_port: p++; - port_str=(*p)?p:0; - + port_str = (*p) ? p : 0; + end: /* fix all the stuff */ - if (proto==UNKNOWN_SOCK){ + if(proto == UNKNOWN_SOCK) { /* try to guess */ - if (port_str){ - switch(def){ + if(port_str) { + switch(def) { case TCP_SOCK: case UDP_SOCK: - proto=def; + proto = def; break; default: - proto=UDP_SOCK; + proto = UDP_SOCK; DBG("guess:%s is a tcp socket\n", name); } - }else if (strchr(name, '/')){ - switch(def){ + } else if(strchr(name, '/')) { + switch(def) { case TCP_SOCK: case UDP_SOCK: DBG("guess:%s is a unix socket\n", name); - proto=UNIXS_SOCK; + proto = UNIXS_SOCK; break; default: /* def is filename based => use default */ - proto=def; + proto = def; } - }else{ + } else { /* using default */ - proto=def; + proto = def; } } - if (port_str){ - port=str2s(port_str, strlen(port_str), &err); - if (err){ + if(port_str) { + port = str2s(port_str, strlen(port_str), &err); + if(err) { /* try getservbyname */ - se=getservbyname(port_str, - (proto==TCP_SOCK)?"tcp":(proto==UDP_SOCK)?"udp":0); - if (se) port=ntohs(se->s_port); - else goto error; + se = getservbyname(port_str, (proto == TCP_SOCK) ? "tcp" + : (proto == UDP_SOCK) ? "udp" + : 0); + if(se) + port = ntohs(se->s_port); + else + goto error; } - }else{ + } else { /* no port, check if the hostname is a port * (e.g. tcp:3012 == tcp:*:3012 */ - if (proto==TCP_SOCK|| proto==UDP_SOCK){ - port=str2s(name, strlen(name), &err); - if (err){ - port=0; - }else{ - name="*"; /* inaddr any */ + if(proto == TCP_SOCK || proto == UDP_SOCK) { + port = str2s(name, strlen(name), &err); + if(err) { + port = 0; + } else { + name = "*"; /* inaddr any */ } } } - id=ctl_malloc(sizeof(struct id_list)); - if (id==0){ + id = ctl_malloc(sizeof(struct id_list)); + if(id == 0) { LOG(L_ERR, "ERROR:parse_listen_id: out of memory\n"); goto error; } - id->name=name; - id->proto=proto; - id->data_proto=P_BINRPC; - id->port=port; - id->buf=s; - id->next=0; + id->name = name; + id->proto = proto; + id->data_proto = P_BINRPC; + id->port = port; + id->buf = s; + id->next = 0; return id; error: - if (s) ctl_free(s); + if(s) + ctl_free(s); return 0; } -void free_id_list_elem(struct id_list* id) +void free_id_list_elem(struct id_list *id) { - if (id->buf){ + if(id->buf) { ctl_free(id->buf); - id->buf=0; + id->buf = 0; } } -void free_id_list(struct id_list* l) +void free_id_list(struct id_list *l) { - struct id_list* nxt; - - for (;l; l=nxt){ - nxt=l->next; + struct id_list *nxt; + + for(; l; l = nxt) { + nxt = l->next; free_id_list_elem(l); ctl_free(l); } } - -int init_ctrl_sockets(struct ctrl_socket** c_lst, struct id_list* lst, - int def_port, int perm, int uid, int gid) +int init_ctrl_sockets(struct ctrl_socket **c_lst, struct id_list *lst, + int def_port, int perm, int uid, int gid) { - struct id_list* l; + struct id_list *l; int s = -1; - struct ctrl_socket* cs; + struct ctrl_socket *cs; int extra_fd; union sockaddr_u su; - - for (l=lst; l; l=l->next){ - extra_fd=-1; - switch(l->proto){ + + for(l = lst; l; l = l->next) { + extra_fd = -1; + switch(l->proto) { case UNIXS_SOCK: - s=init_unix_sock(&su.sa_un, l->name, SOCK_STREAM, - perm, uid, gid); + s = init_unix_sock( + &su.sa_un, l->name, SOCK_STREAM, perm, uid, gid); break; case UNIXD_SOCK: - s=init_unix_sock(&su.sa_un, l->name, SOCK_DGRAM, - perm, uid, gid); + s = init_unix_sock( + &su.sa_un, l->name, SOCK_DGRAM, perm, uid, gid); break; case TCP_SOCK: - if (l->port==0) l->port=def_port; - s=init_tcpudp_sock(&su.sa_in, l->name, l->port, TCP_SOCK); + if(l->port == 0) + l->port = def_port; + s = init_tcpudp_sock(&su.sa_in, l->name, l->port, TCP_SOCK); break; case UDP_SOCK: - if (l->port==0) l->port=def_port; - s=init_tcpudp_sock(&su.sa_in, l->name, l->port, UDP_SOCK); + if(l->port == 0) + l->port = def_port; + s = init_tcpudp_sock(&su.sa_in, l->name, l->port, UDP_SOCK); break; #ifdef USE_FIFO case FIFO_SOCK: - s=init_fifo_fd(l->name, perm, uid, gid, &extra_fd); + s = init_fifo_fd(l->name, perm, uid, gid, &extra_fd); break; #endif default: - LOG(L_ERR, "ERROR: init_ctrl_listeners: unsupported" - " proto %d\n", l->proto); + LOG(L_ERR, + "ERROR: init_ctrl_listeners: unsupported" + " proto %d\n", + l->proto); continue; } - if (s==-1) goto error; + if(s == -1) + goto error; /* add listener */ - cs=ctl_malloc(sizeof(struct ctrl_socket)); - if (cs==0){ + cs = ctl_malloc(sizeof(struct ctrl_socket)); + if(cs == 0) { LOG(L_ERR, "ERROR: init_ctrl_listeners: out of memory\n"); goto error; } - memset(cs,0, sizeof(struct ctrl_socket)); - cs->transport=l->proto; - cs->p_proto=l->data_proto; - cs->fd=s; - cs->write_fd=extra_fd; /* needed for fifo write */ - cs->name=l->name; - cs->port=l->port; - cs->u=su; + memset(cs, 0, sizeof(struct ctrl_socket)); + cs->transport = l->proto; + cs->p_proto = l->data_proto; + cs->fd = s; + cs->write_fd = extra_fd; /* needed for fifo write */ + cs->name = l->name; + cs->port = l->port; + cs->u = su; /* add it to the list */ - cs->next=*c_lst; - *c_lst=cs; + cs->next = *c_lst; + *c_lst = cs; } return 0; error: - if(s>=0) close(s); - if(extra_fd>=0) close(extra_fd); + if(s >= 0) + close(s); + if(extra_fd >= 0) + close(extra_fd); return -1; } - -void free_ctrl_socket_list(struct ctrl_socket* l) +void free_ctrl_socket_list(struct ctrl_socket *l) { - struct ctrl_socket* nxt; - - for (;l; l=nxt){ - nxt=l->next; - if (l->data) + struct ctrl_socket *nxt; + + for(; l; l = nxt) { + nxt = l->next; + if(l->data) ctl_free(l->data); ctl_free(l); } diff --git a/src/modules/ctl/ctrl_socks.h b/src/modules/ctl/ctrl_socks.h index ab849d40be6..df217268754 100644 --- a/src/modules/ctl/ctrl_socks.h +++ b/src/modules/ctl/ctrl_socks.h @@ -28,57 +28,60 @@ #include "init_socks.h" +enum payload_proto +{ + P_BINRPC, + P_FIFO +}; -enum payload_proto { P_BINRPC , P_FIFO }; - -struct id_list{ - char* name; +struct id_list +{ + char *name; enum socket_protos proto; enum payload_proto data_proto; int port; - char* buf; /* name points somewhere here */ - struct id_list* next; + char *buf; /* name points somewhere here */ + struct id_list *next; }; -union sockaddr_u{ +union sockaddr_u +{ union sockaddr_union sa_in; struct sockaddr_un sa_un; }; - /* list of control sockets */ -struct ctrl_socket{ +struct ctrl_socket +{ int fd; int write_fd; /* used only by fifo */ enum socket_protos transport; enum payload_proto p_proto; - char* name; + char *name; int port; - struct ctrl_socket* next; + struct ctrl_socket *next; union sockaddr_u u; void *data; /* extra data, socket dependent */ }; +struct id_list *parse_listen_id(char *, int, enum socket_protos); -struct id_list* parse_listen_id(char*, int, enum socket_protos); - -int init_ctrl_sockets(struct ctrl_socket** c_lst, struct id_list* lst, - int def_port, int perm, int uid, int gid); -void free_id_list(struct id_list*); -void free_ctrl_socket_list(struct ctrl_socket* l); +int init_ctrl_sockets(struct ctrl_socket **c_lst, struct id_list *lst, + int def_port, int perm, int uid, int gid); +void free_id_list(struct id_list *); +void free_ctrl_socket_list(struct ctrl_socket *l); -inline static char* payload_proto_name(enum payload_proto p) +inline static char *payload_proto_name(enum payload_proto p) { - switch(p){ + switch(p) { case P_BINRPC: return "binrpc"; case P_FIFO: return "fifo"; - default: - ; + default:; } return ""; } diff --git a/src/modules/ctl/fifo_server.c b/src/modules/ctl/fifo_server.c index 0d9e7bcbe03..f3655aa9074 100644 --- a/src/modules/ctl/fifo_server.c +++ b/src/modules/ctl/fifo_server.c @@ -91,39 +91,47 @@ #include "ctl.h" -#define MAX_FIFO_COMMAND 128 /* Maximum length of a FIFO server command */ -#define MAX_CONSUME_BUFFER 1024 /* Buffer dimensions for FIFO server */ -#define MAX_LINE_BUFFER 2048 /* Maximum parameter line length */ -#define DEFAULT_REPLY_RETRIES 4 /* Default number of reply write attempts */ -#define DEFAULT_REPLY_WAIT 80000 /* How long we should wait for the client, in micro seconds */ -#define DEFAULT_FIFO_DIR "/tmp/" /* Where reply pipes may be opened */ -#define MAX_MSG_CHUNKS 1024 /* maximum message pieces */ -#define FIFO_TX_TIMEOUT 200 /* maximum time to block writing to +#define MAX_FIFO_COMMAND 128 /* Maximum length of a FIFO server command */ +#define MAX_CONSUME_BUFFER 1024 /* Buffer dimensions for FIFO server */ +#define MAX_LINE_BUFFER 2048 /* Maximum parameter line length */ +#define DEFAULT_REPLY_RETRIES 4 /* Default number of reply write attempts */ +#define DEFAULT_REPLY_WAIT \ + 80000 /* How long we should wait for the client, in micro seconds */ +#define DEFAULT_FIFO_DIR "/tmp/" /* Where reply pipes may be opened */ +#define MAX_MSG_CHUNKS 1024 /* maximum message pieces */ +#define FIFO_TX_TIMEOUT \ + 200 /* maximum time to block writing to the fifo */ /* readline from a buffer helper */ -struct readline_handle{ - char* s; /* buffer start */ - char* end; /* end */ - char* crt; /* crt. pos */ +struct readline_handle +{ + char *s; /* buffer start */ + char *end; /* end */ + char *crt; /* crt. pos */ }; -enum text_flags { - CHUNK_SEEN = (1 << 0), - CHUNK_POSITIONAL = (1 << 1), /* Positional parameter, should be followed by \n */ - CHUNK_MEMBER_NAME = (1 << 2), /* Struct member name, should be followed by : */ - CHUNK_MEMBER_VALUE = (1 << 3) /* Struct member value, should be followed by , if +enum text_flags +{ + CHUNK_SEEN = (1 << 0), + CHUNK_POSITIONAL = + (1 << 1), /* Positional parameter, should be followed by \n */ + CHUNK_MEMBER_NAME = + (1 << 2), /* Struct member name, should be followed by : */ + CHUNK_MEMBER_VALUE = + (1 << 3) /* Struct member value, should be followed by , if * there is another member name and \n if not */ }; - + /* * Generit text chunk. Flags attribute contains arbitrary flags */ -struct text_chunk { +struct text_chunk +{ unsigned char flags; str s; - struct text_chunk* next; + struct text_chunk *next; void *ctx; /* context, which must be passed along */ }; @@ -131,57 +139,66 @@ struct text_chunk { /* * This is the parameter if rpc_struct_add */ -struct rpc_struct_out { - struct rpc_context* ctx; - struct text_chunk* line; +struct rpc_struct_out +{ + struct rpc_context *ctx; + struct text_chunk *line; }; -struct rpc_struct { - struct rpc_context* ctx; - struct text_chunk* names; /* Names of elements */ - struct text_chunk* values; /* Element values as strings */ - struct rpc_struct* next; +struct rpc_struct +{ + struct rpc_context *ctx; + struct text_chunk *names; /* Names of elements */ + struct text_chunk *values; /* Element values as strings */ + struct rpc_struct *next; }; /* * Context structure containing state of processing */ -typedef struct rpc_context { - char* method; /* Request method name */ - char* reply_file; /* Full path and name to the reply FIFO file */ - int reply_sent; /* This flag ensures that we do not send a reply twice */ - int code; /* Reply code */ - char* reason; /* Reason phrase */ - struct text_chunk* body; /* First line to be appended as reply body */ - struct text_chunk* last; /* Last body line */ - struct text_chunk* strs; /* Strings to be collected at the end of processing */ - struct rpc_struct* structs; /* Structures to be collected at the end of processing */ +typedef struct rpc_context +{ + char *method; /* Request method name */ + char *reply_file; /* Full path and name to the reply FIFO file */ + int reply_sent; /* This flag ensures that we do not send a reply twice */ + int code; /* Reply code */ + char *reason; /* Reason phrase */ + struct text_chunk *body; /* First line to be appended as reply body */ + struct text_chunk *last; /* Last body line */ + struct text_chunk + *strs; /* Strings to be collected at the end of processing */ + struct rpc_struct + *structs; /* Structures to be collected at the end of processing */ struct readline_handle read_h; - struct send_handle* send_h; + struct send_handle *send_h; int line_no; } rpc_ctx_t; - - -char* fifo_dir = DEFAULT_FIFO_DIR; /* dir where reply fifos are +char *fifo_dir = DEFAULT_FIFO_DIR; /* dir where reply fifos are allowed */ -int fifo_reply_retries = DEFAULT_REPLY_RETRIES; -int fifo_reply_wait = DEFAULT_REPLY_WAIT; +int fifo_reply_retries = DEFAULT_REPLY_RETRIES; +int fifo_reply_wait = DEFAULT_REPLY_WAIT; -static rpc_t func_param; /* Pointers to implementation of RPC functions */ +static rpc_t func_param; /* Pointers to implementation of RPC functions */ -static int rpc_send (rpc_ctx_t* ctx); /* Send the reply to the client */ -static void rpc_fault (rpc_ctx_t* ctx, int code, char* fmt, ...); /* Signal a failure to the client */ -static int rpc_add (rpc_ctx_t* ctx, char* fmt, ...); /* Add a new piece of data to the result */ -static int rpc_scan (rpc_ctx_t* ctx, char* fmt, ...); /* Retrieve request parameters */ -static int rpc_rpl_printf (rpc_ctx_t* ctx, char* fmt, ...); /* Add printf-like formatted data to the result set */ -static int rpc_struct_add (struct text_chunk* s, char* fmt, ...); /* Create a new structure */ -static int rpc_struct_scan (struct rpc_struct* s, char* fmt, ...); /* Scan attributes of a structure */ -static int rpc_struct_printf(struct text_chunk* s, char* name, char* fmt, ...); +static int rpc_send(rpc_ctx_t *ctx); /* Send the reply to the client */ +static void rpc_fault(rpc_ctx_t *ctx, int code, char *fmt, + ...); /* Signal a failure to the client */ +static int rpc_add(rpc_ctx_t *ctx, char *fmt, + ...); /* Add a new piece of data to the result */ +static int rpc_scan( + rpc_ctx_t *ctx, char *fmt, ...); /* Retrieve request parameters */ +static int rpc_rpl_printf(rpc_ctx_t *ctx, char *fmt, + ...); /* Add printf-like formatted data to the result set */ +static int rpc_struct_add( + struct text_chunk *s, char *fmt, ...); /* Create a new structure */ +static int rpc_struct_scan(struct rpc_struct *s, char *fmt, + ...); /* Scan attributes of a structure */ +static int rpc_struct_printf(struct text_chunk *s, char *name, char *fmt, ...); /* @@ -196,11 +213,11 @@ static int rpc_struct_printf(struct text_chunk* s, char* name, char* fmt, ...); * 0 then only line delimiters, tab and zero will be * escaped. */ -static void escape(str* dst, char* r, int len, int all) +static void escape(str *dst, char *r, int len, int all) { int i; - char* w; - if (!len) { + char *w; + if(!len) { dst->len = 0; return; } @@ -208,28 +225,45 @@ static void escape(str* dst, char* r, int len, int all) w = dst->s; for(i = 0; i < len; i++) { switch(r[i]) { - case '\n': *w++ = '\\'; *w++ = 'n'; break; - case '\r': *w++ = '\\'; *w++ = 'r'; break; - case '\t': *w++ = '\\'; *w++ = 't'; break; - case '\\': *w++ = '\\'; *w++ = '\\'; break; - case '\0': *w++ = '\\'; *w++ = '0'; break; - case ':': - if (all) { + case '\n': *w++ = '\\'; - *w++ = 'o'; - } else *w++ = r[i]; - break; - - case ',': - if (all) { + *w++ = 'n'; + break; + case '\r': *w++ = '\\'; - *w++ = 'c'; - } else *w++ = r[i]; - break; + *w++ = 'r'; + break; + case '\t': + *w++ = '\\'; + *w++ = 't'; + break; + case '\\': + *w++ = '\\'; + *w++ = '\\'; + break; + case '\0': + *w++ = '\\'; + *w++ = '0'; + break; + case ':': + if(all) { + *w++ = '\\'; + *w++ = 'o'; + } else + *w++ = r[i]; + break; - default: - *w++ = r[i]; - break; + case ',': + if(all) { + *w++ = '\\'; + *w++ = 'c'; + } else + *w++ = r[i]; + break; + + default: + *w++ = r[i]; + break; } } dst->len = w - dst->s; @@ -246,12 +280,12 @@ static void escape(str* dst, char* r, int len, int all) * Return value 0 indicates success, -1 indicates * formatting error. */ -static int unescape(str* dst, char* r, int len) +static int unescape(str *dst, char *r, int len) { - char* w; + char *w; int i; - if (!len) { + if(!len) { dst->len = 0; return 0; } @@ -259,22 +293,39 @@ static int unescape(str* dst, char* r, int len) w = dst->s; for(i = 0; i < len; i++) { switch(*r) { - case '\\': - r++; - i++; - switch(*r++) { - case '\\': *w++ = '\\'; break; - case 'n': *w++ = '\n'; break; - case 'r': *w++ = '\r'; break; - case 't': *w++ = '\t'; break; - case '0': *w++ = '\0'; break; - case 'c': *w++ = ':'; break; /* Structure delimiter */ - case 'o': *w++ = ','; break; /* Structure delimiter */ - default: return -1; - } - break; + case '\\': + r++; + i++; + switch(*r++) { + case '\\': + *w++ = '\\'; + break; + case 'n': + *w++ = '\n'; + break; + case 'r': + *w++ = '\r'; + break; + case 't': + *w++ = '\t'; + break; + case '0': + *w++ = '\0'; + break; + case 'c': + *w++ = ':'; + break; /* Structure delimiter */ + case 'o': + *w++ = ','; + break; /* Structure delimiter */ + default: + return -1; + } + break; - default: *w++ = *r++; break; + default: + *w++ = *r++; + break; } } dst->len = w - dst->s; @@ -286,18 +337,19 @@ static int unescape(str* dst, char* r, int len) * Create a new text chunk, the input text will * be escaped. */ -struct text_chunk* new_chunk_escape(str* src, int escape_all) +struct text_chunk *new_chunk_escape(str *src, int escape_all) { - struct text_chunk* l; - if (!src) return 0; + struct text_chunk *l; + if(!src) + return 0; - l = ctl_malloc(sizeof(struct text_chunk)); - if (!l) { + l = ctl_malloc(sizeof(struct text_chunk)); + if(!l) { ERR("No Memory Left\n"); return 0; } l->s.s = ctl_malloc(src->len * 2 + 1); - if (!l->s.s) { + if(!l->s.s) { ERR("No Memory Left\n"); ctl_free(l); return 0; @@ -314,18 +366,19 @@ struct text_chunk* new_chunk_escape(str* src, int escape_all) * will not be escaped. The function returns * 0 on an error */ -struct text_chunk* new_chunk(str* src) +struct text_chunk *new_chunk(str *src) { - struct text_chunk* l; - if (!src) return 0; + struct text_chunk *l; + if(!src) + return 0; - l = ctl_malloc(sizeof(struct text_chunk)); - if (!l) { + l = ctl_malloc(sizeof(struct text_chunk)); + if(!l) { ERR("No Memory Left\n"); return 0; } l->s.s = ctl_malloc(src->len + 1); - if (!l->s.s) { + if(!l->s.s) { ERR("No Memory Left\n"); ctl_free(l); return 0; @@ -343,25 +396,26 @@ struct text_chunk* new_chunk(str* src) * Create a new text chunk, the input text * will be unescaped first. */ -struct text_chunk* new_chunk_unescape(str* src) +struct text_chunk *new_chunk_unescape(str *src) { - struct text_chunk* l; - if (!src) return 0; + struct text_chunk *l; + if(!src) + return 0; - l = ctl_malloc(sizeof(struct text_chunk)); - if (!l) { + l = ctl_malloc(sizeof(struct text_chunk)); + if(!l) { ERR("No Memory Left\n"); return 0; } l->s.s = ctl_malloc(src->len + 1); - if (!l->s.s) { + if(!l->s.s) { ERR("No Memory Left\n"); ctl_free(l); return 0; } l->next = 0; l->flags = 0; - if (unescape(&l->s, src->s, src->len) < 0) { + if(unescape(&l->s, src->s, src->len) < 0) { ctl_free(l->s.s); ctl_free(l); return 0; @@ -371,18 +425,21 @@ struct text_chunk* new_chunk_unescape(str* src) } -static void free_chunk(struct text_chunk* c) +static void free_chunk(struct text_chunk *c) { - if (c && c->s.s) ctl_free(c->s.s); - if (c) ctl_free(c); + if(c && c->s.s) + ctl_free(c->s.s); + if(c) + ctl_free(c); } -static void free_struct(struct rpc_struct* s) +static void free_struct(struct rpc_struct *s) { - struct text_chunk* c; + struct text_chunk *c; - if (!s) return; + if(!s) + return; while(s->names) { c = s->names; s->names = s->names->next; @@ -402,63 +459,67 @@ static void free_struct(struct rpc_struct* s) /* * Parse a structure */ -static struct rpc_struct* new_struct(rpc_ctx_t* ctx, str* line) +static struct rpc_struct *new_struct(rpc_ctx_t *ctx, str *line) { - char* comma, *colon; - struct rpc_struct* s; + char *comma, *colon; + struct rpc_struct *s; str left, right = STR_NULL, name, value; - struct text_chunk* n, *v; + struct text_chunk *n, *v; - if (!line->len) { - rpc_fault(ctx, 400, "Line %d Empty - Structure Expected", - ctx->line_no); + if(!line->len) { + rpc_fault(ctx, 400, "Line %d Empty - Structure Expected", ctx->line_no); return 0; } - s = (struct rpc_struct*)ctl_malloc(sizeof(struct rpc_struct)); - if (!s) { + s = (struct rpc_struct *)ctl_malloc(sizeof(struct rpc_struct)); + if(!s) { rpc_fault(ctx, 500, "Internal Server Error (No Memory Left)"); return 0; } memset(s, 0, sizeof(struct rpc_struct)); s->ctx = ctx; - + left = *line; do { comma = q_memchr(left.s, ',', left.len); - if (comma) { + if(comma) { right.s = comma + 1; right.len = left.len - (comma - left.s) - 1; left.len = comma - left.s; } - - /* Split the record to name and value */ + + /* Split the record to name and value */ colon = q_memchr(left.s, ':', left.len); - if (!colon) { + if(!colon) { rpc_fault(ctx, 400, "Colon missing in struct on line %d", - ctx->line_no); - goto err;; + ctx->line_no); + goto err; + ; } name.s = left.s; name.len = colon - name.s; value.s = colon + 1; value.len = left.len - (colon - left.s) - 1; - - /* Create name chunk */ + + /* Create name chunk */ n = new_chunk_unescape(&name); - if (!n) { - rpc_fault(ctx, 400, "Error while processing struct member '%.*s' " - "on line %d", name.len, ZSW(name.s), ctx->line_no); + if(!n) { + rpc_fault(ctx, 400, + "Error while processing struct member '%.*s' " + "on line %d", + name.len, ZSW(name.s), ctx->line_no); goto err; } n->next = s->names; s->names = n; - /* Create value chunk */ + /* Create value chunk */ v = new_chunk_unescape(&value); - if (!v) { - rpc_fault(ctx, 400, "Error while processing struct member '%.*s'" - " on line %d", name.len, ZSW(name.s), ctx->line_no); + if(!v) { + rpc_fault(ctx, 400, + "Error while processing struct member '%.*s'" + " on line %d", + name.len, ZSW(name.s), ctx->line_no); goto err; } v->next = s->values; @@ -468,8 +529,9 @@ static struct rpc_struct* new_struct(rpc_ctx_t* ctx, str* line) } while(comma); return s; - err: - if (s) free_struct(s); +err: + if(s) + free_struct(s); return 0; } @@ -480,26 +542,28 @@ static struct rpc_struct* new_struct(rpc_ctx_t* ctx, str* line) * * Returns -1 on error, 0 on success */ -static int read_line(char** b, int* read, struct readline_handle* rh) +static int read_line(char **b, int *read, struct readline_handle *rh) { - char* eol; - char* trim; - - if (rh->crt>=rh->end){ + char *eol; + char *trim; + + if(rh->crt >= rh->end) { /* end, nothing more to read */ return -1; } - for(eol=rh->crt; (eolend) && (*eol!='\n'); eol++); - *eol=0; - trim=eol; + for(eol = rh->crt; (eol < rh->end) && (*eol != '\n'); eol++) + ; + *eol = 0; + trim = eol; /* trim spaces at the end */ - for(trim=eol;(trim>rh->crt) && - ((*trim=='\r')||(*trim==' ')||(*trim=='\t')); trim--){ - *trim=0; + for(trim = eol; (trim > rh->crt) + && ((*trim == '\r') || (*trim == ' ') || (*trim == '\t')); + trim--) { + *trim = 0; } - *b=rh->crt; - *read = (int)(trim-rh->crt); - rh->crt=eol+1; + *b = rh->crt; + *read = (int)(trim - rh->crt); + rh->crt = eol + 1; return 0; } @@ -511,23 +575,22 @@ static int read_line(char** b, int* read, struct readline_handle* rh) * The result is allocated using ctl_malloc and thus * has to be freed using ctl_free */ -static char *trim_filename(char * file) +static char *trim_filename(char *file) { int prefix_len, fn_len; char *new_fn; - + /* we only allow files in "/tmp" -- any directory * changes are not welcome */ - if (strchr(file, '.') || strchr(file, '/') - || strchr(file, '\\')) { - ERR("Forbidden filename: %s\n" - , file); + if(strchr(file, '.') || strchr(file, '/') || strchr(file, '\\')) { + ERR("Forbidden filename: %s\n", file); return 0; } - prefix_len = strlen(fifo_dir); fn_len = strlen(file); + prefix_len = strlen(fifo_dir); + fn_len = strlen(file); new_fn = ctl_malloc(prefix_len + fn_len + 1); - if (new_fn == 0) { + if(new_fn == 0) { ERR("No memory left\n"); return 0; } @@ -539,53 +602,49 @@ static char *trim_filename(char * file) } - /* reply fifo security checks: * checks if fd is a fifo, is not hardlinked and it's not a softlink * opened file descriptor + file name (for soft link check) * returns 0 if ok, <0 if not */ -static int fifo_check(int fd, char* fname) +static int fifo_check(int fd, char *fname) { struct stat fst; struct stat lst; - - if (fstat(fd, &fst) < 0) { - ERR("fstat failed: %s\n", - strerror(errno)); + + if(fstat(fd, &fst) < 0) { + ERR("fstat failed: %s\n", strerror(errno)); return -1; } - /* check if fifo */ - if (!S_ISFIFO(fst.st_mode)){ + /* check if fifo */ + if(!S_ISFIFO(fst.st_mode)) { ERR("%s is not a fifo\n", fname); return -1; } - /* check if hard-linked */ - if (fst.st_nlink > 1) { - ERR("%s is hard-linked %d times\n", - fname, (unsigned)fst.st_nlink); + /* check if hard-linked */ + if(fst.st_nlink > 1) { + ERR("%s is hard-linked %d times\n", fname, (unsigned)fst.st_nlink); return -1; } - - /* lstat to check for soft links */ - if (lstat(fname, &lst) < 0) { - ERR("lstat failed: %s\n", - strerror(errno)); + + /* lstat to check for soft links */ + if(lstat(fname, &lst) < 0) { + ERR("lstat failed: %s\n", strerror(errno)); return -1; } - if (S_ISLNK(lst.st_mode)) { + if(S_ISLNK(lst.st_mode)) { ERR("%s is a soft link\n", fname); return -1; } - /* if this is not a symbolic link, check to see if the inode didn't + /* if this is not a symbolic link, check to see if the inode didn't * change to avoid possible sym.link, rm sym.link & replace w/ fifo race */ - if ((lst.st_dev != fst.st_dev) || (lst.st_ino != fst.st_ino)) { - ERR("inode/dev number differ : %d %d (%s)\n", - (int)fst.st_ino, (int)lst.st_ino, fname); + if((lst.st_dev != fst.st_dev) || (lst.st_ino != fst.st_ino)) { + ERR("inode/dev number differ : %d %d (%s)\n", (int)fst.st_ino, + (int)lst.st_ino, fname); return -1; } - /* success */ + /* success */ return 0; } @@ -596,113 +655,110 @@ static int fifo_check(int fd, char* fname) */ static int open_reply_pipe(char *pipe_name) { - + int fifofd; int flags; - + int retries = fifo_reply_retries; - - fifofd=-1; - if (!pipe_name || *pipe_name == 0) { + + fifofd = -1; + if(!pipe_name || *pipe_name == 0) { DBG("No file to write to about missing cmd\n"); goto error; } - - tryagain: - /* open non-blocking to make sure that a broken client will not + +tryagain: + /* open non-blocking to make sure that a broken client will not * block the FIFO server forever */ fifofd = open(pipe_name, O_WRONLY | O_NONBLOCK); - if (fifofd == -1) { - /* retry several times if client is not yet ready for getting + if(fifofd == -1) { + /* retry several times if client is not yet ready for getting * feedback via a reply pipe */ - if (errno == ENXIO) { - /* give up on the client - we can't afford server blocking */ - if (retries == 0) { + if(errno == ENXIO) { + /* give up on the client - we can't afford server blocking */ + if(retries == 0) { ERR("No client at %s\n", pipe_name); goto error; } - /* don't be noisy on the very first try */ - if (retries != fifo_reply_retries) { + /* don't be noisy on the very first try */ + if(retries != fifo_reply_retries) { DBG("Retry countdown: %d\n", retries); } sleep_us(fifo_reply_wait); retries--; goto tryagain; } - /* some other opening error */ - ERR("Open error (%s): %s\n", - pipe_name, strerror(errno)); + /* some other opening error */ + ERR("Open error (%s): %s\n", pipe_name, strerror(errno)); goto error; } - /* security checks: is this really a fifo?, is + /* security checks: is this really a fifo?, is * it hardlinked? is it a soft link? */ - if (fifo_check(fifofd, pipe_name) < 0) goto error; - - /* we want server blocking for big writes */ - if ((flags = fcntl(fifofd, F_GETFL, 0)) < 0) { + if(fifo_check(fifofd, pipe_name) < 0) + goto error; + + /* we want server blocking for big writes */ + if((flags = fcntl(fifofd, F_GETFL, 0)) < 0) { ERR("(%s): getfl failed: %s\n", pipe_name, strerror(errno)); goto error; } flags &= ~O_NONBLOCK; - if (fcntl(fifofd, F_SETFL, flags) < 0) { - ERR("(%s): setfl cntl failed: %s\n", - pipe_name, strerror(errno)); + if(fcntl(fifofd, F_SETFL, flags) < 0) { + ERR("(%s): setfl cntl failed: %s\n", pipe_name, strerror(errno)); goto error; } - + return fifofd; - error: - if (fifofd!=-1) close(fifofd); +error: + if(fifofd != -1) + close(fifofd); return -1; } - - /* * Man FIFO routine running in the FIFO * processes requests received * through the FIFO file repeatedly */ -int fifo_process(char* msg_buf, int size, int* bytes_needed, void *sh, - void** saved_state) +int fifo_process(char *msg_buf, int size, int *bytes_needed, void *sh, + void **saved_state) { - rpc_exportx_t* exp; - char* buf; + rpc_exportx_t *exp; + char *buf; int line_len; char *file_sep; - struct text_chunk* p; - struct rpc_struct* s; + struct text_chunk *p; + struct rpc_struct *s; int r; int req_size; static rpc_ctx_t context; unsigned int rdata; - DBG("process_fifo: called with %d bytes, offset %d: %.*s\n", - size, (int)(long)*saved_state, size, msg_buf); + DBG("process_fifo: called with %d bytes, offset %d: %.*s\n", size, + (int)(long)*saved_state, size, msg_buf); /* search for the end of the request (\n\r) */ - if (size < 6){ /* min fifo request */ - *bytes_needed=6-size; + if(size < 6) { /* min fifo request */ + *bytes_needed = 6 - size; return 0; /* we want more bytes, nothing processed */ } - for (r=1+(int)(long)*saved_state;r end of request */ - req_size=r; + req_size = r; goto process; } } /* no end of request found => ask for more bytes */ - *bytes_needed=1; + *bytes_needed = 1; /* save current offset, to optimize search */ - *saved_state=(void*)(long)(r-1); + *saved_state = (void *)(long)(r - 1); return 0; /* we want again the whole buffer */ process: - - DBG("process_fifo %d bytes request: %.*s\n", - req_size, req_size, msg_buf); + + DBG("process_fifo %d bytes request: %.*s\n", req_size, req_size, msg_buf); file_sep = 0; context.method = 0; context.reply_file = 0; @@ -712,98 +768,97 @@ int fifo_process(char* msg_buf, int size, int* bytes_needed, void *sh, context.reply_sent = 0; context.last = 0; context.line_no = 0; - context.read_h.s=msg_buf; - context.read_h.end=msg_buf+size; - context.read_h.crt=msg_buf; - context.send_h=(struct send_handle*)sh; - /* commands must look this way '::[filename]' */ - if (read_line(&buf, &line_len, &context.read_h) < 0) { - /* line breaking must have failed -- consume the rest + context.read_h.s = msg_buf; + context.read_h.end = msg_buf + size; + context.read_h.crt = msg_buf; + context.send_h = (struct send_handle *)sh; + /* commands must look this way '::[filename]' */ + if(read_line(&buf, &line_len, &context.read_h) < 0) { + /* line breaking must have failed -- consume the rest * and proceed to a new request */ - ERR("Command expected\n"); - goto consume; - } - context.line_no++; - if (line_len == 0) { - DBG("Empty command received\n"); - goto consume; - } - if (line_len < 3) { - ERR("Command must have at least 3 chars\n"); - goto consume; - } - if (*buf != CMD_SEPARATOR) { - ERR("Command must begin with %c: %.*s\n", - CMD_SEPARATOR, line_len, buf); - goto consume; - } - - context.method = buf + 1; - file_sep = strchr(context.method, CMD_SEPARATOR); - if (file_sep == NULL) { - ERR("File separator missing\n"); - goto consume; - } - if (file_sep == context.method) { - ERR("Empty command\n"); - goto consume; - } - if (*(file_sep + 1) == 0) context.reply_file = NULL; - else { - context.reply_file = file_sep + 1; - context.reply_file = trim_filename(context.reply_file); - if (context.reply_file == 0) { - ERR("Trimming filename\n"); - goto consume; - } - } - /* make command zero-terminated */ - *file_sep = 0; + ERR("Command expected\n"); + goto consume; + } + context.line_no++; + if(line_len == 0) { + DBG("Empty command received\n"); + goto consume; + } + if(line_len < 3) { + ERR("Command must have at least 3 chars\n"); + goto consume; + } + if(*buf != CMD_SEPARATOR) { + ERR("Command must begin with %c: %.*s\n", CMD_SEPARATOR, line_len, buf); + goto consume; + } - exp = rpc_lookupx(context.method, strlen(context.method), &rdata); - if ((exp==NULL) || (exp->r.function==NULL)){ - DBG("Command %s not found\n", context.method); - rpc_fault(&context, 500, "Command '%s' not found", context.method); - goto consume; - } - if (rdata & RPC_EXEC_DELTA) { - LM_ERR("execution of command [%s] is limited by delta [%d]\n", - context.method, ksr_rpc_exec_delta); - rpc_fault(&context, 500, "Command Executed Too Fast"); + context.method = buf + 1; + file_sep = strchr(context.method, CMD_SEPARATOR); + if(file_sep == NULL) { + ERR("File separator missing\n"); + goto consume; + } + if(file_sep == context.method) { + ERR("Empty command\n"); + goto consume; + } + if(*(file_sep + 1) == 0) + context.reply_file = NULL; + else { + context.reply_file = file_sep + 1; + context.reply_file = trim_filename(context.reply_file); + if(context.reply_file == 0) { + ERR("Trimming filename\n"); goto consume; } + } + /* make command zero-terminated */ + *file_sep = 0; + + exp = rpc_lookupx(context.method, strlen(context.method), &rdata); + if((exp == NULL) || (exp->r.function == NULL)) { + DBG("Command %s not found\n", context.method); + rpc_fault(&context, 500, "Command '%s' not found", context.method); + goto consume; + } + if(rdata & RPC_EXEC_DELTA) { + LM_ERR("execution of command [%s] is limited by delta [%d]\n", + context.method, ksr_rpc_exec_delta); + rpc_fault(&context, 500, "Command Executed Too Fast"); + goto consume; + } - exp->r.function(&func_param, &context); + exp->r.function(&func_param, &context); - consume: - if (!context.reply_sent) { - rpc_send(&context); - } - - if (context.reply_file) { - ctl_free(context.reply_file); - context.reply_file = 0; - } +consume: + if(!context.reply_sent) { + rpc_send(&context); + } - /* Collect garbage (unescaped strings and structures) */ - while(context.strs) { - p = context.strs; - context.strs = context.strs->next; - free_chunk(p); - } + if(context.reply_file) { + ctl_free(context.reply_file); + context.reply_file = 0; + } - while(context.structs) { - s = context.structs; - context.structs = context.structs->next; - free_struct(s); - } + /* Collect garbage (unescaped strings and structures) */ + while(context.strs) { + p = context.strs; + context.strs = context.strs->next; + free_chunk(p); + } - *bytes_needed=0; - DBG("Command consumed\n"); - DBG("process_fifo: returning %d, bytes_needed 0\n", req_size+1); - return req_size+1; /* all was processed (including terminating \n)*/ + while(context.structs) { + s = context.structs; + context.structs = context.structs->next; + free_struct(s); + } + *bytes_needed = 0; + DBG("Command consumed\n"); + DBG("process_fifo: returning %d, bytes_needed 0\n", req_size + 1); + return req_size + 1; /* all was processed (including terminating \n)*/ } @@ -813,81 +868,80 @@ int fifo_process(char* msg_buf, int size, int* bytes_needed, void *sh, * make it secure. This function must be executed from mod_init. * This ensures that it has sufficient privileges. */ -int init_fifo_fd(char* fifo, int fifo_mode, int fifo_uid, int fifo_gid, - int* fifo_write) +int init_fifo_fd( + char *fifo, int fifo_mode, int fifo_uid, int fifo_gid, int *fifo_write) { struct stat filestat; int n; long opt; int fifo_read = -1; - - if (fifo == NULL) { + + if(fifo == NULL) { ERR("null fifo: no fifo will be opened\n"); /* error null fifo */ return -1; } - if (strlen(fifo) == 0) { + if(strlen(fifo) == 0) { ERR("empty fifo: fifo disabled\n"); return -1; } - + DBG("Opening fifo...\n"); n = stat(fifo, &filestat); - if (n == 0) { + if(n == 0) { /* FIFO exist, delete it (safer) */ - if (unlink(fifo) < 0) { + if(unlink(fifo) < 0) { ERR("Cannot delete old fifo (%s):" - " %s\n", fifo, strerror(errno)); + " %s\n", + fifo, strerror(errno)); return -1; } - } else if (n < 0 && errno != ENOENT) { - ERR("FIFO stat failed: %s\n", - strerror(errno)); + } else if(n < 0 && errno != ENOENT) { + ERR("FIFO stat failed: %s\n", strerror(errno)); } /* create FIFO ... */ - if ((mkfifo(fifo, fifo_mode) < 0)) { + if((mkfifo(fifo, fifo_mode) < 0)) { ERR("Can't create FIFO: " - "%s (mode=%d)\n", - strerror(errno), fifo_mode); + "%s (mode=%d)\n", + strerror(errno), fifo_mode); return -1; - } - DBG("FIFO created @ %s\n", fifo ); - if ((chmod(fifo, fifo_mode) < 0)) { - ERR("Can't chmod FIFO: %s (mode=%d)\n", - strerror(errno), fifo_mode); + } + DBG("FIFO created @ %s\n", fifo); + if((chmod(fifo, fifo_mode) < 0)) { + ERR("Can't chmod FIFO: %s (mode=%d)\n", strerror(errno), fifo_mode); return -1; } - if ((fifo_uid != -1) || (fifo_gid != -1)) { - if (chown(fifo, fifo_uid, fifo_gid) < 0) { + if((fifo_uid != -1) || (fifo_gid != -1)) { + if(chown(fifo, fifo_uid, fifo_gid) < 0) { ERR("Failed to change the owner/group for %s to %d.%d; %s[%d]\n", - fifo, fifo_uid, fifo_gid, strerror(errno), errno); + fifo, fifo_uid, fifo_gid, strerror(errno), errno); return -1; } } - + DBG("fifo %s opened, mode=%d\n", fifo, fifo_mode); - + fifo_read = open(fifo, O_RDONLY | O_NONBLOCK, 0); - if (fifo_read < 0) { + if(fifo_read < 0) { ERR("fifo_read did not open: %s\n", strerror(errno)); return -1; } /* make sure the read fifo will not close */ *fifo_write = open(fifo, O_WRONLY | O_NONBLOCK, 0); - if (*fifo_write < 0) { + if(*fifo_write < 0) { ERR("fifo_write did not open: %s\n", strerror(errno)); close(fifo_read); return -1; } /* set read fifo blocking mode */ - if ((opt = fcntl(fifo_read, F_GETFL)) == -1) { + if((opt = fcntl(fifo_read, F_GETFL)) == -1) { ERR("fcntl(F_GETFL) failed: %s [%d]\n", strerror(errno), errno); close(fifo_read); close(*fifo_write); *fifo_write = -1; return -1; } - if (fcntl(fifo_read, F_SETFL, opt & (~O_NONBLOCK)) == -1) { + if(fcntl(fifo_read, F_SETFL, opt & (~O_NONBLOCK)) == -1) { ERR("fcntl(F_SETFL) failed: %s [%d]\n", strerror(errno), errno); close(fifo_read); close(*fifo_write); @@ -898,7 +952,6 @@ int init_fifo_fd(char* fifo, int fifo_mode, int fifo_uid, int fifo_gid, } - int fifo_rpc_init() { memset(&func_param, 0, sizeof(func_param)); @@ -910,40 +963,39 @@ int fifo_rpc_init() func_param.struct_add = (rpc_struct_add_f)rpc_struct_add; /* use rpc_struct_add for array_add */ func_param.array_add = (rpc_array_add_f)rpc_struct_add; - func_param.struct_scan = (rpc_struct_scan_f)rpc_struct_scan; + func_param.struct_scan = (rpc_struct_scan_f)rpc_struct_scan; func_param.struct_printf = (rpc_struct_printf_f)rpc_struct_printf; return 0; } - /* * Close and unlink the FIFO file */ -void destroy_fifo(int read_fd, int w_fd, char* fname) +void destroy_fifo(int read_fd, int w_fd, char *fname) { - if (read_fd!=-1) + if(read_fd != -1) close(read_fd); - if(w_fd!=-1) + if(w_fd != -1) close(w_fd); /* if FIFO was created, delete it */ - if (fname && strlen(fname)) { - if (unlink(fname) < 0) { + if(fname && strlen(fname)) { + if(unlink(fname) < 0) { WARN("Cannot delete fifo (%s):" - " %s\n", fname, strerror(errno)); + " %s\n", + fname, strerror(errno)); } } } - #define REASON_BUF_LEN 1024 /* * An error occurred, signal it to the client */ -static void rpc_fault(rpc_ctx_t* ctx, int code, char* fmt, ...) +static void rpc_fault(rpc_ctx_t *ctx, int code, char *fmt, ...) { static char buf[REASON_BUF_LEN]; va_list ap; @@ -955,55 +1007,59 @@ static void rpc_fault(rpc_ctx_t* ctx, int code, char* fmt, ...) } -inline static int build_iovec(rpc_ctx_t* ctx, struct iovec* v, int v_size) +inline static int build_iovec(rpc_ctx_t *ctx, struct iovec *v, int v_size) { - struct text_chunk* p; + struct text_chunk *p; int r_c_len; int r; - - - + + /* reason code */ - v[0].iov_base=int2str(ctx->code, &r_c_len); - v[0].iov_len=r_c_len; - v[1].iov_base=" "; - v[1].iov_len=1; + v[0].iov_base = int2str(ctx->code, &r_c_len); + v[0].iov_len = r_c_len; + v[1].iov_base = " "; + v[1].iov_len = 1; /* reason txt */ - v[2].iov_base=ctx->reason; - v[2].iov_len=strlen(ctx->reason); - v[3].iov_base="\n"; - v[3].iov_len=1; - r=4; + v[2].iov_base = ctx->reason; + v[2].iov_len = strlen(ctx->reason); + v[3].iov_base = "\n"; + v[3].iov_len = 1; + r = 4; /* Send the body */ while(ctx->body) { p = ctx->body; ctx->body = ctx->body->next; - if (p->s.len){ - if (r>=v_size) goto error_overflow; - v[r].iov_base=p->s.s; - v[r].iov_len=p->s.len; + if(p->s.len) { + if(r >= v_size) + goto error_overflow; + v[r].iov_base = p->s.s; + v[r].iov_len = p->s.len; r++; } - if (p->flags & CHUNK_POSITIONAL) { - if (r>=v_size) goto error_overflow; - v[r].iov_base="\n"; - v[r].iov_len=1; + if(p->flags & CHUNK_POSITIONAL) { + if(r >= v_size) + goto error_overflow; + v[r].iov_base = "\n"; + v[r].iov_len = 1; r++; - } else if (p->flags & CHUNK_MEMBER_NAME) { - if (r>=v_size) goto error_overflow; - v[r].iov_base=":"; - v[r].iov_len=1; + } else if(p->flags & CHUNK_MEMBER_NAME) { + if(r >= v_size) + goto error_overflow; + v[r].iov_base = ":"; + v[r].iov_len = 1; r++; - } else if (p->flags & CHUNK_MEMBER_VALUE) { - if (p->next && p->next->flags & CHUNK_MEMBER_NAME) { - if (r>=MAX_MSG_CHUNKS) goto error_overflow; - v[r].iov_base=","; - v[r].iov_len=1; + } else if(p->flags & CHUNK_MEMBER_VALUE) { + if(p->next && p->next->flags & CHUNK_MEMBER_NAME) { + if(r >= MAX_MSG_CHUNKS) + goto error_overflow; + v[r].iov_base = ","; + v[r].iov_len = 1; r++; } else { - if (r>=v_size) goto error_overflow; - v[r].iov_base="\n"; - v[r].iov_len=1; + if(r >= v_size) + goto error_overflow; + v[r].iov_base = "\n"; + v[r].iov_len = 1; r++; } } @@ -1017,48 +1073,48 @@ inline static int build_iovec(rpc_ctx_t* ctx, struct iovec* v, int v_size) } - /* * Send a reply, either positive or negative, to the client */ -static int rpc_send(rpc_ctx_t* ctx) +static int rpc_send(rpc_ctx_t *ctx) { struct iovec v[MAX_MSG_CHUNKS]; int f; int n; int ret; /* Send the reply only once */ - if (ctx->reply_sent) return 1; - else ctx->reply_sent = 1; - - if ((n=build_iovec(ctx, v, MAX_MSG_CHUNKS))<0) + if(ctx->reply_sent) + return 1; + else + ctx->reply_sent = 1; + + if((n = build_iovec(ctx, v, MAX_MSG_CHUNKS)) < 0) goto error; - if (ctx->send_h->type==S_FIFO){ - /* Open the reply file */ + if(ctx->send_h->type == S_FIFO) { + /* Open the reply file */ f = open_reply_pipe(ctx->reply_file); - if (f == -1) { + if(f == -1) { ERR("No reply pipe %s\n", ctx->reply_file); return -1; } - ret=tsend_dgram_ev(f, v, n, FIFO_TX_TIMEOUT); + ret = tsend_dgram_ev(f, v, n, FIFO_TX_TIMEOUT); close(f); - }else{ - ret=sock_send_v(ctx->send_h, v, n); + } else { + ret = sock_send_v(ctx->send_h, v, n); } - return (ret>=0)?0:-1; + return (ret >= 0) ? 0 : -1; error: ERR("rpc_send fifo error\n"); return -1; } - /* * Add a chunk to reply */ -static void append_chunk(rpc_ctx_t* ctx, struct text_chunk* l) +static void append_chunk(rpc_ctx_t *ctx, struct text_chunk *l) { - if (!ctx->last) { + if(!ctx->last) { ctx->body = l; ctx->last = l; } else { @@ -1071,160 +1127,165 @@ static void append_chunk(rpc_ctx_t* ctx, struct text_chunk* l) /* * Convert a value to data chunk and add it to reply */ -static int print_value(rpc_ctx_t* ctx, char fmt, va_list* ap) +static int print_value(rpc_ctx_t *ctx, char fmt, va_list *ap) { - struct text_chunk* l; + struct text_chunk *l; str str_val; - str* sp; + str *sp; char buf[256]; str null_value = str_init(""); switch(fmt) { - case 'd': - case 't': - str_val.s = int2str(va_arg(*ap, int), &str_val.len); - l = new_chunk(&str_val); - if (!l) { - rpc_fault(ctx, 500, "Internal server error while processing" - " line %d", ctx->line_no); - goto err; - } - break; - - case 'f': - str_val.s = buf; - str_val.len = snprintf(buf, 256, "%f", va_arg(*ap, double)); - if (str_val.len < 0) { - rpc_fault(ctx, 400, "Error While Converting double"); - ERR("Error while converting double\n"); - goto err; - } - l = new_chunk(&str_val); - if (!l) { - rpc_fault(ctx, 500, "Internal Server Error, line %d", + case 'd': + case 't': + str_val.s = int2str(va_arg(*ap, int), &str_val.len); + l = new_chunk(&str_val); + if(!l) { + rpc_fault(ctx, 500, + "Internal server error while processing" + " line %d", ctx->line_no); - goto err; - } - break; - - case 'b': - str_val.len = 1; - str_val.s = ((va_arg(*ap, int) == 0) ? "0" : "1"); - l = new_chunk(&str_val); - if (!l) { - rpc_fault(ctx, 500, "Internal Server Error, line %d", + goto err; + } + break; + + case 'f': + str_val.s = buf; + str_val.len = snprintf(buf, 256, "%f", va_arg(*ap, double)); + if(str_val.len < 0) { + rpc_fault(ctx, 400, "Error While Converting double"); + ERR("Error while converting double\n"); + goto err; + } + l = new_chunk(&str_val); + if(!l) { + rpc_fault(ctx, 500, "Internal Server Error, line %d", ctx->line_no); - goto err; - } - break; + goto err; + } + break; - case 's': - str_val.s = va_arg(*ap, char*); - if(str_val.s!=NULL) { - str_val.len = strlen(str_val.s); - } else { - str_val = null_value; - } - l = new_chunk_escape(&str_val, 0); - if (!l) { - rpc_fault(ctx, 500, "Internal Server Error, line %d", + case 'b': + str_val.len = 1; + str_val.s = ((va_arg(*ap, int) == 0) ? "0" : "1"); + l = new_chunk(&str_val); + if(!l) { + rpc_fault(ctx, 500, "Internal Server Error, line %d", ctx->line_no); - goto err; - } - break; + goto err; + } + break; - case 'S': - sp = va_arg(*ap, str*); - if(sp!=NULL && sp->s!=NULL) { - str_val = *sp; - } else { - str_val = null_value; - } - l = new_chunk_escape(&str_val, 0); - if (!l) { - rpc_fault(ctx, 500, "Internal Server Error, line %d", - ctx->line_no); - goto err; - } - break; + case 's': + str_val.s = va_arg(*ap, char *); + if(str_val.s != NULL) { + str_val.len = strlen(str_val.s); + } else { + str_val = null_value; + } + l = new_chunk_escape(&str_val, 0); + if(!l) { + rpc_fault(ctx, 500, "Internal Server Error, line %d", + ctx->line_no); + goto err; + } + break; + + case 'S': + sp = va_arg(*ap, str *); + if(sp != NULL && sp->s != NULL) { + str_val = *sp; + } else { + str_val = null_value; + } + l = new_chunk_escape(&str_val, 0); + if(!l) { + rpc_fault(ctx, 500, "Internal Server Error, line %d", + ctx->line_no); + goto err; + } + break; - default: - rpc_fault(ctx, 500, "Bug In SER (Invalid formatting character %c)", fmt); - ERR("Invalid formatting character\n"); - goto err; + default: + rpc_fault(ctx, 500, "Bug In SER (Invalid formatting character %c)", + fmt); + ERR("Invalid formatting character\n"); + goto err; } l->flags |= CHUNK_POSITIONAL; append_chunk(ctx, l); return 0; - err: +err: return -1; } -static int rpc_add(rpc_ctx_t* ctx, char* fmt, ...) +static int rpc_add(rpc_ctx_t *ctx, char *fmt, ...) { - void** void_ptr; + void **void_ptr; va_list ap; str s = {"", 0}; - struct text_chunk* l; + struct text_chunk *l; va_start(ap, fmt); while(*fmt) { - if (*fmt == '{' || *fmt == '[') { - void_ptr = va_arg(ap, void**); + if(*fmt == '{' || *fmt == '[') { + void_ptr = va_arg(ap, void **); l = new_chunk(&s); - if (!l) { + if(!l) { rpc_fault(ctx, 500, "Internal Server Error"); goto err; } - l->ctx=ctx; + l->ctx = ctx; append_chunk(ctx, l); *void_ptr = l; } else { - if (print_value(ctx, *fmt, &ap) < 0) goto err; + if(print_value(ctx, *fmt, &ap) < 0) + goto err; } fmt++; } va_end(ap); return 0; - err: +err: va_end(ap); return -1; } #define RPC_BUF_SIZE 1024 -static int rpc_struct_printf(struct text_chunk* c, char* name, char* fmt, ...) +static int rpc_struct_printf(struct text_chunk *c, char *name, char *fmt, ...) { int n, buf_size; - char* buf; - char* buf0; + char *buf; + char *buf0; va_list ap; str s, nm; - struct text_chunk* l, *m; - rpc_ctx_t* ctx; + struct text_chunk *l, *m; + rpc_ctx_t *ctx; - ctx=(rpc_ctx_t*)c->ctx; - buf = (char*)ctl_malloc(RPC_BUF_SIZE); - if (!buf) { - rpc_fault(ctx, 500, "Internal Server Error (No memory left)"); + ctx = (rpc_ctx_t *)c->ctx; + buf = (char *)ctl_malloc(RPC_BUF_SIZE); + if(!buf) { + rpc_fault(ctx, 500, "Internal Server Error (No memory left)"); ERR("No memory left\n"); return -1; } buf_size = RPC_BUF_SIZE; - while (1) { - /* Try to print in the allocated space. */ + while(1) { + /* Try to print in the allocated space. */ va_start(ap, fmt); n = vsnprintf(buf, buf_size, fmt, ap); va_end(ap); - /* If that worked, return the string. */ - if (n > -1 && n < buf_size) { + /* If that worked, return the string. */ + if(n > -1 && n < buf_size) { nm.s = name; nm.len = strlen(name); - m = new_chunk_escape(&nm, 1); /* Escape all characters, including : and , */ - if (!m) { + m = new_chunk_escape( + &nm, 1); /* Escape all characters, including : and , */ + if(!m) { rpc_fault(ctx, 500, "Internal Server Error"); goto err; } @@ -1232,7 +1293,7 @@ static int rpc_struct_printf(struct text_chunk* c, char* name, char* fmt, ...) s.s = buf; s.len = n; l = new_chunk_escape(&s, 1); - if (!l) { + if(!l) { rpc_fault(ctx, 500, "Internal Server Error"); free_chunk(m); ERR("Error while creating text_chunk structure"); @@ -1242,21 +1303,23 @@ static int rpc_struct_printf(struct text_chunk* c, char* name, char* fmt, ...) l->flags |= CHUNK_MEMBER_VALUE; l->next = c->next; c->next = l; - if (c == ctx->last) ctx->last = l; + if(c == ctx->last) + ctx->last = l; m->flags |= CHUNK_MEMBER_NAME; m->next = c->next; c->next = m; - if (c == ctx->last) ctx->last = m; + if(c == ctx->last) + ctx->last = m; return 0; } - /* Else try again with more space. */ - if (n > -1) { /* glibc 2.1 */ + /* Else try again with more space. */ + if(n > -1) { /* glibc 2.1 */ buf_size = n + 1; /* precisely what is needed */ - } else { /* glibc 2.0 */ - buf_size *= 2; /* twice the old size */ + } else { /* glibc 2.0 */ + buf_size *= 2; /* twice the old size */ } - if ((buf0 = ctl_realloc(buf, buf_size)) == 0) { + if((buf0 = ctl_realloc(buf, buf_size)) == 0) { rpc_fault(ctx, 500, "Internal Server Error (No memory left)"); ERR("No memory left\n"); goto err; @@ -1264,40 +1327,41 @@ static int rpc_struct_printf(struct text_chunk* c, char* name, char* fmt, ...) buf = buf0; } return 0; - err: - if (buf) ctl_free(buf); +err: + if(buf) + ctl_free(buf); return -1; } -static int rpc_rpl_printf(rpc_ctx_t* ctx, char* fmt, ...) +static int rpc_rpl_printf(rpc_ctx_t *ctx, char *fmt, ...) { int n, buf_size; - char* buf; - char* buf0; + char *buf; + char *buf0; va_list ap; str s; - struct text_chunk* l; + struct text_chunk *l; - buf = (char*)ctl_malloc(RPC_BUF_SIZE); - if (!buf) { - rpc_fault(ctx, 500, "Internal Server Error (No memory left)"); + buf = (char *)ctl_malloc(RPC_BUF_SIZE); + if(!buf) { + rpc_fault(ctx, 500, "Internal Server Error (No memory left)"); ERR("No memory left\n"); return -1; } buf_size = RPC_BUF_SIZE; - while (1) { - /* Try to print in the allocated space. */ + while(1) { + /* Try to print in the allocated space. */ va_start(ap, fmt); n = vsnprintf(buf, buf_size, fmt, ap); va_end(ap); - /* If that worked, return the string. */ - if (n > -1 && n < buf_size) { + /* If that worked, return the string. */ + if(n > -1 && n < buf_size) { s.s = buf; s.len = n; l = new_chunk_escape(&s, 0); - if (!l) { + if(!l) { rpc_fault(ctx, 500, "Internal Server Error"); ERR("Error while creating text_chunk structure"); goto err; @@ -1306,13 +1370,13 @@ static int rpc_rpl_printf(rpc_ctx_t* ctx, char* fmt, ...) ctl_free(buf); return 0; } - /* Else try again with more space. */ - if (n > -1) { /* glibc 2.1 */ + /* Else try again with more space. */ + if(n > -1) { /* glibc 2.1 */ buf_size = n + 1; /* precisely what is needed */ - } else { /* glibc 2.0 */ - buf_size *= 2; /* twice the old size */ + } else { /* glibc 2.0 */ + buf_size *= 2; /* twice the old size */ } - if ((buf0 = ctl_realloc(buf, buf_size)) == 0) { + if((buf0 = ctl_realloc(buf, buf_size)) == 0) { rpc_fault(ctx, 500, "Internal Server Error (No memory left)"); ERR("No memory left\n"); goto err; @@ -1320,21 +1384,22 @@ static int rpc_rpl_printf(rpc_ctx_t* ctx, char* fmt, ...) buf = buf0; } return 0; - err: - if (buf) ctl_free(buf); +err: + if(buf) + ctl_free(buf); return -1; } -static int rpc_scan(rpc_ctx_t* ctx, char* fmt, ...) +static int rpc_scan(rpc_ctx_t *ctx, char *fmt, ...) { - struct text_chunk* l; - struct rpc_struct* s; - int* int_ptr; - char** char_ptr; - str* str_ptr; - double* double_ptr; - void** void_ptr; + struct text_chunk *l; + struct rpc_struct *s; + int *int_ptr; + char **char_ptr; + str *str_ptr; + double *double_ptr; + void **void_ptr; int read; str line; int nofault; @@ -1345,206 +1410,219 @@ static int rpc_scan(rpc_ctx_t* ctx, char* fmt, ...) nofault = 0; read = 0; - modifiers=0; + modifiers = 0; while(*fmt) { - if (read_line(&line.s, &line.len, &ctx->read_h) < 0) { + if(read_line(&line.s, &line.len, &ctx->read_h) < 0) { va_end(ap); return read; } ctx->line_no++; switch(*fmt) { - case '*': /* start of optional parameters */ - nofault = 1; - modifiers++; - break; - case 'b': /* Bool */ - case 't': /* Date and time */ - case 'd': /* Integer */ - if (!line.len) { - if(nofault==0) - rpc_fault(ctx, 400, "Invalid parameter value on line %d", - ctx->line_no); - goto error; - } - int_ptr = va_arg(ap, int*); - *int_ptr = strtol(line.s, 0, 0); - break; + case '*': /* start of optional parameters */ + nofault = 1; + modifiers++; + break; + case 'b': /* Bool */ + case 't': /* Date and time */ + case 'd': /* Integer */ + if(!line.len) { + if(nofault == 0) + rpc_fault(ctx, 400, + "Invalid parameter value on line %d", + ctx->line_no); + goto error; + } + int_ptr = va_arg(ap, int *); + *int_ptr = strtol(line.s, 0, 0); + break; - case 'f': /* double */ - if (!line.len) { - if(nofault==0) - rpc_fault(ctx, 400, "Invalid parameter value on line %d", + case 'f': /* double */ + if(!line.len) { + if(nofault == 0) + rpc_fault(ctx, 400, + "Invalid parameter value on line %d", ctx->line_no); - goto error; - } - double_ptr = va_arg(ap, double*); - *double_ptr = strtod(line.s, 0); - break; + goto error; + } + double_ptr = va_arg(ap, double *); + *double_ptr = strtod(line.s, 0); + break; - case 's': /* zero terminated string */ - case 'S': /* str structure */ - l = new_chunk_unescape(&line); - if (!l) { - if(nofault==0) { - rpc_fault(ctx, 500, "Internal Server Error"); - ERR("Not enough memory\n"); + case 's': /* zero terminated string */ + case 'S': /* str structure */ + l = new_chunk_unescape(&line); + if(!l) { + if(nofault == 0) { + rpc_fault(ctx, 500, "Internal Server Error"); + ERR("Not enough memory\n"); + } + goto error; } - goto error; - } - /* Make sure it gets released at the end */ - l->next = ctx->strs; - ctx->strs = l; + /* Make sure it gets released at the end */ + l->next = ctx->strs; + ctx->strs = l; - if (*fmt == 's') { - char_ptr = va_arg(ap, char**); - *char_ptr = l->s.s; - } else { - str_ptr = va_arg(ap, str*); - *str_ptr = l->s; - } - break; + if(*fmt == 's') { + char_ptr = va_arg(ap, char **); + *char_ptr = l->s.s; + } else { + str_ptr = va_arg(ap, str *); + *str_ptr = l->s; + } + break; - case '{': - void_ptr = va_arg(ap, void**); - s = new_struct(ctx, &line); - if (!s) goto error; - s->next = ctx->structs; - ctx->structs = s; - *void_ptr = s; - break; + case '{': + void_ptr = va_arg(ap, void **); + s = new_struct(ctx, &line); + if(!s) + goto error; + s->next = ctx->structs; + ctx->structs = s; + *void_ptr = s; + break; - default: - ERR("Invalid parameter type in formatting string: %c\n", *fmt); - rpc_fault(ctx, 500, "Server Internal Error (Invalid Formatting Character '%c')", *fmt); - goto error; + default: + ERR("Invalid parameter type in formatting string: %c\n", *fmt); + rpc_fault(ctx, 500, + "Server Internal Error (Invalid Formatting Character " + "'%c')", + *fmt); + goto error; } fmt++; read++; } va_end(ap); - return read-modifiers; + return read - modifiers; - error: +error: va_end(ap); - return -(read-modifiers); + return -(read - modifiers); } -static int rpc_struct_add(struct text_chunk* s, char* fmt, ...) +static int rpc_struct_add(struct text_chunk *s, char *fmt, ...) { static char buf[MAX_LINE_BUFFER]; str st, *sp; - void** void_ptr; + void **void_ptr; va_list ap; - struct text_chunk* m, *c; - rpc_ctx_t* ctx; + struct text_chunk *m, *c; + rpc_ctx_t *ctx; str null_value = str_init(""); - ctx=(rpc_ctx_t*)s->ctx; + ctx = (rpc_ctx_t *)s->ctx; va_start(ap, fmt); while(*fmt) { - /* Member name escaped */ - st.s = va_arg(ap, char*); + /* Member name escaped */ + st.s = va_arg(ap, char *); st.len = strlen(st.s); - m = new_chunk_escape(&st, 1); /* Escape all characters, including : and , */ - if (!m) { + m = new_chunk_escape( + &st, 1); /* Escape all characters, including : and , */ + if(!m) { rpc_fault(ctx, 500, "Internal Server Error"); goto err; } m->flags |= CHUNK_MEMBER_NAME; - if(*fmt=='{' || *fmt=='[') { - void_ptr = va_arg(ap, void**); - m->ctx=ctx; + if(*fmt == '{' || *fmt == '[') { + void_ptr = va_arg(ap, void **); + m->ctx = ctx; append_chunk(ctx, m); *void_ptr = m; } else { switch(*fmt) { - case 'd': - case 't': - st.s = int2str(va_arg(ap, int), &st.len); - c = new_chunk(&st); - break; - - case 'f': - st.s = buf; - st.len = snprintf(buf, 256, "%f", va_arg(ap, double)); - if (st.len < 0) { - rpc_fault(ctx, 400, "Error While Converting double"); - ERR("Error while converting double\n"); - goto err; - } - c = new_chunk(&st); - break; + case 'd': + case 't': + st.s = int2str(va_arg(ap, int), &st.len); + c = new_chunk(&st); + break; + + case 'f': + st.s = buf; + st.len = snprintf(buf, 256, "%f", va_arg(ap, double)); + if(st.len < 0) { + rpc_fault(ctx, 400, "Error While Converting double"); + ERR("Error while converting double\n"); + goto err; + } + c = new_chunk(&st); + break; case 'b': st.len = 1; st.s = ((va_arg(ap, int) == 0) ? "0" : "1"); c = new_chunk(&st); - break; - - case 's': - st.s = va_arg(ap, char*); - if(st.s==NULL) { - st = null_value; - } else { - st.len = strlen(st.s); - } - c = new_chunk_escape(&st, 1); - break; - - case 'S': - sp = va_arg(ap, str*); - if(sp!=NULL && sp->s!=NULL) { - st = *sp; - } else { - st = null_value; - } - c = new_chunk_escape(&st, 1); - break; - - default: - rpc_fault(ctx, 500, "Bug In SER (Invalid formatting character %c)", - *fmt); - ERR("Invalid formatting character\n"); - goto err; + break; + + case 's': + st.s = va_arg(ap, char *); + if(st.s == NULL) { + st = null_value; + } else { + st.len = strlen(st.s); + } + c = new_chunk_escape(&st, 1); + break; + + case 'S': + sp = va_arg(ap, str *); + if(sp != NULL && sp->s != NULL) { + st = *sp; + } else { + st = null_value; + } + c = new_chunk_escape(&st, 1); + break; + + default: + rpc_fault(ctx, 500, + "Bug In SER (Invalid formatting character %c)", + *fmt); + ERR("Invalid formatting character\n"); + goto err; } - if (!c) { + if(!c) { rpc_fault(ctx, 500, "Internal Server Error"); goto err; } c->flags |= CHUNK_MEMBER_VALUE; c->next = s->next; s->next = c; - if (s == ctx->last) ctx->last = c; + if(s == ctx->last) + ctx->last = c; m->next = s->next; s->next = m; - if (s == ctx->last) ctx->last = m; + if(s == ctx->last) + ctx->last = m; } fmt++; } va_end(ap); return 0; - err: - if (m) free_chunk(m); +err: + if(m) + free_chunk(m); va_end(ap); return -1; } -static int find_member(struct text_chunk** value, struct rpc_struct* s, str* member_name) +static int find_member( + struct text_chunk **value, struct rpc_struct *s, str *member_name) { - struct text_chunk* n, *v; + struct text_chunk *n, *v; n = s->names; v = s->values; while(n) { - if (member_name->len == n->s.len && - !strncasecmp(member_name->s, n->s.s, n->s.len)) { - if (n->flags & CHUNK_SEEN) goto skip; + if(member_name->len == n->s.len + && !strncasecmp(member_name->s, n->s.s, n->s.len)) { + if(n->flags & CHUNK_SEEN) + goto skip; else { *value = v; n->flags |= CHUNK_SEEN; @@ -1560,72 +1638,73 @@ static int find_member(struct text_chunk** value, struct rpc_struct* s, str* mem } -static int rpc_struct_scan(struct rpc_struct* s, char* fmt, ...) +static int rpc_struct_scan(struct rpc_struct *s, char *fmt, ...) { - struct text_chunk* val; + struct text_chunk *val; va_list ap; - int* int_ptr; - double* double_ptr; - char** char_ptr; - str* str_ptr; + int *int_ptr; + double *double_ptr; + char **char_ptr; + str *str_ptr; str member_name; int ret, read; read = 0; va_start(ap, fmt); while(*fmt) { - member_name.s = va_arg(ap, char*); + member_name.s = va_arg(ap, char *); member_name.len = strlen(member_name.s); ret = find_member(&val, s, &member_name); - if (ret > 0) { + if(ret > 0) { va_end(ap); return read; } switch(*fmt) { - case 'b': /* Bool */ - case 't': /* Date and time */ - case 'd': /* Integer */ - int_ptr = va_arg(ap, int*); - if (!val->s.len) { - rpc_fault(s->ctx, 400, "Invalid Parameter Value"); - goto error; - } - /* String in text_chunk is always zero terminated */ - *int_ptr = strtol(val->s.s, 0, 0); - break; + case 'b': /* Bool */ + case 't': /* Date and time */ + case 'd': /* Integer */ + int_ptr = va_arg(ap, int *); + if(!val->s.len) { + rpc_fault(s->ctx, 400, "Invalid Parameter Value"); + goto error; + } + /* String in text_chunk is always zero terminated */ + *int_ptr = strtol(val->s.s, 0, 0); + break; - case 'f': /* double */ - double_ptr = va_arg(ap, double*); - if (!val->s.len) { - rpc_fault(s->ctx, 400, "Invalid Parameter Value"); - goto error; - } - /* String in text_chunk is always zero terminated */ - *double_ptr = strtod(val->s.s, 0); - break; + case 'f': /* double */ + double_ptr = va_arg(ap, double *); + if(!val->s.len) { + rpc_fault(s->ctx, 400, "Invalid Parameter Value"); + goto error; + } + /* String in text_chunk is always zero terminated */ + *double_ptr = strtod(val->s.s, 0); + break; - case 's': /* zero terminated string */ - char_ptr = va_arg(ap, char**); - /* String in text_chunk is always zero terminated */ - *char_ptr = val->s.s; - break; + case 's': /* zero terminated string */ + char_ptr = va_arg(ap, char **); + /* String in text_chunk is always zero terminated */ + *char_ptr = val->s.s; + break; - case 'S': /* str structure */ - str_ptr = va_arg(ap, str*); - *str_ptr = val->s; - break; - default: - rpc_fault(s->ctx, 500, "Invalid character in formatting string '%c'", *fmt); - ERR("Invalid parameter type in formatting string: %c\n", *fmt); - goto error; + case 'S': /* str structure */ + str_ptr = va_arg(ap, str *); + *str_ptr = val->s; + break; + default: + rpc_fault(s->ctx, 500, + "Invalid character in formatting string '%c'", *fmt); + ERR("Invalid parameter type in formatting string: %c\n", *fmt); + goto error; } fmt++; read++; } va_end(ap); return read; - error: +error: va_end(ap); return -read; } diff --git a/src/modules/ctl/fifo_server.h b/src/modules/ctl/fifo_server.h index d40390b1391..4c25b618f11 100644 --- a/src/modules/ctl/fifo_server.h +++ b/src/modules/ctl/fifo_server.h @@ -27,17 +27,17 @@ #define CMD_SEPARATOR ':' -extern char* fifo_dir; -extern int fifo_reply_retries; -extern int fifo_reply_wait; +extern char *fifo_dir; +extern int fifo_reply_retries; +extern int fifo_reply_wait; /* Initialize FIFO server data structures */ -int init_fifo_fd(char* fifo, int fifo_mode, int fifo_uid, int fifo_gid, - int* wfd); +int init_fifo_fd( + char *fifo, int fifo_mode, int fifo_uid, int fifo_gid, int *wfd); -int fifo_process(char* msg_buf, int size, int* bytes_need, void *sh, void** s); +int fifo_process(char *msg_buf, int size, int *bytes_need, void *sh, void **s); /* memory deallocation */ -void destroy_fifo(int read_fd, int w_fd, char* fname); +void destroy_fifo(int read_fd, int w_fd, char *fname); int fifo_rpc_init(); #endif diff --git a/src/modules/ctl/init_socks.c b/src/modules/ctl/init_socks.c index 269bfd869c5..9575765f2f0 100644 --- a/src/modules/ctl/init_socks.c +++ b/src/modules/ctl/init_socks.c @@ -32,7 +32,7 @@ #include /*IPTOS_LOWDELAY*/ #include #include -#include /* unlink */ +#include /* unlink */ #include /* chmod */ #include @@ -41,7 +41,7 @@ #endif -static int tcp_proto_no=-1; /* tcp protocol number as returned by +static int tcp_proto_no = -1; /* tcp protocol number as returned by getprotobyname */ @@ -50,15 +50,17 @@ static int set_non_blocking(int s) { int flags; /* non-blocking */ - flags=fcntl(s, F_GETFL); - if (flags==-1){ - LOG(L_ERR, "ERROR: set_non_blocking: fnctl failed: (%d) %s\n", - errno, strerror(errno)); + flags = fcntl(s, F_GETFL); + if(flags == -1) { + LOG(L_ERR, "ERROR: set_non_blocking: fnctl failed: (%d) %s\n", errno, + strerror(errno)); goto error; } - if (fcntl(s, F_SETFL, flags|O_NONBLOCK)==-1){ - LOG(L_ERR, "ERROR: set_non_blocking: fcntl: set non-blocking failed:" - " (%d) %s\n", errno, strerror(errno)); + if(fcntl(s, F_SETFL, flags | O_NONBLOCK) == -1) { + LOG(L_ERR, + "ERROR: set_non_blocking: fcntl: set non-blocking failed:" + " (%d) %s\n", + errno, strerror(errno)); goto error; } return 0; @@ -67,163 +69,170 @@ static int set_non_blocking(int s) } - - /* opens, binds and listens-on a control unix socket of type 'type' * it will change the permissions to perm, if perm!=0 * and the ownership to uid.gid if !=-1 * returns socket fd or -1 on error */ -int init_unix_sock(struct sockaddr_un* su, char* name, int type, int perm, - int uid, int gid) +int init_unix_sock(struct sockaddr_un *su, char *name, int type, int perm, + int uid, int gid) { struct sockaddr_un ifsun; int s; int len; int optval; - - s=-1; + + s = -1; unlink(name); - memset(&ifsun, 0, sizeof (struct sockaddr_un)); - len=strlen(name); - if (len>UNIX_PATH_MAX){ - LOG(L_ERR, "ERROR: init_unix_sock: name too long (%d > %d): %s\n", - len, UNIX_PATH_MAX, name); + memset(&ifsun, 0, sizeof(struct sockaddr_un)); + len = strlen(name); + if(len > UNIX_PATH_MAX) { + LOG(L_ERR, "ERROR: init_unix_sock: name too long (%d > %d): %s\n", len, + UNIX_PATH_MAX, name); goto error; } - ifsun.sun_family=AF_UNIX; + ifsun.sun_family = AF_UNIX; memcpy(ifsun.sun_path, name, len); #ifdef HAVE_SOCKADDR_SA_LEN - ifsun.sun_len=len; + ifsun.sun_len = len; #endif - s=socket(PF_UNIX, type, 0); - if (s==-1){ - LOG(L_ERR, "ERROR: init_unix_sock: cannot create unix socket %s:" - " %s [%d]\n", name, strerror(errno), errno); + s = socket(PF_UNIX, type, 0); + if(s == -1) { + LOG(L_ERR, + "ERROR: init_unix_sock: cannot create unix socket %s:" + " %s [%d]\n", + name, strerror(errno), errno); goto error; } - optval=1; - if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))==-1){ - LOG(L_ERR, "ERROR: init_unix_sock: setsockopt: %s [%d]\n", + optval = 1; + if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) == -1) { + LOG(L_ERR, "ERROR: init_unix_sock: setsockopt: %s [%d]\n", strerror(errno), errno); /* continue */ } - if (set_non_blocking(s)==-1){ + if(set_non_blocking(s) == -1) { LOG(L_ERR, "ERROR: init_unix_sock: set non blocking failed\n"); } - if (bind(s, (struct sockaddr *)&ifsun, sizeof(ifsun))==-1){ - LOG(L_ERR, "ERROR: init_unix_sock: bind: %s [%d]\n", - strerror(errno), errno); + if(bind(s, (struct sockaddr *)&ifsun, sizeof(ifsun)) == -1) { + LOG(L_ERR, "ERROR: init_unix_sock: bind: %s [%d]\n", strerror(errno), + errno); goto error; } /* then the permissions */ - if (perm){ /* mode==0 doesn't make sense, nobody can read/write */ - if (chmod(name, perm)<0){ - LOG(L_ERR, "ERROR: init_unix_sock: failed to change the" + if(perm) { /* mode==0 doesn't make sense, nobody can read/write */ + if(chmod(name, perm) < 0) { + LOG(L_ERR, + "ERROR: init_unix_sock: failed to change the" " permissions for %s to %04o: %s[%d]\n", name, perm, strerror(errno), errno); goto error; } } /* try to change ownership */ - if ((uid!=-1) && (gid!=-1)){ - if (chown(name, uid, gid)<0){ - LOG(L_ERR, "ERROR: init_unix_sock: failed to change the" + if((uid != -1) && (gid != -1)) { + if(chown(name, uid, gid) < 0) { + LOG(L_ERR, + "ERROR: init_unix_sock: failed to change the" " owner/group for %s to %d.%d: %s[%d]\n", name, uid, gid, strerror(errno), errno); goto error; } } - if ((type==SOCK_STREAM) && (listen(s, 128)==-1)){ - LOG(L_ERR, "ERROR: init_unix_sock: listen: %s [%d]\n", - strerror(errno), errno); + if((type == SOCK_STREAM) && (listen(s, 128) == -1)) { + LOG(L_ERR, "ERROR: init_unix_sock: listen: %s [%d]\n", strerror(errno), + errno); goto error; } - *su=ifsun; + *su = ifsun; return s; error: - if (s!=-1) close(s); + if(s != -1) + close(s); return -1; } - /* opens, binds and listens-on a control tcp socket * returns socket fd or -1 on error */ -int init_tcpudp_sock(union sockaddr_union* sa_un, char* address, int port, - enum socket_protos type) +int init_tcpudp_sock(union sockaddr_union *sa_un, char *address, int port, + enum socket_protos type) { union sockaddr_union su; - struct hostent* he; + struct hostent *he; int s; int optval; - - s=-1; - if ((type!=UDP_SOCK) && (type!=TCP_SOCK)){ - LOG(L_CRIT, "BUG: init_tcpudp_sock called with bad type: %d\n", - type); + + s = -1; + if((type != UDP_SOCK) && (type != TCP_SOCK)) { + LOG(L_CRIT, "BUG: init_tcpudp_sock called with bad type: %d\n", type); goto error; } - memset(&su, 0, sizeof (su)); + memset(&su, 0, sizeof(su)); /* if no address specified, or address=='*', listen on all * ipv4 addresses */ - if ((address==0)||((*address)==0)||((*address=='*') && (*(address+1)==0))){ - su.sin.sin_family=AF_INET; - su.sin.sin_port=htons(port); - su.sin.sin_addr.s_addr=INADDR_ANY; + if((address == 0) || ((*address) == 0) + || ((*address == '*') && (*(address + 1) == 0))) { + su.sin.sin_family = AF_INET; + su.sin.sin_port = htons(port); + su.sin.sin_addr.s_addr = INADDR_ANY; #ifdef HAVE_SOCKADDR_SA_LEN - su.sin.sin_len=sizeof(struct sockaddr_in); + su.sin.sin_len = sizeof(struct sockaddr_in); #endif - }else{ - he=resolvehost(address); - if (he==0){ + } else { + he = resolvehost(address); + if(he == 0) { LOG(L_ERR, "ERROR: init_tcpudp_sock: bad address %s\n", address); goto error; } - if (hostent2su(&su, he, 0, port)==-1) goto error; + if(hostent2su(&su, he, 0, port) == -1) + goto error; } - s=socket(AF2PF(su.s.sa_family), (type==TCP_SOCK)?SOCK_STREAM:SOCK_DGRAM,0); - if (s==-1){ - LOG(L_ERR, "ERROR: init_tcpudp_sock: cannot create tcp socket:" - " %s [%d]\n", strerror(errno), errno); + s = socket(AF2PF(su.s.sa_family), + (type == TCP_SOCK) ? SOCK_STREAM : SOCK_DGRAM, 0); + if(s == -1) { + LOG(L_ERR, + "ERROR: init_tcpudp_sock: cannot create tcp socket:" + " %s [%d]\n", + strerror(errno), errno); goto error; } /* REUSEADDR */ - optval=1; - if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))==-1){ - LOG(L_ERR, "ERROR: init_tcpudp_sock: setsockopt: %s [%d]\n", + optval = 1; + if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) == -1) { + LOG(L_ERR, "ERROR: init_tcpudp_sock: setsockopt: %s [%d]\n", strerror(errno), errno); /* continue */ } /* tos */ - optval=IPTOS_LOWDELAY; - if (setsockopt(s, IPPROTO_IP, IP_TOS, (void*)&optval,sizeof(optval)) ==-1){ + optval = IPTOS_LOWDELAY; + if(setsockopt(s, IPPROTO_IP, IP_TOS, (void *)&optval, sizeof(optval)) + == -1) { LOG(L_WARN, "WARNING: init_tcpudp_sock: setsockopt tos: %s\n", strerror(errno)); /* continue since this is not critical */ } - if (set_non_blocking(s)==-1){ + if(set_non_blocking(s) == -1) { LOG(L_ERR, "ERROR: init_tcpudp_sock: set non blocking failed\n"); } - - if (bind(s, &su.s, sockaddru_len(su))==-1){ - LOG(L_ERR, "ERROR: init_tcpudp_sock: bind: %s [%d]\n", - strerror(errno), errno); + + if(bind(s, &su.s, sockaddru_len(su)) == -1) { + LOG(L_ERR, "ERROR: init_tcpudp_sock: bind: %s [%d]\n", strerror(errno), + errno); goto error; } - if ((type==TCP_SOCK) && (listen(s, 128)==-1)){ + if((type == TCP_SOCK) && (listen(s, 128) == -1)) { LOG(L_ERR, "ERROR: init_tcpudp_sock: listen: %s [%d]\n", strerror(errno), errno); goto error; } - *sa_un=su; + *sa_un = su; return s; error: - if (s!=-1) close(s); + if(s != -1) + close(s); return -1; } - /* set all socket/fd options: disable nagle, tos lowdelay, non-blocking * return -1 on error */ int init_sock_opt(int s, enum socket_protos type) @@ -231,36 +240,40 @@ int init_sock_opt(int s, enum socket_protos type) int optval; #ifdef DISABLE_NAGLE int flags; - struct protoent* pe; + struct protoent *pe; #endif - - if ((type==UDP_SOCK)||(type==TCP_SOCK)){ + + if((type == UDP_SOCK) || (type == TCP_SOCK)) { #ifdef DISABLE_NAGLE - if (type==TCP_SOCK){ - flags=1; - if (tcp_proto_no==-1){ /* if not already set */ - pe=getprotobyname("tcp"); - if (pe!=0){ - tcp_proto_no=pe->p_proto; + if(type == TCP_SOCK) { + flags = 1; + if(tcp_proto_no == -1) { /* if not already set */ + pe = getprotobyname("tcp"); + if(pe != 0) { + tcp_proto_no = pe->p_proto; } } - if ( (tcp_proto_no!=-1) && (setsockopt(s, tcp_proto_no, - TCP_NODELAY, &flags, sizeof(flags))<0) ){ - LOG(L_WARN, "WARNING: init_sock_opt: could not disable" - " Nagle: %s\n", strerror(errno)); + if((tcp_proto_no != -1) + && (setsockopt(s, tcp_proto_no, TCP_NODELAY, &flags, + sizeof(flags)) + < 0)) { + LOG(L_WARN, + "WARNING: init_sock_opt: could not disable" + " Nagle: %s\n", + strerror(errno)); } } #endif /* tos*/ optval = IPTOS_LOWDELAY; - if (setsockopt(s, IPPROTO_IP, IP_TOS, (void*)&optval, - sizeof(optval)) ==-1){ + if(setsockopt(s, IPPROTO_IP, IP_TOS, (void *)&optval, sizeof(optval)) + == -1) { LOG(L_WARN, "WARNING: init_sock_opt: setsockopt tos: %s\n", strerror(errno)); /* continue since this is not critical */ } } - if (set_non_blocking(s)==-1){ + if(set_non_blocking(s) == -1) { LOG(L_ERR, "ERROR: init_sock_opt: set non blocking failed\n"); } return 0; diff --git a/src/modules/ctl/init_socks.h b/src/modules/ctl/init_socks.h index 856af81d053..5e76c134b03 100644 --- a/src/modules/ctl/init_socks.h +++ b/src/modules/ctl/init_socks.h @@ -24,22 +24,28 @@ #include #include "../../core/ip_addr.h" -enum socket_protos { UNKNOWN_SOCK=0, UDP_SOCK, TCP_SOCK, - UNIXS_SOCK, UNIXD_SOCK +enum socket_protos +{ + UNKNOWN_SOCK = 0, + UDP_SOCK, + TCP_SOCK, + UNIXS_SOCK, + UNIXD_SOCK #ifdef USE_FIFO - , FIFO_SOCK + , + FIFO_SOCK #endif }; -int init_unix_sock(struct sockaddr_un* su, char* name, int type, - int perm, int uid, int gid); -int init_tcpudp_sock(union sockaddr_union* su, char* address, int port, - enum socket_protos type); +int init_unix_sock(struct sockaddr_un *su, char *name, int type, int perm, + int uid, int gid); +int init_tcpudp_sock(union sockaddr_union *su, char *address, int port, + enum socket_protos type); int init_sock_opt(int s, enum socket_protos type); -inline static char* socket_proto_name(enum socket_protos p) +inline static char *socket_proto_name(enum socket_protos p) { - switch(p){ + switch(p) { case UDP_SOCK: return "udp"; case TCP_SOCK: @@ -52,8 +58,7 @@ inline static char* socket_proto_name(enum socket_protos p) case FIFO_SOCK: return "fifo"; #endif - default: - ; + default:; } return ""; } diff --git a/src/modules/ctl/io_listener.c b/src/modules/ctl/io_listener.c index 5de2e9cb05f..ed584e4c7f9 100644 --- a/src/modules/ctl/io_listener.c +++ b/src/modules/ctl/io_listener.c @@ -19,7 +19,7 @@ */ #include "../../core/globals.h" -#include "../../core/pt.h" /* process_count */ +#include "../../core/pt.h" /* process_count */ #include "../../core/timer.h" #include "../../core/timer_ticks.h" #include "../../core/tsend.h" @@ -39,74 +39,79 @@ #define HANDLE_IO_INLINE #include "../../core/io_wait.h" -#include /* required by io_wait.h if SIGIO_RT is used */ +#include /* required by io_wait.h if SIGIO_RT is used */ #include /* iovec */ -#define IO_STREAM_CONN_TIMEOUT S_TO_TICKS(120) -#define IO_LISTEN_TIMEOUT 10 /* in s, how often the timer +#define IO_STREAM_CONN_TIMEOUT S_TO_TICKS(120) +#define IO_LISTEN_TIMEOUT \ + 10 /* in s, how often the timer will be run */ -#define IO_LISTEN_TX_TIMEOUT 10 /* ms */ -#define DGRAM_BUF_SIZE 65535 -#define STREAM_BUF_SIZE 65535 +#define IO_LISTEN_TX_TIMEOUT 10 /* ms */ +#define DGRAM_BUF_SIZE 65535 +#define STREAM_BUF_SIZE 65535 /* 0 has a special meaning, don't use it (see io_wait.h)*/ -enum fd_type { F_T_RESERVED=0, F_T_CTRL_DGRAM, F_T_CTRL_STREAM, - F_T_READ_STREAM +enum fd_type +{ + F_T_RESERVED = 0, + F_T_CTRL_DGRAM, + F_T_CTRL_STREAM, + F_T_READ_STREAM #ifdef USE_FIFO - , F_T_FIFO + , + F_T_FIFO #endif }; -struct stream_req{ - unsigned char *end; /* end of read data */ - unsigned char* proc; /* processed so far */ - int bytes_to_go; /* how many bytes we still have to read */ +struct stream_req +{ + unsigned char *end; /* end of read data */ + unsigned char *proc; /* processed so far */ + int bytes_to_go; /* how many bytes we still have to read */ unsigned char buf[STREAM_BUF_SIZE]; }; -struct stream_connection{ - struct stream_connection* next; - struct stream_connection* prev; +struct stream_connection +{ + struct stream_connection *next; + struct stream_connection *prev; int fd; enum payload_proto p_proto; - struct ctrl_socket* parent; + struct ctrl_socket *parent; struct stream_req req; - void* saved_state; /* connection/datagram saved state */ + void *saved_state; /* connection/datagram saved state */ ticks_t expire; union sockaddr_u from; }; - -typedef int (*send_ev_f)(void* send_h , struct iovec* v, size_t count); - +typedef int (*send_ev_f)(void *send_h, struct iovec *v, size_t count); static io_wait_h ctl_io_h; -static int io_read_connections=0; +static int io_read_connections = 0; static struct stream_connection stream_conn_lst; /* list head */ -static struct stream_connection* s_conn_new(int sock, - struct ctrl_socket* cs, - union sockaddr_u* from) +static struct stream_connection *s_conn_new( + int sock, struct ctrl_socket *cs, union sockaddr_u *from) { - struct stream_connection* s_c; + struct stream_connection *s_c; - s_c=ctl_malloc(sizeof(struct stream_connection)); - if (s_c){ + s_c = ctl_malloc(sizeof(struct stream_connection)); + if(s_c) { memset(s_c, 0, sizeof(struct stream_connection)); - s_c->fd=sock; - s_c->req.end=&s_c->req.buf[0]; - s_c->req.proc=s_c->req.end; - s_c->req.bytes_to_go=0; /* BINRPC_MIN_HDR ? */ - s_c->expire=get_ticks_raw()+IO_STREAM_CONN_TIMEOUT; - s_c->p_proto=cs->p_proto; - s_c->from=*from; - s_c->parent=cs; + s_c->fd = sock; + s_c->req.end = &s_c->req.buf[0]; + s_c->req.proc = s_c->req.end; + s_c->req.bytes_to_go = 0; /* BINRPC_MIN_HDR ? */ + s_c->expire = get_ticks_raw() + IO_STREAM_CONN_TIMEOUT; + s_c->p_proto = cs->p_proto; + s_c->from = *from; + s_c->parent = cs; } return s_c; } @@ -115,8 +120,7 @@ static struct stream_connection* s_conn_new(int sock, #define s_conn_add(s_c) clist_append(&stream_conn_lst, s_c, next, prev) - -inline static void s_conn_rm(struct stream_connection* sc) +inline static void s_conn_rm(struct stream_connection *sc) { clist_rm(sc, next, prev); ctl_free(sc); @@ -124,187 +128,192 @@ inline static void s_conn_rm(struct stream_connection* sc) } - - /* * sends on a "disconnected" socket/fd (e.g. not connected udp socket, * unix datagram socket) * returns: number of bytes written on success, * <0 on error (-1 tsend* error, -2 packet too big) */ -inline static int sendv_disc(struct send_handle* sh, struct iovec* v, - size_t count) +inline static int sendv_disc( + struct send_handle *sh, struct iovec *v, size_t count) { char buf[DGRAM_BUF_SIZE]; - char* p; - char* end; + char *p; + char *end; int r; - p=buf; - end=p+DGRAM_BUF_SIZE; - for (r=0; rend) + p = buf; + end = p + DGRAM_BUF_SIZE; + for(r = 0; r < count; r++) { + if((p + v[r].iov_len) > end) goto error_overflow; memcpy(p, v[r].iov_base, v[r].iov_len); - p+=v[r].iov_len; + p += v[r].iov_len; } - return tsend_dgram(sh->fd, buf, (int)(p-buf), &sh->from.sa_in.s, - sh->from_len, IO_LISTEN_TX_TIMEOUT); + return tsend_dgram(sh->fd, buf, (int)(p - buf), &sh->from.sa_in.s, + sh->from_len, IO_LISTEN_TX_TIMEOUT); error_overflow: return -2; } - /* returns: number of bytes written on success, * <0 on error (-1 send error, -2 too big) */ -int sock_send_v(void *h, struct iovec* v, size_t count) +int sock_send_v(void *h, struct iovec *v, size_t count) { - struct send_handle* sh; + struct send_handle *sh; - sh=(struct send_handle*)h; - if (sh->type==S_CONNECTED) + sh = (struct send_handle *)h; + if(sh->type == S_CONNECTED) return tsend_dgram_ev(sh->fd, v, count, IO_LISTEN_TX_TIMEOUT); else return sendv_disc(sh, v, count); }; - -void io_listen_loop(int fd_no, struct ctrl_socket* cs_lst) +void io_listen_loop(int fd_no, struct ctrl_socket *cs_lst) { int max_fd_no; - char* poll_err; + char *poll_err; int poll_method; struct ctrl_socket *cs; int type; clist_init(&stream_conn_lst, next, prev); - type=F_T_RESERVED; + type = F_T_RESERVED; #if 0 /* estimate used fd numbers -- FIXME: broken, make it a function in pt.h */ max_fd_no=get_max_procs()*3 -1 /* timer */ +3; /* stdin/out/err*/; max_fd_no+=fd_no+MAX_IO_READ_CONNECTIONS; /*our listen fds + max. allowed tmp. fds */ #endif - max_fd_no=get_max_open_fds(); + max_fd_no = get_max_open_fds(); /* choose/fix the poll method */ /* FIXME: make it a config param? */ #if USE_TCP - poll_method=tcp_poll_method; /* try to resue the tcp poll method */ - poll_err=check_poll_method(poll_method); + poll_method = tcp_poll_method; /* try to resue the tcp poll method */ + poll_err = check_poll_method(poll_method); #else poll_method = 0; /* make check for TCP poll method fail */ poll_err = NULL; #endif /* set an appropiate poll method */ - if (poll_err || (poll_method==0)){ - poll_method=choose_poll_method(); - if (poll_err){ + if(poll_err || (poll_method == 0)) { + poll_method = choose_poll_method(); + if(poll_err) { LOG(L_ERR, "ERROR: io_listen_loop: %s, using %s instead\n", poll_err, poll_method_name(poll_method)); - }else{ - LOG(L_INFO, "io_listen_loop: using %s as the io watch method" - " (auto detected)\n", poll_method_name(poll_method)); - } - }else{ - LOG(L_INFO, "io_listen_loop: using %s io watch method (config)\n", + } else { + LOG(L_INFO, + "io_listen_loop: using %s as the io watch method" + " (auto detected)\n", poll_method_name(poll_method)); + } + } else { + LOG(L_INFO, "io_listen_loop: using %s io watch method (config)\n", + poll_method_name(poll_method)); } - if (init_io_wait(&ctl_io_h, max_fd_no, poll_method)<0) + if(init_io_wait(&ctl_io_h, max_fd_no, poll_method) < 0) goto error; /* add all the sockets we listen on for connections */ - for (cs=cs_lst; cs; cs=cs->next){ - switch(cs->transport){ + for(cs = cs_lst; cs; cs = cs->next) { + switch(cs->transport) { case UDP_SOCK: case UNIXD_SOCK: - type=F_T_CTRL_DGRAM; + type = F_T_CTRL_DGRAM; break; case TCP_SOCK: case UNIXS_SOCK: - type=F_T_CTRL_STREAM; + type = F_T_CTRL_STREAM; break; #ifdef USE_FIFO case FIFO_SOCK: - type=F_T_FIFO; /* special */ - cs->data=s_conn_new(cs->fd, cs, &cs->u);/* reuse stream conn */ - if (cs->data==0){ + type = F_T_FIFO; /* special */ + cs->data = + s_conn_new(cs->fd, cs, &cs->u); /* reuse stream conn */ + if(cs->data == 0) { LOG(L_ERR, "ERROR: io_listen_loop: out of memory\n"); goto error; } - break; + break; #endif case UNKNOWN_SOCK: - LOG(L_CRIT, "BUG: io_listen_loop: bad control socket transport" - " %d\n", cs->transport); + LOG(L_CRIT, + "BUG: io_listen_loop: bad control socket transport" + " %d\n", + cs->transport); goto error; } DBG("io_listen_loop: adding socket %d, type %d, transport" - " %d (%s)\n", cs->fd, type, cs->transport, cs->name); - if (io_watch_add(&ctl_io_h, cs->fd, POLLIN, type, cs)<0){ + " %d (%s)\n", + cs->fd, type, cs->transport, cs->name); + if(io_watch_add(&ctl_io_h, cs->fd, POLLIN, type, cs) < 0) { LOG(L_CRIT, "ERROR: io_listen_loop: init: failed to add" - "listen socket to the fd list\n"); + "listen socket to the fd list\n"); goto error; } } /* initialize the config framework */ - if (cfg_child_init()) goto error; + if(cfg_child_init()) + goto error; /* main loop */ - switch(ctl_io_h.poll_method){ + switch(ctl_io_h.poll_method) { case POLL_POLL: - while(1){ + while(1) { io_wait_loop_poll(&ctl_io_h, IO_LISTEN_TIMEOUT, 0); } break; #ifdef HAVE_SELECT case POLL_SELECT: - while(1){ + while(1) { io_wait_loop_select(&ctl_io_h, IO_LISTEN_TIMEOUT, 0); } break; #endif #ifdef HAVE_SIGIO_RT case POLL_SIGIO_RT: - while(1){ + while(1) { io_wait_loop_sigio_rt(&ctl_io_h, IO_LISTEN_TIMEOUT); } break; #endif #ifdef HAVE_EPOLL case POLL_EPOLL_LT: - while(1){ + while(1) { io_wait_loop_epoll(&ctl_io_h, IO_LISTEN_TIMEOUT, 0); } break; case POLL_EPOLL_ET: - while(1){ + while(1) { io_wait_loop_epoll(&ctl_io_h, IO_LISTEN_TIMEOUT, 1); } break; #endif #ifdef HAVE_KQUEUE case POLL_KQUEUE: - while(1){ + while(1) { io_wait_loop_kqueue(&ctl_io_h, IO_LISTEN_TIMEOUT, 0); } break; #endif #ifdef HAVE_DEVPOLL case POLL_DEVPOLL: - while(1){ + while(1) { io_wait_loop_devpoll(&ctl_io_h, IO_LISTEN_TIMEOUT, 0); } break; #endif default: - LOG(L_CRIT, "BUG: io_listen_loop: no support for poll method " + LOG(L_CRIT, + "BUG: io_listen_loop: no support for poll method " " %s (%d)\n", - poll_method_name(ctl_io_h.poll_method), ctl_io_h.poll_method); + poll_method_name(ctl_io_h.poll_method), + ctl_io_h.poll_method); goto error; } /* should never reach this point under normal (non-error) circumstances */ @@ -313,7 +322,6 @@ void io_listen_loop(int fd_no, struct ctrl_socket* cs_lst) } - /* handles an io event on one of the watched dgram connections * (it can read the whole packet) * @@ -329,28 +337,28 @@ void io_listen_loop(int fd_no, struct ctrl_socket* cs_lst) * queued -- the receive buffer might still be non-empty) * */ -static int handle_ctrl_dgram(struct ctrl_socket* cs) +static int handle_ctrl_dgram(struct ctrl_socket *cs) { unsigned char buf[DGRAM_BUF_SIZE]; int bytes; int bytes_needed; int ret; struct send_handle sh; - void* saved_state; + void *saved_state; - saved_state=0; /* we get always a new datagram */ - sh.fd=cs->fd; - sh.type=S_DISCONNECTED; - sh.from_len=(cs->transport==UDP_SOCK)?sockaddru_len(cs->u.sa_in): - sizeof(cs->u.sa_un); + saved_state = 0; /* we get always a new datagram */ + sh.fd = cs->fd; + sh.type = S_DISCONNECTED; + sh.from_len = (cs->transport == UDP_SOCK) ? sockaddru_len(cs->u.sa_in) + : sizeof(cs->u.sa_un); again: - bytes=recvfrom(cs->fd, buf, DGRAM_BUF_SIZE, 0, &sh.from.sa_in.s, - &sh.from_len); - if (bytes==-1){ - if ((errno==EAGAIN)||(errno==EWOULDBLOCK)){ - ret=0; + bytes = recvfrom( + cs->fd, buf, DGRAM_BUF_SIZE, 0, &sh.from.sa_in.s, &sh.from_len); + if(bytes == -1) { + if((errno == EAGAIN) || (errno == EWOULDBLOCK)) { + ret = 0; goto skip; - }else if (errno==EINTR){ + } else if(errno == EINTR) { goto again; } LOG(L_ERR, "ERROR; handle_ctrl_dgram: recvfrom on %s: [%d] %s\n", @@ -358,10 +366,10 @@ static int handle_ctrl_dgram(struct ctrl_socket* cs) goto error; } DBG("handle_ctrl_dgram: new packet on %s\n", cs->name); - ret=1; + ret = 1; #ifdef USE_FIFO - if (cs->p_proto==P_FIFO) - fifo_process((char*)buf, bytes, &bytes_needed, &sh, &saved_state); + if(cs->p_proto == P_FIFO) + fifo_process((char *)buf, bytes, &bytes_needed, &sh, &saved_state); else #endif process_rpc_req(buf, bytes, &bytes_needed, &sh, &saved_state); @@ -373,7 +381,6 @@ static int handle_ctrl_dgram(struct ctrl_socket* cs) } - /* handles a new connect on one of the watched stream connections * * params: cs - pointer to the control socket for which we have an io ev. @@ -388,58 +395,60 @@ static int handle_ctrl_dgram(struct ctrl_socket* cs) * queued -- the receive buffer might still be non-empty) * */ -static int handle_new_connect(struct ctrl_socket* cs) +static int handle_new_connect(struct ctrl_socket *cs) { int ret; union sockaddr_u from; unsigned int from_len; int new_sock; - struct stream_connection* s_conn; + struct stream_connection *s_conn; - from_len=(cs->transport==UDP_SOCK)?sockaddru_len(cs->u.sa_in): - sizeof(cs->u.sa_un); + from_len = (cs->transport == UDP_SOCK) ? sockaddru_len(cs->u.sa_in) + : sizeof(cs->u.sa_un); again: - new_sock=accept(cs->fd, &from.sa_in.s, &from_len); - if (new_sock==-1){ - if ((errno==EAGAIN)||(errno==EWOULDBLOCK)){ - ret=0; + new_sock = accept(cs->fd, &from.sa_in.s, &from_len); + if(new_sock == -1) { + if((errno == EAGAIN) || (errno == EWOULDBLOCK)) { + ret = 0; goto skip; - }else if (errno==EINTR){ + } else if(errno == EINTR) { goto again; } - LOG(L_ERR, "ERROR: io_listen: handle_new_connect:" + LOG(L_ERR, + "ERROR: io_listen: handle_new_connect:" " error while accepting connection on %s: [%d] %s\n", cs->name, errno, strerror(errno)); goto error; } - ret=1; - if (io_read_connections>=MAX_IO_READ_CONNECTIONS){ - LOG(L_ERR, "ERROR: io listen: maximum number of connections" + ret = 1; + if(io_read_connections >= MAX_IO_READ_CONNECTIONS) { + LOG(L_ERR, + "ERROR: io listen: maximum number of connections" " exceeded: %d/%d\n", io_read_connections, MAX_IO_READ_CONNECTIONS); close(new_sock); goto skip; /* success because accept was successful */ } - if (init_sock_opt(new_sock, cs->transport)<0){ + if(init_sock_opt(new_sock, cs->transport) < 0) { LOG(L_ERR, "ERROR: io listen: handle_new_connect:" - " init_sock_opt failed\n"); + " init_sock_opt failed\n"); close(new_sock); goto skip; } /* add socket to the list */ - s_conn=s_conn_new(new_sock, cs, &from); - if (s_conn){ + s_conn = s_conn_new(new_sock, cs, &from); + if(s_conn) { s_conn_add(s_conn); io_watch_add(&ctl_io_h, s_conn->fd, POLLIN, F_T_READ_STREAM, s_conn); - }else{ + } else { LOG(L_ERR, "ERROR: io listen: handle_new_connect:" - " s_conn_new failed\n"); + " s_conn_new failed\n"); close(new_sock); goto skip; } io_read_connections++; - DBG("handle_stream read: new connection (%d) on %s\n", - io_read_connections, cs->name); + DBG("handle_stream read: new connection (%d) on %s\n", io_read_connections, + cs->name); skip: return ret; error: @@ -447,7 +456,6 @@ static int handle_new_connect(struct ctrl_socket* cs) } - /* handles a read event on one of the accepted connections * * params: s_c - pointer to the stream_connection for which we have an io ev. @@ -464,83 +472,85 @@ static int handle_new_connect(struct ctrl_socket* cs) * queued -- the receive buffer might still be non-empty) * */ -static int handle_stream_read(struct stream_connection* s_c, int idx) +static int handle_stream_read(struct stream_connection *s_c, int idx) { int bytes_free; int bytes_read; int bytes_needed; int bytes_processed; - struct stream_req* r; + struct stream_req *r; struct send_handle sh; - sh.fd=s_c->fd; - sh.type=S_CONNECTED; - sh.from_len=0; - r=&s_c->req; - bytes_free=STREAM_BUF_SIZE-(int)(r->end-r->buf); - if (bytes_free==0){ + sh.fd = s_c->fd; + sh.type = S_CONNECTED; + sh.from_len = 0; + r = &s_c->req; + bytes_free = STREAM_BUF_SIZE - (int)(r->end - r->buf); + if(bytes_free == 0) { LOG(L_ERR, "ERROR: handle_stream_read: buffer overrun\n"); goto close_connection; } again: - bytes_read=read(s_c->fd, r->end, bytes_free); - if (bytes_read==-1){ - if ((errno==EAGAIN)||(errno==EWOULDBLOCK)){ + bytes_read = read(s_c->fd, r->end, bytes_free); + if(bytes_read == -1) { + if((errno == EAGAIN) || (errno == EWOULDBLOCK)) { goto no_read; /* nothing has been read */ - }else if (errno==EINTR) goto again; + } else if(errno == EINTR) + goto again; LOG(L_ERR, "ERROR: handle_stream_read: error reading: %s [%d]\n", strerror(errno), errno); goto error_read; - }else if(bytes_read==0){ /* eof */ + } else if(bytes_read == 0) { /* eof */ DBG("handle_stream read: eof on %s\n", s_c->parent->name); goto close_connection; } LM_DBG("bytes read: %d\n", bytes_read); - r->end+=bytes_read; - if (bytes_read && (bytes_readbytes_to_go)){ - r->bytes_to_go-=bytes_read; + r->end += bytes_read; + if(bytes_read && (bytes_read < r->bytes_to_go)) { + r->bytes_to_go -= bytes_read; goto skip; /* not enough bytes read, no point in trying to process them */ } - do{ + do { #ifdef USE_FIFO - if (s_c->p_proto==P_FIFO) - bytes_processed=fifo_process((char*)r->proc, (int)(r->end-r->proc), - &bytes_needed, &sh, &s_c->saved_state); + if(s_c->p_proto == P_FIFO) + bytes_processed = + fifo_process((char *)r->proc, (int)(r->end - r->proc), + &bytes_needed, &sh, &s_c->saved_state); else #endif - bytes_processed=process_rpc_req(r->proc, (int)(r->end-r->proc), - &bytes_needed, &sh, &s_c->saved_state); - if (bytes_processed<0){ + bytes_processed = process_rpc_req(r->proc, (int)(r->end - r->proc), + &bytes_needed, &sh, &s_c->saved_state); + if(bytes_processed < 0) { /* error while processing the packet => close the connection */ goto close_connection; } LM_DBG("bytes processed: %d\n", bytes_processed); - r->proc+=bytes_processed; - r->bytes_to_go=bytes_needed; - if (bytes_needed>0){ - if (bytes_read==0){ /*want more bytes, but we have eof*/ + r->proc += bytes_processed; + r->bytes_to_go = bytes_needed; + if(bytes_needed > 0) { + if(bytes_read == 0) { /*want more bytes, but we have eof*/ LOG(L_ERR, "ERROR: handle_stream_read: unexpected EOF\n"); goto close_connection; } break; /* no more read bytes ready for processing */ } /* if (bytes_needed==0) -- packet fully processed */ - s_c->saved_state=0; /* reset per datagram state */ - }while(r->procend); + s_c->saved_state = 0; /* reset per datagram state */ + } while(r->proc < r->end); /* free some space in the buffer */ - if (r->proc>r->buf){ - if (r->end>r->proc){ - memmove(r->buf, r->proc, (int)(r->end-r->proc)); - r->end-=(int)(r->proc-r->buf); - }else{ - r->end=r->buf; + if(r->proc > r->buf) { + if(r->end > r->proc) { + memmove(r->buf, r->proc, (int)(r->end - r->proc)); + r->end -= (int)(r->proc - r->buf); + } else { + r->end = r->buf; } - r->proc=r->buf; + r->proc = r->buf; } skip: /* everything went fine, we just have to read more */ - s_c->expire=get_ticks_raw()+IO_STREAM_CONN_TIMEOUT; /* update timeout*/ + s_c->expire = get_ticks_raw() + IO_STREAM_CONN_TIMEOUT; /* update timeout*/ return 1; no_read: @@ -560,7 +570,6 @@ static int handle_stream_read(struct stream_connection* s_c, int idx) } - #ifdef USE_FIFO /* handles a read event on one of the fifos * @@ -578,92 +587,93 @@ static int handle_stream_read(struct stream_connection* s_c, int idx) * queued -- the receive buffer might still be non-empty) * */ -static int handle_fifo_read(struct ctrl_socket* cs, int idx) +static int handle_fifo_read(struct ctrl_socket *cs, int idx) { int bytes_free; int bytes_read; int bytes_needed; int bytes_processed; - struct stream_req* r; + struct stream_req *r; struct send_handle sh; - struct stream_connection* sc; - - sh.fd=-1; - sh.type=S_FIFO; - sh.from_len=0; - sc=(struct stream_connection *)cs->data; - r=&(sc->req); - bytes_free=STREAM_BUF_SIZE-(int)(r->end-r->buf); - if (bytes_free==0){ + struct stream_connection *sc; + + sh.fd = -1; + sh.type = S_FIFO; + sh.from_len = 0; + sc = (struct stream_connection *)cs->data; + r = &(sc->req); + bytes_free = STREAM_BUF_SIZE - (int)(r->end - r->buf); + if(bytes_free == 0) { LOG(L_ERR, "ERROR: handle_stream_read: buffer overrun\n"); goto error; } again: - bytes_read=read(cs->fd, r->end, bytes_free); - if (bytes_read==-1){ - if ((errno==EAGAIN)||(errno==EWOULDBLOCK)){ + bytes_read = read(cs->fd, r->end, bytes_free); + if(bytes_read == -1) { + if((errno == EAGAIN) || (errno == EWOULDBLOCK)) { goto no_read; /* nothing has been read */ - }else if (errno==EINTR) goto again; + } else if(errno == EINTR) + goto again; LOG(L_ERR, "ERROR: handle_fifo_read: error reading: %s [%d]\n", strerror(errno), errno); goto error_read; - }else if(bytes_read==0){ /* eof */ + } else if(bytes_read == 0) { /* eof */ DBG("handle_fifo_read: eof on %s\n", cs->name); } - r->end+=bytes_read; - if (bytes_read && (bytes_readbytes_to_go)){ - r->bytes_to_go-=bytes_read; + r->end += bytes_read; + if(bytes_read && (bytes_read < r->bytes_to_go)) { + r->bytes_to_go -= bytes_read; goto skip; /* not enough bytes read, no point in trying to process them */ } - do{ - bytes_processed=fifo_process((char*)r->proc, (int)(r->end-r->proc), - &bytes_needed, &sh, &sc->saved_state); - if (bytes_processed<0){ + do { + bytes_processed = fifo_process((char *)r->proc, (int)(r->end - r->proc), + &bytes_needed, &sh, &sc->saved_state); + if(bytes_processed < 0) { /* error while processing the packet => skip */ goto discard; } - r->proc+=bytes_processed; - r->bytes_to_go=bytes_needed; - if (bytes_needed>0){ - if (bytes_read==0){ /*want more bytes, but we have eof*/ + r->proc += bytes_processed; + r->bytes_to_go = bytes_needed; + if(bytes_needed > 0) { + if(bytes_read == 0) { /*want more bytes, but we have eof*/ LOG(L_ERR, "ERROR: handle_fifo_read: unexpected EOF\n"); goto discard; /* discard buffered contents */ } break; /* no more read bytes ready for processing */ } /* if (bytes_needed==0) -- packet fully processed */ - sc->saved_state=0; /* reset per datagram state */ - if (bytes_processed==0){ + sc->saved_state = 0; /* reset per datagram state */ + if(bytes_processed == 0) { /* nothing processed, nothing needed, no error - looks like * a bug */ LOG(L_ERR, "ERROR: handle_fifo_read: unexpected return\n"); goto discard; } - }while(r->procend); + } while(r->proc < r->end); /* free some space in the buffer */ - if (r->proc>r->buf){ - if (r->end>r->proc){ - memmove(r->buf, r->proc, (int)(r->end-r->proc)); - r->end-=(int)(r->proc-r->buf); - }else{ - r->end=r->buf; + if(r->proc > r->buf) { + if(r->end > r->proc) { + memmove(r->buf, r->proc, (int)(r->end - r->proc)); + r->end -= (int)(r->proc - r->buf); + } else { + r->end = r->buf; } - r->proc=r->buf; + r->proc = r->buf; } skip: /* everything went fine, we just have to read more */ return 1; error_read: /* temporary read error ? */ - r->proc=r->buf; - r->end=r->buf; + r->proc = r->buf; + r->end = r->buf; return -1; discard: /* reset the whole receive buffer */ - r->proc=r->buf; - r->end=r->buf; - sc->saved_state=0; /* reset saved state */ + r->proc = r->buf; + r->end = r->buf; + sc->saved_state = 0; /* reset saved state */ return 1; no_read: /* false alarm */ @@ -671,12 +681,11 @@ static int handle_fifo_read(struct ctrl_socket* cs, int idx) error: return 1; /* there's nothing wrong with the fifo, just a bad application packet */ - /* close connection */ + /* close connection */ } #endif - /* generic handle io routine, it will call the appropiate * handle_xxx() based on the fd_map type * @@ -690,26 +699,26 @@ static int handle_fifo_read(struct ctrl_socket* cs, int idx) * >0 on successfull read from the fd (when there might be more io * queued -- the receive buffer might still be non-empty) */ -inline static int handle_io(struct fd_map* fm, short events, int idx) +inline static int handle_io(struct fd_map *fm, short events, int idx) { int ret; /* update the local config */ cfg_update(); - switch(fm->type){ + switch(fm->type) { case F_T_CTRL_DGRAM: - ret=handle_ctrl_dgram((struct ctrl_socket*)fm->data); + ret = handle_ctrl_dgram((struct ctrl_socket *)fm->data); break; case F_T_CTRL_STREAM: - ret=handle_new_connect((struct ctrl_socket*)fm->data); + ret = handle_new_connect((struct ctrl_socket *)fm->data); break; case F_T_READ_STREAM: - ret=handle_stream_read((struct stream_connection*)fm->data, idx); + ret = handle_stream_read((struct stream_connection *)fm->data, idx); break; #ifdef USE_FIFO case F_T_FIFO: - ret=handle_fifo_read((struct ctrl_socket*)fm->data, idx); + ret = handle_fifo_read((struct ctrl_socket *)fm->data, idx); break; #endif case F_T_RESERVED: @@ -726,33 +735,33 @@ inline static int handle_io(struct fd_map* fm, short events, int idx) } - -void io_listen_who_rpc(rpc_t* rpc, void* ctx) +void io_listen_who_rpc(rpc_t *rpc, void *ctx) { - struct stream_connection* sc; + struct stream_connection *sc; struct ip_addr ip; int port; int i; - i=0; + i = 0; /* check if called from another process */ - if (stream_conn_lst.next==0){ + if(stream_conn_lst.next == 0) { rpc->fault(ctx, 606, "rpc available only over binrpc (ctl)"); return; } /* p_proto transport from sport to tport*/ - clist_foreach(&stream_conn_lst, sc, next){ + clist_foreach(&stream_conn_lst, sc, next) + { i++; rpc->add(ctx, "ss", payload_proto_name(sc->parent->p_proto), - socket_proto_name(sc->parent->transport)); - switch(sc->parent->transport){ + socket_proto_name(sc->parent->transport)); + switch(sc->parent->transport) { case UDP_SOCK: case TCP_SOCK: su2ip_addr(&ip, &sc->from.sa_in); - port=su_getport(&sc->from.sa_in); + port = su_getport(&sc->from.sa_in); rpc->add(ctx, "ss", ip_addr2a(&ip), int2str(port, 0)); su2ip_addr(&ip, &sc->parent->u.sa_in); - port=su_getport(&sc->parent->u.sa_in); + port = su_getport(&sc->parent->u.sa_in); rpc->add(ctx, "ss", ip_addr2a(&ip), int2str(port, 0)); break; case UNIXS_SOCK: @@ -760,29 +769,27 @@ void io_listen_who_rpc(rpc_t* rpc, void* ctx) #ifdef USE_FIFO case FIFO_SOCK: #endif - rpc->add(ctx, "ss", "", "" ); + rpc->add(ctx, "ss", "", ""); rpc->add(ctx, "ss", sc->parent->name, ""); break; default: - rpc->add(ctx, "ssss", "", - "", "", "", ""); + rpc->add(ctx, "ssss", "", "", "", "", ""); } /* idle time * rpc->add(ctx, "d", TICKS_TO_MS(get_ticks_raw()- (sc->expire-IO_STREAM_CONN_TIMEOUT)));*/ } - if (i==0){ + if(i == 0) { rpc->fault(ctx, 400, "no open stream connection"); } } - -void io_listen_conn_rpc(rpc_t* rpc, void* ctx) +void io_listen_conn_rpc(rpc_t *rpc, void *ctx) { /* check if called from another process */ - if (stream_conn_lst.next==0){ + if(stream_conn_lst.next == 0) { rpc->fault(ctx, 606, "rpc available only over binrpc (ctl)"); return; } diff --git a/src/modules/ctl/io_listener.h b/src/modules/ctl/io_listener.h index bb427a0bdae..fab4ea32441 100644 --- a/src/modules/ctl/io_listener.h +++ b/src/modules/ctl/io_listener.h @@ -22,17 +22,21 @@ #define _io_listener_h #include "ctrl_socks.h" -#define MAX_IO_READ_CONNECTIONS 128 /* FIXME: make it a config var */ +#define MAX_IO_READ_CONNECTIONS 128 /* FIXME: make it a config var */ -enum sock_con_type { S_CONNECTED, S_DISCONNECTED +enum sock_con_type +{ + S_CONNECTED, + S_DISCONNECTED #ifdef USE_FIFO - , S_FIFO + , + S_FIFO #endif }; - -struct send_handle{ +struct send_handle +{ int fd; int type; union sockaddr_u from; @@ -40,10 +44,9 @@ struct send_handle{ }; +int sock_send_v(void *h, struct iovec *v, size_t count); -int sock_send_v(void *h, struct iovec* v, size_t count); - -void io_listen_loop(int fd_no, struct ctrl_socket* cs_lst); +void io_listen_loop(int fd_no, struct ctrl_socket *cs_lst); #endif