/* Send a ftp command `act' and receive ftp reply. * If `act' is NULL nothing will be sent. * Return 0 if our reply match to "XXX .*\r\n" where XXX * is expected_reply *__follow by a space!!!__*. * Otherwise return recursively and receive other data. */ static int ftp_act (net_t *netpolicy, const char *act, const char *expected_reply) { int l, r, rval, ftpsz = sizeof (netpolicy->ftpreply); char *ptr, buffer[WORDSIZE0]; if (act) { l = strlen (act); if (send (netpolicy->sd, act, l, 0) != l) return -1; } if ((rval = t_recv (netpolicy->sd, netpolicy->ftpreply, ftpsz)) <= 0) return -1; netpolicy->ftpreply[rval] = 0x00; if (rval < (l = strlen (expected_reply))) return -1; r = strlen (netpolicy->ftpreply); if (strncmp (netpolicy->ftpreply, expected_reply, l) == 0 && netpolicy->ftpreply[l] == ' ' && netpolicy->ftpreply[r - 2] == '\r' && netpolicy->ftpreply[r - 1] == '\n') { return 0; } else { snprintf (buffer, WORDSIZE, "\r\n%s ", expected_reply); if ((ptr = strstr (netpolicy->ftpreply, buffer))) { r = strlen (ptr); return (ptr[l + 2] == ' ' && !strcmp (ptr + r - 2, "\r\n")) ? 0 : -1; } else { if (atoi (netpolicy->ftpreply) != 550) /* file not found */ return ftp_act (netpolicy, NULL, expected_reply); /* try next data ... */ } } return -1; }
kii_socket_code_t socket_recv_cb( kii_socket_context_t* socket_context, char* buffer, size_t length_to_read, size_t* out_actual_length) { int res; int received; int total = 0; char *pBuf = NULL; context_t* ctx = (context_t*)socket_context->app_context; do { res = t_select((void *)handle, ctx->sock, 1000); if (res == A_OK) { #if CONNECT_SSL received = SSL_read(ssl, buffer, length_to_read); #else received = t_recv(handle, ctx->sock, buffer, length_to_read, 0); #endif if(received > 0) { total = received; break; } } } while (res == A_OK); if (total > 0) { *out_actual_length = total; return KII_SOCKETC_OK; } else { printf("failed to receive:\n"); // TOOD: could be 0 on success? *out_actual_length = 0; socket_close_cb(socket_context); return KII_SOCKETC_FAIL; } }
int get_file_over_ftp (net_t *netpolicy) { char buffer [HUGE0], *ptr; struct hostent *phostent; struct sockaddr_in client, pasvclient; int rval; unsigned long filesz, received; slassert (netpolicy != NULL); fprintf (stdout, netpolicy->msg); fflush (stdout); if (!netpolicy->savepart) slassert (!netpolicy->checkstamp); if (!netpolicy->overwrite && file_exist (netpolicy->destpath, NULL, true)) /* regular file exist */ { return shutdown_net_t (netpolicy, FILE_DOWNLOADED, "Already downloaded"); } __act (phostent = gethostbyname (netpolicy->hostname), -1, "Cannot resolve host"); client.sin_family = phostent->h_addrtype; client.sin_port = htons ((unsigned short) netpolicy->port); memcpy (&client.sin_addr.s_addr, phostent->h_addr_list[0], phostent->h_length); __act ((netpolicy->sd = socket (client.sin_family, SOCK_STREAM, 0)) >= 0, -1, NULL); __act ((rval = t_connect (netpolicy->sd, (struct sockaddr *)&client, sizeof client, netpolicy->timeout)) >= 0, -1, NULL); /* starting FTP transaction */ fprintf (stdout, "\r%s [Starting ftp transaction]", netpolicy->msg); fflush (stdout); netpolicy->ftpquit = true; /* read server ready */ __act (t_recv (netpolicy->sd, buffer, HUGE) >= 0, FILE_NOT_DOWNLOADED, "Server not ready"); __act (strncmp (buffer, "220", 3) == 0, FILE_NOT_DOWNLOADED, "Server not ready"); /* anonymous login */ clrline (stdout, strlen (netpolicy->msg) + 30); fprintf (stdout, "\r%s [FTP anonymous login]", netpolicy->msg); fflush (stdout); /* sending username */ __act (!ftp_act (netpolicy, "USER anonymous\r\n", "331"), FILE_NOT_DOWNLOADED, "User failed"); /* sending pass */ __act (!ftp_act (netpolicy, "PASS [email protected]\r\n", "230"), FILE_NOT_DOWNLOADED, "Pass failed"); clrline (stdout, strlen (netpolicy->msg) + 25); fprintf (stdout, "\r%s [Retrieving file]", netpolicy->msg); fflush (stdout); __act (!ftp_act (netpolicy, "SYST\r\n", "215"), FILE_NOT_DOWNLOADED, "Syst error"); __act (!ftp_act (netpolicy, "TYPE I\r\n", "200"), FILE_NOT_DOWNLOADED, "Type I error"); __act (!ftp_act (netpolicy, "PASV\r\n", "227"), FILE_NOT_DOWNLOADED, "Pasv error"); /* obtain ip and port on passive mode */ __act (ptr = strchr (netpolicy->ftpreply, '('), FILE_NOT_DOWNLOADED, "Error on pasv mode"); sscanf (ptr, "(%hu,%hu,%hu,%hu,%hu,%hu)", &netpolicy->pasv[0], &netpolicy->pasv[1], /* IP */ &netpolicy->pasv[2], &netpolicy->pasv[3], /* IP */ &netpolicy->pasv[4], &netpolicy->pasv[5]); /* port: pasv[4] * 256 + pasv[5] */ snprintf (netpolicy->pasvip, sizeof netpolicy->pasvip, "%hu.%hu.%hu.%hu", netpolicy->pasv[0], netpolicy->pasv[1], netpolicy->pasv[2], netpolicy->pasv[3]); netpolicy->pasvport = netpolicy->pasv[4] * 256 + netpolicy->pasv[5]; netpolicy->pasvsd = socket (AF_INET, SOCK_STREAM, 0); pasvclient.sin_family = AF_INET; pasvclient.sin_port = htons((unsigned short) netpolicy->pasvport); __act (inet_pton (AF_INET, netpolicy->pasvip, &pasvclient.sin_addr) == 1 && !t_connect (netpolicy->pasvsd, (struct sockaddr *) &pasvclient, sizeof pasvclient, netpolicy->timeout), -1, "ftp pasv error"); snprintf (buffer, HUGE, "SIZE %s\r\n", netpolicy->srcpath); __act (!ftp_act (netpolicy, buffer, "213"), FILE_NOT_DOWNLOADED, (atoi (netpolicy->ftpreply) == 550) ? "File not found" : "Size error"); filesz = atoi (netpolicy->ftpreply + 4); /* 4 = strlen ("213 ") */ /* require file */ snprintf (buffer, HUGE, "RETR %s\r\n", netpolicy->srcpath); __act (!ftp_act (netpolicy, buffer, "150"), FILE_NOT_DOWNLOADED, "Retr error"); checkstamp (netpolicy, netpolicy->pasvsd); __act (netpolicy->oldstamp != 0, FILE_ALREADY_UPDATE, "Already update"); if (netpolicy->savepart) { slassert ((strlen (netpolicy->destpath) + strlen (".part")) < sizeof (netpolicy->destpath)); strcat (netpolicy->destpath, ".part"); } __act (netpolicy->fddest = fopen (netpolicy->destpath, "wb"), -1, netpolicy->destpath); if (netpolicy->oldstamp == 1) { fwrite (netpolicy->stamprecv, 1, STAMPSZ, netpolicy->fddest); } received = (netpolicy->checkstamp) ? STAMPSZ : 0; clrline (stdout, strlen (netpolicy->msg) + 25); netstat (NETSTAT_INIT, 0, 0, 0); /* init */ while ((rval = recv (netpolicy->pasvsd, buffer, HUGE, 0)) > 0) { received += (unsigned long) rval; fprintf (stdout, "\r%s [%s]", netpolicy->msg, netstat (0, 1, received, filesz)); fflush (stdout); fwrite (buffer, 1, rval, netpolicy->fddest); } fprintf (stdout, "\r%s [%s]\n", netpolicy->msg, netstat (NETSTAT_END, 1, received, filesz)); /* finish, flush 100% */ fflush (stdout); return shutdown_net_t (netpolicy, 0, NULL); }
void udp_echo_poll() { struct sockaddr_in him; /* IP info of current rhost */ int len; int e; UDPCLIENT tmpclient,nextclient; in_udpechoq++; /* don't re-entry from tk_yield() */ if (in_udpechoq != 1) { in_udpechoq--; return; } /* check for received echo packet */ if (es_sock != INVALID_SOCKET) { int sa_size = sizeof(him); len = recvfrom(es_sock, inbuf, TCP_MSS, 0, (struct sockaddr *)&him, &sa_size); if (len < 0) { e = t_errno(es_sock); if (e != EWOULDBLOCK) dprintf("UDP echo server socket error %d\n", e); } else if(len == 0) { dtrap(); /* socket closed? */ udp_secho_close(NULL); } else /* if(len > 0) */ sendto(es_sock, inbuf, len, 0, (struct sockaddr *)&him, sizeof(him)); } for (tmpclient = udpq; tmpclient; tmpclient = nextclient) { nextclient=tmpclient->next; /* check for received echo reply */ len = t_recv(tmpclient->sock, inbuf, TCP_MSS, 0); if (len < 0) { e = t_errno(tmpclient->sock); if (e != EWOULDBLOCK) ns_printf(tmpclient->pio,"UDP echo client socket error %d\n", e); } else if(len == 0) { dtrap(); /* socket closed? */ tmpclient->sock=INVALID_SOCKET; udp_client_del(tmpclient); continue; } else /* if(len > 0) - got some echo data */ { ns_printf(tmpclient->pio,"UDP echo reply; len:%d, reply:%lu", len, tmpclient->replies); tmpclient->replies++; tmpclient->tot_rcvd++; /* see if it's one of ours, print number if so */ if (strncmp(inbuf, echodata, 16) == 0) ns_printf(tmpclient->pio,", Our send#:%ld",atol((&inbuf[17]))); ns_printf(tmpclient->pio,"\n%s",prompt); } if ( tmpclient->state == UDP_BUSY ) { if ( tmpclient->send_cnt >= tmpclient->times ) { /* Previous "udecho" command has completed */ tmpclient->state = UDP_IDLE ; } else { if ( udp_send_an_echo(tmpclient) == SUCCESS ) ns_printf(tmpclient->pio,"%s",prompt); } } if ( tmpclient->ticks + (UDP_IDLE_TIMEOUT*TPS) < cticks ) { /* This client has been lying around ldle for a long time */ ns_printf(tmpclient->pio,"Deleting idle UDP Echo Client.\n%s",prompt); udp_client_del(tmpclient); } } in_udpechoq--; }
void tcp_ipv6_poll() { int indx = 0; int freeSlot = 0; for(; indx<MAX_SOCK_NO; indx++) if(g_sockets[indx].indxToListen == -1) { int sa_size = sizeof(struct sockaddr_in6); struct sockaddr_in6 stFrom; SOCKTYPE sock = t_accept(g_sockets[indx].sock,(struct sockaddr*)&stFrom, &sa_size); //C_NLOG_INFO("Iniche:tcp_ipv6_poll: 0"); if(sock <= 0) continue; //C_NLOG_INFO("Iniche:tcp_ipv6_poll: 1"); //C_NLOG_INFO("Iniche:tcp_ipv6_poll: 2"); int iTmp = 1; int e = t_setsockopt(sock, SOL_SOCKET, SO_NONBLOCK, (void *)&iTmp, sizeof(iTmp)); if (e == SOCKET_ERROR) { e = t_errno(sock); t_socketclose(sock); C_NLOG_ERR("Iniche:tcp_ipv6_pool: t_setsockopt() SO_NONBLOCK failed, Err: %d", e); continue; } iTmp = INICHE_TCP_RCV_MAX_BUFF_LEN; e = t_setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (void *)&iTmp, sizeof(iTmp)); if (e == SOCKET_ERROR) { e = t_errno(sock); t_socketclose(sock); C_NLOG_ERR("Iniche:tcp_ipv6_pool: t_setsockopt() SO_RCVBUF failed, Err: %d", e); continue; } int isSett = 0; for(; freeSlot < MAX_SOCK_NO; freeSlot++) { if(g_sockets[freeSlot].sock == INVALID_SOCKET) { //callback g_sockets[freeSlot].sock = sock; g_sockets[freeSlot].indxToListen = indx; (gFunctions->m_pfnAccept)((void *)(g_sockets+freeSlot)); //C_NLOG_INFO("Iniche:tcp_ipv6_poll: HCONN accepted = %X, index=%d",sock,freeSlot); isSett = 1; break; } } //if no free slot founded, then break if(isSett == 0) { C_NLOG_ERR("Iniche:tcp_ipv6_poll: No free slot for creating soket !"); t_socketclose(sock); break; } //C_NLOG_INFO("Iniche:tcp_ipv6_poll: 4"); } unsigned char buff[INICHE_TCP_RCV_MAX_BUFF_LEN]; int j = 0; for(; j < MAX_SOCK_NO; j++) { if(g_sockets[j].indxToListen != -1) { if(g_sockets[j].sock == INVALID_SOCKET) { continue; } int len = t_recv(g_sockets[j].sock, (char*)buff, INICHE_TCP_RCV_MAX_BUFF_LEN, 0); if(len > 0) { //C_NLOG_INFO("Iniche:tcp_ipv6_poll: 6 len = %d, buff=%s", len, (char*)buff); (gFunctions->m_pfnRecv)((void*)(g_sockets+j),buff,len); } } } }