static void find_default_family(apr_pool_t *p) { #if APR_HAVE_IPV6 /* We know the platform supports IPv6, but this particular * system may not have IPv6 enabled. See if we can get an * AF_INET6 socket. */ if (default_family == APR_UNSPEC) { apr_socket_t *tmp_sock; # if APR_MAJOR_VERSION == 1 if (apr_socket_create(&tmp_sock, APR_INET6, SOCK_STREAM, APR_PROTO_TCP, p) == APR_SUCCESS) { apr_socket_close(tmp_sock); default_family = APR_INET6; } # else if (apr_socket_create(&tmp_sock, APR_INET6, SOCK_STREAM, p) == APR_SUCCESS) { apr_socket_close(tmp_sock); default_family = APR_INET6; } # endif else { default_family = APR_INET; } } #endif }
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; 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(agent->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); return TRUE; }
static void test_send(abts_case *tc, void *data) { apr_status_t rv; apr_socket_t *sock; apr_socket_t *sock2; apr_proc_t proc; int protocol; apr_size_t length; sock = setup_socket(tc); if (!sock) return; launch_child(tc, &proc, "read", p); rv = apr_socket_accept(&sock2, sock, p); APR_ASSERT_SUCCESS(tc, "Problem with receiving connection", rv); apr_socket_protocol_get(sock2, &protocol); ABTS_INT_EQUAL(tc, APR_PROTO_TCP, protocol); length = strlen(DATASTR); apr_socket_send(sock2, DATASTR, &length); /* Make sure that the client received the data we sent */ ABTS_SIZE_EQUAL(tc, strlen(DATASTR), wait_child(tc, &proc)); rv = apr_socket_close(sock2); APR_ASSERT_SUCCESS(tc, "Problem closing connected socket", rv); rv = apr_socket_close(sock); APR_ASSERT_SUCCESS(tc, "Problem closing socket", rv); }
static void test_atreadeof(abts_case *tc, void *data) { apr_status_t rv; apr_socket_t *sock; apr_socket_t *sock2; apr_proc_t proc; apr_size_t length = STRLEN; char datastr[STRLEN]; int atreadeof = -1; sock = setup_socket(tc); if (!sock) return; launch_child(tc, &proc, "write", p); rv = apr_socket_accept(&sock2, sock, p); APR_ASSERT_SUCCESS(tc, "Problem with receiving connection", rv); /* Check that the remote socket is still open */ rv = apr_socket_atreadeof(sock2, &atreadeof); APR_ASSERT_SUCCESS(tc, "Determine whether at EOF, #1", rv); ABTS_INT_EQUAL(tc, 0, atreadeof); memset(datastr, 0, STRLEN); apr_socket_recv(sock2, datastr, &length); /* Make sure that the server received the data we sent */ ABTS_STR_EQUAL(tc, DATASTR, datastr); ABTS_SIZE_EQUAL(tc, strlen(datastr), wait_child(tc, &proc)); /* The child is dead, so should be the remote socket */ rv = apr_socket_atreadeof(sock2, &atreadeof); APR_ASSERT_SUCCESS(tc, "Determine whether at EOF, #2", rv); ABTS_INT_EQUAL(tc, 1, atreadeof); rv = apr_socket_close(sock2); APR_ASSERT_SUCCESS(tc, "Problem closing connected socket", rv); launch_child(tc, &proc, "close", p); rv = apr_socket_accept(&sock2, sock, p); APR_ASSERT_SUCCESS(tc, "Problem with receiving connection", rv); /* The child closed the socket as soon as it could... */ rv = apr_socket_atreadeof(sock2, &atreadeof); APR_ASSERT_SUCCESS(tc, "Determine whether at EOF, #3", rv); if (!atreadeof) { /* ... but perhaps not yet; wait a moment */ apr_sleep(apr_time_from_msec(5)); rv = apr_socket_atreadeof(sock2, &atreadeof); APR_ASSERT_SUCCESS(tc, "Determine whether at EOF, #4", rv); } ABTS_INT_EQUAL(tc, 1, atreadeof); wait_child(tc, &proc); rv = apr_socket_close(sock2); APR_ASSERT_SUCCESS(tc, "Problem closing connected socket", rv); rv = apr_socket_close(sock); APR_ASSERT_SUCCESS(tc, "Problem closing socket", rv); }
static void test_recv(abts_case *tc, void *data) { apr_status_t rv; apr_socket_t *sock; apr_socket_t *sock2; apr_proc_t proc; int protocol; apr_size_t length = STRLEN; char datastr[STRLEN]; sock = setup_socket(tc); if (!sock) return; launch_child(tc, &proc, "write", p); rv = apr_socket_accept(&sock2, sock, p); APR_ASSERT_SUCCESS(tc, "Problem with receiving connection", rv); apr_socket_protocol_get(sock2, &protocol); ABTS_INT_EQUAL(tc, APR_PROTO_TCP, protocol); memset(datastr, 0, STRLEN); apr_socket_recv(sock2, datastr, &length); /* Make sure that the server received the data we sent */ ABTS_STR_EQUAL(tc, DATASTR, datastr); ABTS_SIZE_EQUAL(tc, strlen(datastr), wait_child(tc, &proc)); rv = apr_socket_close(sock2); APR_ASSERT_SUCCESS(tc, "Problem closing connected socket", rv); rv = apr_socket_close(sock); APR_ASSERT_SUCCESS(tc, "Problem closing socket", rv); }
static void test_timeout(abts_case *tc, void *data) { apr_status_t rv; apr_socket_t *sock; apr_socket_t *sock2; apr_proc_t proc; int protocol; int exit; sock = setup_socket(tc); if (!sock) return; launch_child(tc, &proc, "read", p); rv = apr_socket_accept(&sock2, sock, p); APR_ASSERT_SUCCESS(tc, "Problem with receiving connection", rv); apr_socket_protocol_get(sock2, &protocol); ABTS_INT_EQUAL(tc, APR_PROTO_TCP, protocol); exit = wait_child(tc, &proc); ABTS_INT_EQUAL(tc, SOCKET_TIMEOUT, exit); /* We didn't write any data, so make sure the child program returns * an error. */ rv = apr_socket_close(sock2); APR_ASSERT_SUCCESS(tc, "Problem closing connected socket", rv); rv = apr_socket_close(sock); APR_ASSERT_SUCCESS(tc, "Problem closing socket", rv); }
static apt_bool_t mrcp_server_agent_listen_socket_create(mrcp_connection_agent_t *agent) { apr_status_t status; if(!agent->sockaddr) { return FALSE; } /* create listening socket */ status = apr_socket_create(&agent->listen_sock, agent->sockaddr->family, SOCK_STREAM, APR_PROTO_TCP, agent->pool); if(status != APR_SUCCESS) { return FALSE; } apr_socket_opt_set(agent->listen_sock, APR_SO_NONBLOCK, 0); apr_socket_timeout_set(agent->listen_sock, -1); apr_socket_opt_set(agent->listen_sock, APR_SO_REUSEADDR, 1); status = apr_socket_bind(agent->listen_sock, agent->sockaddr); if(status != APR_SUCCESS) { apr_socket_close(agent->listen_sock); agent->listen_sock = NULL; return FALSE; } status = apr_socket_listen(agent->listen_sock, SOMAXCONN); if(status != APR_SUCCESS) { apr_socket_close(agent->listen_sock); agent->listen_sock = NULL; return FALSE; } return TRUE; }
int main(int argc, char *argv[]) { apr_pool_t *p; apr_socket_t *sock; apr_status_t rv; apr_sockaddr_t *remote_sa; apr_initialize(); atexit(apr_terminate); apr_pool_create(&p, NULL); if (argc < 2) { exit(-1); } rv = apr_sockaddr_info_get(&remote_sa, "127.0.0.1", APR_UNSPEC, 8021, 0, p); if (rv != APR_SUCCESS) { exit(-1); } if (apr_socket_create(&sock, remote_sa->family, SOCK_STREAM, 0, p) != APR_SUCCESS) { exit(-1); } rv = apr_socket_timeout_set(sock, apr_time_from_sec(3)); if (rv) { exit(-1); } apr_socket_connect(sock, remote_sa); if (!strcmp("read", argv[1])) { char datarecv[STRLEN]; apr_size_t length = STRLEN; apr_status_t rv; memset(datarecv, 0, STRLEN); rv = apr_socket_recv(sock, datarecv, &length); apr_socket_close(sock); if (APR_STATUS_IS_TIMEUP(rv)) { exit(SOCKET_TIMEOUT); } if (strcmp(datarecv, DATASTR)) { exit(-1); } exit((int)length); } else if (!strcmp("write", argv[1])) { apr_size_t length = strlen(DATASTR); apr_socket_send(sock, DATASTR, &length); apr_socket_close(sock); exit((int)length); } exit(-1); }
apr_status_t handle_new_client(apr_socket_t *socket, apr_pool_t *pool, dynalogin_session_t *h) { char buf[ERRBUFLEN + 1]; apr_status_t res; apr_threadattr_t *t_attr; apr_thread_t *t; apr_pool_t *subpool; socket_thread_data_t *thread_data; res = apr_pool_create(&subpool, pool); if(res != APR_SUCCESS) { syslog(LOG_ERR, "failed to create pool: %s", apr_strerror(res, buf, ERRBUFLEN)); apr_socket_close(socket); return res; } res = apr_threadattr_create(&t_attr, subpool); if(res != APR_SUCCESS) { syslog(LOG_ERR, "failed to create threadattr: %s", apr_strerror(res, buf, ERRBUFLEN)); apr_pool_destroy(subpool); apr_socket_close(socket); return res; } thread_data = apr_pcalloc(subpool, sizeof(struct socket_thread_data_t)); if(thread_data == NULL) { syslog(LOG_ERR, "handle_new_client: apr_pcalloc failed"); apr_pool_destroy(subpool); apr_socket_close(socket); return res; } thread_data->pool = subpool; thread_data->socket = socket; thread_data->tls_session = NULL; // to be populated later if using TLS thread_data->dynalogin_session = h; res = apr_thread_create(&t, t_attr, (apr_thread_start_t)socket_thread_main, thread_data, subpool); if(res != APR_SUCCESS) { syslog(LOG_ERR, "failed to spawn a thread: %s", apr_strerror(res, buf, ERRBUFLEN)); apr_pool_destroy(subpool); apr_socket_close(socket); } return res; }
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; 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(agent->pollset, &connection->sock_pfd) != TRUE) { 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); return connection; }
/* Close RTP/RTCP sockets */ static void mpf_rtp_socket_pair_close(mpf_rtp_stream_t *stream) { if(stream->rtp_socket) { apr_socket_close(stream->rtp_socket); stream->rtp_socket = NULL; } if(stream->rtcp_socket) { apr_socket_close(stream->rtcp_socket); stream->rtcp_socket = NULL; } }
apr_status_t test_server_destroy(test_baton_t *tb, apr_pool_t *pool) { serf_connection_close(tb->connection); apr_socket_close(tb->serv_sock); if (tb->client_sock) { apr_socket_close(tb->client_sock); } return APR_SUCCESS; }
/** Create connection */ static apt_bool_t rtsp_client_connect(rtsp_client_t *client, rtsp_client_connection_t *connection, const char *ip, apr_port_t port) { char *local_ip = NULL; char *remote_ip = NULL; apr_sockaddr_t *l_sockaddr = NULL; apr_sockaddr_t *r_sockaddr = NULL; if(apr_sockaddr_info_get(&r_sockaddr,ip,APR_INET,port,0,connection->pool) != APR_SUCCESS) { return FALSE; } if(apr_socket_create(&connection->sock,r_sockaddr->family,SOCK_STREAM,APR_PROTO_TCP,connection->pool) != APR_SUCCESS) { return FALSE; } 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,r_sockaddr) != APR_SUCCESS) { apr_socket_close(connection->sock); connection->sock = NULL; return FALSE; } if(apr_socket_addr_get(&l_sockaddr,APR_LOCAL,connection->sock) != APR_SUCCESS) { apr_socket_close(connection->sock); connection->sock = NULL; return FALSE; } apr_sockaddr_ip_get(&local_ip,l_sockaddr); apr_sockaddr_ip_get(&remote_ip,r_sockaddr); connection->id = apr_psprintf(connection->pool,"%s:%hu <-> %s:%hu", local_ip,l_sockaddr->port, remote_ip,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_poller_task_descriptor_add(client->task,&connection->sock_pfd) != TRUE) { apt_log(RTSP_LOG_MARK,APT_PRIO_WARNING,"Failed to Add to Pollset %s",connection->id); apr_socket_close(connection->sock); connection->sock = NULL; return FALSE; } apt_log(RTSP_LOG_MARK,APT_PRIO_NOTICE,"Established RTSP Connection %s",connection->id); return TRUE; }
/** Destroy wakeup pipe */ static apt_bool_t apt_wakeup_pipe_destroy(apt_pollset_t *pollset) { /* Close both sides of the wakeup pipe */ if(pollset->wakeup_pipe[0]) { apr_socket_close(pollset->wakeup_pipe[0]); pollset->wakeup_pipe[0] = NULL; } if(pollset->wakeup_pipe[1]) { apr_socket_close(pollset->wakeup_pipe[1]); pollset->wakeup_pipe[1] = NULL; } return TRUE; }
static apt_bool_t mrcp_server_agent_connection_accept(mrcp_connection_agent_t *agent) { apr_socket_t *sock; apr_pool_t *pool; mrcp_connection_t *connection; if(!agent->null_connection) { apr_pool_create(&pool,NULL); 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; 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(apr_pollset_add(agent->pollset, &connection->sock_pfd) != APR_SUCCESS) { apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Add to Pollset"); apr_socket_close(sock); mrcp_connection_destroy(connection); return FALSE; } connection->agent = agent; connection->it = apt_list_push_back(agent->connection_list,connection); connection->parser = mrcp_parser_create(agent->resource_factory,connection->pool); connection->generator = mrcp_generator_create(agent->resource_factory,connection->pool); apr_socket_addr_get(&connection->sockaddr,APR_REMOTE,sock); if(apr_sockaddr_ip_get(&connection->remote_ip.buf,connection->sockaddr) == APR_SUCCESS) { connection->remote_ip.length = strlen(connection->remote_ip.buf); } apt_log(APT_LOG_MARK,APT_PRIO_NOTICE,"Accepted TCP/MRCPv2 Connection %s:%d", connection->remote_ip.buf, connection->sockaddr->port); return TRUE; }
static apr_status_t jxr_conn_destruct(void *conn, void *params, apr_pool_t *p) { apr_status_t rv = APR_SUCCESS; jaxer_connection *ac = conn; ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, (ac->request) ? ac->request->pool : p, "mod_jaxer: jxr_conn_destruct: closing connection (%d) for worker %s", ac->sock, ac->worker->name); if (ac->sock) { if((rv = apr_socket_close(ac->sock)) != APR_SUCCESS) { ap_log_perror(APLOG_MARK, APLOG_WARNING, rv, (ac->request) ? ac->request->pool : p, "mod_jaxer: apr_socket_close failed: return code=%d", rv); } ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, (ac->request) ? ac->request->pool : p, "mod_jaxer: jxr_conn_destruct: connection (%d) for worker %s closed", ac->sock, ac->worker->name); ac->sock = 0; ac->has_error = 0; ac->request = 0; } return rv; }
void CALLBACK tcp_proxy_on_disconnect(void *plugin_private, const WebSocketServer * server) { TcpProxyData *tpd = (TcpProxyData *) plugin_private; request_rec *r = server->request(server); APACHELOG(APLOG_DEBUG, r, "tcp_proxy_on_disconnect"); if (tpd != 0) { /* When disconnecting, inform the thread that it is time to stop */ tpd->active = 0; tcp_proxy_shutdown_socket(tpd); if (tpd->thread) { apr_status_t status; /* Wait for the thread to finish */ status = apr_thread_join(&status, tpd->thread); } tcp_proxy_shutdown_socket(tpd); if (tpd->tcpsocket) { apr_socket_close(tpd->tcpsocket); tpd->tcpsocket = NULL; } apr_pool_destroy(tpd->pool); } }
OCSP_RESPONSE *modssl_dispatch_ocsp_request(const apr_uri_t *uri, apr_interval_time_t timeout, OCSP_REQUEST *request, conn_rec *c, apr_pool_t *p) { OCSP_RESPONSE *response = NULL; apr_socket_t *sd; BIO *bio; bio = serialize_request(request, uri); if (bio == NULL) { ap_log_cerror(APLOG_MARK, APLOG_ERR, 0, c, APLOGNO(01989) "could not serialize OCSP request"); ssl_log_ssl_error(SSLLOG_MARK, APLOG_ERR, mySrvFromConn(c)); return NULL; } sd = send_request(bio, uri, timeout, c, p); if (sd == NULL) { /* Errors already logged. */ BIO_free(bio); return NULL; } /* Clear the BIO contents, ready for the response. */ (void)BIO_reset(bio); response = read_response(sd, bio, c, p); apr_socket_close(sd); BIO_free(bio); return response; }
static void close_socket(CuTest *tc) { apr_status_t rv; rv = apr_socket_close(sock); CuAssertIntEquals(tc, APR_SUCCESS, rv); }
/* Cleanup callback for a server. */ static apr_status_t cleanup_server(void *baton) { serv_ctx_t *servctx = baton; apr_status_t status; if (servctx->serv_sock) status = apr_socket_close(servctx->serv_sock); else status = APR_EGENERAL; if (servctx->client_sock) { apr_socket_close(servctx->client_sock); } return status; }
static void* APR_THREAD_FUNC test_server_run(apr_thread_t *thd, void *data) { server_thread_data * server_data = (server_thread_data*)data; apr_status_t rv; apr_pool_t *mp; apr_socket_t *s;/* listening socket */ apr_pool_create(&mp, NULL); rv = do_listen(&s, mp, server_data->port); if (rv != APR_SUCCESS) { char errbuf[256]; apr_strerror(rv, errbuf, sizeof(errbuf)); printf("test server error listening: %d, %s\n", rv, errbuf); apr_pool_destroy(mp); apr_thread_exit(thd, rv); return; } server_data->handle->test_server_running = 1; while (server_data->handle->test_server_running == 1) { apr_socket_t *ns;/* accepted socket */ //printf("A"); rv = apr_socket_accept(&ns, s, mp); if(rv == 11) { //printf("."); } else { //printf("B"); if (rv != APR_SUCCESS) { char errbuf[256]; apr_strerror(rv, errbuf, sizeof(errbuf)); printf("test server error accepting: %d, %s\n", rv, errbuf); apr_pool_destroy(mp); apr_thread_exit(thd, rv); return; } /* it is a good idea to specify socket options for the newly accepted socket explicitly */ apr_socket_opt_set(ns, APR_SO_NONBLOCK, 0); apr_socket_timeout_set(ns, 5000); rv = server_data->request_process_callback(ns, mp, server_data); apr_socket_close(ns); if (rv != APR_SUCCESS) { char errbuf[256]; apr_strerror(rv, errbuf, sizeof(errbuf)); printf("test server error processing: %d, %s\n", rv, errbuf); apr_pool_destroy(mp); apr_thread_exit(thd, rv); return; } } } //printf("apr_pool_destroy\n"); apr_pool_destroy(mp); //printf("apr_thread_exit\n"); apr_thread_exit(thd, APR_SUCCESS); //printf("return\n"); return NULL; }
/** Close connection */ static apt_bool_t rtsp_server_connection_close(rtsp_server_t *server, rtsp_server_connection_t *rtsp_connection) { apr_size_t remaining_sessions = 0; if(!rtsp_connection || !rtsp_connection->sock) { return FALSE; } apt_log(RTSP_LOG_MARK,APT_PRIO_INFO,"Close RTSP Connection %s",rtsp_connection->id); apt_poller_task_descriptor_remove(server->task,&rtsp_connection->sock_pfd); apr_socket_close(rtsp_connection->sock); rtsp_connection->sock = NULL; APR_RING_REMOVE(rtsp_connection,link); remaining_sessions = apr_hash_count(rtsp_connection->session_table); if(remaining_sessions) { rtsp_server_session_t *session; void *val; apr_hash_index_t *it; apt_log(RTSP_LOG_MARK,APT_PRIO_NOTICE,"Terminate Remaining RTSP Sessions [%"APR_SIZE_T_FMT"]", remaining_sessions); it = apr_hash_first(rtsp_connection->pool,rtsp_connection->session_table); for(; it; it = apr_hash_next(it)) { apr_hash_this(it,NULL,NULL,&val); session = val; if(session && session->terminating == FALSE) { rtsp_server_session_terminate_request(server,session); } } } else { rtsp_server_connection_destroy(rtsp_connection); } return TRUE; }
/* 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; 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; APR_RING_ELEM_INIT(rtsp_connection,link); if(apr_socket_accept(&rtsp_connection->sock,server->listen_sock,rtsp_connection->pool) != APR_SUCCESS) { apt_log(RTSP_LOG_MARK,APT_PRIO_WARNING,"Failed to Accept RTSP Connection"); 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) { apt_log(RTSP_LOG_MARK,APT_PRIO_WARNING,"Failed to Get RTSP Socket Address"); 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_poller_task_descriptor_add(server->task,&rtsp_connection->sock_pfd) != TRUE) { apt_log(RTSP_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(RTSP_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); rtsp_connection->server = server; APR_RING_INSERT_TAIL(&server->connection_list,rtsp_connection,rtsp_server_connection_t,link); return TRUE; }
static void process_socket(apr_pool_t *p, apr_socket_t *sock, int my_child_num, apr_bucket_alloc_t *bucket_alloc) { conn_rec *current_conn; long conn_id = my_child_num; int csd; ap_sb_handle_t *sbh; (void)apr_os_sock_get(&csd, sock); if (csd >= FD_SETSIZE) { ap_log_error(APLOG_MARK, APLOG_WARNING, 0, NULL, "filedescriptor (%u) larger than FD_SETSIZE (%u) " "found, you probably need to rebuild Apache with a " "larger FD_SETSIZE", csd, FD_SETSIZE); apr_socket_close(sock); return; } ap_create_sb_handle(&sbh, p, 0, my_child_num); current_conn = ap_run_create_connection(p, ap_server_conf, sock, conn_id, sbh, bucket_alloc); if (current_conn) { ap_process_connection(current_conn, sock); ap_lingering_close(current_conn); } }
static apr_socket_t* mpf_socket_create(apr_sockaddr_t **l_sockaddr, const char *ip, apr_port_t port, apr_pool_t *pool) { apr_socket_t *socket = NULL; *l_sockaddr = NULL; apr_sockaddr_info_get( l_sockaddr, ip, APR_INET, port, 0, pool); if(!*l_sockaddr) { apt_log(APT_LOG_MARK,APT_PRIO_DEBUG,"Failed to Get Sockaddr %s:%hu",ip,port); return NULL; } if(apr_socket_create(&socket,(*l_sockaddr)->family,SOCK_DGRAM,0,pool) != APR_SUCCESS) { apt_log(APT_LOG_MARK,APT_PRIO_DEBUG,"Failed to Create Socket %s:%hu", ip,port); return NULL; } apr_socket_opt_set(socket,APR_SO_NONBLOCK,1); apr_socket_timeout_set(socket,0); apr_socket_opt_set(socket,APR_SO_REUSEADDR,1); if(apr_socket_bind(socket,*l_sockaddr) != APR_SUCCESS) { apt_log(APT_LOG_MARK,APT_PRIO_DEBUG,"Failed to Bind Socket to %s:%hu", ip,port); apr_socket_close(socket); return NULL; } return socket; }
static void * APR_THREAD_FUNC lfd_worker_protocol_main_impl(apr_thread_t * thd, void* param) { apr_status_t rc; apr_socket_t * sock = (apr_socket_t*) param; struct lfd_sess * sess; rc = lfd_sess_create(&sess, thd, sock); if(APR_SUCCESS != rc) { //cannot call lfs_sess_destroy because we were unable to construct the object. apr_socket_close(sock); lfd_log_apr_err(rc, "lfd_sess_create failed with errorcode %d"); return NULL; } //if any of the following stages fail, the session obliteration code at the end is run. if(APR_SUCCESS == rc) { rc = emit_greeting(sess); if(APR_SUCCESS != rc) lfd_log_apr_err(rc, "emit_greeting failed"); } if(APR_SUCCESS == rc) { rc = ftp_protocol_loop(sess); if(APR_SUCCESS != rc) lfd_log_apr_err(rc, "ftp_protocol_loop failed"); } lfd_sess_destroy(sess); return NULL; }
static void mrcp_server_agent_listen_socket_destroy(mrcp_connection_agent_t *agent) { if(agent->listen_sock) { apr_socket_close(agent->listen_sock); agent->listen_sock = NULL; } }
static void close_socket(abts_case *tc, void *data) { apr_status_t rv; rv = apr_socket_close(sock); ABTS_INT_EQUAL(tc, APR_SUCCESS, rv); }
static void mrcp_client_agent_control_socket_destroy(mrcp_connection_agent_t *agent) { if(agent->control_sock) { apr_socket_close(agent->control_sock); agent->control_sock = NULL; } }
apr_status_t port_socket_close0(port_t *self) { port_socket_data_t *data = self->data; process_t *proc = proc_lookup(pid_serial(self->owner_in)); if (proc) { xpool_t *tmp = xpool_make(self->pool); int len = buffer_len(data->in_buf); term_t msg; if (len > 0) { term_t bin = make_binary(intnum(len), buffer_ptr(data->in_buf), tmp); msg = make_tuple3(A_TCP, port_id(self, tmp), bin, tmp); proc_new_mail(proc, msg); buffer_clear(data->in_buf); } msg = make_tuple2(A_TCP_CLOSED, port_id(self, tmp), tmp); proc_new_mail(proc, msg); xpool_destroy(tmp); } return apr_socket_close(data->sock); }