static int _transfer(struct in_addr *addr, uint8_t *wbuf, size_t wlen, uint8_t *rbuf, size_t rlen) { struct sockaddr_in servaddr; socklen_t slen = sizeof(servaddr); int ret; int sockfd = open_udp_clientfd(); make_sockaddr(&servaddr, addr, VIRTUAL_SWITCH_LISTEN_PORT); sendto(sockfd, wbuf, wlen, 0, (struct sockaddr *)&servaddr, slen); struct timeval tv = { 3, 0 }; ret = recvfrom_timeout(sockfd, rbuf, rlen, NULL, NULL, &tv); if (ret <= 0) { hsb_critical("_transfer: get err pkt, len=%d\n", ret); close(sockfd); return -1; } close(sockfd); return ret; }
static void connect2dst() { int ret; if (the_working_paras.src_ip) the_working_paras.sockfd = tcp_socket_init(the_working_paras.src_ip, the_working_paras.src_port); else the_working_paras.sockfd = tcp_socket_init_no_addr(); if (the_working_paras.sockfd<0) { ERR_DBG_PRINT_QUIT("create socket on %s:%d failed ", the_working_paras.src_ip, (int)the_working_paras.src_port); } make_sockaddr(&the_working_paras.dst_sock_addr, inet_addr(the_working_paras.dst_ip), htons(the_working_paras.dst_port)); ret=connect(the_working_paras.sockfd ,(void *)&the_working_paras.dst_sock_addr ,sizeof(struct sockaddr_in)); if (ret<0) { ERR_DBG_PRINT_QUIT("connect to %s:%d failed ", the_working_paras.dst_ip, (int)the_working_paras.dst_port); } set_fd_nonblock(the_working_paras.sockfd); DBG_PRINT("connect to %s:%d succeed!", the_working_paras.dst_ip, (int)the_working_paras.dst_port); }
SOCKET udpsock_server(int port, const char* addr) { #if defined(WIN32) WSADATA wsaData; int nResult = WSAStartup(MAKEWORD(2, 2), &wsaData); if(nResult != NO_ERROR) { std::cout << "WSAStartup failed with error: " << nResult << std::endl; return 1; } #endif int handle = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP ); if (handle < 1) return -1; sockaddr_in address = make_sockaddr(addr, port); if ( bind( handle, (const sockaddr*) &address, sizeof(sockaddr_in) ) < 0 ) return -1; return handle; }
extent_client::extent_client(std::string dst) { sockaddr_in dstsock; make_sockaddr(dst.c_str(), &dstsock); cl = new rpcc(dstsock); if (cl->bind() != 0) { printf("extent_client: bind failed\n"); } }
/* bind the socket */ bool SocketAPM::bind(const char *address, uint16_t port) { struct sockaddr_in sockaddr; make_sockaddr(address, port, sockaddr); if (::bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) != 0) { return false; } return true; }
int udpsock_senddata(SOCKET sock, const char * addr, int port, unsigned char * data, int size) { sockaddr_in dest = make_sockaddr(addr, port); int ret = sendto(sock, (char *)data, size, 0, (sockaddr*)&dest, sizeof(dest)); if (ret == -1) { #if defined(WIN32) std::cout << "\nSend Error Code : " << WSAGetLastError(); #else std::cout << "\nSend Error\n"; #endif } return ret; }
rsm_client::rsm_client(std::string dst) { printf("create rsm_client\n"); std::vector<std::string> mems; pthread_mutex_init(&rsm_client_mutex, NULL); sockaddr_in dstsock; make_sockaddr(dst.c_str(), &dstsock); primary = dst; { ScopedLock ml(&rsm_client_mutex); VERIFY (init_members()); } printf("rsm_client: done\n"); }
/* send some data */ ssize_t SocketAPM::sendto(const void *buf, size_t size, const char *address, uint16_t port) { struct sockaddr_in sockaddr; make_sockaddr(address, port, sockaddr); return ::sendto(fd, buf, size, 0, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); }
int socks5_connect(int client_sockfd, int *connect_sockfd_p) { uint8_t buffer[4]; if (read_all(client_sockfd, buffer, 4)) return -2; unsigned char ver = buffer[0]; unsigned char cmd = buffer[1]; unsigned char reserved = buffer[2]; unsigned char addr_type = buffer[3]; if (ver != SOCKS_VER) return -1; if (reserved != 0) return -1; if (!(addr_type == ATYP_IPV4 || addr_type == ATYP_DOMAIN || addr_type == ATYP_IPV6)) return -1; static address_union address; // thread unsafe switch (addr_type) { case ATYP_IPV4: { if (read_all(client_sockfd, address.ipv4, 4)) return -2; break; } case ATYP_IPV6: { if (read_all(client_sockfd, address.ipv6, 16)) return -2; break; } case ATYP_DOMAIN: { if (read_all(client_sockfd, buffer, 1)) return -2; unsigned char domain_name_len = buffer[0]; if (domain_name_len == 0) return -1; assert(sizeof(char) == 1); if (read_all(client_sockfd, address.domain_name, domain_name_len)) return -2; address.domain_name[domain_name_len] = 0; break; } } if (read_all(client_sockfd, buffer, 2)) return -2; unsigned int port = ntohs(*(uint16_t *)buffer); uint8_t resp_buf[10] = { SOCKS_VER, 0/*rep*/, 0/*rsv*/, ATYP_IPV4, 0, 0, 0, 0/*bind.ipv4*/, 0, 0/*bind.port*/ }; unsigned char rep; if (cmd == CMD_CONNECT) { rep = REP_SUCCEEDED; struct sockaddr *connect_addr; socklen_t connect_addr_len; int ret = make_sockaddr(addr_type, &address, port, &connect_addr, &connect_addr_len); assert(ret >= 0); if (ret > 0) { rep = ret; } else { int connect_sockfd = socket(connect_addr->sa_family, SOCK_STREAM, 0); if (connect_sockfd < 0) perror_and_exit("socket"); if (connect(connect_sockfd, connect_addr, connect_addr_len)) { if (errno == ECONNREFUSED || errno == ENETUNREACH || errno == ETIMEDOUT) { switch (errno) { case ECONNREFUSED: rep = REP_CONNECTION_REFUSED; break; case ETIMEDOUT: rep = REP_HOST_UNREACHABLE; break; case ENETUNREACH: rep = REP_NETWORK_UNREACHABLE; break; } if (close(connect_sockfd)) perror_and_exit("close"); } else perror_and_exit("connect"); } else { *connect_sockfd_p = connect_sockfd; } free(connect_addr); } } else { rep = REP_COMMAND_NOT_SUPPORTED; } resp_buf[1] = rep; if (write_all(client_sockfd, resp_buf, sizeof(resp_buf))) { if (rep == REP_SUCCEEDED) { if (close(*connect_sockfd_p)) perror_and_exit("close"); } return -2; } return (rep == REP_SUCCEEDED ? 0 : 1); }
/** * creates a new server socket, and returns the new server socket's file * descriptor. * * @function make_tcp_server_socket * * @date 2015-03-05 * * @revision none * * @designer Eric Tsang * * @programmer Eric Tsang * * @note none * * @signature int make_tcp_server_socket(short port) * * @param port port number on local host to bind the new server socket to. * * @param isNonBlocking true if the socket should be put into non blocking * mode; false otherwise. * * @return socket file descriptor to the new server socket. may return -1 on * binding error. */ struct socket_t make_tcp_server_socket(short port, bool isNonBlocking) { // local address that server socket is bound to struct sockaddr localAddr; // socket file descriptor to the new server socket int svrSock; // create TCP socket if((svrSock = socket(AF_INET, SOCK_STREAM, 0)) == -1) { fatal_error("failed to create TCP socket"); } // set sock opt to reuse address int arg = 1; if(setsockopt(svrSock,SOL_SOCKET,SO_REUSEADDR,&arg,sizeof(arg)) == -1) { fatal_error("failed to set sock opt to reuse address"); } // set sock opt to not linger after close { struct linger linger; memset(&linger,0,sizeof(linger)); linger.l_onoff = 1; linger.l_linger = 0; if (setsockopt(svrSock,SOL_SOCKET,SO_LINGER,(char*) &linger,sizeof(linger)) == -1) { fatal_error("failed to set sock opt to not linger"); } } // make the server listening socket non-blocking if (isNonBlocking) { int existingFlags = fcntl(svrSock,F_GETFL,0); if (existingFlags == -1 || fcntl(svrSock,F_SETFL,O_NONBLOCK|existingFlags) == -1) { fatal_error("fcntl"); } } // bind server socket to local host localAddr = make_sockaddr(0, INADDR_ANY, port); if(bind(svrSock, (struct sockaddr*) &localAddr, sizeof(localAddr)) == -1) { perror("failed to bind server socket to address structure"); close(svrSock); svrSock = -1; } // put server socket into listening mode if (listen(svrSock, LISTENQ) == -1) { fatal_error("listen"); } // return... struct socket_t socket; memset(&socket,0,sizeof(socket)); socket.fd = svrSock; socket.localAddr = localAddr; return socket; }
/** * craetes a new socket that is connected to the specified remote host. * * @function make_tcp_client_socket * * @date 2015-03-05 * * @revision none * * @designer Eric Tsang * * @programmer Eric Tsang * * @note none * * @signature int make_tcp_client_socket(char* remoteName, long remoteAddr, * short remotePort, short localPort) * * @param remoteName name of the remote host. either this, or {remoteAddr} * needs to be specified; one of them can be 0, but not both. * @param remoteAddr address in host byte ordering of the remote host. * either this, or {remoteName} needs to be specified; one of them can be 0, * but not both. * @param remotePort the remote host's port. * @param localPort the local port. can be 0 if you don't care. * @param isNonBlocking true if the socket should be put into non blocking * mode; false otherwise. * * @return socket file descriptor to the new connected client socket. may * return -1 on error. */ struct socket_t make_tcp_client_socket(char* remoteName, long remoteAddr, short remotePort, short localPort, bool isNonBlocking) { // local address that client socket is bound to struct sockaddr local; // remote address that client socket should connect to struct sockaddr remote; // socket file descriptor to the new client socket int clntSock; // create TCP socket if((clntSock = socket(AF_INET, SOCK_STREAM, 0)) == -1) { fatal_error("failed to create TCP socket"); } // set sock opt to reuse address { int arg = 1; if(setsockopt(clntSock,SOL_SOCKET,SO_REUSEADDR,&arg,sizeof(arg)) == -1) { fatal_error("failed to set sock opt to reuse address"); } } // set sock opt to not linger after close { struct linger linger; memset(&linger,0,sizeof(linger)); linger.l_onoff = 1; linger.l_linger = 0; if (setsockopt(clntSock,SOL_SOCKET,SO_LINGER,(char*) &linger,sizeof(linger)) == -1) { fatal_error("failed to set sock opt to not linger"); } } // bind socket to local host if a local port is specified if(clntSock > 0 && localPort) { local = make_sockaddr(0, INADDR_ANY, localPort); if(bind(clntSock, (struct sockaddr*) &local, sizeof(local)) == -1) { perror("failed to bind socket to local host"); close(clntSock); clntSock = -1; } } // make the server listening socket non-blocking if specified if (clntSock > 0 && isNonBlocking) { int existingFlags = fcntl(clntSock, F_GETFL,0); if (existingFlags == -1 || fcntl(clntSock, F_SETFL, O_NONBLOCK | existingFlags) == -1) { fatal_error("failed to make socket non-blocking"); } } // connect socket to remote host remote = make_sockaddr(remoteName, remoteAddr, remotePort); if (clntSock > 0 && connect(clntSock, (struct sockaddr*) &remote, sizeof(remote)) == -1) { // check for fatal error. ignore EINPROGRESS error if socket is // non-blocking because this is expected. if (isNonBlocking && errno == EINPROGRESS) { errno = 0; } // propagate error otherwise else { perror("failed to connect to remote host"); close(clntSock); clntSock = -1; } } // return... struct socket_t socket; memset(&socket,0,sizeof(socket_t)); socket.fd = clntSock; socket.localAddr = local; socket.remoteAddr = remote; return socket; }