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 }
/* ---------------------------- connection stuff ------------------------------ */ static apr_status_t mongo_connect_helper( mongo_connection * conn ){ /* setup */ apr_status_t rv; conn->socket = NULL; conn->connected = 0; rv = apr_sockaddr_info_get(&conn->server_address, conn->left_opts->host, APR_UNSPEC, conn->left_opts->port, 0, conn->p); if ( rv != APR_SUCCESS ) { return rv; } /* memset( conn->sa.sin_zero , 0 , sizeof(conn->sa.sin_zero) ); conn->sa.sin_family = AF_INET; conn->sa.sin_port = htons(conn->left_opts->port); conn->sa.sin_addr.s_addr = inet_addr( conn->left_opts->host ); conn->addressSize = sizeof(conn->sa); */ /* connect */ rv = apr_socket_create(&conn->socket, APR_INET, SOCK_STREAM, APR_PROTO_TCP, conn->p); if ( rv != APR_SUCCESS ) { return rv; } /* conn->sock = socket( AF_INET, SOCK_STREAM, 0 ); if ( conn->sock <= 0 ){ return mongo_conn_no_socket; } if ( connect( conn->sock , (struct sockaddr*)&conn->sa , conn->addressSize ) ){ return mongo_conn_fail; } */ rv = apr_socket_connect(conn->socket, conn->server_address); if ( rv != APR_SUCCESS ) { if ( APR_STATUS_IS_EAFNOSUPPORT(rv )) { // IP6 ? rv = apr_socket_create(&conn->socket, APR_INET6, SOCK_STREAM, APR_PROTO_TCP, conn->p); if ( rv != APR_SUCCESS ) { return rv; } rv = apr_socket_connect(conn->socket, conn->server_address); if ( rv != APR_SUCCESS ) { return rv; } } } /* nagle */ // FIXME: figure out nagle option for APR // setsockopt( conn->sock, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one) ); /* TODO signals */ conn->connected = 1; return APR_SUCCESS; }
apr_status_t sspdy_connect(apr_socket_t **skt, const char *hostname, apr_port_t port, apr_pool_t *pool) { apr_sockaddr_t *host_address = NULL; apr_status_t status; STATUSERR(apr_sockaddr_info_get(&host_address, hostname, APR_UNSPEC, port, 0, pool)); STATUSERR(apr_socket_create(skt, host_address->family, SOCK_STREAM, APR_PROTO_TCP, pool)); STATUSERR(apr_socket_timeout_set(*skt, 0)); STATUSERR(apr_socket_opt_set(*skt, APR_TCP_NODELAY, 1)); status = apr_socket_connect(*skt, host_address); if (status != APR_SUCCESS) { if (!APR_STATUS_IS_EINPROGRESS(status)) return status; } return APR_SUCCESS; }
static apr_socket_t* create_listen_sock(apr_pool_t *mp, unsigned short channel) { apr_status_t rv; apr_socket_t *s; apr_sockaddr_t *sa; rv = apr_sockaddr_info_get(&sa, NULL, APR_INET, DEF_LISTEN_PORT+channel, 0, mp); // Create apr_sockaddr_t from hostname, address family, and port. if (rv != APR_SUCCESS) return NULL; rv = apr_socket_create(&s, sa->family, SOCK_STREAM, APR_PROTO_TCP, mp); //CREATE A SOCKET if (rv != APR_SUCCESS) return NULL; /* non-blocking socket */ apr_socket_opt_set(s, APR_SO_NONBLOCK, 1); //Setup socket options for the specified socke apr_socket_timeout_set(s, 0); //Setup socket timeout for the specified socket apr_socket_opt_set(s, APR_SO_REUSEADDR, 1);/* this is useful for a server(socket listening) process */ rv = apr_socket_bind(s, sa); //Bind the socket to its associated port if (rv != APR_SUCCESS) return NULL; rv = apr_socket_listen(s, DEF_SOCKET_BACKLOG); //Listen to a bound socket for connections. if (rv != APR_SUCCESS) return NULL; return s; }
void server_bind_listen(server_t *svr) { apr_sockaddr_info_get(&svr->comm->sa, NULL, APR_INET, svr->comm->port, 0, svr->comm->mp); /* apr_socket_create(&r->s, r->sa->family, SOCK_DGRAM, APR_PROTO_UDP, r->pl_recv); */ apr_socket_create(&svr->comm->s, svr->comm->sa->family, SOCK_STREAM, APR_PROTO_TCP, svr->comm->mp); apr_socket_opt_set(svr->comm->s, APR_SO_NONBLOCK, 1); apr_socket_timeout_set(svr->comm->s, -1); /* this is useful for a server(socket listening) process */ apr_socket_opt_set(svr->comm->s, APR_SO_REUSEADDR, 1); apr_socket_opt_set(svr->comm->s, APR_TCP_NODELAY, 1); apr_status_t status = APR_SUCCESS; status = apr_socket_bind(svr->comm->s, svr->comm->sa); if (status != APR_SUCCESS) { LOG_ERROR("cannot bind."); printf("%s", apr_strerror(status, malloc(100), 100)); SAFE_ASSERT(status == APR_SUCCESS); } status = apr_socket_listen(svr->comm->s, 30000); // This is important! if (status != APR_SUCCESS) { LOG_ERROR("cannot listen."); printf("%s", apr_strerror(status, malloc(100), 100)); SAFE_ASSERT(status == APR_SUCCESS); } LOG_INFO("successfuly bind and listen on port %d.", svr->comm->port); }
static apt_bool_t mrcp_client_agent_control_socket_create(mrcp_connection_agent_t *agent) { apr_status_t status; if(!agent->control_sockaddr) { return FALSE; } /* create control socket */ status = apr_socket_create(&agent->control_sock, agent->control_sockaddr->family, SOCK_DGRAM, APR_PROTO_UDP, agent->pool); if(status != APR_SUCCESS) { return FALSE; } status = apr_socket_bind(agent->control_sock, agent->control_sockaddr); if(status != APR_SUCCESS) { apr_socket_close(agent->control_sock); agent->control_sock = NULL; return FALSE; } if(agent->control_sockaddr->port == 0) { apr_socket_addr_get(&agent->control_sockaddr,APR_LOCAL,agent->control_sock); } return TRUE; }
int lua_apr_socket_create(lua_State *L) { /* Socket types */ const char *proto_options[] = { "tcp", "udp", NULL }; const int proto_values[] = { APR_PROTO_TCP, APR_PROTO_UDP }; lua_apr_socket *object; apr_status_t status; int family, type, protocol; protocol = proto_values[luaL_checkoption(L, 1, "tcp", proto_options)]; family = family_check(L, 2); type = protocol == APR_PROTO_TCP ? SOCK_STREAM : SOCK_DGRAM; /* Create and initialize the socket and its associated memory pool. */ status = socket_alloc(L, &object); object->family = family; object->protocol = protocol; if (status == APR_SUCCESS) status = apr_socket_create(&object->handle, family, type, protocol, object->pool); if (status != APR_SUCCESS) return push_error_status(L, status); socket_init(L, object); return 1; }
int sock_bind(net_sock_t *nsock, char *address, int port) { int err; network_sock_t *sock = (network_sock_t *)nsock; if (sock == NULL) return(1); log_printf(10, "sock_bind: host=%s:%d\n", address, port); // err = apr_sockaddr_info_get(&(sock->sa), address, APR_INET, port, APR_IPV4_ADDR_OK, sock->mpool); err = apr_sockaddr_info_get(&(sock->sa), address, APR_INET, port, 0, sock->mpool); //log_printf(10, "sock_bind: apr_sockaddr_info_get=%d APR_SUCCESS=%d\n", err, APR_SUCCESS); if (err != APR_SUCCESS) return(err); err = apr_socket_create(&(sock->fd), APR_INET, SOCK_STREAM, APR_PROTO_TCP, sock->mpool); //log_printf(10, "sock_bind: apr_socket_create=%d APR_SUCCESS=%d\n", err, APR_SUCCESS); if (err != APR_SUCCESS) return(err); apr_socket_opt_set(sock->fd, APR_SO_NONBLOCK, 1); apr_socket_opt_set(sock->fd, APR_SO_REUSEADDR, 1); //log_printf(10, "sock_bind: apr_socket_opt_set=%d APR_SUCCESS=%d\n", err, APR_SUCCESS); if (sock->tcpsize > 0) { apr_socket_opt_set(sock->fd, APR_SO_SNDBUF, sock->tcpsize); apr_socket_opt_set(sock->fd, APR_SO_RCVBUF, sock->tcpsize); } err = apr_socket_bind(sock->fd, sock->sa); log_printf(10, "sock_bind: apr_socket_bind=%d APR_SUCCESS=%d\n", err, APR_SUCCESS); return(err); }
int main() { apr_initialize(); apr_pool_t *mempool; apr_sockaddr_t *socket_addr; apr_socket_t *socket; apr_pool_create( &mempool, NULL ); apr_sockaddr_info_get( &socket_addr, NULL, APR_INET, REPLY_PORT, 0, mempool ); apr_socket_create( &socket, socket_addr->family, SOCK_STREAM, APR_PROTO_TCP, mempool ); apr_socket_bind( socket, socket_addr ); apr_socket_listen( socket, SOMAXCONN ); apr_socket_t *accepted; apr_socket_accept( &accepted, socket, mempool ); int *replies = (int*)malloc( frl_reply_size ); apr_size_t len = frl_reply_size; do { apr_socket_recv( accepted, (char*)replies, &len ); int *iter_replies = replies+2; for ( int i = 0; i < 100; i++, iter_replies+=2 ) { std::cout<<*iter_replies<<" "<<*(iter_replies+1)<<std::endl; } std::cout<<"The End."<<std::endl; } while (1); apr_terminate(); return 0; }
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 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; }
/** * Create a listening socket, and listen it. */ static apr_status_t do_listen(apr_socket_t **sock, apr_pool_t *mp, int port) { apr_status_t rv; apr_socket_t *s; apr_sockaddr_t *sa; rv = apr_sockaddr_info_get(&sa, NULL, APR_INET, port, 0, mp); if (rv != APR_SUCCESS) { return rv; } rv = apr_socket_create(&s, sa->family, SOCK_STREAM, APR_PROTO_TCP, mp); if (rv != APR_SUCCESS) { return rv; } /* it is a good idea to specify socket options explicitly. * in this case, we make a blocking socket as the listening socket */ apr_socket_opt_set(s, APR_SO_NONBLOCK, 0); apr_socket_timeout_set(s, 100); apr_socket_opt_set(s, APR_SO_REUSEADDR, 1);/* this is useful for a server(socket listening) process */ rv = apr_socket_bind(s, sa); if (rv != APR_SUCCESS) { return rv; } rv = apr_socket_listen(s, DEF_SOCKET_BACKLOG); if (rv != APR_SUCCESS) { return rv; } *sock = s; return rv; }
apr_status_t serf_listener_create( serf_listener_t **listener, serf_context_t *ctx, const char *host, apr_uint16_t port, void *accept_baton, serf_accept_client_t accept, apr_pool_t *pool) { apr_sockaddr_t *sa; apr_status_t rv; serf_listener_t *l = apr_palloc(pool, sizeof(*l)); l->ctx = ctx; l->baton.type = SERF_IO_LISTENER; l->baton.u.listener = l; l->accept_func = accept; l->accept_baton = accept_baton; apr_pool_create(&l->pool, pool); rv = apr_sockaddr_info_get(&sa, host, APR_UNSPEC, port, 0, l->pool); if (rv) return rv; rv = apr_socket_create(&l->skt, sa->family, SOCK_STREAM, #if APR_MAJOR_VERSION > 0 APR_PROTO_TCP, #endif l->pool); if (rv) return rv; rv = apr_socket_opt_set(l->skt, APR_SO_REUSEADDR, 1); if (rv) return rv; rv = apr_socket_bind(l->skt, sa); if (rv) return rv; rv = apr_socket_listen(l->skt, 5); if (rv) return rv; l->desc.desc_type = APR_POLL_SOCKET; l->desc.desc.s = l->skt; l->desc.reqevents = APR_POLLIN; rv = ctx->pollset_add(ctx->pollset_baton, &l->desc, &l->baton); if (rv) return rv; *listener = l; return APR_SUCCESS; }
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); }
static void create_socket(abts_case *tc, void *data) { apr_status_t rv; rv = apr_socket_create(&sock, APR_INET, SOCK_STREAM, 0, p); ABTS_INT_EQUAL(tc, APR_SUCCESS, rv); ABTS_PTR_NOTNULL(tc, sock); }
static void create_socket(CuTest *tc) { apr_status_t rv; rv = apr_socket_create(&sock, APR_INET, SOCK_STREAM, p); CuAssertIntEquals(tc, APR_SUCCESS, rv); CuAssertPtrNotNull(tc, sock); }
/** * do_listen -- create a listen socket */ apr_status_t do_listen(apr_pollset_t *pollset, apr_hash_t *ht, apr_pool_t *mp, const char *param) { apr_status_t rv; apr_socket_t *s; apr_sockaddr_t *sa; char **ptr; int port; ptr = str_split_char(param, ":"); if (ptr == NULL) { fprintf(stderr, "Get Null param!\n"); return APR_EINVAL; } // then convert param port = atoi(ptr[1]); if (port < 0) return APR_EINVAL; rv = apr_sockaddr_info_get(&sa, ptr[0], APR_INET, port, 0, mp); if (rv != APR_SUCCESS) { return rv; } rv = apr_socket_create(&s, sa->family, SOCK_STREAM, APR_PROTO_TCP, mp); if (rv != APR_SUCCESS) { return rv; } /* non-blocking socket */ apr_socket_opt_set(s, APR_SO_NONBLOCK, 1); apr_socket_timeout_set(s, 0); apr_socket_opt_set(s, APR_SO_REUSEADDR, 1);/* this is useful for a * server(socket listening) process */ rv = apr_socket_bind(s, sa); if (rv != APR_SUCCESS) { return rv; } rv = apr_socket_listen(s, DEF_SOCKET_BACKLOG); if (rv != APR_SUCCESS) { return rv; } // Then.., add it to hash table, listen and connect socket // against a different val, so we can distinguish in the // pollset apr_hash_set(ht, s, APR_HASH_KEY_STRING, "S"); // server // Then.., if everything is OK, add it to the pollset apr_pollfd_t pfd = {mp, APR_POLL_SOCKET, APR_POLLIN, 0, {NULL}, NULL}; pfd.desc.s = s; apr_pollset_add(pollset, &pfd); free(ptr); return APR_SUCCESS; }
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; }
/** * Connect to the remote host */ static apr_status_t tcp_proxy_do_tcp_connect(request_rec * r, TcpProxyData * tpd, apr_pool_t * mp) { apr_sockaddr_t *sa; apr_socket_t *s; apr_status_t rv; APACHELOG(APLOG_DEBUG, r, "tcp_proxy_do_tcp_connect: connect to host %s port %s", tpd->host, tpd->port); int port = atoi(tpd->port); rv = apr_sockaddr_info_get(&sa, tpd->host, APR_INET, port, 0, mp); if (rv != APR_SUCCESS) { return rv; } if (!port) { rv = apr_getservbyname(sa, tpd->port); if (rv != APR_SUCCESS) { return rv; } } rv = apr_socket_create(&s, sa->family, SOCK_STREAM, APR_PROTO_TCP, mp); if (rv != APR_SUCCESS) { return rv; } /* it is a good idea to specify socket options explicitly. * in this case, we make a blocking socket with timeout. */ apr_socket_opt_set(s, APR_SO_NONBLOCK, 0); apr_socket_opt_set(s, APR_SO_KEEPALIVE, 1); if (tpd->timeout) apr_socket_timeout_set(s, ((apr_interval_time_t) tpd->timeout) * APR_USEC_PER_SEC); rv = apr_socket_connect(s, sa); if (rv != APR_SUCCESS) { return rv; } /* see the tutorial about the reason why we have to specify options again */ apr_socket_opt_set(s, APR_SO_NONBLOCK, 0); apr_socket_opt_set(s, APR_SO_KEEPALIVE, 1); if (tpd->timeout) apr_socket_timeout_set(s, ((apr_interval_time_t) tpd->timeout) * APR_USEC_PER_SEC); tpd->tcpsocket = s; return APR_SUCCESS; }
/******************************************************************************** * * Used to establish a connection * ********************************************************************************/ apr_status_t stomp_connect(stomp_connection **connection_ref, const char *hostname, int port, apr_pool_t *pool) { apr_status_t rc; int socket_family; stomp_connection *connection=NULL; btlogger_trace("stomp_connect"); // // Allocate the connection and a memory pool for the connection. // connection = apr_pcalloc(pool, sizeof(stomp_connection)); if( connection == NULL ) { btlogger_warn("stomp_connect cannot allocate for pool APR_ENOMEM"); return APR_ENOMEM; } #define CHECK_SUCCESS if( rc!=APR_SUCCESS ) { return rc; } // Look up the remote address rc = apr_sockaddr_info_get(&connection->remote_sa, hostname, APR_INET, port, 0, pool); CHECK_SUCCESS; // Create and Connect the socket. socket_family = connection->remote_sa->sa.sin.sin_family; rc = apr_socket_create(&connection->socket, socket_family, SOCK_STREAM, APR_PROTO_TCP, pool); CHECK_SUCCESS; rc = apr_socket_connect(connection->socket, connection->remote_sa); // CHECK_SUCCESS; if( rc != APR_SUCCESS ) { apr_status_t rc2 = apr_socket_shutdown(connection->socket, APR_SHUTDOWN_WRITE); btlogger_debug("from stomp.c (1) closing socket"); rc2 = apr_socket_close(connection->socket); return rc; } // Get the Socket Info rc = apr_socket_addr_get(&connection->remote_sa, APR_REMOTE, connection->socket); CHECK_SUCCESS; // rc = apr_sockaddr_ip_get(&connection->remote_ip, connection->remote_sa); // CHECK_SUCCESS; connection->remote_ip = connection->remote_sa->hostname; rc = apr_socket_addr_get(&connection->local_sa, APR_LOCAL, connection->socket); CHECK_SUCCESS; rc = apr_sockaddr_ip_get(&connection->local_ip, connection->local_sa); CHECK_SUCCESS; // Set socket options. // rc = apr_socket_timeout_set( connection->socket, 2*APR_USEC_PER_SEC); // CHECK_SUCCESS; #undef CHECK_SUCCESS *connection_ref = connection; return rc; }
static void im_ssl_listen(nx_module_t *module) { nx_im_ssl_conf_t *imconf; nx_exception_t e; imconf = (nx_im_ssl_conf_t *) module->config; try { if ( imconf->listensock == NULL ) { apr_sockaddr_t *sa; CHECKERR_MSG(apr_socket_create(&(imconf->listensock), APR_INET, SOCK_STREAM, APR_PROTO_TCP, module->input.pool), "couldn't create tcp socket"); CHECKERR_MSG(apr_sockaddr_info_get(&sa, imconf->host, APR_INET, imconf->port, 0, module->input.pool), "apr_sockaddr_info failed for %s:%d", imconf->host, imconf->port); CHECKERR_MSG(apr_socket_opt_set(imconf->listensock, APR_SO_NONBLOCK, 1), "couldn't set SO_NONBLOCK on listen socket"); CHECKERR_MSG(apr_socket_timeout_set(imconf->listensock, 0), "couldn't set socket timeout on listen socket"); CHECKERR_MSG(apr_socket_opt_set(imconf->listensock, APR_SO_REUSEADDR, 1), "couldn't set SO_REUSEADDR on listen socket"); CHECKERR_MSG(apr_socket_opt_set(imconf->listensock, APR_TCP_NODELAY, 1), "couldn't set TCP_NODELAY on listen socket"); CHECKERR_MSG(apr_socket_bind(imconf->listensock, sa), "couldn't bind ssl socket to %s:%d", imconf->host, imconf->port); } else { log_debug("ssl socket already initialized"); } CHECKERR_MSG(apr_socket_listen(imconf->listensock, SOMAXCONN), "couldn't listen to ssl socket on %s:%d", imconf->host, imconf->port); nx_module_pollset_add_socket(module, imconf->listensock, APR_POLLIN | APR_POLLHUP); } catch(e) { if ( imconf->listensock != NULL ) { apr_socket_close(imconf->listensock); imconf->listensock = NULL; } apr_pool_clear(module->input.pool); rethrow(e); } }
void client_connect(client_t *cli) { LOG_DEBUG("connecting to server %s %d", cli->comm->ip, cli->comm->port); apr_status_t status = APR_SUCCESS; status = apr_sockaddr_info_get(&cli->comm->sa, cli->comm->ip, APR_INET, cli->comm->port, 0, cli->comm->mp); SAFE_ASSERT(status == APR_SUCCESS); status = apr_socket_create(&cli->comm->s, cli->comm->sa->family, SOCK_STREAM, APR_PROTO_TCP, cli->comm->mp); SAFE_ASSERT(status == APR_SUCCESS); status = apr_socket_opt_set(cli->comm->s, APR_TCP_NODELAY, 1); SAFE_ASSERT(status == APR_SUCCESS); while (1) { // repeatedly retry LOG_TRACE("TCP CLIENT TRYING TO CONNECT."); status = apr_socket_connect(cli->comm->s, cli->comm->sa); if (status == APR_SUCCESS /*|| status == APR_EINPROGRESS */) { break; } else if (status == APR_ECONNREFUSED) { LOG_DEBUG("client connect refused, maybe the server is not ready yet"); } else if (status == APR_EINVAL) { LOG_ERROR("client connect error, invalid argument. ip: %s, port: %d", cli->comm->ip, cli->comm->port); SAFE_ASSERT(0); } else { LOG_ERROR("client connect error:%s", apr_strerror(status, (char*)malloc(100), 100)); SAFE_ASSERT(0); } apr_sleep(50 * 1000); } LOG_INFO("connected socket on remote addr %s, port %d", cli->comm->ip, cli->comm->port); status = apr_socket_opt_set(cli->comm->s, APR_SO_NONBLOCK, 1); SAFE_ASSERT(status == APR_SUCCESS); // add to epoll // context_t *ctx = c->ctx; // while (ctx->ps == NULL) { // // not inited yet, just wait. // } // apr_pollfd_t pfd = {cli->comm->mp, APR_POLL_SOCKET, APR_POLLIN, 0, {NULL}, NULL}; pfd.desc.s = cli->comm->s; pfd.client_data = cli->pjob; cli->pjob->pfd = pfd; poll_mgr_add_job(cli->pjob->mgr, cli->pjob); // status = apr_pollset_add(pollset_, &ctx->pfd); // status = apr_pollset_add(ctx->ps, &ctx->pfd); // SAFE_ASSERT(status == 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; }
static apr_status_t open_lisp_socket (lisp_cfg_t * cfg) { apr_sockaddr_t * addr; apr_socket_t * socket; if ((SERVER_SOCKET (cfg)) != 0) { if (SERVER_SOCKET_SAFE_P (cfg)) return (APR_SUCCESS); RELAY_ERROR (close_lisp_socket (cfg)); } RELAY_ERROR (apr_sockaddr_info_get ((&addr), (cfg->server_address), APR_UNSPEC, (cfg->server_port), 0, socket_pool)); #if (HAVE_APR_1_2_2) RELAY_ERROR (apr_socket_create ((&socket), AF_INET, SOCK_STREAM, APR_PROTO_TCP, socket_pool)); #else RELAY_ERROR (apr_socket_create ((&socket), AF_INET, SOCK_STREAM, socket_pool)); #endif #if (HAVE_APR_1_2_2) RELAY_ERROR (apr_socket_connect (socket, addr)); #else RELAY_ERROR (apr_connect (socket, addr)); #endif { input_buffer_t * buffer = (apr_palloc (socket_pool, (sizeof (input_buffer_t)))); (buffer -> start) = (buffer -> data); (buffer -> end) = (buffer -> data); RELAY_ERROR (apr_socket_data_set (socket, buffer, "input-buffer", 0)); } (SERVER_SOCKET (cfg)) = socket; (SERVER_SOCKET_SAFE_P (cfg)) = 0; return (APR_SUCCESS); }
static void udp6_socket(abts_case *tc, void *data) { apr_status_t rv; apr_socket_t *sock = NULL; rv = apr_socket_create(&sock, APR_INET6, SOCK_DGRAM, 0, p); if (APR_STATUS_IS_EAFNOSUPPORT(rv)) { ABTS_NOT_IMPL(tc, "IPv6 not enabled"); return; } ABTS_INT_EQUAL(tc, APR_SUCCESS, rv); ABTS_PTR_NOTNULL(tc, sock); apr_socket_close(sock); }
static void make_socket(apr_socket_t **sock, apr_sockaddr_t **sa, apr_port_t port, apr_pool_t *p, abts_case *tc) { apr_status_t rv; rv = apr_sockaddr_info_get(sa, "127.0.0.1", APR_UNSPEC, port, 0, p); ABTS_INT_EQUAL(tc, APR_SUCCESS, rv); rv = apr_socket_create(sock, (*sa)->family, SOCK_DGRAM, 0, p); ABTS_INT_EQUAL(tc, APR_SUCCESS, rv); rv = apr_socket_bind((*sock), (*sa)); ABTS_INT_EQUAL(tc, APR_SUCCESS, rv); }
/****************************************************************************** * * Used to establish a connection * ********************************************************************************/ APR_DECLARE(apr_status_t) stomp_connect(stomp_connection **connection_ref, const char *hostname, int port, apr_pool_t *pool) { apr_status_t rc; int socket_family; stomp_connection *connection=NULL; // // Allocate the connection and a memory pool for the connection. // connection = apr_pcalloc(pool, sizeof(stomp_connection)); if( connection == NULL ) return APR_ENOMEM; #define CHECK_SUCCESS if( rc!=APR_SUCCESS ) { return rc; } // Look up the remote address rc = apr_sockaddr_info_get(&connection->remote_sa, hostname, APR_UNSPEC, port, APR_IPV4_ADDR_OK, pool); CHECK_SUCCESS; // Create the socket. socket_family = connection->remote_sa->sa.sin.sin_family; rc = apr_socket_create(&connection->socket, socket_family, SOCK_STREAM, APR_PROTO_TCP, pool); CHECK_SUCCESS; // Set socket options. rc = apr_socket_opt_set(connection->socket, APR_SO_NONBLOCK, 1); CHECK_SUCCESS; rc = apr_socket_timeout_set(connection->socket, 1 * APR_USEC_PER_SEC); CHECK_SUCCESS; // Try connect rc = apr_socket_connect(connection->socket, connection->remote_sa); CHECK_SUCCESS; // Get the Socket Info rc = apr_socket_addr_get(&connection->remote_sa, APR_REMOTE, connection->socket); CHECK_SUCCESS; rc = apr_sockaddr_ip_get(&connection->remote_ip, connection->remote_sa); CHECK_SUCCESS; rc = apr_socket_addr_get(&connection->local_sa, APR_LOCAL, connection->socket); CHECK_SUCCESS; rc = apr_sockaddr_ip_get(&connection->local_ip, connection->local_sa); CHECK_SUCCESS; #undef CHECK_SUCCESS *connection_ref = connection; return rc; } // stomp_connect
static void make_socket(apr_socket_t **sock, apr_sockaddr_t **sa, apr_port_t port, apr_pool_t *p, CuTest *tc) { apr_status_t rv; rv = apr_sockaddr_info_get(sa, "127.0.0.1", APR_UNSPEC, port, 0, p); CuAssertIntEquals(tc, APR_SUCCESS, rv); rv = apr_socket_create(sock, (*sa)->family, SOCK_DGRAM, p); CuAssertIntEquals(tc, APR_SUCCESS, rv); rv =apr_socket_bind((*sock), (*sa)); CuAssertIntEquals(tc, APR_SUCCESS, rv); }
apr_status_t start_test_server(serv_ctx_t *servctx) { apr_status_t status; apr_socket_t *serv_sock; /* create server socket */ #if APR_VERSION_AT_LEAST(1, 0, 0) status = apr_socket_create(&serv_sock, servctx->serv_addr->family, SOCK_STREAM, 0, servctx->pool); #else status = apr_socket_create(&serv_sock, servctx->serv_addr->family, SOCK_STREAM, servctx->pool); #endif if (status != APR_SUCCESS) return status; apr_socket_opt_set(serv_sock, APR_SO_NONBLOCK, 1); apr_socket_timeout_set(serv_sock, 0); apr_socket_opt_set(serv_sock, APR_SO_REUSEADDR, 1); status = apr_socket_bind(serv_sock, servctx->serv_addr); if (status != APR_SUCCESS) return status; /* listen for clients */ status = apr_socket_listen(serv_sock, SOMAXCONN); if (status != APR_SUCCESS) return status; servctx->serv_sock = serv_sock; servctx->client_sock = NULL; return APR_SUCCESS; }
/* Start a TCP server on port SERV_PORT in thread THREAD. srv_replay is a array of action to replay when connection started. replay_count is count of actions in srv_replay. */ static apr_status_t prepare_server(test_baton_t *tb, apr_pool_t *pool) { apr_status_t status; apr_socket_t *serv_sock; /* create server socket */ #if APR_VERSION_AT_LEAST(1, 0, 0) status = apr_socket_create(&serv_sock, APR_INET, SOCK_STREAM, 0, pool); #else status = apr_socket_create(&serv_sock, APR_INET, SOCK_STREAM, pool); #endif if (status != APR_SUCCESS) return status; apr_socket_opt_set(serv_sock, APR_SO_NONBLOCK, 1); apr_socket_timeout_set(serv_sock, 0); apr_socket_opt_set(serv_sock, APR_SO_REUSEADDR, 1); status = apr_socket_bind(serv_sock, tb->serv_addr); if (status != APR_SUCCESS) return status; /* Start replay from first action. */ tb->cur_action = 0; tb->action_buf_pos = 0; /* listen for clients */ apr_socket_listen(serv_sock, SOMAXCONN); if (status != APR_SUCCESS) return status; tb->serv_sock = serv_sock; tb->client_sock = NULL; return APR_SUCCESS; }