void diplay_socket_information(struct evhttp_bound_socket *handle) {
	struct sockaddr_storage ss;
	evutil_socket_t fd;
	ev_socklen_t socklen = sizeof(ss);
	char addrbuf[128];
	void *inaddr;
	const char *addr;
	int got_port = -1;

	fd = evhttp_bound_socket_get_fd(handle);
	memset(&ss, 0, sizeof(ss));
	if (getsockname(fd, (struct sockaddr *)&ss, &socklen)) {
		perror("getsockname() failed");
		exit(1);
	}
	if (ss.ss_family == AF_INET) {
		got_port = ntohs(((struct sockaddr_in*)&ss)->sin_port);
		inaddr = &((struct sockaddr_in*)&ss)->sin_addr;
	} else {
		fprintf(stderr, "Weird address family %d\n", ss.ss_family);
		exit(1);
	}
	addr = evutil_inet_ntop(ss.ss_family, inaddr, addrbuf, sizeof(addrbuf));
	if (addr) {
		printf("Listening on %s:%d\n", addr, got_port);
		evutil_snprintf(uri_root, sizeof(uri_root),
			"http://%s:%d",addr,got_port);
	} else {
		fprintf(stderr, "evutil_inet_ntop failed\n");
		exit(1);
	}
}
示例#2
0
static int http_get_address(struct evhttp_bound_socket *handle, char *buffer, int buflen)
{
    /* Extract and display the address we're listening on. */
    struct sockaddr_storage ss;
    evutil_socket_t fd;
    ev_socklen_t socklen = sizeof(ss);
    char addrbuf[128];
    void *inaddr;
    const char *addr;
    int got_port = -1;

    memset(&ss, 0, sizeof(ss));

    fd = evhttp_bound_socket_get_fd(handle);
    if (getsockname(fd, (struct sockaddr *)&ss, &socklen))
    {
        return ERROR_FAIL;
    }

    if (ss.ss_family == AF_INET)
    {
        got_port = ntohs(((struct sockaddr_in*)&ss)->sin_port);
        inaddr = &((struct sockaddr_in*)&ss)->sin_addr;
    }
    else if (ss.ss_family == AF_INET6)
    {
        got_port = ntohs(((struct sockaddr_in6*)&ss)->sin6_port);
        inaddr = &((struct sockaddr_in6*)&ss)->sin6_addr;
    }
    else
    {
        fprintf(stderr, "Unexpected address family %d\n", ss.ss_family);
        return ERROR_FAIL;
    }

    addr = evutil_inet_ntop(ss.ss_family, inaddr, addrbuf, sizeof(addrbuf));
    if (addr)
    {
        evutil_snprintf(buffer, buflen, "http://%s:%d", addr,got_port);
    }
    else
    {
        fprintf(stderr, "evutil_inet_ntop failed\n");
        return ERROR_FAIL;
    }

    return S_OK;
}
示例#3
0
static struct evhttp *
http_setup(ev_uint16_t *pport)
{
	struct evhttp *myhttp;
	ev_uint16_t port;
	struct evhttp_bound_socket *sock;

	myhttp = evhttp_new(NULL);
	if (!myhttp)
		event_errx(1, "Could not start web server");

	/* Try a few different ports */
	sock = evhttp_bind_socket_with_handle(myhttp, "127.0.0.1", 0);
	if (!sock)
		event_errx(1, "Couldn't open web port");

	port = regress_get_socket_port(evhttp_bound_socket_get_fd(sock));

	*pport = port;
	return (myhttp);
}
示例#4
0
bool jw_httpsrv_create(struct event_base *evbase,
                       jw_httpsrv        *rethttpsrv,
                       jw_err            *err)
{
    bool retval = false;

    jw_httpsrv httpsrv = jw_data_malloc(sizeof(struct _jw_httpsrv));
    if (NULL == httpsrv)
    {
        JABBERWERX_ERROR(err, JW_ERR_NO_MEMORY);
        goto jw_httpsrv_create_done_label;
    }
    memset(httpsrv, 0, sizeof(struct _jw_httpsrv));

    httpsrv->http = evhttp_new(evbase);
    if (NULL == httpsrv->http)
    {
        JABBERWERX_ERROR(err, JW_ERR_NO_MEMORY);
        goto jw_httpsrv_create_done_label;
    }

    httpsrv->next_body = evbuffer_new();
    if (NULL == httpsrv->next_body)
    {
        JABBERWERX_ERROR(err, JW_ERR_NO_MEMORY);
        goto jw_httpsrv_create_done_label;
    }

    evhttp_set_gencb(httpsrv->http, _request_cb, httpsrv);

    struct evhttp_bound_socket *handle =
        evhttp_bind_socket_with_handle(httpsrv->http, "127.0.0.1", 0);
    if (NULL == handle)
    {
        jw_log(JW_LOG_WARN, "failed to bind http server to port");
        JABBERWERX_ERROR(err, JW_ERR_INVALID_STATE);
        goto jw_httpsrv_create_done_label;
    }

    evutil_socket_t fd = evhttp_bound_socket_get_fd(handle);
    struct sockaddr_storage ss;
    ev_socklen_t socklen = sizeof(ss);
    memset(&ss, 0, sizeof(ss));

    if (0 != getsockname(fd, (struct sockaddr *)&ss, &socklen))
    {
        assert(false);
    }
    if (AF_INET == ss.ss_family)
    {
        httpsrv->port = ntohs(((struct sockaddr_in*)&ss)->sin_port);
    }
    else if (AF_INET6 == ss.ss_family)
    {
        httpsrv->port = ntohs(((struct sockaddr_in6*)&ss)->sin6_port);
    }
    else
    {
        assert(false);
    }

    *rethttpsrv = httpsrv;

    jw_log(JW_LOG_VERBOSE,
           "successfully started httpsrv on 127.0.0.1:%u", httpsrv->port);

    retval = true;

jw_httpsrv_create_done_label:
    if (!retval && httpsrv) { jw_httpsrv_destroy(httpsrv); }
    return retval;
}
示例#5
0
int
main(int argc, char **argv)
{
	struct event_base *base;
	struct evhttp *http;
	struct evhttp_bound_socket *handle;

	unsigned short port = atoi(argv[1]);
#ifdef WIN32
	WSADATA WSAData;
	WSAStartup(0x101, &WSAData);
#else
	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
		return (1);
#endif
	base = event_base_new();
	if (!base) {
		fprintf(stderr, "Couldn't create an event_base: exiting\n");
		return 1;
	}

	/* Create a new evhttp object to handle requests. */
	http = evhttp_new(base);
	if (!http) {
		fprintf(stderr, "couldn't create evhttp. Exiting.\n");
		return 1;
	}

	evhttp_set_cb(http, "/s2cover", s2cover_request_cb, NULL);
	evhttp_set_cb(http, "/s2info", s2info_request_cb, NULL);
	evhttp_set_cb(http, "/fetch", fetch_request_cb, NULL);

	/* Now we tell the evhttp what port to listen on */
	handle = evhttp_bind_socket_with_handle(http, "0.0.0.0", port);
	if (!handle) {
		fprintf(stderr, "couldn't bind to port %d. Exiting.\n",
		    (int)port);
		return 1;
	}

	{
		/* Extract and display the address we're listening on. */
		struct sockaddr_storage ss;
		evutil_socket_t fd;
		ev_socklen_t socklen = sizeof(ss);
		char addrbuf[128];
		void *inaddr;
		const char *addr;
		int got_port = -1;
		fd = evhttp_bound_socket_get_fd(handle);
		memset(&ss, 0, sizeof(ss));
		if (getsockname(fd, (struct sockaddr *)&ss, &socklen)) {
			perror("getsockname() failed");
			return 1;
		}
		if (ss.ss_family == AF_INET) {
			got_port = ntohs(((struct sockaddr_in*)&ss)->sin_port);
			inaddr = &((struct sockaddr_in*)&ss)->sin_addr;
		} else if (ss.ss_family == AF_INET6) {
			got_port = ntohs(((struct sockaddr_in6*)&ss)->sin6_port);
			inaddr = &((struct sockaddr_in6*)&ss)->sin6_addr;
		} else {
			fprintf(stderr, "Weird address family %d\n",
			    ss.ss_family);
			return 1;
		}
		addr = evutil_inet_ntop(ss.ss_family, inaddr, addrbuf,
		    sizeof(addrbuf));
		if (addr) {
			printf("HI Listening on %s:%d\n", addr, got_port);
			evutil_snprintf(uri_root, sizeof(uri_root),
			    "http://%s:%d",addr,got_port);
		} else {
			fprintf(stderr, "evutil_inet_ntop failed\n");
			return 1;
		}
	}

	event_base_dispatch(base);

	return 0;
}