Skip to content

Commit 6f51a40

Browse files
author
ionutrazvanionita
committed
[httpd] expose server and client socket info
1 parent 932683c commit 6f51a40

File tree

8 files changed

+46
-13
lines changed

8 files changed

+46
-13
lines changed

cachedb/cachedb_pool.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,19 @@
3030
#include "cachedb_id.h"
3131

3232
typedef struct cachedb_pool_con_t{
33+
/* IMPORTANT: A structure overwriting this one defined
34+
* in a module implementing this interface MUST always
35+
* also overwrite these 3 fields, have them in the
36+
* beginning
37+
* EXMAPLE: modules/cachedb_local/cachedb_local.h lcache_con
38+
* structure definition*/
3339
struct cachedb_id *id;
3440
unsigned int ref;
3541
struct cachedb_pool_con_t *next;
3642
} cachedb_pool_con;
3743

3844
cachedb_pool_con* cachedb_pool_get(struct cachedb_id* id);
39-
cachedb_pool_con** filter_pool_by_scheme(str *scheme,int *size);
45+
cachedb_pool_con** filter_pool_by_scheme(str *scheme,int *size);
4046
void cachedb_pool_insert(cachedb_pool_con *con);
4147
int cachedb_pool_remove(cachedb_pool_con *con);
4248

modules/httpd/httpd.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ str ip = {NULL, 0};
5858
str buffer = {NULL, 0};
5959
int post_buf_size = DEFAULT_POST_BUF_SIZE;
6060
struct httpd_cb *httpd_cb_list = NULL;
61+
union sockaddr_union httpd_server_info;
6162

6263

6364
static proc_export_t mi_procs[] = {
@@ -113,12 +114,21 @@ static int mod_init(void)
113114
{
114115
struct ip_addr *_ip;
115116

117+
memset(&httpd_server_info, 0, sizeof(union sockaddr_union));
118+
httpd_server_info.sin.sin_port = port;
119+
116120
if (ip.s) {
117121
ip.len = strlen(ip.s);
118122
if ( (_ip=str2ip(&ip)) == NULL ) {
119123
LM_ERR("invalid IP [%.*s]\n", ip.len, ip.s);
120124
return -1;
121125
}
126+
127+
httpd_server_info.sin.sin_addr.s_addr = _ip->u.addr32[0];
128+
httpd_server_info.sin.sin_family = _ip->af;
129+
} else {
130+
httpd_server_info.sin.sin_addr.s_addr = INADDR_LOOPBACK;
131+
httpd_server_info.sin.sin_family = AF_INET;
122132
}
123133

124134
if (post_buf_size < MIN_POST_BUF_SIZE) {
@@ -211,6 +221,7 @@ int httpd_bind(httpd_api_t *api)
211221

212222
api->lookup_arg = httpd_lookup_arg;
213223
api->register_httpdcb = httpd_register_httpdcb;
224+
api->get_server_info = httpd_get_server_info;
214225
return 0;
215226
}
216227

@@ -244,3 +255,4 @@ static struct mi_root* mi_list_root_path(struct mi_root* cmd, void* param)
244255
return NULL;
245256
}
246257

258+

modules/httpd/httpd_load.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ typedef int (httpd_acces_handler_cb) (void *cls, void *connection, const char *u
6868
const char *method, const char *version,
6969
const char *upload_data, size_t *upload_data_size,
7070
void **con_cls,
71-
str *buffer, str *page);
71+
str *buffer, str *page, union sockaddr_union* cl_socket);
7272

