TEST(http, connections_overflow) { WEB_DECL(); uv_os_sock_t sock[2]; uv_os_sock_t overflow; char buf[2048]; size_t s; WEB_INIT(); settings.max_connections_size = 2; //shttp_set_log_callback(&on_log, &log_buf); WEB_START(); sock[0] = connect_tcp("127.0.0.1", TEST_PORT); sock[1] = connect_tcp("127.0.0.1", TEST_PORT); overflow = connect_tcp("127.0.0.1", TEST_PORT); s = max_recv(overflow, buf, 2048, RECV_TIMEOUT); ASSERT_EQ( s, 0); closesocket(sock[0]); closesocket(sock[1]); closesocket(overflow); WEB_STOP(); }
int start_tcp(void) { int ret = 0; if (IS_ENABLED(CONFIG_NET_IPV6)) { ret = connect_tcp(&tcp6, CONFIG_NET_APP_PEER_IPV6_ADDR, &conf.ipv6, tls_result_ipv6, sizeof(tls_result_ipv6), net_app_tls_stack_ipv6, K_THREAD_STACK_SIZEOF( net_app_tls_stack_ipv6)); if (ret < 0) { NET_ERR("Cannot init IPv6 TCP client (%d)", ret); } } if (IS_ENABLED(CONFIG_NET_IPV4)) { ret = connect_tcp(&tcp4, CONFIG_NET_APP_PEER_IPV4_ADDR, &conf.ipv4, tls_result_ipv4, sizeof(tls_result_ipv4), net_app_tls_stack_ipv4, K_THREAD_STACK_SIZEOF( net_app_tls_stack_ipv4)); if (ret < 0) { NET_ERR("Cannot init IPv6 TCP client (%d)", ret); } } return ret; }
int CodeRevision::removetcp(int i) { fprintf(stderr, "removing shred %d\n", _shreds[i]->procID ); connect_tcp(); if ( _connected ) { Net_Msg msg; msg.type = MSG_REMOVE; // msg.param = _shreds[i].procID; msg.param = 0xffffffff; otf_hton( &msg); ck_send( _tcp , (char*)&msg, sizeof(msg) ); msg.type = MSG_DONE; otf_hton( &msg); ck_send( _tcp , (char*)&msg, sizeof(msg) ); // Net_Msg ret; // if ( ck_recv( _tcp , (char *)&ret, sizeof(ret) ) ) { // fprintf( stderr, "[chuck(remote)]: operation %s\n", ( ret.param ? "successful" : "failed (sorry)" ) ); _shreds.erase( _shreds.begin() + i ); // } } close_tcp(); return 1; }
TEST(http, large_headers) { // large_headers will realloc memory while reading headers, and relocate buffers WEB_DECL(); uv_os_sock_t sock; char buf[2048]; size_t s; int i; WEB_INIT(); WEB_START(); sock = connect_tcp("127.0.0.1", TEST_PORT); for(i =0; i < 3; i ++) { ASSERT_EQ(true, send_n(sock, GET_REQUEST, strlen(GET_REQUEST))); for(s =0; s < 200; s ++) { send_n(sock, "h234567890: 345678\r\n", 20); } send_n(sock, "\r\n", 2); s = max_recv(sock, buf, 2048, RECV_TIMEOUT); ASSERT_EQ( s, strlen(hello_world_response)); ASSERT_EQ( 0, strcmp(buf, hello_world_response)); } closesocket(sock); WEB_STOP(); }
TEST(http, async_reuse_connect) { WEB_DECL(); uv_os_sock_t sock; char buf[2048]; size_t s; WEB_INIT(); settings.callbacks.on_message_complete = &on_message_complete_muti_write_async; WEB_START(); sock = connect_tcp("127.0.0.1", TEST_PORT); ASSERT_EQ(true, send_n(sock, simple_request, strlen(simple_request))); s = max_recv(sock, buf, 2048, RECV_TIMEOUT); ASSERT_EQ( s, strlen(hello_world_response)); ASSERT_EQ( 0, strcmp(buf, hello_world_response)); ASSERT_EQ(true, send_n(sock, simple_request, strlen(simple_request))); s = max_recv(sock, buf, 2048, RECV_TIMEOUT); ASSERT_EQ( s, strlen(hello_world_response)); ASSERT_EQ( 0, strcmp(buf, hello_world_response)); closesocket(sock); WEB_STOP(); }
TEST(http, async_check_writing) { WEB_DECL(); uv_os_sock_t sock; char buf[2048]; size_t s; WEB_INIT(); shttp_set_log_callback(&on_log, &log_buf); shttp_assert_ctx = &assert_buf; shttp_assert_cb = &on_assert; settings.callbacks.on_message_complete = &on_message_complete_async_check_is_writing; WEB_START(); sock = connect_tcp("127.0.0.1", TEST_PORT); ASSERT_EQ(true, send_n(sock, simple_request, strlen(simple_request))); s = max_recv(sock, buf, 2048, RECV_TIMEOUT); //ASSERT_EQ( s, strlen(hello_world_response)); //ASSERT_EQ( 0, strcmp(buf, hello_world_response)); closesocket(sock); WEB_STOP(); assert_buf.str[assert_buf.len] = 0; ASSERT_STREQ("0 == shttp_atomic_read32(&conn_outgoing(conn).is_writing)", assert_buf.str); }
TEST(http, headers_count_too_large_at_second_request) { WEB_DECL(); uv_os_sock_t sock; char buf[2048]; size_t s; WEB_INIT(); shttp_set_log_callback(&on_log, &log_buf); WEB_START(); sock = connect_tcp("127.0.0.1", TEST_PORT); ASSERT_EQ(true, send_n(sock, GET_REQUEST "\r\n" GET_MINI_REQUEST HEAD_MINI_SIMPLE_X_200 HEAD_MINI_SIMPLE_X_50 HEAD_MINI_SIMPLE_X_10 HEAD_MINI_SIMPLE_X_10 "\r\n", strlen(GET_REQUEST "\r\n" GET_MINI_REQUEST HEAD_MINI_SIMPLE_X_200 HEAD_MINI_SIMPLE_X_50 HEAD_MINI_SIMPLE_X_10 HEAD_MINI_SIMPLE_X_10 "\r\n"))); s = max_recv(sock, buf, 2048, RECV_TIMEOUT); ASSERT_EQ( s, strlen(hello_world_response)); ASSERT_EQ( 0, strcmp(buf, hello_world_response)); closesocket(sock); WEB_STOP(); log_buf.str[log_buf.len] = 0; ASSERT_STREQ("parse error: header length too large.", log_buf.str); }
TEST(http, headers_count_to_large) { WEB_DECL(); uv_os_sock_t sock; char buf[2048]; size_t s; WEB_INIT(); shttp_set_log_callback(&on_log, &log_buf); WEB_START(); sock = connect_tcp("127.0.0.1", TEST_PORT); ASSERT_EQ(true, send_n(sock, GET_REQUEST, strlen(GET_REQUEST))); for(s =0; s < 250; s ++) { send_n(sock, "h: 3\r\n", 6); } send_n(sock, "\r\n", 2); s = max_recv(sock, buf, 2048, 200 * RECV_TIMEOUT); ASSERT_EQ( s, 0); closesocket(sock); log_buf.str[log_buf.len] = 0; ASSERT_STREQ("parse error: header length too large.", log_buf.str); WEB_STOP(); }
void connect_call_handler(unsigned long long uniqueSockID, int threads, unsigned char *buf, ssize_t len) { int index; socklen_t addrlen; struct sockaddr_in *addr; u_char *pt; pt = buf; addrlen = *(int *) pt; pt += sizeof(int); if (addrlen <= 0) { PRINT_DEBUG("READING ERROR! CRASH, addrlen=%d", addrlen); nack_send(uniqueSockID, connect_call); return; } addr = (struct sockaddr_in *) malloc(addrlen); memcpy(addr, pt, addrlen); pt += addrlen; if (pt - buf != len) { PRINT_DEBUG("READING ERROR! CRASH, diff=%d len=%d", pt - buf, len); nack_send(uniqueSockID, connect_call); return; } PRINT_DEBUG("%d,%d,%d", (addr->sin_addr).s_addr, ntohs(addr->sin_port), addr->sin_family); index = findjinniSocket(uniqueSockID); /** if that requested socket does not exist !! * this means we can not even talk to the requester FINS crash as a response!! */ if (index == -1) { PRINT_DEBUG( " CRASH !socket descriptor not found into jinni sockets! Bind failed on Jinni Side "); nack_send(uniqueSockID, connect_call); return; } if (jinniSockets[index].type == SOCK_DGRAM) { connect_udp(uniqueSockID, addr); } else if (jinniSockets[index].type == SOCK_STREAM) { connect_tcp(uniqueSockID, addr); } else { PRINT_DEBUG("This socket is of unknown type"); nack_send(uniqueSockID, connect_call); } return; }
/** * Handles TCP events from Simple TCP */ static void tcp_event(struct tcp_socket *s, void *ptr, tcp_socket_event_t event) { struct mqtt_connection* conn = ptr; /* Take care of event */ switch(event) { /* Fall through to manage different disconnect event the same way. */ case TCP_SOCKET_CLOSED: case TCP_SOCKET_TIMEDOUT: case TCP_SOCKET_ABORTED: { ctimer_stop(&conn->keep_alive_timer); call_event(conn, MQTT_EVENT_DISCONNECTED, &event); /* If connecting retry */ if(conn->state == MQTT_CONN_STATE_TCP_CONNECTING || conn->auto_reconnect == 1) { DBG("MQTT - Disconnected by tcp event %d, reconnecting...", event); connect_tcp(conn); } else { conn->state = MQTT_CONN_STATE_NOT_CONNECTED; } break; } case TCP_SOCKET_CONNECTED: { DBG("MQTT - Got TCP_SOCKET_CONNECTED\r\n"); conn->state = MQTT_CONN_STATE_TCP_CONNECTED; process_post(&mqtt_process, mqtt_do_connect_mqtt_event, conn); break; } case TCP_SOCKET_DATA_SENT: { DBG("MQTT - Got TCP_DATA_SENT\r\n"); conn->out_buffer_sent = 1; conn->out_buffer_ptr = conn->out_buffer; ctimer_restart(&conn->keep_alive_timer); break; } default: { DBG("MQTT - TCP Event %d is currently not manged by the tcp event callback\r\n", event); } } }
void TextContent::sendtcp () { string blob = ""; for ( int i = 0 ; i < _buf->nlines() ; i++ ) { blob += _buf->line(i).str(); if ( i != _buf->nlines() - 1 ) blob += "\n"; } connect_tcp(); if ( _connected ) { //DO THAT TCP MAGIC THING Net_Msg msg; msg.type = MSG_ADD; msg.param = 1; strcpy ( (char*)msg.buffer, "editor->" ); strcat ( (char*)msg.buffer, _buf->filename().c_str() ); msg.param2 = (t_CKUINT) blob.size(); //trailing NUL? msg.length = 0; otf_hton( &msg ); ck_send ( _tcp , (char *)&msg, sizeof(msg) ); t_CKUINT left = blob.size() ; //include trailing NUL ? t_CKUINT done = 0; while ( left ) { msg.length = ( left > NET_BUFFER_SIZE ) ? NET_BUFFER_SIZE : left; msg.param3 = msg.length; memcpy( (void*)msg.buffer, (void*) (blob.c_str() + done ), msg.length * sizeof (char) ); left -= msg.length; done += msg.length; msg.param2 = left; fprintf(stderr, "sending %ld to %ld of %s\n", done - msg.length, done, _buf->filename().c_str()); otf_hton( &msg ); ck_send ( _tcp, (char*)&msg, sizeof(msg) ); } msg.type = MSG_DONE; otf_hton( &msg ); ck_send (_tcp , (char*)&msg, sizeof(msg)); close_tcp(); } else { fprintf(stderr,"sendtcp::unable to establish connection to ChucK process...\n"); } }
TEST(http, muti_write) { WEB_DECL(); uv_os_sock_t sock; char buf[2048]; size_t s; WEB_INIT(); WEB_START(); sock = connect_tcp("127.0.0.1", TEST_PORT); ASSERT_EQ(true, send_n(sock, simple_request, strlen(simple_request))); s = max_recv(sock, buf, 2048, RECV_TIMEOUT); ASSERT_EQ( s, strlen(hello_world_response)); ASSERT_EQ( 0, strcmp(buf, hello_world_response)); closesocket(sock); WEB_STOP(); }
TEST(http, async_empty_message) { WEB_DECL(); uv_os_sock_t sock; char buf[2048]; size_t s; WEB_INIT(); settings.callbacks.on_message_complete = &on_message_complete_with_empty_async; WEB_START(); sock = connect_tcp("127.0.0.1", TEST_PORT); ASSERT_EQ(true, send_n(sock, simple_request, strlen(simple_request))); s = max_recv(sock, buf, 2048, RECV_TIMEOUT); ASSERT_EQ( s, strlen(EMPTY_REPONSE)); ASSERT_EQ( 0, strcmp(buf, EMPTY_REPONSE)); closesocket(sock); WEB_STOP(); }
/* * Have an exchange with the server over TCP/IP and get the IPs of our local * and the remote host. */ static void client_get_hosts(char *lhost, char *rhost) { SS raddr; socklen_t rlen; char *service; uint32_t port; int fd = -1; recv_mesg(&port, sizeof(port), "TCP IPv4 server port"); port = decode_uint32(&port); service = qasprintf("%d", port); connect_tcp(ServerName, service, &raddr, &rlen, &fd); free(service); get_socket_ip((SA *)&raddr, rlen, rhost, NI_MAXHOST); send_mesg(rhost, NI_MAXHOST, "server IP"); recv_mesg(lhost, NI_MAXHOST, "client IP"); close(fd); }
TEST(http, format_realloc) { WEB_DECL(); uv_os_sock_t sock; char buf[20480]; size_t s; WEB_INIT(); settings.callbacks.on_message_complete = &on_message_complete_format_realloc; WEB_START(); sock = connect_tcp("127.0.0.1", TEST_PORT); ASSERT_EQ(true, send_n(sock, simple_request, strlen(simple_request))); s = max_recv(sock, buf, 20480, RECV_TIMEOUT); ASSERT_EQ( s, strlen(head_abc_x_35_hello_world_response_x_52_str)); ASSERT_EQ( 0, strcmp(buf, head_abc_x_35_hello_world_response_x_52_str)); closesocket(sock); WEB_STOP(); }
TEST(http, large_headers_at_second_request) { WEB_DECL(); uv_os_sock_t sock; char buf[2048]; size_t s; WEB_INIT(); shttp_set_log_callback(&on_log, &log_buf); WEB_START(); sock = connect_tcp("127.0.0.1", TEST_PORT); ASSERT_EQ(true, send_n(sock, GET_REQUEST "\r\n" GET_REQUEST HEAD_SIMPLE_X_100 "\r\n", strlen(GET_REQUEST "\r\n" GET_REQUEST HEAD_SIMPLE_X_100 "\r\n"))); s = max_recv(sock, buf, 2048, RECV_TIMEOUT); ASSERT_EQ( s, 2*strlen(hello_world_response)); ASSERT_EQ( 0, strcmp(buf, HELLO_WORLD_RESPONSE HELLO_WORLD_RESPONSE)); closesocket(sock); WEB_STOP(); }
TEST(http, pipeline_request_while_two_read) { WEB_DECL(); uv_os_sock_t sock; char buf[2048]; size_t s; WEB_INIT(); WEB_START(); sock = connect_tcp("127.0.0.1", TEST_PORT); ASSERT_EQ(true, send_n(sock, pipeline_requests, strlen(pipeline_requests)-12)); ASSERT_EQ(true, send_n(sock, pipeline_requests + (strlen(pipeline_requests)-12), 12)); s = max_recv(sock, buf, 2048, RECV_TIMEOUT); ASSERT_EQ( 0, strncmp(buf, hello_world_response, strlen(hello_world_response))); ASSERT_EQ( 0, strncmp(buf + strlen(hello_world_response), hello_world_response, strlen(hello_world_response))); closesocket(sock); WEB_STOP(); }
TEST(http, send_error_request) { WEB_DECL(); uv_os_sock_t sock; char buf[2048]; size_t s; WEB_INIT(); shttp_set_log_callback(&on_log, &log_buf); WEB_START(); sock = connect_tcp("127.0.0.1", TEST_PORT); ASSERT_EQ(true, send_n(sock, "1234567890" GET_REQUEST "123\r\n\r\n", strlen(GET_REQUEST) + 17)); s = max_recv(sock, buf, 2048, RECV_TIMEOUT); ASSERT_EQ( s, 0); closesocket(sock); log_buf.str[log_buf.len] = 0; ASSERT_STREQ("parse error:HPE_INVALID_METHOD", log_buf.str); WEB_STOP(); }
TEST(http, async_end_not_call_with_not_thunked) { WEB_DECL(); uv_os_sock_t sock; char buf[2048]; size_t s; WEB_INIT(); shttp_set_log_callback(&on_log, &log_buf); settings.callbacks.on_message_complete = &on_message_complete_async_not_thunked; WEB_START(); sock = connect_tcp("127.0.0.1", TEST_PORT); ASSERT_EQ(true, send_n(sock, simple_request, strlen(simple_request))); s = max_recv(sock, buf, 2048, RECV_TIMEOUT); //ASSERT_EQ(s, strlen(BODY_NOT_COMPLETE)); //ASSERT_EQ(0, strcmp(buf, BODY_NOT_COMPLETE)); closesocket(sock); WEB_STOP(); log_buf.str[log_buf.len] = 0; ASSERT_STREQ("callback: chunked must is true while body is not completed.", log_buf.str); }
char *ident_username(rfbClientPtr client) { ClientData *cd = (ClientData *) client->clientData; char *str, *newhost, *user = NULL, *newuser = NULL; int len; if (cd) { user = cd->username; } if (!user || *user == '\0') { int n, sock, ok = 0; int block = 0; int refused = 0; /* * need to check to see if the operation will block for * a long time: a firewall may just ignore our packets. */ #if LIBVNCSERVER_HAVE_FORK { pid_t pid, pidw; int rc; if ((pid = fork()) > 0) { usleep(100 * 1000); /* 0.1 sec for quick success or refusal */ pidw = waitpid(pid, &rc, WNOHANG); if (pidw <= 0) { usleep(1500 * 1000); /* 1.5 sec */ pidw = waitpid(pid, &rc, WNOHANG); if (pidw <= 0) { int rc2; rfbLog("ident_username: set block=1 (hung)\n"); block = 1; kill(pid, SIGTERM); usleep(100 * 1000); waitpid(pid, &rc2, WNOHANG); } } if (pidw > 0 && !block) { if (WIFEXITED(rc) && WEXITSTATUS(rc) == 1) { rfbLog("ident_username: set refused=1 (exit)\n"); refused = 1; } } } else if (pid == -1) { ; } else { /* child */ signal(SIGHUP, SIG_DFL); signal(SIGINT, SIG_DFL); signal(SIGQUIT, SIG_DFL); signal(SIGTERM, SIG_DFL); if ((sock = connect_tcp(client->host, 113)) < 0) { exit(1); } else { close(sock); exit(0); } } } #endif if (block || refused) { ; } else if ((sock = connect_tcp(client->host, 113)) < 0) { rfbLog("ident_username: could not connect to ident: %s:%d\n", client->host, 113); } else { char msg[128]; int ret; fd_set rfds; struct timeval tv; int rport = get_remote_port(client->sock); int lport = get_local_port(client->sock); sprintf(msg, "%d, %d\r\n", rport, lport); n = write(sock, msg, strlen(msg)); FD_ZERO(&rfds); FD_SET(sock, &rfds); tv.tv_sec = 3; tv.tv_usec = 0; ret = select(sock+1, &rfds, NULL, NULL, &tv); if (ret > 0) { int i; char *q, *p; for (i=0; i < (int) sizeof(msg); i++) { msg[i] = '\0'; } usleep(250*1000); n = read(sock, msg, 127); close(sock); if (n <= 0) goto badreply; /* 32782 , 6000 : USERID : UNIX :runge */ q = strstr(msg, "USERID"); if (!q) goto badreply; q = strstr(q, ":"); if (!q) goto badreply; q++; q = strstr(q, ":"); if (!q) goto badreply; q++; q = lblanks(q); p = q; while (*p) { if (*p == '\r' || *p == '\n') { *p = '\0'; } p++; } ok = 1; if (strlen(q) > 24) { *(q+24) = '\0'; } newuser = strdup(q); badreply: n = 0; /* avoid syntax error */ } else { close(sock); } } if (! ok || !newuser) { newuser = strdup("unknown-user"); } if (cd) { if (cd->username) { free(cd->username); } cd->username = newuser; } user = newuser; } if (!strcmp(user, "unknown-user") && cd && cd->unixname[0] != '\0') { user = cd->unixname; } if (unixpw && openssl_last_ip && strstr("UNIX:", user) != user) { newhost = ip2host(openssl_last_ip); } else { newhost = ip2host(client->host); } len = strlen(user) + 1 + strlen(newhost) + 1; str = (char *) malloc(len); sprintf(str, "%s@%s", user, newhost); free(newhost); return str; }
int main(int argc, char *argv[]) { const char *connect_address = NULL; const char *server_address = NULL; const char *unix_path = NULL; unsigned short tcp_port = 0xb1ee; /* 45550 */ const char *str; sigset_t mask; for (;;) { int opt; opt = getopt_long(argc, argv, "c:l::u::p:i:dvh", main_options, NULL); if (opt < 0) break; switch (opt) { case 'c': connect_address = optarg; break; case 'l': if (optarg) server_address = optarg; else server_address = "0.0.0.0"; break; case 'u': if (optarg) unix_path = optarg; else unix_path = "/tmp/bt-server-bredr"; break; case 'p': tcp_port = atoi(optarg); break; case 'i': if (strlen(optarg) > 3 && !strncmp(optarg, "hci", 3)) str = optarg + 3; else str = optarg; if (!isdigit(*str)) { usage(); return EXIT_FAILURE; } hci_index = atoi(str); break; case 'd': debug_enabled = true; break; case 'v': printf("%s\n", VERSION); return EXIT_SUCCESS; case 'h': usage(); return EXIT_SUCCESS; default: return EXIT_FAILURE; } } if (argc - optind > 0) { fprintf(stderr, "Invalid command line parameters\n"); return EXIT_FAILURE; } if (unix_path && server_address) { fprintf(stderr, "Invalid to specify TCP and Unix servers\n"); return EXIT_FAILURE; } if (connect_address && (unix_path || server_address)) { fprintf(stderr, "Invalid to specify client and server mode\n"); return EXIT_FAILURE; } mainloop_init(); sigemptyset(&mask); sigaddset(&mask, SIGINT); sigaddset(&mask, SIGTERM); mainloop_set_signal(&mask, signal_callback, NULL, NULL); if (connect_address) { int host_fd, dev_fd; printf("Connecting to %s:%u\n", connect_address, tcp_port); dev_fd = connect_tcp(connect_address, tcp_port); if (dev_fd < 0) return EXIT_FAILURE; printf("Opening virtual device\n"); host_fd = open_vhci(0x00); if (host_fd < 0) { close(dev_fd); return EXIT_FAILURE; } if (!setup_proxy(host_fd, false, dev_fd, true)) { close(dev_fd); close(host_fd); return EXIT_FAILURE; } } else { int server_fd; if (unix_path) { printf("Listening on %s\n", unix_path); server_fd = open_unix(unix_path); } else if (server_address) { printf("Listening on %s:%u\n", server_address, tcp_port); server_fd = open_tcp(server_address, tcp_port); } else { fprintf(stderr, "Missing emulator device\n"); return EXIT_FAILURE; } if (server_fd < 0) return EXIT_FAILURE; mainloop_add_fd(server_fd, EPOLLIN, server_callback, NULL, NULL); } return mainloop_run(); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(mqtt_process, ev, data) { static struct mqtt_connection *conn; PROCESS_BEGIN(); while(1) { PROCESS_WAIT_EVENT(); if(ev == mqtt_abort_now_event) { DBG("MQTT - Abort\n"); conn = data; conn->state = MQTT_CONN_STATE_ABORT_IMMEDIATE; abort_connection(conn); } if(ev == mqtt_do_connect_tcp_event) { conn = data; DBG("MQTT - Got mqtt_do_connect_tcp_event!\n"); connect_tcp(conn); } if(ev == mqtt_do_connect_mqtt_event) { conn = data; conn->socket.output_data_max_seg = conn->max_segment_size; DBG("MQTT - Got mqtt_do_connect_mqtt_event!\n"); if(conn->out_buffer_sent == 1) { PT_INIT(&conn->out_proto_thread); while(connect_pt(&conn->out_proto_thread, conn) < PT_EXITED && conn->state != MQTT_CONN_STATE_ABORT_IMMEDIATE) { PT_MQTT_WAIT_SEND(); } } } if(ev == mqtt_do_disconnect_mqtt_event) { conn = data; DBG("MQTT - Got mqtt_do_disconnect_mqtt_event!\n"); /* Send MQTT Disconnect if we are connected */ if(conn->state == MQTT_CONN_STATE_SENDING_MQTT_DISCONNECT) { if(conn->out_buffer_sent == 1) { PT_INIT(&conn->out_proto_thread); while(disconnect_pt(&conn->out_proto_thread, conn) < PT_EXITED && conn->state != MQTT_CONN_STATE_ABORT_IMMEDIATE) { PT_MQTT_WAIT_SEND(); } abort_connection(conn); call_event(conn, MQTT_EVENT_DISCONNECTED, &ev); } else { process_post(&mqtt_process, mqtt_do_disconnect_mqtt_event, conn); } } } if(ev == mqtt_do_pingreq_event) { conn = data; DBG("MQTT - Got mqtt_do_pingreq_event!\n"); if(conn->out_buffer_sent == 1 && conn->state == MQTT_CONN_STATE_CONNECTED_TO_BROKER) { PT_INIT(&conn->out_proto_thread); while(pingreq_pt(&conn->out_proto_thread, conn) < PT_EXITED && conn->state == MQTT_CONN_STATE_CONNECTED_TO_BROKER) { PT_MQTT_WAIT_SEND(); } } } if(ev == mqtt_do_subscribe_event) { conn = data; DBG("MQTT - Got mqtt_do_subscribe_mqtt_event!\n"); if(conn->out_buffer_sent == 1 && conn->state == MQTT_CONN_STATE_CONNECTED_TO_BROKER) { PT_INIT(&conn->out_proto_thread); while(subscribe_pt(&conn->out_proto_thread, conn) < PT_EXITED && conn->state == MQTT_CONN_STATE_CONNECTED_TO_BROKER) { PT_MQTT_WAIT_SEND(); } } } if(ev == mqtt_do_unsubscribe_event) { conn = data; DBG("MQTT - Got mqtt_do_unsubscribe_mqtt_event!\n"); if(conn->out_buffer_sent == 1 && conn->state == MQTT_CONN_STATE_CONNECTED_TO_BROKER) { PT_INIT(&conn->out_proto_thread); while(unsubscribe_pt(&conn->out_proto_thread, conn) < PT_EXITED && conn->state == MQTT_CONN_STATE_CONNECTED_TO_BROKER) { PT_MQTT_WAIT_SEND(); } } } if(ev == mqtt_do_publish_event) { conn = data; DBG("MQTT - Got mqtt_do_publish_mqtt_event!\n"); if(conn->out_buffer_sent == 1 && conn->state == MQTT_CONN_STATE_CONNECTED_TO_BROKER) { PT_INIT(&conn->out_proto_thread); while(publish_pt(&conn->out_proto_thread, conn) < PT_EXITED && conn->state == MQTT_CONN_STATE_CONNECTED_TO_BROKER) { PT_MQTT_WAIT_SEND(); } } } } PROCESS_END(); }
int CodeRevision::sendtcp( int replace ) { string blob = ""; for ( int i = 0 ; i < _buffer->nlines() ; i++ ) { blob += _buffer->line(i).str(); if ( i != _buffer->nlines() - 1 ) blob += "\n"; } connect_tcp(); if ( _connected ) { //DO THAT TCP MAGIC THING Net_Msg msg; if ( replace == 0 ) { msg.type = MSG_ADD; msg.param = 1; } else { msg.type = MSG_REPLACE; msg.param = replace;; } strcpy ( (char*)msg.buffer, "editor->" ); strcat ( (char*)msg.buffer, _buffer->filename().c_str() ); msg.param2 = (t_CKUINT) blob.size(); //trailing NUL? msg.length = 0; otf_hton( &msg ); ck_send ( _tcp , (char *)&msg, sizeof(msg) ); t_CKUINT left = blob.size() ; //include trailing NUL ? t_CKUINT done = 0; while ( left ) { msg.length = ( left > NET_BUFFER_SIZE ) ? NET_BUFFER_SIZE : left; msg.param3 = msg.length; memcpy( (void*)msg.buffer, (void*) (blob.c_str() + done ), msg.length * sizeof (char) ); left -= msg.length; done += msg.length; msg.param2 = left; fprintf(stderr, "sending %ld to %ld of %s\n", done - msg.length, done, _buffer->filename().c_str()); otf_hton( &msg ); ck_send ( _tcp, (char*)&msg, sizeof(msg) ); } msg.type = MSG_DONE; otf_hton( &msg ); ck_send (_tcp , (char*)&msg, sizeof(msg)); Net_Msg ret; if ( ck_recv( _tcp , (char *)&ret, sizeof(ret) ) ) { fprintf( stderr, "[chuck(remote)]: operation %s\n", ( ret.param ? "successful" : "failed (sorry)" ) ); } close_tcp(); return ( ret.param ) ? 1 : 0 ; } else { fprintf(stderr,"sendtcp::unable to establish connection to ChucK process...\n"); } return 0; }
void *handle(void *arg) { pthread_detach(pthread_self()); int client_sock = *(int*)arg; int server_sock; int return_value; char host_name[128]; char buffer[BUFSIZ]; char server_ip[INET_ADDRSTRLEN]; free(arg); return_value = recv(client_sock, buffer, BUFSIZ, 0); if (return_value <= 0) { close(client_sock); fputs("recv 0 Byte from client...\n", stderr); pthread_exit(NULL); } printf("%s", buffer); if (!host_filter(buffer, host_name)) { fputs("can't got host name from client!\n", stderr); close(client_sock); pthread_exit(NULL); } if (!ns_look_up(host_name, server_ip)) { fputs("DNS Error!\n", stderr); close(client_sock); pthread_exit(NULL); } if ((server_sock = connect_tcp(server_ip, 80)) < 0) { fputs("connect server Error!\n", stderr); close(client_sock); pthread_exit(NULL); } else { return_value = send(server_sock, buffer, return_value, 0); if (return_value < 0) { perror("send"); } } while (1) { fd_set sock_set; FD_ZERO(&sock_set); FD_SET(client_sock, &sock_set); FD_SET(server_sock, &sock_set); int count; int return_value; int max_fd_value = client_sock > server_sock?client_sock:server_sock; if ((return_value = select(max_fd_value + 1, &sock_set, NULL, NULL, NULL)) < 0) { perror("select"); continue; } printf("select finished!"); if (FD_ISSET(server_sock, &sock_set)) { while ((count = read(server_sock, buffer, BUFSIZ))) { write(client_sock, buffer, count); if (count < BUFSIZ) break; else if (count < 0) { perror("server_sock write error!"); break; } } } if (FD_ISSET(client_sock, &sock_set)) { while ((count = read(client_sock, buffer, BUFSIZ))) { write(server_sock, buffer, count); if (count < BUFSIZ) break; else if (count < 0) { perror("client_sock write error!"); break; } } } } return NULL; }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(mqtt_process, ev, data) { static struct mqtt_connection* conn; static uip_ipaddr_t* ipaddr = NULL; PROCESS_BEGIN(); while(1) { PROCESS_WAIT_EVENT(); if(ev == mqtt_do_connect_tcp_event) { conn = data; DBG("MQTT - Got mqtt_do_connect_tcp_event!\r\n"); connect_tcp(conn); } if(ev == mqtt_do_connect_mqtt_event) { conn = data; DBG("MQTT - Got mqtt_do_connect_mqtt_event!\r\n"); PT_INIT(&conn->out_proto_thread); while(connect_pt(&conn->out_proto_thread, conn) != PT_ENDED) { PT_MQTT_WAIT_SEND(); } } if(ev == mqtt_do_disconnect_mqtt_event) { conn = data; DBG("MQTT - Got mqtt_do_disconnect_mqtt_event!\r\n"); PT_INIT(&conn->out_proto_thread); while(disconnect_pt(&conn->out_proto_thread, conn) != PT_ENDED) { PT_MQTT_WAIT_SEND(); } } if(ev == mqtt_do_pingreq_event) { conn = data; DBG("MQTT - Got mqtt_do_pingreq_event!\r\n"); PT_INIT(&conn->out_proto_thread); while(pingreq_pt(&conn->out_proto_thread, conn) != PT_ENDED) { PT_MQTT_WAIT_SEND(); } } if(ev == mqtt_do_subscribe_event) { conn = data; DBG("MQTT - Got mqtt_do_subscribe_mqtt_event!\r\n"); PT_INIT(&conn->out_proto_thread); while(subscribe_pt(&conn->out_proto_thread, conn) != PT_ENDED) { PT_MQTT_WAIT_SEND(); } } if(ev == mqtt_do_unsubscribe_event) { conn = data; DBG("MQTT - Got mqtt_do_unsubscribe_mqtt_event!\r\n"); PT_INIT(&conn->out_proto_thread); while(unsubscribe_pt(&conn->out_proto_thread, conn) != PT_ENDED) { PT_MQTT_WAIT_SEND(); } } if(ev == mqtt_do_publish_event) { conn = data; DBG("MQTT - Got mqtt_do_publish_mqtt_event!\r\n"); PT_INIT(&conn->out_proto_thread); while(publish_pt(&conn->out_proto_thread, conn) != PT_ENDED) { PT_MQTT_WAIT_SEND(); } } #if 0 if(ev == mqtt_do_publish_event) { } #endif /* mDNS event - either found a hostname, or not. */ if(ev == mdns_event_found) { if((char *)data != NULL && mdns_lookup((char *)data) != NULL) { ipaddr = mdns_lookup((char *)data); DBG("MQTT - Host found with ip %i %i %i %i TCP connecting...\r\n", ipaddr->u8[12], ipaddr->u8[13], ipaddr->u8[14], ipaddr->u8[15]); } else { printf("MQTT - Host not found, cannot continue.\r\n"); } /* Find the connection(s) that are waiting for this lookup. Note that it * could possibly be more then one connection. */ for(conn = list_head(mqtt_conn_list); conn != NULL; conn = list_item_next(conn)) { if( conn->state == MQTT_CONN_STATE_DNS_LOOKUP && strcmp( (char*)data, conn->server_host ) == 0 ) { /* Update the connection of the DNS error or connect, depending on the * DNS lookup result. */ if( ipaddr == NULL ) { conn->state = MQTT_CONN_STATE_DNS_ERROR; call_event(conn, MQTT_EVENT_DNS_ERROR, NULL); } else { uip_ipaddr_copy( &(conn->server_ip), ipaddr ); process_post(&mqtt_process, mqtt_do_connect_tcp_event, conn); } } } } } PROCESS_END(); }
static int do_fence_request_tcp(fence_req_t *req, mcast_info *info) { char ip_addr_src[1024]; int fd = -1; char response = 1; struct mcast_hostlist_arg arg; fd = connect_tcp(req, info->args.auth, info->key, info->key_len); if (fd < 0) { dbg_printf(2, "Could not send reply to fence request: %s\n", strerror(errno)); goto out; } inet_ntop(req->family, req->address, ip_addr_src, sizeof(ip_addr_src)); dbg_printf(2, "Request %d seqno %d src %s target %s\n", req->request, req->seqno, ip_addr_src, req->domain); switch(req->request) { case FENCE_NULL: response = info->cb->null((char *)req->domain, info->priv); break; case FENCE_ON: if (map_check(info->map, ip_addr_src, (const char *)req->domain) == 0) { response = RESP_PERM; break; } response = info->cb->on((char *)req->domain, ip_addr_src, req->seqno, info->priv); break; case FENCE_OFF: if (map_check(info->map, ip_addr_src, (const char *)req->domain) == 0) { response = RESP_PERM; break; } response = info->cb->off((char *)req->domain, ip_addr_src, req->seqno, info->priv); break; case FENCE_REBOOT: if (map_check(info->map, ip_addr_src, (const char *)req->domain) == 0) { response = RESP_PERM; break; } response = info->cb->reboot((char *)req->domain, ip_addr_src, req->seqno, info->priv); break; case FENCE_STATUS: if (map_check(info->map, ip_addr_src, (const char *)req->domain) == 0) { response = RESP_PERM; break; } response = info->cb->status((char *)req->domain, info->priv); break; case FENCE_DEVSTATUS: response = info->cb->devstatus(info->priv); break; case FENCE_HOSTLIST: arg.map = info->map; arg.src = ip_addr_src; arg.fd = fd; mcast_hostlist_begin(arg.fd); response = info->cb->hostlist(mcast_hostlist, &arg, info->priv); mcast_hostlist_end(arg.fd); break; } dbg_printf(3, "Sending response to caller...\n"); if (write(fd, &response, 1) < 0) { perror("write"); } /* XVM shotguns multicast packets, so we want to avoid * acting on the same request multiple times if the first * attempt was successful. */ history_record(info->history, req); out: if (fd != -1) close(fd); return 1; }
int main(int argc, char *argv[]) { if (argc < 4) usage(); char buf[256]; int ret; char server_hostname[NI_MAXHOST]; int server_port; int client_sock; Sum_State_T sum_state; if ( (sum_state = malloc(sizeof(*sum_state))) == NULL) { fprintf(stderr, "ERROR allocating Sum_State_T: %s\n", strerror(errno)); exit(1); } sum_state->idx = 0; /* first operand begins at argv[2] */ sum_state->max_idx = argc - 3; /* do we have too many operands? */ if (sum_state->max_idx >= MAX_OPERANDS) usage(); /* Convert operand arguments to numbers */ const char *errstr; int i; for (i = 0; i <= sum_state->max_idx; i++) { sum_state->operands[i] = strtonum(argv[i+2], LLONG_MIN, LLONG_MAX, &errstr); if (errstr != NULL) usage(); } /* split IP from port number */ if (parse_host_port(argv[1], server_hostname, NI_MAXHOST, &server_port) != 0) usage(); /* Windows is dumb */ #ifdef WIN32 WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { fprintf(stderr, "WSAStartup() failed.\n"); exit(1); } #endif fprintf(stderr, "Connecting to server..."); /* Connect to server */ if ((client_sock = connect_tcp(server_hostname, server_port, &errstr)) == -1) { fprintf(stderr, "Failed to connect: %s\n", errstr); return 1; } fprintf(stderr, "Done.\n"); /* Done connecting... set up AMP Protocol */ AMP_Proto_T *proto; if ( (proto = amp_new_proto()) == NULL) { fprintf(stderr, "Couldn't allocate AMP_Proto.\n"); return 1; }; amp_set_write_handler(proto, do_write, &client_sock); /* Set up libamp logging to stderr for any error messages that * it wishes to report */ amp_set_log_handler(amp_stderr_logger); /* Make first call... */ do_sum_call(proto, sum_state); /* Start reading loop */ int bytesRead; while ( (bytesRead = recv(client_sock, buf, sizeof(buf), 0)) >= 0) { if ( (ret = amp_consume_bytes(proto, buf, bytesRead)) != 0) { fprintf(stderr, "ERROR detected by amp_consume_bytes(): %s\n", amp_strerror(ret)); return 1; }; } return 0; }
void boot_client(char *str) { init_tcp_cht(NULL); vtimer_usleep(1000*1000*2); connect_tcp("connect_tcp 2"); }
int main(int argc, char * argv[]) #endif { bool threaded = false; ETERM *cmd_tuple; #ifdef __WIN32__ _setmode( _fileno( stdout ), _O_BINARY ); _setmode( _fileno( stdin ), _O_BINARY ); #endif erl_init(NULL, 0); log_flag = false; if (argc >= 2) { if ( #ifdef __WIN32__ wcscmp(argv[1], L"true") == 0 #else strcmp(argv[1], "true") == 0 #endif ) log_flag = true; } if (argc >= 3) { int ListenPortNo = #ifdef __WIN32__ _wtoi(argv[2]); #else atoi(argv[2]); #endif char * ret = connect_tcp(ListenPortNo); if(ret != NULL) { return -1; } else REMOTE_LOG("Logging over TCP, Voila!\n"); } init_marshall(); REMOTE_LOG("Port: OCI Process started...\n"); threaded = InitializeThreadPool(); if(threaded) REMOTE_LOG("Port: Thread pool created...\n"); REMOTE_LOG("Port: Initialized Oracle OCI\n"); while(!exit_loop && (cmd_tuple = (ETERM *)read_cmd()) != NULL) { if(threaded && ProcessCommand(cmd_tuple)) { //REMOTE_LOG("Port: Command sumitted to thread-pool for processing..."); } } REMOTE_LOG("Port: Process oci terminating...\n"); close_tcp(); REMOTE_LOG("Port: Thread pool destroyed...\n"); CleanupThreadPool(); return 0; }