Esempio n. 1
0
/* redundant buffer size should be larger than hlen */
static int get_header_from_response (request_rec *r, apr_socket_t *s,
                                     char *header, apr_size_t *p_hlen,
                                     char *redundant, apr_size_t *p_rlen)
{
    int i, crlf_loc;
    apr_status_t rv;
    apr_size_t len, hlen = 0;
    char *nheader = header;
    while (1) {
        len = *p_hlen - hlen;
        if (len == 0) {
            char buf[1];
            len = 1;
            rv = apr_socket_recv (s, buf, &len);
            if (rv == APR_EOF || len == 0) {
                ap_log_rerror (APLOG_MARK, APLOG_ERR, 0, r,
                               "fail to get compelte http head "
                               "(maybe the server is too busy)");
            }
            else {
                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
                              "the http header's size is too large");
            }
            return -1;
        }
        rv = apr_socket_recv (s, nheader, &len);
        if (strncmp (header, CRLF_STR, 2) == 0) {
            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "internal error");
            return -1;
        }
        for (i = 2; i < hlen + len; i++)
            if (strncmp (header + i, CRLF_STR CRLF_STR, 4) == 0) {
                crlf_loc = i + 2;
                break;
            }
        if (i < hlen + len)
            break;
        if (rv == APR_EOF || len == 0) {
            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
                          "fail to get compelete http head"
                          " (maybe the server is too busy)");
            return -1;
        }
        hlen += len;
        nheader += len;
    }
    if (hlen + len - crlf_loc - 2 > *p_rlen) {
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "internal error");
#ifdef DEBUG
        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
                      "the bufsize of redundant is too small");
