mowgli_linebuf_t * new_conn(const char * host, const char * port, bool use_ssl, mowgli_linebuf_readline_cb_t *cb, void * udata) { struct addrinfo hints, * res; mowgli_vio_sockaddr_t addr; mowgli_linebuf_t * linebuf; int ret; linebuf = mowgli_linebuf_create(cb, udata); memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; if ((ret = getaddrinfo(host, port, &hints, &res)) != 0) { linebuf->vio->error.op = MOWGLI_VIO_ERR_OP_OTHER; linebuf->vio->error.type = MOWGLI_VIO_ERR_ERRCODE; linebuf->vio->error.code = ret; mowgli_strlcpy(linebuf->vio->error.string, gai_strerror(ret), sizeof(linebuf->vio->error.string)); mowgli_vio_error(linebuf->vio); return NULL; } if (use_ssl) if (mowgli_vio_openssl_setssl(linebuf->vio, NULL, NULL) != 0) return NULL; if (mowgli_vio_socket(linebuf->vio, res->ai_family, res->ai_socktype, res->ai_protocol) != 0) return NULL; mowgli_linebuf_attach_to_eventloop(linebuf, me.ev); if (mowgli_vio_connect(linebuf->vio, mowgli_vio_sockaddr_from_struct(&addr, res->ai_addr, res->ai_addrlen)) != 0) return NULL; return linebuf; }
int mowgli_vio_openssl_default_accept(mowgli_vio_t *vio, mowgli_vio_t *newvio) { const int fd = mowgli_vio_getfd(vio); int afd; int ret; return_val_if_fail(fd != -1, -255); mowgli_ssl_connection_t *connection = vio->privdata; mowgli_ssl_connection_t *newconnection; vio->error.op = MOWGLI_VIO_ERR_OP_ACCEPT; if (!newvio) { const char errstr[] = "accept not called with valid new VIO object"; vio->error.type = MOWGLI_VIO_ERR_API; mowgli_strlcpy(vio->error.string, errstr, sizeof(errstr)); return mowgli_vio_error(vio); } if ((afd = accept(fd, (struct sockaddr *) &newvio->addr.addr, &(newvio->addr.addrlen))) < 0) { if (!mowgli_eventloop_ignore_errno(errno)) return mowgli_vio_err_errcode(vio, strerror, errno); else return 0; } newvio->io.fd = afd; mowgli_vio_openssl_setssl(newvio, &connection->settings, vio->ops); newconnection = newvio->privdata; newconnection->ssl_context = connection->ssl_context; newconnection->ssl_handle = SSL_new(newconnection->ssl_context); if (!SSL_set_fd(newconnection->ssl_handle, afd)) return mowgli_vio_err_sslerrcode(newvio, ERR_get_error()); if ((ret = SSL_accept(newconnection->ssl_handle)) != 1) { unsigned long err; switch (SSL_get_error(newconnection->ssl_handle, ret)) { case SSL_ERROR_WANT_READ: mowgli_vio_setflag(vio, MOWGLI_VIO_FLAGS_NEEDREAD, true); MOWGLI_VIO_SETREAD(vio) return 0; case SSL_ERROR_WANT_WRITE: mowgli_vio_setflag(vio, MOWGLI_VIO_FLAGS_NEEDWRITE, true); MOWGLI_VIO_SETWRITE(vio) return 0; case SSL_ERROR_ZERO_RETURN: return 0; case SSL_ERROR_SYSCALL: return mowgli_vio_err_errcode(newvio, strerror, errno); default: err = ERR_get_error(); break; } if (err > 0) { errno = EIO; return mowgli_vio_err_errcode(vio, strerror, errno); } return -1; }
static int net_connect(struct a_network *net, char *h_name, mowgli_vio_sockaddr_t *addr) { char buf[65536]; net->linebuf = mowgli_linebuf_create(readline_cb, net); net->linebuf->shutdown_cb = shutdown_cb; net->linebuf->delim = "\n"; if (net->state == NET_CONNECTED) { a_log(LERROR, "Attempt to connect already-connected network"); return; } if (addr->addr.ss_family == AF_INET) { struct sockaddr_in *addr4 = (void*)&addr->addr; addr4->sin_port = htons(net->port); addr->addrlen = sizeof(struct sockaddr_in); inet_ntop(AF_INET, &addr4->sin_addr, buf, 65536); a_log(LDEBUG, "Connecting IPv4 %s/%d", buf, net->port); } else if (addr->addr.ss_family == AF_INET6) { struct sockaddr_in6 *addr6 = (void*)&addr->addr; addr6->sin6_port = net->port; addr->addrlen = sizeof(struct sockaddr_in); inet_ntop(AF_INET6, &addr6->sin6_addr, buf, 65536); a_log(LDEBUG, "Connecting IPv6 %s/%d", buf, net->port); } else { a_log(LERROR, "Unknown address family %d for %s (%s)", addr->addr.ss_family, h_name, net->name); return -1; } a_log(LNOTICE, "Connecting to %s (%s)", h_name, net->name); if (net->use_ssl) { if (mowgli_vio_openssl_setssl(net->linebuf->vio, NULL, NULL) != 0) { a_log(LERROR, "Failed to set SSL on %s", net->name); return -1; } } if (mowgli_vio_socket(net->linebuf->vio, AF_INET, SOCK_STREAM, 0) != 0) { a_log(LERROR, "Failed to create socket for %s", net->name); return -1; } mowgli_linebuf_attach_to_eventloop(net->linebuf, alicorn.eventloop); if (mowgli_vio_connect(net->linebuf->vio, addr) != 0) { a_log(LERROR, "Failed to connect to %s (%s)", h_name, net->name); return -1; } net->state = NET_CONNECTED; net_do_register(net); return 0; }