Example #1
0
int
handleSSLClientRead(SSL *ssl){
    char buf[4096];
    memset((void *)&buf, '\0', sizeof(buf));

    int nbytes = 0;
    fprintf(stderr, "\n Handling SSL client read");

    nbytes = SSL_read(ssl, buf, sizeof(buf));

    //connection closed
    if (nbytes == 0) {
        return -1;
    }

    //error 
    if (nbytes < 0) {
        int ret = nbytes;
        int err = SSL_get_error(ssl, ret);
        fprintf(stderr, "Error while reading from ssl connection %d\n", err);
        return -1;
    }

    while(nbytes > 0) {
        fprintf(stderr, "\n Number of bytes received on ssl socket: %d", nbytes);
        nbytes = SSL_read(ssl, buf, sizeof(buf));
    }

    fprintf(stderr, "\n Received Nbytes: %d", nbytes);

    decode_request(buf, nbytes);
    char *msg = "Received Message";
    SSL_write(ssl, msg, strlen(msg));
    return 0;
}
Example #2
0
static int
proto(glite_renewal_core_context ctx, int sock)
{
    char  *buf = NULL;
    size_t  buf_len;
    int  ret;
    edg_wlpr_Response  response;
    edg_wlpr_Request  request;
    command_table  *command;
    struct timeval timeout;

    memset(&request, 0, sizeof(request));
    memset(&response, 0, sizeof(response));

    timeout.tv_sec = (long) default_timeout;
    timeout.tv_usec = (long) ((default_timeout - timeout.tv_sec) * 1e6);

    ret = edg_wlpr_Read(sock, &timeout, &buf, &buf_len);
    if (ret) {
        edg_wlpr_Log(ctx, LOG_ERR, "Error reading from client: %s",
                     edg_wlpr_GetErrorText(ret));
        return ret;
    }

    ret = decode_request(ctx, buf, buf_len, &request);
    free(buf);
    if (ret)
        goto end;

    /* XXX check request (protocol version, ...) */

    command = find_command(ctx, request.command);
    if (command == NULL) {
        ret = EDG_WLPR_ERROR_UNKNOWN_COMMAND;
        edg_wlpr_Log(ctx, LOG_ERR, "Received unknown command (%d)", request.command);
        goto end;
    }

    command->handler(ctx, &request, &response);

    ret = encode_response(ctx, &response, &buf);
    if (ret)
        goto end;

    ret = edg_wlpr_Write(sock, &timeout, buf, strlen(buf) + 1);
    free(buf);
    if (ret) {
        edg_wlpr_Log(ctx, LOG_ERR, "Error sending response of command %d to client: %s",
                     request.command,
                     edg_wlpr_GetErrorText(ret));
        goto end;
    }

end:
    edg_wlpr_CleanRequest(&request);
    edg_wlpr_CleanResponse(&response);

    return ret;
}
int tdh_socket_connection_context::decode(tdhs_packet_t* request) {
	tb_assert(decode_request!=NULL);
	int state = decode_request(request->req, *request);
	if (state) {
		easy_warn_log("TDHS: decode failed!");
		request->req.status = TDHS_DECODE_FAILED;
		if(state == ERROR_OUT_OF_IN)
			return ERROR_OUT_OF_IN;
		else
			return EASY_ERROR;
	}
	request->req.status = TDHS_DECODE_DONE;
	return EASY_OK;
}
Example #4
0
bool czsrv::parse_request(void)
{
 std::stringstream ss(request_str);

 std::string token;
	ss >> token;
	if(!token.compare("GET"))// || !token.compare("HEADER"))
	{
//	std::clog<< "[Found] " << token << std::endl;
		ss >> URI_str;

		if(0 < URI_str.length())
		{
//			std::cout << "[URL] " << URI_str << std::endl;
			decode_request(URI_str);
			return true;
		}
	}
Example #5
0
TH_HDL client_handler(void* client)
{
	client_t* cli = (client_t*)client;
	char buff[BUFF_SIZE];

	cli->running = true;
	if(cli->server->auth && !client_authenticate(cli))
			cli->running = false;
	
	while(cli->running && cli->server->running)
	{
		//readline
		int r = read_line(cli->sock, buff, BUFF_SIZE, true);
		if(r <= 0)
			break;
		request_t req;

		_log(LVL_TRACE, "Request received : %s\n", buff);

		if(decode_request(cli, &req, buff, r) < 0)
			continue;
		// Process request
		process_request(&req);

		//Send response
		encode_reply(&req, buff, BUFF_SIZE);

		_log(LVL_TRACE, "Reply sent : %s\n", buff);
		send(cli->sock, buff, strlen(buff), 0);
	}

	_log(LVL_TRACE, "Exiting client thread\n", buff);

	cli->running = false;
	shutdown(cli->sock, SHUT_WR);
	close(cli->sock);
	server_unregister_client(cli->server, cli);
	free(client);

	_log(LVL_TRACE, "Client thread exited\n", buff);
	TH_RETURN;
}
Example #6
0
int
handleClientRead(int fd) {
    char buf[4096];
    memset((void *)&buf, '\0', sizeof(buf));
    int nbytes = 0;

    fprintf(stderr, "\n Handling client read");

    while((nbytes = recv(fd, buf, sizeof(buf), 0)) > 0) {
        fprintf(stderr, "\n Number of bytes on the socket: %d", nbytes);
    }
    
    decode_request(buf, nbytes);
    char *msg = "hello";
    
    if(send(fd, msg, strlen(msg), 0) < 0) {
        return -1;
    }
    return 0;
}
Example #7
0
int main(int argc, char *argv[]) {
	int port = 1220;
	if (argc > 1) 
		port = atoi(argv[1]);

	int fd = socket(AF_INET, SOCK_STREAM, 0);
	int flags = 1;
	setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&flags, sizeof(flags));
	setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (void *)&flags, sizeof(flags));

	struct sockaddr_in addr;
	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(port);
	addr.sin_addr.s_addr = htonl(INADDR_ANY);

	if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
		perror("bind()");
		if(fd > 0) close(fd);
		exit(1);
	}

	if (listen(fd, 1024) < 0) {
		perror("listen()");
		exit(1);
	}

	RespRequest *req = create_request(BUF_SIZE);
	RespResponse *res = create_response(BUF_SIZE);
	while (1) {
		reset_request(req);
		reset_response(res);	

		socklen_t addr_len = sizeof(addr);
		int cfd = accept(fd, (struct sockaddr*)&addr, &addr_len);
		if (cfd < 0) {
			perror("accept()");
			continue;
		}
		char buf[1024];
		int rnum = read(cfd,buf,sizeof(buf));
		
		if (decode_request(req, buf, rnum) == 0 && 
			(req->state == OK || req->state == PART_OK) ){
			if (req->argc > 1 && strncmp(request_argv(req,0), "add", 3) == 0) {
				int total = 0;
				for (int i = 1; i < req->argc; i++)
					total += atoi(request_argv(req,i));
				encode_response_integer(res, total);
			}
			else {
				encode_response_status(res,0,"ERR unknown command");
			}
		}
		else {
			encode_response_status(res,0,"ERR decode command fail");
		}
		write(cfd, res->buf, res->used_size);
		close(cfd);
	}
	destroy_request(req);
	destroy_response(res);

	close(fd);
	return 0;
}