#endif
        return -1;
    }
    for (i = crlf_loc + 2, *p_rlen = 0; i < hlen + len; i++)
        redundant[(*p_rlen)++] = header[i];
    *p_hlen = crlf_loc;
    return 0;
}
Esempio n. 2
0
static apr_status_t talkTalk(apr_socket_t *socket, apr_pool_t *parent)
{
    apr_pool_t *pool;
    apr_size_t len;
    char *buf;
    apr_status_t rv;

    if (apr_pool_create(&pool, parent) != APR_SUCCESS)
        return APR_ENOPOOL;


    buf = apr_palloc(pool, BUF_SIZE);
    if (!buf)
        return ENOMEM;

    do {
        len = BUF_SIZE;
        rv = apr_socket_recv(socket, buf, &len);
        if (APR_STATUS_IS_EOF(rv) || len == 0 || rv != APR_SUCCESS)
            break;
        rv = apr_socket_send(socket, buf, &len);
        if (len == 0 || rv != APR_SUCCESS)
            break;
    } while (rv == APR_SUCCESS);

    apr_pool_clear(pool);
    return APR_SUCCESS;
}
Esempio n. 3
0
/** Match against builtin wake up descriptor in a pollset */
APT_DECLARE(apt_bool_t) apt_pollset_is_wakeup(apt_pollset_t *pollset, const apr_pollfd_t *descriptor)
{
	apt_bool_t status = FALSE;
#ifdef WIN32
	if(descriptor->desc.s == pollset->wakeup_pipe[0]) {
		char rb[512];
		apr_size_t nr = sizeof(rb);

		/* simply read out from the input side of the pipe all the data. */
		while(apr_socket_recv(pollset->wakeup_pipe[0], rb, &nr) == APR_SUCCESS) {
			if(nr != sizeof(rb)) {
				break;
			}
		}
		status = TRUE;
	}
#else
	if(descriptor->desc.f == pollset->wakeup_pipe[0]) {
		char rb[512];
		apr_size_t nr = sizeof(rb);

		/* simply read out from the input side of the pipe all the data. */
		while(apr_file_read(pollset->wakeup_pipe[0], rb, &nr) == APR_SUCCESS) {
			if(nr != sizeof(rb)) {
				break;
			}
		}
		status = TRUE;
	}
#endif
	return status;
}
Esempio n. 4
0
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;
}
Esempio n. 5
0
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 apt_bool_t mrcp_server_agent_messsage_receive(mrcp_connection_agent_t *agent, mrcp_connection_t *connection)
{
	apr_status_t status;
	apr_size_t offset;
	apr_size_t length;
	apt_text_stream_t *stream;

	if(!connection || !connection->sock) {
		return FALSE;
	}
	stream = &connection->rx_stream;
	
	/* init length of the stream */
	stream->text.length = sizeof(connection->rx_buffer)-1;
	/* calculate offset remaining from the previous receive / if any */
	offset = stream->pos - stream->text.buf;
	/* calculate available length */
	length = stream->text.length - offset;
	status = apr_socket_recv(connection->sock,stream->pos,&length);
	if(status == APR_EOF || length == 0) {
		return mrcp_server_agent_connection_close(agent,connection);
	}
	/* calculate actual length of the stream */
	stream->text.length = offset + length;
	stream->pos[length] = '\0';
	apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Receive MRCPv2 Stream %s [%lu bytes]\n%s",
		connection->id,
		length,
		stream->pos);

	/* reset pos */
	stream->pos = stream->text.buf;
	/* walk through the stream parsing RTSP messages */
	return mrcp_stream_walk(connection->parser,stream,mrcp_server_message_handler,connection);
}
Esempio n. 7
0
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 apt_bool_t mrcp_client_agent_control_pocess(mrcp_connection_agent_t *agent)
{
	connection_task_msg_data_t task_msg_data;
	apr_size_t size = sizeof(connection_task_msg_data_t);
	apr_status_t status = apr_socket_recv(agent->control_sock, (char*)&task_msg_data, &size);
	if(status == APR_EOF || size == 0) {
		return FALSE;
	}

	switch(task_msg_data.type) {
		case CONNECTION_TASK_MSG_ADD_CHANNEL:
			mrcp_client_agent_channel_add(agent,task_msg_data.channel,task_msg_data.descriptor);
			break;
		case CONNECTION_TASK_MSG_MODIFY_CHANNEL:
			mrcp_client_agent_channel_modify(agent,task_msg_data.channel,task_msg_data.descriptor);
			break;
		case CONNECTION_TASK_MSG_REMOVE_CHANNEL:
			mrcp_client_agent_channel_remove(agent,task_msg_data.channel);
			break;
		case CONNECTION_TASK_MSG_SEND_MESSAGE:
			mrcp_client_agent_messsage_send(agent,task_msg_data.channel,task_msg_data.message);
			break;
		case CONNECTION_TASK_MSG_TERMINATE:
			return FALSE;
	}

	return TRUE;
}
Esempio n. 9
0
static int
thrasher_recv_v3_pkt(thrasher_pkt_t * pkt, apr_socket_t * sock)
{
    uint8_t         allowed;
    uint32_t        ident;
    apr_size_t      torecv;
    char            recv_data[5];


    /*
     * recv 4 byte ident along with the boolean on whether
     * the address is allowed or not 
     */
    torecv = 5;
    if (apr_socket_recv(sock, recv_data, &torecv) != APR_SUCCESS)
        return -1;


    memcpy(&ident, recv_data, sizeof(uint32_t));
    allowed = (uint8_t) recv_data[4];

    if (pkt->ident != ntohl(ident))
        /*
         * our identifiers did not match, we must
         * return an error, something very odd 
         * happened here 
         */
        return -1;

    if (allowed > 1)
        return -1;

    return allowed;
}
Esempio n. 10
0
/* Implements svn_read_fn_t */
static svn_error_t *
sock_read_cb(void *baton, char *buffer, apr_size_t *len)
{
  sock_baton_t *b = baton;
  apr_status_t status;
  apr_interval_time_t interval;

  status = apr_socket_timeout_get(b->sock, &interval);
  if (status)
    return svn_error_wrap_apr(status, _("Can't get socket timeout"));

  /* Always block on read.
   * During pipelining, we set the timeout to 0 for some write
   * operations so that we can try them without blocking. If APR had
   * separate timeouts for read and write, we would only set the
   * write timeout, but it doesn't. So here, we revert back to blocking.
   */
  apr_socket_timeout_set(b->sock, -1);
  status = apr_socket_recv(b->sock, buffer, len);
  apr_socket_timeout_set(b->sock, interval);

  if (status && !APR_STATUS_IS_EOF(status))
    return svn_error_wrap_apr(status, _("Can't read from connection"));
  return SVN_NO_ERROR;
}
Esempio n. 11
0
File: buf.c Progetto: Abioy/mpaxos
apr_status_t buf_from_sock(buf_t *buf, apr_socket_t *sock) {
    apr_status_t status = APR_SUCCESS;

    size_t sum = 0;
    while (true) {
	buf_readjust(buf, 1);

	size_t empty_tail = buf->sz - buf->idx_write;
	size_t n = empty_tail;

	status = apr_socket_recv(sock, buf->raw + buf->idx_write, &n);

	SAFE_ASSERT(n >= 0);
	//SAFE_ASSERT(status == APR_SUCCESS);

	buf->idx_write += n;
	sum += n;

	if (status != APR_SUCCESS) {
	    // nothing more to read.
	    break;
	}
    }
    //return sum;
    return status;
}
Esempio n. 12
0
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);
}
Esempio n. 13
0
/* Receive MRCP message through TCP/MRCPv2 connection */
static apt_bool_t mrcp_client_poller_signal_process(void *obj, const apr_pollfd_t *descriptor)
{
	mrcp_connection_agent_t *agent = obj;
	mrcp_connection_t *connection = descriptor->client_data;
	apr_status_t status;
	apr_size_t offset;
	apr_size_t length;
	apt_text_stream_t *stream;
	mrcp_message_t *message;
	apt_message_status_e msg_status;

	if(!connection || !connection->sock) {
		return FALSE;
	}
	stream = &connection->rx_stream;

	/* calculate offset remaining from the previous receive / if any */
	offset = stream->pos - stream->text.buf;
	/* calculate available length */
	length = connection->rx_buffer_size - offset;

	status = apr_socket_recv(connection->sock,stream->pos,&length);
	if(status == APR_EOF || length == 0) {
		apt_pollset_t *pollset = apt_poller_task_pollset_get(agent->task);
		apt_log(APT_LOG_MARK,APT_PRIO_INFO,"TCP/MRCPv2 Peer Disconnected %s",connection->id);
		if(pollset) {
			apt_pollset_remove(pollset,&connection->sock_pfd);
		}
		apr_socket_close(connection->sock);
		connection->sock = NULL;

		mrcp_client_agent_disconnect_raise(agent,connection);
		return TRUE;
	}
	
	/* calculate actual length of the stream */
	stream->text.length = offset + length;
	stream->pos[length] = '\0';
	apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Receive MRCPv2 Stream %s [%"APR_SIZE_T_FMT" bytes]\n%.*s",
			connection->id,
			length,
			connection->verbose == TRUE ? length : 0,
			stream->pos);

	/* reset pos */
	apt_text_stream_reset(stream);

	do {
		msg_status = mrcp_parser_run(connection->parser,stream,&message);
		if(mrcp_client_message_handler(connection,message,msg_status) == FALSE) {
			return FALSE;
		}
	}
	while(apt_text_is_eos(stream) == FALSE);

	/* scroll remaining stream */
	apt_text_stream_scroll(stream);
	return TRUE;
}
Esempio n. 14
0
apr_status_t read_line(apr_pool_t *pool, socket_thread_data_t *td,
		char **buf, apr_size_t bufsize)
{
	char errbuf[ERRBUFLEN + 1];
	apr_status_t res;
	char *_buf;
	apr_size_t readsize = bufsize, i;
	int ret;

	if((*buf = apr_pcalloc(pool, bufsize))==NULL)
	{
		syslog(LOG_ERR, "read_line: apr_pcalloc failed");
		apr_pool_destroy(pool);
		return;
	}
	_buf = *buf;

	if(td->tls_session == NULL)
	{
		res = apr_socket_recv(td->socket, _buf, &readsize);

		if(res == APR_SUCCESS || res == APR_EOF)
		{
			_buf[readsize] = 0;
			for(i = 0; i < readsize; i++)
				if(_buf[i] == '\r' || _buf[i] == '\n')
					_buf[i] = 0;
		}
		else
			syslog(LOG_ERR, "unexpected result while reading socket: %s",
				apr_strerror(res, errbuf, ERRBUFLEN));
	}
	else
	{
		ret = gnutls_record_recv (*(td->tls_session), _buf, bufsize);
		if (ret == 0)
		{
			// Connection has been closed by peer
			_buf[0] = 0;
			res = APR_EOF;
		}
		else if (ret < 0)
		{
			syslog(LOG_ERR, "received corrupted "
					"data(GNUTLS error code = %d). Closing the connection", ret);
			res = APR_EGENERAL;
		}
		else if (ret > 0)
		{
			_buf[ret] = 0;
			for(i = 0; i < ret; i++)
				if(_buf[i] == '\r' || _buf[i] == '\n')
					_buf[i] = 0;
			res = APR_SUCCESS;
		}
	}

	return res;
}
Esempio n. 15
0
/* expect_len is the expect len */
static int get_body_from_response (request_rec *r,
                                   apr_socket_t *s,
                                   apr_size_t expect_len,
                                   char *redundant,
                                   apr_size_t rlen,
                                   char *body,
                                   apr_size_t *p_blen)
{
    int i;
    apr_size_t len;
    apr_status_t rv;
    if (rlen > expect_len) {
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
                      "fail to get expect size file content");
        return -1;
    }
    *p_blen = rlen;
    for (i = 0; i < rlen; i++)
        body[i] = redundant[i];
    body += rlen;
    while (1) {
        len = expect_len - *p_blen;
        if (len == 0) {
            char buf[1];
            len = 1;
            rv = apr_socket_recv (s, buf, &len);
            if (rv != APR_EOF) {
                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
                              "fail to get expect size file content");
                return -1;
            }
            break;
        }
        rv = apr_socket_recv (s, body, &len);
        *p_blen += len;
        body += len;
        if (rv == APR_EOF || len == 0)
            break;
    }
    if (*p_blen != expect_len) {
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
                      "fail to get expect size file content");
        return -1;
    }
    return 0;
}
Esempio n. 16
0
static apr_status_t socket_bucket_read(apr_bucket *a, const char **str,
                                       apr_size_t *len, apr_read_type_e block)
{
    apr_socket_t *p = a->data;
    char *buf;
    apr_status_t rv;
    apr_interval_time_t timeout;

    if (block == APR_NONBLOCK_READ) {
        apr_socket_timeout_get(p, &timeout);
        apr_socket_timeout_set(p, 0);
    }

    *str = NULL;
    *len = APR_BUCKET_BUFF_SIZE;
    buf = apr_bucket_alloc(*len, a->list); /* XXX: check for failure? */

    rv = apr_socket_recv(p, buf, len);

    if (block == APR_NONBLOCK_READ) {
        apr_socket_timeout_set(p, timeout);
    }

    if (rv != APR_SUCCESS && rv != APR_EOF) {
        apr_bucket_free(buf);
        return rv;
    }
    /*
     * If there's more to read we have to keep the rest of the socket
     * for later. XXX: Note that more complicated bucket types that
     * refer to data not in memory and must therefore have a read()
     * function similar to this one should be wary of copying this
     * code because if they have a destroy function they probably
     * want to migrate the bucket's subordinate structure from the
     * old bucket to a raw new one and adjust it as appropriate,
     * rather than destroying the old one and creating a completely
     * new bucket.
     *
     * Even if there is nothing more to read, don't close the socket here
     * as we have to use it to send any response :)  We could shut it 
     * down for reading, but there is no benefit to doing so.
     */
    if (*len > 0) {
        apr_bucket_heap *h;
        /* Change the current bucket to refer to what we read */
        a = apr_bucket_heap_make(a, buf, *len, apr_bucket_free);
        h = a->data;
        h->alloc_len = APR_BUCKET_BUFF_SIZE; /* note the real buffer size */
        *str = buf;
        APR_BUCKET_INSERT_AFTER(a, apr_bucket_socket_create(p, a->list));
    }
    else {
        apr_bucket_free(buf);
        a = apr_bucket_immortal_make(a, "", 0);
        *str = a->data;
    }
    return APR_SUCCESS;
}
Esempio n. 17
0
/* Receive RTSP message through RTSP connection */
static apt_bool_t rtsp_server_message_receive(apt_net_server_task_t *task, apt_net_server_connection_t *connection)
{
	rtsp_server_t *server = apt_net_server_task_object_get(task);
	char buffer[RTSP_MESSAGE_MAX_SIZE];
	apt_bool_t more_messages_on_buffer = FALSE;
	apr_status_t status;
	apt_text_stream_t text_stream;
	rtsp_message_t *message;

	if(!connection || !connection->sock) {
		return FALSE;
	}
	
	text_stream.text.buf = buffer;
	text_stream.text.length = sizeof(buffer)-1;
	status = apr_socket_recv(connection->sock, text_stream.text.buf, &text_stream.text.length);
	if(status == APR_EOF || text_stream.text.length == 0) {
		return apt_net_server_connection_close(task,connection);
	}
	text_stream.text.buf[text_stream.text.length] = '\0';
	text_stream.pos = text_stream.text.buf;

	apt_log(APT_PRIO_INFO,"Receive RTSP Message size=%lu\n%s",text_stream.text.length,text_stream.text.buf);
	do {
		message = rtsp_message_create(RTSP_MESSAGE_TYPE_UNKNOWN,connection->pool);
		if(rtsp_message_parse(message,&text_stream) == TRUE) {
			apt_str_t *destination = &message->header.transport.destination;
			if(!destination->buf && connection->client_ip) {
				apt_string_assign(destination,connection->client_ip,connection->pool);
			}
			rtsp_server_session_request_process(server,connection->obj,message);
		}
		else {
			rtsp_message_t *response;
			apt_log(APT_PRIO_WARNING,"Failed to Parse RTSP Message");
			response = rtsp_response_create(message,RTSP_STATUS_CODE_BAD_REQUEST,
									RTSP_REASON_PHRASE_BAD_REQUEST,message->pool);
			if(rtsp_server_message_send(server,connection,response) == FALSE) {
				apt_log(APT_PRIO_WARNING,"Failed to Send RTSP Response");
			}
		}

		more_messages_on_buffer = FALSE;
		if(text_stream.text.length > (apr_size_t)(text_stream.pos - text_stream.text.buf)) {
			/* there are more RTSP messages to signal */
			more_messages_on_buffer = TRUE;
			text_stream.text.length -= text_stream.pos - text_stream.text.buf;
			text_stream.text.buf = text_stream.pos;
			apt_log(APT_PRIO_DEBUG,"Saving Remaining Buffer for Next Message");
		}
	}
	while(more_messages_on_buffer);

	return TRUE;
}
Esempio n. 18
0
apr_status_t buffer_socket_recv(buffer_t *self, apr_socket_t *sock)
{
	apr_status_t rs;
	apr_size_t len = buffer_available(self);
	char *space = self->node->first_avail;
	rs = apr_socket_recv(sock, space, &len);
	if (rs != 0)
		return rs;
	self->node->first_avail += (int)len;
	return 0;
}
Esempio n. 19
0
/* Wrapper for apr_socket_recv that handles updating the worker stats. */
static apr_status_t get_data(proxy_conn_rec *conn,
                             char *buffer,
                             apr_size_t *buflen)
{
    apr_status_t rv = apr_socket_recv(conn->sock, buffer, buflen);

    if (rv == APR_SUCCESS) {
        conn->worker->s->read += *buflen;
    }

    return rv;
}
Esempio n. 20
0
static void looping_read(mongo_connection * conn, void* buf, int len){
    apr_status_t rv;
    char* cbuf = buf;
    while (len){
        apr_size_t sent=len; 
        rv = apr_socket_recv( conn->socket, cbuf, &sent );
//        int sent = recv(conn->sock, cbuf, len, 0);
        if (sent == 0 || sent == -1|| rv != APR_SUCCESS) MONGO_THROW(MONGO_EXCEPT_NETWORK);
        cbuf += sent;
        len -= sent;
    }
}
Esempio n. 21
0
/* Receive RTSP message through RTSP connection */
static apt_bool_t rtsp_server_poller_signal_process(void *obj, const apr_pollfd_t *descriptor)
{
	rtsp_server_t *server = obj;
	rtsp_server_connection_t *rtsp_connection = descriptor->client_data;
	apr_status_t status;
	apr_size_t offset;
	apr_size_t length;
	apt_text_stream_t *stream;
	rtsp_message_t *message;
	apt_message_status_e msg_status;

	if(descriptor->desc.s == server->listen_sock) {
		apt_log(RTSP_LOG_MARK,APT_PRIO_DEBUG,"Accept Connection");
		return rtsp_server_connection_accept(server);
	}
	
	if(!rtsp_connection || !rtsp_connection->sock) {
		return FALSE;
	}
	stream = &rtsp_connection->rx_stream;

	/* calculate offset remaining from the previous receive / if any */
	offset = stream->pos - stream->text.buf;
	/* calculate available length */
	length = sizeof(rtsp_connection->rx_buffer) - 1 - offset;

	status = apr_socket_recv(rtsp_connection->sock,stream->pos,&length);
	if(status == APR_EOF || length == 0) {
		apt_log(RTSP_LOG_MARK,APT_PRIO_INFO,"RTSP Peer Disconnected %s",rtsp_connection->id);
		return rtsp_server_connection_close(server,rtsp_connection);
	}

	/* calculate actual length of the stream */
	stream->text.length = offset + length;
	stream->pos[length] = '\0';
	apt_log(RTSP_LOG_MARK,APT_PRIO_INFO,"Receive RTSP Data %s [%"APR_SIZE_T_FMT" bytes]\n%s",
		rtsp_connection->id,
		length,
		stream->pos);

	/* reset pos */
	apt_text_stream_reset(stream);

	do {
		msg_status = rtsp_parser_run(rtsp_connection->parser,stream,&message);
		rtsp_server_message_handler(rtsp_connection,message,msg_status);
	}
	while(apt_text_is_eos(stream) == FALSE);

	/* scroll remaining stream */
	apt_text_stream_scroll(stream);
	return TRUE;
}
Esempio n. 22
0
SWITCH_DECLARE(switch_status_t) switch_socket_recv(switch_socket_t *sock, char *buf, switch_size_t *len)
{
	switch_status_t r;

	r = apr_socket_recv(sock, buf, len);

	if (r == 35 || r == 730035) {
		r = SWITCH_STATUS_BREAK;
	}

	return r;
}
Esempio n. 23
0
static apt_bool_t rtp_rx_process(mpf_rtp_stream_t *rtp_stream)
{
	char buffer[MAX_RTP_PACKET_SIZE];
	apr_size_t size = sizeof(buffer);
	apr_size_t max_count = 5;
	while(max_count && apr_socket_recv(rtp_stream->rtp_socket,buffer,&size) == APR_SUCCESS) {
		rtp_rx_packet_receive(rtp_stream,buffer,size);

		size = sizeof(buffer);
		max_count--;
	}
	return TRUE;
}
Esempio n. 24
0
static apr_status_t socket_reader(void *baton, apr_size_t bufsize,
                                  char *buf, apr_size_t *len)
{
    socket_context_t *ctx = baton;
    apr_status_t status;

    *len = bufsize;
    status = apr_socket_recv(ctx->skt, buf, len);

    if (ctx->progress_func)
        ctx->progress_func(ctx->progress_baton, *len, 0);

    return status;
}
static apr_status_t bucket_socket_ex_read(apr_bucket *a, const char **str,
                                          apr_size_t *len,
                                          apr_read_type_e block)
{
    socket_ex_data *data = a->data;
    apr_socket_t *p = data->sock;
    char *buf;
    apr_status_t rv;
    apr_interval_time_t timeout;

    if (block == APR_NONBLOCK_READ) {
        apr_socket_timeout_get(p, &timeout);
        apr_socket_timeout_set(p, 0);
    }

    *str = NULL;
    *len = APR_BUCKET_BUFF_SIZE;
    buf = apr_bucket_alloc(*len, a->list);

    rv = apr_socket_recv(p, buf, len);

    if (block == APR_NONBLOCK_READ) {
        apr_socket_timeout_set(p, timeout);
    }

    if (rv != APR_SUCCESS && rv != APR_EOF) {
        apr_bucket_free(buf);
        return rv;
    }

    if (*len > 0) {
        apr_bucket_heap *h;

        /* count for stats */
        *data->counter += *len;

        /* Change the current bucket to refer to what we read */
        a = apr_bucket_heap_make(a, buf, *len, apr_bucket_free);
        h = a->data;
        h->alloc_len = APR_BUCKET_BUFF_SIZE; /* note the real buffer size */
        *str = buf;
        APR_BUCKET_INSERT_AFTER(a, bucket_socket_ex_create(data, a->list));
    }
    else {
        apr_bucket_free(buf);
        a = apr_bucket_immortal_make(a, "", 0);
        *str = a->data;
    }
    return APR_SUCCESS;
}
Esempio n. 26
0
/**
 * read the request (and ignore it)
 * send the callback data back..
 */
