static int tcp_do_accept(struct socket * listen_sock, message * m, struct tcp_pcb * newpcb) { struct socket * newsock; unsigned sock_num; int ret; debug_tcp_print("socket num %ld", get_sock_num(listen_sock)); if ((ret = copy_from_user(m->m_source, &sock_num, sizeof(sock_num), (cp_grant_id_t) m->IO_GRANT, 0)) != OK) return EFAULT; if (!is_valid_sock_num(sock_num)) return EBADF; newsock = get_sock(sock_num); assert(newsock->pcb); /* because of previous open() */ /* we really want to forget about this socket */ tcp_err((struct tcp_pcb *)newsock->pcb, NULL); tcp_abandon((struct tcp_pcb *)newsock->pcb, 0); tcp_arg(newpcb, newsock); tcp_err(newpcb, tcp_error_callback); tcp_sent(newpcb, tcp_sent_callback); tcp_recv(newpcb, tcp_recv_callback); tcp_nagle_disable(newpcb); tcp_accepted(((struct tcp_pcb *)(listen_sock->pcb))); newsock->pcb = newpcb; debug_tcp_print("Accepted new connection using socket %d\n", sock_num); return OK; }
/******************************************************************//** * @brief Send data to udp port from queued buffer (ssBuffer) * @param[in] buf - pointer to queue for sending data * @retval err_result_ok - execution was successful * @retval err_result_error - problems with udp sendto function **********************************************************************/ errType deqUdp::sendData(ssBuffer *buf) { /// @todo not needed to realize all sendData check ability to use base method sendData of parent class // if (verbose_level) printf("udp_port::sendData()\n"); int flags=0; errType result=err_result_ok; sockaddr_in host_ip; WORD sock=get_sock(); BYTE *sendData; DWORD len=buf->getFrontBlockSize(); memset(&host_ip,0,sizeof(host_ip)); sendData=new BYTE[len]; buf->popBlock(&host_ip, sendData); printf("Отправка пакета: содержимое ["); for (size_t i=0; i<len; i++) printf("%.2X ", sendData[i]); printf("]\n\t\t\t адресат: %s, сокет %d/udp\n\n",inet_ntoa(host_ip.sin_addr),ntohs(host_ip.sin_port)); ssize_t sended=0; sended=sendto(sock, sendData, len, flags,(sockaddr*) &host_ip, sizeof(host_ip)); // if (verbose_level) printf("data length=%d want to be sent via udp port %d, sended=%d\n",(int)len,ntohs(ip_addr.sin_port),(int)sended); if (sended==-1) { // if (verbose_level) printf("errno=%d\n",errno); result=err_result_error; } delete []sendData; return result; }
static void clear_lost_client(int sindex) { int cli ; for (cli = 0; cli < RH_MAX_CLIENTS; cli++) { if (!is_used(sindex, cli) && is_sock(sindex, cli)) { close(get_sock(sindex, cli)); set_sock(sindex, cli, INVALID_SOCKET); } } }
int UDP_sock(in_port_t port){ int sock; struct sockaddr_in Local; sock = get_sock(SOCK_DGRAM); set_sock(sock); name_sock(&Local, htonl(INADDR_ANY), port); bind_sock(sock, &Local); printf("Socket UDP sulla porta %d creato\n", ntohs(port)); return sock; }
/* @nport: net order port */ struct sock *udp_lookup_sock(unsigned short nport) { struct hlist_head *head = udp_slot_head(_ntohs(nport)); struct hlist_node *node; struct sock *sk; /* FIXME: lock for udp hash table */ if (hlist_empty(head)) return NULL; hlist_for_each_sock(sk, node, head) if (sk->sk_sport == nport) return get_sock(sk); return NULL; }
static int open_fd(int type, char *val) { int fd; switch (type) { case ACT_LOCAL: fd = get_sock(val, 0, AF_UNIX, SOCK_DGRAM); break; case ACT_REMOTE: fd = get_sock(val, 514, AF_INET, SOCK_DGRAM); break; case ACT_FILE: fd = open_file(val); break; default: debug(1, "unknown type %i", type); fd = -1; break; } return(fd); }
int TCP_sock_snd(in_addr_t ip, in_port_t port){ int sock; char ipstr[15]; struct sockaddr_in Local, To; inet_ntop(AF_INET, &ip, ipstr,(socklen_t)15); /* conversione a stringa dell'ip per stamparlo */ sock = get_sock(SOCK_STREAM); set_sock(sock); name_sock(&Local, htonl(INADDR_ANY), 0); bind_sock(sock, &Local); name_sock(&To, ip, port); connect_sock(sock, &To); printf ("Connessione TCP con %s avvenuta\n",ipstr); return sock; }
void stream_send_dataToClient(int sindex, int nLen, unsigned char * pData, Stream_Head_t* fh) { int nRet = -1; int cnt = 0; int sendsocket = -1; int fh_len = sizeof(Stream_Head_t); int data_len = nLen; int send_len = 0; for (cnt = 0; cnt < RH_MAX_CLIENTS; cnt++) { if (is_used(sindex, cnt)) { sendsocket = get_sock(sindex, cnt); if (sendsocket > 0) { send_len = fh_len; nRet = socket_tcp_sendNonBlock(sendsocket, (char *)fh, &send_len, 300); if (nRet < 0 || send_len != fh_len) { close(sendsocket); set_client_used(sindex, cnt, FALSE); set_sock(sindex, cnt, INVALID_SOCKET); printf("xxx Error: SOCK = %d count =%d errno=%d<%s> ret = %d\n", sendsocket, cnt, errno,strerror(errno), nRet); continue; } send_len = data_len; nRet = socket_tcp_sendNonBlock(sendsocket, (char *)pData, &send_len, 300); if (nRet < 0 || send_len != data_len) { close(sendsocket); set_client_used(sindex, cnt, FALSE); set_sock(sindex, cnt, INVALID_SOCKET); printf("Error: SOCK = %d count = %d errno = %d ret = %d\n", sendsocket, cnt, errno, nRet); continue; } } } } }
/******************************************************************//** * @brief Read udp port data to queued buffer (ssBuffer) * @param[in] addr - pointer to queue for readed data * @param[in] peek - PEEK MODE * @param[out] len - length of readed data * @param[out] ipaddr - information about data sender * @retval err_result_ok - execution was successful * @retval err_result_error - problems with udp sendto function **********************************************************************/ errType deqUdp::readData(ssBuffer* addr, size_t* len, in_addr *ipaddr,bool peek) { /// @todo not needed to realize all sendData check ability to use base method sendData of parent class // if (verbose_level) printf("udp_port::readData(buffer)\n"); errType result=err_result_ok; int numbytes=0; socklen_t socklen=0; sockaddr_in sfrom;//=new sockaddr_in; bool repeat=false; WORD sock=get_sock(); BYTE tmp_rcvData[*len]; //BYTE* rcvData; socklen=sizeof(sockaddr_in); memset(&sfrom,0,sizeof(sockaddr_in)); sfrom.sin_family=AF_INET; // if (verbose_level) printf("listening %lu bytes on %d port\n",*len,ntohs(ip_addr.sin_port)); do { if (peek) numbytes = recvfrom(sock, tmp_rcvData, *len , MSG_PEEK,(struct sockaddr *)&sfrom, &socklen); else numbytes = recvfrom(sock, tmp_rcvData, *len , 0 ,(struct sockaddr *)&sfrom, &socklen); //printf("packet from %s:%d\n",inet_ntoa(sfrom.sin_addr),ntohs(sfrom.sin_port)); if (((sfrom.sin_addr.s_addr)&0xFF000000)==0) repeat=true; } while (repeat); if ( numbytes == -1) { perror("recvfrom"); result=err_result_error; } else if (numbytes>0) { *len=numbytes; addr->pushBlock(&sfrom, tmp_rcvData, numbytes); // if (verbose_level) addr->dbgPrint(); // } return result; }
int socket_ioctl(int fd, int cmd, int flags) { struct socket *sock; sock = get_sock(fd); if (NULL == sock) return -ENOENT; switch (cmd) { case SKIOCS_FLAGS: sock->obstruct_flags = flags; break; default: return -EINVAL; } return 0; }
int TCP_sock_rcv(in_port_t port){ int sock, sock_rcv; int porta; struct sockaddr_in Local, From; char ipstr[15]; porta = ntohs(port); sock = get_sock(SOCK_STREAM); set_sock(sock); name_sock(&Local, htonl(INADDR_ANY), port); bind_sock(sock, &Local); listen_sock(sock); printf("Attendo una connessione TCP sulla porta %d\n\n", porta); sock_rcv = accept_sock(sock, &From); inet_ntop(AF_INET, &From.sin_addr, ipstr,(socklen_t)15); /* conversione a stringa dell'ip per stamparlo */ printf("Connessione TCP da %s : %d\n\n", ipstr, ntohs(From.sin_port)); return sock_rcv; }
int ftp_transfer(UrlResource *rsrc, libnet_callback notify) { Url *u = NULL; FILE *out = NULL; char *line = NULL; int sock = -1; int data_sock = -1; int passive = 1; int retval = 0; int msg_code = 0; u = rsrc->url; /* first of all, if this is proxied, just pass it off to the http module, since that's how we support proxying. */ rsrc->proxy = get_proxy("FTP_PROXY"); if ( rsrc->proxy ) { return http_transfer(rsrc, notify); } ftp_set_defaults(rsrc, u); sock = util_tcp_connect(u->host, u->port); if (sock < 0) { if (rsrc->running) { notify(NET_MSG_DOWNLOAD_FINISH, (UINT32)-NET_ERR_CONNECT_FAILED); } //S_CLOSE(sock); return 0; } if ( !(line = get_line(rsrc, sock)) ) { if (rsrc->running) { notify(NET_MSG_DOWNLOAD_FINISH, (UINT32)-NET_ERR_FTP_SERVER_ERROR); } S_CLOSE(sock); return 0; } if ( !check_numeric("220", line) ) { safe_free(line); S_CLOSE(sock); LIBNET_DEBUG("bad server greeting"); if (rsrc->running) { notify(NET_MSG_DOWNLOAD_FINISH, (UINT32)-NET_ERR_FTP_SERVER_ERROR); } return 0; } safe_free(line); send_control(sock, "USER ", u->username, "\r\n", NULL); if ( !(line = get_line(rsrc, sock)) ) { msg_code = -NET_ERR_LOGIN_FAILED; goto cleanup; } /* do the password dance */ if ( !check_numeric("230", line) ) { if ( !check_numeric("331", line)) { safe_free(line); LIBNET_DEBUG("bad/unexpected response: %s", line); msg_code = -NET_ERR_LOGIN_FAILED; goto cleanup; } else { safe_free(line); send_control(sock, "PASS ", u->password, "\r\n", NULL); if ( !((line = get_line(rsrc, sock)) && check_numeric("230", line)) ) { safe_free(line); LIBNET_DEBUG("login failed"); msg_code = -NET_ERR_LOGIN_FAILED; goto cleanup; } //safe_free(line); } } safe_free(line); /* set binmode */ send_control(sock, "TYPE I\r\n", NULL); if ( !(line = get_line(rsrc, sock)) ) { msg_code = -NET_ERR_FTP_SERVER_ERROR; goto cleanup; } safe_free(line); #if 1 // user can't change ftp path to "/" if server don't support it if ( u->path && (STRCMP(u->path, "/") != 0)) { /* CWD using relative path */ char *relative_path = u->path[0] == '/' ? &u->path[1] : &u->path[0]; send_control(sock, "CWD ", relative_path, "\r\n", NULL); if ( !((line = get_line(rsrc, sock)) && check_numeric("250", line)) ) { safe_free(line); msg_code = -NET_ERR_OPERATION_NOT_PERMIT; goto cleanup; } safe_free(line); } #endif /* finally, the good stuff */ /* get a socket for reading. try passive first. */ if ( ! (rsrc->options & OPT_ACTIVE) ) { if ( (data_sock = get_passive_sock(rsrc, sock)) < 0 ) { msg_code = -NET_ERR_CONNECT_FAILED; goto cleanup; } } if ( data_sock < 0 ) { if ( (data_sock = get_sock(rsrc, sock)) < 1 ) { msg_code = -NET_ERR_CONNECT_FAILED; goto cleanup; } else passive = 0; } if (u->file) { send_control(sock, "SIZE ", u->file, "\r\n", NULL); line = get_line(rsrc, sock); if (line && check_numeric("213", line)) { rsrc->outfile_size = ATOI(line + 4); } else if (line && check_numeric("550", line)) { safe_free(line); msg_code = -NET_ERR_FILE_NOT_FOUND; goto cleanup; } else { rsrc->outfile_size = 0; } if (line) FREE(line); } /* handle resume */ if ( rsrc->outfile_offset && (rsrc->options & OPT_RESUME) ) { char numstring[32]; /* ugly hack */ sprintf(numstring, "%ld", (long int )rsrc->outfile_offset); send_control(sock, "REST ", numstring, "\r\n", NULL); if ( !((line = get_line(rsrc, sock)) && check_numeric("350", line)) ) { safe_free(line); LIBNET_DEBUG("server does not support FTP resume, try again without -r"); msg_code = -NET_ERR_OPERATION_NOT_PERMIT; goto cleanup; } safe_free(line); } if (u->file) send_control(sock, "RETR ", u->file, "\r\n", NULL); else send_control(sock, "NLST\r\n", NULL); if ( !((line = get_line(rsrc, sock)) && (check_numeric("150", line) || check_numeric("125", line))) ) { safe_free(line); msg_code = -NET_ERR_OPERATION_NOT_PERMIT; goto cleanup; } LIBNET_DEBUG("ftp reply(RETR): %s\n", line); if ( !passive ) data_sock = S_ACCEPT(data_sock, NULL, NULL); /* rsrc->outfile_size = guess_file_size(line); */ safe_free(line); if (rsrc->outfile) { if (get_file_size(rsrc->outfile) > 0) out = fopen(rsrc->outfile, "rb+"); else out = fopen(rsrc->outfile, "wb"); if ( !out ) { //report(ERR, "opening %s: %s", rsrc->outfile, // strerror(errno)); msg_code = -NET_ERR_FILE_SAVE_ERROR; goto cleanup; } } retval = dump_data(rsrc, data_sock, out, notify); if (rsrc->running) { line = get_line(rsrc, sock); /* 226 Transfer complete */ LIBNET_DEBUG("ftp(done): %s\n", line); safe_free(line); } cleanup: send_control(sock, "QUIT\r\n", NULL); line = get_line(rsrc, sock); /* 221 Goodbye */ LIBNET_DEBUG("ftp(QUIT): %s\n", line); safe_free(line); if (out) fclose(out); if (data_sock >= 0) { S_CLOSE(data_sock); } if (sock >= 0) { S_CLOSE(sock); } #ifndef WIN32 if (rsrc->outfile) fs_sync(rsrc->outfile); #endif if (msg_code < 0 && rsrc->running) { notify(NET_MSG_DOWNLOAD_FINISH, (UINT32)msg_code); } return retval; }
int ftp_transfer(UrlResource *rsrc) { Url *u = NULL; char *line = NULL; int sock = 0; int data_sock = 0; int passive = 1; int retval = 0; u = rsrc->url; /* * first of all, if this is proxied, just pass it off to the * http module, since that's how we support proxying. */ rsrc->proxy = get_proxy("FTP_PROXY"); if (rsrc->proxy && (rsrc->proxy[0] != '\0')) { return http_transfer(rsrc); } ftp_set_defaults(rsrc, u); if (!(sock = tcp_connect(rsrc->op, u->host, u->port))) return FALSE; if (!(line = get_line(rsrc, sock))) return FALSE; if (!check_numeric("220", line)) { ui_error(rsrc->op, "bad ftp server greeting: %s", line); nvfree(line); return FALSE; } send_control(sock, "USER ", u->username, "\r\n", NULL); if (!(line = get_line(rsrc, sock))) return FALSE; /* do the password dance */ if (!check_numeric("230", line)) { if (!check_numeric("331", line)) { ui_error(rsrc->op, "bad/unexpected response: %s", line); nvfree(line); return FALSE; } else { nvfree(line); send_control(sock, "PASS ", u->password, "\r\n", NULL); if (!((line = get_line(rsrc, sock)) && check_numeric("230", line)) ) { nvfree(line); ui_error(rsrc->op, "login failed"); return FALSE; } nvfree(line); } } /* set binmode */ send_control(sock, "TYPE I\r\n", NULL); if (!(line = get_line(rsrc, sock))) return 0; nvfree(line); if (u->path) { send_control(sock, "CWD ", u->path, "\r\n", NULL); if (!((line = get_line(rsrc, sock)) && check_numeric("250", line))) { nvfree(line); close_quit(sock); return 0; } nvfree(line); } /* finally, the good stuff */ /* get a socket for reading. try passive first. */ if ((data_sock = get_passive_sock(rsrc, sock)) == -1) { return FALSE; } if (!data_sock) { if ((data_sock = get_sock(rsrc, sock)) < 1) return 0; else passive = 0; } if (u->file) { send_control(sock, "SIZE ", u->file, "\r\n", NULL); line = get_line(rsrc, sock); if (line && check_numeric("213", line)) { rsrc->outfile_size = atoi(line + 3); } else { rsrc->outfile_size = 0; } } if (u->file) send_control(sock, "RETR ", u->file, "\r\n", NULL); else send_control(sock, "NLST\r\n", NULL); if (!((line = get_line(rsrc, sock)) && (check_numeric("150", line) || check_numeric("125", line)))) { nvfree(line); close_quit(sock); return 0; } if (!passive) data_sock = accept(data_sock, NULL, NULL); nvfree(line); retval = dump_data(rsrc, data_sock); line = get_line(rsrc, sock); /* 226 Transfer complete */ nvfree(line); send_control(sock, "QUIT\r\n", NULL); line = get_line(rsrc, sock); /* 221 Goodbye */ nvfree(line); close(sock); close(data_sock); return retval; } /* ftp_transfer() */
static int privServer_process_thread(void * arg) { struct sockaddr_in ClientAddr; int sClientSocket = -1; int ServSock = -1; pthread_t client_threadid[RH_MAX_CLIENTS] = {0}; void *ret = 0; int clientsocket = 0; int nLen = 0; int ipos = 0; Server_PosIndex_t cli_info[RH_MAX_CLIENTS]; Stream_Server_t *stream_net_info = (Stream_Server_t *)arg; privServer_init_pos(stream_net_info->sindex); for (ipos = 0; ipos < RH_MAX_CLIENTS; ipos++) { memset(&(cli_info[ipos]),0,sizeof(Server_PosIndex_t)); } SERVERSTARTRUN: ServSock = create_tcpBind(stream_net_info->port, stream_net_info->ip); printf("[%s:%d]\n", stream_net_info->ip, stream_net_info->port); printf("[%s:%d]--ServSock=%d\n",stream_net_info->ip, stream_net_info->port,ServSock); if (ServSock < 0) { printf("RH_CreateTcpBindFd error:%d,error msg: = %stream_net_info", errno, strerror(errno)); if(errno != EAGAIN || errno != EINTR) { ; } sleep(3); ServSock = INVALID_SOCKET; goto Exit_pthread; } if (listen(ServSock, 100) < 0) { printf("listen error:%d,error msg: = %s", errno, strerror(errno)); close(ServSock); if (errno != EAGAIN || errno != EINTR) { ;// exit(0); } sleep(3); ServSock = INVALID_SOCKET; goto Exit_pthread; } if (setNonblock_fd(ServSock) < 0) { printf("RH_SetNonblockFd error:%d,error msg: = %s\n", errno, strerror(errno)); close(ServSock); if (errno != EAGAIN || errno != EINTR) { // exit(0); } sleep(3); ServSock = INVALID_SOCKET; goto Exit_pthread; } int FdSendBuf = 655350; nLen = sizeof(struct sockaddr_in); while(1) { memset(&ClientAddr, 0, sizeof(struct sockaddr_in)); sClientSocket=0; nLen = sizeof(struct sockaddr_in); sClientSocket = accept(ServSock, (void *)&ClientAddr, (unsigned int *)&nLen); if (sClientSocket > 0) { printf("accept success!!!!ip=[%s]--sClientSocket=%d\n",inet_ntoa(ClientAddr.sin_addr), sClientSocket); int nPos = 0; clear_lost_client(stream_net_info->sindex); nPos = privServer_get_nullPos(stream_net_info->sindex); if (-1 == nPos) { close(sClientSocket); printf("MAXCLIENT ERR[%s:%d]\n", stream_net_info->ip, stream_net_info->port); } else { cli_info[nPos].index = stream_net_info->sindex; cli_info[nPos].pos = nPos; set_client_used(stream_net_info->sindex, nPos, TRUE); set_sock(stream_net_info->sindex, nPos, sClientSocket) ; if (set_sendBufSize(sClientSocket, FdSendBuf) != 0) { printf("RH_SetSndBufSizeFd Is Error!<fd :%d>\n", sClientSocket); } FdSendBuf = get_sendBufSize(sClientSocket); if (setNonblock_fd(sClientSocket) != 0) { printf("RH_SetSndBufSizeFd Is Error!<fd :%d>\n", sClientSocket); } } } else { if (sClientSocket > 0) { close(sClientSocket); printf("close sClientSocket socket!!! %d\n", sClientSocket); sClientSocket = -1; } if(errno == ECONNABORTED || errno == EAGAIN) //Èí¼þÔÒòÖÐ¶Ï { usleep(100000); continue; } if (ServSock > 0) { printf("close enclive socket!!! %d\n", ServSock); close(ServSock); ServSock = INVALID_SOCKET; sleep(1); } goto SERVERSTARTRUN; } } Exit_pthread: printf("exit the drawtimethread \n"); for (ipos = 0; ipos < RH_MAX_CLIENTS; ipos++) { if (client_threadid[ipos]) { clientsocket = get_sock(stream_net_info->sindex, ipos); if (clientsocket != INVALID_SOCKET) { close(clientsocket); set_sock(stream_net_info->sindex, ipos, INVALID_SOCKET); } if (pthread_join(client_threadid[ipos], &ret) == 0) { } } } printf("close the encoder server socket \n"); if (ServSock > 0) { printf("close gserv socket \n"); close(ServSock); } if (stream_net_info) { free(stream_net_info); stream_net_info = NULL; } ServSock = INVALID_SOCKET; printf("End.\n"); return 0; }