~kolk/nginx_mptcp_module

ref: 14a6540671a9a6e7f5e3c1a3b508b9f1a2429ad1 nginx_mptcp_module/ngx_http_mptcp_module.c -rw-r--r-- 2.0 KiB
14a65406Kacper Kołodziej Initial version of MPTCP module for Nginx 1 year, 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 * Copyright (C) 2019 Kacper Kołodziej <kacper@kolodziej.it> https://kacperkolodziej.pl/
 */

#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <sys/socket.h>

extern ngx_module_t ngx_http_mptcp_module;

static ngx_int_t ngx_http_mptcp_variable(ngx_http_request_t *r,
		ngx_http_variable_value_t *v, uintptr_t data)
{
	int fd, result, mptcp_enabled;
#define MPTCP_ENABLED 42
	socklen_t mptcp_enabled_len = sizeof(mptcp_enabled);

	fd = r->connection->fd;
	result = getsockopt(fd, SOL_TCP, MPTCP_ENABLED, &mptcp_enabled, &mptcp_enabled_len);
	if (result == 0) {
		if (mptcp_enabled) {
			v->len = 2;
			v->data = (u_char*)"on";
			v->not_found = 0;
			v->valid = 1;
			v->no_cacheable = 1;
		} else {
			v->len = 3;
			v->data = (u_char*)"off";
			v->not_found = 0;
			v->valid = 1;
			v->no_cacheable = 1;
		}
	} else {
		v->not_found = 1;
	}

	return NGX_OK;
}

static ngx_str_t ngx_http_mptcp_enabled_name = ngx_string("mptcp_enabled");

static ngx_int_t ngx_http_mptcp_init(ngx_conf_t* cf) {
	ngx_http_variable_t *var;
	var = ngx_http_add_variable(cf, &ngx_http_mptcp_enabled_name,
			NGX_HTTP_VAR_NOCACHEABLE);
	if (var == NULL) {
		return NGX_ERROR;
	}

	var->get_handler = ngx_http_mptcp_variable;

	return NGX_OK;
}

static ngx_http_module_t ngx_http_mptcp_module_ctx = {
	ngx_http_mptcp_init,			/* preconfiguration */
	NULL,					/* postconfiguration */

	NULL,					/* create main configuration */
	NULL,					/* init main configuration */

	NULL,					/* create server configuration */
	NULL,					/* merge server configuration */

	NULL,					/* create location configuration */
	NULL 					/* merge location configuration */
};

ngx_module_t ngx_http_mptcp_module = {
	NGX_MODULE_V1,
	&ngx_http_mptcp_module_ctx,		/* module context */
	NULL,					/* module directives */ 
	NGX_HTTP_MODULE,			/* module type */
	NULL,					/* init master */
	NULL,					/* init module */
	NULL,					/* init process */
	NULL,					/* init thread */
	NULL,					/* exit thread */
	NULL,					/* exit process */
	NULL,					/* exit master */
	NGX_MODULE_V1_PADDING
};