static void connect_device(const std::string& address, std::string* response) { if (address.empty()) { *response = "empty address"; return; } std::string serial; std::string host; int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT; if (!parse_host_and_port(address, &serial, &host, &port, response)) { return; } std::string error; int fd = network_connect(host.c_str(), port, SOCK_STREAM, 10, &error); if (fd == -1) { *response = android::base::StringPrintf("unable to connect to %s: %s", serial.c_str(), error.c_str()); return; } D("client: connected %s remote on fd %d", serial.c_str(), fd); close_on_exec(fd); disable_tcp_nagle(fd); int ret = register_socket_transport(fd, serial.c_str(), port, 0); if (ret < 0) { adb_close(fd); *response = android::base::StringPrintf("already connected to %s", serial.c_str()); } else { *response = android::base::StringPrintf("connected to %s", serial.c_str()); } }
static int scpi_connect(struct scpi_instrument *scpi) { int ret; if(scpi->network) { ret = network_connect(scpi); if (ret != 0) return ret; if (scpi->control_socket != scpi->main_socket) { scpi_fprintf(scpi, "DCL\n"); if (strlen(scpi->response)) { if (!strcmp(scpi->response, "DCL\n")) printf("Warning : %s DCL response: %s\n", __func__, scpi->response); } } } else if (scpi->serial) { tty_connect(scpi); } else { printf("misconfigured SCPI data structure\n"); return -1; } scpi_fprintf(scpi, "*CLS\n"); scpi_fprintf(scpi, "*RST\n"); scpi_fprintf(scpi, "*IDN?\n"); if (!strstr(scpi->response, scpi->id_regex)) { printf("instrument doesn't match regex\n"); printf("\twanted : '%s'\n", scpi->id_regex); printf("\trecieved : '%s'\n", scpi->response); return -1; } printf("Instrument ID: %s\n", scpi->response); return 0; }
void Nxt_network::connect(unsigned int port, string ip_add, Server_settings &settings, string password){ unsigned char buffer[MAX_PASSWORD_LENGTH+1]; unsigned short int pass_len, i; unsigned char answer[5]; struct timeval timeout; timeout.tv_sec = TIMEOUT; timeout.tv_usec = 0; ws_ver=MAKEWORD(2, 0); // winsock 2.0 ws_status=WSAStartup(ws_ver, &ws_data); if(ws_status != 0){ string s; s = this->error_to_string(WSAGetLastError()); throw Nxt_exception::Nxt_exception("connect", "Nxt_network", NETWORK_COM_ERROR, s); } this->my_sock=socket(AF_INET, SOCK_STREAM, 0); if(my_sock == INVALID_SOCKET){ string s; s = this->error_to_string(WSAGetLastError()); throw Nxt_exception::Nxt_exception("connect", "Nxt_network", NETWORK_COM_ERROR, s); } sockaddr_in sock_in; sock_in.sin_port=htons(port); sock_in.sin_addr.s_addr=inet_addr(ip_add.c_str()); sock_in.sin_family=AF_INET; if(setsockopt(my_sock, SOL_SOCKET, SO_RCVTIMEO, (const char *)&timeout, sizeof(timeout)) == -1){ string s; s = this->error_to_string(WSAGetLastError()); throw Nxt_exception::Nxt_exception("connect", "Nxt_network", NETWORK_COM_ERROR, s); } if(network_connect(&this->my_sock,&sock_in)){ string s; s = this->error_to_string(WSAGetLastError()); throw Nxt_exception::Nxt_exception("send", "Nxt_network", NETWORK_COM_ERROR, s); } if(password.empty()){ password=DEFALT_PASSWORD; } pass_len = password.length(); if(pass_len > MAX_PASSWORD_LENGTH){ pass_len = MAX_PASSWORD_LENGTH; } i=0; while(i <pass_len){ buffer[i] = password[i]; i++; } while(i < MAX_PASSWORD_LENGTH+1){ buffer[i] = '\0'; i++; } send(buffer,MAX_PASSWORD_LENGTH+1); receive(answer, 9); if(answer[4]){ throw Nxt_exception::Nxt_exception("connect", "Nxt_network", answer[4] & 0xff); } settings.mode = (Server_mode) answer[3]; settings.timeout = (0xff & answer[5]) | ((0xff & answer[6]) << 8)| ((0xff & answer[7]) << 16)| ((0xff & answer[8]) << 24); return; }
struct rtable_t *rtable_bind(const char *address_port){ struct rtable_t *rtable = (struct rtable_t*)malloc(sizeof(struct rtable_t)); rtable->msg = (struct message_t*)malloc(sizeof(struct message_t)); rtable->server = (struct server_t*)malloc(sizeof(struct server_t)); rtable->server = network_connect(address_port); return rtable; }
int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error) { int fd = -1; #if ADB_HOST if (find_emulator_transport_by_adb_port(adb_port) != nullptr) { return -1; } const char *host = getenv("ADBHOST"); if (host) { fd = network_connect(host, adb_port, SOCK_STREAM, 0, error); } #endif if (fd < 0) { fd = network_loopback_client(adb_port, SOCK_STREAM, error); } if (fd >= 0) { D("client: connected on remote on fd %d", fd); close_on_exec(fd); disable_tcp_nagle(fd); std::string serial = android::base::StringPrintf("emulator-%d", console_port); if (register_socket_transport(fd, serial.c_str(), adb_port, 1) == 0) { return 0; } adb_close(fd); } return -1; }
/* * Given the remctl object (for error reporting), host, and port, attempt a * network connection. Returns the file descriptor if successful or * INVALID_SOCKET on failure. */ static socket_type internal_connect(struct remctl *r, const char *host, unsigned short port) { struct addrinfo hints, *ai; char portbuf[16]; int status; socket_type fd; /* * Look up the remote host and open a TCP connection. Call getaddrinfo * and network_connect instead of network_connect_host so that we can * report the complete error on host resolution. */ memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; snprintf(portbuf, sizeof(portbuf), "%hu", port); status = getaddrinfo(host, portbuf, &hints, &ai); if (status != 0) { internal_set_error(r, "unknown host %s: %s", host, gai_strerror(status)); return INVALID_SOCKET; } fd = network_connect(ai, r->source, r->timeout); freeaddrinfo(ai); if (fd == INVALID_SOCKET) { internal_set_error(r, "cannot connect to %s (port %hu): %s", host, port, socket_strerror(socket_errno)); return INVALID_SOCKET; } return fd; }
void app_send(struct item *key, struct item *message) /*@ requires [?f0]world(ss_pub) &*& item(key, symmetric_key_item(?creator, ?id), ss_pub) &*& item(message, ?msg, ss_pub) &*& [_]ss_pub(msg) &*& app_send_event(creator, msg) == true; @*/ /*@ ensures [f0]world(ss_pub) &*& item(key, symmetric_key_item(creator, id), ss_pub) &*& item(message, msg, ss_pub); @*/ { struct network_status *net_stat = network_connect("localhost", APP_RECEIVE_PORT); struct item *hash = create_hmac(key, message); //@ assert item(hash, ?h, ss_pub); //@ get_info_for_item(h); //@ close ss_pub(h); //@ leak ss_pub(h); struct item *m = create_pair(hash, message); //@ assert item(m, ?pmessage, ss_pub); //@ get_info_for_item(pmessage); //@ close ss_pub(pmessage); //@ leak ss_pub(pmessage); network_send(net_stat, m); item_free(hash); item_free(m); network_disconnect(net_stat); }
int network_connect_to (const char *proxy, int sock, unsigned long address, int port) { struct t_proxy *ptr_proxy; struct sockaddr_in addr; struct hostent *hostent; char *ip4; ptr_proxy = NULL; if (proxy && proxy[0]) { ptr_proxy = proxy_search (proxy); if (!ptr_proxy) return 0; } if (ptr_proxy) { memset (&addr, 0, sizeof (addr)); addr.sin_addr.s_addr = htonl (address); ip4 = inet_ntoa(addr.sin_addr); memset (&addr, 0, sizeof (addr)); addr.sin_port = htons (CONFIG_INTEGER(ptr_proxy->options[PROXY_OPTION_PORT])); addr.sin_family = AF_INET; hostent = gethostbyname (CONFIG_STRING(ptr_proxy->options[PROXY_OPTION_ADDRESS])); if (!hostent) return 0; memcpy(&(addr.sin_addr), *(hostent->h_addr_list), sizeof(struct in_addr)); if (!network_connect (sock, (struct sockaddr *) &addr, sizeof (addr))) return 0; if (!network_pass_proxy (proxy, sock, ip4, port)) return 0; } else { memset (&addr, 0, sizeof (addr)); addr.sin_port = htons (port); addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl (address); if (!network_connect (sock, (struct sockaddr *) &addr, sizeof (addr))) return 0; } return 1; }
/** * proto_conn_create(s, sas, decr, nopfs, requirepfs, nokeepalive, K, timeo, * callback_dead, cookie): * Create a connection with one end at ${s} and the other end connecting to * the target addresses ${sas}. If ${decr} is 0, encrypt the outgoing data; * if ${decr} is nonzero, decrypt the incoming data. If ${nopfs} is non-zero, * don't use perfect forward secrecy. If ${requirepfs} is non-zero, drop * the connection if the other end tries to disable perfect forward secrecy. * Enable transport layer keep-alives (if applicable) on both sockets if and * only if ${nokeepalive} is zero. Drop the connection if the handshake or * connecting to the target takes more than ${timeo} seconds. When the * connection is dropped, invoke ${callback_dead}(${cookie}). Free ${sas} * once it is no longer needed. Return a cookie which can be passed to * proto_conn_drop. */ void * proto_conn_create(int s, struct sock_addr ** sas, int decr, int nopfs, int requirepfs, int nokeepalive, const struct proto_secret * K, double timeo, int (* callback_dead)(void *), void * cookie) { struct conn_state * C; /* Bake a cookie for this connection. */ if ((C = malloc(sizeof(struct conn_state))) == NULL) goto err0; C->callback_dead = callback_dead; C->cookie = cookie; C->sas = sas; C->decr = decr; C->nopfs = nopfs; C->requirepfs = requirepfs; C->nokeepalive = nokeepalive; C->K = K; C->timeo = timeo; C->s = s; C->t = -1; C->connect_cookie = NULL; C->connect_timeout_cookie = NULL; C->handshake_cookie = NULL; C->handshake_timeout_cookie = NULL; C->k_f = C->k_r = NULL; C->pipe_f = C->pipe_r = NULL; C->stat_f = C->stat_r = 1; /* Start the connect timer. */ if ((C->connect_timeout_cookie = events_timer_register_double( callback_connect_timeout, C, C->timeo)) == NULL) goto err1; /* Connect to target. */ if ((C->connect_cookie = network_connect(C->sas, callback_connect_done, C)) == NULL) goto err2; /* If we're decrypting, start the handshake. */ if (C->decr) { if (starthandshake(C, C->s, C->decr)) goto err3; } /* Success! */ return (C); err3: network_connect_cancel(C->connect_cookie); err2: events_timer_cancel(C->connect_timeout_cookie); err1: free(C); err0: /* Failure! */ return (NULL); }
int ps4link_connect(char *hostname) { // Listen datagram socket port for debugnet console. console_socket = network_listen(0x4712, SOCK_DGRAM); // Create the console thread. if (console_socket > 0) { pthread_create(&console_thread_id, NULL, ps4link_thread_console, (void *)&console_thread_id); } // Connect to the request port. request_socket = network_connect(hostname, 0x4711, SOCK_STREAM); // request_socket = network_listen(0x4711, SOCK_STREAM); // Create the request thread. while(request_socket<0) { request_socket = network_connect(hostname, 0x4711, SOCK_STREAM); sleep(1); debugNetPrintf(DEBUG,"waiting ps4...\n"); } if (request_socket > 0) { pthread_create(&request_thread_id, NULL, ps4link_thread_request, (void *)&request_thread_id); } // Connect to the command port future use to send commands to psp2 //command_socket = network_connect(hostname, 0x4712, SOCK_DGRAM); // Delay for a moment to let ps2link finish setup. #ifdef _WIN32 Sleep(1); #else sleep(1); #endif // End function. return 0; }
bool NetworkWorker::connect(const std::string& host, int port, const std::string& nick) { { Glib::Threads::Mutex::Lock lock(m_mutex); if (!m_network.disconnected || network_connect(&m_network, host.c_str(), port) == RET_FAILURE) return false; this->start(); } putOut("NICK %s\r\nUSER %s %s %s :myirc of Raphy & Bart\r\n", nick.c_str(), nick.c_str(), nick.c_str(), host.c_str()); return true; }
int _adb_connect(const std::string& service, std::string* error) { D("_adb_connect: %s", service.c_str()); if (service.empty() || service.size() > MAX_PAYLOAD_V1) { *error = android::base::StringPrintf("bad service name length (%zd)", service.size()); return -1; } int fd; std::string reason; if (__adb_server_name) { fd = network_connect(__adb_server_name, __adb_server_port, SOCK_STREAM, 0, &reason); if (fd == -1) { *error = android::base::StringPrintf("can't connect to %s:%d: %s", __adb_server_name, __adb_server_port, reason.c_str()); return -2; } } else { fd = network_loopback_client(__adb_server_port, SOCK_STREAM, &reason); if (fd == -1) { *error = android::base::StringPrintf("cannot connect to daemon: %s", reason.c_str()); return -2; } } if ((memcmp(&service[0],"host",4) != 0 || service == "host:reconnect") && switch_socket_transport(fd, error)) { return -1; } if (!SendProtocolString(fd, service)) { *error = perror_str("write failure during connection"); adb_close(fd); return -1; } if (service != "reconnect") { if (!adb_status(fd, error)) { adb_close(fd); return -1; } } D("_adb_connect: return fd %d", fd); return fd; }
int main() { int8_t buffer[MAX_PACKET]; pthread_t serial_monitor_thread; state_t *state = calloc(1, sizeof(state_t)); state->atmegafd = open("/dev/ttyATH0", O_RDWR | O_NOCTTY | O_NDELAY); pthread_mutex_init(&state->atmegafp_mutex, NULL); pthread_mutex_init(&state->clientfd_mutex, NULL); pthread_mutex_init(&state->sockfd_mutex, NULL); pthread_mutex_init(&state->last_utime_mutex, NULL); //pthread_create(&serial_monitor_thread, NULL, serial_monitor, state); while(1){ network_connect(state); while (1) { int len; if ((len = recv(state->sockfd, buffer, sizeof(buffer), 0)) <= 0) { if(debug){ perror("recv"); printf("[atherosd] Connection Broken\n"); } break; } pthread_mutex_lock(&state->last_utime_mutex); state->last_utime = utime_now(); pthread_mutex_unlock(&state->last_utime_mutex); process_message(state, buffer, len); } close(state->sockfd); close(state->clientfd); sleep(1); if(debug) printf("[atherosd] Restarting Connection\n"); } return 0; }
void send() //@ requires [?f0]world(dummy_pub, dummy_key_clsfy) &*& principal(?p, ?c); //@ ensures [f0]world(dummy_pub, dummy_key_clsfy) &*& principal(p, c); { struct network_status *net_stat = network_connect("localhost", APP_RECEIVE_PORT); char i; struct item *m = create_data_item_from_int(i); //@ assert item(m, ?data, dummy_pub); //@ close dummy_pub(data); //@ leak dummy_pub(data); network_send(net_stat, m); item_free(m); network_disconnect(net_stat); }
int main_forward() { int ret = 1; /* reuse the normal network setup */ if (network_init()) { /* Now accept from gdb */ if (network_accept()) { /* Now connect to remote deebe */ if (network_connect()) { while (true) { network_read(); if (network_in_buffer_total > 0) { _forward_packet(&network_out_buffer[0], &network_in_buffer[0], &network_out_buffer_total, &network_in_buffer_total); network_write_fwd(); } network_read_fwd(); if (network_in_buffer_total > 0) { _forward_packet(&network_out_buffer[0], &network_in_buffer[0], &network_out_buffer_total, &network_in_buffer_total); network_write(); } } } } network_cleanup(); } return ret; }
int main(int argc, char* argv[]) { //declare variables int ret; // poll client stuff int i, j, maxi, connfd, sockfd; int maxfd; struct sockaddr_in cliaddr; struct timeval clienttimeout; int nready; socklen_t clilen; // int client[MAX_CLIENTS]; fd_set master_set, readset, clientset; VERBOSE = 0; printf("ps4sh version %s\n",PS4SH_VERSION); //call read config if exist we need redefine syntax and variables read_config(); //if we call ps4sh with parameter it is the ps4 ip copy it to proper variable if (argc == 2) { strcpy(dst_ip, argv[1]); dst_ip[strlen(argv[1])] = '\0'; } clienttimeout.tv_sec = 0; clienttimeout.tv_usec = USEC; // create request socket connected to ps4link fio service printf("Connecting to fio ps4link ip %s ", dst_ip); request_socket = ps4link_fio_listener(dst_ip, SRV_PORT, 60); if (request_socket < 0) { printf(", failed\n"); return 1; } //udp socket to send commands to ps4 command_socket = network_connect(dst_ip, 0x4712, SOCK_DGRAM); if (command_socket < 0) { printf(", failed\n"); return 1; } // client stuff for(i = 0; i < MAX_CLIENTS; i++) { client[i] = -1; } FD_ZERO(&clientset); // end client stuff printf("\n"); //create console log udp socket bind to 0.0.0.0 console_socket = ps4link_log_listener(src_ip, LOG_PORT); if ( console_socket < 0 ) { perror(""); printf("Unable to start log service!\n"); //if i can't create local udp socket listening in LOG_PORT exit return 1; } //create ps4sh listener to let tools from third parties connect to ps4sh ps4sh_socket = ps4link_srv_setup(src_ip, SRV_PORT); if ( ps4sh_socket < 0 ) { perror(""); printf("Unable to start command server!\n"); //if i can't create local tcp socket listening in SRV_PORT exit return 1; } //populate set standard output, command listener connected to ps4, udp logs listener, local command listener FD_ZERO(&master_set); //standard output FD_SET(0, &master_set); //ps4link fio channel FD_SET(request_socket, &master_set); //udp log channel FD_SET(console_socket, &master_set); //ps4sh channel FD_SET(ps4sh_socket, &master_set); client[0] = 0; client[1] = request_socket; client[2] = console_socket; client[3] = ps4sh_socket; maxfd = ps4sh_socket; maxi = 3; //initilize readline initialize_readline(); debugNetPrintf(INFO,"Ready\n"); //mail loop while(doloop) { readset = master_set; ret = select(maxfd+1, &readset, NULL, NULL, NULL); if ( ret < 0 ) { if ( FD_ISSET(0, &readset) ) { continue; } debugNetPrintf(ERROR,"In select %s\n",strerror(errno)); break; } else if (ret == 0) { /* no file desc are ready, lets move on in life */ } else { for(i = 0; i <= maxi; i++) { if ( (sockfd = client[i]) < 0) { continue; } if ( !FD_ISSET(sockfd, &readset) ) { continue; } //if we have udp log messages from ps4 debugnet if ( sockfd == console_socket) { ps4sh_log_read(console_socket); } //if we have local standard messages else if (sockfd == 0) { rl_callback_read_char(); } //if we have messages related to ps4link fio service else if(sockfd == request_socket) { ps4sh_srv_read(request_socket); } //if we have pending connection related to ps4sh server listener else if (sockfd == ps4sh_socket) { clilen = sizeof(cliaddr); connfd = accept(ps4sh_socket, (struct sockaddr *)&cliaddr, &clilen); //search empty space for(j = 0; i<FD_SETSIZE; j++) { if(client[j] < 0) { client[j] = connfd; break; } } //populate in the set FD_SET(connfd, &master_set); //sanity checks if(connfd > maxfd) { maxfd = connfd; } if ( j > maxi ) { maxi = j; } if (--nready <= 0) { continue; } } else { if ( ps4sh_srv_read(sockfd) < 0 ) { close(sockfd); FD_CLR(sockfd, &master_set); client[i] = -1; maxi--; } } } } } rl_callback_handler_remove(); if ( (ret = network_disconnect(request_socket)) == -1 ) { debugNetPrintf(ERROR,"From request_socket network_disconect %s\n",strerror(errno)); } if ( (ret = network_disconnect(console_socket)) == -1 ) { debugNetPrintf(ERROR,"From console_socket network_disconect %s\n",strerror(errno)); } if ( log_to_file ) { if ((ret = close(log_f_fd)) == -1) debugNetPrintf(ERROR,"From file log closing %s\n",strerror(errno)); } if (strcmp(ps4sh_history, "") != 0 ) { if ( (ret = write_history(ps4sh_history)) != 0) debugNetPrintf(ERROR,"From ps4sh_history %s\n",strerror(errno)); } printf("\n"); return(0); }
/* Connect to a streaming */ void cb_play(duda_request_t *dr) { int s; int fd; int len; int size = 64; int chunk_len; int raw_size = 65536*4; char raw[raw_size]; char *h264_header; char *channel; char *file_sock; char *file_meta; char chunk[size]; struct stat st; const char *base_url = "/h264streamer/play/"; /* * Get channel name * */ s = (dr->sr->uri.len - strlen(base_url)); if (s < 1) { response->http_status(dr, 404); response->end(dr, NULL); } channel = monkey->mem_alloc(s + 1); strncpy(channel, dr->sr->uri.data + strlen(base_url), s); channel[s] = '\0'; /* file sock */ s = strlen(CH_ROOT) + strlen(CH_SOCK) + strlen(channel) + 1; file_sock = malloc(s); snprintf(file_sock, s, "%s%s%s", CH_ROOT, channel, CH_SOCK); /* file meta */ file_meta = malloc(s); snprintf(file_meta, s, "%s%s%s", CH_ROOT, channel, CH_META); /* validate meta data file */ if (stat(file_meta, &st) != 0) { response->http_status(dr, 400); response->printf(dr, "Invalid channel"); response->end(dr, NULL); } /* read meta header */ h264_header = malloc(st.st_size); fd = open(file_meta, O_RDONLY); read(fd, h264_header, st.st_size); close(fd); /* response headers */ response->http_status(dr, 200); response->http_content_length(dr, -1); response->http_header(dr, HTTP_CONTENT_TYPE_H264); response->http_header(dr, HTTP_CHUNKED_TE); response->send_headers(dr); /* meta */ len = snprintf(chunk, size, "%X\r\n", (int) st.st_size); response->print(dr, chunk, len); response->print(dr, h264_header, st.st_size); response->flush(dr); //send(dr->cs->socket, chunk, len, 0); //send(dr->cs->socket, h264_header, st.st_size, 0); /* Connect to the decoded h264 video stream */ int stream = -1; while (stream == -1) { stream = network_connect(file_sock); } printf("Connected to stream...\n"); while (1) { memset(raw, '\0', sizeof(raw)); len = recv(stream, raw, raw_size, 0); if (len == -1) { sleep(0.2); continue; } printf("recv=%i\n", len); chunk_len = snprintf(chunk, size, "%X\r\n", len); /* response->print(dr, chunk, chunk_len); response->print(dr, raw, len); response->flush(dr); */ int r; r = send(dr->cs->socket, chunk, chunk_len, 0); printf("print chunk ret = %i\n", r); if (r == -1) { perror("send"); } r = send(dr->cs->socket, raw, len, 0); printf("print raw ret = %i\n", r); if (r == -1) { perror("send"); } continue; r = response->print(dr, chunk, chunk_len); printf("print chunk ret = %i\n", r); r = response->print(dr, raw, len); printf("print chunk ret = %i\n", r); len = response->flush(dr); printf(" BYTES SENT: %i\n", len); } //response->end(dr, NULL); }
/* Handle an incoming connection. */ static int callback_gotconn(void * cookie, int s) { struct accept_state * A = cookie; struct conn_state * C; /* This accept is no longer in progress. */ A->accept_cookie = NULL; /* We have gained a connection. */ A->nconn += 1; /* Bake a cookie for this connection. */ if ((C = malloc(sizeof(struct conn_state))) == NULL) goto err1; C->A = A; C->s = s; C->t = -1; C->connect_cookie = NULL; C->handshake_cookie = NULL; C->connect_timeout_cookie = C->handshake_timeout_cookie = NULL; C->k_f = C->k_r = NULL; C->pipe_f = C->pipe_r = NULL; C->stat_f = C->stat_r = 1; /* Start the connect timer. */ if ((C->connect_timeout_cookie = events_timer_register_double( callback_connect_timeout, C, C->A->timeo)) == NULL) goto err2; /* Connect to target. */ if ((C->connect_cookie = network_connect(A->sas, callback_connect_done, C)) == NULL) goto err3; /* If we're decrypting, start the handshake. */ if (C->A->decr) { if (starthandshake(C, C->s, C->A->decr)) goto err4; } /* Accept another connection if we can. */ if (doaccept(A)) goto err0; /* Success! */ return (0); err4: network_connect_cancel(C->connect_cookie); err3: events_timer_cancel(C->connect_timeout_cookie); err2: free(C); err1: A->nconn -= 1; close(s); err0: /* Failure! */ return (-1); }
void symbolic_attacker(int attacker_id, struct keypair* keypair) /*@ requires [?f]world(?pub, ?key_clsfy) &*& true == bad(attacker_id) &*& principal(attacker_id, ?count) &*& keypair(keypair, attacker_id, ?id, ?info, pub); @*/ //@ ensures false; { //@ retreive_proof_obligations(); for (;;) /*@ invariant [f]world(pub, key_clsfy) &*& proof_obligations(pub) &*& principal(attacker_id, _) &*& keypair(keypair, attacker_id, id, info, pub); @*/ { struct network_status *net_stat = 0; int net_choise = random_int_(); int port = random_int_(); if (net_choise % 2 == 0) net_stat = network_bind_and_accept(port % 65536); else net_stat = network_connect("localhost", port % 65536); { int action = random_int_(); int *counter; switch (action % 13) { case 0: //@ open [f]world(pub, key_clsfy); //@ assert [_]is_key_classifier(_, pub, key_clsfy); //@ retreive_public_invariant_constraints(key_clsfy); //@ duplicate_lemma_function_pointer_chunk(key_classifier); /*@ { lemma void public_key_classifier(cryptogram key, int p, int c, bool symmetric) requires polarssl_proof_pred(pub, key_clsfy)() &*& [_]polarssl_pub(pub)(key) &*& symmetric ? key == cg_symmetric_key(p, c) : key == cg_private_key(p, c); ensures polarssl_proof_pred(pub, key_clsfy)() &*& col || true == key_clsfy(p, c, symmetric); { open [_]polarssl_pub(pub)(key); item k; if (symmetric) k = symmetric_key_item(p, c); else k = private_key_item(p, c); open polarssl_proof_pred(pub, key_clsfy)(); assert is_key_classifier(?proof, pub, key_clsfy); proof(k, p, c, symmetric); close polarssl_proof_pred(pub, key_clsfy)(); } produce_lemma_function_pointer_chunk(public_key_classifier) : public_key_classifier(polarssl_pub(pub), key_clsfy, polarssl_proof_pred(pub, key_clsfy)) (key__, p__, c__, sym__) { call(); } {duplicate_lemma_function_pointer_chunk(public_key_classifier);}; } @*/ //@ close polarssl_proof_pred(pub, key_clsfy)(); attacker(); //@ open polarssl_proof_pred(pub, key_clsfy)(); //@ close [f]world(pub, key_clsfy); //@ leak public_invariant_constraints(_, _); //@ leak is_public_key_classifier(_, _, _, _); //@ leak is_key_classifier(_, _, _); break; case 1: // Anyone can publish arbitrary data items... send_data(net_stat); break; case 2: // Anyone can create pairs of public items... send_pair_composed(net_stat); break; case 3: // Anyone can deconstruct a public pair... send_pair_decomposed(net_stat); break; case 4: // Bad principals can publish generated nonce items... send_nonce(net_stat); break; case 5: // Bad principals can increment public nonces... increment_and_send_nonce(net_stat); break; case 6: // Bad principals can leak their keys... send_keys(net_stat, keypair); break; case 7: // Anyone can hmac public payload with public key send_hmac(net_stat, keypair); break; case 8: // Anyone can symmteric encrypt public payload with public key send_symmetric_encrypted(net_stat, keypair); break; case 9: // Anyone can symmteric decrypt message with public key send_symmetric_decrypted(net_stat, keypair); break; case 10: // Anyone can asymmteric encrypt public payload with public key send_asymmetric_encrypted(net_stat, keypair); break; case 11: // Anyone can asymmteric decrypt message with public key send_asymmetric_decrypted(net_stat, keypair); break; case 12: // Anyone can asymmteric sign public payload with public key send_asymmetric_signature(net_stat, keypair); } } network_disconnect(net_stat); } //@ leak proof_obligations(pub); }
void network_connect_child (struct t_hook *hook_connect) { struct t_proxy *ptr_proxy; struct addrinfo hints, *res, *res_local, *ptr_res; char status_str[2], *ptr_address, *status_with_string; char ipv4_address[INET_ADDRSTRLEN + 1], ipv6_address[INET6_ADDRSTRLEN + 1]; char status_without_string[1 + 5 + 1]; const char *error; int rc, length, num_written; res = NULL; res_local = NULL; status_str[1] = '\0'; ptr_address = NULL; ptr_proxy = NULL; if (HOOK_CONNECT(hook_connect, proxy) && HOOK_CONNECT(hook_connect, proxy)[0]) { ptr_proxy = proxy_search (HOOK_CONNECT(hook_connect, proxy)); if (!ptr_proxy) { /* proxy not found */ snprintf (status_without_string, sizeof (status_without_string), "%c00000", '0' + WEECHAT_HOOK_CONNECT_PROXY_ERROR); num_written = write (HOOK_CONNECT(hook_connect, child_write), status_without_string, strlen (status_without_string)); (void) num_written; return; } } if (ptr_proxy) { /* get info about peer */ memset (&hints, 0, sizeof (hints)); hints.ai_family = (CONFIG_BOOLEAN(ptr_proxy->options[PROXY_OPTION_IPV6])) ? AF_INET6 : AF_INET; hints.ai_socktype = SOCK_STREAM; rc = getaddrinfo (CONFIG_STRING(ptr_proxy->options[PROXY_OPTION_ADDRESS]), NULL, &hints, &res); if (rc != 0) { /* address not found */ status_with_string = NULL; error = gai_strerror (rc); if (error) { length = 1 + 5 + strlen (error) + 1; status_with_string = malloc (length); if (status_with_string) { snprintf (status_with_string, length, "%c%05d%s", '0' + WEECHAT_HOOK_CONNECT_ADDRESS_NOT_FOUND, (int)strlen (error), error); } } if (status_with_string) { num_written = write (HOOK_CONNECT(hook_connect, child_write), status_with_string, strlen (status_with_string)); } else { snprintf (status_without_string, sizeof (status_without_string), "%c00000", '0' + WEECHAT_HOOK_CONNECT_ADDRESS_NOT_FOUND); num_written = write (HOOK_CONNECT(hook_connect, child_write), status_without_string, strlen (status_without_string)); } if (status_with_string) free (status_with_string); (void) num_written; return; } if (!res) { /* adddress not found */ snprintf (status_without_string, sizeof (status_without_string), "%c00000", '0' + WEECHAT_HOOK_CONNECT_ADDRESS_NOT_FOUND); num_written = write (HOOK_CONNECT(hook_connect, child_write), status_without_string, strlen (status_without_string)); (void) num_written; return; } if ((CONFIG_BOOLEAN(ptr_proxy->options[PROXY_OPTION_IPV6]) && (res->ai_family != AF_INET6)) || ((!CONFIG_BOOLEAN(ptr_proxy->options[PROXY_OPTION_IPV6]) && (res->ai_family != AF_INET)))) { /* IP address not found */ snprintf (status_without_string, sizeof (status_without_string), "%c00000", '0' + WEECHAT_HOOK_CONNECT_IP_ADDRESS_NOT_FOUND); num_written = write (HOOK_CONNECT(hook_connect, child_write), status_without_string, strlen (status_without_string)); (void) num_written; freeaddrinfo (res); return; } if (CONFIG_BOOLEAN(ptr_proxy->options[PROXY_OPTION_IPV6])) ((struct sockaddr_in6 *)(res->ai_addr))->sin6_port = htons (CONFIG_INTEGER(ptr_proxy->options[PROXY_OPTION_PORT])); else ((struct sockaddr_in *)(res->ai_addr))->sin_port = htons (CONFIG_INTEGER(ptr_proxy->options[PROXY_OPTION_PORT])); /* connect to peer */ if (!network_connect (HOOK_CONNECT(hook_connect, sock), res->ai_addr, res->ai_addrlen)) { /* connection refused */ snprintf (status_without_string, sizeof (status_without_string), "%c00000", '0' + WEECHAT_HOOK_CONNECT_CONNECTION_REFUSED); num_written = write (HOOK_CONNECT(hook_connect, child_write), status_without_string, strlen (status_without_string)); (void) num_written; freeaddrinfo (res); return; } if (!network_pass_proxy (HOOK_CONNECT(hook_connect, proxy), HOOK_CONNECT(hook_connect, sock), HOOK_CONNECT(hook_connect, address), HOOK_CONNECT(hook_connect, port))) { /* proxy fails to connect to peer */ snprintf (status_without_string, sizeof (status_without_string), "%c00000", '0' + WEECHAT_HOOK_CONNECT_PROXY_ERROR); num_written = write (HOOK_CONNECT(hook_connect, child_write), status_without_string, strlen (status_without_string)); (void) num_written; freeaddrinfo (res); return; } status_str[0] = '0' + WEECHAT_HOOK_CONNECT_OK; } else { /* set local hostname/IP if asked by user */ if (HOOK_CONNECT(hook_connect, local_hostname) && HOOK_CONNECT(hook_connect, local_hostname[0])) { memset (&hints, 0, sizeof(hints)); hints.ai_family = (HOOK_CONNECT(hook_connect, ipv6)) ? AF_INET6 : AF_INET; hints.ai_socktype = SOCK_STREAM; rc = getaddrinfo (HOOK_CONNECT(hook_connect, local_hostname), NULL, &hints, &res_local); if (rc != 0) { /* fails to set local hostname/IP */ status_with_string = NULL; error = gai_strerror (rc); if (error) { length = 1 + 5 + strlen (error) + 1; status_with_string = malloc (length); if (status_with_string) { snprintf (status_with_string, length, "%c%05d%s", '0' + WEECHAT_HOOK_CONNECT_LOCAL_HOSTNAME_ERROR, (int)strlen (error), error); } } if (status_with_string) { num_written = write (HOOK_CONNECT(hook_connect, child_write), status_with_string, strlen (status_with_string)); } else { snprintf (status_without_string, sizeof (status_without_string), "%c00000", '0' + WEECHAT_HOOK_CONNECT_LOCAL_HOSTNAME_ERROR); num_written = write (HOOK_CONNECT(hook_connect, child_write), status_without_string, strlen (status_without_string)); } if (status_with_string) free (status_with_string); (void) num_written; if (res_local) freeaddrinfo (res_local); return; } else if (!res_local || (HOOK_CONNECT(hook_connect, ipv6) && (res_local->ai_family != AF_INET6)) || ((!HOOK_CONNECT(hook_connect, ipv6) && (res_local->ai_family != AF_INET)))) { /* fails to set local hostname/IP */ snprintf (status_without_string, sizeof (status_without_string), "%c00000", '0' + WEECHAT_HOOK_CONNECT_LOCAL_HOSTNAME_ERROR); num_written = write (HOOK_CONNECT(hook_connect, child_write), status_without_string, strlen (status_without_string)); (void) num_written; if (res_local) freeaddrinfo (res_local); return; } if (bind (HOOK_CONNECT(hook_connect, sock), res_local->ai_addr, res_local->ai_addrlen) < 0) { /* fails to set local hostname/IP */ snprintf (status_without_string, sizeof (status_without_string), "%c00000", '0' + WEECHAT_HOOK_CONNECT_LOCAL_HOSTNAME_ERROR); num_written = write (HOOK_CONNECT(hook_connect, child_write), status_without_string, strlen (status_without_string)); (void) num_written; if (res_local) freeaddrinfo (res_local); return; } } /* get info about peer */ memset (&hints, 0, sizeof(hints)); hints.ai_family = (HOOK_CONNECT(hook_connect, ipv6)) ? AF_INET6 : AF_INET; hints.ai_socktype = SOCK_STREAM; rc = getaddrinfo (HOOK_CONNECT(hook_connect, address), NULL, &hints, &res); if (rc != 0) { status_with_string = NULL; error = gai_strerror (rc); if (error) { length = 1 + 5 + strlen (error) + 1; status_with_string = malloc (length); if (status_with_string) { snprintf (status_with_string, length, "%c%05d%s", '0' + WEECHAT_HOOK_CONNECT_ADDRESS_NOT_FOUND, (int)strlen (error), error); } } if (status_with_string) { num_written = write (HOOK_CONNECT(hook_connect, child_write), status_with_string, strlen (status_with_string)); } else { snprintf (status_without_string, sizeof (status_without_string), "%c00000", '0' + WEECHAT_HOOK_CONNECT_ADDRESS_NOT_FOUND); num_written = write (HOOK_CONNECT(hook_connect, child_write), status_without_string, strlen (status_without_string)); } if (status_with_string) free (status_with_string); (void) num_written; if (res) freeaddrinfo (res); if (res_local) freeaddrinfo (res_local); return; } else if (!res) { /* address not found */ snprintf (status_without_string, sizeof (status_without_string), "%c00000", '0' + WEECHAT_HOOK_CONNECT_ADDRESS_NOT_FOUND); num_written = write (HOOK_CONNECT(hook_connect, child_write), status_without_string, strlen (status_without_string)); (void) num_written; if (res) freeaddrinfo (res); if (res_local) freeaddrinfo (res_local); return; } status_str[0] = '0' + WEECHAT_HOOK_CONNECT_IP_ADDRESS_NOT_FOUND; /* try all IP addresses found, stop when connection is ok */ for (ptr_res = res; ptr_res; ptr_res = ptr_res->ai_next) { /* skip IP address if it's not good family */ if ((HOOK_CONNECT(hook_connect, ipv6) && (ptr_res->ai_family != AF_INET6)) || ((!HOOK_CONNECT(hook_connect, ipv6) && (ptr_res->ai_family != AF_INET)))) continue; /* connect to peer */ if (HOOK_CONNECT(hook_connect, ipv6)) ((struct sockaddr_in6 *)(ptr_res->ai_addr))->sin6_port = htons (HOOK_CONNECT(hook_connect, port)); else ((struct sockaddr_in *)(ptr_res->ai_addr))->sin_port = htons (HOOK_CONNECT(hook_connect, port)); if (network_connect (HOOK_CONNECT(hook_connect, sock), ptr_res->ai_addr, ptr_res->ai_addrlen)) { status_str[0] = '0' + WEECHAT_HOOK_CONNECT_OK; if (HOOK_CONNECT(hook_connect, ipv6)) { if (inet_ntop (AF_INET6, &((struct sockaddr_in6 *)(ptr_res->ai_addr))->sin6_addr, ipv6_address, INET6_ADDRSTRLEN)) { ptr_address = ipv6_address; } } else { if (inet_ntop (AF_INET, &((struct sockaddr_in *)(ptr_res->ai_addr))->sin_addr, ipv4_address, INET_ADDRSTRLEN)) { ptr_address = ipv4_address; } } break; } else status_str[0] = '0' + WEECHAT_HOOK_CONNECT_CONNECTION_REFUSED; } } if (status_str[0] == '0' + WEECHAT_HOOK_CONNECT_OK) { status_with_string = NULL; if (ptr_address) { length = strlen (status_str) + 5 + strlen (ptr_address) + 1; status_with_string = malloc (length); if (status_with_string) { snprintf (status_with_string, length, "%s%05d%s", status_str, (int)strlen (ptr_address), ptr_address); } } if (status_with_string) { num_written = write (HOOK_CONNECT(hook_connect, child_write), status_with_string, strlen (status_with_string)); (void) num_written; free (status_with_string); } else { snprintf (status_without_string, sizeof (status_without_string), "%s00000", status_str); num_written = write (HOOK_CONNECT(hook_connect, child_write), status_without_string, strlen (status_without_string)); (void) num_written; } } else { snprintf (status_without_string, sizeof (status_without_string), "%s00000", status_str); num_written = write (HOOK_CONNECT(hook_connect, child_write), status_without_string, strlen (status_without_string)); (void) num_written; } if (res) freeaddrinfo (res); if (res_local) freeaddrinfo (res_local); }
struct item *client(char server, struct item *key, struct item *request) /*@ requires [?f0]world(rpc_pub, rpc_key_clsfy) &*& principal(?client, ?count) &*& item(key, symmetric_key_item(?creator, ?id), rpc_pub) &*& item(request, ?req, rpc_pub) &*& [_]rpc_pub(req) &*& true == request(creator, server, req) &*& shared_with(creator, id) == server; @*/ /*@ ensures [f0]world(rpc_pub, rpc_key_clsfy) &*& principal(client, count) &*& item(key, symmetric_key_item(creator, id), rpc_pub) &*& item(request, req, rpc_pub) &*& item(result, ?resp, rpc_pub) &*& ( col || bad(creator) || bad(server) || response(creator, server, req, resp) ); @*/ { struct network_status *net_stat = network_connect("localhost", SERVER_PORT); { struct item *tag = create_data_item_from_int(0); //@ item d = data_item(chars_of_int(0)); //@ assert item(tag, d, rpc_pub); //@ close rpc_pub(d); //@ leak rpc_pub(d); struct item *payload = create_pair(tag, request); //@ item p = pair_item(d, req); //@ assert item(payload, p, rpc_pub); //@ close rpc_pub(p); //@ leak rpc_pub(p); item_free(tag); struct item *hash = create_hmac(key, payload); //@ item h = hmac_item(creator, id, some(p)); //@ if (!col) assert item(hash, h, rpc_pub); //@ close rpc_pub(h); //@ leak rpc_pub(h); struct item *m = create_pair(hash, payload); //@ assert item(m, ?msg, rpc_pub); //@ item msg0 = pair_item(h, p); //@ if (!col) msg == msg0; //@ close rpc_pub(msg); //@ leak rpc_pub(msg); item_free(hash); item_free(payload); network_send(net_stat, m); item_free(m); } struct item *response; { struct item *r = network_receive(net_stat); check_is_pair(r); //@ assert item(r, pair_item(?h0, ?p0), rpc_pub); struct item *hmac1 = pair_get_first(r); //@ assert item(hmac1, ?h, rpc_pub); struct item *payload = pair_get_second(r); //@ assert item(payload, ?p, rpc_pub); /*@ if (!col) { assert h0 == h; assert p0 == p; open [_]rpc_pub(pair_item(h, p)); open [_]rpc_pub(h); open [_]rpc_pub(p); } @*/ struct item *hmac2 = create_hmac(key, payload); item_check_equal(hmac1, hmac2); item_free(hmac1); item_free(hmac2); item_free(r); //@ assert col || h == hmac_item(creator, id, some(p)); struct item *tag = pair_get_first(payload); check_is_data(tag); int tagValue = item_get_data_as_int(tag); if (tagValue != 1) abort(); //@ item d = data_item(chars_of_int(1)); //@ assert item(tag, ?d0, rpc_pub); //@ assert col || d == d0; item_free(tag); struct item *reqresp = pair_get_second(payload); struct item *request1 = pair_get_first(reqresp); response = pair_get_second(reqresp); //@ assert item(request1, ?req1, rpc_pub); //@ assert item(response, ?resp, rpc_pub); //@ if (!col) assert p == pair_item(d, pair_item(req1, resp)); item_free(payload); item_free(reqresp); item_check_equal(request, request1); //@ assert col || req1 == req; item_free(request1); } network_disconnect(net_stat); return response; }
bool network_connect( ENetHost* socket, const char* server, int port ) { return network_connect( socket, server, port, 3000 ); }
int service_to_fd(const char* name, const atransport* transport) { int ret = -1; if(!strncmp(name, "tcp:", 4)) { int port = atoi(name + 4); name = strchr(name + 4, ':'); if(name == 0) { std::string error; ret = network_loopback_client(port, SOCK_STREAM, &error); if (ret >= 0) disable_tcp_nagle(ret); } else { #if ADB_HOST std::string error; ret = network_connect(name + 1, port, SOCK_STREAM, 0, &error); #else return -1; #endif } #if !defined(_WIN32) /* winsock doesn't implement unix domain sockets */ } else if(!strncmp(name, "local:", 6)) { ret = socket_local_client(name + 6, ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM); } else if(!strncmp(name, "localreserved:", 14)) { ret = socket_local_client(name + 14, ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM); } else if(!strncmp(name, "localabstract:", 14)) { ret = socket_local_client(name + 14, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM); } else if(!strncmp(name, "localfilesystem:", 16)) { ret = socket_local_client(name + 16, ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM); #endif #if !ADB_HOST } else if(!strncmp("dev:", name, 4)) { ret = unix_open(name + 4, O_RDWR | O_CLOEXEC); } else if(!strncmp(name, "framebuffer:", 12)) { ret = create_service_thread(framebuffer_service, 0); } else if (!strncmp(name, "jdwp:", 5)) { ret = create_jdwp_connection_fd(atoi(name+5)); } else if(!strncmp(name, "shell", 5)) { ret = ShellService(name + 5, transport); } else if(!strncmp(name, "exec:", 5)) { ret = StartSubprocess(name + 5, nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone); } else if(!strncmp(name, "sync:", 5)) { ret = create_service_thread(file_sync_service, NULL); } else if(!strncmp(name, "remount:", 8)) { ret = create_service_thread(remount_service, NULL); } else if(!strncmp(name, "reboot:", 7)) { void* arg = strdup(name + 7); if (arg == NULL) return -1; ret = create_service_thread(reboot_service, arg); } else if(!strncmp(name, "root:", 5)) { ret = create_service_thread(restart_root_service, NULL); } else if(!strncmp(name, "unroot:", 7)) { ret = create_service_thread(restart_unroot_service, NULL); } else if(!strncmp(name, "backup:", 7)) { ret = StartSubprocess(android::base::StringPrintf("/system/bin/bu backup %s", (name + 7)).c_str(), nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone); } else if(!strncmp(name, "restore:", 8)) { ret = StartSubprocess("/system/bin/bu restore", nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone); } else if(!strncmp(name, "tcpip:", 6)) { int port; if (sscanf(name + 6, "%d", &port) != 1) { return -1; } ret = create_service_thread(restart_tcp_service, (void *) (uintptr_t) port); } else if(!strncmp(name, "usb:", 4)) { ret = create_service_thread(restart_usb_service, NULL); } else if (!strncmp(name, "reverse:", 8)) { ret = reverse_service(name + 8); } else if(!strncmp(name, "disable-verity:", 15)) { ret = create_service_thread(set_verity_enabled_state_service, (void*)0); } else if(!strncmp(name, "enable-verity:", 15)) { ret = create_service_thread(set_verity_enabled_state_service, (void*)1); #endif } if (ret >= 0) { close_on_exec(ret); } return ret; }
void session_create(struct session_t * session) { network_connect(&session->network); session->run = &session_run; }