Example #1
0
    void
epcap_response(u_char *user, const struct pcap_pkthdr *hdr, const u_char *pkt)
{
    EPCAP_STATE *ep = (EPCAP_STATE *)user;
    ei_x_buff msg = {0};

    IS_FALSE(ei_x_new_with_version(&msg));

    /* {packet, DatalinkType, Time, ActualLength, Packet} */
    IS_FALSE(ei_x_encode_tuple_header(&msg, 5));
    IS_FALSE(ei_x_encode_atom(&msg, "packet"));

    /* DataLinkType */
    IS_FALSE(ei_x_encode_long(&msg, ep->datalink));

    /* {MegaSec, Sec, MicroSec} */
    IS_FALSE(ei_x_encode_tuple_header(&msg, 3));
    IS_FALSE(ei_x_encode_long(&msg, abs(hdr->ts.tv_sec / 1000000)));
    IS_FALSE(ei_x_encode_long(&msg, hdr->ts.tv_sec % 1000000));
    IS_FALSE(ei_x_encode_long(&msg, hdr->ts.tv_usec));

    /* ActualLength} */
    IS_FALSE(ei_x_encode_long(&msg, hdr->len));

    /* Packet */
    IS_FALSE(ei_x_encode_binary(&msg, pkt, hdr->caplen));

    /* } */

    epcap_send_free(&msg);
}
Example #2
0
static void send_errno_result(int value)
{
    ei_x_buff x;
    ei_x_new_with_version(&x);
    ei_x_encode_tuple_header(&x, 2);
    ei_x_encode_long(&x, value);
    ei_x_encode_long(&x, erl_errno);
    send_bin_term(&x);
    ei_x_free(&x);
}
Example #3
0
static ErlDrvSSizeT expat_erl_control(ErlDrvData drv_data,
			     unsigned int command,
			     char *buf, ErlDrvSizeT len,
			     char **rbuf, ErlDrvSizeT rlen)
{
   expat_data* d = (expat_data*)drv_data;
   int res, errcode;
   char *errstring;
   ErlDrvBinary *b;
   size_t size;

   switch (command)
   {
      case PARSE_COMMAND:
      case PARSE_FINAL_COMMAND:
	 ei_x_new_with_version(&event_buf);
	 ei_x_new(&xmlns_buf);
	 res = XML_Parse(d->parser, buf, len, command == PARSE_FINAL_COMMAND);

	 if(!res)
	 {
	    errcode = XML_GetErrorCode(d->parser);
	    errstring = (char *)XML_ErrorString(errcode);

	    ei_x_encode_list_header(&event_buf, 1);
	    ei_x_encode_tuple_header(&event_buf, 2);
	    ei_x_encode_long(&event_buf, XML_ERROR);
	    ei_x_encode_tuple_header(&event_buf, 2);
	    ei_x_encode_long(&event_buf, errcode);
	    ei_x_encode_binary(&event_buf, errstring, strlen(errstring));
	 }

	 ei_x_encode_empty_list(&event_buf);

	 size = event_buf.index;

	 b = driver_alloc_binary(size);
	 memcpy(b->orig_bytes, event_buf.buff, size);

	 ei_x_free(&event_buf);
	 ei_x_free(&xmlns_buf);
 
	 *rbuf = (char *)b;
	 return size;
      default:
	 return 0;
   }
}
Example #4
0
void *erlXML_StartElementHandler(expat_data *d,
				 const XML_Char *name,
				 const XML_Char **atts)
{
   int i;

   ei_x_encode_list_header(&event_buf, 1);
   ei_x_encode_tuple_header(&event_buf, 2);
   ei_x_encode_long(&event_buf, XML_START);
   ei_x_encode_tuple_header(&event_buf, 2);
   encode_name(name);
   ei_x_append(&event_buf, &xmlns_buf);
   ei_x_free(&xmlns_buf);
   ei_x_new(&xmlns_buf);

   for (i = 0; atts[i]; i += 2) {}

   if (i > 0)
   {
      ei_x_encode_list_header(&event_buf, i/2);

      for (i = 0; atts[i]; i += 2)
      {
	 ei_x_encode_tuple_header(&event_buf, 2);
	 encode_name(atts[i]);
	 ei_x_encode_binary(&event_buf, atts[i+1], strlen(atts[i+1]));
      }
   }

   ei_x_encode_empty_list(&event_buf);

   return NULL;
}
Example #5
0
void *erlXML_StartElementHandler(expat_data *d,
				 const XML_Char *name,
				 const XML_Char **atts)
{
   int i;

   ei_x_encode_list_header(&event_buf, 1);
   ei_x_encode_tuple_header(&event_buf, 2);
   ei_x_encode_long(&event_buf, XML_START);
   ei_x_encode_tuple_header(&event_buf, 2);
   ei_x_encode_string(&event_buf, name);

   for (i = 0; atts[i]; i += 2) {}

   if (i > 0)
   {
      ei_x_encode_list_header(&event_buf, i/2);

      for (i = 0; atts[i]; i += 2)
      {
	 ei_x_encode_tuple_header(&event_buf, 2);
	 ei_x_encode_string(&event_buf, atts[i]);
	 ei_x_encode_string(&event_buf, atts[i+1]);
      }
   }

   ei_x_encode_empty_list(&event_buf);

   return NULL;
}
Example #6
0
/* Check if integer or float */
static int pdigit(const char** fmt, ei_x_buff* x)
{
    const char* start = *fmt;
    char c;
    int dotp=0;
    double d;
    long l;

    if (**fmt == '-' || **fmt == '+')
	(*fmt)++;
    for (;;) {
	c = *(*fmt)++;
	if (isdigit((int)c))
	    continue;
	else if (!dotp && (c == '.')) {
	    dotp = 1;
	    continue;
	} else
	    break;
    } 
    --(*fmt);
    if (dotp) {
	sscanf(start, "%lf", &d);
	return ei_x_encode_double(x, d);
    } else {
	sscanf(start, "%ld", &l);
	return ei_x_encode_long(x, l);
    }
}
Example #7
0
void *erlXML_EndElementHandler(expat_data *d,
			       const XML_Char *name)
{
   ei_x_encode_list_header(&event_buf, 1);
   ei_x_encode_tuple_header(&event_buf, 2);
   ei_x_encode_long(&event_buf, XML_END);
   ei_x_encode_string(&event_buf, name);
   return NULL;
}
Example #8
0
 /* 
  * The format letters are:
  *   a  -  An atom
  *   c  -  A character
  *   s  -  A string
  *   i  -  An integer
  *   l  -  A long integer
  *   u  -  An unsigned long integer
  *   f  -  A float 
  *   d  -  A double float 
  *   p  -  An Erlang PID
  */