7373
/**
7474
* Callback used by httpd in order to obtain content. The
@@ -133,9 +133,13 @@ typedef int (*register_httpdcb_f)(const char *mod, str *root_path,
133133
httpd_flush_data_cb f2,
134134
httpd_init_proc_cb f3);
135135

136+
union sockaddr_union* httpd_get_server_info(void);
137+
typedef union sockaddr_union*(*get_server_info_f)(void);
138+
136139
typedef struct httpd_api {
137140
lookup_arg_f lookup_arg;
138141
register_httpdcb_f register_httpdcb;
142+
get_server_info_f get_server_info;
139143
}httpd_api_t;
140144

141145

modules/httpd/httpd_proc.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ extern str ip;
5454
extern str buffer;
5555
extern int post_buf_size;
5656
extern struct httpd_cb *httpd_cb_list;
57+
extern union sockaddr_union httpd_server_info;
5758

5859
static const str MI_HTTP_U_URL = str_init("<html><body>"
5960
"Unable to parse URL!</body></html>");
@@ -387,6 +388,11 @@ void httpd_lookup_arg(void *connection, const char *key,
387388
return;
388389
}
389390

391+
union sockaddr_union* httpd_get_server_info(void)
392+
{
393+
return &httpd_server_info;
394+
}
395+
390396

391397
int answer_to_connection (void *cls, struct MHD_Connection *connection,
392398
const char *url, const char *method,
@@ -406,6 +412,8 @@ int answer_to_connection (void *cls, struct MHD_Connection *connection,
406412
int accept_type = HTTPD_STD_CNT_TYPE;
407413
int ret_code = MHD_HTTP_OK;
408414

415+
union sockaddr_union* cl_socket;
416+
409417
LM_DBG("START *** cls=%p, connection=%p, url=%s, method=%s, "
410418
"versio=%s, upload_data[%zu]=%p, *con_cls=%p\n",
411419
cls, connection, url, method, version,
@@ -423,6 +431,9 @@ int answer_to_connection (void *cls, struct MHD_Connection *connection,
423431
pr = NULL;
424432
}
425433

434+
cl_socket = *(union sockaddr_union**)MHD_get_connection_info(connection,
435+
MHD_CONNECTION_INFO_CLIENT_ADDRESS);
436+
426437
if(strncmp(method, "POST", 4)==0) {
427438
if(pr == NULL){
428439
pr = *con_cls;
@@ -503,7 +514,7 @@ int answer_to_connection (void *cls, struct MHD_Connection *connection,
503514
normalised_url,
504515
method, version,
505516
upload_data, upload_data_size, con_cls,
506-
&buffer, &page);
517+
&buffer, &page, cl_socket);
507518
} else {
508519
page = MI_HTTP_U_URL;
509520
ret_code = MHD_HTTP_BAD_REQUEST;
@@ -589,7 +600,7 @@ int answer_to_connection (void *cls, struct MHD_Connection *connection,
589600
normalised_url,
590601
method, version,
591602
upload_data, upload_data_size, con_cls,
592-
&buffer, &page);
603+
&buffer, &page, cl_socket);
593604
} else {
594605
page = MI_HTTP_U_URL;
595606
ret_code = MHD_HTTP_BAD_REQUEST;
@@ -613,7 +624,7 @@ int answer_to_connection (void *cls, struct MHD_Connection *connection,
613624
normalised_url,
614625
method, version,
615626
upload_data, upload_data_size, con_cls,
616-
&buffer, &page);
627+
&buffer, &page, cl_socket);
617628
} else {
618629
page = MI_HTTP_U_URL;
619630
ret_code = MHD_HTTP_BAD_REQUEST;

modules/mi_http/mi_http.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ int mi_http_answer_to_connection (void *cls, void *connection,
4141
const char *url, const char *method,
4242
const char *version, const char *upload_data,
4343
size_t *upload_data_size, void **con_cls,
44-
str *buffer, str *page);
44+
str *buffer, str *page, union sockaddr_union* cl_socket);
4545
static ssize_t mi_http_flush_data(void *cls, uint64_t pos, char *buf, size_t max);
4646

4747
str http_root = str_init("mi");
@@ -252,7 +252,7 @@ int mi_http_answer_to_connection (void *cls, void *connection,
252252
const char *url, const char *method,
253253
const char *version, const char *upload_data,
254254
size_t *upload_data_size, void **con_cls,
255-
str *buffer, str *page)
255+
str *buffer, str *page, union sockaddr_union* cl_socket)
256256
{
257257
int mod = -1;
258258
int cmd = -1;

modules/mi_json/mi_json.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ int mi_json_answer_to_connection (void *cls, void *connection,
4141
const char *url, const char *method,
4242
const char *version, const char *upload_data,
4343
size_t *upload_data_size, void **con_cls,
44-
str *buffer, str *page);
44+
str *buffer, str *page, union sockaddr_union* cl_socket);
4545
static ssize_t mi_json_flush_data(void *cls, uint64_t pos, char *buf,
4646
size_t max);
4747

@@ -243,7 +243,7 @@ int mi_json_answer_to_connection (void *cls, void *connection,
243243
const char *url, const char *method,
244244
const char *version, const char *upload_data,
245245
size_t *upload_data_size, void **con_cls,
246-
str *buffer, str *page)
246+
str *buffer, str *page, union sockaddr_union* cl_socket)
247247
{
248248
str command = {NULL, 0};
249249
str params = {NULL, 0};

modules/mi_xmlrpc_ng/mi_xmlrpc_http.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ int mi_xmlrpc_http_answer_to_connection (void *cls, void *connection,
4141
const char *url, const char *method,
4242
const char *version, const char *upload_data,
4343
size_t *upload_data_size, void **con_cls,
44-
str *buffer, str *page);
44+
str *buffer, str *page, union sockaddr_union* cl_socket);
4545
static ssize_t mi_xmlrpc_http_flush_data(void *cls, uint64_t pos, char *buf, size_t max);
4646

4747
str http_root = str_init("RPC2");
@@ -256,7 +256,7 @@ int mi_xmlrpc_http_answer_to_connection (void *cls, void *connection,
256256
const char *url, const char *method,
257257
const char *version, const char *upload_data,
258258
size_t *upload_data_size, void **con_cls,
259-
str *buffer, str *page)
259+
str *buffer, str *page, union sockaddr_union* cl_socket)
260260
{
261261
str arg = {NULL, 0};
262262
struct mi_root *tree = NULL;

modules/pi_http/pi_http.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ int ph_answer_to_connection (void *cls, void *connection,
4747
const char *url, const char *method,
4848
const char *version, const char *upload_data,
4949
size_t *upload_data_size, void **con_cls,
50-
str *buffer, str *page);
50+
str *buffer, str *page, union sockaddr_union* cl_socket);
5151
static ssize_t ph_flush_data(void *cls, uint64_t pos, char *buf, size_t max);
5252
static struct mi_root *mi_framework_reload(struct mi_root* cmd, void* param);
5353

@@ -238,7 +238,7 @@ int ph_answer_to_connection (void *cls, void *connection,
238238
const char *url, const char *method,
239239
const char *version, const char *upload_data,
240240
size_t *upload_data_size, void **con_cls,
241-
str *buffer, str *page)
241+
str *buffer, str *page, union sockaddr_union* cl_socket)
242242
{
243243
int mod = -1;
244244
int cmd = -1;

0 commit comments

Comments
 (0)