/// Sets the hostname on which to listen void onion_set_hostname(onion *server, const char *hostname){ if (server->listen_points){ free(server->listen_points[0]->hostname); server->listen_points[0]->hostname=strdup(hostname); } else{ onion_add_listen_point(server, hostname, NULL, onion_http_new()); } }
/// Sets the port to listen void onion_set_port(onion *server, const char *port){ if (server->listen_points){ free(server->listen_points[0]->port); server->listen_points[0]->port=strdup(port); } else{ onion_add_listen_point(server, NULL, port, onion_http_new()); } }
int main(int argc, char **argv){ o=onion_new(O_THREADED); signal(SIGINT, free_onion); onion_set_root_handler(o, onion_handler_export_local_new(".")); onion_add_listen_point(o, "localhost", "8080", onion_http_new()); onion_add_listen_point(o, "localhost", "8081", onion_http_new()); onion_add_listen_point(o, "localhost", "4443", onion_https_new(O_SSL_CERTIFICATE_KEY, "cert.pem", "cert.key")); /** onion_set_port(o, "localhost", "6121", onion_protocol_spdy()); */ onion_listen(o); onion_free(o); return 0; }
int main(int argc, char **argv){ o=onion_new(O_THREADED); signal(SIGINT, free_onion); onion_set_root_handler(o, onion_handler_export_local_new(".")); onion_add_listen_point(o, "localhost", "8080", onion_http_new()); onion_add_listen_point(o, "localhost", "8081", onion_http_new()); #ifdef HAVE_GNUTLS onion_add_listen_point(o, "localhost", "4443", onion_https_new(O_SSL_CERTIFICATE_KEY, "cert.pem", "cert.key")); #else ONION_WARNING("HTTPS support is not enabled. Recompile with gnutls"); #endif /** onion_set_port(o, "localhost", "6121", onion_protocol_spdy()); */ onion_listen(o); onion_free(o); return 0; }
int trivia(void) { printf("creating web-server\n"); o=onion_new(O_ONE_LOOP); printf("obtaining root url\n"); onion_url *urls=onion_root_url(o); printf("adding css skeleton\n"); if(add_css_page(urls)) { printf("error: add_questions(): %s\n", strerror(errno)); exit(1); } printf("adding questions\n"); if(add_questions(urls)) { printf("error: add_questions(): %s\n", strerror(errno)); exit(1); } printf("adding landing page\n"); if(add_start_page(urls)) { printf("error: add_landing_page(): %s\n", strerror(errno)); exit(1); } printf("adding done page\n"); if(add_done_page(urls)) { printf("error: add_done_page(): %s\n", strerror(errno)); exit(1); } printf("adding reset page\n"); if(add_reset_page(urls)) { printf("error: add_landing_page(): %s\n", strerror(errno)); exit(1); } printf("adding signal handlers\n"); signal(SIGTERM, onexit); signal(SIGINT, onexit); printf("adding listening point\n"); onion_add_listen_point(o, NULL, "80", onion_http_new()); printf("listening\n"); onion_listen(o); printf("freeing\n"); onion_free(o); return 0; }
/** * @short Performs the listening with the given mode * @memberof onion_t * * This is the main loop for the onion server. * * It initiates the listening on all the selected ports and addresses. * * @returns !=0 if there is any error. It returns actualy errno from the network operations. See socket for more information. */ int onion_listen(onion *o){ #ifdef HAVE_PTHREADS if (!(o->flags&O_DETACHED) && (o->flags&O_DETACH_LISTEN)){ // Must detach and return o->flags|=O_DETACHED; pthread_create(&o->listen_thread,NULL, (void*)onion_listen, o); return 0; } #endif if (!o->listen_points){ onion_add_listen_point(o,NULL,NULL,onion_http_new()); ONION_DEBUG("Created default HTTP listen port"); } /// Start listening size_t successful_listened_points=0; onion_listen_point **lp=o->listen_points; while (*lp){ int listen_result=onion_listen_point_listen(*lp); if (!listen_result) { successful_listened_points++; } lp++; } if (!successful_listened_points){ ONION_ERROR("There are no available listen points"); return 1; } if (o->flags&O_ONE){ onion_listen_point **listen_points=o->listen_points; if (listen_points[1]!=NULL){ ONION_WARNING("Trying to use non-poll and non-thread mode with several listen points. Only the first will be listened"); } onion_listen_point *op=listen_points[0]; do{ onion_request *req=onion_request_new(op); if (!req) continue; ONION_DEBUG("Accepted request %p", req); onion_request_set_no_keep_alive(req); int ret; do{ ret=req->connection.listen_point->read_ready(req); }while(ret>=0); ONION_DEBUG("End of request %p", req); onion_request_free(req); //req->connection.listen_point->close(req); }while(((o->flags&O_ONE_LOOP) == O_ONE_LOOP) && op->listenfd>0); } else{ onion_listen_point **listen_points=o->listen_points; while (*listen_points){ onion_listen_point *p=*listen_points; ONION_DEBUG("Adding listen point fd %d to poller", p->listenfd); onion_poller_slot *slot=onion_poller_slot_new(p->listenfd, (void*)onion_listen_point_accept, p); onion_poller_slot_set_type(slot, O_POLL_ALL); onion_poller_add(o->poller, slot); listen_points++; } #ifdef HAVE_PTHREADS ONION_DEBUG("Start polling / listening %p, %p, %p", o->listen_points, *o->listen_points, *(o->listen_points+1)); if (o->flags&O_THREADED){ o->threads=malloc(sizeof(pthread_t)*(o->nthreads-1)); int i; for (i=0;i<o->nthreads-1;i++){ pthread_create(&o->threads[i],NULL,(void*)onion_poller_poll, o->poller); } // Here is where it waits.. but eventually it will exit at onion_listen_stop onion_poller_poll(o->poller); ONION_DEBUG("Closing onion_listen"); for (i=0;i<o->nthreads-1;i++){ pthread_join(o->threads[i],NULL); } } else #endif onion_poller_poll(o->poller); listen_points=o->listen_points; while (*listen_points){ onion_listen_point *p=*listen_points; if (p->listenfd>0){ ONION_DEBUG("Removing %d from poller", p->listenfd); onion_poller_remove(o->poller, p->listenfd); } listen_points++; } } return 0; }