static apr_status_t basic_response_test(apr_socket_t *sock, apr_pool_t *mp, struct _server_thread_data *server_data) {
	char buf[BUFSIZE];
	apr_size_t len = sizeof(buf) - 1;/* -1 for a null-terminated */
	apr_status_t rv = apr_socket_recv(sock, buf, &len);
	if (rv == APR_EOF || len == 0) {
		if(len==0) {
			printf("Server connection stopped with rv=%d\n", rv);
		}
		return APR_SUCCESS;
	}
	char * response_data = (char *)server_data->callback_data;
	apr_size_t length = strlen(response_data);
	apr_socket_send(sock, response_data, &length);
	return APR_SUCCESS;
}
Esempio n. 27
0
static int
thrasher_recv_boolean(thrasher_pkt_t * pkt, apr_socket_t * sock)
{
    uint8_t         resp;
    apr_size_t      torecv;

    torecv = 1;

    if (apr_socket_recv(sock, (char *) &resp, &torecv) != APR_SUCCESS)
        return -1;

    if (resp > 1)
        return -1;

    return (int) resp;
}
Esempio n. 28
0
/** Socket-specific read. */
static ssize_t kdsock_comm_read(kdcomm *c, void *buf, ssize_t buf_s) {
    kdsock_comm *self = (kdsock_comm *)c->obj;
    ssize_t bytes_read = 0;
    size_t recv_size;
    apr_pool_t *read_pool;    
    apr_status_t recv_err;
    enum comm_state s;
  
    apr_pool_create(&read_pool, self->pool);
       
    do {
        s = kdsock_comm_wait_read(c);

        if (s == COMM_READY) {
            recv_size = buf_s - bytes_read;
            recv_err = apr_socket_recv(self->apr_sock, buf + bytes_read, &recv_size);

            /* Read error. */
            if (recv_err != APR_SUCCESS || recv_err != APR_SUCCESS) {
                s = COMM_ERR;
                break;
            }

            /* Successful read. */
            else if (recv_size > 0) 
                bytes_read += recv_size;
        }

        /* No error, let the caller examine the state. */
        else if (s == COMM_HUP || s == COMM_EINTR) 
            break;

        /* Error. */
        else if (s ==  COMM_ERR) 
            break;
    } while (0);

    if (s == COMM_ERR) {
        self->c->state = COMM_ERR;
        KERROR_SET(_comm_, 0, "read error");
        bytes_read = -1;
    }

    apr_pool_destroy(read_pool);
    
    return bytes_read;
}
Esempio n. 29
0
/* Receive MRCP message through TCP/MRCPv2 connection */
static apt_bool_t mrcp_server_poller_signal_process(void *obj, const apr_pollfd_t *descriptor)
{
	mrcp_connection_agent_t *agent = obj;
	mrcp_connection_t *connection = descriptor->client_data;
	apr_status_t status;
	apr_size_t offset;
	apr_size_t length;
	apt_text_stream_t *stream;

	if(descriptor->desc.s == agent->listen_sock) {
		apt_log(APT_LOG_MARK,APT_PRIO_DEBUG,"Accept Connection");
		return mrcp_server_agent_connection_accept(agent);
	}
	
	if(!connection || !connection->sock) {
		return FALSE;
	}
	stream = &connection->rx_stream;

	/* init length of the buffer to read */
	offset = 0;
	length = connection->rx_buffer_size;
	if(stream->pos > stream->text.buf) {
		/* calculate offset remaining from the previous receive / if any */
		offset = stream->pos - stream->text.buf;
		/* calculate available length */
		length -= offset;
	}

	status = apr_socket_recv(connection->sock,stream->pos,&length);
	if(status == APR_EOF || length == 0) {
		return mrcp_server_agent_connection_close(agent,connection);
	}
	/* calculate actual length of the stream */
	stream->text.length = offset + length;
	stream->pos[length] = '\0';
	apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Receive MRCPv2 Stream %s [%lu bytes]\n%s",
		connection->id,
		length,
		stream->pos);

	/* reset pos */
	apt_text_stream_reset(stream);
	/* walk through the stream parsing MRCP messages */
	return mrcp_stream_walk(connection->parser,stream,mrcp_server_message_handler,connection);
}
Esempio n. 30
0
static int mrecv(request_rec *r, char *msg, int *len)
{
    apr_status_t rv;
    apr_socket_t *s;

    if ((s = mconnect(r)) == NULL)
        return HTTP_INTERNAL_SERVER_ERROR;

    if ((rv = apr_socket_recv(s, msg, len)) != APR_SUCCESS) {
        ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, "recv failed");
        mclose(r);

        return HTTP_INTERNAL_SERVER_ERROR;
    }

    return OK;
}