int io_connect(struct io *io, const struct sockaddr *sa, const struct sockaddr *bsa) { int sock, errno_save; if ((sock = socket(sa->sa_family, SOCK_STREAM, 0)) == -1) goto fail; io_set_blocking(sock, 0); io_set_linger(sock, 0); if (bsa && bind(sock, bsa, bsa->sa_len) == -1) goto fail; if (connect(sock, sa, sa->sa_len) == -1) if (errno != EINPROGRESS) goto fail; io->sock = sock; io_reset(io, EV_WRITE, io_dispatch_connect); return (sock); fail: if (sock != -1) { errno_save = errno; close(sock); errno = errno_save; io->error = strerror(errno); } return (-1); }
static int sp_attach_io_channel(sp_t *sp, int fd) { return_val_if_fail(sp, -1); return_val_if_fail(fd != -1, -1); sp->fd = fd; sp->input = evbuffer_new(); io_set_blocking(fd, 0); return 0; }