int ei_x_encode_error_tuple_atom(ei_x_buff *buff, char *atom) { EI(ei_x_encode_version(buff)); EI(ei_x_encode_tuple_header(buff, 2)); EI(ei_x_encode_atom(buff, "error")); EI(ei_x_encode_atom(buff, atom)); return 0; }
int ei_x_encode_error_tuple_string(ei_x_buff *buff, char *str) { EI(ei_x_encode_version(buff)); EI(ei_x_encode_tuple_header(buff, 2)); EI(ei_x_encode_atom(buff, "error")); EI(ei_x_encode_string(buff, str)); return 0; }
void reset_complete_trees(struct exmpp_xml_ctx *ctx) { ctx->complete_trees_ready = 0; ctx->complete_trees->index = 0; ei_x_encode_version(ctx->complete_trees); }
static ErlDrvSSizeT do_call(ErlDrvData drv_data, unsigned int command, char *buf, ErlDrvSizeT len, char **rbuf, ErlDrvSizeT rlen, unsigned *ret_flags) { int nlen; ei_x_buff x; switch (command) { case 0: *rbuf = buf; *ret_flags |= DRIVER_CALL_KEEP_BUFFER; return len; case 1: ei_x_new(&x); ei_x_format(&x, "{[], a, [], b, c}"); nlen = x.index; if (nlen > rlen) { *rbuf =driver_alloc(nlen); } memcpy(*rbuf,x.buff,nlen); ei_x_free(&x); return nlen; case 2: ei_x_new(&x); ei_x_encode_version(&x); ei_x_encode_tuple_header(&x,2); ei_x_encode_atom(&x,"return"); ei_x_append_buf(&x,buf+1,len-1); nlen = x.index; if (nlen > rlen) { *rbuf =driver_alloc(nlen); } memcpy(*rbuf,x.buff,nlen); ei_x_free(&x); return nlen; default: return -1; } }
int ei_x_format(ei_x_buff* x, const char* fmt, ... ) { va_list ap; union arg* args; union arg* saved_args; int res; res = ei_x_encode_version(x); if (res < 0) return res; va_start(ap, fmt); res = read_args(fmt,ap,&args); saved_args = args; va_end(ap); if (res < 0) { return -1; } res = eiformat(&fmt, &args, x); ei_free(saved_args); return res; }
int main(void) #endif { ErlConnect conp; Erl_IpAddr thisipaddr = (Erl_IpAddr)0; FILE *fp = (FILE *)0; char* charp = "foo"; double *doublep = NULL; double doublex = 0.0; ei_cnode xec; ei_reg *ei_regp = NULL; ei_term eterm; ei_x_buff eix; erlang_big *bigp = NULL; erlang_fun efun; erlang_msg *msgp = NULL; erlang_msg emsg; erlang_pid *pidp = NULL; erlang_pid epid; erlang_port eport; erlang_ref eref; erlang_trace etrace; int *intp = NULL; int intx = 0; long *longp = NULL; long longx = 0; short creation = 0; struct ei_reg_stat *ei_reg_statp = NULL; struct ei_reg_tabstat *ei_reg_tabstatp = NULL; struct hostent *hostp = NULL; unsigned char * ucharp = (unsigned char *)"foo"; unsigned long *ulongp = NULL; unsigned long ulongx = 0; void *voidp = NULL; #ifndef VXWORKS EI_LONGLONG *longlongp = (EI_LONGLONG*)NULL; EI_LONGLONG longlongx = 0; EI_ULONGLONG *ulonglongp = (EI_ULONGLONG*)NULL; EI_ULONGLONG ulonglongx = 0; #endif enum erlang_char_encoding enc; intx = erl_errno; ei_connect_init(&xec, charp, charp, creation); ei_connect_xinit (&xec, charp, charp, charp, thisipaddr, charp, creation); ei_connect(&xec, charp); ei_xconnect (&xec, thisipaddr, charp); ei_receive(intx, ucharp, intx); ei_receive_msg(intx, &emsg, &eix); ei_xreceive_msg(intx, &emsg, &eix); ei_send(intx, &epid, charp, intx); ei_reg_send(&xec, intx, charp, charp, intx); ei_rpc(&xec, intx, charp, charp, charp, intx, &eix); ei_rpc_to(&xec, intx, charp, charp, charp, intx); ei_rpc_from(&xec, intx, intx, &emsg, &eix); ei_publish(&xec, intx); ei_accept(&xec, intx, &conp); ei_unpublish(&xec); ei_thisnodename(&xec); ei_thishostname(&xec); ei_thisalivename(&xec); ei_self(&xec); ei_gethostbyname(charp); ei_gethostbyaddr(charp, intx, intx); ei_gethostbyname_r(charp, hostp, charp, intx, intp); ei_gethostbyaddr_r(charp, intx, intx, hostp, charp, intx, intp); ei_encode_version(charp, intp); ei_x_encode_version(&eix); ei_encode_long(charp, intp, longx); ei_x_encode_long(&eix, longx); ei_encode_ulong(charp, intp, ulongx); ei_x_encode_ulong(&eix, ulongx); ei_encode_double(charp, intp, doublex); ei_x_encode_double(&eix, doublex); ei_encode_boolean(charp, intp, intx); ei_x_encode_boolean(&eix, intx); ei_encode_char(charp, intp, 'a'); ei_x_encode_char(&eix, 'a'); ei_encode_string(charp, intp, charp); ei_encode_string_len(charp, intp, charp, intx); ei_x_encode_string(&eix, charp); ei_x_encode_string_len(&eix, charp, intx); ei_encode_atom(charp, intp, charp); ei_encode_atom_as(charp, intp, charp, ERLANG_LATIN1, ERLANG_UTF8); ei_encode_atom_len(charp, intp, charp, intx); ei_encode_atom_len_as(charp, intp, charp, intx, ERLANG_ASCII, ERLANG_LATIN1); ei_x_encode_atom(&eix, charp); ei_x_encode_atom_as(&eix, charp, ERLANG_LATIN1, ERLANG_UTF8); ei_x_encode_atom_len(&eix, charp, intx); ei_x_encode_atom_len_as(&eix, charp, intx, ERLANG_LATIN1, ERLANG_UTF8); ei_encode_binary(charp, intp, (void *)0, longx); ei_x_encode_binary(&eix, (void*)0, intx); ei_encode_pid(charp, intp, &epid); ei_x_encode_pid(&eix, &epid); ei_encode_fun(charp, intp, &efun); ei_x_encode_fun(&eix, &efun); ei_encode_port(charp, intp, &eport); ei_x_encode_port(&eix, &eport); ei_encode_ref(charp, intp, &eref); ei_x_encode_ref(&eix, &eref); ei_encode_trace(charp, intp, &etrace); ei_x_encode_trace(&eix, &etrace); ei_encode_tuple_header(charp, intp, intx); ei_x_encode_tuple_header(&eix, longx); ei_encode_list_header(charp, intp, intx); ei_x_encode_list_header(&eix, longx); /* #define ei_encode_empty_list(buf,i) ei_encode_list_header(buf,i,0) */ ei_x_encode_empty_list(&eix); ei_get_type(charp, intp, intp, intp); ei_get_type_internal(charp, intp, intp, intp); ei_decode_version(charp, intp, intp); ei_decode_long(charp, intp, longp); ei_decode_ulong(charp, intp, ulongp); ei_decode_double(charp, intp, doublep); ei_decode_boolean(charp, intp, intp); ei_decode_char(charp, intp, charp); ei_decode_string(charp, intp, charp); ei_decode_atom(charp, intp, charp); ei_decode_atom_as(charp, intp, charp, MAXATOMLEN_UTF8, ERLANG_WHATEVER, &enc, &enc); ei_decode_binary(charp, intp, (void *)0, longp); ei_decode_fun(charp, intp, &efun); free_fun(&efun); ei_decode_pid(charp, intp, &epid); ei_decode_port(charp, intp, &eport); ei_decode_ref(charp, intp, &eref); ei_decode_trace(charp, intp, &etrace); ei_decode_tuple_header(charp, intp, intp); ei_decode_list_header(charp, intp, intp); ei_decode_ei_term(charp, intp, &eterm); ei_print_term(fp, charp, intp); ei_s_print_term(&charp, charp, intp); ei_x_format(&eix, charp); ei_x_format_wo_ver(&eix, charp); ei_x_new(&eix); ei_x_new_with_version(&eix); ei_x_free(&eix); ei_x_append(&eix, &eix); ei_x_append_buf(&eix, charp, intx); ei_skip_term(charp, intp); ei_reg_open(intx); ei_reg_resize(ei_regp, intx); ei_reg_close(ei_regp); ei_reg_setival(ei_regp, charp, longx); ei_reg_setfval(ei_regp, charp, doublex); ei_reg_setsval(ei_regp, charp, charp); ei_reg_setpval(ei_regp, charp, voidp, intx); ei_reg_setval(ei_regp, charp, intx); ei_reg_getival(ei_regp, charp); ei_reg_getfval(ei_regp, charp); ei_reg_getsval(ei_regp, charp); ei_reg_getpval(ei_regp, charp, intp); ei_reg_getval(ei_regp, charp, intx); ei_reg_markdirty(ei_regp, charp); ei_reg_delete(ei_regp, charp); ei_reg_stat(ei_regp, charp, ei_reg_statp); ei_reg_tabstat(ei_regp, ei_reg_tabstatp); ei_reg_dump(intx, ei_regp, charp, intx); ei_reg_restore(intx, ei_regp, charp); ei_reg_purge(ei_regp); #if defined(HAVE_GMP_H) && defined(HAVE_LIBGMP) { mpz_t obj; ei_decode_bignum(charp, intp, obj); ei_encode_bignum(charp, intp, obj); ei_x_encode_bignum(&eix, obj); } #endif /* HAVE_GMP_H && HAVE_LIBGMP */ #ifndef VXWORKS ei_decode_longlong(charp, intp, longlongp); ei_decode_ulonglong(charp, intp, ulonglongp); ei_encode_longlong(charp, intp, longlongx); ei_encode_ulonglong(charp, intp, ulonglongx); ei_x_encode_longlong(&eix, longlongx); ei_x_encode_ulonglong(&eix, ulonglongx); #endif #ifdef USE_EI_UNDOCUMENTED ei_decode_intlist(charp, intp, longp, intp); ei_receive_encoded(intx, &charp, intp, msgp, intp); ei_send_encoded(intx, pidp, charp, intx); ei_send_reg_encoded(intx, pidp, charp, charp, intx); ei_decode_big(charp, intp, bigp); ei_big_comp(bigp, bigp); ei_big_to_double(bigp, doublep); ei_small_to_big(intx, bigp); ei_alloc_big(intx); ei_free_big(bigp); #endif /* USE_EI_UNDOCUMENTED */ return BUFSIZ + EAGAIN + EHOSTUNREACH + EIO + EI_BIN + EI_DELET + EI_DIRTY + EI_FLT + EI_FORCE + EI_INT + EI_NOPURGE + EI_STR + EMSGSIZE + ENOMEM + ERL_ERROR + ERL_EXIT + ERL_LINK + ERL_MSG + ERL_NO_TIMEOUT + ERL_REG_SEND + ERL_SEND + ERL_TICK + ERL_TIMEOUT + ERL_UNLINK + ETIMEDOUT + MAXATOMLEN; }
int ei_x_encode_ok(ei_x_buff *buff) { EI(ei_x_encode_version(buff)); EI(ei_x_encode_atom(buff, "ok")); return 0; }
int ei_x_encode_ok_tuple_header(ei_x_buff *buff) { EI(ei_x_encode_version(buff)); EI(ei_x_encode_tuple_header(buff, 2)); EI(ei_x_encode_atom(buff, "ok")); return 0; }
int cmd_erlang_info(struct sip_msg* msg, char *cn, char *rp, char *ar) { #define AVP_PRINTBUF_SIZE 1024 static char printbuf[AVP_PRINTBUF_SIZE]; int printbuf_len, bytessent; ei_x_buff argbuf; struct nodes_list* node; struct erlang_cmd *erl_cmd; erlang_pid erl_pid; str conname, regproc; int retcode = -1; if(msg==NULL) { LM_ERR("cmd_erlang_info: received null msg\n"); return -1; } if(fixup_get_svalue(msg, (gparam_p)cn, &conname)<0) { LM_ERR("cmd_erlang_info: cannot get the connection name\n"); return -1; } for(node=nodes_lst;node;node=node->next) { LM_DBG("cmd_erlang_info: matching %s with %.*s\n",node->name,conname.len,conname.s); if(strcmp(node->name, conname.s)==0) break; } if(node==0){ LM_ERR("cmd_erlang_info: no such connection %.*s\n",conname.len,conname.s); return -1; } if(fixup_get_svalue(msg, (gparam_p)rp, ®proc)<0) { LM_ERR("cmd_erlang_info: cannot get the regproc name\n"); return -1; } printbuf_len = AVP_PRINTBUF_SIZE-1; if(pv_printf(msg, (pv_elem_p)ar, printbuf, &printbuf_len)<0 || printbuf_len<=0) { LM_ERR("cmd_erlang_info: cannot expand erlang message body\n"); return -1; } erl_cmd=shm_malloc(sizeof(struct erlang_cmd)); if(!erl_cmd) { LM_ERR("no shm"); return -1; } memset(erl_cmd,0,sizeof(struct erlang_cmd)); erl_cmd->erlbuf=shm_malloc(AVP_PRINTBUF_SIZE); if (!erl_cmd->erlbuf) { LM_ERR("no shm memory\n\n"); goto error; } if(lock_init(&(erl_cmd->lock))==NULL) { LM_ERR("cannot init the lock\n"); goto error; } erl_cmd->reg_name=shm_strdup(®proc); if (!erl_cmd->reg_name) { LM_ERR("no shm memory\n\n"); goto error; } LM_DBG("cmd_erlang_info: %.*s %.*s %.*s\n",conname.len,conname.s, regproc.len,regproc.s, printbuf_len,printbuf); erl_cmd->cmd=ERLANG_INFO; erl_cmd->node=node; argbuf.buff=erl_cmd->erlbuf; argbuf.buffsz=AVP_PRINTBUF_SIZE-1; argbuf.index=0; ei_x_encode_version(&argbuf); //User passed term will be sedond element of tuple, first is our erlang pid ei_x_encode_tuple_header(&argbuf, 2); // we are hacking erlang pid so we can have worker system pid here memcpy(&erl_pid,ei_self(&(node->ec)),sizeof(erlang_pid)); erl_pid.num=getpid(); ei_x_encode_pid(&argbuf, &erl_pid); //so much for pid, now encode term if(ei_x_format_wo_ver(&argbuf, printbuf)!=0) { LM_ERR("cannot fromat erlang binary from arg string\n"); goto error; } // ei_x_encode_atom(&argbuf,"user"); erl_cmd->erlbuf_len=argbuf.index; lock_get(&(erl_cmd->lock)); bytessent=write(pipe_fds[1], &erl_cmd, sizeof(erl_cmd)); LM_DBG("cmd_erlang_info: locked, sent %d %d %s %d %p %p, waiting for release\n",bytessent, erl_cmd->cmd,erl_cmd->reg_name,erl_cmd->erlbuf_len,erl_cmd->erlbuf, erl_cmd); lock_get(&(erl_cmd->lock)); LM_DBG("after lock\n"); if (erl_cmd->retcode <0) { retcode=erl_cmd->retcode; LM_DBG("cmd_erlang_info: failed %d\n",retcode); goto error; } retcode=1; error: if(erl_cmd) { if(erl_cmd->erlbuf) shm_free(erl_cmd->erlbuf); if(erl_cmd->reg_name) shm_free(erl_cmd->reg_name); shm_free(erl_cmd); } return retcode; }
int cmd_erlang_rex(struct sip_msg* msg, char *cn , char *mo, char *fu, char *ar, char *_ret_pv) { #define AVP_PRINTBUF_SIZE 1024 static char printbuf[AVP_PRINTBUF_SIZE]; int printbuf_len, bytessent; ei_x_buff argbuf; struct nodes_list* node; struct erlang_cmd *erl_cmd; erlang_pid erl_pid; str conname, regproc; str mod, fun; int retcode = -1; if(msg==NULL) { LM_ERR("cmd_erlang_rex: received null msg\n"); return -1; } if(fixup_get_svalue(msg, (gparam_p)cn, &conname)<0) { LM_ERR("cmd_erlang_rex: cannot get the connection name\n"); return -1; } for(node=nodes_lst;node;node=node->next) { LM_DBG("matching %s with %.*s\n",node->name,conname.len,conname.s); if(strcmp(node->name, conname.s)==0) break; } if(node==0){ LM_ERR("cmd_erlang_rex: no such connection %.*s\n",conname.len,conname.s); return -1; } if(fixup_get_svalue(msg, (gparam_p)mo, &mod)<0) { LM_ERR("cmd_erlang_rex: cannot get the mod name\n"); return -1; } if(fixup_get_svalue(msg, (gparam_p)fu, &fun)<0) { LM_ERR("cmd_erlang_rex: cannot get the fun name\n"); return -1; } printbuf_len = AVP_PRINTBUF_SIZE-1; if(pv_printf(msg, (pv_elem_p)ar, printbuf, &printbuf_len)<0 || printbuf_len<=0) { LM_ERR("erlang_cmd_rex: cannot expand args expression.\n"); return -1; } erl_cmd=shm_malloc(sizeof(struct erlang_cmd)); if(!erl_cmd) { LM_ERR("no shm"); return -1; } memset(erl_cmd,0,sizeof(struct erlang_cmd)); erl_cmd->erlbuf=shm_malloc(AVP_PRINTBUF_SIZE); if (!erl_cmd->erlbuf) { LM_ERR("no shm memory\n\n"); goto error; } erl_cmd->ret_pv = (pv_spec_t*)shm_malloc(sizeof(pv_spec_t)); if (!erl_cmd->ret_pv) { LM_ERR("no shm memory\n\n"); goto error; } memcpy(erl_cmd->ret_pv, (pv_spec_t *)_ret_pv, sizeof(pv_spec_t)); if(lock_init(&(erl_cmd->lock))==NULL) { LM_ERR("cannot init the lock\n"); goto error; } regproc.s="rex"; regproc.len=3; erl_cmd->reg_name=shm_strdup(®proc); if (!erl_cmd->reg_name) { LM_ERR("no shm memory\n\n"); goto error; } LM_DBG("cmd_erlang_rex: %.*s rex %.*s\n",conname.len,conname.s, printbuf_len,printbuf); erl_cmd->cmd=ERLANG_REX; erl_cmd->node=node; argbuf.buff=erl_cmd->erlbuf; argbuf.buffsz=AVP_PRINTBUF_SIZE-1; argbuf.index=0; ei_x_encode_version(&argbuf); //User passed term will be second element of tuple, first is our (hacked) erlang pid ei_x_encode_tuple_header(&argbuf, 2); // we are hacking erlang pid so we can have worker system pid here memcpy(&erl_pid,ei_self(&(node->ec)),sizeof(erlang_pid)); erl_pid.num=getpid(); ei_x_encode_pid(&argbuf, &erl_pid); erl_cmd->num=erl_pid.num; erl_cmd->serial=erl_pid.serial; //so much for pid, now encode rex call tuple {call, mod, fun, arg, user} ei_x_encode_tuple_header(&argbuf, 5); ei_x_encode_atom(&argbuf,"call"); ei_x_encode_atom_len(&argbuf, mod.s, mod.len); ei_x_encode_atom_len(&argbuf, fun.s, fun.len); if(ei_x_format_wo_ver(&argbuf, printbuf)!=0) { LM_ERR("cannot fromat erlang binary from arg string\n"); goto error; } ei_x_encode_atom(&argbuf,"user"); erl_cmd->erlbuf_len=argbuf.index; lock_get(&(erl_cmd->lock)); bytessent=write(pipe_fds[1], &erl_cmd, sizeof(erl_cmd)); LM_DBG("cmd_erlang_call: locked, sent %d %d %s %d %p %p, waiting for release\n",bytessent, erl_cmd->cmd,erl_cmd->reg_name,erl_cmd->erlbuf_len,erl_cmd->erlbuf, erl_cmd); lock_get(&(erl_cmd->lock)); LM_DBG("after lock\n"); if (erl_cmd->retcode <0) { retcode=erl_cmd->retcode; LM_DBG("cmd_erlang_call: failed %d\n",retcode); goto error; } //reuse argbuf.buff=erl_cmd->erlbuf; argbuf.buffsz=erl_cmd->erlbuf_len; argbuf.index=erl_cmd->decode_index; fill_retpv(erl_cmd->ret_pv,&argbuf,&(argbuf.index)); retcode=1; error: if(erl_cmd) { if(erl_cmd->ret_pv) shm_free(erl_cmd->ret_pv); if(erl_cmd->erlbuf) shm_free(erl_cmd->erlbuf); if(erl_cmd->reg_name) shm_free(erl_cmd->reg_name); shm_free(erl_cmd); } return retcode; }