int nsock_full_write(fd_t fd_,void *data, int size) { int nbytes = nsock_write(fd_,data,size); int tmp_size = size; while(nbytes>=0&&nbytes<tmp_size) { data = (char*)data+nbytes; tmp_size -= nbytes; nbytes = nsock_write(fd_,data,tmp_size); } if(size==nbytes) return tmp_size; return -1; }
ssize_t nsock_fdprintf(nsock_t *ns, const u_char *fmt, ...) { va_list vl; u_char tbuf[8192]; va_start(vl, fmt); memset(tbuf, 0, sizeof(tbuf)); vsnprintf((char *)tbuf, sizeof(tbuf) - 1, (char *)fmt, vl); va_end(vl); return nsock_write(ns, tbuf, strlen((char *)tbuf)); }
static void read_stdin_handler(nsock_pool nsp, nsock_event evt, void *data) { enum nse_status status = nse_status(evt); enum nse_type type = nse_type(evt); char *buf, *tmp = NULL; int nbytes; assert(type == NSE_TYPE_READ); if (status == NSE_STATUS_EOF) { if (o.sendonly) { /* In --send-only mode, exit after EOF on stdin. */ nsock_loop_quit(nsp); } else { shutdown(nsi_getsd(cs.sock_nsi), SHUT_WR); } return; } else if (status == NSE_STATUS_ERROR) { loguser("%s.\n", socket_strerror(nse_errorcode(evt))); exit(1); } else if (status == NSE_STATUS_TIMEOUT) { loguser("%s.\n", socket_strerror(ETIMEDOUT)); exit(1); } else if (status == NSE_STATUS_CANCELLED || status == NSE_STATUS_KILL) { return; } else { assert(status == NSE_STATUS_SUCCESS); } buf = nse_readbuf(evt, &nbytes); /* read from stdin */ if (o.linedelay) ncat_delay_timer(o.linedelay); if (o.crlf) { if (fix_line_endings(buf, &nbytes, &tmp, &cs.crlf_state)) buf = tmp; } nsock_write(nsp, cs.sock_nsi, write_socket_handler, -1, NULL, buf, nbytes); ncat_log_send(buf, nbytes); if (tmp) free(tmp); refresh_idle_timer(nsp); }
void telnet_event_handler(nsock_pool nsp, nsock_event nse, void *mydata) { nsock_iod nsi = nse_iod(nse); enum nse_status status = nse_status(nse); enum nse_type type = nse_type(nse); struct sockaddr_in peer; struct telnet_state *ts; int nbytes; char *str; int read_timeout = -1; int write_timeout = 2000; ts = (struct telnet_state *)mydata; printf("telnet_event_handler: Received callback of type %s with status %s\n", nse_type2str(type), nse_status2str(status)); if (status == NSE_STATUS_SUCCESS) { switch (type) { case NSE_TYPE_CONNECT: case NSE_TYPE_CONNECT_SSL: nsi_getlastcommunicationinfo(nsi, NULL, NULL, NULL, (struct sockaddr *)&peer, sizeof peer); printf("Successfully connected %sto %s:%hu -- start typing lines\n", (type == NSE_TYPE_CONNECT_SSL) ? "(SSL!) " : "", inet_ntoa(peer.sin_addr), peer.sin_port); /* First of all, lets add STDIN to our list of watched filehandles */ if ((ts->stdin_nsi = nsi_new2(nsp, STDIN_FILENO, NULL)) == NULL) { fprintf(stderr, "Failed to create stdin msi\n"); exit(1); } /* Now lets read from stdin and the network, line buffered (by nsock) */ ts->latest_readtcpev = nsock_readlines(nsp, ts->tcp_nsi, telnet_event_handler, read_timeout, ts, 1); ts->latest_readstdinev = nsock_readlines(nsp, ts->stdin_nsi, telnet_event_handler, read_timeout, ts, 1); break; case NSE_TYPE_READ: str = nse_readbuf(nse, &nbytes); if (nsi == ts->tcp_nsi) { printf("%s", str); /* printf("Read from tcp socket (%d bytes):\n%s", nbytes, str); */ ts->latest_readtcpev = nsock_readlines(nsp, ts->tcp_nsi, telnet_event_handler, read_timeout, ts, 1); } else { /* printf("Read from stdin (%d bytes):\n%s", nbytes, str); */ nsock_write(nsp, ts->tcp_nsi, telnet_event_handler, write_timeout, ts, str, nbytes); ts->latest_readstdinev = nsock_readlines(nsp, ts->stdin_nsi, telnet_event_handler, read_timeout, ts, 1); } break; case NSE_TYPE_WRITE: /* Nothing to do, really */ break; case NSE_TYPE_TIMER: break; default: fprintf(stderr, "telnet_event_handler: Got bogus type -- quitting\n"); exit(1); break; } } else if (status == NSE_STATUS_EOF) { printf("Got EOF from %s\nCancelling outstanding readevents.\n", (nsi == ts->tcp_nsi) ? "tcp socket" : "stdin"); /* One of these is the event I am currently handling! But I wanted to be evil when testing this out... */ if (nsock_event_cancel(nsp, ts->latest_readtcpev, 1) != 0) { printf("Cancelled tcp event: %li\n", ts->latest_readtcpev); } if (nsock_event_cancel(nsp, ts->latest_readstdinev, 1) != 0) { printf("Cancelled stdin event: %li\n", ts->latest_readstdinev); } } else if (status == NSE_STATUS_ERROR) { if (nsi_checkssl(nsi)) { printf("SSL %s failed: %s\n", nse_type2str(type), ERR_error_string(ERR_get_error(), NULL)); } else { int err; err = nse_errorcode(nse); printf("%s failed: (%d) %s\n", nse_type2str(type), err, strerror(err)); } } return; }