Skip to content

port ngx_udp_connect() to fix compilation errors with nginx 1.12.0+ #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
language: c

compilers:
- clang
- gcc

env:
# Mainline
- X_NGINX_VERSION=1.13.0
# Stable
- X_NGINX_VERSION=1.12.0
# Previous stable
- X_NGINX_VERSION=1.10.3

sudo: false
addons:
apt:
packages:
- libpcre3-dev
- libssl-dev
cache:
apt: true

install:
- wget -O - http://nginx.org/download/nginx-${X_NGINX_VERSION}.tar.gz | tar -xzf -
- git clone https://github.com/trungtm/nginx-statsd

script:
- cd nginx-${X_NGINX_VERSION}
- ./configure --with-debug --add-module=../nginx-statsd
- make
- objs/nginx -V
1 change: 1 addition & 0 deletions README.mkd
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
nginx-statsd
============
[![Build Status](https://travis-ci.org/trungtm/nginx-statsd.svg?branch=master)](https://travis-ci.org/trungtm/nginx-statsd)

An nginx module for sending statistics to statsd.

Expand Down
18 changes: 15 additions & 3 deletions config
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
ngx_addon_name=ngx_http_statsd
HTTP_MODULES="$HTTP_MODULES ngx_http_statsd_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_statsd.c"
CORE_LIBS="$CORE_LIBS -lssl"

if test -n "$ngx_module_link"; then
ngx_module_type=HTTP
ngx_module_name=ngx_http_statsd_module
ngx_module_srcs="$ngx_addon_dir/ngx_http_statsd.c"

. auto/module
else
HTTP_MODULES="$HTTP_MODULES ngx_http_statsd_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_statsd.c"
CORE_LIBS="$CORE_LIBS -lssl"
Copy link

@levb levb Jul 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you still need -lssl? I believe that USE_OPENSSL=YES already accomplishes that. Or is there an issue with older nginx versions?

I had to remove -lssl in my fork anyway since we try to link with openssl statically, and this -lssl was screwing it up on CentOS/RedHat.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. I just converted the old config to the new one
USE_OPENSSL=YES is better option.

fi

USE_OPENSSL=YES

88 changes: 85 additions & 3 deletions ngx_http_statsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ typedef struct {
ngx_array_t *stats;
} ngx_http_statsd_conf_t;

ngx_int_t ngx_udp_connect(ngx_resolver_connection_t *rec);

static void ngx_statsd_updater_cleanup(void *data);
static ngx_int_t ngx_http_statsd_udp_send(ngx_udp_endpoint_t *l, u_char *buf, size_t len);
Expand Down Expand Up @@ -350,6 +349,89 @@ static void ngx_http_statsd_udp_dummy_handler(ngx_event_t *ev)
{
}

static ngx_int_t
ngx_http_statsd_udp_connect(ngx_resolver_connection_t *rec)
{
int rc;
ngx_int_t event;
ngx_event_t *rev, *wev;
ngx_socket_t s;
ngx_connection_t *c;

s = ngx_socket(rec->sockaddr->sa_family, SOCK_DGRAM, 0);

ngx_log_debug1(NGX_LOG_DEBUG_EVENT, &rec->log, 0, "UDP socket %d", s);

if (s == (ngx_socket_t) -1) {
ngx_log_error(NGX_LOG_ALERT, &rec->log, ngx_socket_errno,
ngx_socket_n " failed");
return NGX_ERROR;
}

c = ngx_get_connection(s, &rec->log);

if (c == NULL) {
if (ngx_close_socket(s) == -1) {
ngx_log_error(NGX_LOG_ALERT, &rec->log, ngx_socket_errno,
ngx_close_socket_n "failed");
}

return NGX_ERROR;
}

if (ngx_nonblocking(s) == -1) {
ngx_log_error(NGX_LOG_ALERT, &rec->log, ngx_socket_errno,
ngx_nonblocking_n " failed");

goto failed;
}

rev = c->read;
wev = c->write;

rev->log = &rec->log;
wev->log = &rec->log;

rec->udp = c;

c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1);

ngx_log_debug3(NGX_LOG_DEBUG_EVENT, &rec->log, 0,
"connect to %V, fd:%d #%uA", &rec->server, s, c->number);

rc = connect(s, rec->sockaddr, rec->socklen);

/* TODO: iocp */

if (rc == -1) {
ngx_log_error(NGX_LOG_CRIT, &rec->log, ngx_socket_errno,
"connect() failed");

goto failed;
}

/* UDP sockets are always ready to write */
wev->ready = 1;

event = (ngx_event_flags & NGX_USE_CLEAR_EVENT) ?
/* kqueue, epoll */ NGX_CLEAR_EVENT:
/* select, poll, /dev/poll */ NGX_LEVEL_EVENT;
/* eventport event type has no meaning: oneshot only */

if (ngx_add_event(rev, NGX_READ_EVENT, event) != NGX_OK) {
goto failed;
}

return NGX_OK;

failed:

ngx_close_connection(c);
rec->udp = NULL;

return NGX_ERROR;
}

static ngx_int_t
ngx_http_statsd_udp_send(ngx_udp_endpoint_t *l, u_char *buf, size_t len)
{
Expand All @@ -364,7 +446,7 @@ ngx_http_statsd_udp_send(ngx_udp_endpoint_t *l, u_char *buf, size_t len)
rec->log.data = NULL;
rec->log.action = "logging";

if(ngx_udp_connect(rec) != NGX_OK) {
if(ngx_http_statsd_udp_connect(rec) != NGX_OK) {
if(rec->udp != NULL) {
ngx_free_connection(rec->udp);
rec->udp = NULL;
Expand Down Expand Up @@ -747,4 +829,4 @@ ngx_escape_statsd_key(u_char *dst, u_char *src, size_t size)
}

return (uintptr_t) dst;
}
}