/* A proxy chain is a comma-separated list of proxy specification strings: * proto://[user:pass@]host[:port] */ int nsock_proxychain_new(const char *proxystr, nsock_proxychain *chain, nsock_pool nspool) { struct npool *nsp = (struct npool *)nspool; struct proxy_chain *pxc, **pchain = (struct proxy_chain **)chain; *pchain = NULL; pxc = (struct proxy_chain *)safe_malloc(sizeof(struct proxy_chain)); gh_list_init(&pxc->nodes); if (proxystr) { struct proxy_parser *parser; parser = proxy_parser_new(proxystr); while (!parser->done) { gh_list_append(&pxc->nodes, &parser->value->nodeq); proxy_parser_next(parser); } proxy_parser_delete(parser); } if (nsp) { if (nsock_pool_set_proxychain(nspool, pxc) < 0) { nsock_proxychain_delete(pxc); return -1; } } *pchain = pxc; return 1; }
/* 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->loglevel = NSOCK_LOG_ERROR; nsp->logger = (nsock_logger_t)nsock_stderr_logger; nsp->id = nsp_next_id++; nsp->userdata = userdata; nsp->engine = get_io_engine(); nsock_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 nsp->px_chain = NULL; 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; 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; }
int main(int argc, char *argv[]) { gh_list lists[16]; gh_list_elem *current, *next; int num = 0; int ret; int i; for(i=0; i < 16; i++) gh_list_init(&lists[i]); for(num=25000; num < 50000; num++) { for(i=0; i < 16; i++) { gh_list_append(&lists[i], (void *)num); } } for(num=24999; num >= 0; num--) { for(i=0; i < 16; i++) { gh_list_prepend(&lists[i], (void *)num); } } for(num=0; num < 50000; num++) { for(i=0; i < 16; i++) { ret = (int) gh_list_pop(&lists[i]); if (ret != num) fatal("prepend_test: Bogus return value %d when expected %d\n", ret, num); } } for(i=0; i < 16; i++) { ret = (int) gh_list_pop(&lists[i]); if (ret != 0) fatal("Ret is bogus for list %d", i); } printf("Done with first set\n"); for(num=24999; num >= 0; num--) { for(i=0; i < 16; i++) { gh_list_prepend(&lists[i], (void *)num); } } for(num=25000; num < 50000; num++) { for(i=0; i < 16; i++) { gh_list_append(&lists[i], (void *)num); } } for(num=0; num < 50000; num++) { for(i=0; i < 16; i++) { ret = (int) gh_list_pop(&lists[i]); if (ret != num) fatal("prepend_test: Bogus return value %d when expected %d\n", ret, num); } } printf("Done with second set\n"); for(num=25000; num < 50000; num++) { for(i=0; i < 16; i++) { gh_list_append(&lists[i], (void *)num); } } for(num=24999; num >= 0; num--) { for(i=0; i < 16; i++) { gh_list_prepend(&lists[i], (void *)num); } } for(num=0; num < 50000; num++) { for(i=0; i < 16; i++) { ret = (int) gh_list_pop(&lists[i]); if (ret != num) fatal("prepend_test: Bogus return value %d when expected %d\n", ret, num); } } printf("Done with third set ...\n"); for(num=24999; num >= 0; num--) { for(i=0; i < 16; i++) { gh_list_prepend(&lists[i], (void *)num); } } for(num=25000; num < 50000; num++) { for(i=0; i < 16; i++) { gh_list_append(&lists[i], (void *)num); } } for(i=0; i < 16; i++) { num=0; for(current = GH_LIST_FIRST_ELEM(&lists[i]); current; current = next) { next = GH_LIST_ELEM_NEXT(current); if ((int)GH_LIST_ELEM_DATA(current) != num) fatal("Got %d when I expected %d\n", (int)GH_LIST_ELEM_DATA(current), num); gh_list_remove_elem(&lists[i], current); num++; } if (num != 50000) fatal("Number is %d, even though %d was expected", num, 50000); if (GH_LIST_COUNT(&lists[i]) != 0) { fatal("List should be empty, but instead it has %d members!\n", GH_LIST_COUNT(&lists[i])); } } printf("Done with fourth set, freeing buffers\n"); for(i=0; i < 16; i++) { gh_list_free(&lists[i]); } }