const response* handle_message_end(void) { const response* resp = 0; if (!response_ok(data_response)) return data_response; MODULE_CALL(message_end, (session.fd), 1); if (session.backend->message_end != 0) resp = session.backend->message_end(session.fd); if (session.fd >= 0) close(session.fd); session.fd = -1; if (!response_ok(resp)) handle_reset(); return resp; }
static int EHLO(void) { const response* resp; protocol.name = "ESMTP"; line.len = 0; if (!response_ok(resp = handle_reset()) || !response_ok(resp = handle_helo(&arg, &line))) return respond(resp); if (!str_cat(&line, &init_capabilities)) { respond(&resp_oom); return 0; } if (!respond_line(250, 0, domain_name.s, domain_name.len)) return 0; return respond_multiline(250, 1, line.s); }
static int HELO(void) { const response* resp; if (response_ok(resp = handle_reset())) resp = handle_helo(&arg, &line); return (resp != 0) ? respond(resp) : respond_line(250, 1, domain_name.s, domain_name.len); }
static void do_reset(void) { const response* resp; if (!response_ok(resp = handle_reset())) { respond(resp); exit(0); } saw_rcpt = 0; saw_mail = 0; }
void handle_data_bytes(const char* bytes, unsigned len) { const response* r; struct plugin* plugin; if (!response_ok(data_response)) return; for (plugin = session.plugin_list; plugin != 0; plugin = plugin->next) if (plugin->data_block != 0) if ((r = plugin->data_block(bytes, len)) != 0 && !response_ok(r)) { handle_reset(); data_response = r; return; } if (session.backend->data_block != 0) session.backend->data_block(bytes, len); if (session.fd >= 0) if (write(session.fd, bytes, len) != (ssize_t)len) { handle_reset(); data_response = &resp_internal; } }
const response* handle_data_start(void) { const response* resp = 0; if (session.fd >= 0) { close(session.fd); session.fd = -1; } if (session.flags & FLAG_NEED_FILE) { if ((session.fd = scratchfile()) == -1) return &resp_internal; } data_response = 0; if (session.backend->data_start != 0) resp = session.backend->data_start(session.fd); if (response_ok(resp)) MODULE_CALL(data_start, (session.fd), 1); if (!response_ok(resp)) { handle_reset(); data_response = resp; } return resp; }
void CoAP_RD_Resource::handler_post(CoAPCallback &callback) { std::string ep_identifier = ""; std::string ep_domain = "hisilicion"; if (get_wrapper() != 0) get_wrapper()->get_domain_and_endpointIdentifier(callback, ep_domain, ep_identifier); if (ep_identifier.empty()) { get_wrapper()->bad_request(callback); return; } CoAPResource *r = find_resource_from_cache_by_ep(ep_domain, ep_identifier); if (r) { r->update(callback); get_wrapper()->changed_request(callback); } else { r = create_node_resource(callback, ep_domain, ep_identifier); if (r) { void *rd; std::string payload; add_node_to_cache(r); r->update(callback); rd = r->Create(); r->SetCoAPResource(rd); get_wrapper()->get_payload(callback, payload); r->set_payload(payload); response_ok(r, callback); } else { get_wrapper()->internal_err_request(callback); } } }
const response* handle_sender(str* sender, str* params) { const response* resp = 0; const response* tmpresp = 0; if (params == 0) { no_params.len = 0; params = &no_params; } MODULE_CALL(sender, (sender, params), 0); if (resp == 0) return &resp_no_sender; if (session.backend->sender != 0) if (!response_ok(tmpresp = session.backend->sender(sender, params))) return tmpresp; if (resp == 0 || resp->message == 0) resp = tmpresp; return resp; }
const response* handle_recipient(str* recip, str* params) { const response* resp = 0; const response* hresp = 0; if (params == 0) { no_params.len = 0; params = &no_params; } MODULE_CALL(recipient, (recip, params), 0); if (resp == 0) return &resp_no_rcpt; if (session.backend->recipient != 0) if (!response_ok(hresp = session.backend->recipient(recip, params))) return hresp; if (resp == 0 || resp->message == 0) resp = hresp; return resp; }
static int DATA(void) { const response* resp; if (!saw_mail) return respond(&resp_no_mail); if (!saw_rcpt) return respond(&resp_no_rcpt); if (!response_ok(resp = handle_data_start())) return respond(resp); if (!respond(&resp_data_ok)) return 0; if (!copy_body()) { do_reset(); return 0; } if ((resp = handle_message_end()) == 0) resp = &resp_accepted_message; return respond(resp); }
//----------------------------------------------------------------------------- void *handle_request (void *data) { bool done; struct conn_info *info; char msg_buf[MSG_MAX]; char *cmd, *port, *p; struct response_node response_head; struct recv_buf recv_buf = { .pos = 0, .len = 0 }; info = data; done = false; while (!done) { // go to cleanup if connection was closed if (!read_msg (info->sock, &recv_buf, msg_buf)) break; // parse message cmd = strtok_r (msg_buf, " \r\n", &p); port = strtok_r (NULL, " \r\n", &p); // construct response response_head.next = NULL; if (!cmd) { response_bad (&response_head); } else if (cmd_equal (cmd, "CONNECT", 7)) { if (!port || add_client (info->addr, port)) { response_bad (&response_head); } else { response_ok (&response_head); #ifdef P2PSERV_LOG printf (ANSI_GREEN "+ %s %s\n" ANSI_RESET, info->addr, port); #endif } } else if (cmd_equal (cmd, "DISCONNECT", 10)) { if (!port || remove_client (info->addr, port)) { response_bad (&response_head); } else { response_ok (&response_head); #ifdef P2PSERV_LOG printf (ANSI_RED "- %s %s\n" ANSI_RESET, info->addr, port); #endif } } else if (cmd_equal (cmd, "LIST", 4)) { struct response_node *jlist; if (!port || clients_to_json (&jlist, info->addr, port)) { response_bad (&response_head); } else { response_head.next = jlist; #ifdef P2PSERV_LOG printf (ANSI_YELLOW "L %s %s\n" ANSI_RESET, info->addr, port); #endif } } else if (cmd_equal (cmd, "EXIT", 4)) { response_ok (&response_head); done = true; } else { response_bad (&response_head); } // send response //send_msg (info->sock, response.str, response.len); //free (response.str); send_response (info->sock, response_head.next); free_response (response_head.next); } #ifdef P2PSERV_LOG printf ("D %s\n", info->addr); fflush (stdout); #endif close (info->sock); free (info); pthread_mutex_lock (&num_threads_lock); num_threads--; pthread_mutex_unlock (&num_threads_lock); pthread_exit (NULL); }