// -------------------------------------------------------------------------- int main(int argc, char** argv) { struct params* params = 0; int sockfd = -1; params = alloc_params(); extract_params_from_cmdline_options(params, argc, argv); if (is_help_desired(params)) { show_help(argv[0]); goto out; } sockfd = inet_connect(hostname(params), portnumber(params), SOCK_STREAM); if (sockfd < 0) { fprintf(stderr, "failed to connect to remote host.\n"); goto out; } do_interactive_loop(sockfd); out: if (sockfd > 0) { close(sockfd); } free_params(params); return 0; }
static int dict_tcp_connect(DICT_TCP *dict_tcp) { int fd; /* * Connect to the server. Enforce a time limit on all operations so that * we do not get stuck. */ if ((fd = inet_connect(dict_tcp->dict.name, NON_BLOCKING, DICT_TCP_TMOUT)) < 0) { msg_warn("connect to TCP map %s: %m", dict_tcp->dict.name); return (-1); } dict_tcp->fp = vstream_fdopen(fd, O_RDWR); vstream_control(dict_tcp->fp, VSTREAM_CTL_TIMEOUT, DICT_TCP_TMOUT, VSTREAM_CTL_END); /* * Allocate per-map I/O buffers on the fly. */ if (dict_tcp->raw_buf == 0) { dict_tcp->raw_buf = vstring_alloc(10); dict_tcp->hex_buf = vstring_alloc(10); } return (0); }
int main(int argc, char *argv[]) { int socket_fd; char buffer[1024]; ssize_t num_read; socket_fd = inet_connect(SERVER, PORT_NUM, SOCK_STREAM); if (socket_fd == -1) { fprintf(stderr, "inet_connect() failed\n"); exit(EXIT_FAILURE); } printf("connect success\n"); while (1) { num_read = read(socket_fd, buffer, 1024); if (num_read == -1) { fprintf(stderr, "read() failed: %s\n", strerror(errno)); exit(EXIT_FAILURE); } printf("num_read = %d\n", (int) num_read); if (num_read != 0) printf("recv from server: %s\n", buffer); } return 0; }
static int restore_tcp_conn_state(int sk, struct inet_sk_info *ii) { int ifd, aux; TcpStreamEntry *tse; pr_info("Restoring TCP connection id %x ino %x\n", ii->ie->id, ii->ie->ino); ifd = open_image(CR_FD_TCP_STREAM, O_RSTR, ii->ie->ino); if (ifd < 0) goto err; if (pb_read_one(ifd, &tse, PB_TCP_STREAM) < 0) goto err_c; if (restore_tcp_seqs(sk, tse)) goto err_c; if (inet_bind(sk, ii)) goto err_c; if (inet_connect(sk, ii)) goto err_c; if (restore_tcp_opts(sk, tse)) goto err_c; if (restore_tcp_queues(sk, tse, ifd)) goto err_c; if (tse->has_nodelay && tse->nodelay) { aux = 1; if (restore_opt(sk, SOL_TCP, TCP_NODELAY, &aux)) goto err_c; } if (tse->has_cork && tse->cork) { aux = 1; if (restore_opt(sk, SOL_TCP, TCP_CORK, &aux)) goto err_c; } tcp_stream_entry__free_unpacked(tse, NULL); close(ifd); return 0; err_c: tcp_stream_entry__free_unpacked(tse, NULL); close(ifd); err: return -1; }
int main(int argc, char *argv[]) { int j, sfd; size_t len; ssize_t num_write, num_read; char buf[BUF_SIZE]; if (argc < 2 || strcmp(argv[1], "--help") == 0) { fprintf(stderr, "Usage: %s host msg...\n", argv[0]); exit(EXIT_FAILURE); } /* Construct server address from first command-line argument */ sfd = inet_connect(argv[1], SERVICE, SOCK_DGRAM); if (sfd == -1) { perror("inet_connect"); exit(EXIT_FAILURE); } /* Send remaining command-line argument to server as separate datagrams */ for (j = 2; j < argc; ++j) { len = strlen(argv[j]); num_write = write(sfd, argv[j], len); if (num_write == -1 || (size_t)num_write != len) { fprintf(stderr, "partial/failed write\n"); exit(EXIT_FAILURE); } num_read = read(sfd, buf, BUF_SIZE); if (num_read == -1) { perror("read"); exit(EXIT_FAILURE); } printf("[%ld bytes] %.*s\n", (long) num_read, (int) num_read, buf); } exit(EXIT_SUCCESS); }
int main(int argc, char* argv[]){ int opt; int sock_type = SOCK_STREAM; char* host; char* port = DEFAULT_PORT; bool udp = false; while((opt = getopt(argc, argv, "p:u:h")) != -1){ switch(opt){ case 'h': break; case 'p': port = optarg; break; case 'u': sock_type = SOCK_DGRAM; break; case '?': print_usage(); break; } } if(argv[optind] == NULL){ print_usage(); _exit(-1); } host = argv[optind]; int remote_socket = inet_connect(host, port, sock_type); if(remote_socket == -1){ _exit(-1); } ssize_t bytes_read; char buffer[BUFFER_SIZE]; struct timespec req; req.tv_sec = 0; req.tv_nsec = 50000000; switch(fork()){ case -1: printf("Error: Unable to fork: %s\n", strerror(errno)); _exit(-1); case 0: for(;;){ bytes_read = recv(remote_socket, buffer, BUFFER_SIZE, 0); if(bytes_read == -1){ printf("Error: Could not read from remote socket: %s\n", strerror(errno)); _exit(-1); } if(bytes_read == 0){ printf("The remote host ended the connection\n"); close(remote_socket); _exit(0); } printf("%s>%.*s\n", host, (int)bytes_read, buffer); } default: for(;;){ printf("echo>"); fflush(STDIN_FILENO); bytes_read = read(STDIN_FILENO, buffer, BUFFER_SIZE); if(bytes_read == -1){ printf("Error: Could not read from stdin: %s\n", strerror(errno)); _exit(-1); } if(bytes_read == 0){ shutdown(remote_socket, SHUT_WR); _exit(0); } if(send(remote_socket, buffer, bytes_read, 0) == -1){ printf("Error: Could not send to server: %s\n", strerror(errno)); _exit(-1); } nanosleep(&req, NULL); } } }
int tcp_socket_outgoing_spec(const char *address_and_port) { return inet_connect(address_and_port, SOCK_STREAM); }
static int connect_to_ssh(BDRVSSHState *s, QDict *options, int ssh_flags, int creat_mode, Error **errp) { int r, ret; const char *host, *user, *path, *host_key_check; int port; if (!qdict_haskey(options, "host")) { ret = -EINVAL; error_setg(errp, "No hostname was specified"); goto err; } host = qdict_get_str(options, "host"); if (qdict_haskey(options, "port")) { port = qdict_get_int(options, "port"); } else { port = 22; } if (!qdict_haskey(options, "path")) { ret = -EINVAL; error_setg(errp, "No path was specified"); goto err; } path = qdict_get_str(options, "path"); if (qdict_haskey(options, "user")) { user = qdict_get_str(options, "user"); } else { user = g_get_user_name(); if (!user) { error_setg_errno(errp, errno, "Can't get user name"); ret = -errno; goto err; } } if (qdict_haskey(options, "host_key_check")) { host_key_check = qdict_get_str(options, "host_key_check"); } else { host_key_check = "yes"; } /* Construct the host:port name for inet_connect. */ g_free(s->hostport); s->hostport = g_strdup_printf("%s:%d", host, port); /* Open the socket and connect. */ s->sock = inet_connect(s->hostport, errp); if (s->sock < 0) { ret = -errno; goto err; } /* Create SSH session. */ s->session = libssh2_session_init(); if (!s->session) { ret = -EINVAL; session_error_setg(errp, s, "failed to initialize libssh2 session"); goto err; } #if TRACE_LIBSSH2 != 0 libssh2_trace(s->session, TRACE_LIBSSH2); #endif r = libssh2_session_handshake(s->session, s->sock); if (r != 0) { ret = -EINVAL; session_error_setg(errp, s, "failed to establish SSH session"); goto err; } /* Check the remote host's key against known_hosts. */ ret = check_host_key(s, host, port, host_key_check, errp); if (ret < 0) { goto err; } /* Authenticate. */ ret = authenticate(s, user, errp); if (ret < 0) { goto err; } /* Start SFTP. */ s->sftp = libssh2_sftp_init(s->session); if (!s->sftp) { session_error_setg(errp, s, "failed to initialize sftp handle"); ret = -EINVAL; goto err; } /* Open the remote file. */ DPRINTF("opening file %s flags=0x%x creat_mode=0%o", path, ssh_flags, creat_mode); s->sftp_handle = libssh2_sftp_open(s->sftp, path, ssh_flags, creat_mode); if (!s->sftp_handle) { session_error_setg(errp, s, "failed to open remote file '%s'", path); ret = -EINVAL; goto err; } r = libssh2_sftp_fstat(s->sftp_handle, &s->attrs); if (r < 0) { sftp_error_setg(errp, s, "failed to read file attributes"); return -EINVAL; } /* Delete the options we've used; any not deleted will cause the * block layer to give an error about unused options. */ qdict_del(options, "host"); qdict_del(options, "port"); qdict_del(options, "user"); qdict_del(options, "path"); qdict_del(options, "host_key_check"); return 0; err: if (s->sftp_handle) { libssh2_sftp_close(s->sftp_handle); } s->sftp_handle = NULL; if (s->sftp) { libssh2_sftp_shutdown(s->sftp); } s->sftp = NULL; if (s->session) { libssh2_session_disconnect(s->session, "from qemu ssh client: " "error opening connection"); libssh2_session_free(s->session); } s->session = NULL; return ret; }
static int xsasl_dovecot_server_connect(XSASL_DOVECOT_SERVER_IMPL *xp) { const char *myname = "xsasl_dovecot_server_connect"; VSTRING *line_str; VSTREAM *sasl_stream; char *line, *cmd, *mech_name; unsigned int major_version, minor_version; int fd, success, have_mech_line; int sec_props; const char *path; if (msg_verbose) msg_info("%s: Connecting", myname); /* * Not documented, but necessary for testing. */ path = xp->socket_path; if (strncmp(path, "inet:", 5) == 0) { fd = inet_connect(path + 5, BLOCKING, AUTH_TIMEOUT); } else { if (strncmp(path, "unix:", 5) == 0) path += 5; fd = unix_connect(path, BLOCKING, AUTH_TIMEOUT); } if (fd < 0) { msg_warn("SASL: Connect to %s failed: %m", xp->socket_path); return (-1); } sasl_stream = vstream_fdopen(fd, O_RDWR); vstream_control(sasl_stream, CA_VSTREAM_CTL_PATH(xp->socket_path), CA_VSTREAM_CTL_TIMEOUT(AUTH_TIMEOUT), CA_VSTREAM_CTL_END); /* XXX Encapsulate for logging. */ vstream_fprintf(sasl_stream, "VERSION\t%u\t%u\n" "CPID\t%u\n", AUTH_PROTOCOL_MAJOR_VERSION, AUTH_PROTOCOL_MINOR_VERSION, (unsigned int) getpid()); if (vstream_fflush(sasl_stream) == VSTREAM_EOF) { msg_warn("SASL: Couldn't send handshake: %m"); return (-1); } success = 0; have_mech_line = 0; line_str = vstring_alloc(256); /* XXX Encapsulate for logging. */ while (vstring_get_nonl(line_str, sasl_stream) != VSTREAM_EOF) { line = vstring_str(line_str); if (msg_verbose) msg_info("%s: auth reply: %s", myname, line); cmd = line; line = split_at(line, '\t'); if (strcmp(cmd, "VERSION") == 0) { if (sscanf(line, "%u\t%u", &major_version, &minor_version) != 2) { msg_warn("SASL: Protocol version error"); break; } if (major_version != AUTH_PROTOCOL_MAJOR_VERSION) { /* Major version is different from ours. */ msg_warn("SASL: Protocol version mismatch (%d vs. %d)", major_version, AUTH_PROTOCOL_MAJOR_VERSION); break; } } else if (strcmp(cmd, "MECH") == 0 && line != NULL) { mech_name = line; have_mech_line = 1; line = split_at(line, '\t'); if (line != 0) { sec_props = name_mask_delim_opt(myname, xsasl_dovecot_serv_sec_props, line, "\t", NAME_MASK_ANY_CASE | NAME_MASK_IGNORE); if ((sec_props & SEC_PROPS_PRIVATE) != 0) continue; } else sec_props = 0; xsasl_dovecot_server_mech_append(&xp->mechanism_list, mech_name, sec_props); } else if (strcmp(cmd, "SPID") == 0) { /* * Unfortunately the auth protocol handshake wasn't designed well * to differentiate between auth-client/userdb/master. * auth-userdb and auth-master send VERSION + SPID lines only and * nothing afterwards, while auth-client sends VERSION + MECH + * SPID + CUID + more. The simplest way that we can determine if * we've connected to the correct socket is to see if MECH line * exists or not (alternatively we'd have to have a small timeout * after SPID to see if CUID is sent or not). */ if (!have_mech_line) { msg_warn("SASL: Connected to wrong auth socket (auth-master instead of auth-client)"); break; } } else if (strcmp(cmd, "DONE") == 0) { /* Handshake finished. */ success = 1; break; } else { /* ignore any unknown commands */ } } vstring_free(line_str); if (!success) { /* handshake failed */ (void) vstream_fclose(sasl_stream); return (-1); } xp->sasl_stream = sasl_stream; return (0); }
static int xsasl_dovecot_server_connect(XSASL_DOVECOT_SERVER_IMPL *xp) { const char *myname = "xsasl_dovecot_server_connect"; VSTRING *line_str; VSTREAM *sasl_stream; char *line, *cmd, *mech_name; unsigned int major_version, minor_version; int fd, success; int sec_props; const char *path; if (msg_verbose) msg_info("%s: Connecting", myname); /* * Not documented, but necessary for testing. */ path = xp->socket_path; if (strncmp(path, "inet:", 5) == 0) { fd = inet_connect(path + 5, BLOCKING, AUTH_TIMEOUT); } else { if (strncmp(path, "unix:", 5) == 0) path += 5; fd = unix_connect(path, BLOCKING, AUTH_TIMEOUT); } if (fd < 0) { msg_warn("SASL: Connect to %s failed: %m", xp->socket_path); return (-1); } sasl_stream = vstream_fdopen(fd, O_RDWR); vstream_control(sasl_stream, VSTREAM_CTL_PATH, xp->socket_path, VSTREAM_CTL_TIMEOUT, AUTH_TIMEOUT, VSTREAM_CTL_END); /* XXX Encapsulate for logging. */ vstream_fprintf(sasl_stream, "VERSION\t%u\t%u\n" "CPID\t%u\n", AUTH_PROTOCOL_MAJOR_VERSION, AUTH_PROTOCOL_MINOR_VERSION, (unsigned int) getpid()); if (vstream_fflush(sasl_stream) == VSTREAM_EOF) { msg_warn("SASL: Couldn't send handshake: %m"); return (-1); } success = 0; line_str = vstring_alloc(256); /* XXX Encapsulate for logging. */ while (vstring_get_nonl(line_str, sasl_stream) != VSTREAM_EOF) { line = vstring_str(line_str); if (msg_verbose) msg_info("%s: auth reply: %s", myname, line); cmd = line; line = split_at(line, '\t'); if (strcmp(cmd, "VERSION") == 0) { if (sscanf(line, "%u\t%u", &major_version, &minor_version) != 2) { msg_warn("SASL: Protocol version error"); break; } if (major_version != AUTH_PROTOCOL_MAJOR_VERSION) { /* Major version is different from ours. */ msg_warn("SASL: Protocol version mismatch (%d vs. %d)", major_version, AUTH_PROTOCOL_MAJOR_VERSION); break; } } else if (strcmp(cmd, "MECH") == 0 && line != NULL) { mech_name = line; line = split_at(line, '\t'); if (line != 0) { sec_props = name_mask_delim_opt(myname, xsasl_dovecot_serv_sec_props, line, "\t", NAME_MASK_ANY_CASE | NAME_MASK_IGNORE); if ((sec_props & SEC_PROPS_PRIVATE) != 0) continue; } else sec_props = 0; xsasl_dovecot_server_mech_append(&xp->mechanism_list, mech_name, sec_props); } else if (strcmp(cmd, "DONE") == 0) { /* Handshake finished. */ success = 1; break; } else { /* ignore any unknown commands */ } } vstring_free(line_str); if (!success) { /* handshake failed */ (void) vstream_fclose(sasl_stream); return (-1); } xp->sasl_stream = sasl_stream; return (0); }
// -------------------------------------------------------------------------- int main(int argc, char** argv) { struct params* params = 0; char* buf; size_t bufsize = 4096; size_t buflen = 0; // how many chars in buf. buf = malloc(bufsize); if (!buf) { goto out; } memset(buf, 0, bufsize); signal(SIGUSR1, signal_handler); params = alloc_params(); extract_params_from_cmdline_options(params, argc, argv); if (is_help_desired(params)) { show_help(argv[0]); goto out; } int sockfd; sockfd = inet_connect(hostname(params), portnumber(params), SOCK_STREAM); if (sockfd < 0) { fprintf(stderr, "failed to connect to remote host.\n"); goto out; } buflen = 0; buflen += snprintf(buf+buflen, bufsize-buflen, "GET / HTTP/1.0\r\n"); buflen += snprintf(buf+buflen, bufsize-buflen, "\r\n"); buflen += snprintf(buf+buflen, bufsize-buflen, "Host: %s\r\n", hostname(params)); buflen += snprintf(buf+buflen, bufsize-buflen, "From: [email protected]\r\n"); buflen += snprintf(buf+buflen, bufsize-buflen, "User-Agent: Brad's-client/0.01\r\n"); buflen += snprintf(buf+buflen, bufsize-buflen, "\r\n"); fputs(buf, stderr); ssize_t count; count = write(sockfd, buf, buflen); //fprintf(stderr, "wrote %ld bytes to the socket\n", count); memset(buf, 0 , bufsize); while (0 < (count = read(sockfd, buf, bufsize))) { fputs(buf, stderr); } if (sockfd > 0) { close(sockfd); } out: free_params(params); if (buf) { free(buf); } return 0; }
int main(int argc, char *argv[]) { int socket_fd; char send_buf[1024]; unsigned char recv_buf[1024]; ssize_t bytes; memset(send_buf, 0, sizeof(send_buf)); memset(recv_buf, 0, sizeof(recv_buf)); snprintf(send_buf, sizeof(send_buf), "POST /service/getIpInfo2.php HTTP/1.1\r\n" "Host: ip.taobao.com\r\n" "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" "Origin: http://localhost\r\n" "User-Agent: Mozilla/5.0 (X11; Linux) AppleWebKit/538.1 (KHTML, like Gecko) Chrome/18.0.1025.133 Safari/538.1 Midori/0.5\r\n" "Content-Type: application/x-www-form-urlencoded\r\n" //"Accept-Encoding: gzip, deflate\r\n" "Accept-Language: en-us;q=0.750\r\n" "Referer: http://localhost/\r\n" "Connection: Keep-Alive\r\n" //"Cookie: lzstat_uv=17740663032264063595|2857556; lzstat_ss=990659082_1_1418313139_2857556\r\n" "Content-Length: 7\r\n" "\r\n" "ip=myip"); socket_fd = inet_connect("110.75.85.86", "80", SOCK_STREAM); if (socket_fd == -1) { fprintf(stderr, "Cannot connected to %s\n", HOST); exit(EXIT_FAILURE); } bytes = write(socket_fd, send_buf, strlen(send_buf)); if (bytes != strlen(send_buf)) { fprintf(stderr, "Cannot write to %s\n", HOST); exit(EXIT_FAILURE); } bytes = recv(socket_fd, recv_buf, sizeof(recv_buf), 0); if (bytes != -1) { recv_buf[bytes] = '\0'; printf("recv %ld bytes\n", bytes); } close(socket_fd); int index, i, j; for (index = 0; index < bytes; ) { for (i = 0; i < 16; ++index, ++i) { if (index < bytes) printf("%02x ", recv_buf[index]); else printf(" "); if (i == 7) printf(" "); } printf(" "); for (i = index - 16, j = 0; (i < index) && (i < bytes); ++i, ++j) { if (isprint(recv_buf[i])) printf("%c", recv_buf[i]); else printf("."); if (j == 7) printf(" "); } printf("\n"); } printf("\n\n"); return 0; /**/ memset(send_buf, 0, sizeof(send_buf)); memset(recv_buf, 0, sizeof(recv_buf)); snprintf(send_buf, sizeof(send_buf), "GET /ic.asp HTTP/1.1\r\n" "Host: 1111.ip138.com\r\n" "\r\n"); // 112.84.191.168 socket_fd = inet_connect("1111.ip138.com", "80", SOCK_STREAM); if (socket_fd == -1) { fprintf(stderr, "Cannot connected to %s\n", HOST); exit(EXIT_FAILURE); } bytes = write(socket_fd, send_buf, strlen(send_buf)); if (bytes != strlen(send_buf)) { fprintf(stderr, "Cannot write to %s\n", HOST); exit(EXIT_FAILURE); } bytes = recv(socket_fd, recv_buf, sizeof(recv_buf), 0); if (bytes != -1) { recv_buf[bytes] = '\0'; printf("%s", recv_buf); } close(socket_fd); return 0; }