/* ** This ties it all together. It is called from main(). ** Since the last thing it does is call get_connexion(), ** the parent never returns but the child does numberous times. */ void run_standalone(int server_port) { int fd; /* ** Duplicate this process and close the origional one ** so that the shell will stop waiting. Also, disassociate ** from the controlling terminal and close all file descriptors. */ gu_daemon(PPR_UMASK); /* By convention, PPR processes run in the PPR home directory. */ chdir(HOMEDIR); DODEBUG_MAIN(("entering standalone mode")); create_lock_file(); DODEBUG_MAIN(("starting server on port %d", server_port)); fd = bind_server(server_port); signal_restarting(SIGCHLD, reapchild); standalone_main_loop(fd); } /* run_standalone() */
int create_server_socket(uint16_t port) { int server_s; int sock_opt = 1; int ret; server_s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (server_s == -1) DIE("unable to create socket"); /* accept fd must set noblock for rst */ ret = fd_set_noblock(server_s); if (ret < 0) DIE("fd_set_noblock failed"); /* close server socket on exec so cgi's can't write to it */ if (fcntl(server_s, F_SETFD, 1) == -1) DIE("can't set close-on-exec"); if ((setsockopt(server_s, SOL_SOCKET, SO_REUSEADDR, (void *)&sock_opt, sizeof(sock_opt))) == -1) DIE("setsockopt failed"); /* internet family-specific code encapsulated in bind_server() */ if (bind_server(server_s, NULL, port) == -1) DIE("unable to bind"); if (listen(server_s, 100) == -1) DIE("unable to listen"); return server_s; }
static int create_server_socket(void) { int server_s; server_s = socket(SERVER_AF, SOCK_STREAM, IPPROTO_TCP); if (server_s == -1) { DIE("unable to create socket"); } if (set_nonblock_fd(server_s) == -1) { DIE("fcntl: unable to set server socket to nonblocking"); } if (fcntl(server_s, F_SETFD, 1) == -1) { DIE("can't set close-on-exec on server socket!"); } if ((setsockopt(server_s, SOL_SOCKET, SO_REUSEADDR, (void *) &sock_opt, sizeof (sock_opt))) == -1) { DIE("setsockopt"); } if (bind_server(server_s, server_ip) == -1) { DIE("unable to bind"); } if (listen(server_s, backlog) == -1) { DIE("unable to listen"); } return server_s; }
static int create_server_socket(void) { int server_s; server_s = socket(SERVER_AF, SOCK_STREAM, IPPROTO_TCP); if (server_s == -1) { DIE("unable to create socket"); } /* server socket is nonblocking */ if (set_nonblock_fd(server_s) == -1) { DIE("fcntl: unable to set server socket to nonblocking"); } /* close server socket on exec so cgi's can't write to it */ if (fcntl(server_s, F_SETFD, 1) == -1) { DIE("can't set close-on-exec on server socket!"); } /* reuse socket addr */ if ((setsockopt(server_s, SOL_SOCKET, SO_REUSEADDR, (void *) &sock_opt, sizeof (sock_opt))) == -1) { DIE("setsockopt"); } /* internet family-specific code encapsulated in bind_server() */ if (bind_server(server_s, server_ip) == -1) { DIE("unable to bind"); } /* listen: large number just in case your kernel is nicely tweaked */ if (listen(server_s, backlog) == -1) { DIE("unable to listen"); } return server_s; }
DWORD WinRegRegisterForRPC( PSTR pszServiceName, rpc_binding_vector_p_t* ppServerBinding ) { volatile DWORD dwError = 0; volatile DWORD dwRpcStatus = 0; rpc_binding_vector_p_t pServerBinding = NULL; BOOLEAN bRegistered = FALSE; BOOLEAN bBound = FALSE; BOOLEAN bEPRegistered = FALSE; static ENDPOINT endpoints[] = { {"ncacn_ip_tcp", NULL}, {"ncacn_np" , "\\\\pipe\\\\winreg"}, {NULL , NULL} }; DCETHREAD_TRY { rpc_server_register_if(winreg_v1_0_s_ifspec, NULL, NULL, (unsigned32*)&dwRpcStatus); } DCETHREAD_CATCH_ALL(THIS_CATCH) { if (dwRpcStatus == RPC_S_OK) { dwError = dcethread_exc_getstatus(THIS_CATCH); if (!dwError) { dwError = SRVSVC_ERROR_RPC_EXCEPTION_UPON_REGISTER; } } } DCETHREAD_ENDTRY; BAIL_ON_DCE_ERROR(dwError, dwRpcStatus); BAIL_ON_SRVSVC_ERROR(dwError); bRegistered = TRUE; SRVSVC_LOG_INFO("RPC Service registered successfully."); DCETHREAD_TRY { dwError = bind_server(&pServerBinding, winreg_v1_0_s_ifspec, endpoints); } DCETHREAD_CATCH_ALL(THIS_CATCH) { if (!dwError) { dwError = dcethread_exc_getstatus(THIS_CATCH); } if (!dwError) { dwError = SRVSVC_ERROR_RPC_EXCEPTION_UPON_REGISTER; } } DCETHREAD_ENDTRY; BAIL_ON_SRVSVC_ERROR(dwError); bBound = TRUE; DCETHREAD_TRY { rpc_ep_register(winreg_v1_0_s_ifspec, pServerBinding, NULL, (idl_char*)pszServiceName, (unsigned32*)&dwRpcStatus); } DCETHREAD_CATCH_ALL(THIS_CATCH) { if (dwRpcStatus == RPC_S_OK) { dwError = dcethread_exc_getstatus(THIS_CATCH); if (!dwError) { dwError = SRVSVC_ERROR_RPC_EXCEPTION_UPON_REGISTER; } } } DCETHREAD_ENDTRY; BAIL_ON_DCE_ERROR(dwError, dwRpcStatus); BAIL_ON_SRVSVC_ERROR(dwError); bEPRegistered = TRUE; SRVSVC_LOG_INFO("RPC Endpoint registered successfully."); *ppServerBinding = pServerBinding; cleanup: return dwError; error: SRVSVC_LOG_ERROR("Failed to register RPC endpoint. Error Code: [%u]\n", dwError); if (bEPRegistered) { DCETHREAD_TRY { DWORD tmpStatus = 0; rpc_ep_unregister(winreg_v1_0_s_ifspec, pServerBinding, NULL, (unsigned32*)&tmpStatus); } DCETHREAD_CATCH_ALL(THIS_CATCH) DCETHREAD_ENDTRY; } if (bBound) { DCETHREAD_TRY { DWORD tmpStatus = 0; rpc_binding_vector_free(&pServerBinding, (unsigned32*)&tmpStatus); } DCETHREAD_CATCH_ALL(THIS_CATCH) DCETHREAD_ENDTRY; } if (bRegistered) { DCETHREAD_TRY { DWORD tmpStatus = 0; rpc_server_unregister_if(winreg_v1_0_s_ifspec, NULL, (unsigned32*)&tmpStatus); } DCETHREAD_CATCH_ALL(THIS_CATCH) DCETHREAD_ENDTRY; } *ppServerBinding = NULL; goto cleanup; }
static int create_server_socket(void) { int server_s; #ifdef SERVER_SSL if (do_ssl) { if (InitSSLStuff() != 1) { /*TO DO - emit warning the SSL stuff will not work*/ // syslog(LOG_ALERT, "Failure initialising SSL support - "); if (do_sock == 0) { // syslog(LOG_ALERT, " normal sockets disabled, so exiting");fflush(NULL); return 0; } else { // syslog(LOG_ALERT, " supporting normal (unencrypted) sockets only");fflush(NULL); do_sock = 2; } } } else do_sock = 2; #endif /*SERVER_SSL*/ #ifdef SERVER_SSL if(do_sock){ #endif /*SERVER_SSL*/ server_s = socket(SERVER_AF, SOCK_STREAM, IPPROTO_TCP); if (server_s == -1) { DIE("unable to create socket"); } /* server socket is nonblocking */ if (set_nonblock_fd(server_s) == -1) { DIE("fcntl: unable to set server socket to nonblocking"); } /* close server socket on exec so cgi's can't write to it */ if (fcntl(server_s, F_SETFD, 1) == -1) { DIE("can't set close-on-exec on server socket!"); } /* reuse socket addr */ if ((setsockopt(server_s, SOL_SOCKET, SO_REUSEADDR, (void *) &sock_opt, sizeof (sock_opt))) == -1) { DIE("setsockopt"); } /* internet family-specific code encapsulated in bind_server() */ if (bind_server(server_s, server_ip) == -1) { DIE("unable to bind"); } /* listen: large number just in case your kernel is nicely tweaked */ if (listen(server_s, backlog) == -1) { DIE("unable to listen"); } return server_s; #ifdef SERVER_SSL } else return -1; #endif /*SERVER_SSL*/ }
DWORD SrvSvcRegisterForRPC( PSTR pszServiceName, rpc_binding_vector_p_t* ppServerBinding ) { volatile DWORD dwError = 0; volatile DWORD dwRpcStatus = 0; rpc_binding_vector_p_t pServerBinding = NULL; BOOLEAN bRegistered = FALSE; BOOLEAN bBound = FALSE; BOOLEAN bEPRegistered = FALSE; static ENDPOINT endpoints[] = { {"ncacn_np", "\\\\pipe\\\\srvsvc"}, {"ncalrpc", NULL}, // endpoint is fetched from config parameter {NULL, NULL}, // placeholder for ncacn_ip_tcp (if enabled) {NULL, NULL} }; DWORD i = 0; PSTR lpcSocketPath = NULL; BOOLEAN registerTcpIp = FALSE; dwError = SrvSvcConfigGetLpcSocketPath(&lpcSocketPath); BAIL_ON_SRVSVC_ERROR(dwError); // Fill in the socket path for local procedure calls (ncalrpc) while (endpoints[i].protocol) { if (lpcSocketPath && LwRtlCStringIsEqual(endpoints[i].protocol, "ncalrpc", TRUE)) { endpoints[i].endpoint = lpcSocketPath; } i++; } dwError = SrvSvcConfigGetRegisterTcpIp(®isterTcpIp); BAIL_ON_SRVSVC_ERROR(dwError); // Append ncacn_ip_tcp endpoint if it's enabled in the configuration if (registerTcpIp) { endpoints[i++].protocol = "ncacn_ip_tcp"; } DCETHREAD_TRY { rpc_server_register_if (srvsvc_v3_0_s_ifspec, NULL, NULL, (unsigned32*)&dwRpcStatus); } DCETHREAD_CATCH_ALL(THIS_CATCH) { if ( dwRpcStatus == RPC_S_OK ) { dwError = dcethread_exc_getstatus (THIS_CATCH); if(!dwError) { dwError = SRVSVC_ERROR_RPC_EXCEPTION_UPON_REGISTER; } } } DCETHREAD_ENDTRY; BAIL_ON_DCE_ERROR(dwError, dwRpcStatus); BAIL_ON_SRVSVC_ERROR(dwError); bRegistered = TRUE; SRVSVC_LOG_INFO("RPC Service registered successfully."); DCETHREAD_TRY { dwError = bind_server(&pServerBinding, srvsvc_v3_0_s_ifspec, endpoints); } DCETHREAD_CATCH_ALL(THIS_CATCH) { if(!dwError) { dwError = dcethread_exc_getstatus (THIS_CATCH); } if(!dwError) { dwError = SRVSVC_ERROR_RPC_EXCEPTION_UPON_REGISTER; } } DCETHREAD_ENDTRY; BAIL_ON_SRVSVC_ERROR(dwError); bBound = TRUE; DCETHREAD_TRY { rpc_ep_register(srvsvc_v3_0_s_ifspec, pServerBinding, NULL, (idl_char*)pszServiceName, (unsigned32*)&dwRpcStatus); } DCETHREAD_CATCH_ALL(THIS_CATCH) { if ( dwRpcStatus == RPC_S_OK ) { dwError = dcethread_exc_getstatus (THIS_CATCH); if(!dwError) { dwError = SRVSVC_ERROR_RPC_EXCEPTION_UPON_REGISTER; } } } DCETHREAD_ENDTRY; BAIL_ON_DCE_ERROR(dwError, dwRpcStatus); BAIL_ON_SRVSVC_ERROR(dwError); bEPRegistered = TRUE; SRVSVC_LOG_INFO("RPC Endpoint registered successfully."); *ppServerBinding = pServerBinding; cleanup: // DCE/RPC runtime makes a copy of ncalrpc socket path internally // so it is safe to free it here LW_SAFE_FREE_MEMORY(lpcSocketPath); return dwError; error: SRVSVC_LOG_ERROR("Failed to register RPC endpoint. Error Code: [%u]\n", dwError); if (bEPRegistered) { DCETHREAD_TRY { DWORD tmpStatus = 0; rpc_ep_unregister(srvsvc_v3_0_s_ifspec, pServerBinding, NULL, (unsigned32*)&tmpStatus); } DCETHREAD_CATCH_ALL(THIS_CATCH) DCETHREAD_ENDTRY; } if (bBound) { DCETHREAD_TRY { DWORD tmpStatus = 0; rpc_binding_vector_free(&pServerBinding, (unsigned32*)&tmpStatus); } DCETHREAD_CATCH_ALL(THIS_CATCH) DCETHREAD_ENDTRY; } if (bRegistered) { DCETHREAD_TRY { DWORD tmpStatus = 0; rpc_server_unregister_if (srvsvc_v3_0_s_ifspec, NULL, (unsigned32*)&tmpStatus); } DCETHREAD_CATCH_ALL(THIS_CATCH) DCETHREAD_ENDTRY; } *ppServerBinding = NULL; goto cleanup; }
int main(int argc, char *argv[]) { unsigned32 status; rpc_binding_vector_p_t server_binding; char * string_binding; unsigned32 i; const char * protocol = NULL; const char * endpoint = NULL; int c; /* * Process the cmd line args */ while ((c = getopt(argc, argv, "e:nut")) != EOF) { switch (c) { case 'e': endpoint = optarg; break; case 'n': protocol = PROTOCOL_NP; break; case 'u': protocol = PROTOCOL_UDP; break; case 't': protocol = PROTOCOL_TCP; break; default: usage(); } } #if 0 if (endpoint && !protocol) { printf("ERROR: protocol is required when endpoint is specified\n"); exit(1); } #else if(!endpoint || !protocol) { usage(); exit(1); } #endif #ifndef _WIN32 /* Temporarily disable using all protocols because something is currently busted on Unix */ if (!protocol) { protocol = PROTOCOL_TCP; } #endif /* * Register the Interface with the local endpoint mapper (rpcd) */ printf ("Registering server.... \n"); rpc_server_register_if(echo_v1_0_s_ifspec, NULL, NULL, &status); chk_dce_err(status, "rpc_server_register_if()", "", 1); printf("registered.\nPreparing binding handle...\n"); bind_server(&server_binding, echo_v1_0_s_ifspec, protocol, endpoint); if(!endpoint) { /* * Register bindings with the endpoint mapper */ printf("registering bindings with endpoint mapper\n"); rpc_ep_register(echo_v1_0_s_ifspec, server_binding, NULL, (unsigned char *)"QDA application server", &status); chk_dce_err(status, "rpc_ep_register()", "", 1); } printf("registered.\n"); /* * Print out the servers endpoints (TCP and UDP port numbers) */ printf ("Server's communications endpoints are:\n"); for (i=0; i<RPC_FIELD_COUNT(server_binding); i++) { rpc_binding_to_string_binding(RPC_FIELD_BINDING_H(server_binding)[i], (unsigned char **)&string_binding, &status); if (string_binding) printf("\t%s\n", string_binding); } #ifndef _WIN32 /* * Start the signal waiting thread in background. This thread will * Catch SIGINT and gracefully shutdown the server. */ wait_for_signals(); #endif /* * Begin listening for calls */ printf ("listening for calls....\n"); DCETHREAD_TRY { rpc_server_listen(rpc_c_listen_max_calls_default, &status); } DCETHREAD_CATCH_ALL(THIS_CATCH) { printf ("Server stoppped listening\n"); } DCETHREAD_ENDTRY; /* * If we reached this point, then the server was stopped, most likely * by the signal handler thread called rpc_mgmt_stop_server(). * gracefully cleanup and unregister the bindings from the * endpoint mapper. */ #ifndef _WIN32 /* * Kill the signal handling thread */ #endif if (!endpoint) { printf ("Unregistering server from the endpoint mapper....\n"); rpc_ep_unregister(echo_v1_0_s_ifspec, server_binding, NULL, &status); chk_dce_err(status, "rpc_ep_unregister()", "", 0); } /* * retire the binding information */ printf("Cleaning up communications endpoints...\n"); rpc_server_unregister_if(echo_v1_0_s_ifspec, NULL, &status); chk_dce_err(status, "rpc_server_unregister_if()", "", 0); exit(0); }