int connection_http_to_proxy(Connection *conn) { Proxy *proxy = Request_get_action(conn->req, proxy); check(proxy != NULL, "Should have a proxy backend."); int proxy_fd = netdial(1, bdata(proxy->server), proxy->port); check(proxy_fd != -1, "Failed to connect to proxy backend %s:%d", bdata(proxy->server), proxy->port); if(!conn->proxy_iob) { conn->proxy_iob = IOBuf_create(BUFFER_SIZE, proxy_fd, IOBUF_SOCKET); check_mem(conn->proxy_iob); } if(!conn->client) { conn->client = h_calloc(sizeof(httpclient_parser), 1); check_mem(conn->client); hattach(conn->client, conn); } return CONNECT; error: return FAILED; }
void fetchtask(void *v) { int fd, n; char buf[512]; struct timeval stTv; //fprintf(stderr, "starting...\n"); for(;;){ if((fd = netdial(TCP, server, 80)) < 0){ fprintf(stderr, "dial %s: %s (%s)\n", server, strerror(errno), taskgetstate()); continue; } stTv.tv_sec = 60; stTv.tv_usec = 0; setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&stTv, sizeof(stTv)); snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n", url, server); fdwrite(fd, buf, strlen(buf)); while((n = fdread(fd, buf, sizeof buf)) > 0) ; if (n < 0) { fail++; }else{ sucess++; } close(fd); } }
/* iperf_udp_connect * * connect to a TCP stream listener */ int iperf_udp_connect(struct iperf_test *test) { int s, buf, sz; if ((s = netdial(test->settings->domain, Pudp, test->bind_address, test->server_hostname, test->server_port)) < 0) { i_errno = IESTREAMCONNECT; return -1; } /* Write to the UDP stream to let the server know we're here. */ buf = 123456789; if (write(s, &buf, sizeof(buf)) < 0) { // XXX: Should this be changed to IESTREAMCONNECT? print_trace(); i_errno = IESTREAMWRITE; return -1; } /* Wait until the server confirms the client UDP write */ // XXX: Should this read be TCP instead? if ((sz = recv(s, &buf, sizeof(buf), 0)) < 0) { i_errno = IESTREAMREAD; return -1; } return s; }
/* * iperf_udp_connect * * "Connect" to a UDP stream listener. */ int iperf_udp_connect(struct iperf_test *test) { int s, buf, sz; #ifdef SO_RCVTIMEO struct timeval tv; #endif /* Create and bind our local socket. */ if ((s = netdial(test->settings->domain, Pudp, test->bind_address, test->server_hostname, test->server_port)) < 0) { i_errno = IESTREAMCONNECT; return -1; } /* * Set socket buffer size if requested. Do this for both sending and * receiving so that we can cover both normal and --reverse operation. */ int opt; if ((opt = test->settings->socket_bufsize)) { if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)) < 0) { i_errno = IESETBUF; return -1; } if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt)) < 0) { i_errno = IESETBUF; return -1; } } #ifdef SO_RCVTIMEO /* 30 sec timeout for a case when there is a network problem. */ tv.tv_sec = 30; tv.tv_usec = 0; setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&tv, sizeof(struct timeval)); #endif /* * Write a datagram to the UDP stream to let the server know we're here. * The server learns our address by obtaining its peer's address. */ buf = 123456789; /* this can be pretty much anything */ if (write(s, &buf, sizeof(buf)) < 0) { // XXX: Should this be changed to IESTREAMCONNECT? i_errno = IESTREAMWRITE; return -1; } /* * Wait until the server replies back to us. */ if ((sz = recv(s, &buf, sizeof(buf), 0)) < 0) { i_errno = IESTREAMREAD; return -1; } return s; }
void proxytask(void *v) { int fd, remotefd; fd = (int)v; if((remotefd = netdial(TCP, server, port)) < 0) { close(fd); return; } fprintf(stderr, "connected to %s:%d\n", server, port); taskcreate(rwtask, mkfd2(fd, remotefd), STACK); taskcreate(rwtask, mkfd2(remotefd, fd), STACK); }
void fetchtask(ltctx *lt, void *v) { int fd, n; char buf[512]; fprintf(stderr, "starting...\n"); for(;;) { if((fd = netdial(lt, TCP, server, 80)) < 0) { fprintf(stderr, "dial %s: %s (%s)\n", server, strerror(errno), taskgetstate(lt)); continue; } snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n", url, server); fdwrite(lt, fd, buf, strlen(buf)); while((n = fdread(lt, fd, buf, sizeof buf)) > 0) ; close(fd); write(1, ".", 1); } }
void fetchtask(void *v) { int fd, n; char buf[512]; fprintf(stderr, "starting...\n"); for (; ;) { if ((fd = netdial(TCP, server, 80)) < 0) { // 异步连接服务器, 会造成协程切换 fprintf(stderr, "dial %s: %s (%s)\n", server, strerror(errno), taskgetstate()); continue; } snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n", url, server); fdwrite(&fd, buf, strlen(buf)); // 异步数据读写, 这里可能会造成协程切换, 因为一定有阻塞操作 while ((n = fdread(&fd, buf, sizeof buf)) > 0) { // 异步读取 buf[n] = '\0'; printf("buf:%s", buf); } close(fd); write(1, ".", 1); } }