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; }
/* 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 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; }
/** 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; }
/****************************************************************************** * * 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
/** * Rewrite UserAgent IP */ static void rewrite_req_ip(request_rec *r, const char *new_ip) { conn_rec *c = r->connection; // Rewrite IP apr_sockaddr_t *temp_sa = _USERAGENT_ADDR; apr_sockaddr_info_get(&temp_sa, new_ip, APR_UNSPEC, temp_sa->port, APR_IPV4_ADDR_OK, c->pool); _USERAGENT_ADDR = temp_sa; apr_sockaddr_ip_get(&_USERAGENT_IP, _USERAGENT_ADDR); apr_sockaddr_ip_get(&_REMOTE_HOST, _USERAGENT_ADDR); //c->remote_host = NULL; // Force DNS re-resolution #ifdef DEBUG ap_log_error(APLOG_MARK, APLOG_WARNING, 0, NULL, MODULE_NAME "::rewrite_req_ip IP Connection from: %s:%d [%s] to port=%d newip=%s (OK)", _CLIENT_IP, _CLIENT_ADDR->port, _USERAGENT_IP, c->local_addr->port, new_ip); #endif }
int lua_apr_host_to_addr(lua_State *L) { apr_sockaddr_t *address; apr_pool_t *pool; const char *host; char *ip_address; apr_status_t status; int family; pool = to_pool(L); host = luaL_checkstring(L, 1); family = family_check(L, 2); status = apr_sockaddr_info_get(&address, host, family, SOCK_STREAM, 0, pool); if (status != APR_SUCCESS) return push_error_status(L, status); lua_settop(L, 0); do { status = apr_sockaddr_ip_get(&ip_address, address); if (status != APR_SUCCESS) return push_error_status(L, status); lua_pushstring(L, ip_address); address = address->next; } while (address != NULL); return lua_gettop(L); }
static char *conv_apr_sockaddr(apr_sockaddr_t *sa, char *buf_end, int *len) { char *p = buf_end; bool_int is_negative; int sub_len; char *ipaddr_str; p = conv_10(sa->port, TRUE, &is_negative, p, &sub_len); *--p = ':'; apr_sockaddr_ip_get(&ipaddr_str, sa); sub_len = strlen(ipaddr_str); #if APR_HAVE_IPV6 if (sa->family == APR_INET6 && !IN6_IS_ADDR_V4MAPPED(&sa->sa.sin6.sin6_addr)) { *(p - 1) = ']'; p -= sub_len + 2; *p = '['; memcpy(p + 1, ipaddr_str, sub_len); } else #endif { p -= sub_len; memcpy(p, ipaddr_str, sub_len); } *len = buf_end - p; return (p); }
static const char *proxies_set(cmd_parms *cmd, void *cfg, const char *arg) { remoteip_config_t *config = ap_get_module_config(cmd->server->module_config, &remoteip_module); remoteip_proxymatch_t *match; apr_status_t rv; char *ip = apr_pstrdup(cmd->temp_pool, arg); char *s = ap_strchr(ip, '/'); if (s) { *s++ = '\0'; } if (!config->proxymatch_ip) { config->proxymatch_ip = apr_array_make(cmd->pool, 1, sizeof(*match)); } match = (remoteip_proxymatch_t *) apr_array_push(config->proxymatch_ip); match->internal = cmd->info; if (looks_like_ip(ip)) { /* Note s may be null, that's fine (explicit host) */ rv = apr_ipsubnet_create(&match->ip, ip, s, cmd->pool); } else { apr_sockaddr_t *temp_sa; if (s) { return apr_pstrcat(cmd->pool, "RemoteIP: Error parsing IP ", arg, " the subnet /", s, " is invalid for ", cmd->cmd->name, NULL); } rv = apr_sockaddr_info_get(&temp_sa, ip, APR_UNSPEC, 0, APR_IPV4_ADDR_OK, cmd->temp_pool); while (rv == APR_SUCCESS) { apr_sockaddr_ip_get(&ip, temp_sa); rv = apr_ipsubnet_create(&match->ip, ip, NULL, cmd->pool); if (!(temp_sa = temp_sa->next)) { break; } match = (remoteip_proxymatch_t *) apr_array_push(config->proxymatch_ip); match->internal = cmd->info; } } if (rv != APR_SUCCESS) { char msgbuf[128]; apr_strerror(rv, msgbuf, sizeof(msgbuf)); return apr_pstrcat(cmd->pool, "RemoteIP: Error parsing IP ", arg, " (", msgbuf, " error) for ", cmd->cmd->name, NULL); } return NULL; }
/******************************************************************************** * * 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; }
apr_sockaddr_t* url_GetAddress(const URLInfo* url, int index) { apr_sockaddr_t* a = getConn(url, index); #if DEBUG char* str; apr_sockaddr_ip_get(&str, a); printf("Connecting to %s\n", str); #endif return a; }
static void im_ssl_disconnect(nx_module_input_t *input) { char *ipstr; apr_sockaddr_t *sa; log_debug("im_ssl got disconnect"); CHECKERR_MSG(apr_socket_addr_get(&sa, APR_REMOTE, input->desc.s), "couldn't get info on remote socket"); CHECKERR_MSG(apr_sockaddr_ip_get(&ipstr, sa), "couldn't get IP of remote socket"); log_warn("SSL connection closed from %s:%u", ipstr, sa->port); im_ssl_free_input(input); }
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; }
int slayer_server_log_request(slayer_server_log_manager_t *manager, apr_pool_t *mpool, apr_socket_t *conn, const char *request_line, int response_code, int nbytes_sent, apr_int64_t time_toservice) { //generate data char dstring[1024]; apr_int64_t current_time = apr_time_now(); apr_size_t result_size; apr_time_exp_t ltime; apr_time_exp_lt(<ime,current_time); apr_strftime (dstring, &result_size, sizeof(dstring), "%d/%b/%Y:%H:%M:%S %z", <ime ); apr_sockaddr_t *client_addr; char *client_ip; apr_socket_addr_get(&client_addr,0,conn); apr_sockaddr_ip_get(&client_ip,client_addr); if (manager->fhandle) { char *message = apr_pstrcat(mpool,client_ip," - - ","[",dstring,"] \"",request_line,"\" ", apr_itoa(mpool,response_code)," ",apr_itoa(mpool,nbytes_sent), " ",apr_ltoa(mpool,time_toservice), "\n",NULL); slayer_server_log_message(manager,message); } slayer_server_log_add_entry(manager,mpool,client_ip,current_time,request_line,response_code,nbytes_sent,time_toservice); return 0; }
svn_ra_svn_conn_t *svn_ra_svn_create_conn2(apr_socket_t *sock, apr_file_t *in_file, apr_file_t *out_file, int compression_level, apr_pool_t *pool) { svn_ra_svn_conn_t *conn = apr_palloc(pool, sizeof(*conn)); assert((sock && !in_file && !out_file) || (!sock && in_file && out_file)); #ifdef SVN_HAVE_SASL conn->sock = sock; conn->encrypted = FALSE; #endif conn->session = NULL; conn->read_ptr = conn->read_buf; conn->read_end = conn->read_buf; conn->write_pos = 0; conn->block_handler = NULL; conn->block_baton = NULL; conn->capabilities = apr_hash_make(pool); conn->compression_level = compression_level; conn->pool = pool; if (sock != NULL) { apr_sockaddr_t *sa; conn->stream = svn_ra_svn__stream_from_sock(sock, pool); if (!(apr_socket_addr_get(&sa, APR_REMOTE, sock) == APR_SUCCESS && apr_sockaddr_ip_get(&conn->remote_ip, sa) == APR_SUCCESS)) conn->remote_ip = NULL; } else { conn->stream = svn_ra_svn__stream_from_files(in_file, out_file, pool); conn->remote_ip = NULL; } return conn; }
static void im_tcp_got_disconnect(nx_module_t *module, nx_event_t *event) { apr_socket_t *sock; nx_module_input_t *input; apr_sockaddr_t *sa; char *ipstr; ASSERT(event != NULL); ASSERT(module != NULL); log_debug("im_tcp_disconnect"); sock = (apr_socket_t *) event->data; CHECKERR_MSG(apr_socket_data_get((void **) &input, "input", sock), "couldn't get input data from socket"); 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_warn("TCP connection closed from %s:%u", ipstr, sa->port); im_tcp_free_input(input); }
static int socket_addr_get(lua_State *L) { const char *options[] = { "local", "remote", NULL }; const apr_interface_e values[] = { APR_LOCAL, APR_REMOTE }; lua_apr_socket *object; apr_sockaddr_t *address; apr_status_t status; apr_interface_e which; char *ip_address; object = socket_check(L, 1, 1); which = values[luaL_checkoption(L, 2, "remote", options)]; status = apr_socket_addr_get(&address, which, object->handle); if (status == APR_SUCCESS) status = apr_sockaddr_ip_get(&ip_address, address); if (status != APR_SUCCESS) return push_error_status(L, status); lua_pushstring(L, ip_address); lua_pushinteger(L, address->port); lua_pushstring(L, address->hostname); return 3; }
static int remoteip_modify_request(request_rec *r) { conn_rec *c = r->connection; remoteip_config_t *config = (remoteip_config_t *) ap_get_module_config(r->server->module_config, &remoteip_module); remoteip_req_t *req = NULL; apr_sockaddr_t *temp_sa; apr_status_t rv; char *remote; char *proxy_ips = NULL; char *parse_remote; char *eos; unsigned char *addrbyte; void *internal = NULL; if (!config->header_name) { return DECLINED; } remote = (char *) apr_table_get(r->headers_in, config->header_name); if (!remote) { return OK; } remote = apr_pstrdup(r->pool, remote); temp_sa = c->remote_addr; while (remote) { /* verify c->remote_addr is trusted if there is a trusted proxy list */ if (config->proxymatch_ip) { int i; remoteip_proxymatch_t *match; match = (remoteip_proxymatch_t *)config->proxymatch_ip->elts; for (i = 0; i < config->proxymatch_ip->nelts; ++i) { if (apr_ipsubnet_test(match[i].ip, c->remote_addr)) { internal = match[i].internal; break; } } if (i && i >= config->proxymatch_ip->nelts) { break; } } if ((parse_remote = strrchr(remote, ',')) == NULL) { parse_remote = remote; remote = NULL; } else { *(parse_remote++) = '\0'; } while (*parse_remote == ' ') { ++parse_remote; } eos = parse_remote + strlen(parse_remote) - 1; while (eos >= parse_remote && *eos == ' ') { *(eos--) = '\0'; } if (eos < parse_remote) { if (remote) { *(remote + strlen(remote)) = ','; } else { remote = parse_remote; } break; } /* We map as IPv4 rather than IPv6 for equivilant host names * or IPV4OVERIPV6 */ rv = apr_sockaddr_info_get(&temp_sa, parse_remote, APR_UNSPEC, temp_sa->port, APR_IPV4_ADDR_OK, r->pool); if (rv != APR_SUCCESS) { ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rv, r, "RemoteIP: Header %s value of %s cannot be parsed " "as a client IP", config->header_name, parse_remote); if (remote) { *(remote + strlen(remote)) = ','; } else { remote = parse_remote; } break; } addrbyte = (unsigned char *) &temp_sa->sa.sin.sin_addr; /* For intranet (Internal proxies) ignore all restrictions below */ if (!internal && ((temp_sa->family == APR_INET /* For internet (non-Internal proxies) deny all * RFC3330 designated local/private subnets: * 10.0.0.0/8 169.254.0.0/16 192.168.0.0/16 * 127.0.0.0/8 172.16.0.0/12 */ && (addrbyte[0] == 10 || addrbyte[0] == 127 || (addrbyte[0] == 169 && addrbyte[1] == 254) || (addrbyte[0] == 172 && (addrbyte[1] & 0xf0) == 16) || (addrbyte[0] == 192 && addrbyte[1] == 168))) #if APR_HAVE_IPV6 || (temp_sa->family == APR_INET6 /* For internet (non-Internal proxies) we translated * IPv4-over-IPv6-mapped addresses as IPv4, above. * Accept only Global Unicast 2000::/3 defined by RFC4291 */ && ((temp_sa->sa.sin6.sin6_addr.s6_addr[0] & 0xe0) != 0x20)) #endif )) { ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rv, r, "RemoteIP: Header %s value of %s appears to be " "a private IP or nonsensical. Ignored", config->header_name, parse_remote); if (remote) { *(remote + strlen(remote)) = ','; } else { remote = parse_remote; } break; } /* save away our results */ if (!req) { req = (remoteip_req_t *) apr_palloc(r->pool, sizeof(remoteip_req_t)); } /* Set remote_ip string */ if (!internal) { if (proxy_ips) { proxy_ips = apr_pstrcat(r->pool, proxy_ips, ", ", c->remote_ip, NULL); } else { proxy_ips = c->remote_ip; } } req->remote_addr = temp_sa; apr_sockaddr_ip_get(&req->remote_ip, req->remote_addr); } /* Nothing happened? */ if (!req) { return OK; } req->proxied_remote = remote; req->proxy_ips = proxy_ips; if (req->proxied_remote) { apr_table_setn(r->headers_in, config->header_name, req->proxied_remote); } else { apr_table_unset(r->headers_in, config->header_name); } if (req->proxy_ips) { apr_table_setn(r->notes, "remoteip-proxy-ip-list", req->proxy_ips); if (config->proxies_header_name) { apr_table_setn(r->headers_in, config->proxies_header_name, req->proxy_ips); } } c->remote_addr = req->remote_addr; c->remote_ip = req->remote_ip; ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, req->proxy_ips ? "Using %s as client's IP by proxies %s" : "Using %s as client's IP by internal proxies", req->remote_ip, req->proxy_ips); return OK; }
lt_http_status_t lt_http_server_run( lt_http_server_t * server ) { apr_pool_t * pool = NULL; apr_socket_t * client = NULL; char error[1025]; memset( error, 0, 1025 ); if( server == NULL ) return LT_HTTP_INVALID_ARG; /* prepare connection pool */ apr_pool_create( &pool, server->pool ); /* make the socket non-blocking */ if( APR_SUCCESS != apr_socket_opt_set( server->socket, APR_SO_NONBLOCK, 1 ) ) { my_perror( "ERROR: apr_socket_opt_set failed with: " ); return LT_HTTP_INVALID_ARG; } while( 1 ) { apr_status_t rv; /* bool reading should be atomic operation so no locking is needed */ if( server->stoprequested ) { break; } /* clear pool memory */ apr_pool_clear( pool ); /* accept new connection */ rv = apr_socket_accept( &client, server->socket, pool ); if( APR_STATUS_IS_EAGAIN( rv ) || APR_STATUS_IS_EINTR( rv ) ) { /* sleep for 100ms before accepting new client */ apr_sleep( 100 * 1000 ); continue; } if( APR_SUCCESS != rv ) { my_perror( "ERROR: apr_socket_accept failed with: " ); continue; } /* determine client address */ { apr_sockaddr_t * sa = NULL; char * ip = NULL; if( APR_SUCCESS != apr_socket_addr_get( &sa, APR_REMOTE, client ) ) { my_perror( "ERROR: apr_socket_addr_get failed with: " ); apr_socket_close( client ); continue; } if( APR_SUCCESS != apr_sockaddr_ip_get( &ip, sa ) ) { my_perror( "ERROR: apr_sockaddr_ip_get failed with: " ); apr_socket_close( client ); continue; } } /* immediatelly start sending HTTP response headers */ { char * headers = apr_pstrcat( pool, "HTTP/1.0 200 OK\r\n" "Content-Length: ", apr_ltoa( pool, server->finfo.size ), "\r\n", "Content-Type: application/octet-stream;" " charset=utf-8\r\n", "Connection: Close\r\n", "\r\n", NULL ); apr_size_t headers_size = strlen( headers ); if( APR_SUCCESS != apr_socket_send( client, headers, &headers_size ) ) { my_perror( "ERROR: apr_socket_send failed with: " ); apr_socket_close( client ); continue; } } /* send file contents */ { apr_off_t offset = 0; apr_size_t len = server->finfo.size; if( APR_SUCCESS != apr_socket_sendfile( client, server->file, NULL, &offset, &len, 0 ) ) { my_perror( "ERROR: apr_socket_sendfile failed with: " ); apr_socket_close( client ); continue; } } /* read and discard all headers */ { apr_status_t rv; /* set non-block option on client socket */ if( APR_SUCCESS != apr_socket_timeout_set( client, 2 * 1000 * 1000 ) ) { my_perror( "ERROR: apr_socket_timeout_set failed with: " ); apr_socket_close( client ); continue; } /* read all data until 2 sec timeout or eof, then proceed to */ /* close */ do { char buffer[1024]; apr_size_t len = 1024; rv = apr_socket_recv( client, buffer, &len ); if( APR_STATUS_IS_TIMEUP( rv ) || APR_STATUS_IS_EOF( rv ) ) { break; } } while( 1 ); } /* close our side of connection */ if( APR_SUCCESS != apr_socket_shutdown( client, APR_SHUTDOWN_WRITE ) ) { /* we actually don't care about errors arriving during shutdown * phase * my_perror( "ERROR: apr_socket_shutdown(WRITE) failed with: " ); */ apr_socket_close( client ); continue; } /* close other side of connection */ if( APR_SUCCESS != apr_socket_shutdown( client, APR_SHUTDOWN_READ ) ) { /* we actually don't care about errors arriving during shutdown * phase * my_perror( "ERROR: apr_socket_shutdown(READ) failed with: " ); */ apr_socket_close( client ); continue; } /* close socket */ if( APR_SUCCESS != apr_socket_close( client ) ) { /* we actually don't care about errors arriving during shutdown * phase * my_perror( "ERROR: apr_socket_close failed with: " ); */ continue; } } return LT_HTTP_SUCCESS; }
static void sendto_receivefrom_helper(abts_case *tc, const char *addr, int family) { apr_status_t rv; apr_socket_t *sock = NULL; apr_socket_t *sock2 = NULL; char sendbuf[STRLEN] = "APR_INET, SOCK_DGRAM"; char recvbuf[80]; char *ip_addr; apr_port_t fromport; apr_sockaddr_t *from; apr_sockaddr_t *to; apr_size_t len = 30; rv = apr_socket_create(&sock, family, SOCK_DGRAM, 0, p); #if APR_HAVE_IPV6 if ((family == APR_INET6) && APR_STATUS_IS_EAFNOSUPPORT(rv)) { ABTS_NOT_IMPL(tc, "IPv6 not enabled"); return; } #endif ABTS_INT_EQUAL(tc, APR_SUCCESS, rv); if (rv != APR_SUCCESS) return; rv = apr_socket_create(&sock2, family, SOCK_DGRAM, 0, p); ABTS_INT_EQUAL(tc, APR_SUCCESS, rv); if (rv != APR_SUCCESS) return; rv = apr_sockaddr_info_get(&to, addr, family, 7772, 0, p); ABTS_INT_EQUAL(tc, APR_SUCCESS, rv); rv = apr_sockaddr_info_get(&from, addr, family, 7771, 0, p); ABTS_INT_EQUAL(tc, APR_SUCCESS, rv); rv = apr_socket_opt_set(sock, APR_SO_REUSEADDR, 1); APR_ASSERT_SUCCESS(tc, "Could not set REUSEADDR on socket", rv); rv = apr_socket_opt_set(sock2, APR_SO_REUSEADDR, 1); APR_ASSERT_SUCCESS(tc, "Could not set REUSEADDR on socket2", rv); rv = apr_socket_bind(sock, to); APR_ASSERT_SUCCESS(tc, "Could not bind socket", rv); if (rv != APR_SUCCESS) return; rv = apr_socket_bind(sock2, from); APR_ASSERT_SUCCESS(tc, "Could not bind second socket", rv); if (rv != APR_SUCCESS) return; len = STRLEN; rv = apr_socket_sendto(sock2, to, 0, sendbuf, &len); ABTS_INT_EQUAL(tc, APR_SUCCESS, rv); ABTS_SIZE_EQUAL(tc, STRLEN, len); /* fill the "from" sockaddr with a random address from another * family to ensure that recvfrom sets it up properly. */ #if APR_HAVE_IPV6 if (family == APR_INET) rv = apr_sockaddr_info_get(&from, "3ffE:816e:abcd:1234::1", APR_INET6, 4242, 0, p); else #endif rv = apr_sockaddr_info_get(&from, "127.1.2.3", APR_INET, 4242, 0, p); ABTS_INT_EQUAL(tc, APR_SUCCESS, rv); len = 80; rv = apr_socket_recvfrom(from, sock, 0, recvbuf, &len); ABTS_INT_EQUAL(tc, APR_SUCCESS, rv); ABTS_SIZE_EQUAL(tc, STRLEN, len); ABTS_STR_EQUAL(tc, "APR_INET, SOCK_DGRAM", recvbuf); apr_sockaddr_ip_get(&ip_addr, from); fromport = from->port; ABTS_STR_EQUAL(tc, addr, ip_addr); ABTS_INT_EQUAL(tc, 7771, fromport); apr_socket_close(sock); apr_socket_close(sock2); }
static void im_ssl_accept(nx_module_t *module) { nx_im_ssl_conf_t *imconf; apr_socket_t *sock; apr_sockaddr_t *sa; char *ipstr; nx_module_input_t *input; SSL *ssl; apr_pool_t *pool = NULL; apr_status_t rv; nx_exception_t e; log_debug("im_ssl_accept"); imconf = (nx_im_ssl_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"); 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"); nx_module_pollset_add_socket(module, imconf->listensock, APR_POLLIN | APR_POLLHUP); ssl = nx_ssl_from_socket(&(imconf->ssl_ctx), sock); ASSERT(ssl != NULL); SSL_set_accept_state(ssl); //SSL_accept(ssl); 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"); input = nx_module_input_new(module, pool); input->desc_type = APR_POLL_SOCKET; input->desc.s = sock; input->inputfunc = imconf->inputfunc; ASSERT(input->inputfunc != NULL); nx_module_input_data_set(input, "ssl", ssl); 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, sock, APR_POLLIN | APR_POLLHUP); log_info("SSL connection accepted from %s:%u", ipstr, sa->port); } } catch(e) { apr_pool_destroy(pool); rethrow(e); } }
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_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 NULL; } apt_log(APT_LOG_MARK,APT_PRIO_NOTICE,"Established 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 connection; }
SWITCH_DECLARE(switch_status_t) switch_sockaddr_ip_get(char **addr, switch_sockaddr_t *sa) { return apr_sockaddr_ip_get(addr, sa); }
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; }
static char * ipaddr(apr_sockaddr_t *a) { char * string; apr_sockaddr_ip_get(&string, a); return string; }
static void sendto_receivefrom(abts_case *tc, void *data) { apr_status_t rv; apr_socket_t *sock = NULL; apr_socket_t *sock2 = NULL; char sendbuf[STRLEN] = "APR_INET, SOCK_DGRAM"; char recvbuf[80]; char *ip_addr; apr_port_t fromport; apr_sockaddr_t *from; apr_sockaddr_t *to; apr_size_t len = 30; int family; const char *addr; #if APR_HAVE_IPV6 family = APR_INET6; addr = "::1"; rv = apr_socket_create(&sock, family, SOCK_DGRAM, 0, p); if (V6_NOT_ENABLED(rv)) { #endif family = APR_INET; addr = "127.0.0.1"; rv = apr_socket_create(&sock, family, SOCK_DGRAM, 0, p); #if APR_HAVE_IPV6 } #endif ABTS_INT_EQUAL(tc, APR_SUCCESS, rv); rv = apr_socket_create(&sock2, family, SOCK_DGRAM, 0, p); ABTS_INT_EQUAL(tc, APR_SUCCESS, rv); rv = apr_sockaddr_info_get(&to, addr, APR_UNSPEC, 7772, 0, p); ABTS_INT_EQUAL(tc, APR_SUCCESS, rv); rv = apr_sockaddr_info_get(&from, addr, APR_UNSPEC, 7771, 0, p); ABTS_INT_EQUAL(tc, APR_SUCCESS, rv); rv = apr_socket_opt_set(sock, APR_SO_REUSEADDR, 1); APR_ASSERT_SUCCESS(tc, "Could not set REUSEADDR on socket", rv); rv = apr_socket_opt_set(sock2, APR_SO_REUSEADDR, 1); APR_ASSERT_SUCCESS(tc, "Could not set REUSEADDR on socket2", rv); rv = apr_socket_bind(sock, to); ABTS_INT_EQUAL(tc, APR_SUCCESS, rv); rv = apr_socket_bind(sock2, from); ABTS_INT_EQUAL(tc, APR_SUCCESS, rv); len = STRLEN; rv = apr_socket_sendto(sock2, to, 0, sendbuf, &len); ABTS_INT_EQUAL(tc, APR_SUCCESS, rv); ABTS_INT_EQUAL(tc, STRLEN, len); len = 80; rv = apr_socket_recvfrom(from, sock, 0, recvbuf, &len); ABTS_INT_EQUAL(tc, APR_SUCCESS, rv); ABTS_INT_EQUAL(tc, STRLEN, len); ABTS_STR_EQUAL(tc, "APR_INET, SOCK_DGRAM", recvbuf); apr_sockaddr_ip_get(&ip_addr, from); fromport = from->port; ABTS_STR_EQUAL(tc, addr, ip_addr); ABTS_INT_EQUAL(tc, 7771, fromport); apr_socket_close(sock); apr_socket_close(sock2); }
/*! Internal use callback associated with cpe_socket_after_accept(). * Handle the accept and put the new accepted socket in the event system. */ static apr_status_t cpe_socket_accept_cb(void *context, apr_pollfd_t *pfd, cpe_event *e) { apr_socket_t *newsock; apr_status_t rv; apr_sockaddr_t *sockaddr; cpe_socket_prepare_ctx *ctx; cpe_event *event; char *hostip; ctx = context; assert(ctx != NULL); pfd = NULL; cpe_event_add(e); CHECK(rv = apr_socket_accept(&newsock, ctx->pc_orig_socket, g_cpe_pool)); rv = apr_socket_addr_get(&sockaddr, APR_REMOTE, newsock); if (rv == APR_SUCCESS) { apr_sockaddr_ip_get(&hostip, sockaddr); } else { cpe_log(CPE_ERR, "apr_sock_addr_get: %s", cpe_errmsg(rv)); cpe_socket_close(newsock); return rv; } if (cpe_increment_peers(ctx) != APR_SUCCESS) { cpe_socket_close(newsock); cpe_log(CPE_WARN, "rejected connection from %s %d (too many)", hostip, sockaddr->port); return APR_EGENERAL; } /* Useless because APR doesn't call the callback on socket close (as * documented), but on pool destruction. So for the time being we use * our wrapper cpe_socket_close() */ CHECK(apr_socket_data_set(newsock, ctx, "dummykey", cpe_socket_cleanup_cb)); /* SECURITY: if the user didn't specify an accept filter, accept * only connections from localhost */ if (ctx->pc_afilter != NULL) { rv = ctx->pc_afilter(sockaddr); } else { rv = cpe_filter_localhost(sockaddr); } if (rv != APR_SUCCESS) { cpe_socket_close(newsock); cpe_log(CPE_WARN, "rejected connection from %s %d (filtered)", hostip, sockaddr->port); return rv; } cpe_log(CPE_INFO, "accepted connection from %s %d", hostip, sockaddr->port); /* Critical for CPE: set the socket non-blocking. */ apr_socket_opt_set(newsock, APR_SO_NONBLOCK, 1); apr_socket_timeout_set(newsock, 0); /* * Create an event for the new socket and add it to the event system. */ cpe_log(CPE_DEB, "new accepted socket %p, creating event", newsock); event = cpe_event_fdesc_create(APR_POLL_SOCKET, ctx->pc_reqevents, (apr_descriptor) newsock, 0, ctx->pc_callback, ctx->pc_ctx1); if (event == NULL) { cpe_log(CPE_ERR, "%s", "cpe_event_fdesc_create fail"); return APR_EGENERAL; } CHECK(cpe_event_add(event)); if (ctx->pc_one_shot_cb != NULL) { CHECK(ctx->pc_one_shot_cb(ctx->pc_one_shot_ctx, &event->ev_pollfd, event)); } return rv; }
static int reverseproxy_modify_connection(request_rec *r) { conn_rec *c = r->connection; reverseproxy_config_t *config = (reverseproxy_config_t *) ap_get_module_config(r->server->module_config, &reverseproxy_module); if (!config->enable_module) return DECLINED; reverseproxy_conn_t *conn; #ifdef REMOTEIP_OPTIMIZED apr_sockaddr_t temp_sa_buff; apr_sockaddr_t *temp_sa = &temp_sa_buff; #else apr_sockaddr_t *temp_sa; #endif apr_status_t rv; char *remote = (char *) apr_table_get(r->headers_in, config->header_name); char *proxy_ips = NULL; char *parse_remote; char *eos; unsigned char *addrbyte; void *internal = NULL; apr_pool_userdata_get((void*)&conn, "mod_reverseproxy-conn", c->pool); if (conn) { if (remote && (strcmp(remote, conn->prior_remote) == 0)) { /* TODO: Recycle r-> overrides from previous request */ goto ditto_request_rec; } else { /* TODO: Revert connection from previous request */ #if AP_MODULE_MAGIC_AT_LEAST(20111130,0) c->client_addr = conn->orig_addr; c->client_ip = (char *) conn->orig_ip; #else c->remote_addr = conn->orig_addr; c->remote_ip = (char *) conn->orig_ip; #endif } } remote = apr_pstrdup(r->pool, remote); #if AP_MODULE_MAGIC_AT_LEAST(20111130,0) #ifdef REMOTEIP_OPTIMIZED memcpy(temp_sa, c->client_addr, sizeof(*temp_sa)); temp_sa->pool = r->pool; #else temp_sa = c->client_addr; #endif #else #ifdef REMOTEIP_OPTIMIZED memcpy(temp_sa, c->remote_addr, sizeof(*temp_sa)); temp_sa->pool = r->pool; #else temp_sa = c->remote_addr; #endif #endif while (remote) { /* verify c->client_addr is trusted if there is a trusted proxy list */ if (config->proxymatch_ip) { int i; reverseproxy_proxymatch_t *match; match = (reverseproxy_proxymatch_t *)config->proxymatch_ip->elts; for (i = 0; i < config->proxymatch_ip->nelts; ++i) { #if AP_MODULE_MAGIC_AT_LEAST(20111130,0) if (apr_ipsubnet_test(match[i].ip, c->client_addr)) { internal = match[i].internal; break; } #else if (apr_ipsubnet_test(match[i].ip, c->remote_addr)) { internal = match[i].internal; break; } #endif } } if ((parse_remote = strrchr(remote, ',')) == NULL) { parse_remote = remote; remote = NULL; } else { *(parse_remote++) = '\0'; } while (*parse_remote == ' ') ++parse_remote; eos = parse_remote + strlen(parse_remote) - 1; while (eos >= parse_remote && *eos == ' ') *(eos--) = '\0'; if (eos < parse_remote) { if (remote) *(remote + strlen(remote)) = ','; else remote = parse_remote; break; } #ifdef REMOTEIP_OPTIMIZED /* Decode client_addr - sucks; apr_sockaddr_vars_set isn't 'public' */ if (inet_pton(AF_INET, parse_remote, &temp_sa->sa.sin.sin_addr) > 0) { apr_sockaddr_vars_set(temp_sa, APR_INET, temp_sa.port); } #if APR_HAVE_IPV6 else if (inet_pton(AF_INET6, parse_remote, &temp_sa->sa.sin6.sin6_addr) > 0) { apr_sockaddr_vars_set(temp_sa, APR_INET6, temp_sa.port); } #endif else { rv = apr_get_netos_error(); #else /* !REMOTEIP_OPTIMIZED */ /* We map as IPv4 rather than IPv6 for equivilant host names * or IPV4OVERIPV6 */ rv = apr_sockaddr_info_get(&temp_sa, parse_remote, APR_UNSPEC, temp_sa->port, APR_IPV4_ADDR_OK, r->pool); if (rv != APR_SUCCESS) { #endif ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rv, r, "RemoteIP: Header %s value of %s cannot be parsed " "as a client IP", config->header_name, parse_remote); if (remote) *(remote + strlen(remote)) = ','; else remote = parse_remote; break; } addrbyte = (unsigned char *) &temp_sa->sa.sin.sin_addr; /* For intranet (Internal proxies) ignore all restrictions below */ if (!internal && ((temp_sa->family == APR_INET /* For internet (non-Internal proxies) deny all * RFC3330 designated local/private subnets: * 10.0.0.0/8 169.254.0.0/16 192.168.0.0/16 * 127.0.0.0/8 172.16.0.0/12 */ && (addrbyte[0] == 10 || addrbyte[0] == 127 || (addrbyte[0] == 169 && addrbyte[1] == 254) || (addrbyte[0] == 172 && (addrbyte[1] & 0xf0) == 16) || (addrbyte[0] == 192 && addrbyte[1] == 168))) #if APR_HAVE_IPV6 || (temp_sa->family == APR_INET6 /* For internet (non-Internal proxies) we translated * IPv4-over-IPv6-mapped addresses as IPv4, above. * Accept only Global Unicast 2000::/3 defined by RFC4291 */ && ((temp_sa->sa.sin6.sin6_addr.s6_addr[0] & 0xe0) != 0x20)) #endif )) { ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rv, r, "RemoteIP: Header %s value of %s appears to be " "a private IP or nonsensical. Ignored", config->header_name, parse_remote); if (remote) *(remote + strlen(remote)) = ','; else remote = parse_remote; break; } #if AP_MODULE_MAGIC_AT_LEAST(20111130,0) if (!conn) { conn = (reverseproxy_conn_t *) apr_palloc(c->pool, sizeof(*conn)); apr_pool_userdata_set(conn, "mod_reverseproxy-conn", NULL, c->pool); conn->orig_addr = c->client_addr; conn->orig_ip = c->client_ip; } /* Set remote_ip string */ if (!internal) { if (proxy_ips) proxy_ips = apr_pstrcat(r->pool, proxy_ips, ", ", c->client_ip, NULL); else proxy_ips = c->client_ip; } c->client_addr = temp_sa; apr_sockaddr_ip_get(&c->client_ip, c->client_addr); } /* Nothing happened? */ if (!conn || (c->client_addr == conn->orig_addr)) return OK; /* Fixups here, remote becomes the new Via header value, etc * In the heavy operations above we used request scope, to limit * conn pool memory growth on keepalives, so here we must scope * the final results to the connection pool lifetime. * To limit memory growth, we keep recycling the same buffer * for the final apr_sockaddr_t in the remoteip conn rec. */ c->client_ip = apr_pstrdup(c->pool, c->client_ip); conn->proxied_ip = c->client_ip; r->useragent_ip = c->client_ip; r->useragent_addr = c->client_addr; memcpy(&conn->proxied_addr, temp_sa, sizeof(*temp_sa)); conn->proxied_addr.pool = c->pool; c->client_addr = &conn->proxied_addr; #else if (!conn) { conn = (reverseproxy_conn_t *) apr_palloc(c->pool, sizeof(*conn)); apr_pool_userdata_set(conn, "mod_reverseproxy-conn", NULL, c->pool); conn->orig_addr = c->remote_addr; conn->orig_ip = c->remote_ip; } /* Set remote_ip string */ if (!internal) { if (proxy_ips) proxy_ips = apr_pstrcat(r->pool, proxy_ips, ", ", c->remote_ip, NULL); else proxy_ips = c->remote_ip; } c->remote_addr = temp_sa; apr_sockaddr_ip_get(&c->remote_ip, c->remote_addr); } /* Nothing happened? */ if (!conn || (c->remote_addr == conn->orig_addr)) return OK; /* Fixups here, remote becomes the new Via header value, etc * In the heavy operations above we used request scope, to limit * conn pool memory growth on keepalives, so here we must scope * the final results to the connection pool lifetime. * To limit memory growth, we keep recycling the same buffer * for the final apr_sockaddr_t in the remoteip conn rec. */ c->remote_ip = apr_pstrdup(c->pool, c->remote_ip); conn->proxied_ip = c->remote_ip; memcpy(&conn->proxied_addr, temp_sa, sizeof(*temp_sa)); conn->proxied_addr.pool = c->pool; c->remote_addr = &conn->proxied_addr; #endif if (remote) remote = apr_pstrdup(c->pool, remote); conn->proxied_remote = remote; conn->prior_remote = apr_pstrdup(c->pool, apr_table_get(r->headers_in, config->header_name)); if (proxy_ips) proxy_ips = apr_pstrdup(c->pool, proxy_ips); conn->proxy_ips = proxy_ips; /* Unset remote_host string DNS lookups */ c->remote_host = NULL; c->remote_logname = NULL; ditto_request_rec: if (conn->proxy_ips) { apr_table_setn(r->notes, "reverseproxy-proxy-ip-list", conn->proxy_ips); if (config->proxies_header_name) apr_table_setn(r->headers_in, config->proxies_header_name, conn->proxy_ips); } ap_log_rerror(APLOG_MARK, APLOG_INFO|APLOG_NOERRNO, 0, r, conn->proxy_ips ? "Using %s as client's IP by proxies %s" : "Using %s as client's IP by internal proxies", conn->proxied_ip, conn->proxy_ips); return OK; }
// virtual LLIOPipe::EStatus LLIOServerSocket::process_impl( const LLChannelDescriptors& channels, buffer_ptr_t& buffer, bool& eos, LLSD& context, LLPumpIO* pump) { LL_RECORD_BLOCK_TIME(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; }
// 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; }