static int proto_smtp_conntrack_cleanup(void *ce_priv) { struct proto_smtp_conntrack_priv *priv = ce_priv; if (!priv) return POM_OK; if (priv->parser[POM_DIR_FWD]) packet_stream_parser_cleanup(priv->parser[POM_DIR_FWD]); if (priv->parser[POM_DIR_REV]) packet_stream_parser_cleanup(priv->parser[POM_DIR_REV]); if (priv->data_evt) { if (event_is_started(priv->data_evt)) event_process_end(priv->data_evt); else event_cleanup(priv->data_evt); } if (priv->reply_evt) { if (event_is_started(priv->reply_evt)) event_process_end(priv->reply_evt); else event_cleanup(priv->reply_evt); } free(priv); return POM_OK; }
static int analyzer_smtp_ce_priv_cleanup(void *obj, void *priv) { struct analyzer_smtp_ce_priv *cpriv = priv; if (cpriv->evt_msg) { if (event_is_started(cpriv->evt_msg)) event_process_end(cpriv->evt_msg); else event_cleanup(cpriv->evt_msg); } if (cpriv->evt_auth) { if (event_is_started(cpriv->evt_auth)) event_process_end(cpriv->evt_auth); else event_cleanup(cpriv->evt_auth); } if (cpriv->client_hello) free(cpriv->client_hello); if (cpriv->server_hello) free(cpriv->server_hello); if (cpriv->server_host) free(cpriv->server_host); if (cpriv->client_addr) ptype_cleanup(cpriv->client_addr); if (cpriv->server_addr) ptype_cleanup(cpriv->server_addr); free(cpriv); return POM_OK; }
void event_clocktick_handle(event_t* event, struct TOS_state* state) { event_queue_t* queue = &(state->queue); clock_tick_data_t* data = (clock_tick_data_t*)event->data; atomic TOS_LOCAL_ADDRESS = (short)(event->mote & 0xffff); if (TOS_LOCAL_ADDRESS != event->mote) { dbg(DBG_ERROR, "ERROR in clock tick event handler! Things are probably ver bad....\n"); } if (data->valid) { if (dbg_active(DBG_CLOCK)) { char buf[1024]; printTime(buf, 1024); dbg(DBG_CLOCK, "CLOCK: event handled for mote %i at %s (%i ticks).\n", event->mote, buf, data->interval); } setTime[NODE_NUM] = tos_state.tos_time; event->time = event->time + data->interval; queue_insert_event(queue, event); if (!data->disabled) { TOS_ISSUE_INTERRUPT(SIG_OUTPUT_COMPARE2)(); } else { interruptPending[NODE_NUM] = 1; } } else { //dbg(DBG_CLOCK, "CLOCK: invalid event discarded.\n"); event_cleanup(event); } }
// python function cleanup(channel=None) static PyObject *py_cleanup(PyObject *self, PyObject *args, PyObject *kwargs) { int i; int chancount = -666; int found = 0; int channel = -666; unsigned int gpio; PyObject *chanlist = NULL; PyObject *chantuple = NULL; PyObject *tempobj; static char *kwlist[] = {"channel", NULL}; unsigned int bcm_gpio; void cleanup_one(void) { // clean up any /sys/class exports event_cleanup(gpio); // set everything back to input if (gpio_direction[bcm_gpio] != -1) { setup_gpio(gpio, INPUT, PUD_OFF); gpio_direction[bcm_gpio] = -1; found = 1; } }
// python function cleanup() static PyObject *py_cleanup(PyObject *self, PyObject *args) { int i; int found = 0; if (module_setup && !setup_error) { // clean up any /sys/class exports event_cleanup(); // set everything back to input for (i=0; i<54; i++) { if (gpio_direction[i] != -1) { setup_gpio(i, INPUT, PUD_OFF); gpio_direction[i] = -1; found = 1; } } } // check if any channels set up - if not warn about misuse of GPIO.cleanup() if (!found && gpio_warnings) { PyErr_WarnEx(NULL, "No channels have been set up yet - nothing to clean up! Try cleaning up at the end of your program instead!", 1); } Py_RETURN_NONE; }
int event_refcount_dec(struct event *evt) { evt->refcount--; if (!evt->refcount) return event_cleanup(evt); return POM_OK; }
// python function cleanup(channel=None) static PyObject *py_cleanup(PyObject *self, PyObject *args, PyObject *kwargs) { int i = 0; int found = 0,v = 0; int channel = -666; unsigned int gpio; unsigned int sys_gpio; static char *kwlist[] = {"channel", NULL}; v = get_lmk_revision(); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &channel)) return NULL; if (channel != -666 && get_gpio_number(channel, &gpio, &sys_gpio)) return NULL; if (module_setup && !setup_error) { if (channel == -666) { // clean up any /sys/class exports event_cleanup_all(); // set everything back to input for (i=0; i<256; i++) { if (gpio_direction[i] != -1) { debug("Clean %d \n",i); if(v == BANANAPRO){ setup_gpio(*(pinTobcm_BP+i), INPUT, PUD_OFF);//take care } else if(v == LEMAKER_GUITAR){ setup_gpio(*(pinTobcm_GT+i), INPUT, PUD_OFF);//take care } gpio_direction[i] = -1; found = 1; } } } else { // clean up any /sys/class exports event_cleanup(sys_gpio); // set everything back to input if (gpio_direction[sys_gpio] != -1) { setup_gpio(gpio, INPUT, PUD_OFF); gpio_direction[i] = -1; found = 1; } } } //printf("-->Before set warning\n"); //printf("found %d \t gpio_warnings %d\n",found,gpio_warnings); // check if any channels set up - if not warn about misuse of GPIO.cleanup() if (!found && gpio_warnings) { PyErr_WarnEx(NULL, "No channels have been set up yet - nothing to clean up! Try cleaning up at the end of your program instead!", 1); } //printf("-->After set warning\n"); Py_RETURN_NONE; }
int event_process(struct event *evt, struct proto_process_stack *stack, int stack_index, ptime ts) { int res = event_process_begin(evt, stack, stack_index, ts); if (res != POM_OK) { event_cleanup(evt); return res; } return event_process_end(evt); }
int event_process_end(struct event *evt) { debug_event("Processing event end %s", evt->reg->info->name); if (!(evt->flags & EVENT_FLAG_PROCESS_BEGAN)) { pomlog(POMLOG_ERR "Internal error: event %s processing hasn't begun", evt->reg->info->name); event_cleanup(evt); return POM_ERR; } if (evt->flags & EVENT_FLAG_PROCESS_DONE) { pomlog(POMLOG_ERR "Internal error: event %s has already been processed entirely", evt->reg->info->name); event_cleanup(evt); return POM_ERR; } struct event_listener *lst; for (lst = evt->reg->listeners; lst; lst = lst->next) { if (lst->process_end && lst->process_end(evt, lst->obj) != POM_OK) { pomlog(POMLOG_WARN "An error occured while processing event %s", evt->reg->info->name); } } for (lst = evt->tmp_listeners; lst; lst = lst->next) { if (lst->process_end && lst->process_end(evt, lst->obj) != POM_OK) { pomlog(POMLOG_WARN "An error occured while processing event %s", evt->reg->info->name); } registry_perf_dec(evt->reg->perf_listeners, 1); } evt->ce = NULL; evt->flags |= EVENT_FLAG_PROCESS_DONE; registry_perf_dec(evt->reg->perf_ongoing, 1); registry_perf_inc(evt->reg->perf_processed, 1); return event_refcount_dec(evt); }
/** * @brief Cleans up and removes active events. */ void events_cleanup (void) { int i; /* Free active events. */ for (i=0; i<event_nactive; i++) event_cleanup( &event_active[i] ); if (event_active != NULL) { free(event_active); } event_active = NULL; event_nactive = 0; event_mactive = 0; }
// python function cleanup(channel=None) static PyObject *py_cleanup(PyObject *self, PyObject *args, PyObject *kwargs) { int i; int found = 0; int channel = -666; unsigned int gpio; static char *kwlist[] = {"channel", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &channel)) return NULL; if (channel != -666 && get_gpio_number(channel, &gpio)) return NULL; if (module_setup && !setup_error) { if (channel == -666) { // clean up any /sys/class exports event_cleanup_all(); // set everything back to input for (i=0; i<54; i++) { if (gpio_direction[i] != -1) { setup_gpio(i, INPUT, PUD_OFF); gpio_direction[i] = -1; found = 1; } } } else { // clean up any /sys/class exports event_cleanup(gpio); // set everything back to input if (gpio_direction[gpio] != -1) { setup_gpio(gpio, INPUT, PUD_OFF); gpio_direction[i] = -1; found = 1; } } } // check if any channels set up - if not warn about misuse of GPIO.cleanup() if (!found && gpio_warnings) { PyErr_WarnEx(NULL, "No channels have been set up yet - nothing to clean up! Try cleaning up at the end of your program instead!", 1); } Py_RETURN_NONE; }
// python function cleanup() static PyObject *py_cleanup(PyObject *self, PyObject *args) { int i; // clean up any /sys/class exports event_cleanup(); // set everything back to input for (i=0; i<54; i++) if (gpio_direction[i] != -1) { // printf("GPIO %d --> INPUT\n", i); setup_gpio(i, INPUT, PUD_OFF); gpio_direction[i] = -1; } Py_INCREF(Py_None); return Py_None; }
/** * @brief Removes an event by ID. * * @param eventid ID of the event to remove. */ void event_remove( unsigned int eventid ) { int i; Event_t *ev; /* Find the event. */ for (i=0; i<event_nactive; i++) { ev = &event_active[i]; if (ev->id == eventid) { /* Clean up event. */ event_cleanup(ev); /* Move memory. */ memmove( &event_active[i], &event_active[i+1], sizeof(Event_t) * (event_nactive-i-1) ); event_nactive--; return; } } WARN("Event ID '%u' not valid.", eventid); }
static int proto_ppp_pap_process(void *proto_priv, struct packet *p, struct proto_process_stack *stack, unsigned int stack_index) { struct proto_process_stack *s = &stack[stack_index]; if (sizeof(struct ppp_pap_header) > s->plen) return PROTO_INVALID; struct ppp_pap_header *pchdr = s->pload; size_t len = ntohs(pchdr->length); if (len > s->plen) return PROTO_INVALID; // Keep only the payload len len -= sizeof(struct ppp_pap_header); PTYPE_UINT8_SETVAL(s->pkt_info->fields_value[proto_ppp_pap_field_code], pchdr->code); PTYPE_UINT8_SETVAL(s->pkt_info->fields_value[proto_ppp_pap_field_identifier], pchdr->identifier); struct proto_ppp_pap_priv *priv = proto_priv; if (conntrack_get(stack, stack_index) != POM_OK) return PROTO_ERR; if (conntrack_delayed_cleanup(s->ce, *PTYPE_UINT32_GETVAL(priv->p_auth_timeout), p->ts) != POM_OK) { conntrack_unlock(s->ce); return PROTO_ERR; } conntrack_unlock(s->ce); if (pchdr->code == 1 && event_has_listener(priv->evt_request)) { if (len < 4) return PROTO_INVALID; uint8_t *peer_id_len = s->pload + sizeof(struct ppp_pap_header); if (*peer_id_len > len - 2) return PROTO_INVALID; len -= (*peer_id_len + 1); uint8_t *pwd_len = peer_id_len + *peer_id_len + 1; if (*pwd_len > len - 1) return PROTO_INVALID; // Process the challenge/response event struct event *evt = event_alloc(priv->evt_request); if (!evt) return PROTO_ERR; struct data *evt_data = event_get_data(evt); PTYPE_UINT8_SETVAL(evt_data[evt_ppp_pap_request_code].value, pchdr->code); data_set(evt_data[evt_ppp_pap_request_code]); PTYPE_UINT8_SETVAL(evt_data[evt_ppp_pap_request_identifier].value, pchdr->identifier); data_set(evt_data[evt_ppp_pap_request_identifier]); PTYPE_STRING_SETVAL_N(evt_data[evt_ppp_pap_request_peer_id].value, (char *)peer_id_len + 1, *peer_id_len); data_set(evt_data[evt_ppp_pap_request_peer_id]); PTYPE_STRING_SETVAL_N(evt_data[evt_ppp_pap_request_password].value, (char *)pwd_len + 1, *pwd_len); data_set(evt_data[evt_ppp_pap_request_password]); if (event_process(evt, stack, stack_index, p->ts) != POM_OK) return PROTO_ERR; } if ((pchdr->code == 2 || pchdr->code == 3) && event_has_listener(priv->evt_ack_nack)) { struct event *evt = event_alloc(priv->evt_ack_nack); if (!evt) return PROTO_ERR; struct data *evt_data = event_get_data(evt); PTYPE_UINT8_SETVAL(evt_data[evt_ppp_pap_ack_nack_code].value, pchdr->code); data_set(evt_data[evt_ppp_pap_ack_nack_code]); PTYPE_UINT8_SETVAL(evt_data[evt_ppp_pap_ack_nack_identifier].value, pchdr->identifier); data_set(evt_data[evt_ppp_pap_ack_nack_identifier]); uint8_t *msg_len = s->pload + sizeof(struct ppp_pap_header); if (*msg_len > len - 1) { event_cleanup(evt); return PROTO_INVALID; } PTYPE_STRING_SETVAL_N(evt_data[evt_ppp_pap_ack_nack_message].value, (char *)msg_len + 1, *msg_len); data_set(evt_data[evt_ppp_pap_ack_nack_message]); if (event_process(evt, stack, stack_index, p->ts) != POM_OK) return PROTO_ERR; } return PROTO_OK; }
int analyzer_eap_finalize(struct analyzer_eap_priv *apriv, struct analyzer_eap_ce_priv *cpriv) { if (!cpriv->evt_request || !cpriv->evt_response) return POM_OK; struct event *evt = NULL; struct data *evt_data = NULL; struct data *evt_req_data = event_get_data(cpriv->evt_request); struct data *evt_rsp_data = event_get_data(cpriv->evt_response); if (!data_is_set(evt_rsp_data[evt_eap_md5_challenge_value])) return POM_OK; if (!data_is_set(evt_req_data[evt_eap_md5_challenge_value])) return POM_OK; evt = event_alloc(apriv->evt_md5_auth); if (!evt) return POM_ERR; evt_data = event_get_data(evt); if (ptype_copy(evt_data[analyzer_eap_md5_challenge].value, evt_req_data[evt_eap_md5_challenge_value].value) != POM_OK) { event_cleanup(evt); return POM_ERR; } data_set(evt_data[analyzer_eap_md5_challenge]); if (ptype_copy(evt_data[analyzer_eap_md5_response].value, evt_rsp_data[evt_eap_md5_challenge_value].value) != POM_OK) { event_cleanup(evt); return POM_ERR; } data_set(evt_data[analyzer_eap_md5_response]); if (cpriv->client) { evt_data[analyzer_eap_common_client].value = cpriv->client; data_set(evt_data[analyzer_eap_common_client]); data_do_clean(evt_data[analyzer_eap_common_client]); cpriv->client = NULL; } if (cpriv->server) { evt_data[analyzer_eap_common_server].value = cpriv->server; data_set(evt_data[analyzer_eap_common_server]); data_do_clean(evt_data[analyzer_eap_common_server]); cpriv->server = NULL; } if (cpriv->vlan) { evt_data[analyzer_eap_common_vlan].value = cpriv->vlan; data_set(evt_data[analyzer_eap_common_vlan]); data_do_clean(evt_data[analyzer_eap_common_vlan]); cpriv->vlan = NULL; } if (cpriv->top_proto) { PTYPE_STRING_SETVAL(evt_data[analyzer_eap_common_top_proto].value, cpriv->top_proto); data_set(evt_data[analyzer_eap_common_top_proto]); } if (ptype_copy(evt_data[analyzer_eap_common_identifier].value, evt_req_data[evt_eap_common_identifier].value) != POM_OK) { event_cleanup(evt); return POM_ERR; } data_set(evt_data[analyzer_eap_common_identifier]); if (!data_is_set(evt_rsp_data[evt_eap_md5_challenge_name])) { event_cleanup(evt); return POM_OK; } if (ptype_copy(evt_data[analyzer_eap_common_username].value, evt_rsp_data[evt_eap_md5_challenge_name].value) != POM_OK) { event_cleanup(evt); return POM_ERR; } data_set(evt_data[analyzer_eap_common_username]); if (cpriv->evt_result) { struct data *evt_res_data = event_get_data(cpriv->evt_result); ptype_copy(evt_data[analyzer_eap_common_success].value, evt_res_data[evt_eap_success_failure_success].value); data_set(evt_data[analyzer_eap_common_success]); event_refcount_dec(cpriv->evt_result); cpriv->evt_result = NULL; } ptime ts = event_get_timestamp(cpriv->evt_response); event_refcount_dec(cpriv->evt_request); cpriv->evt_request = NULL; event_refcount_dec(cpriv->evt_response); cpriv->evt_response = NULL; return event_process(evt, NULL, 0, ts); }
static int analyzer_smtp_event_process_begin(struct event *evt, void *obj, struct proto_process_stack *stack, unsigned int stack_index) { struct analyzer *analyzer = obj; struct analyzer_smtp_priv *apriv = analyzer->priv; struct proto_process_stack *s = &stack[stack_index]; if (!s->ce) return POM_ERR; // Only process stuff if we have the DATA event or if we already have an event struct event_reg *evt_reg = event_get_reg(evt); struct data *evt_data = event_get_data(evt); struct analyzer_smtp_ce_priv *cpriv = conntrack_get_priv(s->ce, analyzer); // It's expected that an SMTP connection will always contain at least one message // So we always create the cpriv and event, no matter what if (!cpriv) { cpriv = malloc(sizeof(struct analyzer_smtp_ce_priv)); if (!cpriv) { pom_oom(sizeof(struct analyzer_smtp_ce_priv)); return POM_ERR; } memset(cpriv, 0, sizeof(struct analyzer_smtp_ce_priv)); if (conntrack_add_priv(s->ce, analyzer, cpriv, analyzer_smtp_ce_priv_cleanup) != POM_OK) { free(cpriv); return POM_ERR; } } if (!cpriv->evt_msg) { cpriv->evt_msg = event_alloc(apriv->evt_msg); if (!cpriv->evt_msg) return POM_ERR; } struct data *msg_data = event_get_data(cpriv->evt_msg); if (evt_reg == apriv->evt_cmd) { if (!cpriv->common_data_fetched) analyzer_smtp_event_fetch_common_data(cpriv, stack, stack_index, POM_DIR_REVERSE(s->direction)); // Process commands // A message was being transmitted and we recevied a new command if (event_is_started(cpriv->evt_msg)) { event_process_end(cpriv->evt_msg); cpriv->evt_msg = NULL; } char *cmd = PTYPE_STRING_GETVAL(evt_data[proto_smtp_cmd_name].value); if (!cmd) return POM_OK; char *arg = PTYPE_STRING_GETVAL(evt_data[proto_smtp_cmd_arg].value); if (arg) { while (*arg == ' ') arg++; } if (!strcasecmp(cmd, "MAIL")) { if (strncasecmp(arg, "FROM:", strlen("FROM:"))) { pomlog(POMLOG_DEBUG "Unparseable MAIL command"); return POM_OK; } arg += strlen("FROM:"); while (*arg == ' ') arg++; if (*arg == '<') arg++; size_t len; char *end = strchr(arg, '>'); if (end) len = end - arg; else len = strlen(arg); PTYPE_STRING_SETVAL_N(msg_data[analyzer_smtp_msg_from].value, arg, len); data_set(msg_data[analyzer_smtp_msg_from]); cpriv->last_cmd = analyzer_smtp_last_cmd_mail_from; } else if (!strcasecmp(cmd, "RCPT")) { if (strncasecmp(arg, "TO:", strlen("TO:"))) { pomlog(POMLOG_DEBUG "Unparseable RCPT command"); return POM_OK; } arg += strlen("TO:"); while (*arg == ' ') arg++; if (*arg == '<') arg++; size_t len; char *end = strchr(arg, '>'); if (end) len = end - arg; else len = strlen(arg); struct ptype *to = ptype_alloc("string"); if (!to) return POM_ERR; PTYPE_STRING_SETVAL_N(to, arg, len); if (data_item_add_ptype(msg_data, analyzer_smtp_msg_to, strdup("to"), to) != POM_OK) { ptype_cleanup(to); return POM_ERR; } cpriv->last_cmd = analyzer_smtp_last_cmd_rcpt_to; } else if (!strcasecmp(cmd, "DATA")) { cpriv->last_cmd = analyzer_smtp_last_cmd_data; if (!event_is_started(cpriv->evt_msg)) { analyzer_smtp_event_fill_common_data(cpriv, msg_data); event_process_begin(cpriv->evt_msg, stack, stack_index, event_get_timestamp(evt)); } else { pomlog(POMLOG_DEBUG "Message event already started !"); } } else if (!strcasecmp(cmd, "RSET")) { // Cleanup the event event_cleanup(cpriv->evt_msg); cpriv->evt_msg = NULL; cpriv->last_cmd = analyzer_smtp_last_cmd_other; } else if (!strcasecmp(cmd, "HELO") || !strcasecmp(cmd, "EHLO")) { if (cpriv->client_hello) { pomlog(POMLOG_DEBUG "We already have a client hello !"); free(cpriv->client_hello); } cpriv->client_hello = strdup(arg); if (!cpriv->client_hello) { pom_oom(strlen(arg) + 1); return POM_ERR; } cpriv->last_cmd = analyzer_smtp_last_cmd_other; } else if (!strcasecmp(cmd, "AUTH")) { if (!strncasecmp(arg, "PLAIN", strlen("PLAIN"))) { arg += strlen("PLAIN"); while (*arg == ' ') arg++; if (cpriv->evt_auth) { event_process_end(cpriv->evt_auth); cpriv->evt_auth = NULL; } if (strlen(arg)) { if (analyzer_smtp_parse_auth_plain(apriv, cpriv, arg) == POM_OK) { event_process_begin(cpriv->evt_auth, stack, stack_index, event_get_timestamp(evt)); cpriv->last_cmd = analyzer_smtp_last_cmd_auth_plain_creds; } } else { cpriv->last_cmd = analyzer_smtp_last_cmd_auth_plain; } } else if (!strncasecmp(arg, "LOGIN", strlen("LOGIN"))) { arg += strlen("LOGIN"); while (*arg == ' ') arg++; if (cpriv->evt_auth) { event_process_end(cpriv->evt_auth); cpriv->evt_auth = NULL; } cpriv->evt_auth = event_alloc(apriv->evt_auth); if (!cpriv->evt_auth) return POM_ERR; struct data *auth_data = event_get_data(cpriv->evt_auth); analyzer_smtp_event_fill_common_data(cpriv, auth_data); // Set the authentication type PTYPE_STRING_SETVAL(auth_data[analyzer_smtp_auth_type].value, "LOGIN"); data_set(auth_data[analyzer_smtp_auth_type]); if (strlen(arg)) { char *username = NULL; size_t out_len = 0; struct ptype *username_pt = NULL; if (decoder_decode_simple("base64", arg, strlen(arg), &username, &out_len) == POM_OK) { username_pt = ptype_alloc("string"); if (username_pt) { PTYPE_STRING_SETVAL_P(username_pt, username); if (data_item_add_ptype(auth_data, analyzer_smtp_auth_params, strdup("username"), username_pt) != POM_OK) { ptype_cleanup(username_pt); event_cleanup(cpriv->evt_auth); cpriv->evt_auth = NULL; username_pt = NULL; } } else { free(username); } } if (!username_pt) { cpriv->last_cmd = analyzer_smtp_last_cmd_other; event_process_begin(cpriv->evt_auth, stack, stack_index, event_get_timestamp(evt)); } } else { cpriv->last_cmd = analyzer_smtp_last_cmd_auth_login; } } } else if (cpriv->last_cmd == analyzer_smtp_last_cmd_auth_plain) { // We are expecting the credentials right now if (analyzer_smtp_parse_auth_plain(apriv, cpriv, cmd) == POM_OK) { event_process_begin(cpriv->evt_auth, stack, stack_index, event_get_timestamp(evt)); cpriv->last_cmd = analyzer_smtp_last_cmd_auth_plain_creds; } else { cpriv->last_cmd = analyzer_smtp_last_cmd_other; } } else if (cpriv->last_cmd == analyzer_smtp_last_cmd_auth_login) { char *username = NULL; size_t out_len = 0; struct ptype *username_pt = NULL; if (decoder_decode_simple("base64", cmd, strlen(cmd), &username, &out_len) == POM_OK) { username_pt = ptype_alloc("string"); if (username_pt) { PTYPE_STRING_SETVAL_P(username_pt, username); struct data *auth_data = event_get_data(cpriv->evt_auth); if (data_item_add_ptype(auth_data, analyzer_smtp_auth_params, strdup("username"), username_pt) != POM_OK) { ptype_cleanup(username_pt); event_process_end(cpriv->evt_auth); cpriv->evt_auth = NULL; username_pt = NULL; } } else { free(username); } } if (!username_pt) { cpriv->last_cmd = analyzer_smtp_last_cmd_other; } else { event_process_begin(cpriv->evt_auth, stack, stack_index, event_get_timestamp(evt)); cpriv->last_cmd = analyzer_smtp_last_cmd_auth_login_user; } } else if (cpriv->last_cmd == analyzer_smtp_last_cmd_auth_login_user) { char *password = NULL; size_t out_len = 0; struct ptype *password_pt = NULL; if (decoder_decode_simple("base64", cmd, strlen(cmd), &password, &out_len) == POM_OK) { password_pt = ptype_alloc("string"); if (password_pt) { PTYPE_STRING_SETVAL_P(password_pt, password); struct data *auth_data = event_get_data(cpriv->evt_auth); if (data_item_add_ptype(auth_data, analyzer_smtp_auth_params, strdup("password"), password_pt) != POM_OK) { ptype_cleanup(password_pt); event_process_end(cpriv->evt_auth); cpriv->evt_auth = NULL; password_pt = NULL; } } else { free(password); } } if (!password_pt) { cpriv->last_cmd = analyzer_smtp_last_cmd_other; } else { cpriv->last_cmd = analyzer_smtp_last_cmd_auth_login_pass; } } else { cpriv->last_cmd = analyzer_smtp_last_cmd_other; } } else if (evt_reg == apriv->evt_reply) { if (!cpriv->common_data_fetched) analyzer_smtp_event_fetch_common_data(cpriv, stack, stack_index, s->direction); // Process replies uint16_t code = *PTYPE_UINT16_GETVAL(evt_data[proto_smtp_reply_code].value); switch (cpriv->last_cmd) { default: case analyzer_smtp_last_cmd_other: if (code == 220 && evt_data[proto_smtp_reply_text].items && evt_data[proto_smtp_reply_text].items->value) { // STARTTLS returns 220 as well so ignore extra code 220 if (!cpriv->server_hello) { char *helo = PTYPE_STRING_GETVAL(evt_data[proto_smtp_reply_text].items->value); cpriv->server_hello = strdup(helo); if (!cpriv->server_hello) { pom_oom(strlen(helo) + 1); return POM_ERR; } } } break; case analyzer_smtp_last_cmd_mail_from: if (code != 250) { // FROM is invalid data_unset(msg_data[analyzer_smtp_msg_from]); } break; case analyzer_smtp_last_cmd_rcpt_to: // For now just don't do anything // It's best to keep a destination in there even if it's invalid or denied break; case analyzer_smtp_last_cmd_data: if (code == 354) { // The message is starting, keep last_cmd intact return POM_OK; } // Message is over (if ever transmited) if (event_is_started(cpriv->evt_msg)) { struct data *msg_data = event_get_data(cpriv->evt_msg); PTYPE_UINT16_SETVAL(msg_data[analyzer_smtp_msg_result].value, code); data_set(msg_data[analyzer_smtp_msg_result]); event_process_end(cpriv->evt_msg); cpriv->evt_msg = NULL; } break; case analyzer_smtp_last_cmd_auth_plain: case analyzer_smtp_last_cmd_auth_login: case analyzer_smtp_last_cmd_auth_login_user: // Check if authentication phase can continue if (code == 334) { // Don't reset cpriv->last_cmd return POM_OK; } else { struct data *evt_data = event_get_data(cpriv->evt_auth); PTYPE_BOOL_SETVAL(evt_data[analyzer_smtp_auth_success].value, 0); data_set(evt_data[analyzer_smtp_auth_success]); event_process_end(cpriv->evt_auth); cpriv->evt_auth = NULL; } break; case analyzer_smtp_last_cmd_auth_plain_creds: case analyzer_smtp_last_cmd_auth_login_pass: { // We just processed the credentials struct data *auth_data = event_get_data(cpriv->evt_auth); char success = 0; if (code == 235) success = 1; PTYPE_BOOL_SETVAL(auth_data[analyzer_smtp_auth_success].value, success); data_set(auth_data[analyzer_smtp_auth_success]); event_process_end(cpriv->evt_auth); cpriv->evt_auth = NULL; break; } } cpriv->last_cmd = analyzer_smtp_last_cmd_other; } return POM_OK; }
static int analyzer_smtp_parse_auth_plain(struct analyzer_smtp_priv *apriv, struct analyzer_smtp_ce_priv *cpriv, char *auth_plain) { // Parse SASL AUTH PLAIN as described in RFC 4616 // The decoded arg must be at least 3 bytes if (strlen(auth_plain) < 4 || memchr(auth_plain, '=', 4)) { pomlog(POMLOG_DEBUG "AUTH PLAIN argument too short"); return POM_OK; } // Allocate the event cpriv->evt_auth = event_alloc(apriv->evt_auth); if (!cpriv->evt_auth) return POM_ERR; struct data *evt_data = event_get_data(cpriv->evt_auth); analyzer_smtp_event_fill_common_data(cpriv, evt_data); // Set the authentication type PTYPE_STRING_SETVAL(evt_data[analyzer_smtp_auth_type].value, "PLAIN"); data_set(evt_data[analyzer_smtp_auth_type]); // Parse the authentication stuff char *creds_str = NULL; size_t out_len = 0; if (decoder_decode_simple("base64", auth_plain, strlen(auth_plain), &creds_str, &out_len) != POM_OK) { pomlog(POMLOG_DEBUG "Unable to decode AUTH PLAIN message"); return POM_OK; } if (out_len < 3) { pomlog(POMLOG_DEBUG "Invalid decoded AUTH PLAIN data"); return POM_OK; } char *tmp = creds_str; // Add the identity if (strlen(tmp)) { // SASL AUTH PLAIN specifies struct ptype *identity = ptype_alloc("string"); if (!identity) goto err; PTYPE_STRING_SETVAL(identity, tmp); if (data_item_add_ptype(evt_data, analyzer_smtp_auth_params, strdup("identity"), identity) != POM_OK) { ptype_cleanup(identity); goto err; } } tmp += strlen(tmp) + 1; // Add the username struct ptype *username = ptype_alloc("string"); if (!username) goto err; PTYPE_STRING_SETVAL(username, tmp); if (data_item_add_ptype(evt_data, analyzer_smtp_auth_params, strdup("username"), username) != POM_OK) { ptype_cleanup(username); goto err; } tmp += strlen(tmp) + 1; // Add the password struct ptype *password = ptype_alloc("string"); if (!password) goto err; PTYPE_STRING_SETVAL(password, tmp); if (data_item_add_ptype(evt_data, analyzer_smtp_auth_params, strdup("password"), password) != POM_OK) { ptype_cleanup(password); goto err; } free(creds_str); return POM_OK; err: event_cleanup(cpriv->evt_auth); cpriv->evt_auth = NULL; free(creds_str); return POM_ERR; }
void event_cleanup_all(void) { event_cleanup(-666); }
int analyzer_ppp_pap_finalize(struct analyzer_ppp_pap_priv *apriv, struct analyzer_ppp_pap_ce_priv *cpriv) { if (!cpriv->evt_request) return POM_OK; struct event *evt = NULL; struct data *evt_data = NULL; struct data *evt_req_data = event_get_data(cpriv->evt_request); evt = event_alloc(apriv->evt_auth); if (!evt) return POM_ERR; evt_data = event_get_data(evt); if (ptype_copy(evt_data[analyzer_ppp_pap_auth_peer_id].value, evt_req_data[evt_ppp_pap_request_peer_id].value) != POM_OK) { event_cleanup(evt); return POM_ERR; } data_set(evt_data[analyzer_ppp_pap_auth_peer_id]); if (ptype_copy(evt_data[analyzer_ppp_pap_auth_password].value, evt_req_data[evt_ppp_pap_request_password].value) != POM_OK) { event_cleanup(evt); return POM_ERR; } data_set(evt_data[analyzer_ppp_pap_auth_password]); if (cpriv->client) { evt_data[analyzer_ppp_pap_auth_client].value = cpriv->client; data_set(evt_data[analyzer_ppp_pap_auth_client]); data_do_clean(evt_data[analyzer_ppp_pap_auth_client]); cpriv->client = NULL; } if (cpriv->server) { evt_data[analyzer_ppp_pap_auth_server].value = cpriv->server; data_set(evt_data[analyzer_ppp_pap_auth_server]); data_do_clean(evt_data[analyzer_ppp_pap_auth_server]); cpriv->server = NULL; } if (cpriv->vlan) { evt_data[analyzer_ppp_pap_auth_vlan].value = cpriv->vlan; data_set(evt_data[analyzer_ppp_pap_auth_vlan]); data_do_clean(evt_data[analyzer_ppp_pap_auth_vlan]); cpriv->vlan = NULL; } if (cpriv->top_proto) { PTYPE_STRING_SETVAL(evt_data[analyzer_ppp_pap_auth_top_proto].value, cpriv->top_proto); data_set(evt_data[analyzer_ppp_pap_auth_top_proto]); } if (ptype_copy(evt_data[analyzer_ppp_pap_auth_identifier].value, evt_req_data[evt_ppp_pap_request_identifier].value) != POM_OK) { event_cleanup(evt); return POM_ERR; } data_set(evt_data[analyzer_ppp_pap_auth_identifier]); if (cpriv->evt_ack_nack) { struct data *evt_ack_data = event_get_data(cpriv->evt_ack_nack); uint8_t code = *PTYPE_UINT8_GETVAL(evt_ack_data[evt_ppp_pap_ack_nack_code].value); if (code == 2) { PTYPE_BOOL_SETVAL(evt_data[analyzer_ppp_pap_auth_success].value, 1); } else { PTYPE_BOOL_SETVAL(evt_data[analyzer_ppp_pap_auth_success].value, 0); } data_set(evt_data[analyzer_ppp_pap_auth_success]); event_refcount_dec(cpriv->evt_ack_nack); cpriv->evt_ack_nack = NULL; } ptime ts = event_get_timestamp(cpriv->evt_request); event_refcount_dec(cpriv->evt_request); cpriv->evt_request = NULL; return event_process(evt, NULL, 0, ts); }
// Process commands that are posted to the event queue void event_command_in_handle(event_t* event, struct TOS_state* state) { incoming_command_data_t* cmdData = (incoming_command_data_t*)event->data; GuiMsg* msg = cmdData->msg; dbg_clear(DBG_SIM, "SIM: Handling incoming command type %d for mote %d\n", msg->msgType, msg->moteID); switch (msg->msgType) { case AM_TURNONMOTECOMMAND: dbg_clear(DBG_SIM, "SIM: Turning on mote %d\n", msg->moteID); nido_start_mote(msg->moteID); break; case AM_TURNOFFMOTECOMMAND: dbg_clear(DBG_SIM, "SIM: Turning off mote %d\n", msg->moteID); nido_stop_mote(msg->moteID); break; case AM_RADIOMSGSENDCOMMAND: { RadioMsgSendCommand *rmsg = (RadioMsgSendCommand*)cmdData->payLoad; TOS_MsgPtr buffer; dbg_clear(DBG_SIM, "SIM: Enqueueing radio message for mote %d (payloadlen %d)\n", msg->moteID, msg->payLoadLen); if (external_comm_buffers_[msg->moteID] == NULL) external_comm_buffers_[msg->moteID] = &external_comm_msgs_[msg->moteID]; buffer = external_comm_buffers_[msg->moteID]; memcpy(buffer, &(rmsg->message), msg->payLoadLen); buffer->group = TOS_AM_GROUP; external_comm_buffers_[msg->moteID] = NIDO_received_radio(buffer); } break; case AM_UARTMSGSENDCOMMAND: { UARTMsgSendCommand *umsg = (UARTMsgSendCommand*)cmdData->payLoad; TOS_MsgPtr buffer; int len = (msg->payLoadLen > sizeof(TOS_Msg))? sizeof(TOS_Msg):msg->payLoadLen; dbg_clear(DBG_SIM, "SIM: Enqueueing UART message for mote %d (payloadlen %d)\n", msg->moteID, msg->payLoadLen); if (external_comm_buffers_[msg->moteID] == NULL) external_comm_buffers_[msg->moteID] = &external_comm_msgs_[msg->moteID]; buffer = external_comm_buffers_[msg->moteID]; memcpy(buffer, &(umsg->message), len); buffer->group = TOS_AM_GROUP; external_comm_buffers_[msg->moteID] = NIDO_received_uart(buffer); } break; case AM_INTERRUPTCOMMAND: { InterruptEvent interruptEvent; InterruptCommand* pcmd = (InterruptCommand*)cmdData->payLoad; interruptEvent.id = pcmd->id; dbg_clear(DBG_TEMP, "\nSIM: Interrupt command, id: %i.\n\n", pcmd->id); sendTossimEvent(TOS_BCAST_ADDR, AM_INTERRUPTEVENT, tos_state.tos_time, &interruptEvent); break; } default: dbg_clear(DBG_SIM, "SIM: Unrecognizable command type received from TinyViz %i\n", msg->msgType); break; } event_cleanup(event); }