static int pformat(const char** fmt, union arg** args, ei_x_buff* x)
{
    int res = 0;
    ++(*fmt);	/* skip tilde */
    switch (*(*fmt)++) {
    case 'a': 
	res = ei_x_encode_atom(x, (*args)->s);
	(*args)++;
	break;
    case 'c':
	res = ei_x_encode_char(x, (*args)->c);
	(*args)++;
	break;
    case 's':
	res = ei_x_encode_string(x, (*args)->s);
	(*args)++;
	break;
    case 'i':
	res = ei_x_encode_long(x, (*args)->l);
	(*args)++;
	break;
    case 'l':
	res = ei_x_encode_long(x, (*args)->l);
	(*args)++;
	break;
    case 'u':
	res = ei_x_encode_ulong(x, (*args)->u);
	(*args)++;
	break;
    case 'f':     /* float is expanded to double (C calling conventions) */
    case 'd':
	res = ei_x_encode_double(x, (*args)->d);
	(*args)++;
	break;	
    case 'p':
	res = ei_x_encode_pid(x, (*args)->pid);
	(*args)++;
	break;
    default:
	res = -1;
	break;
    }
    return res;
}
Example #9
0
static int unwrap(char *buf, int index, ei_x_buff *presult)
{
    ei_x_buff result = *presult;

    /*
      {unwrap, {Idx, Input}} -> {ok, {conf_state, Output}}
    */

    int arity;
    gss_buffer_desc in;
    gss_buffer_desc out;
    long idx;
    int conf_state;
    OM_uint32 maj_stat, min_stat;
    gss_qop_t qop;
	
    memset(&in, 0, sizeof(in));
    memset(&out, 0, sizeof(out));

    EI(ei_decode_tuple_header(buf, &index, &arity));
    
    EI(arity != 2);

    EI(ei_decode_long(buf, &index, &idx));

    EI(decode_gssapi_binary(buf, &index, &in));

    if (idx < 0 || idx >= MAX_SESSIONS || !g_sessions[idx])
	ENCODE_ERROR("bad_instance");

    maj_stat = gss_unwrap(&min_stat, g_sessions[idx],
			  &in, &out, &conf_state, &qop);

    if (!GSS_ERROR(maj_stat)) {
	const char *conf_str = conf_state ? "true":"false";

	EI(ei_x_encode_atom(&result, "ok") ||
	   ei_x_encode_tuple_header(&result, 2) ||
	   ei_x_encode_atom(&result, conf_str) ||
	   ei_x_encode_binary(&result, out.value, out.length)
	    );

    } else {
	EI(ei_x_encode_atom(&result, "error") || ei_x_encode_long(&result, maj_stat));
    }

error:
    if (in.value)
	gss_release_buffer(&min_stat, &in);

    if (out.value)
	gss_release_buffer(&min_stat, &out);

    *presult = result;
    return 0;
}
Example #10
0
File: q2e.c Project: csurface/gen_q
int ei_x_encode_unknown(ei_x_buff* types, ei_x_buff* values, K r, QOpts* opts) {
    EI(ei_x_encode_tuple_header(types, r->n+1));
    EI(ei_x_encode_long(types, r->t));
    EI(ei_x_encode_tuple_header(values, r->n));
    int i;
    for(i=0; i<r->n; ++i) {
        EI(ei_x_encode_k_tv(types, values, kK(r)[i], opts));
    }
    return 0;
}
Example #11
0
void *erlXML_CharacterDataHandler(expat_data *d,
				  const XML_Char *s,
				  int len)
{
   ei_x_encode_list_header(&event_buf, 1);
   ei_x_encode_tuple_header(&event_buf, 2);
   ei_x_encode_long(&event_buf, XML_CDATA);
   ei_x_encode_binary(&event_buf, s, len);
   return NULL;
}
Example #12
0
File: q2e.c Project: csurface/gen_q
int ei_x_encode_datetime(ei_x_buff* types, ei_x_buff* values, K r, QOpts* opts) {
    if(opts->unix_timestamp_is_q_datetime) {
        long v = datetime_to_unix_timestamp(r->f);
        EI(ei_x_encode_atom(types, "datetime"));
        EI(ei_x_encode_long(values, v));
    } else {
        EI(ei_x_encode_kf(types, values, "datetime", r, opts));
    }
    return 0;
}
Example #13
0
static void key_size(bloom_drv_t *driver) {
  long result;
  ei_x_buff x;
  result = bloom_key_size(driver->bloom);
  
  ei_x_new_with_version(&x);
  ei_x_encode_long(&x, result);
  
  driver_output(driver->port, x.buff, x.index);
  ei_x_free(&x);
}
Example #14
0
static int erl_json_ei_number(void* ctx, const char * val, unsigned int len) {
  State* pState = (State*) ctx;

  flog(stderr, "number", 0, val, len);

  list_header_for_value(pState);

  switch(numbers_as(pState)) {
    case EEP0018_PARSE_NUMBERS_AS_NUMBER:
    {
      if(memchr(val, '.', len) || memchr(val, 'e', len) || memchr(val, 'E', len))
        ei_x_encode_double(&pState->ei_buf, strtod(val, 0));
      else
        ei_x_encode_long(&pState->ei_buf, strtol(val, 0, 10));
      break;
    }
    case EEP0018_PARSE_NUMBERS_AS_FLOAT:
    {
      ei_x_encode_double(&pState->ei_buf, strtod(val, 0));
      break;
    }
    case EEP0018_PARSE_NUMBERS_AS_TUPLE:
    {
      /* 
        While "1e1" is a valid JSON number, it is not a valid parameter to list_to_float/1 
        We fix that by inserting ".0" before the exponent e. 
      */
      const char* exp = memchr(val, 'e', len);
      if(exp && exp > val) {
        const char* dot = memchr(val, '.', exp - val);
        if(!dot) {
          char* tmp = alloca(len + 5);
          memcpy(tmp, val, exp - val);
          memcpy(tmp + (exp - val), ".0", 2);
          memcpy(tmp + (exp - val) + 2, exp, len - (exp - val));
          len += 2;
          val = tmp;
          tmp[len] = 0;
        }
      }

      ei_x_encode_tuple_header(&pState->ei_buf, 3);
      ei_x_encode_atom_len(&pState->ei_buf, "number", 6);
      ei_x_encode_string_len(&pState->ei_buf, val, len);
      ei_x_encode_atom_len(&pState->ei_buf, "a", 1);      // a dummy
      break;
    }
  }

  
  return 1;
}
Example #15
0
static void get_size(Cmd *cmd) {
  char *buff = cmd->data;
  int len = cmd->size;
  Gd *gd = cmd->gd;
  unsigned int x;
  unsigned int y;
  ei_x_buff b;
  
  if (gd->image == NULL) {
    driver_failure_atom(gd->port, "null_image");
    return;
  }
  
  x = gdImageSX(gd->image);
  y = gdImageSY(gd->image);
  
  ei_x_new_with_version(&b);
  ei_x_encode_tuple_header(&b, 2);
  ei_x_encode_long(&b, x);
  ei_x_encode_long(&b, y);
  driver_output(gd->port, b.buff, b.index);
  ei_x_free(&b);
}
Example #16
0
static void cmd_ei_set_get_tracelevel(char* buf, int len)
{
    int  index = 0;
    long level = 0;
    long ret   = 0;
    ei_x_buff x;

    if (ei_decode_long(buf, &index, &level) < 0) {
	fail("expected long");
    }
    
    ei_set_tracelevel((int)level);

    ret = (long) ei_get_tracelevel();

    ei_x_new_with_version(&x);
    ei_x_encode_tuple_header(&x, 2);
    ei_x_encode_atom(&x, "tracelevel");
    ei_x_encode_long(&x, ret);
    send_bin_term(&x);
    ei_x_free(&x);
}
Example #17
0
static void cmd_ei_connect_init(char* buf, int len)
{
    int index = 0, r = 0;
    int type, size;
    long l;
    char b[100];
    char cookie[MAXATOMLEN], * cp = cookie;
    ei_x_buff res;
    if (ei_decode_long(buf, &index, &l) < 0)
	fail("expected int");
    sprintf(b, "c%ld", l);
    /* FIXME don't use internal and maybe use skip?! */
    ei_get_type_internal(buf, &index, &type, &size);
    if (ei_decode_atom(buf, &index, cookie) < 0)
	fail("expected atom (cookie)");
    if (cookie[0] == '\0')
	cp = NULL;
    r = ei_connect_init(&ec, b, cp, 0);
    ei_x_new_with_version(&res);
    ei_x_encode_long(&res, r);
    send_bin_term(&res);
    ei_x_free(&res);
}
Example #18
0
ETERM * write_to_png_stream(ETERM* arg, int c_node) {
	cairo_context * const ctx = get_cairo_context(arg);
	if(!ctx)
		return erl_format("{c_node, ~i, {error, '~s'}}", c_node, ERR_CONTEXT);

	struct png_data out = {};
	const int status = cairo_surface_write_to_png_stream(ctx->sf, write_cb, &out);
	ETERM *term = NULL;

	ei_x_buff req;
	ei_x_new_with_version(&req);
	ei_x_encode_tuple_header(&req, 3);
	ei_x_encode_atom(&req, "c_node");
	ei_x_encode_long(&req, c_node);
	ei_x_encode_tuple_header(&req, 2);
	ei_x_encode_atom(&req, "ok");
	ei_x_encode_binary(&req, out.buf, out.written);

	int index = 0;
	ei_decode_term(req.buff, &index, &term);
	ei_x_free(&req);
	free(out.buf);
	return term;
}
Example #19
0
static int delete_sec_context(char *buf, int index, ei_x_buff *presult)
{
    ei_x_buff result = *presult;

    /*
      {delete_sec_context, Idx} -> {ok, }
    */

    long idx;
    OM_uint32 maj_stat, min_stat;
	
    EI(ei_decode_long(buf, &index, &idx));

    if (idx < 0 || idx >= MAX_SESSIONS || !g_sessions[idx] ||
	g_sessions[idx] == GSS_C_NO_CONTEXT)
	ENCODE_ERROR("bad_instance");

    maj_stat = gss_delete_sec_context(&min_stat, &g_sessions[idx],
				      GSS_C_NO_BUFFER);

    g_sessions[idx] = NULL;

    if (!GSS_ERROR(maj_stat)) {
	EI(ei_x_encode_atom(&result, "ok") ||
	   ei_x_encode_atom(&result, "done")
	    );
    } else {
	fprintf(stderr, "gss_delete_sec_context: %08x", maj_stat);
	gss_print_errors(min_stat);
	EI(ei_x_encode_atom(&result, "error") || ei_x_encode_long(&result, maj_stat));
    }

error:
    *presult = result;
    return 0;
}
Example #20
0
static inline void write_long(State *p, long d)
  { ei_x_encode_long(&p->ei_buf, d); }
