Skip to content

Instantly share code, notes, and snippets.

@errzey
Created April 12, 2015 17:34
Show Gist options
  • Save errzey/472ac7c6c6f3ca09ca94 to your computer and use it in GitHub Desktop.
Save errzey/472ac7c6c6f3ca09ca94 to your computer and use it in GitHub Desktop.
psuedocode for kernelcorn
struct query {
struct event * r_pipe_ev;
int r_pipe; /* response pipe */
int q_pipe; /* query pipe */
int response[256];
};
static void
read_response(int sock, short events, void * arg) {
struct query * q = (struct query *)arg;
ssize_t nread;
if ((nread = read(q->r_pipe, q->response, sizeof(q->response))) == -1) {
fprintf(stderr, "whoah nelly %s\n", strerror(errno));
goto done;
}
/* XXX: don't know if you need it, but add logic to make sure we get the whole response,
* and if we didn't, we need a buffering mechanism, and return here if not complete.
*
* Like
*
* nread = read(q->r_pipe &q->response_buffer[q->buffer_idx], ...);
* if (p->response_len < expected_response_len) {
* return;
* }
*
*/
q->response[nread] = '\0';
fprintf(stderr, "response: %s\n", q->response);
done:
close(q->r_pipe);
close(q->q_pipe);
event_free(q->r_pipe_ev);
free(q);
}
static void
do_query(struct event_base * evbase, const char * path) {
struct query * q;
q = calloc(1, sizeof(struct query));
q->r_pipe = open("/tmp/tor-onions-response", O_NONBLOCK);
q->q_pipe = open("/tmp/tor-onions-query", O_NONBLOCK);
q->r_pipe_ev = event_new(evbase, q->r_pipe, EV_READ | EV_PERSIST, read_response, q);
/* in reality we need to make this another event if we get a -1 and an errno that
* is retry-able.
*/
write(q->q_pipe, path, strlen(path) + 1);
/* now we add the event for reading the response and return. the function read_response
* will execute once data is ready.
*/
event_add(q->r_pipe, NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment