Example #1
0
void ssl_want_read(struct connection *c)
{
	struct conn_info *b = c->newconn;

	set_timeout(c);

	if (c->no_tsl) c->ssl->options |= SSL_OP_NO_TLSv1;
	switch (SSL_get_error(c->ssl, SSL_connect(c->ssl))) {
		case SSL_ERROR_NONE:
			c->newconn = NULL;
			b->func(c);
			mem_free(b);
			break;
		case SSL_ERROR_WANT_READ:
			set_handlers(*b->sock, (void(*)(void *))ssl_want_read, NULL, (void(*)(void *))exception, c);
			break;
		case SSL_ERROR_WANT_WRITE:
			set_handlers(*b->sock, NULL, (void(*)(void *))ssl_want_read, (void(*)(void *))exception, c);
			break;
		default:
			c->no_tsl++;
			setcstate(c, S_SSL_ERROR);
			retry_connection(c);
			break;
	}
}
Example #2
0
void destroy_terminal(struct terminal *term)
{
    while ((term->windows.next) != &term->windows) delete_window(term->windows.next);
    /*if (term->cwd) mem_free(term->cwd);*/
    if (term->title) mem_free(term->title);
    mem_free(term->screen);
    mem_free(term->last_screen);
    set_handlers(term->fdin, NULL, NULL, NULL, NULL);
    mem_free(term->input_queue);
    if (term->blocked != -1) {
        close(term->blocked);
        set_handlers(term->blocked, NULL, NULL, NULL, NULL);
    }
    del_from_list(term);
    close(term->fdin);
    if (term->fdout != 1) {
        if (term->fdout != term->fdin) close(term->fdout);
    } else {
        unhandle_terminal_signals(term);
        free_all_itrms();
#ifndef NO_FORK_ON_EXIT
        if (!list_empty(terminals)) {
            if (fork() > 0) _exit(0);
        }
#endif
    }
    mem_free(term);
    check_if_no_terminal();
}
Example #3
0
void dns_found(struct connection *c, int state)
{
	int s;
	struct conn_info *b = c->newconn;
	if (state) {
		setcstate(c, S_NO_DNS);
		abort_connection(c);
		return;
	}
	if ((s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
		setcstate(c, get_error_from_errno(errno));
		retry_connection(c);
		return;
	}
	*b->sock = s;
	fcntl(s, F_SETFL, O_NONBLOCK);
	memset(&b->sa, 0, sizeof(struct sockaddr_in));
	b->sa.sin_family = AF_INET;
	b->sa.sin_addr.s_addr = b->addr;
	b->sa.sin_port = htons(b->port);
	if (connect(s, (struct sockaddr *)&b->sa, sizeof b->sa)) {
		if (errno != EALREADY && errno != EINPROGRESS) {
#ifdef BEOS
			if (errno == EWOULDBLOCK) errno = ETIMEDOUT;
#endif
			setcstate(c, get_error_from_errno(errno));
			retry_connection(c);
			return;
		}
		set_handlers(s, NULL, (void(*)(void *))connected, (void(*)(void *))exception, c);
		setcstate(c, S_CONN);
	} else {
		connected(c);
	}
}
Example #4
0
void close_socket(int *s)
{
	if (*s == -1) return;
	close(*s);
	set_handlers(*s, NULL, NULL, NULL, NULL);
	*s = -1;
}
Example #5
0
File: kbd.c Project: Efreak/elinks
/** A select_handler_T write_func for itrm_out.sock.  This is called
 * when there is data in @c itrm->out.queue and it is possible to write
 * it to @c itrm->out.sock.  When @c itrm->out.queue becomes empty, this
 * handler is temporarily removed.  */
static void
itrm_queue_write(struct itrm *itrm)
{
	int written;
	int qlen = int_min(itrm->out.queue.len, 128);

	assertm(qlen, "event queue empty");
	if_assert_failed return;

	written = safe_write(itrm->out.sock, itrm->out.queue.data, qlen);
	if (written <= 0) {
		if (written < 0) free_itrm(itrm); /* write error */
		return;
	}

	itrm->out.queue.len -= written;

	if (itrm->out.queue.len == 0) {
		set_handlers(itrm->out.sock,
			     get_handler(itrm->out.sock, SELECT_HANDLER_READ),
			     NULL,
			     get_handler(itrm->out.sock, SELECT_HANDLER_ERROR),
			     get_handler(itrm->out.sock, SELECT_HANDLER_DATA));
	} else {
		assert(itrm->out.queue.len > 0);
		memmove(itrm->out.queue.data, itrm->out.queue.data + written, itrm->out.queue.len);
	}
}
Example #6
0
File: kbd.c Project: Efreak/elinks
void
itrm_queue_event(struct itrm *itrm, unsigned char *data, int len)
{
	int w = 0;

	if (!len) return;

	if (!itrm->out.queue.len && can_write(itrm->out.sock)) {
		w = safe_write(itrm->out.sock, data, len);
		if (w <= 0 && HPUX_PIPE) {
			register_bottom_half(free_itrm, itrm);
			return;
		}
	}

	if (w < len) {
		int left = len - w;
		unsigned char *c = mem_realloc(itrm->out.queue.data,
					       itrm->out.queue.len + left);

		if (!c) {
			free_itrm(itrm);
			return;
		}

		itrm->out.queue.data = c;
		memcpy(itrm->out.queue.data + itrm->out.queue.len, data + w, left);
		itrm->out.queue.len += left;
		set_handlers(itrm->out.sock,
			     get_handler(itrm->out.sock, SELECT_HANDLER_READ),
			     (select_handler_T) itrm_queue_write,
			     (select_handler_T) free_itrm, itrm);
	}
}
Example #7
0
void WindowLoader::set_generic_parameters(const serialization::Node &node, WindowBase *base) {
	set_dimensions(node, base);
	set_color(node, base);
	base->set_renderer(m_renderer.get());
	base->m_name = node.value(NAME);
	set_handlers(node, base);

}
Example #8
0
void connected(struct connection *c)
{
	struct conn_info *b = c->newconn;
	int err = 0;
	socklen_t len = sizeof(int);
	if (getsockopt(*b->sock, SOL_SOCKET, SO_ERROR, (void *)&err, &len))
		if (!(err = errno)) {
			err = -(S_STATE);
			goto bla;
		}
	if (err >= 10000) err -= 10000;	/* Why does EMX return so large values? */
	if (err > 0) {
		bla:
		setcstate(c, get_error_from_errno(err));
		retry_connection(c);
		return;
	}
	set_timeout(c);
#ifdef HAVE_SSL
	if (c->ssl) {
		c->ssl = getSSL();
		SSL_set_fd(c->ssl, *b->sock);
		if (c->no_tsl) c->ssl->options |= SSL_OP_NO_TLSv1;
		switch (SSL_get_error(c->ssl, SSL_connect(c->ssl))) {
			case SSL_ERROR_WANT_READ:
				setcstate(c, S_SSL_NEG);
				set_handlers(*b->sock, (void(*)(void *))ssl_want_read, NULL, (void(*)(void *))exception, c);
				return;
			case SSL_ERROR_WANT_WRITE:
				setcstate(c, S_SSL_NEG);
				set_handlers(*b->sock, NULL, (void(*)(void *))ssl_want_read, (void(*)(void *))exception, c);
				return;
			case SSL_ERROR_NONE:
				break;
			default:
				c->no_tsl++;
				setcstate(c, S_SSL_ERROR);
				retry_connection(c);
				return;
		}
	}
#endif
	c->newconn = NULL;
	b->func(c);
	mem_free(b);
}
Example #9
0
void read_from_socket(struct connection *c, int s, struct read_buffer *buf, void (*read_func)(struct connection *, struct read_buffer *))
{
	buf->done = read_func;
	buf->sock = s;
	if (c->buffer && buf != c->buffer) mem_free(c->buffer);
	c->buffer = buf;
	set_handlers(s, (void (*)(void *))read_select, NULL, (void (*)(void *))exception, c);
}
Example #10
0
void unblock_terminal(struct terminal *term)
{
    close_handle((void *)term->blocked);
    term->blocked = -1;
    set_handlers(term->fdin, (void (*)(void *))in_term, NULL, (void (*)(void *))destroy_terminal, term);
    unblock_itrm(term->fdin);
    redraw_terminal_cls(term);
}
Example #11
0
File: kbd.c Project: Efreak/elinks
/** Disable reading from itrm_in.std.  Reading should be disabled
 * whenever itrm->in.queue is full (there is no room for the data)
 * or itrm->blocked is 1 (other processes may read the data).  */
static void
unhandle_itrm_stdin(struct itrm *itrm)
{
	assert(itrm->in.std >= 0);
	if_assert_failed return;

	set_handlers(itrm->in.std, (select_handler_T) NULL, NULL,
		     (select_handler_T) free_itrm, itrm);
}
Example #12
0
static void end_real_lookup(struct dnsquery *q)
{
	int r = 1;
	if (!q->addr || read(q->h, q->addr, sizeof(ip__address)) != sizeof(ip__address)) goto end;
	r = 0;

	end:
	set_handlers(q->h, NULL, NULL, NULL, NULL);
	close(q->h);
	q->xfn(q, r);
}
Example #13
0
void setIdt()
{
  /* Program interrups/exception service routines */
  idtR.base  = (DWord)idt;
  idtR.limit = IDT_ENTRIES * sizeof(Gate) - 1;
  
  set_handlers();

  /* ADD INITIALIZATION CODE FOR INTERRUPT VECTOR */

  set_idt_reg(&idtR);
}
Example #14
0
void setIdt()
{
	idtR.base  = (DWord)idt;
	idtR.limit = IDT_ENTRIES*sizeof(Gate) - 1;

	set_handlers();
	setInterruptHandler(32, clock_handler, 0);
	setInterruptHandler(33, keyboard_handler, 0);
	setTrapHandler(0x80, syscall_handler, 3); // 3 = user privilege level

	set_idt_reg(&idtR);
}
Example #15
0
void
resume_mouse(void *h)
{
	struct gpm_mouse_spec *gms = h;

	if (!gms) return;

	gms->h = init_mouse(gms->cons, 0);
	if (gms->h < 0) return;

	set_handlers(gms->h, (select_handler_T) gpm_mouse_in, NULL, NULL, gms);
}
Example #16
0
int bind_to_af_unix(void)
{
	int u = 0;
	int a1 = 1;
	int cnt = 0;
	int af;
	if ((af = get_address()) == -1) return -1;
	again:
	if ((s_unix_fd = socket(af, SOCK_STREAM, 0)) == -1) return -1;
#if defined(SOL_SOCKET) && defined(SO_REUSEADDR)
	setsockopt(s_unix_fd, SOL_SOCKET, SO_REUSEADDR, (void *)&a1, sizeof a1);
#endif
	if (bind(s_unix_fd, s_unix, s_unix_l)) {
		/*perror("");
		debug("bind: %d", errno);*/
		close(s_unix_fd);
		if ((s_unix_fd = socket(af, SOCK_STREAM, 0)) == -1) return -1;
#if defined(SOL_SOCKET) && defined(SO_REUSEADDR)
		setsockopt(s_unix_fd, SOL_SOCKET, SO_REUSEADDR, (void *)&a1, sizeof a1);
#endif
		if (connect(s_unix_fd, s_unix, s_unix_l)) {
			/*perror("");
			debug("connect: %d", errno);*/
			if (++cnt < MAX_BIND_TRIES) {
				struct timeval tv = { 0, 100000 };
				fd_set dummy;
				FD_ZERO(&dummy);
				select(0, &dummy, &dummy, &dummy, &tv);
				close(s_unix_fd);
				goto again;
			}
			close(s_unix_fd), s_unix_fd = -1;
			if (!u) {
				unlink_unix();
				u = 1;
				goto again;
			}
			mem_free(s_unix), s_unix = NULL;
			return -1;
		}
		mem_free(s_unix), s_unix = NULL;
		return s_unix_fd;
	}
	if (listen(s_unix_fd, 100)) {
		error("ERROR: listen failed: %d", errno);
		mem_free(s_unix), s_unix = NULL;
		close(s_unix_fd), s_unix_fd = -1;
		return -1;
	}
	set_handlers(s_unix_fd, af_unix_connection, NULL, NULL, NULL);
	return -1;
}
Example #17
0
void read_select(struct connection *c)
{
	struct read_buffer *rb;
	int rd;
	if (!(rb = c->buffer)) {
		internal("read socket has no buffer");
		setcstate(c, S_INTERNAL);
		abort_connection(c);
		return;
	}
	set_handlers(rb->sock, NULL, NULL, NULL, NULL);
	if ((unsigned)rb->len > MAXINT - sizeof(struct read_buffer) - READ_SIZE) overalloc();
	rb = mem_realloc(rb, sizeof(struct read_buffer) + rb->len + READ_SIZE);
	c->buffer = rb;

#ifdef HAVE_SSL
	if(c->ssl) {
		if ((rd = SSL_read(c->ssl, rb->data + rb->len, READ_SIZE)) <= 0) {
			int err;
			if ((err = SSL_get_error(c->ssl, rd)) == SSL_ERROR_WANT_READ) {
				read_from_socket(c, rb->sock, rb, rb->done);
				return;
			}
			if (rb->close && !rd) {
				rb->close = 2;
				rb->done(c, rb);
				return;
			}
			setcstate(c, rd ? (err == SSL_ERROR_SYSCALL ? get_error_from_errno(errno) : S_SSL_ERROR) : S_CANT_READ);
			/*mem_free(rb);*/
			if (!rd || err == SSL_ERROR_SYSCALL) retry_connection(c);
			else abort_connection(c);
			return;
		}
	} else
#endif
		if ((rd = read(rb->sock, rb->data + rb->len, READ_SIZE)) <= 0) {
			if (rb->close && !rd) {
				rb->close = 2;
				rb->done(c, rb);
				return;
			}
			setcstate(c, rd ? get_error_from_errno(errno) : S_CANT_READ);
			/*mem_free(rb);*/
			retry_connection(c);
			return;
		}
	log_data(rb->data + rb->len, rd);
	rb->len += rd;
	rb->done(c, rb);
}
Example #18
0
void write_select(struct connection *c)
{
	struct write_buffer *wb;
	int wr;
	if (!(wb = c->buffer)) {
		internal("write socket has no buffer");
		setcstate(c, S_INTERNAL);
		abort_connection(c);
		return;
	}
	set_timeout(c);
	/*printf("ws: %d\n",wb->len-wb->pos);
	for (wr = wb->pos; wr < wb->len; wr++) printf("%c", wb->data[wr]);
	printf("-\n");*/

#ifdef HAVE_SSL
	if(c->ssl) {
		if ((wr = SSL_write(c->ssl, wb->data + wb->pos, wb->len - wb->pos)) <= 0) {
			int err;
			if ((err = SSL_get_error(c->ssl, wr)) != SSL_ERROR_WANT_WRITE) {
				setcstate(c, wr ? (err == SSL_ERROR_SYSCALL ? get_error_from_errno(errno) : S_SSL_ERROR) : S_CANT_WRITE);
				if (!wr || err == SSL_ERROR_SYSCALL) retry_connection(c);
				else abort_connection(c);
				return;
			}
			else return;
		}
	} else
#endif
		if ((wr = write(wb->sock, wb->data + wb->pos, wb->len - wb->pos)) <= 0) {
#ifdef ATHEOS
	/* Workaround for a bug in Syllable */
			if (wr && errno == EAGAIN) {
				return;
			}
#endif
			setcstate(c, wr ? get_error_from_errno(errno) : S_CANT_WRITE);
			retry_connection(c);
			return;
		}

	/*printf("wr: %d\n", wr);*/
	if ((wb->pos += wr) == wb->len) {
		void (*f)(struct connection *) = wb->done;
		c->buffer = NULL;
		set_handlers(wb->sock, NULL, NULL, NULL, NULL);
		mem_free(wb);
		f(c);
	}
}
Example #19
0
void setIdt()
{
  /* Program interrupts/exception service routines */
  idtR.base  = (DWord)idt;
  idtR.limit = IDT_ENTRIES * sizeof(Gate) - 1;
  /* ADD INITIALIZATION CODE FOR INTERRUPT VECTOR (IDT)*/

  set_handlers();
  
  setInterruptHandler(32, clock_handler, 0);
  setInterruptHandler(33, keyboard_handler, 0);
  
  set_idt_reg(&idtR);
}
void setIdt()
{
  /* Program interrups/exception service routines */
  idtR.base  = (DWord)idt;
  idtR.limit = IDT_ENTRIES * sizeof(Gate) - 1;
  
  set_handlers();
  setInterruptHandler(32, clock_handler /*void (*handler)()*/, 0/*int privLevel*/);
  setInterruptHandler(33, keyboard_handler /*void (*handler)()*/, 0/*int privLevel*/);
  setTrapHandler(0x80, system_call_handler, 3);
  /* ADD INITIALIZATION CODE FOR INTERRUPT VECTOR */

  set_idt_reg(&idtR);
}
Example #21
0
void setIdt()
{
  /* Program interrups/exception service routines */
  idtR.base  = (DWord)idt;
  idtR.limit = IDT_ENTRIES * sizeof(Gate) - 1;
  
  set_handlers();

  /* ADD INITIALIZATION CODE FOR INTERRUPT VECTOR */
  setInterruptHandler(33, keyboard_handler, 0);
  setInterruptHandler(0x80, system_call_handler, 3);
  setInterruptHandler(32, clock_handler, 0); // is in this privileged mode???

  set_idt_reg(&idtR);
}
Example #22
0
void write_to_socket(struct connection *c, int s, unsigned char *data, int len, void (*write_func)(struct connection *))
{
	struct write_buffer *wb;
	log_data(data, len);
	if ((unsigned)len > MAXINT - sizeof(struct write_buffer)) overalloc();
	wb = mem_alloc(sizeof(struct write_buffer) + len);
	wb->sock = s;
	wb->len = len;
	wb->pos = 0;
	wb->done = write_func;
	memcpy(wb->data, data, len);
	if (c->buffer) mem_free(c->buffer);
	c->buffer = wb;
	set_handlers(s, NULL, (void (*)(void *))write_select, (void (*)(void *))exception, c);
}
Example #23
0

void setIdt()
{
  /* Program interrups/exception service routines */
  idtR.base  = (DWord)idt;
  idtR.limit = IDT_ENTRIES * sizeof(Gate) - 1;

  /* ADD INITIALIZATION CODE FOR INTERRUPT VECTOR */
  setInterruptHandler(0x21, keyboard_handler, 0); // IDT entry 33  
  setInterruptHandler(0x20, clock_handler, 0);	//   IDT 32

  setTrapHandler(0x80, system_call_handler, 3);

  set_handlers();
Example #24
0
void close_socket( struct socket *socket )
{
  int eax;
  if ( socket[0].fd != -1 )
  {
    if ( socket->ssl )
    {
      ssl_close( &socket[0] );
      socket->fd = socket->fd;
    }
    close( socket->fd );
    set_handlers( socket->fd, 0, 0, 0, 0 );
    socket = -1;
  }
  return;
}
Example #25
0
static int do_lookup(struct dnsquery *q, int force_async)
{
	/*debug("starting lookup for %s", q->name);*/
#ifndef NO_ASYNC_LOOKUP
	if (!async_lookup && !force_async) {
#endif
		int r;
		sync_lookup:
		r = do_real_lookup(q->name, q->addr);
		q->xfn(q, r);
		return 0;
#ifndef NO_ASYNC_LOOKUP
	} else {
		if ((q->h = start_thread((void (*)(void *, int))lookup_fn, q->name, strlen(q->name) + 1)) == -1) goto sync_lookup;
		set_handlers(q->h, (void (*)(void *))end_real_lookup, NULL, (void (*)(void *))failed_real_lookup, q);
		return 1;
	}
#endif
}
Example #26
0
void *
handle_mouse(int cons, void (*fn)(void *, unsigned char *, int),
	     void *data)
{
	int h;
	struct gpm_mouse_spec *gms;

	h = init_mouse(cons, 0);
	if (h < 0) return NULL;

	gms = mem_alloc(sizeof(*gms));
	if (!gms) return NULL;
	gms->h = h;
	gms->cons = cons;
	gms->fn = fn;
	gms->data = data;
	set_handlers(h, (select_handler_T) gpm_mouse_in, NULL, NULL, gms);

	return gms;
}
Example #27
0
void setIdt()
{
  /* Program interrups/exception service routines */
  idtR.base  = (DWord)idt;
  idtR.limit = IDT_ENTRIES * sizeof(Gate) - 1;
  
  set_handlers();

  void clock_handler(void); //Header clock_handler (Defined at entry.S)

  void keyboard_handler(void); //Header keyboard_handler (Defined at entry.S)

  void system_call_handler(void);
  
  setInterruptHandler(32, clock_handler, 0); //Entrada IDT interrupcio clock
  setInterruptHandler(33, keyboard_handler, 0); //Entrada IDT interrupcio keyboard
  setTrapHandler(0x80, system_call_handler, 3);
  
  /* ADD INITIALIZATION CODE FOR INTERRUPT VECTOR */

  set_idt_reg(&idtR);
}
Example #28
0
static void unhandle_fb_mouse(void)
{
	if (fb_hgpm >= 0) set_handlers(fb_hgpm, (void (*)(void *))NULL, (void (*)(void *))NULL, (void (*)(void *))NULL, NULL);
#ifndef USE_GPM_DX
	fb_hgpm = -1;
	if (fb_old_ws_v) {
		int rs;
		EINTRLOOP(rs, ioctl(1, TIOCSWINSZ, &fb_old_ws));
		fb_old_ws_v = 0;
	}
#endif
	Gpm_Close();
#ifdef SIGTSTP
	install_signal_handler(SIGTSTP, (void (*)(void *))sig_tstp, NULL, 0);
#endif
#ifdef SIGCONT
	install_signal_handler(SIGCONT, (void (*)(void *))sig_cont, NULL, 0);
#endif
#ifdef SIGTTIN
	install_signal_handler(SIGTTIN, (void (*)(void *))sig_tstp, NULL, 0);
#endif
}
Example #29
0
void
read_from_socket(struct socket *socket, struct read_buffer *buffer,
		 struct connection_state state, socket_read_T done)
{
	const int is_buffer_new = (buffer != socket->read_buffer);
	struct socket_weak_ref ref;
	select_handler_T write_handler;

	ref.socket = socket;
	add_to_list(socket_weak_refs, &ref);

	buffer->done = done;

	socket->ops->set_timeout(socket, connection_state(0));
	socket->ops->set_state(socket, state);

	del_from_list(&ref);
	if (ref.socket == NULL) {
		/* socket->ops->set_state deleted the socket. */
		if (is_buffer_new)
			mem_free(buffer);
		return;
	}

	if (socket->read_buffer && buffer != socket->read_buffer)
		mem_free(socket->read_buffer);
	socket->read_buffer = buffer;

	if (socket->duplex) {
		write_handler = get_handler(socket->fd, SELECT_HANDLER_WRITE);
	} else {
		write_handler = NULL;
	}

	set_handlers(socket->fd, (select_handler_T) read_select, write_handler,
		     (select_handler_T) exception, socket);
}
Example #30
0
struct terminal *init_term(int fdin, int fdout, void (*root_window)(struct window *, struct event *, int))
{
    struct terminal *term;
    struct window *win;
    term = mem_alloc(sizeof (struct terminal));
    memset(term, 0, sizeof(struct terminal));
    term->fdin = fdin;
    term->fdout = fdout;
    term->master = term->fdout == get_output_handle();
    /*term->x = 0;
    term->y = 0;
    term->cx = 0;
    term->cy = 0;*/
    term->lcx = -1;
    term->lcy = -1;
    term->dirty = 1;
    term->redrawing = 0;
    term->blocked = -1;
    term->screen = DUMMY;
    term->last_screen = DUMMY;
    term->spec = &dumb_term;
    term->term[0] = 0;
    term->cwd[0] = 0;
    term->input_queue = DUMMY;
    term->qlen = 0;
    init_list(term->windows);
    win = mem_alloc(sizeof (struct window));
    win->handler = root_window;
    win->data = NULL;
    win->term = term;
    add_to_list(term->windows, win);
    /*alloc_term_screen(term, 80, 25);*/
    add_to_list(terminals, term);
    set_handlers(fdin, (void (*)(void *))in_term, NULL, (void (*)(void *))destroy_terminal, term);
    return term;
}