Example #21
0
static int wrap(char *buf, int index, ei_x_buff *presult)
{
    ei_x_buff result = *presult;

    /*
      {wrap, {Idx, Conf_req_flag, Input}} -> {ok, {Conf_state, Output}}
    */

    int arity;
    gss_buffer_desc in;
    gss_buffer_desc out;
    long idx;
    char conf_str[MAXATOMLEN];
    int conf_req;
    int conf_state;
    OM_uint32 maj_stat, min_stat;
	
    memset(&in, 0, sizeof(in));
    memset(&out, 0, sizeof(out));

    EI(ei_decode_tuple_header(buf, &index, &arity));
    
    EI(arity != 3);

    EI(ei_decode_long(buf, &index, &idx));

    EI(ei_decode_atom(buf, &index, conf_str));

    EI(decode_gssapi_binary(buf, &index, &in));

    if (idx < 0 || idx >= MAX_SESSIONS || !g_sessions[idx])
	ENCODE_ERROR("bad_instance");

    if (!strcmp(conf_str, "false")) {
	conf_req = 0;
    } else if (!strcmp(conf_str, "true")) {
	conf_req = 1;
    } else {
	ENCODE_ERROR("bad_parameter");
    }

    maj_stat = gss_wrap(&min_stat, g_sessions[idx],
			conf_req, GSS_C_QOP_DEFAULT, &in,
			&conf_state, &out);

    if (!GSS_ERROR(maj_stat)) {
	const char *conf_str = conf_state ? "true":"false";

	EI(ei_x_encode_atom(&result, "ok") ||
	   ei_x_encode_tuple_header(&result, 2) ||
	   ei_x_encode_atom(&result, conf_str) ||
	   ei_x_encode_binary(&result, out.value, out.length)
	    );

    } else {
	EI(ei_x_encode_atom(&result, "error") || ei_x_encode_long(&result, maj_stat));
    }

error:
    if (in.value)
	gss_release_buffer(&min_stat, &in);

    if (out.value)
	gss_release_buffer(&min_stat, &out);

    *presult = result;
    return 0;
}
Example #22
0
static int accept_sec_context(char *buf, int index, ei_x_buff *presult)
{
    ei_x_buff result = *presult;

    /*
      {accept_sec_context, {Idx, In}} ->
      {ok, {Idx, Name, CCName, Out}} |
      {needsmore, {Idx, Out}}
    */

    int arity;
    gss_buffer_desc in;
    gss_buffer_desc out;
    gss_buffer_desc name;
    int res;
    char *ccname = NULL;
    long idx;
    OM_uint32 min_stat;

    memset(&in, 0, sizeof(in));
    memset(&out, 0, sizeof(out));
    memset(&name, 0, sizeof(name));

    EI(ei_decode_tuple_header(buf, &index, &arity));
    
    EI(arity != 2);

    EI(ei_decode_long(buf, &index, &idx));

    EI(decode_gssapi_binary(buf, &index, &in));

    if (idx < 0) {
	idx = session_find_free();
	if (idx < 0) ENCODE_ERROR("no_mem");
	g_sessions[idx] = GSS_C_NO_CONTEXT;
    } else {
	if (idx < 0 || idx >= MAX_SESSIONS || !g_sessions[idx])
	    ENCODE_ERROR("bad_instance");
    }

    res = accept_user(&g_sessions[idx], &in, &out, &name, &ccname);

    if (!GSS_ERROR(res)) {
	if (res & GSS_S_CONTINUE_NEEDED) {
	    EI(ei_x_encode_atom(&result, "needsmore") ||
	       ei_x_encode_tuple_header(&result, 2) ||
	       ei_x_encode_long(&result, idx) ||
	       ei_x_encode_binary(&result, out.value, out.length)
		);
	} else {
	    const char *ret_ccname = ccname;
	    if (!ret_ccname)
		ret_ccname = "";

	    EI(ei_x_encode_atom(&result, "ok") ||
	       ei_x_encode_tuple_header(&result, 4) ||
	       ei_x_encode_long(&result, idx) ||
	       ei_x_encode_string_len(&result, name.value, name.length) ||
	       ei_x_encode_string(&result, ret_ccname) ||
	       ei_x_encode_binary(&result, out.value, out.length)
		);

	}
    } else {
	EI(ei_x_encode_atom(&result, "error") || ei_x_encode_atom(&result, "unauthorized"));
    }

error:
    if (ccname)
	free(ccname);

    if (in.value)
	gss_release_buffer(&min_stat, &in);

    if (out.value)
	gss_release_buffer(&min_stat, &out);

    if (name.value)
	gss_release_buffer(&min_stat, &name);

    *presult = result;
    return 0;
}
Example #23
0
static int init_sec_context(char *buf, int index, ei_x_buff *presult)
{
    ei_x_buff result = *presult;

    /*
      {init_sec_context, {Idx, Service, Host, Input}} ->
       {ok, {Idx, Data}} | {error, Error}
    */

    int arity;
    gss_buffer_desc in;
    gss_buffer_desc out;
    int res;
    char *service = NULL;
    char *hostname = NULL;
    long idx;
    OM_uint32 min_stat;
	
    memset(&in, 0, sizeof(in));
    memset(&out, 0, sizeof(out));

    EI(ei_decode_tuple_header(buf, &index, &arity));
    
    EI(arity != 4);

    EI(ei_decode_long(buf, &index, &idx));

    DECODE_STRING(&service);
    DECODE_STRING(&hostname);

    EI(decode_gssapi_binary(buf, &index, &in));

    if (idx < 0) {
	idx = session_find_free();
	if (idx < 0) ENCODE_ERROR("no_mem");
	g_sessions[idx] = GSS_C_NO_CONTEXT;
    } else {
	if (idx < 0 || idx >= MAX_SESSIONS || !g_sessions[idx])
	    ENCODE_ERROR("bad_instance");
    }

    res = init_user(&g_sessions[idx], service, hostname, &in, &out);

    if (!GSS_ERROR(res)) {
	const char *status = (res & GSS_S_CONTINUE_NEEDED)?"needsmore":"ok";
	EI(ei_x_encode_atom(&result, status) ||
	   ei_x_encode_tuple_header(&result, 2) ||
	   ei_x_encode_long(&result, idx) ||
	   ei_x_encode_binary(&result, out.value, out.length)
	    );

    } else {
	EI(ei_x_encode_atom(&result, "error") || ei_x_encode_long(&result, res));
    }

error:
    if (service)
	free(service);

    if (hostname)
	free(hostname);

    if (in.value)
	gss_release_buffer(&min_stat, &in);

    if (out.value)
	gss_release_buffer(&min_stat, &out);

    *presult = result;
    return 0;
}
Example #24
0
int
ei_x_encode_uint(ei_x_buff *wb,
		 unsigned int uint)
{
  return ei_x_encode_long(wb, (long)(uint)); /* FIXME: signs etc? */
}
Example #25
0
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;
}
Example #26
0
static int
exmpp_xml_control(ErlDrvData drv_data, unsigned int command,
    char *buf, int len, char **rbuf, int rlen)
{
	struct exmpp_xml_data *edd;
	ei_x_buff *to_return;
	ErlDrvBinary *bin;
	int size, ret;

	edd = (struct exmpp_xml_data *)drv_data;
	size = 0;
	bin = NULL;
	to_return = NULL;

	switch (command) {
	/*
	 * Parsing.
	 */

	case COMMAND_PARSE:
	case COMMAND_PARSE_FINAL:
		if (edd->parser == NULL) {
			/* Start a parser. */
			if (create_parser(edd) != 0) {
				to_return = exmpp_new_xbuf();
				if (to_return == NULL)
					return (-1);

				ret = RET_ERROR;
				ei_x_encode_atom(to_return,
				    "parser_setup_failed");

				break;
			}
		}

		/* Control the total size of data to parse. */
		if (!is_data_size_under_limit(&edd->ctx, len)) {
			to_return = exmpp_new_xbuf();
			if (to_return == NULL)
				return (-1);

			ret = RET_ERROR;
			ei_x_encode_atom(to_return, "stanza_too_big");

			break;
		}

		/* Run XML document parsing. */
		ret = XML_Parse(edd->parser, buf, len,
		    command == COMMAND_PARSE_FINAL);

		if (!ret) {
			enum XML_Error errcode;
			const char *errmsg;

			/* An error occured during parsing; most probably,
			 * XML wasn't well-formed. */
			errcode = XML_GetErrorCode(edd->parser);
			errmsg = XML_ErrorString(errcode);

			to_return = exmpp_new_xbuf();
			if (to_return == NULL)
				return (-1);

			ret = RET_ERROR;
			ei_x_encode_tuple_header(to_return, 2);
			ei_x_encode_atom(to_return, "parsing_failed");
			ei_x_encode_tuple_header(to_return, 2);
			ei_x_encode_long(to_return, errcode);
			ei_x_encode_string(to_return, errmsg);

			break;
		}

		/* Return the complete tree(s). */
		ret = RET_OK;
		if (edd->ctx.complete_trees_ready) {
			/* Terminate the complete trees list. */
			ei_x_encode_empty_list(edd->ctx.complete_trees);

			to_return = edd->ctx.complete_trees;
			size = 1 + to_return->index;
			bin = driver_alloc_binary(size);
			if (bin == NULL)
				return (-1);
			bin->orig_bytes[0] = (char)ret;
			memcpy(bin->orig_bytes + 1,
			    to_return->buff, to_return->index);
		} else {
			/* We need more data to produce a tree. */
			to_return = exmpp_new_xbuf();
			if (to_return == NULL)
				return (-1);

			ei_x_encode_atom(to_return,
			    command == COMMAND_PARSE ? "continue" : "done");
		}

		if (command == COMMAND_PARSE) {
			/* Update the size of processed data. */
			add_data_size(&edd->ctx, len);

			/* Reset the complete trees list. */
			reset_complete_trees(&edd->ctx);
		} else {
			/* We're done with the parser. */
			destroy_parser(edd);
		}

		break;

	case COMMAND_RESET_PARSER:
		if (edd->parser != NULL) {
			reset_context(&edd->ctx);
			XML_ParserReset(edd->parser, "UTF-8");
			init_parser(edd);
		}

		ret = RET_OK;

		break;

	/*
	 * Misc.
	 */

	case COMMAND_PORT_REVISION:
		/* Store the revision in the buffer. */
		to_return = exmpp_new_xbuf();
		if (to_return == NULL)
			return (-1);

		ret = RET_OK;
		ei_x_encode_string(to_return, "$Revision$");

		break;

	default:
		/* Other commands are handled in 'exmpp_xml.c' */
		to_return = exmpp_new_xbuf();
		if (to_return == NULL)
			return (-1);

		ret = control(&edd->ctx, command, buf, to_return);
		if (ret < 0)
			return (-1);
	}

	if (bin == NULL) {
		if (to_return != NULL) {
			size = 1 + to_return->index;
			bin = driver_alloc_binary(size);
			if (bin == NULL)
				return (-1);
			bin->orig_bytes[0] = (char)ret;
			if (to_return->index > 0)
				memcpy(bin->orig_bytes + 1,
				    to_return->buff, to_return->index);
			exmpp_free_xbuf(to_return);
		} else {
			/* The command called doesn't return anything. */
			size = 1;
			bin = driver_alloc_binary(size);
			bin->orig_bytes[0] = RET_OK;
		}
	}

	/* Set the returned buffer. */
	*rbuf = (char *)bin;

	/* Return the size of this buffer. */
	return (size);
}
Example #27
0
void integer(ei_x_buff *eixb, int integer) {
  ei_x_encode_long(eixb, (long)integer);
}
Example #28
0
/*
 * Function returns always success - we uses EPMD for transport
 */
