/* And here is how you create an nsock_pool. This allocates, initializes, and returns an nsock_pool event aggregator. In the case of error, NULL will be returned. If you do not wish to immediately associate any userdata, pass in NULL. */ nsock_pool nsp_new(void *userdata) { mspool *nsp; nsp = (mspool *) safe_malloc(sizeof(*nsp)); memset(nsp, 0, sizeof(*nsp)); gettimeofday(&nsock_tod, NULL); nsp_settrace(nsp, NULL, 0, NULL); nsp->broadcast = 0; if (!nsocklib_initialized) { nsock_library_initialize(); nsocklib_initialized = 1; } nsp->id = nsp_next_id++; /* Now to init the nsock_io_info */ FD_ZERO(&nsp->mioi.fds_master_r); FD_ZERO(&nsp->mioi.fds_master_w); FD_ZERO(&nsp->mioi.fds_master_x); nsp->mioi.max_sd = -1; nsp->mioi.results_left = 0; /* Next comes the event list structure */ gh_list_init(&nsp->evl.connect_events); gh_list_init(&nsp->evl.read_events); gh_list_init(&nsp->evl.write_events); gh_list_init(&nsp->evl.timer_events); #if HAVE_PCAP gh_list_init(&nsp->evl.pcap_read_events); #endif gh_list_init(&nsp->evl.free_events); nsp->evl.next_ev.tv_sec = 0; nsp->evl.events_pending = 0; nsp->userdata = userdata; gh_list_init(&nsp->free_iods); gh_list_init(&nsp->active_iods); nsp->next_event_serial = 1; nsp->quit = 0; #if HAVE_OPENSSL nsp->sslctx = NULL; #endif return (nsock_pool) nsp; }
/* And here is how you create an nsock_pool. This allocates, initializes, and * returns an nsock_pool event aggregator. In the case of error, NULL will be * returned. If you do not wish to immediately associate any userdata, pass in * NULL. */ nsock_pool nsp_new(void *userdata) { mspool *nsp; /* initialize the library in not already done */ if (!nsocklib_initialized) { nsock_library_initialize(); nsocklib_initialized = 1; } nsp = (mspool *)safe_malloc(sizeof(*nsp)); memset(nsp, 0, sizeof(*nsp)); gettimeofday(&nsock_tod, NULL); nsp_settrace(nsp, NULL, 0, NULL); nsp->id = nsp_next_id++; nsp->userdata = userdata; nsp->engine = get_io_engine(); nsp->engine->init(nsp); /* initialize IO events lists */ gh_list_init(&nsp->connect_events); gh_list_init(&nsp->read_events); gh_list_init(&nsp->write_events); #if HAVE_PCAP gh_list_init(&nsp->pcap_read_events); #endif /* initialize timer list */ gh_list_init(&nsp->timer_events); /* initialize the list of IODs */ gh_list_init(&nsp->active_iods); /* initialize caches */ gh_list_init(&nsp->free_iods); gh_list_init(&nsp->free_events); nsp->next_event_serial = 1; nsp->device = NULL; #if HAVE_OPENSSL nsp->sslctx = NULL; #endif return (nsock_pool)nsp; }
int main(int argc, char *argv[]) { struct in_addr target; nsock_pool nsp; nsock_event_id ev; unsigned short portno; enum nsock_loopstatus loopret; struct telnet_state ts; int c; int usessl = 0; int tracelevel = 0; struct timeval now; struct sockaddr_in taddr; ts.stdin_nsi = NULL; while ((c = getopt(argc, argv, "st:")) != -1) { switch (c) { case 's': usessl = 1; break; case 't': tracelevel = atoi(optarg); assert(tracelevel >= 0); break; default: usage(); break; } } if (argc - optind <= 0 || argc - optind > 2) usage(); if (!resolve(argv[optind], &target)) { fprintf(stderr, "Failed to resolve target host: %s\nQUITTING.\n", argv[optind]); exit(1); } optind++; if (optind < argc) portno = atoi(argv[optind]); else portno = 23; /* OK, we start with creating a p00l */ if ((nsp = nsp_new(NULL)) == NULL) { fprintf(stderr, "Failed to create new pool. QUITTING.\n"); exit(1); } gettimeofday(&now, NULL); nsp_settrace(nsp, NULL, tracelevel, &now); if ((ts.tcp_nsi = nsi_new(nsp, NULL)) == NULL) { fprintf(stderr, "Failed to create new nsock_iod. QUITTING.\n"); exit(1); } taddr.sin_family = AF_INET; taddr.sin_addr = target; taddr.sin_port = portno; if (usessl) { ts.ssl_session = NULL; ev = nsock_connect_ssl(nsp, ts.tcp_nsi, telnet_event_handler, 10000, &ts, (struct sockaddr *)&taddr, sizeof taddr, IPPROTO_TCP, portno, ts.ssl_session); } else ev = nsock_connect_tcp(nsp, ts.tcp_nsi, telnet_event_handler, 10000, &ts, (struct sockaddr *)&taddr, sizeof taddr, portno); printf("The event id is %lu -- initiating l00p\n", ev); /* Now lets get this party started right! */ loopret = nsock_loop(nsp, -1); printf("nsock_loop returned %d\n", (int)loopret); return 0; }
int ncat_connect(void) { nsock_pool mypool; int rc; /* Unless explicitely asked not to do so, ncat uses the * fallback nsock engine to maximize compatibility between * operating systems and the different use cases. */ if (!o.nsock_engine) nsock_set_default_engine("select"); /* Create an nsock pool */ if ((mypool = nsp_new(NULL)) == NULL) bye("Failed to create nsock_pool."); if (o.debug > 1) /* A trace level of 1 still gives you an awful lot. */ nsp_settrace(mypool, stderr, 1, nsock_gettimeofday()); /* Allow connections to broadcast addresses. */ nsp_setbroadcast(mypool, 1); #ifdef HAVE_OPENSSL set_ssl_ctx_options((SSL_CTX *) nsp_ssl_init(mypool)); #endif if (httpconnect.storage.ss_family == AF_UNSPEC && socksconnect.storage.ss_family == AF_UNSPEC) { /* A non-proxy connection. Create an iod for a new socket. */ cs.sock_nsi = nsi_new(mypool, NULL); if (cs.sock_nsi == NULL) bye("Failed to create nsock_iod."); if (nsi_set_hostname(cs.sock_nsi, o.target) == -1) bye("Failed to set hostname on iod."); #if HAVE_SYS_UN_H /* For DGRAM UNIX socket we have to use source socket */ if (o.af == AF_UNIX && o.udp) { if (srcaddr.storage.ss_family != AF_UNIX) { char *tmp_name = NULL; /* If no source socket was specified, we have to create temporary one. */ if ((tmp_name = tempnam(NULL, "ncat.")) == NULL) bye("Failed to create name for temporary DGRAM source Unix domain socket (tempnam)."); srcaddr.un.sun_family = AF_UNIX; strncpy(srcaddr.un.sun_path, tmp_name, sizeof(srcaddr.un.sun_path)); free (tmp_name); } nsi_set_localaddr(cs.sock_nsi, &srcaddr.storage, SUN_LEN((struct sockaddr_un *)&srcaddr.storage)); if (o.verbose) loguser("[%s] used as source DGRAM Unix domain socket.\n", srcaddr.un.sun_path); } else #endif if (srcaddr.storage.ss_family != AF_UNSPEC) nsi_set_localaddr(cs.sock_nsi, &srcaddr.storage, sizeof(srcaddr.storage)); if (o.numsrcrtes) { unsigned char *ipopts = NULL; size_t ipoptslen = 0; if (o.af != AF_INET) bye("Sorry, -g can only currently be used with IPv4."); ipopts = buildsrcrte(targetss.in.sin_addr, o.srcrtes, o.numsrcrtes, o.srcrteptr, &ipoptslen); nsi_set_ipoptions(cs.sock_nsi, ipopts, ipoptslen); free(ipopts); /* Nsock has its own copy */ } #if HAVE_SYS_UN_H if (o.af == AF_UNIX) { if (o.udp) { nsock_connect_unixsock_datagram(mypool, cs.sock_nsi, connect_handler, NULL, &targetss.sockaddr, SUN_LEN((struct sockaddr_un *)&targetss.sockaddr)); } else { nsock_connect_unixsock_stream(mypool, cs.sock_nsi, connect_handler, o.conntimeout, NULL, &targetss.sockaddr, SUN_LEN((struct sockaddr_un *)&targetss.sockaddr)); } } else #endif if (o.udp) { nsock_connect_udp(mypool, cs.sock_nsi, connect_handler, NULL, &targetss.sockaddr, targetsslen, inet_port(&targetss)); } #ifdef HAVE_OPENSSL else if (o.sctp && o.ssl) { nsock_connect_ssl(mypool, cs.sock_nsi, connect_handler, o.conntimeout, NULL, &targetss.sockaddr, targetsslen, IPPROTO_SCTP, inet_port(&targetss), NULL); } #endif else if (o.sctp) { nsock_connect_sctp(mypool, cs.sock_nsi, connect_handler, o.conntimeout, NULL, &targetss.sockaddr, targetsslen, inet_port(&targetss)); } #ifdef HAVE_OPENSSL else if (o.ssl) { nsock_connect_ssl(mypool, cs.sock_nsi, connect_handler, o.conntimeout, NULL, &targetss.sockaddr, targetsslen, IPPROTO_TCP, inet_port(&targetss), NULL); } #endif else { nsock_connect_tcp(mypool, cs.sock_nsi, connect_handler, o.conntimeout, NULL, &targetss.sockaddr, targetsslen, inet_port(&targetss)); } } else { /* A proxy connection. */ static int connect_socket; int len; char *line; size_t n; if (httpconnect.storage.ss_family != AF_UNSPEC) { connect_socket = do_proxy_http(); if (connect_socket == -1) return 1; } else if (socksconnect.storage.ss_family != AF_UNSPEC) { struct socket_buffer stateful_buf; struct socks4_data socks4msg; char socksbuf[8]; connect_socket = do_connect(SOCK_STREAM); if (connect_socket == -1) { loguser("Proxy connection failed: %s.\n", socket_strerror(socket_errno())); return 1; } socket_buffer_init(&stateful_buf, connect_socket); if (o.verbose) { loguser("Connected to proxy %s:%hu\n", inet_socktop(&targetss), inet_port(&targetss)); } /* Fill the socks4_data struct */ zmem(&socks4msg, sizeof(socks4msg)); socks4msg.version = SOCKS4_VERSION; socks4msg.type = SOCKS_CONNECT; socks4msg.port = socksconnect.in.sin_port; socks4msg.address = socksconnect.in.sin_addr.s_addr; if (o.proxy_auth) Strncpy(socks4msg.username, (char *) o.proxy_auth, sizeof(socks4msg.username)); len = 8 + strlen(socks4msg.username) + 1; if (send(connect_socket, (char *) &socks4msg, len, 0) < 0) { loguser("Error sending proxy request: %s.\n", socket_strerror(socket_errno())); return 1; } /* The size of the socks4 response is 8 bytes. So read exactly 8 bytes from the buffer */ if (socket_buffer_readcount(&stateful_buf, socksbuf, 8) < 0) { loguser("Error: short reponse from proxy.\n"); return 1; } if (socksbuf[1] != 90) { loguser("Proxy connection failed.\n"); return 1; } /* Clear out whatever is left in the socket buffer which may be already sent by proxy server along with http response headers. */ line = socket_buffer_remainder(&stateful_buf, &n); /* Write the leftover data to stdout. */ Write(STDOUT_FILENO, line, n); } /* Once the proxy negotiation is done, Nsock takes control of the socket. */ cs.sock_nsi = nsi_new2(mypool, connect_socket, NULL); /* Create IOD for nsp->stdin */ if ((cs.stdin_nsi = nsi_new2(mypool, 0, NULL)) == NULL) bye("Failed to create stdin nsiod."); post_connect(mypool, cs.sock_nsi); } /* connect */ rc = nsock_loop(mypool, -1); if (o.verbose) { struct timeval end_time; double time; gettimeofday(&end_time, NULL); time = TIMEVAL_MSEC_SUBTRACT(end_time, start_time) / 1000.0; loguser("%lu bytes sent, %lu bytes received in %.2f seconds.\n", nsi_get_write_count(cs.sock_nsi), nsi_get_read_count(cs.sock_nsi), time); } #if HAVE_SYS_UN_H if (o.af == AF_UNIX && o.udp) { if (o.verbose) loguser("Deleting source DGRAM Unix domain socket. [%s]\n", srcaddr.un.sun_path); unlink(srcaddr.un.sun_path); } #endif nsp_delete(mypool); return rc == NSOCK_LOOP_ERROR ? 1 : 0; }