Пример #1
0
Файл: io.c Проект: alyx/sigyn
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;
}
Пример #2
0
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;
}