int ap_mpm_run(apr_pool_t *_pconf, apr_pool_t *plog, server_rec *s ) { char *listener_shm_name; parent_info_t *parent_info; ULONG rc; pconf = _pconf; ap_server_conf = s; restart_pending = 0; DosSetMaxFH(ap_thread_limit * 2); listener_shm_name = apr_psprintf(pconf, "/sharemem/httpd/parent_info.%d", getppid()); rc = DosGetNamedSharedMem((PPVOID)&parent_info, listener_shm_name, PAG_READ); is_parent_process = rc != 0; ap_scoreboard_fname = apr_psprintf(pconf, "/sharemem/httpd/scoreboard.%d", is_parent_process ? getpid() : getppid()); if (rc == 0) { /* Child process */ ap_listen_rec *lr; int num_listeners = 0; ap_mpm_accept_mutex = parent_info->accept_mutex; /* Set up a default listener if necessary */ if (ap_listeners == NULL) { ap_listen_rec *lr = apr_pcalloc(s->process->pool, sizeof(ap_listen_rec)); ap_listeners = lr; apr_sockaddr_info_get(&lr->bind_addr, "0.0.0.0", APR_UNSPEC, DEFAULT_HTTP_PORT, 0, s->process->pool); apr_socket_create(&lr->sd, lr->bind_addr->family, SOCK_STREAM, s->process->pool); } for (lr = ap_listeners; lr; lr = lr->next) { apr_sockaddr_t *sa; apr_os_sock_put(&lr->sd, &parent_info->listeners[num_listeners].listen_fd, pconf); apr_socket_addr_get(&sa, APR_LOCAL, lr->sd); num_listeners++; } DosFreeMem(parent_info); /* Do the work */ ap_mpm_child_main(pconf); /* Outta here */ return 1; } else { /* Parent process */ char restart; is_parent_process = TRUE; if (ap_setup_listeners(ap_server_conf) < 1) { ap_log_error(APLOG_MARK, APLOG_ALERT, 0, s, "no listening sockets available, shutting down"); return 1; } ap_log_pid(pconf, ap_pid_fname); restart = master_main(); ++ap_my_generation; ap_scoreboard_image->global->running_generation = ap_my_generation; if (!restart) { const char *pidfile = ap_server_root_relative(pconf, ap_pid_fname); if (pidfile != NULL && remove(pidfile) == 0) { ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, ap_server_conf, "removed PID file %s (pid=%d)", pidfile, getpid()); } ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf, "caught SIGTERM, shutting down"); return 1; } } /* Parent process */ return 0; /* Restart */ }
// virtual LLIOPipe::EStatus LLIOServerSocket::process_impl( const LLChannelDescriptors& channels, buffer_ptr_t& buffer, bool& eos, LLSD& context, LLPumpIO* pump) { PUMP_DEBUG; LLMemType m1(LLMemType::MTYPE_IO_TCP); if(!pump) { llwarns << "Need a pump for server socket." << llendl; return STATUS_ERROR; } if(!mInitialized) { PUMP_DEBUG; // This segment sets up the pump so that we do not call // process again until we have an incoming read, aka connect() // from a remote host. lldebugs << "Initializing poll descriptor for LLIOServerSocket." << llendl; apr_pollfd_t poll_fd; poll_fd.p = NULL; poll_fd.desc_type = APR_POLL_SOCKET; poll_fd.reqevents = APR_POLLIN; poll_fd.rtnevents = 0x0; poll_fd.desc.s = mListenSocket->getSocket(); poll_fd.client_data = NULL; pump->setConditional(this, &poll_fd); mInitialized = true; return STATUS_OK; } // we are initialized, and told to process, so we must have a // socket waiting for a connection. lldebugs << "accepting socket" << llendl; PUMP_DEBUG; apr_pool_t* new_pool = NULL; apr_status_t status = apr_pool_create(&new_pool, mPool); apr_socket_t* socket = NULL; status = apr_socket_accept( &socket, mListenSocket->getSocket(), new_pool); LLSocket::ptr_t llsocket(LLSocket::create(socket, new_pool)); //EStatus rv = STATUS_ERROR; if(llsocket) { PUMP_DEBUG; apr_sockaddr_t* remote_addr; apr_socket_addr_get(&remote_addr, APR_REMOTE, socket); char* remote_host_string; apr_sockaddr_ip_get(&remote_host_string, remote_addr); LLSD context; context["remote-host"] = remote_host_string; context["remote-port"] = remote_addr->port; LLPumpIO::chain_t chain; chain.push_back(LLIOPipe::ptr_t(new LLIOSocketReader(llsocket))); if(mReactor->build(chain, context)) { chain.push_back(LLIOPipe::ptr_t(new LLIOSocketWriter(llsocket))); pump->addChain(chain, mResponseTimeout); status = STATUS_OK; } else { llwarns << "Unable to build reactor to socket." << llendl; } } else { llwarns << "Unable to create linden socket." << llendl; } PUMP_DEBUG; // This needs to always return success, lest it get removed from // the pump. return STATUS_OK; }
// virtual LLIOPipe::EStatus LLIOServerSocket::process_impl( const LLChannelDescriptors& channels, buffer_ptr_t& buffer, bool& eos, LLSD& context, LLPumpIO* pump) { LLFastTimer t(FTM_PROCESS_SERVER_SOCKET); PUMP_DEBUG; if(!pump) { LL_WARNS() << "Need a pump for server socket." << LL_ENDL; return STATUS_ERROR; } if(!mInitialized) { PUMP_DEBUG; // This segment sets up the pump so that we do not call // process again until we have an incoming read, aka connect() // from a remote host. LL_DEBUGS() << "Initializing poll descriptor for LLIOServerSocket." << LL_ENDL; apr_pollfd_t poll_fd; poll_fd.p = NULL; poll_fd.desc_type = APR_POLL_SOCKET; poll_fd.reqevents = APR_POLLIN; poll_fd.rtnevents = 0x0; poll_fd.desc.s = mListenSocket->getSocket(); poll_fd.client_data = NULL; pump->setConditional(this, &poll_fd); mInitialized = true; return STATUS_OK; } // we are initialized, and told to process, so we must have a // socket waiting for a connection. LL_DEBUGS() << "accepting socket" << LL_ENDL; PUMP_DEBUG; apr_status_t status; LLSocket::ptr_t llsocket(LLSocket::create(status, mListenSocket)); //EStatus rv = STATUS_ERROR; if(llsocket && status == APR_SUCCESS) { PUMP_DEBUG; apr_sockaddr_t* remote_addr; apr_socket_addr_get(&remote_addr, APR_REMOTE, llsocket->getSocket()); char* remote_host_string; apr_sockaddr_ip_get(&remote_host_string, remote_addr); LLSD context; context["remote-host"] = remote_host_string; context["remote-port"] = remote_addr->port; LLPumpIO::chain_t chain; chain.push_back(LLIOPipe::ptr_t(new LLIOSocketReader(llsocket))); if(mReactor->build(chain, context)) { chain.push_back(LLIOPipe::ptr_t(new LLIOSocketWriter(llsocket))); pump->addChain(chain, mResponseTimeout); } else { LL_WARNS() << "Unable to build reactor to socket." << LL_ENDL; } } else { char buf[256]; LL_WARNS() << "Unable to accept linden socket: " << apr_strerror(status, buf, sizeof(buf)) << LL_ENDL; } PUMP_DEBUG; // This needs to always return success, lest it get removed from // the pump. return STATUS_OK; }
static mrcp_connection_t* mrcp_client_agent_connection_create(mrcp_connection_agent_t *agent, mrcp_control_descriptor_t *descriptor) { char *local_ip = NULL; char *remote_ip = NULL; apt_pollset_t *pollset = apt_poller_task_pollset_get(agent->task); mrcp_connection_t *connection = mrcp_connection_create(); apr_sockaddr_info_get(&connection->r_sockaddr,descriptor->ip.buf,APR_INET,descriptor->port,0,connection->pool); if(!connection->r_sockaddr) { mrcp_connection_destroy(connection); return NULL; } if(apr_socket_create(&connection->sock,connection->r_sockaddr->family,SOCK_STREAM,APR_PROTO_TCP,connection->pool) != APR_SUCCESS) { mrcp_connection_destroy(connection); return NULL; } apr_socket_opt_set(connection->sock, APR_SO_NONBLOCK, 0); apr_socket_timeout_set(connection->sock, -1); apr_socket_opt_set(connection->sock, APR_SO_REUSEADDR, 1); if(apr_socket_connect(connection->sock, connection->r_sockaddr) != APR_SUCCESS) { apr_socket_close(connection->sock); mrcp_connection_destroy(connection); return NULL; } if(apr_socket_addr_get(&connection->l_sockaddr,APR_LOCAL,connection->sock) != APR_SUCCESS) { apr_socket_close(connection->sock); mrcp_connection_destroy(connection); return NULL; } apr_sockaddr_ip_get(&local_ip,connection->l_sockaddr); apr_sockaddr_ip_get(&remote_ip,connection->r_sockaddr); connection->id = apr_psprintf(connection->pool,"%s:%hu <-> %s:%hu", local_ip,connection->l_sockaddr->port, remote_ip,connection->r_sockaddr->port); memset(&connection->sock_pfd,0,sizeof(apr_pollfd_t)); connection->sock_pfd.desc_type = APR_POLL_SOCKET; connection->sock_pfd.reqevents = APR_POLLIN; connection->sock_pfd.desc.s = connection->sock; connection->sock_pfd.client_data = connection; if(apt_pollset_add(pollset, &connection->sock_pfd) != TRUE) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Add to Pollset %s",connection->id); apr_socket_close(connection->sock); mrcp_connection_destroy(connection); return NULL; } apt_log(APT_LOG_MARK,APT_PRIO_NOTICE,"Established TCP/MRCPv2 Connection %s",connection->id); connection->agent = agent; connection->it = apt_list_push_back(agent->connection_list,connection,connection->pool); connection->parser = mrcp_parser_create(agent->resource_factory,connection->pool); connection->generator = mrcp_generator_create(agent->resource_factory,connection->pool); connection->tx_buffer_size = agent->tx_buffer_size; connection->tx_buffer = apr_palloc(connection->pool,connection->tx_buffer_size+1); connection->rx_buffer_size = agent->rx_buffer_size; connection->rx_buffer = apr_palloc(connection->pool,connection->rx_buffer_size+1); apt_text_stream_init(&connection->rx_stream,connection->rx_buffer,connection->rx_buffer_size); if(apt_log_masking_get() != APT_LOG_MASKING_NONE) { connection->verbose = FALSE; mrcp_parser_verbose_set(connection->parser,TRUE); mrcp_generator_verbose_set(connection->generator,TRUE); } return connection; }
static apt_bool_t mrcp_server_agent_connection_accept(mrcp_connection_agent_t *agent) { char *local_ip = NULL; char *remote_ip = NULL; mrcp_connection_t *connection = mrcp_connection_create(); if(apr_socket_accept(&connection->sock,agent->listen_sock,connection->pool) != APR_SUCCESS) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Accept Connection"); mrcp_connection_destroy(connection); return FALSE; } if(apr_socket_addr_get(&connection->r_sockaddr,APR_REMOTE,connection->sock) != APR_SUCCESS || apr_socket_addr_get(&connection->l_sockaddr,APR_LOCAL,connection->sock) != APR_SUCCESS) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Get Socket Address"); apr_socket_close(connection->sock); mrcp_connection_destroy(connection); return FALSE; } apr_sockaddr_ip_get(&local_ip,connection->l_sockaddr); apr_sockaddr_ip_get(&remote_ip,connection->r_sockaddr); apt_string_set(&connection->remote_ip,remote_ip); connection->id = apr_psprintf(connection->pool,"%s:%hu <-> %s:%hu", local_ip,connection->l_sockaddr->port, remote_ip,connection->r_sockaddr->port); if(apr_hash_count(agent->pending_channel_table) == 0) { apt_log(APT_LOG_MARK,APT_PRIO_NOTICE,"Reject Unexpected TCP/MRCPv2 Connection %s",connection->id); apr_socket_close(connection->sock); mrcp_connection_destroy(connection); return FALSE; } memset(&connection->sock_pfd,0,sizeof(apr_pollfd_t)); connection->sock_pfd.desc_type = APR_POLL_SOCKET; connection->sock_pfd.reqevents = APR_POLLIN; connection->sock_pfd.desc.s = connection->sock; connection->sock_pfd.client_data = connection; if(apt_poller_task_descriptor_add(agent->task, &connection->sock_pfd) != TRUE) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Add to Pollset %s",connection->id); apr_socket_close(connection->sock); mrcp_connection_destroy(connection); return FALSE; } apt_log(APT_LOG_MARK,APT_PRIO_NOTICE,"Accepted TCP/MRCPv2 Connection %s",connection->id); connection->agent = agent; APR_RING_INSERT_TAIL(&agent->connection_list,connection,mrcp_connection_t,link); connection->parser = mrcp_parser_create(agent->resource_factory,connection->pool); connection->generator = mrcp_generator_create(agent->resource_factory,connection->pool); connection->tx_buffer_size = agent->tx_buffer_size; connection->tx_buffer = apr_palloc(connection->pool,connection->tx_buffer_size+1); connection->rx_buffer_size = agent->rx_buffer_size; connection->rx_buffer = apr_palloc(connection->pool,connection->rx_buffer_size+1); apt_text_stream_init(&connection->rx_stream,connection->rx_buffer,connection->rx_buffer_size); if(apt_log_masking_get() != APT_LOG_MASKING_NONE) { connection->verbose = FALSE; mrcp_parser_verbose_set(connection->parser,TRUE); mrcp_generator_verbose_set(connection->generator,TRUE); } return TRUE; }
SWITCH_DECLARE(switch_status_t) switch_socket_addr_get(switch_sockaddr_t ** sa, switch_bool_t remote, switch_socket_t *sock) { return apr_socket_addr_get(sa, (apr_interface_e) remote, sock); }
static apr_status_t socket_pipe_create(apr_socket_t **rd, apr_socket_t **wr, apr_pool_t *pool) { static int id = 0; apr_socket_t *ls = NULL; apr_sockaddr_t *pa = NULL; apr_sockaddr_t *ca = NULL; apr_size_t nrd; int uid[2]; int iid[2]; /* Create the unique socket identifier * so that we know the connection originated * from us. */ uid[0] = getpid(); uid[1] = id++; if(apr_socket_create(&ls, AF_INET, SOCK_STREAM, APR_PROTO_TCP, pool) != APR_SUCCESS) { return apr_get_netos_error(); } apr_socket_opt_set(ls, APR_SO_REUSEADDR, 1); if(apr_sockaddr_info_get(&pa,"127.0.0.1",APR_INET,0,0,pool) != APR_SUCCESS) { apr_socket_close(ls); return apr_get_netos_error(); } if(apr_socket_bind(ls, pa) != APR_SUCCESS) { apr_socket_close(ls); return apr_get_netos_error(); } if(apr_socket_addr_get(&ca,APR_LOCAL,ls) != APR_SUCCESS) { apr_socket_close(ls); return apr_get_netos_error(); } if(apr_socket_listen(ls,1) != APR_SUCCESS) { apr_socket_close(ls); return apr_get_netos_error(); } if(apr_socket_create(wr, AF_INET, SOCK_STREAM, APR_PROTO_TCP, pool) != APR_SUCCESS) { apr_socket_close(ls); return apr_get_netos_error(); } apr_socket_opt_set(*wr, APR_SO_REUSEADDR, 1); if(apr_socket_connect(*wr, ca) != APR_SUCCESS) { apr_socket_close(ls); apr_socket_close(*wr); return apr_get_netos_error(); } nrd = sizeof(uid); if(apr_socket_send(*wr, (char *)uid, &nrd) != APR_SUCCESS) { apr_socket_close(ls); apr_socket_close(*wr); return apr_get_netos_error(); } apr_socket_opt_set(ls, APR_SO_NONBLOCK, 0); /* Listening socket is blocking by now. The accept should * return immediatelly because we connected already. */ if(apr_socket_accept(rd, ls, pool) != APR_SUCCESS) { apr_socket_close(ls); apr_socket_close(*wr); return apr_get_netos_error(); } /* Put read side of the pipe to the blocking mode */ apr_socket_opt_set(*rd, APR_SO_NONBLOCK, 0); for (;;) { /* Verify the connection by reading the sent identification */ nrd = sizeof(iid); if(apr_socket_recv(*rd, (char *)iid, &nrd) != APR_SUCCESS) { apr_socket_close(ls); apr_socket_close(*wr); apr_socket_close(*rd); return apr_get_netos_error(); } if(nrd == sizeof(iid)) { if(memcmp(uid, iid, sizeof(uid)) == 0) { /* Wow, we recived what we sent */ break; } } } /* We don't need the listening socket any more */ apr_socket_close(ls); return APR_SUCCESS; }
static void test_get_addr(abts_case *tc, void *data) { apr_status_t rv; apr_socket_t *ld, *sd, *cd; apr_sockaddr_t *sa, *ca; apr_pool_t *subp; char *a, *b; APR_ASSERT_SUCCESS(tc, "create subpool", apr_pool_create(&subp, p)); if ((ld = setup_socket(tc)) != APR_SUCCESS) return; APR_ASSERT_SUCCESS(tc, "get local address of bound socket", apr_socket_addr_get(&sa, APR_LOCAL, ld)); rv = apr_socket_create(&cd, sa->family, SOCK_STREAM, APR_PROTO_TCP, subp); APR_ASSERT_SUCCESS(tc, "create client socket", rv); APR_ASSERT_SUCCESS(tc, "enable non-block mode", apr_socket_opt_set(cd, APR_SO_NONBLOCK, 1)); /* It is valid for a connect() on a socket with NONBLOCK set to * succeed (if the connection can be established synchronously), * but if it does, this test cannot proceed. */ rv = apr_socket_connect(cd, sa); if (rv == APR_SUCCESS) { apr_socket_close(ld); apr_socket_close(cd); ABTS_NOT_IMPL(tc, "Cannot test if connect completes " "synchronously"); return; } if (!APR_STATUS_IS_EINPROGRESS(rv)) { apr_socket_close(ld); apr_socket_close(cd); APR_ASSERT_SUCCESS(tc, "connect to listener", rv); return; } APR_ASSERT_SUCCESS(tc, "accept connection", apr_socket_accept(&sd, ld, subp)); { /* wait for writability */ apr_pollfd_t pfd; int n; pfd.p = p; pfd.desc_type = APR_POLL_SOCKET; pfd.reqevents = APR_POLLOUT|APR_POLLHUP; pfd.desc.s = cd; pfd.client_data = NULL; APR_ASSERT_SUCCESS(tc, "poll for connect completion", apr_poll(&pfd, 1, &n, 5 * APR_USEC_PER_SEC)); } APR_ASSERT_SUCCESS(tc, "get local address of server socket", apr_socket_addr_get(&sa, APR_LOCAL, sd)); APR_ASSERT_SUCCESS(tc, "get remote address of client socket", apr_socket_addr_get(&ca, APR_REMOTE, cd)); /* Test that the pool of the returned sockaddr objects exactly * match the socket. */ ABTS_PTR_EQUAL(tc, subp, sa->pool); ABTS_PTR_EQUAL(tc, subp, ca->pool); /* Check equivalence. */ a = apr_psprintf(p, "%pI fam=%d", sa, sa->family); b = apr_psprintf(p, "%pI fam=%d", ca, ca->family); ABTS_STR_EQUAL(tc, a, b); /* Check pool of returned sockaddr, as above. */ APR_ASSERT_SUCCESS(tc, "get local address of client socket", apr_socket_addr_get(&sa, APR_LOCAL, cd)); APR_ASSERT_SUCCESS(tc, "get remote address of server socket", apr_socket_addr_get(&ca, APR_REMOTE, sd)); /* Check equivalence. */ a = apr_psprintf(p, "%pI fam=%d", sa, sa->family); b = apr_psprintf(p, "%pI fam=%d", ca, ca->family); ABTS_STR_EQUAL(tc, a, b); ABTS_PTR_EQUAL(tc, subp, sa->pool); ABTS_PTR_EQUAL(tc, subp, ca->pool); apr_socket_close(cd); apr_socket_close(sd); apr_socket_close(ld); apr_pool_destroy(subp); }
void LLPluginProcessParent::idle(void) { bool idle_again; do { // process queued messages mIncomingQueueMutex.lock(); while(!mIncomingQueue.empty()) { LLPluginMessage message = mIncomingQueue.front(); mIncomingQueue.pop(); mIncomingQueueMutex.unlock(); receiveMessage(message); mIncomingQueueMutex.lock(); } mIncomingQueueMutex.unlock(); // Give time to network processing if(mMessagePipe) { // Drain any queued outgoing messages mMessagePipe->pumpOutput(); // Only do input processing here if this instance isn't in a pollset. if(!mPolledInput) { mMessagePipe->pumpInput(); } } if(mState <= STATE_RUNNING) { if(APR_STATUS_IS_EOF(mSocketError)) { // Plugin socket was closed. This covers both normal plugin termination and plugin crashes. errorState(); } else if(mSocketError != APR_SUCCESS) { // The socket is in an error state -- the plugin is gone. LL_WARNS("Plugin") << "Socket hit an error state (" << mSocketError << ")" << LL_ENDL; errorState(); } } // If a state needs to go directly to another state (as a performance enhancement), it can set idle_again to true after calling setState(). // USE THIS CAREFULLY, since it can starve other code. Specifically make sure there's no way to get into a closed cycle and never return. // When in doubt, don't do it. idle_again = false; switch(mState) { case STATE_UNINITIALIZED: break; case STATE_INITIALIZED: { apr_status_t status = APR_SUCCESS; apr_sockaddr_t* addr = NULL; mListenSocket = LLSocket::create(gAPRPoolp, LLSocket::STREAM_TCP); mBoundPort = 0; // This code is based on parts of LLSocket::create() in lliosocket.cpp. status = apr_sockaddr_info_get( &addr, "127.0.0.1", APR_INET, mPortToBind, // port 0 = ephemeral ("find me a port") 0, gAPRPoolp); if(ll_apr_warn_status(status)) { killSockets(); errorState(); break; } // This allows us to reuse the address on quick down/up. This is unlikely to create problems. ll_apr_warn_status(apr_socket_opt_set(mListenSocket->getSocket(), APR_SO_REUSEADDR, 1)); status = apr_socket_bind(mListenSocket->getSocket(), addr); if(ll_apr_warn_status(status)) { killSockets(); errorState(); break; } // Get the actual port the socket was bound to { apr_sockaddr_t* bound_addr = NULL; if(ll_apr_warn_status(apr_socket_addr_get(&bound_addr, APR_LOCAL, mListenSocket->getSocket()))) { killSockets(); errorState(); break; } mBoundPort = bound_addr->port; if(mBoundPort == 0) { LL_WARNS("Plugin") << "Bound port number unknown, bailing out." << LL_ENDL; killSockets(); // <ND> FIRE-3877; Some drivers, eg bigfoot. Refuse to tell us which port is used when the socket is bound on port 0 (= choose a free port). // If not out of retry attempts, choose a random port between 5500 - 60000 and try again. if( mBindRetries > 10 ) //In theory we could have bad luck and randomly draft already used ports each try. In practice we already deal with a buggy driver anyway. So just fail instead hogging resources in a loop. errorState(); else { ++mBindRetries; mPortToBind = ll_rand(55000)+5000; // Ports < 4096 are reserved for root (at least on BSD like systems), do never touch them. setState( STATE_INITIALIZED ); idle_again = true; // Just try a new loop to bind the socket } // </ND> break; } } LL_DEBUGS("Plugin") << "Bound tcp socket to port: " << addr->port << LL_ENDL; // Make the listen socket non-blocking status = apr_socket_opt_set(mListenSocket->getSocket(), APR_SO_NONBLOCK, 1); if(ll_apr_warn_status(status)) { killSockets(); errorState(); break; } apr_socket_timeout_set(mListenSocket->getSocket(), 0); if(ll_apr_warn_status(status)) { killSockets(); errorState(); break; } // If it's a stream based socket, we need to tell the OS // to keep a queue of incoming connections for ACCEPT. status = apr_socket_listen( mListenSocket->getSocket(), 10); // FIXME: Magic number for queue size if(ll_apr_warn_status(status)) { killSockets(); errorState(); break; } // If we got here, we're listening. setState(STATE_LISTENING); } break; case STATE_LISTENING: { // Launch the plugin process. // Only argument to the launcher is the port number we're listening on mProcessParams.args.add(stringize(mBoundPort)); if (! (mProcess = LLProcess::create(mProcessParams))) { errorState(); } else { if(mDebug) { #if LL_DARWIN // If we're set to debug, start up a gdb instance in a new terminal window and have it attach to the plugin process and continue. // The command we're constructing would look like this on the command line: // osascript -e 'tell application "Terminal"' -e 'set win to do script "gdb -pid 12345"' -e 'do script "continue" in win' -e 'end tell' LLProcess::Params params; params.executable = "/usr/bin/osascript"; params.args.add("-e"); params.args.add("tell application \"Terminal\""); params.args.add("-e"); params.args.add(STRINGIZE("set win to do script \"gdb -pid " << mProcess->getProcessID() << "\"")); params.args.add("-e"); params.args.add("do script \"continue\" in win"); params.args.add("-e"); params.args.add("end tell"); mDebugger = LLProcess::create(params); #endif } // This will allow us to time out if the process never starts. mHeartbeat.start(); mHeartbeat.setTimerExpirySec(mPluginLaunchTimeout); setState(STATE_LAUNCHED); } } break; case STATE_LAUNCHED: // waiting for the plugin to connect if(pluginLockedUpOrQuit()) { errorState(); } else { // Check for the incoming connection. if(accept()) { // Stop listening on the server port mListenSocket.reset(); setState(STATE_CONNECTED); } } break; case STATE_CONNECTED: // waiting for hello message from the plugin if(pluginLockedUpOrQuit()) { errorState(); } break; case STATE_HELLO: LL_DEBUGS("Plugin") << "received hello message" << LL_ENDL; // Send the message to load the plugin { LLPluginMessage message(LLPLUGIN_MESSAGE_CLASS_INTERNAL, "load_plugin"); message.setValue("file", mPluginFile); message.setValue("dir", mPluginDir); sendMessage(message); } setState(STATE_LOADING); break; case STATE_LOADING: // The load_plugin_response message will kick us from here into STATE_RUNNING if(pluginLockedUpOrQuit()) { errorState(); } break; case STATE_RUNNING: if(pluginLockedUpOrQuit()) { errorState(); } break; case STATE_EXITING: if (! LLProcess::isRunning(mProcess)) { setState(STATE_CLEANUP); } else if(pluginLockedUp()) { LL_WARNS("Plugin") << "timeout in exiting state, bailing out" << LL_ENDL; errorState(); } break; case STATE_LAUNCH_FAILURE: if(mOwner != NULL) { mOwner->pluginLaunchFailed(); } setState(STATE_CLEANUP); break; case STATE_ERROR: if(mOwner != NULL) { mOwner->pluginDied(); } setState(STATE_CLEANUP); break; case STATE_CLEANUP: LLProcess::kill(mProcess); killSockets(); setState(STATE_DONE); break; case STATE_DONE: // just sit here. break; } } while (idle_again); }
static apt_bool_t mrcp_server_agent_connection_accept(mrcp_connection_agent_t *agent) { char *local_ip = NULL; char *remote_ip = NULL; apr_socket_t *sock; apr_pool_t *pool; apt_pollset_t *pollset = apt_poller_task_pollset_get(agent->task); mrcp_connection_t *connection; if(!agent->null_connection) { pool = apt_pool_create(); if(apr_socket_accept(&sock,agent->listen_sock,pool) != APR_SUCCESS) { return FALSE; } apt_log(APT_LOG_MARK,APT_PRIO_NOTICE,"Rejected TCP/MRCPv2 Connection"); apr_socket_close(sock); apr_pool_destroy(pool); return FALSE; } pool = agent->null_connection->pool; if(apr_socket_accept(&sock,agent->listen_sock,pool) != APR_SUCCESS) { return FALSE; } connection = mrcp_connection_create(); connection->sock = sock; if(apr_socket_addr_get(&connection->r_sockaddr,APR_REMOTE,sock) != APR_SUCCESS || apr_socket_addr_get(&connection->l_sockaddr,APR_LOCAL,sock) != APR_SUCCESS) { apr_socket_close(sock); mrcp_connection_destroy(connection); return FALSE; } apr_sockaddr_ip_get(&local_ip,connection->l_sockaddr); apr_sockaddr_ip_get(&remote_ip,connection->r_sockaddr); apt_string_set(&connection->remote_ip,remote_ip); connection->id = apr_psprintf(connection->pool,"%s:%hu <-> %s:%hu", local_ip,connection->l_sockaddr->port, remote_ip,connection->r_sockaddr->port); memset(&connection->sock_pfd,0,sizeof(apr_pollfd_t)); connection->sock_pfd.desc_type = APR_POLL_SOCKET; connection->sock_pfd.reqevents = APR_POLLIN; connection->sock_pfd.desc.s = connection->sock; connection->sock_pfd.client_data = connection; if(apt_pollset_add(pollset, &connection->sock_pfd) != TRUE) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Add to Pollset"); apr_socket_close(sock); mrcp_connection_destroy(connection); return FALSE; } apt_log(APT_LOG_MARK,APT_PRIO_NOTICE,"Accepted TCP/MRCPv2 Connection %s",connection->id); connection->agent = agent; connection->it = apt_list_push_back(agent->connection_list,connection,connection->pool); connection->parser = mrcp_parser_create(agent->resource_factory,connection->pool); connection->generator = mrcp_generator_create(agent->resource_factory,connection->pool); connection->tx_buffer_size = agent->tx_buffer_size; connection->tx_buffer = apr_palloc(connection->pool,connection->tx_buffer_size+1); connection->rx_buffer_size = agent->rx_buffer_size; connection->rx_buffer = apr_palloc(connection->pool,connection->rx_buffer_size+1); apt_text_stream_init(&connection->rx_stream,connection->rx_buffer,connection->rx_buffer_size); return TRUE; }
static void test_get_addr(abts_case *tc, void *data) { apr_status_t rv; apr_socket_t *ld, *sd, *cd; apr_sockaddr_t *sa, *ca; char a[128], b[128]; ld = setup_socket(tc); APR_ASSERT_SUCCESS(tc, "get local address of bound socket", apr_socket_addr_get(&sa, APR_LOCAL, ld)); rv = apr_socket_create(&cd, sa->family, SOCK_STREAM, APR_PROTO_TCP, p); APR_ASSERT_SUCCESS(tc, "create client socket", rv); APR_ASSERT_SUCCESS(tc, "enable non-block mode", apr_socket_opt_set(cd, APR_SO_NONBLOCK, 1)); /* It is valid for a connect() on a socket with NONBLOCK set to * succeed (if the connection can be established synchronously), * but if it does, this test cannot proceed. */ rv = apr_socket_connect(cd, sa); if (rv == APR_SUCCESS) { apr_socket_close(ld); apr_socket_close(cd); ABTS_NOT_IMPL(tc, "Cannot test if connect completes " "synchronously"); return; } if (!APR_STATUS_IS_EINPROGRESS(rv)) { apr_socket_close(ld); apr_socket_close(cd); APR_ASSERT_SUCCESS(tc, "connect to listener", rv); return; } APR_ASSERT_SUCCESS(tc, "accept connection", apr_socket_accept(&sd, ld, p)); { /* wait for writability */ apr_pollfd_t pfd; int n; pfd.p = p; pfd.desc_type = APR_POLL_SOCKET; pfd.reqevents = APR_POLLOUT|APR_POLLHUP; pfd.desc.s = cd; pfd.client_data = NULL; APR_ASSERT_SUCCESS(tc, "poll for connect completion", apr_poll(&pfd, 1, &n, 5 * APR_USEC_PER_SEC)); } APR_ASSERT_SUCCESS(tc, "get local address of server socket", apr_socket_addr_get(&sa, APR_LOCAL, sd)); APR_ASSERT_SUCCESS(tc, "get remote address of client socket", apr_socket_addr_get(&ca, APR_REMOTE, cd)); apr_snprintf(a, sizeof(a), "%pI", sa); apr_snprintf(b, sizeof(b), "%pI", ca); ABTS_STR_EQUAL(tc, a, b); apr_socket_close(cd); apr_socket_close(sd); apr_socket_close(ld); }
void LLPluginProcessParent::idle(void) { bool idle_again; do { // process queued messages mIncomingQueueMutex.lock(); while(!mIncomingQueue.empty()) { LLPluginMessage message = mIncomingQueue.front(); mIncomingQueue.pop(); mIncomingQueueMutex.unlock(); receiveMessage(message); mIncomingQueueMutex.lock(); } mIncomingQueueMutex.unlock(); // Give time to network processing if(mMessagePipe) { // Drain any queued outgoing messages mMessagePipe->pumpOutput(); // Only do input processing here if this instance isn't in a pollset. if(!mPolledInput) { mMessagePipe->pumpInput(); } } if(mState <= STATE_RUNNING) { if(APR_STATUS_IS_EOF(mSocketError)) { // Plugin socket was closed. This covers both normal plugin termination and plugin crashes. errorState(); } else if(mSocketError != APR_SUCCESS) { // The socket is in an error state -- the plugin is gone. LL_WARNS("Plugin") << "Socket hit an error state (" << mSocketError << ")" << LL_ENDL; errorState(); } } // If a state needs to go directly to another state (as a performance enhancement), it can set idle_again to true after calling setState(). // USE THIS CAREFULLY, since it can starve other code. Specifically make sure there's no way to get into a closed cycle and never return. // When in doubt, don't do it. idle_again = false; switch(mState) { case STATE_UNINITIALIZED: break; case STATE_INITIALIZED: { apr_status_t status = APR_SUCCESS; apr_sockaddr_t* addr = NULL; mListenSocket = LLSocket::create(LLSocket::STREAM_TCP); mBoundPort = 0; // This code is based on parts of LLSocket::create() in lliosocket.cpp. status = apr_sockaddr_info_get( &addr, "127.0.0.1", APR_INET, 0, // port 0 = ephemeral ("find me a port") 0, LLAPRRootPool::get()()); if(ll_apr_warn_status(status)) { killSockets(); errorState(); break; } // This allows us to reuse the address on quick down/up. This is unlikely to create problems. ll_apr_warn_status(apr_socket_opt_set(mListenSocket->getSocket(), APR_SO_REUSEADDR, 1)); status = apr_socket_bind(mListenSocket->getSocket(), addr); if(ll_apr_warn_status(status)) { killSockets(); errorState(); break; } // Get the actual port the socket was bound to { apr_sockaddr_t* bound_addr = NULL; if(ll_apr_warn_status(apr_socket_addr_get(&bound_addr, APR_LOCAL, mListenSocket->getSocket()))) { killSockets(); errorState(); break; } mBoundPort = bound_addr->port; if(mBoundPort == 0) { LL_WARNS("Plugin") << "Bound port number unknown, bailing out." << LL_ENDL; killSockets(); errorState(); break; } } LL_DEBUGS("Plugin") << "Bound tcp socket to port: " << addr->port << LL_ENDL; // Make the listen socket non-blocking status = apr_socket_opt_set(mListenSocket->getSocket(), APR_SO_NONBLOCK, 1); if(ll_apr_warn_status(status)) { killSockets(); errorState(); break; } apr_socket_timeout_set(mListenSocket->getSocket(), 0); if(ll_apr_warn_status(status)) { killSockets(); errorState(); break; } // If it's a stream based socket, we need to tell the OS // to keep a queue of incoming connections for ACCEPT. status = apr_socket_listen( mListenSocket->getSocket(), 10); // FIXME: Magic number for queue size if(ll_apr_warn_status(status)) { killSockets(); errorState(); break; } // If we got here, we're listening. setState(STATE_LISTENING); } break; case STATE_LISTENING: { // Launch the plugin process. // Only argument to the launcher is the port number we're listening on std::stringstream stream; stream << mBoundPort; mProcess.addArgument(stream.str()); if(mProcess.launch() != 0) { errorState(); } else { if(mDebug) { // If we're set to debug, start up a gdb instance in a new terminal window and have it attach to the plugin process and continue. std::stringstream cmd; #if LL_DARWIN // The command we're constructing would look like this on the command line: // osascript -e 'tell application "Terminal"' -e 'set win to do script "gdb -pid 12345"' -e 'do script "continue" in win' -e 'end tell' mDebugger.setExecutable("/usr/bin/osascript"); mDebugger.addArgument("-e"); mDebugger.addArgument("tell application \"Terminal\""); mDebugger.addArgument("-e"); cmd << "set win to do script \"gdb -pid " << mProcess.getProcessID() << "\""; mDebugger.addArgument(cmd.str()); mDebugger.addArgument("-e"); mDebugger.addArgument("do script \"continue\" in win"); mDebugger.addArgument("-e"); mDebugger.addArgument("end tell"); mDebugger.launch(); #elif LL_LINUX // The command we're constructing would look like this on the command line: // /usr/bin/xterm -geometry 160x24-0+0 -e '/usr/bin/gdb -n /proc/12345/exe 12345' // This can be changed by setting the following environment variables, for example: // export LL_DEBUG_TERMINAL_COMMAND="/usr/bin/gnome-terminal --geometry=165x24-0+0 -e %s" // export LL_DEBUG_GDB_PATH=/usr/bin/gdb char const* env; std::string const terminal_command = (env = getenv("LL_DEBUG_TERMINAL_COMMAND")) ? env : "/usr/bin/xterm -geometry 160x24+0+0 -e %s"; char const* const gdb_path = (env = getenv("LL_DEBUG_GDB_PATH")) ? env : "/usr/bin/gdb"; cmd << gdb_path << " -n /proc/" << mProcess.getProcessID() << "/exe " << mProcess.getProcessID(); typedef boost::tokenizer< boost::escaped_list_separator< char>, std::basic_string< char>::const_iterator, std::basic_string<char> > tokenizerT; tokenizerT tok(terminal_command.begin(), terminal_command.end(), boost::escaped_list_separator< char >("\\", " ", "'\"")); std::vector< std::basic_string<char> > tokens; for (tokenizerT::iterator cur_token(tok.begin()), end_token(tok.end()); cur_token != end_token; ++cur_token) { if (!cur_token->empty()) tokens.push_back(*cur_token); } std::vector<std::string>::iterator token = tokens.begin(); mDebugger.setExecutable(*token); while (++token != tokens.end()) { if (*token == "%s") { mDebugger.addArgument(cmd.str()); } else { mDebugger.addArgument(*token); } } mDebugger.launch(); #endif } // This will allow us to time out if the process never starts. mHeartbeat.start(); mHeartbeat.setTimerExpirySec(mPluginLaunchTimeout); setState(STATE_LAUNCHED); } } break; case STATE_LAUNCHED: // waiting for the plugin to connect if(pluginLockedUpOrQuit()) { errorState(); } else { // Check for the incoming connection. if(accept()) { // Stop listening on the server port mListenSocket.reset(); setState(STATE_CONNECTED); } } break; case STATE_CONNECTED: // waiting for hello message from the plugin if(pluginLockedUpOrQuit()) { errorState(); } break; case STATE_HELLO: LL_DEBUGS("Plugin") << "received hello message" << LL_ENDL; // Send the message to load the plugin { LLPluginMessage message(LLPLUGIN_MESSAGE_CLASS_INTERNAL, "load_plugin"); message.setValue("file", mPluginFile); sendMessage(message); } setState(STATE_LOADING); break; case STATE_LOADING: // The load_plugin_response message will kick us from here into STATE_RUNNING if(pluginLockedUpOrQuit()) { errorState(); } break; case STATE_RUNNING: if(pluginLockedUpOrQuit()) { errorState(); } break; case STATE_EXITING: if(!mProcess.isRunning()) { setState(STATE_CLEANUP); } else if(pluginLockedUp()) { LL_WARNS("Plugin") << "timeout in exiting state, bailing out" << LL_ENDL; errorState(); } break; case STATE_LAUNCH_FAILURE: if(mOwner != NULL) { mOwner->pluginLaunchFailed(); } setState(STATE_CLEANUP); break; case STATE_ERROR: if(mOwner != NULL) { mOwner->pluginDied(); } setState(STATE_CLEANUP); break; case STATE_CLEANUP: mProcess.kill(); killSockets(); setState(STATE_DONE); break; case STATE_DONE: // just sit here. break; } } while (idle_again); }
static void im_tcp_accept(nx_module_t *module) { nx_im_tcp_conf_t *imconf; apr_socket_t *sock; apr_sockaddr_t *sa; char *ipstr; nx_module_input_t *input = NULL; apr_pool_t *pool = NULL; apr_status_t rv; nx_exception_t e; log_debug("im_tcp_accept"); imconf = (nx_im_tcp_conf_t *) module->config; pool = nx_pool_create_child(module->pool); try { if ( (rv = apr_socket_accept(&sock, imconf->listensock, pool)) != APR_SUCCESS ) { if ( APR_STATUS_IS_EAGAIN(rv) ) { nx_module_add_poll_event(module); apr_pool_destroy(pool); } else { throw(rv, "couldn't accept connection on %s:%u (statuscode: %d)", imconf->host, imconf->port, rv); } } if ( rv == APR_SUCCESS ) { CHECKERR_MSG(apr_socket_opt_set(sock, APR_SO_NONBLOCK, 1), "couldn't set SO_NONBLOCK on accepted socket"); CHECKERR_MSG(apr_socket_timeout_set(sock, 0), "couldn't set socket timeout on accepted socket"); if ( imconf->nodelay == TRUE ) { CHECKERR_MSG(apr_socket_opt_set(sock, APR_TCP_NODELAY, 1), "couldn't set TCP_NODELAY on accepted socket"); } CHECKERR_MSG(apr_socket_addr_get(&sa, APR_REMOTE, sock), "couldn't get info on accepted socket"); CHECKERR_MSG(apr_sockaddr_ip_get(&ipstr, sa), "couldn't get IP of accepted socket"); log_info("connection accepted from %s:%u", ipstr, sa->port); nx_module_pollset_add_socket(module, imconf->listensock, APR_POLLIN | APR_POLLHUP); input = nx_module_input_new(module, pool); input->desc_type = APR_POLL_SOCKET; input->desc.s = sock; input->inputfunc = imconf->inputfunc; CHECKERR_MSG(apr_socket_data_set(sock, input, "input", NULL), "couldn't set data on socket"); NX_DLIST_INSERT_TAIL(imconf->connections, input, link); nx_module_input_data_set(input, "recv_from_str", ipstr); nx_module_pollset_add_socket(module, input->desc.s, APR_POLLIN | APR_POLLHUP); } } catch(e) { apr_pool_destroy(pool); rethrow(e); } }
static void im_tcp_read(nx_module_t *module, nx_event_t *event) { nx_im_tcp_conf_t *imconf; nx_logdata_t *logdata; boolean volatile got_eof = FALSE; apr_socket_t *sock; nx_module_input_t *input; apr_sockaddr_t *sa; char *ipstr; apr_status_t rv; nx_exception_t e; ASSERT(event != NULL); ASSERT(module != NULL); log_debug("im_tcp_read"); if ( nx_module_get_status(module) != NX_MODULE_STATUS_RUNNING ) { log_debug("module %s not running, not reading any more data", module->name); return; } imconf = (nx_im_tcp_conf_t *) module->config; sock = (apr_socket_t *) event->data; ASSERT(imconf->listensock != NULL); if ( sock == imconf->listensock ) { im_tcp_accept(module); return; } //else we have data from a client connection CHECKERR_MSG(apr_socket_data_get((void **) &input, "input", sock), "couldn't get input data from socket"); ASSERT(input != NULL); // if this is null there is a race/double free in event handling if ( (rv = nx_module_input_fill_buffer_from_socket(input)) != APR_SUCCESS ) { if ( APR_STATUS_IS_ECONNREFUSED(rv) || APR_STATUS_IS_ECONNABORTED(rv) || APR_STATUS_IS_ECONNRESET(rv) || APR_STATUS_IS_ETIMEDOUT(rv) || APR_STATUS_IS_TIMEUP(rv) || APR_STATUS_IS_EHOSTUNREACH(rv) || APR_STATUS_IS_ENETUNREACH(rv) || APR_STATUS_IS_EOF(rv) ) { got_eof = TRUE; } else if ( APR_STATUS_IS_EAGAIN(rv) ) { nx_module_add_poll_event(module); } else { im_tcp_free_input(input); throw(rv, "Module %s couldn't read from socket", input->module->name); } } try { while ( (logdata = input->inputfunc->func(input, input->inputfunc->data)) != NULL ) { //log_debug("read %d bytes: [%s]", (int) logdata->datalen, logdata->data); ipstr = nx_module_input_data_get(input, "recv_from_str"); nx_logdata_set_string(logdata, "MessageSourceAddress", ipstr); nx_module_add_logdata_input(module, input, logdata); } } catch(e) { im_tcp_free_input(input); rethrow_msg(e, "Module %s couldn't read the input", input->module->name); } if ( got_eof == TRUE ) { CHECKERR_MSG(apr_socket_addr_get(&sa, APR_REMOTE, input->desc.s), "couldn't get info on accepted socket"); CHECKERR_MSG(apr_sockaddr_ip_get(&ipstr, sa), "couldn't get IP of accepted socket"); log_aprwarn(rv, "TCP connection closed from %s:%u", ipstr, sa->port); im_tcp_free_input(input); } }
int main(int argc, char *argv[]) { apr_pool_t *context; apr_socket_t *sock; apr_size_t length; apr_status_t stat; char datasend[STRLEN] = "Send data test"; char datarecv[STRLEN]; char msgbuf[80]; char *local_ipaddr, *remote_ipaddr; char *dest = "127.0.0.1"; apr_port_t local_port, remote_port; apr_interval_time_t timeout = apr_time_from_sec(2); apr_sockaddr_t *local_sa, *remote_sa; setbuf(stdout, NULL); if (argc > 1) { dest = argv[1]; } if (argc > 2) { timeout = atoi(argv[2]); } fprintf(stdout, "Initializing........."); if (apr_initialize() != APR_SUCCESS) { fprintf(stderr, "Something went wrong\n"); exit(-1); } fprintf(stdout, "OK\n"); atexit(apr_terminate); fprintf(stdout, "Creating context......."); if (apr_pool_create(&context, NULL) != APR_SUCCESS) { fprintf(stderr, "Something went wrong\n"); exit(-1); } fprintf(stdout, "OK\n"); fprintf(stdout,"\tClient: Making socket address..............."); if ((stat = apr_sockaddr_info_get(&remote_sa, dest, APR_UNSPEC, 8021, 0, context)) != APR_SUCCESS) { fprintf(stdout, "Failed!\n"); fprintf(stdout, "Address resolution failed for %s: %s\n", dest, apr_strerror(stat, msgbuf, sizeof(msgbuf))); exit(-1); } fprintf(stdout,"OK\n"); fprintf(stdout, "\tClient: Creating new socket......."); if (apr_socket_create(&sock, remote_sa->family, SOCK_STREAM, context) != APR_SUCCESS) { fprintf(stderr, "Couldn't create socket\n"); exit(-1); } fprintf(stdout, "OK\n"); fprintf(stdout, "\tClient: Setting socket timeout......."); stat = apr_socket_timeout_set(sock, timeout); if (stat) { fprintf(stderr, "Problem setting timeout: %d\n", stat); exit(-1); } fprintf(stdout, "OK\n"); fprintf(stdout, "\tClient: Connecting to socket......."); stat = apr_socket_connect(sock, remote_sa); if (stat != APR_SUCCESS) { apr_socket_close(sock); fprintf(stderr, "Could not connect: %s (%d)\n", apr_strerror(stat, msgbuf, sizeof(msgbuf)), stat); fflush(stderr); exit(-1); } fprintf(stdout, "OK\n"); apr_socket_addr_get(&remote_sa, APR_REMOTE, sock); apr_sockaddr_ip_get(&remote_ipaddr, remote_sa); apr_sockaddr_port_get(&remote_port, remote_sa); apr_socket_addr_get(&local_sa, APR_LOCAL, sock); apr_sockaddr_ip_get(&local_ipaddr, local_sa); apr_sockaddr_port_get(&local_port, local_sa); fprintf(stdout, "\tClient socket: %s:%u -> %s:%u\n", local_ipaddr, local_port, remote_ipaddr, remote_port); fprintf(stdout, "\tClient: Trying to send data over socket......."); length = STRLEN; if ((stat = apr_socket_send(sock, datasend, &length) != APR_SUCCESS)) { apr_socket_close(sock); fprintf(stderr, "Problem sending data: %s (%d)\n", apr_strerror(stat, msgbuf, sizeof(msgbuf)), stat); exit(-1); } fprintf(stdout, "OK\n"); length = STRLEN; fprintf(stdout, "\tClient: Trying to receive data over socket......."); if ((stat = apr_socket_recv(sock, datarecv, &length)) != APR_SUCCESS) { apr_socket_close(sock); fprintf(stderr, "Problem receiving data: %s (%d)\n", apr_strerror(stat, msgbuf, sizeof(msgbuf)), stat); exit(-1); } if (strcmp(datarecv, "Recv data test")) { apr_socket_close(sock); fprintf(stderr, "I did not receive the correct data %s\n", datarecv); exit(-1); } fprintf(stdout, "OK\n"); fprintf(stdout, "\tClient: Shutting down socket......."); if (apr_socket_shutdown(sock, APR_SHUTDOWN_WRITE) != APR_SUCCESS) { apr_socket_close(sock); fprintf(stderr, "Could not shutdown socket\n"); exit(-1); } fprintf(stdout, "OK\n"); fprintf(stdout, "\tClient: Closing down socket......."); if (apr_socket_close(sock) != APR_SUCCESS) { fprintf(stderr, "Could not shutdown socket\n"); exit(-1); } fprintf(stdout, "OK\n"); return 1; }
/* Accept RTSP connection */ static apt_bool_t rtsp_server_connection_accept(rtsp_server_t *server) { rtsp_server_connection_t *rtsp_connection; char *local_ip = NULL; char *remote_ip = NULL; apr_sockaddr_t *l_sockaddr = NULL; apr_sockaddr_t *r_sockaddr = NULL; apt_pollset_t *pollset = apt_poller_task_pollset_get(server->task); apr_pool_t *pool = apt_pool_create(); if(!pool) { return FALSE; } rtsp_connection = apr_palloc(pool,sizeof(rtsp_server_connection_t)); rtsp_connection->pool = pool; rtsp_connection->sock = NULL; rtsp_connection->client_ip = NULL; if(apr_socket_accept(&rtsp_connection->sock,server->listen_sock,rtsp_connection->pool) != APR_SUCCESS) { apr_pool_destroy(pool); return FALSE; } if(apr_socket_addr_get(&l_sockaddr,APR_LOCAL,rtsp_connection->sock) != APR_SUCCESS || apr_socket_addr_get(&r_sockaddr,APR_REMOTE,rtsp_connection->sock) != APR_SUCCESS) { apr_pool_destroy(pool); return FALSE; } apr_sockaddr_ip_get(&local_ip,l_sockaddr); apr_sockaddr_ip_get(&remote_ip,r_sockaddr); rtsp_connection->client_ip = remote_ip; rtsp_connection->id = apr_psprintf(pool,"%s:%hu <-> %s:%hu", local_ip,l_sockaddr->port, remote_ip,r_sockaddr->port); memset(&rtsp_connection->sock_pfd,0,sizeof(apr_pollfd_t)); rtsp_connection->sock_pfd.desc_type = APR_POLL_SOCKET; rtsp_connection->sock_pfd.reqevents = APR_POLLIN; rtsp_connection->sock_pfd.desc.s = rtsp_connection->sock; rtsp_connection->sock_pfd.client_data = rtsp_connection; if(apt_pollset_add(pollset,&rtsp_connection->sock_pfd) != TRUE) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Add to Pollset %s",rtsp_connection->id); apr_socket_close(rtsp_connection->sock); apr_pool_destroy(pool); return FALSE; } apt_log(APT_LOG_MARK,APT_PRIO_NOTICE,"Accepted TCP Connection %s",rtsp_connection->id); rtsp_connection->session_table = apr_hash_make(rtsp_connection->pool); apt_text_stream_init(&rtsp_connection->rx_stream,rtsp_connection->rx_buffer,sizeof(rtsp_connection->rx_buffer)-1); apt_text_stream_init(&rtsp_connection->tx_stream,rtsp_connection->tx_buffer,sizeof(rtsp_connection->tx_buffer)-1); rtsp_connection->parser = rtsp_parser_create(rtsp_connection->pool); rtsp_connection->generator = rtsp_generator_create(rtsp_connection->pool); if(!server->connection_list) { server->connection_list = apt_list_create(server->sub_pool); } rtsp_connection->server = server; rtsp_connection->it = apt_list_push_back(server->connection_list,rtsp_connection,rtsp_connection->pool); return TRUE; }