/* * FtpClose - close a data connection */ GLOBALDEF int FtpClose(netbuf *nData) { netbuf *ctrl; switch (nData->dir) { case FTPLIB_WRITE: /* potential problem - if buffer flush fails, how to notify user? */ if (nData->buf != NULL) writeline(NULL, 0, nData); case FTPLIB_READ: if (nData->buf) free(nData->buf); shutdown(nData->handle,2); net_close(nData->handle); ctrl = nData->ctrl; free(nData); if (ctrl) { ctrl->data = NULL; return(readresp('2', ctrl)); } return 1; case FTPLIB_CONTROL: if (nData->data) { nData->ctrl = NULL; FtpClose(nData); } net_close(nData->handle); free(nData); return 0; } return 1; }
s32 create_server(u16 port) { s32 server = net_socket(AF_INET, SOCK_STREAM, IPPROTO_IP); if (server < 0) return -1; set_blocking(server, false); struct sockaddr_in bindAddress; memset(&bindAddress, 0, sizeof(bindAddress)); bindAddress.sin_family = AF_INET; bindAddress.sin_port = htons(port); bindAddress.sin_addr.s_addr = htonl(INADDR_ANY); s32 ret; if ((ret = net_bind(server, (struct sockaddr *)&bindAddress, sizeof(bindAddress))) < 0) { net_close(server); gxprintf("Error binding socket: [%i] %s\n", -ret, strerror(-ret)); return ret; } if ((ret = net_listen(server, 3)) < 0) { net_close(server); gxprintf("Error listening on socket: [%i] %s\n", -ret, strerror(-ret)); return ret; } return server; }
static s32 tcp_socket(void) { s32 s, res; s = net_socket(PF_INET, SOCK_STREAM, IPPROTO_IP); if (s < 0) return s; if(split_res == 2){ return s; } res = net_fcntl(s, F_GETFL, 0); if (res < 0) { net_close(s); return res; } res = net_fcntl(s, F_SETFL, res | IOS_O_NONBLOCK); if (res < 0) { net_close(s); return res; } return s; }
int net_create_client(const char *host, int port, sock_t *s) { sock_t rs; struct sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = htons(port); if (!host) { addr.sin_addr.s_addr = htonl(INADDR_ANY); } else { addr.sin_addr.s_addr = inet_addr(host); } rs = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (rs == -1) { logmsg("ERROR Create client socket - %d:%s\n", errno, strerror(errno)); return -1; } if (net_socket_block(rs, 0) != 0) { logmsg("Error Set client socket non-block\n"); net_close(rs); return -1; } if (connect(rs, (const struct sockaddr *)&addr, sizeof(addr))==-1) { int ec = errno; if (ec != EINPROGRESS) { logmsg("ERROR Connect to remote host - %d:%s\n", errno, strerror(errno)); net_close(rs); return -1; } } *s = rs; return 0; }
int CTTLS::closeSocket() { if((!iConnected && iPeerClosed==0) || iClosed) { if(iNeedCallCloseSocket && pSSL && ((T_SSL*)pSSL)->sock) { SOCKET server_fd=((T_SSL*)pSSL)->sock; net_close(server_fd); } iNeedCallCloseSocket=0; return 0; } addrConnected.clear(); iPeerClosed=0; iConnected=0; iClosed=1; ssl_context *ssl=&((T_SSL*)pSSL)->ssl; ssl_close_notify( ssl ); Sleep(60); SOCKET server_fd=((T_SSL*)pSSL)->sock; net_close( server_fd ); Sleep(80); if(ssl)ssl_free( ssl ); ((T_SSL*)pSSL)->sock=0; iNeedCallCloseSocket=0; return 0; }
s32 tcp_socket (void) { s32 s, res; s = net_socket (PF_INET, SOCK_STREAM, 0); if (s < 0) { printDebugMsg(NORMAL_DEBUG_MESSAGE,"net_socket failed: %d\n", s); return s; } res = net_fcntl (s, F_GETFL, 0); if (res < 0) { printDebugMsg(NORMAL_DEBUG_MESSAGE,"F_GETFL failed: %d\n", res); net_close (s); return res; } res = net_fcntl (s, F_SETFL, res | 4); if (res < 0) { printDebugMsg(NORMAL_DEBUG_MESSAGE,"F_SETFL failed: %d\n", res); net_close (s); return res; } return s; }
/* ==================== NET_Config A single player game will only use the loopback code ==================== */ void NET_Config (qboolean multiplayer) { int i; if (!multiplayer) { // shut down any existing sockets for (i=0 ; i<2 ; i++) { if (ip_sockets[i]) { // >>> FIX: For Nintendo Wii using devkitPPC / libogc // Switching to the equivalent function in the library: //close (ip_sockets[i]); net_close (ip_sockets[i]); // <<< FIX ip_sockets[i] = 0; } if (ipx_sockets[i]) { // >>> FIX: For Nintendo Wii using devkitPPC / libogc // Switching to the equivalent function in the library: //close (ipx_sockets[i]); net_close (ipx_sockets[i]); // <<< FIX ipx_sockets[i] = 0; } } } else { // open sockets NET_OpenIP (); NET_OpenIPX (); } }
s32 tcp_socket (void) { s32 s, res; s = net_socket (PF_INET, SOCK_STREAM, 0); if (s < 0) { printf ("net_socket failed: %d\n", s); return s; } res = net_fcntl (s, F_GETFL, 0); if (res < 0) { printf ("F_GETFL failed: %d\n", res); net_close (s); return res; } res = net_fcntl (s, F_SETFL, res | 4); if (res < 0) { printf ("F_SETFL failed: %d\n", res); net_close (s); return res; } return s; }
static s32 tcp_socket(void) { s32 s, res; s = net_socket(PF_INET, SOCK_STREAM, IPPROTO_IP); if (s < 0) return s; // Switch off Nagle with TCP_NODELAY u32 nodelay = 1; net_setsockopt(s,IPPROTO_TCP,TCP_NODELAY,&nodelay,sizeof(nodelay)); res = net_fcntl(s, F_GETFL, 0); if (res < 0) { net_close(s); return res; } res = net_fcntl(s, F_SETFL, res | IOS_O_NONBLOCK); if (res < 0) { net_close(s); return res; } return s; }
/**************************************************************************** * NetworkWait ***************************************************************************/ int NetworkWait() { if (!checkincomming) return -3; struct sockaddr_in sin; struct sockaddr_in client_address; socklen_t addrlen = sizeof(client_address); //Open socket socket = net_socket(AF_INET, SOCK_STREAM, IPPROTO_IP); if (socket == INVALID_SOCKET) { return socket; } sin.sin_family = AF_INET; sin.sin_port = htons(PORT); sin.sin_addr.s_addr = htonl(INADDR_ANY); if (net_bind(socket, (struct sockaddr*)&sin, sizeof(sin)) < 0) { net_close(socket); return -1; } if (net_listen(socket, 3) < 0) { net_close(socket); return -1; } connection = net_accept(socket, (struct sockaddr*)&client_address, &addrlen); sprintf(incommingIP, "%s", inet_ntoa(client_address.sin_addr)); if (connection < 0) { net_close(connection); net_close(socket); return -4; } else { unsigned char haxx[9]; //skip haxx net_read(connection, &haxx, 8); wiiloadVersion[0] = haxx[4]; wiiloadVersion[1] = haxx[5]; net_read(connection, &infilesize, 4); if (haxx[4] > 0 || haxx[5] > 4) { net_read(connection, &uncfilesize, 4); // Compressed protocol, read another 4 bytes } waitforanswer = true; checkincomming = false; networkHalt = true; } return 1; }
void CloseConnection() { net_close(connection); if (waitforanswer) { net_close(socket); waitforanswer = false; } }
void NetReceiver::CloseConnection() { if(connection >= 0) net_close(connection); if(socket >= 0) net_close(socket); connection = -1; socket = -1; }
/* * FtpAcceptConnection - accept connection from server * * return 1 if successful, 0 otherwise */ static int FtpAcceptConnection (netbuf *nData, netbuf *nControl) { int sData; struct sockaddr addr; unsigned int l; int i; struct timeval tv; fd_set mask; int rv; FD_ZERO(&mask); FD_SET(nControl->handle, &mask); FD_SET(nData->handle, &mask); tv.tv_usec = 0; tv.tv_sec = ACCEPT_TIMEOUT; i = nControl->handle; if (i < nData->handle) i = nData->handle; i = select(i + 1, &mask, NULL, NULL, &tv); if (i == -1) { strncpy(nControl->response, strerror(errno), sizeof (nControl->response)); net_close(nData->handle); nData->handle = 0; rv = 0; } else if (i == 0) { strcpy(nControl->response, "timed out waiting for connection"); net_close(nData->handle); nData->handle = 0; rv = 0; } else { if (FD_ISSET(nData->handle, &mask)) { l = sizeof (addr); sData = accept(nData->handle, &addr, &l); i = errno; net_close(nData->handle); if (sData > 0) { rv = 1; nData->handle = sData; } else { strncpy(nControl->response, strerror(i), sizeof (nControl->response)); nData->handle = 0; rv = 0; } } else if (FD_ISSET(nControl->handle, &mask)) { net_close(nData->handle); nData->handle = 0; readresp('2', nControl); rv = 0; } } return rv; }
s32 tcp_connect (char *host, const u16 port) { struct hostent *hp; struct sockaddr_in sa; s32 s, res; s64 t; hp = net_gethostbyname (host); if (!hp || !(hp->h_addrtype == PF_INET)) { printDebugMsg(NORMAL_DEBUG_MESSAGE,"net_gethostbyname failed: %d\n", errno); return errno; } s = tcp_socket (); if (s < 0) return s; memset (&sa, 0, sizeof (struct sockaddr_in)); sa.sin_family= PF_INET; sa.sin_len = sizeof (struct sockaddr_in); sa.sin_port= htons (port); memcpy ((char *) &sa.sin_addr, hp->h_addr_list[0], hp->h_length); t = gettime (); while (true) { if (ticks_to_millisecs (diff_ticks (t, gettime ())) > TCP_CONNECT_TIMEOUT) { printDebugMsg(NORMAL_DEBUG_MESSAGE,"tcp_connect timeout\n"); net_close (s); return -ETIMEDOUT; } res = net_connect (s, (struct sockaddr *) &sa, sizeof (struct sockaddr_in)); if (res < 0) { if (res == -EISCONN) break; if (res == -EINPROGRESS || res == -EALREADY) { usleep (20 * 1000); continue; } printDebugMsg(NORMAL_DEBUG_MESSAGE,"net_connect failed: %d\n", res); net_close (s); return res; } break; } return s; }
static void admin_close_session(void) { if (_admin_session) { net_close(_admin_session); _admin_session = 0; } }
void WifiGecko_Close() { if(connection >= 0) net_close(connection); connection = -1; }
int main(int argc, char *argv[]) { int sockfd, port; char host[200]; char msg[200], *p, *cmd; strcpy(host, SERV_HOST_ADDR); port = SERV_TCP_PORT; if (argc > 1) { strcpy(host, argv[1]); /* get host from command line */ p = strchr(host, ':'); if (p) { *p++ = 0; port = atoi(p); } } if (argc > 2) { cmd = argv[2]; } else { cmd = NULL; } if ((sockfd = net_open(host, NULL, port)) < 0) { sprintf(msg, "client: tcp_open for host %s on %d failed\n", host, port); error_abort(msg); } handle_client(stdin, sockfd, cmd); /* do it all */ net_close(sockfd); exit(0); }
s32 ConnectSocket(const char *hostname, u32 port) { s32 socket = 0; struct sockaddr_in connect_addr; if ( (socket = net_socket(AF_INET,SOCK_STREAM,IPPROTO_IP)) < 0) { return -1; } else { //resolving hostname struct hostent *host = 0; host = net_gethostbyname(hostname); if ( host == NULL ) { //resolving failed return -2; } else { connect_addr.sin_family = AF_INET; connect_addr.sin_port = port; memcpy(&connect_addr.sin_addr, host->h_addr_list[0], host->h_length); } } if (net_connect(socket, (struct sockaddr*)&connect_addr , sizeof(connect_addr)) == -1 ) { net_close(socket); return -3; } return socket; }
int close(int fd) { if (fd & SOCKET_FD_MASK) return net_close(fd); return lv2Errno(lv2FsClose(fd)); }
net_http_handle *net_http_open(char *host, char *request) { struct net_http_handle *h = malloc(sizeof(struct net_http_handle)); h->socket = net_connect(host, 80); if (!h->socket) { free(h); return NULL; } int len = snprintf(h->buf, BUF_SIZE, "GET %s HTTP/1.1\r\n" "Host: %s\r\n" "Accept-Encoding:\r\n" "Connection: close\r\n\r\n", request, host); if (len >= BUF_SIZE || net_write(h->socket, h->buf, len) < len) { DBG_WARN("failed to send \"%s\" to %s", request, host); net_close(h->socket); free(h); return NULL; } h->data = NULL; h->len = 0; h->complete = 0; memset(&h->settings, 0, sizeof(http_parser_settings)); h->settings.on_body = on_body_cb; h->settings.on_message_complete = on_message_complete_cb; h->parser = malloc(sizeof(http_parser)); h->parser->data = h; http_parser_init(h->parser, HTTP_RESPONSE); return h; }
s32 Network_Connect(void) { struct hostent *he; struct sockaddr_in sa; s32 ret; /* Close socket if it is already open */ if (sockfd >= 0) net_close(sockfd); /* Create socket */ sockfd = net_socket(AF_INET, SOCK_STREAM, IPPROTO_IP); if (sockfd < 0) return sockfd; /* Get host by name */ he = net_gethostbyname(NusHostname); if (!he) return -1; /* Setup socket */ memcpy(&sa.sin_addr, he->h_addr_list[0], he->h_length); sa.sin_family = AF_INET; sa.sin_port = htons(NusPort); ret = net_connect(sockfd, (struct sockaddr *) &sa, sizeof(sa)); if (ret < 0) return ret; return 0; }
/* * HttpQuit - disconnect from remote * * return 1 if successful, 0 otherwise */ GLOBALREF void HttpQuit(netbuf *nControl) { if(nControl) { net_close(nControl->handle); free(nControl); } }
int net_create_server(const char *host, int port, sock_t *s) { sock_t rs; struct sockaddr_in addr; int opt_reuseaddr = 1; addr.sin_family = AF_INET; addr.sin_port = htons(port); if (!host) { addr.sin_addr.s_addr = htonl(INADDR_ANY); } else { addr.sin_addr.s_addr = inet_addr(host); } rs = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (rs == -1) { logmsg("ERROR Create server socket - %d:%s\n", errno, strerror(errno)); return -1; } if (setsockopt(rs, SOL_SOCKET, SO_REUSEADDR, &opt_reuseaddr, sizeof(opt_reuseaddr))==-1) { logmsg("ERROR Setopt(REUSEADDR) server socket - %d:%s\n", errno, strerror(errno)); net_close(rs); return -1; } if (bind(rs, (const struct sockaddr*)&addr, sizeof(addr))==-1) { logmsg("ERROR Bind server socket - %d:%s\n", errno, strerror(errno)); net_close(rs); return -1; } if (listen(rs, SOMAXCONN)==-1) { logmsg("ERROR Listen on server socket - %d:%s\n", errno, strerror(errno)); net_close(rs); return -1; } if (net_socket_block(rs, 0) != 0) { logmsg("ERROR Set server socket non-block\n"); net_close(rs); return -1; } *s = rs; return 0; }
void net_on_accept(struct net_connection* con, int event, void *arg) { struct hub_info* hub = (struct hub_info*) arg; struct hub_probe* probe = 0; struct ip_addr_encap ipaddr; int server_fd = net_con_get_sd(con); #ifdef PLUGIN_SUPPORT plugin_st status; #endif for (;;) { int fd = net_accept(server_fd, &ipaddr); if (fd == -1) { if (net_error() == EWOULDBLOCK) { break; } else { LOG_ERROR("Accept error: %d %s", net_error(), strerror(net_error())); break; } } #ifdef PLUGIN_SUPPORT status = plugin_check_ip_early(hub, &ipaddr); if (status == st_deny) { plugin_log_connection_denied(hub, &ipaddr); net_close(fd); continue; } plugin_log_connection_accepted(hub, &ipaddr); #endif probe = probe_create(hub, fd, &ipaddr); if (!probe) { LOG_ERROR("Unable to create probe after socket accepted. Out of memory?"); net_close(fd); break; } } }
void close(){ net_close(); input_close(); physics_close(); console_close(); test_logic_close(); // the functions are smart enough to not close if they never initialized }
int net_free(net_t * net) { if (!net_close(net, net_free_cb) && net != NULL) { free(net); net = NULL; } return NET_OK; }
/* * FtpQuit - disconnect from remote * * return 1 if successful, 0 otherwise */ GLOBALDEF void FtpQuit (netbuf *nControl) { if (nControl->dir != FTPLIB_CONTROL) return; FtpSendCmd("QUIT", '2', nControl); net_close(nControl->handle); free(nControl->buf); free(nControl); }
static s32 helper_closetcpip( DebugHelper_t *helper) { net_close( helper->clientsock); helper->clientsock = -1; return 0; }
int main(int argc, char **argv) { struct net_handle* file_ctx = NULL; struct timeval start,end; char rcv_buf[MAX_BUFSIZE]; if (argc < 3) { printf("usage:need argument ip_address port_num\n"); exit(-1); } int rvl=0; int port_num= strtoul(argv[2],NULL,0); zlog_init("./zlog.conf"); rvl = net_module_init(NULL); assert(!rvl); file_ctx = net_open(argv[1],port_num,4,16,SPK_DIR_WRITE,net_intf_tcp); if (!file_ctx) { printf("failed to open net\n"); rvl = SPKERR_BADRES; goto out; } rvl= net_intf_is_connected(file_ctx); if(!rvl) { printf("net is not connected!\n"); goto out; } uint64_t xfer=0; gettimeofday(&start, NULL); uint64_t total_count=0; while (1) { xfer = net_write(file_ctx, rcv_buf, MAX_BUFSIZE); if(xfer != MAX_BUFSIZE) { printf("send data error=>xfer:%ld\n",xfer); } else { // printf("write OK!\n"); } total_count++; gettimeofday(&end, NULL); float diff=(end.tv_sec-start.tv_sec)+(end.tv_usec - start.tv_usec)/1000000.0; if(diff > 5) { float speed=(total_count*MAX_BUFSIZE)/1024/diff; printf("time:%-15f s speed:%-15f KB/s\n",diff,speed); gettimeofday(&start, NULL); total_count=0; } } out: if (file_ctx) { net_close(file_ctx); file_ctx = NULL; } return rvl; }
static struct net_connection* start_listening_socket(const char* bind_addr, uint16_t port, int backlog, struct hub_info* hub) { struct net_connection* server; struct sockaddr_storage addr; socklen_t sockaddr_size; int sd, ret; if (ip_convert_address(bind_addr, port, (struct sockaddr*) &addr, &sockaddr_size) == -1) { return 0; } sd = net_socket_create(addr.ss_family, SOCK_STREAM, IPPROTO_TCP); if (sd == -1) { return 0; } if ((net_set_reuseaddress(sd, 1) == -1) || (net_set_nonblocking(sd, 1) == -1)) { net_close(sd); return 0; } ret = net_bind(sd, (struct sockaddr*) &addr, sockaddr_size); if (ret == -1) { LOG_ERROR("hub_start_service(): Unable to bind to TCP local address. errno=%d, str=%s", net_error(), net_error_string(net_error())); net_close(sd); return 0; } ret = net_listen(sd, backlog); if (ret == -1) { LOG_ERROR("hub_start_service(): Unable to listen to socket"); net_close(sd); return 0; } server = net_con_create(); net_con_initialize(server, sd, net_on_accept, hub, NET_EVENT_READ); return server; }