/*+++++++++++++ OnionServer-Class ++++++++++++++++++ */ int OnionServer::start_server() { onion_url *url=onion_root_url(m_ponion); const char *host, *port; if( m_psettingKinectGrid != NULL){ host = m_psettingKinectGrid->getString("host"); port = m_psettingKinectGrid->getString("port"); }else{ host = "0.0.0.0"; port = "8080"; } onion_set_hostname(m_ponion, host); // Force ipv4. onion_set_port(m_ponion, port); onion_url_add_with_data(url, "kinectgrid_settings.js", (void*)insert_json, m_psettingKinect, NULL); onion_url_add_with_data(url, "index.html", (void*)index_html, m_psettingKinectGrid, NULL); onion_url_add_with_data(url, "", (void*)index_html, m_psettingKinectGrid, NULL); //onion_url_add_with_data(url, "index.html", (void*)checkFormularValues, this, NULL); //onion_url_add_with_data(url, "", (void*)checkFormularValues, this, NULL); onion_url_add_with_data(url, "json", (void*)checkFormularValues, this, NULL); onion_url_add(url, "^.*$", (void*)search_file); /* Now, m_ponion get the O_DETACH_LISTEN flag on creation and the Extra thread is omitable. */ //start loop as thread //return pthread_create( &m_pthread, NULL, &start_myonion_server, m_ponion); onion_listen(m_ponion);//loop return 0; }
int main(int argc, char *argv[]) { printf("Datagram hash client example\n\n"); //Check if arguments of sufficient size if (argc != 4) { fprintf(stderr,"Usage: udpClient serverName serverPort ownPort\n"); exit(EXIT_FAILURE); } //build sockets buildSocket(argv); //Init favicon initializeFavicon(argv); //TODO: MAIN LOOP onion_listen(o); onion_free(o); //Close socket close(sockfd); return EXIT_SUCCESS; }
void t05_server_timeout_threaded_ssl(){ INIT_LOCAL(); CURL *curl=prepare_curl("https://localhost:8081"); ONION_DEBUG("%s",__FUNCTION__); o=onion_new(O_THREADED | O_DETACH_LISTEN); onion_set_root_handler(o,onion_handler_new((void*)process_request,NULL,NULL)); FAIL_IF_NOT_EQUAL_INT(onion_set_certificate(o, O_SSL_CERTIFICATE_KEY, "mycert.pem", "mycert.pem"),0); onion_set_port(o,"8081"); onion_set_timeout(o,3000); onion_listen(o); sleep(1); int fd=connect_to("localhost","8081"); sleep(4); // Should have closed the connection int w=write(fd,"GET /\n\n",7); FAIL_IF_NOT_EQUAL_INT(w,7); char data[256]; FAIL_IF(read(fd, data,sizeof(data))>0); close(fd); FAIL_IF_NOT(curl_get(curl, "https://localhost:8081")); onion_free(o); curl_easy_cleanup(curl); END_LOCAL(); }
void t06_timeouts(){ INIT_LOCAL(); o=onion_new(O_POOL | O_DETACH_LISTEN); onion_set_timeout(o, 100); onion_set_root_handler(o, onion_handler_new((void*)wait_random, NULL, NULL)); onion_set_port(o, "8081"); onion_listen(o); sleep(1); int nthreads=10; pthread_t *thread=malloc(sizeof(pthread_t*)*nthreads); int i; for (i=0;i<nthreads;i++){ pthread_create(&thread[i], NULL, (void*)do_timeout_request, NULL); } for (i=0;i<nthreads;i++){ pthread_join(thread[i], NULL); } free(thread); onion_free(o); END_LOCAL(); }
int main() { request_list_t request_list; request_list.reqres = onion_ptr_list_new(); request_list.running = true; pthread_mutex_init(&request_list.lock, NULL); pthread_t long_process_thread; o = onion_new(O_POOL); pthread_create(&long_process_thread, NULL, (void *)&long_process, &request_list); onion_set_root_handler(o, onion_handler_new(&handler, &request_list, NULL)); ONION_INFO("Listening at http://localhost:8080"); onion_listen(o); // Close request_list.running = false; onion_ptr_list_foreach(request_list.reqres, (void *)free_reqres); onion_ptr_list_free(request_list.reqres); pthread_join(long_process_thread, NULL); onion_free(o); return 0; }
void t04_server_timeout_threaded(){ INIT_LOCAL(); CURL *curl=prepare_curl("http://localhost:8082"); o=onion_new(O_THREADED | O_DETACH_LISTEN); onion_set_root_handler(o,onion_handler_new((void*)process_request,NULL,NULL)); onion_set_port(o,"8082"); onion_set_timeout(o,2000); onion_listen(o); sleep(1); int fd=connect_to("localhost","8082"); sleep(3); // Should have closed the connection int w=write(fd,"GET /\n\n",7); FAIL_IF_NOT_EQUAL_INT(w,7); char data[256]; FAIL_IF(read(fd, data,sizeof(data))>0); close(fd); FAIL_IF_NOT(curl_get(curl, "http://localhost:8082")); onion_free(o); curl_easy_cleanup(curl); END_LOCAL(); }
static void* onion_listen_start (void* data) { onion* o = data; pthread_mutex_lock (&count_mtx); listen_counter++; #ifdef __DEBUG__ #ifdef __USE_GNU long cnt = listen_counter; #endif /*__USE_GNU */ #endif /*__DEBUG__*/ pthread_mutex_unlock (&count_mtx); #ifdef __DEBUG__ #ifdef __USE_GNU /* GNU systems such as Linux with GNU glibc have the non-standard pthread_setname_np */ { char thrname[16]; memset (thrname, 0, sizeof(thrname)); snprintf (thrname, sizeof(thrname), "onlisten%ld", cnt); pthread_setname_np (pthread_self(), thrname); } #endif /*__USE_GNU */ #endif /*__DEBUG__*/ onion_listen (o); return NULL; }
void *listen_thread_f(void *_){ ok_listening=1; ONION_INFO("Start listening"); onion_listen(o); ONION_INFO("End listening"); ok_listening=0; return NULL; }
int main(void) { onion *o = onion_new(O_POOL); onion_url *root = onion_root_url(o); onion_url_add_handler(root, "^wd", onion_handler_webdav(".", NULL)); onion_listen(o); return 0; }
int main(int argc, char **argv){ onion *onion=onion_new(O_ONE_LOOP); onion_handler *root=onion_handler_new((onion_handler_handler)method, NULL, NULL); onion_set_root_handler(onion, root); onion_listen(onion); return 0; }
int main(int argc, char **argv){ o=onion_new(O_ONE_LOOP); onion_handler *root=onion_handler_new((onion_handler_handler)sessions, NULL, NULL); onion_set_root_handler(o, root); signal(SIGINT, free_onion); onion_listen(o); return 0; }
int main(){ onion *o=onion_new(O_THREADED); onion_url *urls=onion_root_url(o); onion_url_add(urls, "", websocket_example); onion_listen(o); onion_free(o); return 0; }
/** * @short In a simple loop asks the user for the answer of the query. */ int main(int argc, char **argv){ onion *server; server=onion_new(O_ONE_LOOP); onion_set_hostname(server, "0.0.0.0"); onion_set_root_handler(server, onion_handler_new((void*)ask_handler, NULL, NULL)); onion_listen(server); onion_free(server); 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; }
void t02_post_a_lot() { INIT_LOCAL(); onion *o = onion_new(O_ONE); onion_set_root_handler(o, onion_handler_new((void *)return_length, NULL, NULL)); pthread_t pt; pthread_create(&pt, NULL, (void *)POST_a_lot, NULL); onion_listen(o); pthread_join(pt, NULL); onion_free(o); END_LOCAL(); }
void t01_post_no_type() { INIT_LOCAL(); onion *o = onion_new(O_ONE); onion_set_root_handler(o, onion_handler_new((void *)process_request, NULL, NULL)); pthread_t pt; pthread_create(&pt, NULL, (void *)POST_json, NULL); onion_listen(o); pthread_join(pt, NULL); onion_free(o); END_LOCAL(); }
void t03_server_https(){ INIT_LOCAL(); CURL *curl=prepare_curl("https://localhost:8080"); o=onion_new(O_ONE_LOOP | O_DETACH_LISTEN); onion_set_root_handler(o,onion_handler_new((void*)process_request,NULL,NULL)); FAIL_IF_NOT_EQUAL_INT(onion_set_certificate(o, O_SSL_CERTIFICATE_KEY, "mycert.pem", "mycert.pem"),0); FAIL_IF_NOT_EQUAL_INT(onion_listen(o),0); //do_petition_set(1,1,1,1); sleep(1); //FAIL_IF_EQUAL_INT( curl_get_to_fail("http://localhost:8080"), HTTP_OK); sleep(1); FAIL_IF_NOT_EQUAL_INT( curl_get(curl, "https://localhost:8080"), HTTP_OK); sleep(1); onion_free(o); curl_easy_cleanup(curl); END_LOCAL(); }
int main(int argc, char **argv){ o=onion_new(O_ONE_LOOP); #ifdef HAVE_REDIS onion_sessions *session_backend=onion_sessions_redis_new("localhost",6379); onion_set_session_backend(o, session_backend); #endif onion_url *root=onion_root_url(o); onion_url_add_handler(root, "status",onion_internal_status()); onion_url_add_handler(root, "^.*", onion_handler_new((onion_handler_handler)sessions, NULL, NULL)); signal(SIGINT, free_onion); onion_listen(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()); 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) { signal(SIGINT, shutdown_server); signal(SIGTERM, shutdown_server); ONION_VERSION_IS_COMPATIBLE_OR_ABORT(); o = onion_new(O_POOL); onion_set_timeout(o, 5000); onion_set_hostname(o, "0.0.0.0"); onion_url *urls = onion_root_url(o); onion_url_add_static(urls, "static", "Hello static world", HTTP_OK); onion_url_add(urls, "timeout", random_timeout); onion_url_add(urls, "", hello); onion_url_add(urls, "^(.*)$", hello); 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; }
void t01_listen_port() { INIT_LOCAL(); if (!geteuid()) { // current user is root // set user to nobody struct passwd * pwd = calloc(1, sizeof(struct passwd)); FAIL_IF_NOT(pwd); size_t buffer_length = sysconf(_SC_GETPW_R_SIZE_MAX); FAIL_IF_NOT(buffer_length > 0); char * buffer = malloc(buffer_length * sizeof(char)); FAIL_IF_NOT(buffer); int lookup_result = getpwnam_r("nobody", pwd, buffer, buffer_length, &pwd); FAIL_IF(lookup_result); FAIL_IF_NOT(pwd); int setuid_result = setuid(pwd->pw_uid); FAIL_IF(setuid_result); free(pwd); free(buffer); } // current user is not root // it has no permissions to bind to port 88 t01_server=onion_new(O_THREADED); onion_set_max_threads(t01_server, 2); t01_errors_count=0; t01_failed=false; onion_log=t01_listen_port_error_handler; onion_set_port(t01_server, "88"); onion_listen(t01_server); onion_free(t01_server); FAIL_IF(t01_failed); END_LOCAL(); }
int main(int argc, char **argv){ char *port="8080"; char *serverip="::"; const char *command="/bin/bash"; const char *certificatefile="/etc/pki/tls/certs/pound.pem"; const char *keyfile="/etc/pki/tls/certs/pound.key"; int error; int i; int ssl=1; #ifdef HAVE_PAM int use_pam=1; #endif for (i=1;i<argc;i++){ if (strcmp(argv[i],"--help")==0){ show_help(); exit(0); } else if(strcmp(argv[i],"-p")==0 || strcmp(argv[i],"--port")==0){ if (i+1>argc){ ONION_ERROR("Need to set the port number."); show_help(); exit(1); } port=argv[++i]; fprintf(stderr, "Using port %s\n",port); } else if(strcmp(argv[i],"-i")==0 || strcmp(argv[i],"--ip")==0){ if (i+1>argc){ ONION_ERROR("Need to set the ip address or hostname."); show_help(); exit(1); } serverip=argv[++i]; fprintf(stderr, "Using ip %s\n",serverip); } else if(strcmp(argv[i],"-c")==0 || strcmp(argv[i],"--cert")==0){ if (i+1>argc){ ONION_ERROR("Need to set the certificate filename"); show_help(); exit(1); } certificatefile=argv[++i]; ONION_INFO("Using certificate %s",certificatefile); } else if(strcmp(argv[i],"-k")==0 || strcmp(argv[i],"--key")==0){ if (i+1>argc){ ONION_ERROR("Need to set the certificate key filename."); show_help(); exit(1); } keyfile=argv[++i]; ONION_INFO("Using certificate key %s",keyfile); } else if(strcmp(argv[i],"-x")==0 || strcmp(argv[i],"--exec")==0){ if (i+1>argc){ ONION_ERROR("Need the command to execute."); show_help(); exit(1); } command=argv[++i]; ONION_INFO("New terminal execute the command %s",command); } else if(strcmp(argv[i],"--no-ssl")==0){ ssl=0; ONION_INFO("Disabling SSL!"); } #ifdef HAVE_PAM else if(strcmp(argv[i],"--no-pam")==0){ use_pam=0; ONION_INFO("Disabling PAM!"); } #endif } o=onion_new(O_POOL|O_SYSTEMD); // I prepare the url handler, with static, uuid and term. Also added the empty rule that redirects to static/index.html onion_url *url=onion_url_new(); onion_handler *term_handler=oterm_handler(o,command); #ifdef HAVE_PAM if (use_pam){ onion_url_add_handler(url, "^term/", onion_handler_auth_pam("Onion Terminal", "login", term_handler)); } else #endif { onion_url_add_with_data(url, "^term/", oterm_nopam, term_handler, NULL); } onion_url_add_with_data(url, "^uuid/", oterm_uuid, onion_handler_get_private_data(term_handler), NULL); #ifdef __DEBUG__ if (getenv("OTERM_DEBUG")) onion_url_add_handler(url, "^static/", onion_handler_export_local_new("static")); else #endif { onion_url_add(url, "^static/", opack_static); } onion_url_add_with_data(url, "", onion_shortcut_internal_redirect, "static/index.html", NULL); srand(time(NULL)); onion_set_root_handler(o, onion_url_to_handler(url)); if (!(onion_flags(o)&O_SSL_AVAILABLE)){ ONION_WARNING("SSL support is not available. Oterm is in unsecure mode!"); } else if (ssl){ // Not necesary the else, as onion_use_certificate would just return an error. But then it will exit. error=onion_set_certificate(o, O_SSL_CERTIFICATE_KEY, certificatefile, keyfile); if (error){ ONION_ERROR("Cant set certificate and key files (%s, %s)",certificatefile, keyfile); show_help(); exit(1); } } onion_set_port(o, port); onion_set_hostname(o, serverip); onion_set_timeout(o,5000); signal(SIGINT, free_onion); signal(SIGPIPE, SIG_IGN); fprintf(stderr, "Listening at %s\n",port); error=onion_listen(o); if (error){ ONION_ERROR("Cant create the server: %s", strerror(errno)); } onion_free(o); return 0; }
int main(int argc, char **argv){ char *port="8080"; char *hostname="::"; const char *dirname="."; const char *certfile="cert.pem"; const char *pamname="login"; int i; for (i=1;i<argc;i++){ if ((strcmp(argv[i],"--port")==0) || (strcmp(argv[i],"-p")==0)){ port=argv[++i]; ONION_INFO("Listening at port %s",port); } if ((strcmp(argv[i],"--listen")==0) || (strcmp(argv[i],"-l")==0)){ hostname=argv[++i]; ONION_INFO("Listening at hostname %s",hostname); } else if (strcmp(argv[i],"--pem")==0){ if (argc<i+1) return show_help(); certfile=argv[++i]; ONION_INFO("Certificate file set to %s",certfile); } else if (strcmp(argv[i],"--pam")==0){ if (argc<i+1) return show_help(); pamname=argv[++i]; ONION_INFO("Pam name is now %s",pamname); } else if (strcmp(argv[i],"--help")==0 || strcmp(argv[i],"-h")==0){ return show_help(); } else dirname=argv[i]; } upload_file_data data={ dirname }; onion_handler *root=onion_handler_new((void*)upload_file,(void*)&data,NULL); onion_handler *dir=onion_handler_export_local_new(argc==2 ? argv[1] : "."); onion_handler_export_local_set_footer(dir, upload_file_footer); onion_handler_add(dir, onion_handler_static("<h1>404 - File not found.</h1>", 404) ); onion_handler_add(root,dir); onion_handler *pam=onion_handler_auth_pam("Onion Fileserver", pamname, root); o=onion_new(O_THREADED); onion_set_root_handler(o, pam); onion_set_certificate(o, O_SSL_CERTIFICATE_KEY, certfile, certfile); onion_set_port(o, port); onion_set_hostname(o, hostname); signal(SIGINT, free_onion); int error=onion_listen(o); if (error){ perror("Cant create the server"); } onion_free(o); return 0; }
int main(int argc, char **argv){ //onion_log=onion_log_syslog; char *port="8080"; char *hostname="::"; const char *dirname="."; #ifdef HAVE_WEBDAV int withwebdav=1; #endif int i; for (i=1;i<argc;i++){ if ((strcmp(argv[i],"--port")==0) || (strcmp(argv[i],"-p")==0)){ port=argv[++i]; ONION_INFO("Listening at port %s",port); } if ((strcmp(argv[i],"--listen")==0) || (strcmp(argv[i],"-l")==0)){ hostname=argv[++i]; ONION_INFO("Listening at hostname %s",hostname); } else if (strcmp(argv[i],"--help")==0 || strcmp(argv[i],"-h")==0){ return show_help(); } #ifdef HAVE_WEBDAV else if (strcmp(argv[i],"--no-webdav")==0){ ONION_INFO("WebDAV support disabled"); withwebdav=0; } #endif else{ dirname=argv[i]; ONION_INFO("Exporting directory %s", dirname); } } onion_handler *root=onion_handler_new((onion_handler_handler)fileserver_page, (void *)dirname, NULL); #ifdef HAVE_WEBDAV if (withwebdav) onion_handler_add(root, onion_handler_webdav(dirname, NULL)); // fallback. else #endif onion_handler_add(root, onion_handler_export_local_new(dirname)); // This is the root directory where the translations are. #define W "." setenv("LANGUAGE","locale",1); // Remove LANGUAGE env var, set it to the locale name, setlocale(LC_ALL,""); bindtextdomain("locale", W); // This is necesary because of the fake name bindtextdomain("es", W); // One per language supported. bindtextdomain("zh", W); bindtextdomain("fr", W); bindtextdomain("pl", W); textdomain("C"); // Default language // All is configured now, now in hands of dgettext(LANG, txt); o=onion_new(O_POOL); onion_set_root_handler(o, root); onion_set_port(o, port); onion_set_hostname(o, hostname); signal(SIGINT, free_onion); int error=onion_listen(o); if (error){ perror("Cant create the server"); } onion_free(o); return 0; }
void do_listen(){ onion_listen(o); }
*/ bool listen() { return onion_listen(ptr); }