static void
__dst_cb_in(struct bufferevent *bev, void *ctx)
{
	struct evbuffer *dst_in;
	struct chunk_transfer_s *ct = (struct chunk_transfer_s *) ctx;

	bufferevent_disable(bev, EV_WRITE);
	dst_in = bufferevent_get_input(bev);
	for (;;) {
		if (ct->reply_parsing == PARSE_STATUS) {
			char *status_line;
			size_t status_line_length;
			status_line_length = 0;
			status_line = evbuffer_readln(dst_in, &status_line_length, EVBUFFER_EOL_CRLF);
			if (!status_line) {
				GRID_DEBUG("Output status line not ready, %"G_GSIZE_FORMAT" bytes remaining",
					evbuffer_get_length(dst_in));
				return;
			} else {
				GRID_DEBUG("Output status line [%s]", status_line);
				free(status_line);
				ct->reply_parsing = PARSE_HEADER;
				bufferevent_enable(bev, EV_READ);
			}
		}
		if (ct->reply_parsing == PARSE_HEADER) {
			char *header_line;
			size_t header_line_length;
			header_line_length = 0;
			header_line = evbuffer_readln(dst_in, &header_line_length, EVBUFFER_EOL_CRLF);
			if (!header_line) {
				GRID_DEBUG("Output header line not ready, %"G_GSIZE_FORMAT" bytes remaining",
						evbuffer_get_length(dst_in));
				return;
			} else {
				if (!header_line_length) {
					GRID_DEBUG("Output header end");
					ct->reply_parsing = PARSE_BODY;
				}
				else {
					GRID_DEBUG("Output header line [%s]", header_line);
					free(header_line);
					bufferevent_enable(bev, EV_READ);
				}
			}
		}
		if (ct->reply_parsing == PARSE_BODY) {
			size_t s;
			s = evbuffer_get_length(dst_in);
			if (!s)
				return;
			evbuffer_drain(dst_in, s);
			GRID_DEBUG("Output body drained of %"G_GSIZE_FORMAT" bytes", s);
			bufferevent_enable(bev, EV_READ);
		}
	}
}
void kismet_conn_readcb(struct bufferevent *bev, void* args) {
  DEBUG(255, "kismet_conn_readcb(%p, %p);", bev, args);
  struct evbuffer* input = bufferevent_get_input(bev);
  struct evbuffer* output = bufferevent_get_output(bev);
  struct server* server = (struct server*) args;
  size_t len;
  char* line = evbuffer_readln(input, &len, EVBUFFER_EOL_CRLF);
  while (line) {
    DEBUG(255, "Input: '%s'", line);
    static const char* CAPABILITY_SSCANF = "*CAPABILITY: %s %[^\n]";
    static const char* GENERAL_HEADER_SSCANF = "*%[A-Z]: %[^\n]";
    char type[BUFSIZ];
    char rest[BUFSIZ];
    if (sscanf(line, CAPABILITY_SSCANF, type, rest) == 2) {
      struct inserter* inserter = server->inserters;
      while (inserter) {
        if (strcmp(inserter->type, type) == 0)
          break;
        inserter = inserter->next;
      };
      if (inserter && !inserter->capabilities) {
        char* token = strtok(rest, ",");
        while (token) {
          unsigned int next_token = 0;
          if (inserter->capabilities) {
            while (inserter->capabilities[++next_token]);
            inserter->capabilities = realloc(inserter->capabilities, sizeof(char*) * (next_token + 2));
          } else
            inserter->capabilities = malloc(sizeof(char*) * 2);
          inserter->capabilities[next_token] = strdup(token);
          inserter->capabilities[++next_token] = NULL;
          token = strtok(NULL, ",");
        };
        static const char* ENABLE_EVENT = "!%d ENABLE %s *\n";
        evbuffer_add_printf(output, ENABLE_EVENT, inserter->ack_id, inserter->type);
      };
    } else if (sscanf(line, GENERAL_HEADER_SSCANF, type, rest) == 2) {
      struct inserter* inserter = server->inserters;
      while (inserter) {
        if (strcmp(inserter->type, type) == 0) {
          char query[1024*64]; /* Should be plenty */
          memset(query, 0, 1024*64);
          if (query_printf(query, rest, inserter)) {
            DEBUG(255, "Query: '%s'", query);
            databaseQuery(server->db, query, NULL, NULL);
          }
          break;
        }
        inserter = inserter->next;
      };
    };
    free(line);
    line = evbuffer_readln(input, &len, EVBUFFER_EOL_CRLF);
  };
};
Exemple #3
0
static void handle_read(struct bufferevent *bev, void *ctx)
{
    // Get the buffer
    struct evbuffer *buf = bufferevent_get_input(bev);

    // Get the client
    client_t *c = ctx;

    // Get the message
    char *request_line;
    size_t len;

    // Read all lines if more than 1 have arrived
    for (;;) {
        request_line = evbuffer_readln(buf, &len, EVBUFFER_EOL_CRLF_STRICT);

        // If we got a messsage, handle it
        if (request_line) {
            handle_command(request_line, c);
            free(request_line);
        } else {
            return;
        }
    }
}
Exemple #4
0
void server_event_read(struct bufferevent *bev, void *arg)
{
    client_t *client = (client_t*) arg;

    char data[4096];
    int res;
    char *line;

    memset(data, 0, 4096);


    res = evbuffer_add_buffer(client->buf, bufferevent_get_input(bev));

    while((line = evbuffer_readln(client->buf, NULL, EVBUFFER_EOL_CRLF)) != NULL)
    {
        log_debug(__FILE__, __LINE__, "Socket #%d - message - (%d) %s", client->socket, strlen(line), line);
        res = message_process(client, line);

        if(res == REQ_PROC_DISCONNECT)
        {
            log_debug(__FILE__, __LINE__, "Closing client connection. socket #%d", client->socket);
            client->server->cur_cons--;
            client_free(client);
            return;
        }
    }
    
    return;
}
Exemple #5
0
static int http_parse_first_line(struct http_connection *hc, struct evbuffer *evb)
{
	int ret = 0;
	char *line = NULL;
	size_t line_length = 0;

	line = evbuffer_readln(evb, &line_length, EVBUFFER_EOL_CRLF);
	if (line == NULL) {
		dbprintf("Could not read http first line\n");
		return -1;
	}
	if (line_length > 2048) {
		dbprintf("Http firstline too long(len > 2048)\n");
		goto failed;
	}

	ret = http_parse_request_line(hc, line);
	if (ret) {
		dbprintf("Could not parse http request line\n");
		goto failed;
	}

	free(line);

	return 0;
failed:
	if (line) {
		free(line);
	}
	return -1;
}
Exemple #6
0
/* Control input callback. Read lines and fire commands. */
void
control_callback(struct client *c, int closed, __unused void *data)
{
	char			*line, *cause;
	struct cmd_list		*cmdlist;
	struct cmd		*cmd;
	struct cmdq_item	*item;

	if (closed)
		c->flags |= CLIENT_EXIT;

	for (;;) {
		line = evbuffer_readln(c->stdin_data, NULL, EVBUFFER_EOL_LF);
		if (line == NULL)
			break;
		if (*line == '\0') { /* empty line exit */
			c->flags |= CLIENT_EXIT;
			break;
		}

		if (cmd_string_parse(line, &cmdlist, NULL, 0, &cause) != 0) {
			item = cmdq_get_callback(control_error, cause);
			cmdq_append(c, item);
		} else {
			TAILQ_FOREACH(cmd, &cmdlist->list, qentry)
				cmd->flags |= CMD_CONTROL;
			item = cmdq_get_command(cmdlist, NULL, NULL, 0);
			cmdq_append(c, item);
			cmd_list_free(cmdlist);
		}

		free(line);
	}
}
Exemple #7
0
void
on_read_cb(struct bufferevent *bev, void *arg)
{
	char			*str = NULL;
	size_t			n_read_out;
	json_error_t		error;
	json_t			*jmsg = NULL;

	while (evbuffer_get_length(bufferevent_get_input(bev)) > 0) {
		if ((str = evbuffer_readln(bufferevent_get_input(bev),
		    &n_read_out, EVBUFFER_EOL_LF)) == NULL) {
			return;
		}
	//	printf("str: %d <> %s\n\n\n", strlen(str), str);
		if ((jmsg = json_loadb(str, n_read_out, 0, &error)) == NULL) {
			jlog(L_ERROR, "json_loadb: %s", error.text);
			bufferevent_free(bufev_sock);
			return;
		}

		free(str);
		dispatch_op(jmsg);
		json_decref(jmsg);
	}
}
void TCPStream::read_cb(struct bufferevent *bev) {
	evbuffer *input, *output;
	char* line;
	size_t n;
	input = bufferevent_get_input(bev);
	output = bufferevent_get_output(bev);

	line = evbuffer_readln(input, &n, EVBUFFER_EOL_CRLF);
	if (line) {
		//evbuffer_add(output, line, n);
		//const char* nl = "\n\r";
		//evbuffer_add(output, nl, 1);
		for (int i = 0; i < n; ++i) {
			buffer += line[i];
		}

		// This is where you would send the buffer back to the server/game engine
		// for processing, but instead for now, we're just going to echo it to the console.
		if (buffer.compare("") != 0) {
			cout << "Socket " << socketfd << ": " << buffer << '\n';
			if (buffer.compare("quit") == 0) {
				error_cb(bev, BEV_EVENT_EOF);
				delete this;
			}
			else if (buffer.compare("shutdown") == 0) {
				parent->Shutdown();
			}
			else {
				buffer = "";
			}
		}
	}
}
Exemple #9
0
static void
respond_to_number(struct bufferevent *bev, void *ctx)
{
	struct evbuffer *b = bufferevent_get_input(bev);
	char *line;
	int n;
	line = evbuffer_readln(b, NULL, EVBUFFER_EOL_LF);
	if (! line)
		return;
	n = atoi(line);
	if (n <= 0)
		TT_FAIL(("Bad number: %s", line));
	TT_BLATHER(("The number was %d", n));
	if (n == 1001) {
		++test_is_done;
		bufferevent_free(bev); /* Should trigger close on other side. */
		return;
	}
	if (!strcmp(ctx, "client") && n == renegotiate_at) {
		SSL_renegotiate(bufferevent_openssl_get_ssl(bev));
	}
	++n;
	evbuffer_add_printf(bufferevent_get_output(bev),
	    "%d\n", n);
	TT_BLATHER(("Done reading; now writing."));
	bufferevent_enable(bev, EV_WRITE);
	bufferevent_disable(bev, EV_READ);
}
static void echo_read_cb(struct bufferevent *bev, void *ctx)
{
	
		char * line;
		char* retval;
		size_t len;
        /* This callback is invoked when there is data to read on bev. */
        struct evbuffer *input = bufferevent_get_input(bev);
        struct evbuffer *output = bufferevent_get_output(bev);
		struct evbuffer_iovec v[2];

		line = evbuffer_readln(input, &len, EVBUFFER_EOL_CRLF);
		retval = (char*)command_parser(line);
		//command_parser(line);
		evbuffer_reserve_space(output, strlen(retval), v, 2);
		evbuffer_add(output, retval, strlen(retval));
		evbuffer_commit_space(output, v, 1);
		
		//evbuffer_add_buffer(output, input);
		

		//evbuffer_copyout(input, line, buffer_len);
		
        /* Copy all the data from the input buffer to the output buffer. */
		printf("%s\n",line);
		free(line);    
}
Exemple #11
0
void
client_read_cb(struct bufferevent *bev, void *ctx)
{
    size_t len;
    struct evbuffer *input = bufferevent_get_input(bev);
    char *line = evbuffer_readln(input, &len, EVBUFFER_EOL_CRLF);

    if (!line)
	return;

#ifdef BOARDD_MT
    process_line_ctx *plc = (process_line_ctx *) malloc(sizeof(process_line_ctx));
    if (!plc)
	return;

    // One request at a time.
    bufferevent_disable(bev, EV_READ);

    plc->bev = bev;
    plc->ctx = ctx;
    plc->line = line;
    threadpool_do(g_threadpool, threadpool_job_new(process_line_job_func, plc));
#else
    process_line(bev, ctx, line);
#endif
}
Exemple #12
0
static void echo_read_cb(struct bufferevent *bev, void *ctx) {
    struct evbuffer *input = bufferevent_get_input(bev);
    size_t str_len = 0;
    char* str_input = evbuffer_readln(input,&str_len,EVBUFFER_EOL_ANY);

    char* val = NULL;
    enum CMD_TYPE cmdtype = parse_cmd(str_input, str_len, &val);
    if (cmdtype == CMD_QUIT){
        bufferevent_free(bev);
        return;
    }
    struct evbuffer *ret = evbuffer_new();
    switch (cmdtype) {
        case CMD_GET:
        case CMD_ECHO:
            evbuffer_add(ret,val,strlen(val));
            break;
        case CMD_ERR:
            evbuffer_add(ret,"(ERR)",5);
            break;
        case CMD_OK:
            evbuffer_add(ret,"(OK)",4);
            break;
        case CMD_NULL:
            evbuffer_add(ret,"(NULL)",6);break;
        default:
            evbuffer_add(ret,"(UNKNOWN)",9);break;
    }
    evbuffer_add(ret,"\n",1);
    evbuffer_add_buffer(bufferevent_get_output(bev), ret);
}
void readcb(struct bufferevent *bev, void *ctx) {
	struct evbuffer *input, *output;
	char *line;
	size_t n;
	int i;

	input=bufferevent_get_input(bev);
	output=bufferevent_get_output(bev);

	while( (line=evbuffer_readln(input, &n, EVBUFFER_EOL_LF))) {
		for(i=0;i<n;++i) {
			line[i]=rot13_char(line[i]);
		}
		evbuffer_add(output, line, n);
		evbuffer_add(output, "\n", 1);
		free(line);
	}

	if(evbuffer_get_length(input)>=MAX_LINE) {
		char buf[1024];

		while(evbuffer_get_length(input)) {
			int n=evbuffer_remove(input, buf, sizeof(buf));

			for(i=0;i<n;++i) {
				buf[i]=rot13_char(buf[i]);
			}
			evbuffer_add(output, buf, n);
		}
		evbuffer_add(output, "\n", 1);
	}
}
Exemple #14
0
static evhtp_res
recv_form_field (RecvFSM *fsm, gboolean *no_line)
{
    char *line;
    size_t len;

    *no_line = FALSE;

    line = evbuffer_readln (fsm->line, &len, EVBUFFER_EOL_CRLF_STRICT);
    if (line != NULL) {
        if (strstr (line, fsm->boundary) != NULL) {
            seaf_debug ("[upload] form field ends.\n");

            g_free (fsm->input_name);
            fsm->input_name = NULL;
            fsm->state = RECV_HEADERS;
        } else {
            seaf_debug ("[upload] form field is %s.\n", line);

            g_hash_table_insert (fsm->form_kvs,
                                 g_strdup(fsm->input_name),
                                 g_strdup(line));
        }
        free (line);
    } else {
        *no_line = TRUE;
    }

    return EVHTP_RES_OK;
}
Exemple #15
0
void
readcb(struct bufferevent *bev, void *ctx)
{
    struct evbuffer *input, *output;
    char *line;
    size_t n;
    int i;
    input = bufferevent_get_input(bev);
    output = bufferevent_get_output(bev);

    while ((line = evbuffer_readln(input, &n, EVBUFFER_EOL_LF))) {
        for (i = 0; i < n; ++i)
            line[i] = rot13_char(line[i]);
        evbuffer_add(output, line, n);
        evbuffer_add(output, "\n", 1);
        free(line);
    }

    if (evbuffer_get_length(input) >= MAX_LINE) {
        /* Too long; just process what there is and go on so that the buffer
         * doesn't grow infinitely long. */
        char buf[1024];
        while (evbuffer_get_length(input)) {
            int n = evbuffer_remove(input, buf, sizeof(buf));
            for (i = 0; i < n; ++i)
                buf[i] = rot13_char(buf[i]);
            evbuffer_add(output, buf, n);
        }
        evbuffer_add(output, "\n", 1);
    }
}
ws_parse_state_t _ws_read_http_status(ws_t ws, 
				struct evbuffer *in, 
				int *http_major_version, 
				int *http_minor_version,
				int *status_code)
{
	char *line = NULL;
	size_t len;
	assert(ws);
	assert(in);

	line = evbuffer_readln(in, &len, EVBUFFER_EOL_CRLF);

	if (!line)
		return WS_PARSE_STATE_NEED_MORE;

	if (_ws_parse_http_status(line, 
		http_major_version, http_minor_version, status_code))
	{
		_ws_free(line);
		return WS_PARSE_STATE_ERROR;
	}

	_ws_free(line);
	return WS_PARSE_STATE_SUCCESS;
}
Exemple #17
0
char *redsocks_evbuffer_readline(struct evbuffer *buf)
{
#if _EVENT_NUMERIC_VERSION >= 0x02000000
	return evbuffer_readln(buf, NULL, EVBUFFER_EOL_CRLF);
#else
	return evbuffer_readline(buf);
#endif
}
Exemple #18
0
/* Control input callback. */
void
control_read_callback(unused struct bufferevent *bufev, void *data)
{
	struct client		*c = data;
	struct bufferevent	*out = c->stdout_event;
	char			*line;
	struct cmd_ctx		 ctx;
	struct cmd_list		*cmdlist;
	char			*cause;

	/* Read all available input lines. */
	line = evbuffer_readln(c->stdin_event->input, NULL, EVBUFFER_EOL_ANY);
	while (line) {
	    /* Parse command. */
	    ctx.msgdata = NULL;
	    ctx.cmdclient = NULL;
	    ctx.curclient = c;

	    ctx.error = control_msg_error;
	    ctx.print = control_msg_print;
	    ctx.info = control_msg_info;

	    if (cmd_string_parse(line, &cmdlist, &cause) != 0) {
		    /* Error */
		    if (cause) {
			    /* cause should always be set if there's an
			     * error.  */
			    evbuffer_add_printf(out->output,
						"%%error in line \"%s\": %s",
						line, cause);
			    bufferevent_write(out, "\n", 1);
			    xfree(cause);
		    }
	    } else {
		    /* Parsed ok. Run command. */
		    cmd_list_exec(cmdlist, &ctx);
		    cmd_list_free(cmdlist);
	    }

	    xfree(line);
	    /* Read input line. */
	    line = evbuffer_readln(c->stdin_event->input, NULL,
				   EVBUFFER_EOL_ANY);
	}
}
Exemple #19
0
void Server::read() {
	DEBUGF(bufferevent_read_buffer(buf, evbuf));
	char *ln;
	while((ln = evbuffer_readln(evbuf, NULL, EVBUFFER_EOL_ANY))) {
		parse(ln);
		free(ln);
	}
	return;
}
Exemple #20
0
void buf_write_callback(struct bufferevent *outgoing, void *arg)
{
	printf("call buf_write_callback\n");
	struct evbuffer *ev_write;

#if 0
	ev_write = evbuffer_new();
	evbuffer_add_printf(ev_write, "[kaka]");

	bufferevent_write_buffer(outgoing, ev_write);

	evbuffer_free(ev_write);
#endif

#if 0
	struct evbuffer *incomming_buffer;

	incomming_buffer = bufferevent_get_input(outgoing);
	if (incomming_buffer == NULL) {
		printf("error: failed to get input bufferevent\n");
		return;
	}
#endif

	size_t input_length = 100, output_length = 100;
	char *input_data, *output_data;

	input_data = evbuffer_readln(outgoing->input, &input_length, EVBUFFER_EOL_ANY);
	if (input_data) {
		printf("buf_write_callback: input = %s\n", input_data);
	} else {
		printf("buf_write_callback: input is NULL (%ld), <input=%p, output=%p>\n",
				input_length, outgoing->input, outgoing->output);
	}

	output_data = evbuffer_readln(outgoing->output, &output_length, EVBUFFER_EOL_ANY);
	if (output_data) {
		printf("buf_write_callback: output = %s\n", output_data);
	} else {
		printf("buf_write_callback: output is NULL (%ld) <input=%p, output=%p>\n",
				input_length, outgoing->input, outgoing->output);
	}
}
Exemple #21
0
void rpc(struct bufferevent*bev, void *ctx) {

  conn_t *conn = ctx;
  struct state *s = conn->state; 

  if (conn->be_client == NULL) return;

  struct evbuffer *buffer = bufferevent_get_input(conn->be_client);


  char *line;
  char header[64], header_v[2500];
  if (s == NULL) {
    conn->state = s = calloc(sizeof(struct state), 1);
    s->pos = STATE_HEADER;
  }

  if (s->pos == STATE_HEADER) {
    while ((line = evbuffer_readln(buffer, NULL, EVBUFFER_EOL_CRLF)) != NULL) {

      if (strlen(line) == 0) {

	free(line);
	s->pos = STATE_BODY;
	break;
      }

      sscanf(line, "%s %s", header, header_v);
      
      if (strcmp(header, "Content-Length:") == 0) 
	s->length = atoi(header_v);

      free(line);
    }
  }

  if (s->pos == STATE_BODY) {
    if (s->length == 0) goto end; 

    s->body = malloc(s->length + 1);
    evbuffer_remove(buffer, s->body, s->length);
    s->body[s->length] = 0;

  }
  else return;

 end:
  handle_request(conn);
}
Exemple #22
0
// read HTTP headers
// return TRUE if all haders are read, FALSE if not enough data
static gboolean s3http_client_parse_headers (S3HttpClient *http, struct evbuffer *input_buf)
{
    size_t line_length = 0;
    char *line = NULL;
    S3HttpClientHeader *header;

	while ((line = evbuffer_readln (input_buf, &line_length, EVBUFFER_EOL_CRLF)) != NULL) {
		char *skey, *svalue;
        
        // the last line
		if (*line == '\0') {
			g_free (line);
			return TRUE;
		}

     //   LOG_debug (HTTP_LOG, "HEADER line: %s\n", line);

		svalue = line;
		skey = strsep (&svalue, ":");
		if (svalue == NULL) {
            LOG_debug (HTTP_LOG, "Wrong header data received !");
	        g_free (line);
			return FALSE;
        }

		svalue += strspn (svalue, " ");

        header = g_new0 (S3HttpClientHeader, 1);
        header->key = g_strdup (skey);
        header->value = g_strdup (svalue);
        http->l_input_headers = g_list_append (http->l_input_headers, header);
        
        if (!strcmp (skey, "Content-Length")) {
            char *endp;
		    http->input_length = evutil_strtoll (svalue, &endp, 10);
		    if (*svalue == '\0' || *endp != '\0') {
                LOG_debug (HTTP_LOG, "Illegal content length: %s", svalue);
                http->input_length = 0;
            }
        }

        g_free (line);        
    }
    LOG_debug (HTTP_LOG, "Wrong header line: %s", line);

    // if we are here - not all headers have been received !
    return FALSE;
}
Exemple #23
0
void client_readcb(struct bufferevent* bev, void* context) {
  struct client* client = context;
  struct evbuffer* input = bufferevent_get_input(bev);
  if (client->websocket && client->websocket->connected) {
    int res = 0;
    while (res == 0 && evbuffer_get_length(input) > 0)
      res = decode_websocket(client);
  } else {
    char* line;
    size_t len;
    int res = 0;
    while (res == 0 && (line = evbuffer_readln(input, &len, EVBUFFER_EOL_CRLF))) {
      res = process_line(client, line, len);
      free(line);
    };
  }
};
Exemple #24
0
static int http_parse_headers(struct http_connection *hc, struct evbuffer *evb)
{
	int ret = 0;
	char *line = NULL;
	size_t line_length = 0;
	struct name_value * parm = NULL;

	while ((line = evbuffer_readln(evb, &line_length, EVBUFFER_EOL_CRLF))
	       != NULL) {
		char *skey, *svalue;

		if (*line == '\0') { /* Last header - Done */
			free(line);
			break;
		}

		/* Processing of header lines */
		svalue = line;
		skey = strsep(&svalue, ":");
		if (svalue == NULL)
			goto failed;

		svalue += strspn(svalue, " ");
		//dbprintf("header name:'%s' header value:'%s'\n", skey, svalue);

		parm = (struct name_value *)malloc(sizeof(struct name_value));
		if (!parm) {
			goto failed;
		}

		parm->name = strdup(skey);
		parm->value = strdup(svalue);

		parm->next = hc->request_header;
		hc->request_header = parm;

		free(line);
	}

	return 0;
failed:
	if (line) {
		free(line);
	}
	return -1;
}
Exemple #25
0
int main(int argc, char **argv)
{
    struct evbuffer *buf = evbuffer_new();
    size_t n;
    const char *line, *str = "GET / HTTP/1.1\r\n"
        "Host: hello.world\r\n"
        "\r\n";
    evbuffer_add(buf, str, strlen(str));
    while (1) {
        line = evbuffer_readln(buf, &n, EVBUFFER_EOL_CRLF);
        printf("line=[%s], address=[%#x], n=%d\n", line, (unsigned int)line, n);
        if (NULL == line) {
            break;
        }
        free(line);
    }
    return 0;
}
Exemple #26
0
void TorControlConnection::readcb(struct bufferevent *bev, void *ctx)
{
    TorControlConnection *self = (TorControlConnection*)ctx;
    struct evbuffer *input = bufferevent_get_input(bev);
    size_t n_read_out = 0;
    char *line;
    assert(input);
    //  If there is not a whole line to read, evbuffer_readln returns NULL
    while((line = evbuffer_readln(input, &n_read_out, EVBUFFER_EOL_CRLF)) != NULL)
    {
        std::string s(line, n_read_out);
        free(line);
        if (s.size() < 4) // Short line
            continue;
        // <status>(-|+| )<data><CRLF>
        self->message.code = atoi(s.substr(0,3));
        self->message.lines.push_back(s.substr(4));
        char ch = s[3]; // '-','+' or ' '
        if (ch == ' ') {
            // Final line, dispatch reply and clean up
            if (self->message.code >= 600) {
                // Dispatch async notifications to async handler
                // Synchronous and asynchronous messages are never interleaved
                self->async_handler(*self, self->message);
            } else {
                if (!self->reply_handlers.empty()) {
                    // Invoke reply handler with message
                    self->reply_handlers.front()(*self, self->message);
                    self->reply_handlers.pop_front();
                } else {
                    LogPrint("tor", "tor: Received unexpected sync reply %i\n", self->message.code);
                }
            }
            self->message.Clear();
        }
    }
    //  Check for size of buffer - protect against memory exhaustion with very long lines
    //  Do this after evbuffer_readln to make sure all full lines have been
    //  removed from the buffer. Everything left is an incomplete line.
    if (evbuffer_get_length(input) > MAX_LINE_LENGTH) {
        LogPrintf("tor: Disconnecting because MAX_LINE_LENGTH exceeded\n");
        self->Disconnect();
    }
}
Exemple #27
0
/* Converts parameters to a string in application/x-www-form-urlencoded format */
static int
body_print(char **body, struct keyval *kv)
{
  struct evbuffer *evbuf;
  struct onekeyval *okv;
  char *k;
  char *v;

  evbuf = evbuffer_new();

  for (okv = kv->head; okv; okv = okv->next)
    {
      k = evhttp_encode_uri(okv->name);
      if (!k)
        continue;

      v = evhttp_encode_uri(okv->value);
      if (!v)
	{
	  free(k);
	  continue;
	}

      evbuffer_add(evbuf, k, strlen(k));
      evbuffer_add(evbuf, "=", 1);
      evbuffer_add(evbuf, v, strlen(v));
      if (okv->next)
	evbuffer_add(evbuf, "&", 1);

      free(k);
      free(v);
    }

  evbuffer_add(evbuf, "\n", 1);

  *body = evbuffer_readln(evbuf, NULL, EVBUFFER_EOL_ANY);

  evbuffer_free(evbuf);

  DPRINTF(E_DBG, L_LASTFM, "Parameters in request are: %s\n", *body);

  return 0;
}
Exemple #28
0
void
server_mc_read_cb(struct bufferevent *bev, void *ctx)
{
    struct server *s = (struct server *)ctx;
    struct evbuffer *in = bufferevent_get_input(bev);
    struct mc       *mc = v_mc_find_if(s->peers, (void *)find_bev, bev);
    size_t len;
    char *line;

    /* Do nothing: this peer seems to exists, but we didn't approve it yet*/
    if (mc == v_mc_end(s->peers))
        return ;

    while ((line = evbuffer_readln(in, &len, EVBUFFER_EOL_CRLF)) != NULL)
    {
        struct vector_cptr *splited;
        char *cmd_name;

        log_debug("[META] [%s]", line);
        splited = split(line);
        cmd_name = v_cptr_at(splited, 0);
        if (strncmp(cmd_name, "udp_port", strlen(cmd_name)) == 0)
        {
            char            *s_udp_port = v_cptr_at(splited, 1);
            unsigned short  port = atoi(s_udp_port);
            struct endpoint udp_remote_endpoint;

            endpoint_init(&udp_remote_endpoint,
                          mc->p.address,
                          mc->p.len);

            endpoint_set_port(&udp_remote_endpoint, port);

            udp_register_new_peer(s->udp,
                                  &udp_remote_endpoint,
                                  DTLS_DISABLE);
        }
        v_cptr_delete(splited);
        free(line);
    }
}
Exemple #29
0
static evhtp_res
recv_file_data (RecvFSM *fsm, gboolean *no_line)
{
    char *line;
    size_t len;

    *no_line = FALSE;

    line = evbuffer_readln (fsm->line, &len, EVBUFFER_EOL_CRLF_STRICT);
    if (!line) {
        /* If we haven't read an entire line, but the line
         * buffer gets too long, flush the content to file.
         * It should be safe to assume the boundary line is
         * no longer than 10240 bytes.
         */
        if (evbuffer_get_length (fsm->line) >= MAX_CONTENT_LINE) {
            seaf_debug ("[upload] recv file data %d bytes.\n",
                     evbuffer_get_length(fsm->line));
            if (fsm->recved_crlf) {
                if (writen (fsm->fd, "\r\n", 2) < 0) {
                    seaf_warning ("[upload] Failed to write temp file: %s.\n",
                               strerror(errno));
                    return EVHTP_RES_SERVERR;
                }
            }

            size_t size = evbuffer_get_length (fsm->line);
            char *buf = g_new (char, size);
            evbuffer_remove (fsm->line, buf, size);
            if (writen (fsm->fd, buf, size) < 0) {
                seaf_warning ("[upload] Failed to write temp file: %s.\n",
                           strerror(errno));
                g_free (buf);
                return EVHTP_RES_SERVERR;
            }
            g_free (buf);
            fsm->recved_crlf = FALSE;
        }
        *no_line = TRUE;
    } else if (strstr (line, fsm->boundary) != NULL) {
Exemple #30
0
/* Control input callback. Read lines and fire commands. */
void
control_callback(struct client *c, int closed, unused void *data)
{
	char		*line, *cause;
	struct cmd_ctx	 ctx;
	struct cmd_list	*cmdlist;

	if (closed)
		c->flags |= CLIENT_EXIT;

	for (;;) {
		line = evbuffer_readln(c->stdin_data, NULL, EVBUFFER_EOL_LF);
		if (line == NULL)
			break;
		if (*line == '\0') { /* empty line exit */
			c->flags |= CLIENT_EXIT;
			break;
		}

		ctx.msgdata = NULL;
		ctx.cmdclient = NULL;
		ctx.curclient = c;

		ctx.error = control_msg_error;
		ctx.print = control_msg_print;
		ctx.info = control_msg_info;

		if (cmd_string_parse(line, &cmdlist, &cause) != 0) {
			control_write(c, "%%error in line \"%s\": %s", line,
			    cause);
			xfree(cause);
		} else {
			cmd_list_exec(cmdlist, &ctx);
			cmd_list_free(cmdlist);
		}

		xfree(line);
	}
}