static int swClient_udp_connect(swClient *cli, char *host, int port, double timeout, int udp_connect) { if (swClient_inet_addr(cli, host, port) < 0) { return SW_ERR; } cli->socket->active = 1; cli->timeout = timeout; int bufsize = SwooleG.socket_buffer_size; if (timeout > 0) { swSocket_set_timeout(cli->socket->fd, timeout); } if (cli->type == SW_SOCK_UNIX_DGRAM) { struct sockaddr_un* client_addr = &cli->socket->info.addr.un; sprintf(client_addr->sun_path, "/tmp/swoole-client.%d.%d.sock", getpid(), cli->socket->fd); client_addr->sun_family = AF_UNIX; unlink(client_addr->sun_path); if (bind(cli->socket->fd, (struct sockaddr *) client_addr, sizeof(cli->socket->info.addr.un)) < 0) { swSysError("bind(%s) failed.", client_addr->sun_path); return SW_ERR; } } else if (udp_connect != 1) { goto connect_ok; } if (connect(cli->socket->fd, (struct sockaddr *) (&cli->server_addr), cli->server_addr.len) == 0) { swSocket_clean(cli->socket->fd); connect_ok: setsockopt(cli->socket->fd, SOL_SOCKET, SO_SNDBUF, &bufsize, sizeof(bufsize)); setsockopt(cli->socket->fd, SOL_SOCKET, SO_RCVBUF, &bufsize, sizeof(bufsize)); if (cli->async && cli->onConnect) { if (SwooleG.main_reactor->add(SwooleG.main_reactor, cli->socket->fd, cli->reactor_fdtype | SW_EVENT_READ) < 0) { return SW_ERR; } cli->onConnect(cli); } return SW_OK; } else { swSysError("connect() failed."); cli->socket->active = 0; return SW_ERR; } }
static int swClient_tcp_connect_sync(swClient *cli, char *host, int port, double timeout, int nonblock) { int ret; cli->timeout = timeout; if (swClient_inet_addr(cli, host, port) < 0) { return SW_ERR; } if (nonblock == 1) { swSetNonBlock(cli->socket->fd); } else { if (cli->timeout > 0) { swSocket_set_timeout(cli->socket->fd, timeout); } swSetBlock(cli->socket->fd); } while (1) { ret = connect(cli->socket->fd, (struct sockaddr *) &cli->server_addr.addr, cli->server_addr.len); if (ret < 0) { if (errno == EINTR) { continue; } } break; } if (ret >= 0) { cli->socket->active = 1; #ifdef SW_USE_OPENSSL if (cli->open_ssl) { if (swClient_enable_ssl_encrypt(cli) < 0) { return SW_ERR; } if (swClient_ssl_handshake(cli) < 0) { return SW_ERR; } } #endif } return ret; }
static int swClient_tcp_connect_sync(swClient *cli, char *host, int port, double timeout, int nonblock) { int ret, n; char buf[1024]; cli->timeout = timeout; if (swClient_inet_addr(cli, host, port) < 0) { return SW_ERR; } if (nonblock == 1) { swSetNonBlock(cli->socket->fd); } else { if (cli->timeout > 0) { swSocket_set_timeout(cli->socket->fd, timeout); } swSetBlock(cli->socket->fd); } while (1) { ret = connect(cli->socket->fd, (struct sockaddr *) &cli->server_addr.addr, cli->server_addr.len); if (ret < 0) { if (errno == EINTR) { continue; } } break; } if (ret >= 0) { cli->socket->active = 1; //socks5 proxy if (cli->socks5_proxy) { swSocks5_pack(buf, cli->socks5_proxy->username == NULL ? 0x00 : 0x02); if (cli->send(cli, buf, 3, 0) < 0) { return SW_ERR; } cli->socks5_proxy->state = SW_SOCKS5_STATE_HANDSHAKE; while (1) { n = cli->recv(cli, buf, sizeof(buf), 0); if (n > 0) { if (swSocks5_connect(cli, buf, n) < 0) { return SW_ERR; } else { if (cli->socks5_proxy->state == SW_SOCKS5_STATE_READY) { break; } else { continue; } } } return SW_ERR; } } #ifdef SW_USE_OPENSSL if (cli->open_ssl) { if (swClient_enable_ssl_encrypt(cli) < 0) { return SW_ERR; } if (swClient_ssl_handshake(cli) < 0) { return SW_ERR; } } #endif } return ret; }