int main(int argc, char **argv) { calcService calc; #ifdef WITH_OPENSSL // SOAP_SOCKET m; // struct soap *tsoap; if (soap_ssl_server_context(&calc, SOAP_SSL_DEFAULT | SOAP_SSL_SKIP_HOST_CHECK, /* use SOAP_SSL_REQUIRE_CLIENT_AUTHENTICATION to verify clients: client must provide a key file e.g. "client.pem" and "password" */ "server.pem", /* keyfile (cert+key): see README.txt to create this file */ "password", /* password to read the private key in the key file */ "cacert.pem", /* cacert file to store trusted certificates (to authenticate clients), see README.txt */ NULL, /* capath */ "dh2048.pem", /* DH file name or DH param key len bits in string (e.g. "2048"), if NULL then RSA with 2048 bits is used instead (bits defined by SOAP_SSL_RSA_BITS) */ NULL, /* if randfile!=NULL: use a file with random data to seed randomness */ "sslserver" /* server identification for SSL session cache (unique server name, e.g. use argv[0]) */ )) { soap_print_fault(&calc, stderr); exit(1); } calc.accept_timeout = 60; /* server times out after 1 minute inactivity */ calc.send_timeout = calc.recv_timeout = 30; /* if I/O stalls, then timeout after 30 seconds */ if (!soap_valid_socket(calc.bind(NULL, 2012, 100))) { calc.soap_stream_fault(std::cerr); exit(1); } std::cerr << "Server running" << std::endl; for (;;) { if (!soap_valid_socket(calc.accept()) || calc.ssl_accept() || calc.serve()) calc.soap_stream_fault(std::cerr); } // m = soap_bind(&calc, NULL, 2012, 100); // if (!soap_valid_socket(m)) // { // soap_print_fault(&calc, stderr); // exit(1); // } // fprintf(stderr, "Bind successful: socket = %d\n", m); // for (;;) // { // SOAP_SOCKET s = soap_accept(&calc); // if (!soap_valid_socket(s)) // { // if (calc.errnum) // soap_print_fault(&calc, stderr); // else // fprintf(stderr, "Server timed out (timeout set to %d seconds)\n", calc.accept_timeout); // break; // } // fprintf(stderr, "Socket %d connection from IP %d.%d.%d.%d\n", s, (int)(calc.ip>>24)&0xFF, (int)(calc.ip>>16)&0xFF, (int)(calc.ip>>8)&0xFF, (int)calc.ip&0xFF); //// tsoap = soap_copy(&calc); //// if (!tsoap) //// { //// soap_closesock(&calc); //// printf("continue ... \n"); //// continue; //// } // if (soap_ssl_accept(&calc) != SOAP_OK) // { /* when soap_ssl_accept() fails, socket is closed and SSL data reset */ // printf("continue 1 ... \n"); // soap_print_fault(&calc, stderr); // fprintf(stderr, "SSL request failed, continue with next call...\n"); // } // printf("continue 2 ... \n"); // } #else if (argc < 2) calc.serve(); /* serve as CGI application */ else { int port = atoi(argv[1]); if (!port) { fprintf(stderr, "Usage: calcserver++ <port>\n"); exit(0); } /* run iterative server on port until fatal error */ if (calc.run(port)) { calc.soap_stream_fault(std::cerr); exit(-1); } } #endif return 0; }
//////////////////////////////////////////////////////////////////////////////// // // Main // //////////////////////////////////////////////////////////////////////////////// int main(int argc, char **argv) { (void) signal(SIGINT, signalhandler_func); #if 1 struct soap soap; char *s = getenv("TMPDIR"); if (s) TMPDIR = s; // use HTTP chunking when possible // chunking allows streaming of DIME content without requiring DIME attachment size to be set // DIME attachments can be streamed without chunking only if the attachment size is set soap_init1(&soap, SOAP_IO_KEEPALIVE | SOAP_IO_CHUNK); // set DIME callbacks soap.fdimereadopen = dime_read_open; soap.fdimereadclose = dime_read_close; soap.fdimeread = dime_read; soap.fdimewriteopen = dime_write_open; soap.fdimewriteclose = dime_write_close; soap.fdimewrite = dime_write; #ifdef _POSIX_THREADS pthread_t tid; #endif struct soap *tsoap; int port = 8085; int m, sk, i; if (soap_ssl_server_context(&soap, SOAP_SSL_DEFAULT | SOAP_SSL_SKIP_HOST_CHECK, /* use SOAP_SSL_REQUIRE_CLIENT_AUTHENTICATION to verify clients: client must provide a key file e.g. "client.pem" and "password" */ "server.pem", /* keyfile (cert+key): see README.txt to create this file */ "password", /* password to read the private key in the key file */ "cacert.pem", /* cacert file to store trusted certificates (to authenticate clients), see README.txt */ NULL, /* capath */ "dh2048.pem", /* DH file name or DH param key len bits in string (e.g. "2048"), if NULL then RSA with 2048 bits is used instead (bits defined by SOAP_SSL_RSA_BITS) */ NULL, /* if randfile!=NULL: use a file with random data to seed randomness */ "sslserver" /* server identification for SSL session cache (unique server name, e.g. use argv[0]) */ )) { soap_print_fault(&soap, stderr); exit(1); } // Unix SIGPIPE, this is OS dependent: // soap.accept_flags = SO_NOSIGPIPE; // some systems like this // soap.socket_flags = MSG_NOSIGNAL; // others need this // signal(SIGPIPE, sigpipe_handle); // or a sigpipe handler (portable) // port is first command line argument //port = atoi(argv[1]); // bind to current host and specified port m = soap_bind(&soap, NULL, port, BACKLOG); // if we could not bind, exit if (m < 0) { soap_print_fault(&soap, stderr); exit(1); } fprintf(stderr, "Socket connection successful %d, port %d \n", m, port); // die after 24 hrs waiting for activity on port soap.accept_timeout = 24*60*60; // IO timeouts soap.send_timeout = 30; soap.recv_timeout = 30; // loop through requests //for (i = 1; ; i++) while(is_main_running) { // accept request sk = soap_accept(&soap); // if timeout or error, report if (sk < 0) { if (soap.errnum) soap_print_fault(&soap, stderr); else fprintf(stderr, "Server timed out\n"); break; } fprintf(stderr, "Thread %d accepts socket %d connection from IP %d.%d.%d.%d\n", i, s, (int)(soap.ip>>24)&0xFF, (int)(soap.ip>>16)&0xFF, (int)(soap.ip>>8)&0xFF, (int)soap.ip&0xFF); // copy soap environment and spawn thread (if Pthreads is installed) tsoap = soap_copy(&soap); #ifdef _POSIX_THREADS pthread_create(&tid, NULL, (void*(*)(void*))process_request, (void*)tsoap); #else process_request((void*)tsoap); #endif } // detach soap_done(&soap); #endif return 0; }
int main(int argc, char *argv[]) { SOAP_SOCKET m; #if defined(_POSIX_THREADS) || defined(_SC_THREADS) pthread_t tid; pthread_mutex_init(&global_flag, NULL); pthread_mutex_init(&libssh2_flag, NULL); pthread_cond_init(&termination_flag, NULL); #endif struct soap soap, *tsoap = NULL; psoap = &soap; int ch, msglevel = LOG_INFO; static char *USAGE = "\nUSAGE:\noph_server [-d] [-l <log_file>] [-p <port>] [-v] [-w]\n"; fprintf(stdout, "%s", OPH_VERSION); fprintf(stdout, "%s", OPH_DISCLAIMER); set_debug_level(msglevel + 10); while ((ch = getopt(argc, argv, "dhl:p:vwxz")) != -1) { switch (ch) { case 'd': msglevel = LOG_DEBUG; break; case 'h': fprintf(stdout, "%s", USAGE); return 0; case 'l': oph_log_file_name = optarg; break; case 'p': oph_server_port = optarg; break; case 'v': return 0; break; case 'w': if (msglevel < LOG_WARNING) msglevel = LOG_WARNING; break; case 'x': fprintf(stdout, "%s", OPH_WARRANTY); return 0; case 'z': fprintf(stdout, "%s", OPH_CONDITIONS); return 0; default: fprintf(stdout, "%s", USAGE); return 0; } } set_debug_level(msglevel + 10); pmesg(LOG_INFO, __FILE__, __LINE__, "Selected log level %d\n", msglevel); #ifdef OPH_SERVER_LOCATION oph_server_location = strdup(OPH_SERVER_LOCATION); #else oph_server_location = getenv(OPH_SERVER_LOCATION_STR); if (!oph_server_location) { fprintf(stderr, "OPH_SERVER_LOCATION has to be set\n"); return 1; } #endif pmesg(LOG_DEBUG, __FILE__, __LINE__, "Server location '%s'\n", oph_server_location); char configuration_file[OPH_MAX_STRING_SIZE]; snprintf(configuration_file, OPH_MAX_STRING_SIZE, OPH_CONFIGURATION_FILE, oph_server_location); set_global_values(configuration_file); if (oph_log_file_name) { if (logfile) fclose(logfile); if (!(logfile = fopen(oph_log_file_name, "a"))) { fprintf(stderr, "Wrong log file name '%s'\n", oph_log_file_name); return 1; } pmesg(LOG_INFO, __FILE__, __LINE__, "Selected log file '%s'\n", oph_log_file_name); if (logfile) set_log_file(logfile); } else oph_log_file_name = hashtbl_get(oph_server_params, OPH_SERVER_CONF_LOGFILE); int int_port = strtol(oph_server_port, NULL, 10); if (oph_handle_signals()) { pmesg(LOG_ERROR, __FILE__, __LINE__, "A problem occurred while setting up signal dispositions\n"); exit(1); } if (mysql_library_init(0, 0, 0)) { pmesg(LOG_ERROR, __FILE__, __LINE__, "Cannot setup MySQL\n"); exit(1); } oph_tp_start_xml_parser(); if (CRYPTO_thread_setup()) { pmesg(LOG_ERROR, __FILE__, __LINE__, "Cannot setup thread mutex for OpenSSL\n"); exit(1); } soap_init(&soap); soap.fget = oph_http_get; if (soap_register_plugin(&soap, oph_plugin)) { pmesg(LOG_ERROR, __FILE__, __LINE__, "Cannot register %s plugin\n", OPH_PLUGIN_ID); soap_print_fault(&soap, stderr); cleanup(); exit(-1); } // Register serverid struct oph_plugin_data *state = NULL; if (!(state = (struct oph_plugin_data *) soap_lookup_plugin(&soap, OPH_PLUGIN_ID))) { pmesg(LOG_ERROR, __FILE__, __LINE__, "Error on lookup plugin struct\n"); soap_print_fault(&soap, stderr); cleanup(); exit(-1); } state->serverid = strdup(oph_web_server); #ifdef WITH_OPENSSL /* init gsoap context and SSL */ if (soap_ssl_server_context(&soap, SOAP_TLSv1_2, oph_server_cert, oph_server_password, oph_server_ca, NULL, NULL, NULL, NULL)) { pmesg(LOG_ERROR, __FILE__, __LINE__, "SSL Server Context Error\n"); soap_print_fault(&soap, stderr); cleanup(); exit(1); } #endif soap.accept_timeout = oph_server_inactivity_timeout; soap.send_timeout = soap.recv_timeout = oph_server_timeout; soap.bind_flags |= SO_REUSEADDR; m = soap_bind(&soap, NULL, int_port, 100); if (!soap_valid_socket(m)) { pmesg(LOG_ERROR, __FILE__, __LINE__, "Soap invalid socket\n"); soap_print_fault(&soap, stderr); cleanup(); exit(1); } pmesg(LOG_DEBUG, __FILE__, __LINE__, "Bind successful: socket = %d\n", m); for (;;) { SOAP_SOCKET s = soap_accept(&soap); if (!soap_valid_socket(s)) { if (soap.errnum) { pmesg(LOG_ERROR, __FILE__, __LINE__, "Soap invalid socket\n"); soap_print_fault(&soap, stderr); } else pmesg(LOG_ERROR, __FILE__, __LINE__, "Server timed out (timeout set to %d seconds)\n", soap.accept_timeout); break; } tsoap = soap_copy(&soap); if (!tsoap) { soap_closesock(&soap); continue; } #if defined(_POSIX_THREADS) || defined(_SC_THREADS) pthread_create(&tid, NULL, (void *(*)(void *)) &process_request, tsoap); #else process_request(tsoap); #endif } cleanup(); return 0; }