void *probe(void *data) { int sock; struct sockaddr_in rmt; st_netfd_t rmt_nfd; struct probedef *probe = (struct probedef *)data; st_utime_t start; ST_INITIATE(probe->port); start = st_utime(); if (debug > 3) fprintf(stderr, "Connecting to: %s\n", probe->ipaddress); if (st_connect(rmt_nfd, (struct sockaddr *)&rmt, sizeof(rmt), TIMEOUT) < 0) { char buf[256]; sprintf(buf, "%s(%d): %s", probe->ipaddress, __LINE__, strerror(errno)); probe->connect = ((float) (st_utime() - start)) * 0.000001; probe->msg = strdup(buf); LOG(LOG_DEBUG, probe->msg); if (debug > 3) fprintf(stderr, "%s: %s\n", probe->ipaddress, probe->msg); goto err_close; } probe->connect = ((float) (st_utime() - start)) * 0.000001; err_close: st_netfd_close(rmt_nfd); probe->total = ((float) (st_utime() - start)) * 0.000001; done: thread_count--; return NULL; }
int _peer_connect(const peer_index_t index) { int client_fd, rv, result; struct addrinfo hints, *ai, *p; struct peer_info *peer_info; result = 0; memset(&hints, 0, sizeof(hints)); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_STREAM; peer_info = &peer_list[index]; LOG("[%d] 向 Peer #%d %s:%s 建立互联\n", self_index, index, peer_info->host, peer_info->port); if ((rv = getaddrinfo(peer_info->host, peer_info->port, &hints, &ai)) != 0) { ERR("[%d] failed to getaddrinfo to peer #%d\n", self_index, index); return -1; } for (p = ai; p != NULL; p = p->ai_next) { if ((client_fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { continue; } if ((peer_info->rpc_fd = st_netfd_open_socket(client_fd)) == NULL) { close(client_fd); continue; } if ((rv = st_connect(peer_info->rpc_fd, p->ai_addr, p->ai_addrlen, ST_UTIME_NO_TIMEOUT)) != 0) { st_netfd_close(peer_info->rpc_fd); continue; } // 写入 1 字节的 rpc 握手头 if ((rv = st_write(peer_info->rpc_fd, (char *)&self_index, 1, ST_UTIME_NO_TIMEOUT)) == -1) { ERR("[%d] handshake failed.\n", self_index); result = -1; } break; } if (p == NULL) { ERR("[%d] failed to connect to peer #%d.\n", self_index, index); result = -1; } freeaddrinfo(ai); ai = NULL; p = NULL; return result; }
int SrsForwarder::connect_server() { int ret = ERROR_SUCCESS; // reopen close_underlayer_socket(); // open socket. srs_trace("forward stream=%s, tcUrl=%s to server=%s, port=%d", stream_name.c_str(), tc_url.c_str(), server.c_str(), port); int sock = socket(AF_INET, SOCK_STREAM, 0); if(sock == -1){ ret = ERROR_SOCKET_CREATE; srs_error("create socket error. ret=%d", ret); return ret; } srs_assert(!stfd); stfd = st_netfd_open_socket(sock); if(stfd == NULL){ ret = ERROR_ST_OPEN_SOCKET; srs_error("st_netfd_open_socket failed. ret=%d", ret); return ret; } srs_freep(client); srs_freep(io); io = new SrsSocket(stfd); client = new SrsRtmpClient(io); kbps->set_io(io, io); // connect to server. std::string ip = srs_dns_resolve(server); if (ip.empty()) { ret = ERROR_SYSTEM_IP_INVALID; srs_error("dns resolve server error, ip empty. ret=%d", ret); return ret; } sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = inet_addr(ip.c_str()); if (st_connect(stfd, (const struct sockaddr*)&addr, sizeof(sockaddr_in), SRS_FORWARDER_SLEEP_US) == -1){ ret = ERROR_ST_CONNECT; srs_error("connect to server error. ip=%s, port=%d, ret=%d", ip.c_str(), port, ret); return ret; } srs_trace("connect to server success. server=%s, ip=%s, port=%d", server.c_str(), ip.c_str(), port); return ret; }
hoxResult hoxSocketAPI::tcp_connect( const std::string& sHost, const unsigned short int nPort, st_netfd_t& nfd ) { int iResult = 0; struct sockaddr_in serverAddress; struct hostent* hostInfo; int sock = -1; nfd = NULL; // Default: invalid socket // gethostbyname() takes a host name or ip address in "numbers and // dots" notation, and returns a pointer to a hostent structure, // which we'll need later. It's not important for us what this // structure is actually composed of. hostInfo = gethostbyname( sHost.c_str() ); if (hostInfo == NULL) { hoxLog(LOG_SYS_WARN, "%s: problem interpreting host: %s", __FUNCTION__, sHost.c_str()); return hoxRC_ERR; } // Create a socket. "AF_INET" means it will use the IPv4 protocol. // "SOCK_STREAM" means it will be a reliable connection (i.e., TCP; // for UDP use SOCK_DGRAM), and I'm not sure what the 0 for the last // parameter means, but it seems to work. sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) { hoxLog(LOG_SYS_WARN, "%s: cannot create socket", __FUNCTION__); return hoxRC_ERR; } if ((nfd = st_netfd_open_socket(sock)) == NULL) { hoxLog(LOG_SYS_WARN, "%s: st_netfd_open_socket failed", __FUNCTION__); close(sock); return hoxRC_ERR; } // Connect to server. First we have to set some fields in the // serverAddress structure. The system will assign me an arbitrary // local port that is not in use. serverAddress.sin_family = hostInfo->h_addrtype; memcpy((char *) &serverAddress.sin_addr.s_addr, hostInfo->h_addr_list[0], hostInfo->h_length); serverAddress.sin_port = htons( nPort); iResult = st_connect( nfd, (struct sockaddr *) &serverAddress, sizeof(serverAddress), ST_UTIME_NO_TIMEOUT ); if ( iResult < 0 ) { hoxLog(LOG_SYS_WARN, "%s: cannot connect", __FUNCTION__); st_netfd_close( nfd ); return hoxRC_ERR; } return hoxRC_OK; }
static void *handle_request(void *arg) { struct pollfd pds[2]; st_netfd_t cli_nfd, rmt_nfd; int sock, n; char buf[IOBUFSIZE]; cli_nfd = (st_netfd_t) arg; pds[0].fd = st_netfd_fileno(cli_nfd); pds[0].events = POLLIN; /* Connect to remote host */ if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) { print_sys_error("socket"); goto done; } if ((rmt_nfd = st_netfd_open_socket(sock)) == NULL) { print_sys_error("st_netfd_open_socket"); close(sock); goto done; } if (st_connect(rmt_nfd, (struct sockaddr *)&rmt_addr, sizeof(rmt_addr), -1) < 0) { print_sys_error("st_connect"); st_netfd_close(rmt_nfd); goto done; } pds[1].fd = sock; pds[1].events = POLLIN; /* Now just pump the data through */ for ( ; ; ) { pds[0].revents = 0; pds[1].revents = 0; if (st_poll(pds, 2, -1) <= 0) { print_sys_error("st_poll"); break; } if (pds[0].revents & POLLIN) { if ((n = (int) st_read(cli_nfd, buf, IOBUFSIZE, -1)) <= 0) break; if (st_write(rmt_nfd, buf, n, -1) != n) break; } if (pds[1].revents & POLLIN) { if ((n = (int) st_read(rmt_nfd, buf, IOBUFSIZE, -1)) <= 0) break; if (st_write(cli_nfd, buf, n, -1) != n) break; } } st_netfd_close(rmt_nfd); done: st_netfd_close(cli_nfd); return NULL; }
void *probe(void *data) { int sock, len; struct sockaddr_in rmt; st_netfd_t rmt_nfd; struct probedef *probe = (struct probedef *)data; char buffer[1024]; st_utime_t start; ST_INITIATE(110); start = st_utime(); if (debug > 3) fprintf(stderr, "Connecting to: %s\n", probe->ipaddress); if (st_connect(rmt_nfd, (struct sockaddr *)&rmt, sizeof(rmt), TIMEOUT) < 0) { char buf[256]; sprintf(buf, "%s(%d): %s", probe->ipaddress, __LINE__, strerror(errno)); probe->connect = ((float) (st_utime() - start)) * 0.000001; probe->msg = strdup(buf); LOG(LOG_DEBUG, probe->msg); if (debug > 3) fprintf(stderr, "%s: %s\n", probe->ipaddress, probe->msg); goto err_close; } probe->connect = ((float) (st_utime() - start)) * 0.000001; // expect here: +OK POP3 xxx.xxxxxxx.xx v2000.70rh server ready memset(buffer, 0, sizeof(buffer)); len = st_read(rmt_nfd, buffer, sizeof(buffer), TIMEOUT); if (len == -1) { ST_ERROR("read", TIMEOUT); goto err_close; } if (debug > 3) fprintf(stderr, "< %s", buffer); if (buffer[0] != '+') { probe->msg = strdup(buffer); goto err_close; } if (probe->username == NULL || probe->username[0] == 0) { probe->msg = strdup("missing username"); goto err_close; } sprintf(buffer, "USER %s\n", probe->username); if (debug > 3) fprintf(stderr, "> %s", buffer); len = st_write(rmt_nfd, buffer, strlen(buffer), TIMEOUT); if (len == -1) { ST_ERROR("write", TIMEOUT); goto err_close; } // expect here: +OK User name accepted, password please memset(buffer, 0, sizeof(buffer)); len = st_read(rmt_nfd, buffer, sizeof(buffer), TIMEOUT); if (len == -1) { ST_ERROR("read", TIMEOUT); goto err_close; } if (debug > 3) fprintf(stderr, "< %s", buffer); if (buffer[0] != '+') { probe->msg = strdup(buffer); goto err_close; } sprintf(buffer, "PASS %s\n", probe->password); if (debug > 3) fprintf(stderr, "> %s", buffer); len = st_write(rmt_nfd, buffer, strlen(buffer), TIMEOUT); if (len == -1) { ST_ERROR("write", TIMEOUT); goto err_close; } // expect here: +OK Mailbox open, 62 messages memset(buffer, 0, sizeof(buffer)); len = st_read(rmt_nfd, buffer, sizeof(buffer), TIMEOUT); if (len == -1) { ST_ERROR("read", TIMEOUT); goto err_close; } if (debug > 3) fprintf(stderr, "< %s", buffer); if (buffer[0] != '+') { probe->msg = strdup(buffer); goto err_close; } sprintf(buffer, "QUIT\n"); if (debug > 3) fprintf(stderr, "> %s", buffer); len = st_write(rmt_nfd, buffer, strlen(buffer), TIMEOUT); if (len == -1) { ST_ERROR("write", TIMEOUT); goto err_close; } // expect here: +OK Sayonara memset(buffer, 0, sizeof(buffer)); len = st_read(rmt_nfd, buffer, sizeof(buffer), TIMEOUT); if (len == -1) { ST_ERROR("read", TIMEOUT); goto err_close; } if (debug > 3) fprintf(stderr, "< %s", buffer); if (buffer[0] != '+') { probe->msg = strdup(buffer); goto err_close; } err_close: st_netfd_close(rmt_nfd); probe->total = ((float) (st_utime() - start)) * 0.000001; done: thread_count--; return NULL; }
int SrsEdgeForwarder::connect_server() { int ret = ERROR_SUCCESS; // reopen close_underlayer_socket(); SrsConfDirective* conf = _srs_config->get_vhost_edge_origin(_req->vhost); srs_assert(conf); // select the origin. std::string server = conf->args.at(origin_index % conf->args.size()); origin_index = (origin_index + 1) % conf->args.size(); std::string s_port = RTMP_DEFAULT_PORT; int port = ::atoi(RTMP_DEFAULT_PORT); size_t pos = server.find(":"); if (pos != std::string::npos) { s_port = server.substr(pos + 1); server = server.substr(0, pos); port = ::atoi(s_port.c_str()); } // open socket. srs_trace("connect edge stream=%s, tcUrl=%s to server=%s, port=%d", _req->stream.c_str(), _req->tcUrl.c_str(), server.c_str(), port); // TODO: FIXME: extract utility method int sock = socket(AF_INET, SOCK_STREAM, 0); if(sock == -1){ ret = ERROR_SOCKET_CREATE; srs_error("create socket error. ret=%d", ret); return ret; } srs_assert(!stfd); stfd = st_netfd_open_socket(sock); if(stfd == NULL){ ret = ERROR_ST_OPEN_SOCKET; srs_error("st_netfd_open_socket failed. ret=%d", ret); return ret; } srs_freep(client); srs_freep(io); io = new SrsSocket(stfd); client = new SrsRtmpClient(io); kbps->set_io(io, io); // connect to server. std::string ip = srs_dns_resolve(server); if (ip.empty()) { ret = ERROR_SYSTEM_IP_INVALID; srs_error("dns resolve server error, ip empty. ret=%d", ret); return ret; } sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = inet_addr(ip.c_str()); if (st_connect(stfd, (const struct sockaddr*)&addr, sizeof(sockaddr_in), SRS_EDGE_FORWARDER_TIMEOUT_US) == -1){ ret = ERROR_ST_CONNECT; srs_error("connect to server error. ip=%s, port=%d, ret=%d", ip.c_str(), port, ret); return ret; } srs_trace("connect to server success. server=%s, ip=%s, port=%d", server.c_str(), ip.c_str(), port); return ret; }
static void *link_to_peers(void *arg) { int client_fd, index, rv; st_netfd_t client; struct addrinfo hints, *ai, *p; const char *host = "0.0.0.0"; fprintf(stderr, "link to perrs\n"); for (int i=0; i<vp_count; i++) { if (i == my_index) continue; char port[16]; //snprintf(port, 16 - 1, "%d", RPC_PORT + i); index = RPC_PORT + i; sprintf(port, "%d", index); memset(&hints, 0, sizeof(hints)); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_STREAM; if ((rv = getaddrinfo(host, port, &hints, &ai)) != 0) { fprintf(stderr, "[%d] failed to getaddrinfo to peer #%d\n", my_index, i); continue; } for (p = ai; p != NULL; p = p->ai_next) { if ((client_fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { continue; } if ((client = st_netfd_open(client_fd)) == NULL) { close(client_fd); continue; } if ((rv = st_connect(client, p->ai_addr, p->ai_addrlen, ST_UTIME_NO_TIMEOUT)) != 0) { st_netfd_close(client); close(client_fd); continue; } else { fd_list[i] = client; } // 写入 1 字节的 rpc 握手头 if ((rv = st_write(client, (char *)&my_index, 1, ST_UTIME_NO_TIMEOUT)) == -1) { fprintf(stderr, "[%d] handshake failed.\n", my_index); } fd_list[i] = client; break; } if (p == NULL) { fprintf(stderr, "[%d] failed to connect to peer #%d.\n", my_index, i); } freeaddrinfo(ai); ai = NULL; p = NULL; // 模拟:发出第一个 rpc 包 char message[] = "hello rpc."; rpcpkg_len len = strlen(message); rpcpkg_len nlen = HTON(len); // network order of len char *package = (char*)calloc(sizeof(rpcpkg_len) + len, sizeof(char)); memcpy(package, &nlen, sizeof(len)); memcpy(package + sizeof(len), message, len); fprintf(stdout, "[%d] construction an package: << ", my_index); for (int j=0; j<len + sizeof(len); j++) { fprintf(stdout, "%02X ", *((uint8_t*)package + j)); } fprintf(stdout, " >>\n"); if ((rv = st_write(client, package, len + sizeof(rpcpkg_len), ST_UTIME_NO_TIMEOUT)) == -1) { fprintf(stderr, "[%d] failed to write package into client\n", my_index); } free(package); } return NULL; }
void *push(void *data) { int sock; struct hostent *hp; struct sockaddr_in server; st_netfd_t rmt_nfd; struct thr_data *td = (struct thr_data *)data; struct q_info *q = td->q; if (HAVE_OPT(DIALSCRIPT) && !online) { int status; status = system(OPT_ARG(DIALSCRIPT)); if (WIFEXITED(status)) { if (WEXITSTATUS(status) != 0) { LOG(LOG_WARNING, "%s: error %d", OPT_ARG(DIALSCRIPT), WEXITSTATUS(status)); goto quit; } } else { LOG(LOG_ERR, "%s exited abnormally", OPT_ARG(DIALSCRIPT)); goto quit; } online = TRUE; } if ((hp = gethostbyname(q->host)) == (struct hostent *) 0) { LOG(LOG_ERR, "can't resolve %s: %s", q->host, strerror(h_errno)); q->fatal = 1; goto quit; } memset(&server, 0, sizeof(server)); server.sin_family = AF_INET; memcpy((char *) &server.sin_addr, (char *) hp->h_addr, hp->h_length); server.sin_port = htons(q->port); sock = socket( AF_INET, SOCK_STREAM, 0 ); if (sock == -1) { LOG(LOG_ERR, "socket: %m"); q->fatal = 1; goto quit; } uw_setproctitle("connecting to %s:%d", q->host, q->port); #ifdef USE_ST if ((rmt_nfd = st_netfd_open_socket(sock)) == NULL) { LOG(LOG_ERR, "st_netfd_open_socket: %m", strerror(errno)); close(sock); q->fatal = 1; goto quit; } if (st_connect(rmt_nfd, (struct sockaddr *)&server, sizeof(server), TIMEOUT) < 0) { LOG(LOG_ERR, "%s:%d: %m", q->host, q->port, strerror(errno)); st_netfd_close(rmt_nfd); q->fatal = 1; goto quit; } #else rmt_nfd = sock; if (connect(rmt_nfd, (struct sockaddr *)&server, sizeof(server)) < 0) { LOG(LOG_ERR, "%s:%d: %m", q->host, q->port, strerror(errno)); close(rmt_nfd); q->fatal = 1; goto quit; } #endif if (pushto(rmt_nfd, td)) { LOG(LOG_INFO, "uploaded %s", td->filename); unlink(td->filename); } #ifdef USE_ST st_netfd_close(rmt_nfd); #else close(rmt_nfd); #endif quit: free(td->filename); free(td); #ifdef USE_ST q->thread_count--; thread_count--; #endif return NULL; }