int erl_rpc_send(erl_rpc_ctx_t *ctx, int depth)
{
	if (ctx->response_sent) return 0;
	ctx->response_sent = 1;
	erl_rpc_ctx_t *handler;
	erl_rpc_param_t *fault = *(ctx->fault_p);

	if (fault)
	{
		LM_ERR("fault: %d %.*s\n",fault->type, STR_FMT(&fault->value.S));
		/* restore clear point */
		ctx->response->index = ctx->response_index;

		/* {error,{struct,[ {"code", 400}, {"error","Error message"}]}}*/
		if (ei_x_encode_tuple_header(ctx->response,1)) goto error;	/* {error,{_,_}} */
		if (rpc_reply_with_struct && ei_x_encode_atom(ctx->response,"struct")) goto error;	/* {error,{struct,_}} */
		if (ei_x_encode_list_header(ctx->response,2)) goto error;	/* {error,{struct,[_,_]}} */
		if (ei_x_encode_tuple_header(ctx->response,2)) goto error;	/* {error,{struct,[{_,_},_]}} */
		if (ei_x_encode_atom(ctx->response,"code")) goto error;		/* {error,{struct,[{code,_},_]}} */
		if (ei_x_encode_long(ctx->response,fault->type)) goto error;/* {error,{struct,[{code,400},_]}} */
		if (ei_x_encode_tuple_header(ctx->response,2)) goto error;	/* {error,{struct,[{code,400},{_,_}]}} */
		if (ei_x_encode_binary(ctx->response,"error",sizeof("error")-1)) goto error;	/* {error,{struct,[{code,400},{<<"error">>,_}]}} */
		if (ei_x_encode_binary(ctx->response,(void*)fault->value.S.s,fault->value.S.len)) /* {error,{struct,[{code,400},{<<"error">>,<<Msg>>}]}} */
			goto error;
		if (ei_x_encode_empty_list(ctx->response)) goto error;
	}
	else if (ctx->reply_params)
	{
		while(ctx->reply_params)
		{
			if (ctx->reply_params->member_name)
			{
				/* {"member_name", _} */
				if (ei_x_encode_tuple_header(ctx->response,2)) goto error;
				if (ei_x_encode_binary(ctx->response,ctx->reply_params->member_name, strlen(ctx->reply_params->member_name)))
					goto error;
			}
			/* {"member_name", MemberValue} */
			switch (ctx->reply_params->type) {
				case ERL_INTEGER_EXT:
					if(ei_x_encode_long(ctx->response,ctx->reply_params->value.n)) goto error;
					break;
				case ERL_FLOAT_EXT:
					if(ei_x_encode_double(ctx->response,ctx->reply_params->value.d)) goto error;
					break;
				case ERL_STRING_EXT:
					if(ei_x_encode_binary(ctx->response,ctx->reply_params->value.S.s,ctx->reply_params->value.S.len)) goto error;
					break;
				case ERL_SMALL_TUPLE_EXT: /* add as {struct,list(no_params)} */
					handler = (erl_rpc_ctx_t*)ctx->reply_params->value.handler;
					if (ei_x_encode_tuple_header(ctx->response,1)) goto error;
					if (rpc_reply_with_struct && ei_x_encode_atom(ctx->response,"struct")) goto error;
					if (ei_x_encode_list_header(ctx->response,handler->no_params)) goto error;
					if (erl_rpc_send(handler, depth++)) goto error;
					if (ei_x_encode_empty_list(ctx->response)) goto error;
					break;
				case ERL_LIST_EXT: /* add as [list(no_params)] */
					handler = (erl_rpc_ctx_t*)ctx->reply_params->value.handler;
					if (ei_x_encode_list_header(ctx->response,handler->no_params)) goto error;
					if (erl_rpc_send(handler, depth++)) goto error;
					if (handler->no_params)
						if (ei_x_encode_empty_list(ctx->response)) goto error;
					break;
				default:
					LM_ERR("Unknown type '%c' for encoding RPC reply\n",ctx->reply_params->type);
					break;
			}
			ctx->reply_params=ctx->reply_params->next;
		}
	}
	else if (!depth)
	{
		/* restore start point */
		LM_WARN("encode empty response -> ok");
		ctx->response->index = ctx->response_index;
		if (ei_x_encode_atom(ctx->response,"ok")) goto error;
	}

	return 0;

error:
	LM_ERR("error while encoding response\n");
	return -1;
}
Example #29
0
static DWORD WINAPI
#else
static void*
#endif
    einode_thread(void* num)
{
    int n = (int)num;
    ei_cnode ec;
    char myname[100], destname[100];
    int r, fd, listen;
    ErlConnect conn;
    erlang_msg msg;
/*    FILE* f;*/

    sprintf(myname, "eiacc%d", n);
    printf("thread %d (%s) listening\n", n, myname, destname);
    r = ei_connect_init(&ec, myname, cookie, 0);
    if ((listen = my_listen(port+n)) <= 0) {
	printf("listen err\n");
	exit(7);
    }
    if (ei_publish(&ec, port + n) == -1) {
	printf("ei_publish port %d\n", port+n);
	exit(8);
    }
    fd = ei_accept(&ec, listen, &conn);
    printf("ei_accept %d\n", fd);
    if (fd >= 0) {
	ei_x_buff x, xs;
	int index, version;
	erlang_pid pid;

	ei_x_new(&x);
	for (;;) {
	    int got = ei_xreceive_msg(fd, &msg, &x);
	    if (got == ERL_TICK)
		continue;
	    if (got == ERL_ERROR) {
		printf("receive error %d\n", n);
		return 0;
	    }
	    printf("received %d\n", got);
	    break;
	}
	index = 0;
	if (ei_decode_version(x.buff, &index, &version) != 0) {
	    printf("ei_decode_version %d\n", n);
	    return 0;
	}
	if (ei_decode_pid(x.buff, &index, &pid) != 0) {
	    printf("ei_decode_pid %d\n", n);
	    return 0;
	}
/*	fprintf(f, "got pid from %s \n", pid.node);*/
	ei_x_new_with_version(&xs);
	ei_x_encode_tuple_header(&xs, 2);
	ei_x_encode_long(&xs, n);
	ei_x_encode_pid(&xs, &pid);
	r = ei_send(fd, &pid, xs.buff, xs.index);
/*	fprintf(f, "sent %d bytes %d\n", xs.index, r);*/
	shutdown(fd, SD_SEND);
	closesocket(fd);
	ei_x_free(&x);
	ei_x_free(&xs);
    } else {
	printf("coudn't connect fd %d r %d\n", fd, r);
    }
    printf("done thread %d\n", n);
/*    fclose(f);*/
    return 0;
}
Example #30
0
int add_event_data(ei_x_buff* sendbuf, int topic, const void* data) {
    switch (topic) {
        case EVENT_COMMAND_FINISHED: {
            ei_x_encode_atom(sendbuf, "event_command_finished");
            struct SCommandFinishedEvent* event_data = (struct SCommandFinishedEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 6);
            {
                if (ei_x_encode_atom(sendbuf, "unitid") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "unitid");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->unitId) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->unitId);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "commandid") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "commandid");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->commandId) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->commandId);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "commandtopicid") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "commandtopicid");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->commandTopicId) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->commandTopicId);
                    return -1;
                }
            }
            break;
        }
        case EVENT_ENEMY_CREATED: {
            ei_x_encode_atom(sendbuf, "event_enemy_created");
            struct SEnemyCreatedEvent* event_data = (struct SEnemyCreatedEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 2);
            {
                if (ei_x_encode_atom(sendbuf, "enemy") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "enemy");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->enemy) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->enemy);
                    return -1;
                }
            }
            break;
        }
        case EVENT_ENEMY_DAMAGED: {
            ei_x_encode_atom(sendbuf, "event_enemy_damaged");
            struct SEnemyDamagedEvent* event_data = (struct SEnemyDamagedEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 12);
            {
                if (ei_x_encode_atom(sendbuf, "enemy") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "enemy");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->enemy) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->enemy);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "attacker") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "attacker");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->attacker) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->attacker);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "damage") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "damage");
                    return -1;
                }
                if (ei_x_encode_double(sendbuf, event_data->damage) < 0) {
                    fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->damage);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "dir_posf3") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "dir_posf3");
                    return -1;
                }
                if (ei_x_encode_tuple_header(sendbuf, 3) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'tuple_header'\n", 3);
                    return -1;
                }
                if (ei_x_encode_double(sendbuf, event_data->dir_posF3[0]) < 0) {
                    fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->dir_posF3[0]);
                    return -1;
                }
                if (ei_x_encode_double(sendbuf, event_data->dir_posF3[1]) < 0) {
                    fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->dir_posF3[1]);
                    return -1;
                }
                if (ei_x_encode_double(sendbuf, event_data->dir_posF3[2]) < 0) {
                    fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->dir_posF3[2]);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "weapondefid") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "weapondefid");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->weaponDefId) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->weaponDefId);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "paralyzer") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "paralyzer");
                    return -1;
                }
                if (ei_x_encode_atom(sendbuf, event_data->paralyzer ? "true" : "false") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", event_data->paralyzer ? "true" : "false");
                    return -1;
                }
            }
            break;
        }
        case EVENT_ENEMY_DESTROYED: {
            ei_x_encode_atom(sendbuf, "event_enemy_destroyed");
            struct SEnemyDestroyedEvent* event_data = (struct SEnemyDestroyedEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 4);
            {
                if (ei_x_encode_atom(sendbuf, "enemy") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "enemy");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->enemy) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->enemy);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "attacker") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "attacker");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->attacker) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->attacker);
                    return -1;
                }
            }
            break;
        }
        case EVENT_ENEMY_ENTER_LOS: {
            ei_x_encode_atom(sendbuf, "event_enemy_enter_los");
            struct SEnemyEnterLOSEvent* event_data = (struct SEnemyEnterLOSEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 2);
            {
                if (ei_x_encode_atom(sendbuf, "enemy") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "enemy");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->enemy) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->enemy);
                    return -1;
                }
            }
            break;
        }
        case EVENT_ENEMY_ENTER_RADAR: {
            ei_x_encode_atom(sendbuf, "event_enemy_enter_radar");
            struct SEnemyEnterRadarEvent* event_data = (struct SEnemyEnterRadarEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 2);
            {
                if (ei_x_encode_atom(sendbuf, "enemy") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "enemy");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->enemy) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->enemy);
                    return -1;
                }
            }
            break;
        }
        case EVENT_ENEMY_FINISHED: {
            ei_x_encode_atom(sendbuf, "event_enemy_finished");
            struct SEnemyFinishedEvent* event_data = (struct SEnemyFinishedEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 2);
            {
                if (ei_x_encode_atom(sendbuf, "enemy") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "enemy");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->enemy) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->enemy);
                    return -1;
                }
            }
            break;
        }
        case EVENT_ENEMY_LEAVE_LOS: {
            ei_x_encode_atom(sendbuf, "event_enemy_leave_los");
            struct SEnemyLeaveLOSEvent* event_data = (struct SEnemyLeaveLOSEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 2);
            {
                if (ei_x_encode_atom(sendbuf, "enemy") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "enemy");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->enemy) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->enemy);
                    return -1;
                }
            }
            break;
        }
        case EVENT_ENEMY_LEAVE_RADAR: {
            ei_x_encode_atom(sendbuf, "event_enemy_leave_radar");
            struct SEnemyLeaveRadarEvent* event_data = (struct SEnemyLeaveRadarEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 2);
            {
                if (ei_x_encode_atom(sendbuf, "enemy") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "enemy");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->enemy) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->enemy);
                    return -1;
                }
            }
            break;
        }
        case EVENT_LOAD: {
            ei_x_encode_atom(sendbuf, "event_load");
            struct SLoadEvent* event_data = (struct SLoadEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 2);
            {
                if (ei_x_encode_atom(sendbuf, "file") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "file");
                    return -1;
                }
                if (ei_x_encode_string(sendbuf, event_data->file) < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'string'\n", event_data->file);
                    return -1;
                }
            }
            break;
        }
        case EVENT_LUA_MESSAGE: {
            ei_x_encode_atom(sendbuf, "event_lua_message");
            struct SLuaMessageEvent* event_data = (struct SLuaMessageEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 2);
            {
                if (ei_x_encode_atom(sendbuf, "indata") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "indata");
                    return -1;
                }
                if (ei_x_encode_string(sendbuf, event_data->inData) < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'string'\n", event_data->inData);
                    return -1;
                }
            }
            break;
        }
        case EVENT_MESSAGE: {
            ei_x_encode_atom(sendbuf, "event_message");
            struct SMessageEvent* event_data = (struct SMessageEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 4);
            {
                if (ei_x_encode_atom(sendbuf, "player") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "player");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->player) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->player);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "message") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "message");
                    return -1;
                }
                if (ei_x_encode_string(sendbuf, event_data->message) < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'string'\n", event_data->message);
                    return -1;
                }
            }
            break;
        }
        case EVENT_RELEASE: {
            ei_x_encode_atom(sendbuf, "event_release");
            struct SReleaseEvent* event_data = (struct SReleaseEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 2);
            {
                if (ei_x_encode_atom(sendbuf, "reason") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "reason");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->reason) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->reason);
                    return -1;
                }
            }
            break;
        }
        case EVENT_SAVE: {
            ei_x_encode_atom(sendbuf, "event_save");
            struct SSaveEvent* event_data = (struct SSaveEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 2);
            {
                if (ei_x_encode_atom(sendbuf, "file") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "file");
                    return -1;
                }
                if (ei_x_encode_string(sendbuf, event_data->file) < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'string'\n", event_data->file);
                    return -1;
                }
            }
            break;
        }
        case EVENT_SEISMIC_PING: {
            ei_x_encode_atom(sendbuf, "event_seismic_ping");
            struct SSeismicPingEvent* event_data = (struct SSeismicPingEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 4);
            {
                if (ei_x_encode_atom(sendbuf, "pos_posf3") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "pos_posf3");
                    return -1;
                }
                if (ei_x_encode_tuple_header(sendbuf, 3) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'tuple_header'\n", 3);
                    return -1;
                }
                if (ei_x_encode_double(sendbuf, event_data->pos_posF3[0]) < 0) {
                    fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->pos_posF3[0]);
                    return -1;
                }
                if (ei_x_encode_double(sendbuf, event_data->pos_posF3[1]) < 0) {
                    fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->pos_posF3[1]);
                    return -1;
                }
                if (ei_x_encode_double(sendbuf, event_data->pos_posF3[2]) < 0) {
                    fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->pos_posF3[2]);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "strength") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "strength");
                    return -1;
                }
                if (ei_x_encode_double(sendbuf, event_data->strength) < 0) {
                    fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->strength);
                    return -1;
                }
            }
            break;
        }
        case EVENT_UNIT_CAPTURED: {
            ei_x_encode_atom(sendbuf, "event_unit_captured");
            struct SUnitCapturedEvent* event_data = (struct SUnitCapturedEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 6);
            {
                if (ei_x_encode_atom(sendbuf, "unitid") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "unitid");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->unitId) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->unitId);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "oldteamid") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "oldteamid");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->oldTeamId) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->oldTeamId);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "newteamid") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "newteamid");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->newTeamId) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->newTeamId);
                    return -1;
                }
            }
            break;
        }
        case EVENT_UNIT_CREATED: {
            ei_x_encode_atom(sendbuf, "event_unit_created");
            struct SUnitCreatedEvent* event_data = (struct SUnitCreatedEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 4);
            {
                if (ei_x_encode_atom(sendbuf, "unit") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "unit");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->unit) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->unit);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "builder") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "builder");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->builder) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->builder);
                    return -1;
                }
            }
            break;
        }
        case EVENT_UNIT_DAMAGED: {
            ei_x_encode_atom(sendbuf, "event_unit_damaged");
            struct SUnitDamagedEvent* event_data = (struct SUnitDamagedEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 12);
            {
                if (ei_x_encode_atom(sendbuf, "unit") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "unit");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->unit) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->unit);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "attacker") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "attacker");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->attacker) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->attacker);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "damage") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "damage");
                    return -1;
                }
                if (ei_x_encode_double(sendbuf, event_data->damage) < 0) {
                    fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->damage);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "dir_posf3") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "dir_posf3");
                    return -1;
                }
                if (ei_x_encode_tuple_header(sendbuf, 3) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'tuple_header'\n", 3);
                    return -1;
                }
                if (ei_x_encode_double(sendbuf, event_data->dir_posF3[0]) < 0) {
                    fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->dir_posF3[0]);
                    return -1;
                }
                if (ei_x_encode_double(sendbuf, event_data->dir_posF3[1]) < 0) {
                    fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->dir_posF3[1]);
                    return -1;
                }
                if (ei_x_encode_double(sendbuf, event_data->dir_posF3[2]) < 0) {
                    fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->dir_posF3[2]);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "weapondefid") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "weapondefid");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->weaponDefId) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->weaponDefId);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "paralyzer") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "paralyzer");
                    return -1;
                }
                if (ei_x_encode_atom(sendbuf, event_data->paralyzer ? "true" : "false") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", event_data->paralyzer ? "true" : "false");
                    return -1;
                }
            }
            break;
        }
        case EVENT_UNIT_DESTROYED: {
            ei_x_encode_atom(sendbuf, "event_unit_destroyed");
            struct SUnitDestroyedEvent* event_data = (struct SUnitDestroyedEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 4);
            {
                if (ei_x_encode_atom(sendbuf, "unit") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "unit");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->unit) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->unit);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "attacker") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "attacker");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->attacker) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->attacker);
                    return -1;
                }
            }
            break;
        }
        case EVENT_UNIT_FINISHED: {
            ei_x_encode_atom(sendbuf, "event_unit_finished");
            struct SUnitFinishedEvent* event_data = (struct SUnitFinishedEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 2);
            {
                if (ei_x_encode_atom(sendbuf, "unit") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "unit");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->unit) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->unit);
                    return -1;
                }
            }
            break;
        }
        case EVENT_UNIT_GIVEN: {
            ei_x_encode_atom(sendbuf, "event_unit_given");
            struct SUnitGivenEvent* event_data = (struct SUnitGivenEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 6);
            {
                if (ei_x_encode_atom(sendbuf, "unitid") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "unitid");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->unitId) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->unitId);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "oldteamid") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "oldteamid");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->oldTeamId) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->oldTeamId);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "newteamid") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "newteamid");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->newTeamId) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->newTeamId);
                    return -1;
                }
            }
            break;
        }
        case EVENT_UNIT_IDLE: {
            ei_x_encode_atom(sendbuf, "event_unit_idle");
            struct SUnitIdleEvent* event_data = (struct SUnitIdleEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 2);
            {
                if (ei_x_encode_atom(sendbuf, "unit") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "unit");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->unit) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->unit);
                    return -1;
                }
            }
            break;
        }
        case EVENT_UNIT_MOVE_FAILED: {
            ei_x_encode_atom(sendbuf, "event_unit_move_failed");
            struct SUnitMoveFailedEvent* event_data = (struct SUnitMoveFailedEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 2);
            {
                if (ei_x_encode_atom(sendbuf, "unit") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "unit");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->unit) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->unit);
                    return -1;
                }
            }
            break;
        }
        case EVENT_UPDATE: {
            ei_x_encode_atom(sendbuf, "event_update");
            struct SUpdateEvent* event_data = (struct SUpdateEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 2);
            {
                if (ei_x_encode_atom(sendbuf, "frame") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "frame");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->frame) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->frame);
                    return -1;
                }
            }
            break;
        }
        case EVENT_WEAPON_FIRED: {
            ei_x_encode_atom(sendbuf, "event_weapon_fired");
            struct SWeaponFiredEvent* event_data = (struct SWeaponFiredEvent*)data;
            ei_x_encode_tuple_header(sendbuf, 4);
            {
                if (ei_x_encode_atom(sendbuf, "unitid") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "unitid");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->unitId) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->unitId);
                    return -1;
                }
            }
            {
                if (ei_x_encode_atom(sendbuf, "weapondefid") < 0) {
                    fprintf(stderr, "cannot encode '%s' as 'atom'\n", "weapondefid");
                    return -1;
                }
                if (ei_x_encode_long(sendbuf, event_data->weaponDefId) < 0) {
                    fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->weaponDefId);
                    return -1;
                }
            }
            break;
        }
        default: {
            ei_x_encode_long(sendbuf, topic);
            ei_x_encode_binary(sendbuf, data, sizeof(data));
        }
    }
    return 0;
}