Mercurial > hg > nginx
comparison src/stream/ngx_stream_variables.c @ 6608:eb4293155e87
Stream: core module variables.
author | Vladimir Homutov <vl@nginx.com> |
---|---|
date | Tue, 14 Jun 2016 18:28:14 +0300 |
parents | c70b7f4537e1 |
children | 5e2821c2de46 |
comparison
equal
deleted
inserted
replaced
6607:c70b7f4537e1 | 6608:eb4293155e87 |
---|---|
9 #include <ngx_core.h> | 9 #include <ngx_core.h> |
10 #include <ngx_stream.h> | 10 #include <ngx_stream.h> |
11 #include <nginx.h> | 11 #include <nginx.h> |
12 | 12 |
13 | 13 |
14 static ngx_int_t ngx_stream_variable_binary_remote_addr( | |
15 ngx_stream_session_t *s, ngx_stream_variable_value_t *v, uintptr_t data); | |
16 static ngx_int_t ngx_stream_variable_remote_addr(ngx_stream_session_t *s, | |
17 ngx_stream_variable_value_t *v, uintptr_t data); | |
18 static ngx_int_t ngx_stream_variable_remote_port(ngx_stream_session_t *s, | |
19 ngx_stream_variable_value_t *v, uintptr_t data); | |
20 static ngx_int_t ngx_stream_variable_server_addr(ngx_stream_session_t *s, | |
21 ngx_stream_variable_value_t *v, uintptr_t data); | |
22 static ngx_int_t ngx_stream_variable_server_port(ngx_stream_session_t *s, | |
23 ngx_stream_variable_value_t *v, uintptr_t data); | |
24 static ngx_int_t ngx_stream_variable_bytes_sent(ngx_stream_session_t *s, | |
25 ngx_stream_variable_value_t *v, uintptr_t data); | |
26 static ngx_int_t ngx_stream_variable_connection(ngx_stream_session_t *s, | |
27 ngx_stream_variable_value_t *v, uintptr_t data); | |
28 | |
14 static ngx_int_t ngx_stream_variable_nginx_version(ngx_stream_session_t *s, | 29 static ngx_int_t ngx_stream_variable_nginx_version(ngx_stream_session_t *s, |
15 ngx_stream_variable_value_t *v, uintptr_t data); | 30 ngx_stream_variable_value_t *v, uintptr_t data); |
31 static ngx_int_t ngx_stream_variable_hostname(ngx_stream_session_t *s, | |
32 ngx_stream_variable_value_t *v, uintptr_t data); | |
33 static ngx_int_t ngx_stream_variable_pid(ngx_stream_session_t *s, | |
34 ngx_stream_variable_value_t *v, uintptr_t data); | |
35 static ngx_int_t ngx_stream_variable_msec(ngx_stream_session_t *s, | |
36 ngx_stream_variable_value_t *v, uintptr_t data); | |
37 static ngx_int_t ngx_stream_variable_time_iso8601(ngx_stream_session_t *s, | |
38 ngx_stream_variable_value_t *v, uintptr_t data); | |
39 static ngx_int_t ngx_stream_variable_time_local(ngx_stream_session_t *s, | |
40 ngx_stream_variable_value_t *v, uintptr_t data); | |
16 | 41 |
17 | 42 |
18 static ngx_stream_variable_t ngx_stream_core_variables[] = { | 43 static ngx_stream_variable_t ngx_stream_core_variables[] = { |
44 | |
45 { ngx_string("binary_remote_addr"), NULL, | |
46 ngx_stream_variable_binary_remote_addr, 0, 0, 0 }, | |
47 | |
48 { ngx_string("remote_addr"), NULL, | |
49 ngx_stream_variable_remote_addr, 0, 0, 0 }, | |
50 | |
51 { ngx_string("remote_port"), NULL, | |
52 ngx_stream_variable_remote_port, 0, 0, 0 }, | |
53 | |
54 { ngx_string("server_addr"), NULL, | |
55 ngx_stream_variable_server_addr, 0, 0, 0 }, | |
56 | |
57 { ngx_string("server_port"), NULL, | |
58 ngx_stream_variable_server_port, 0, 0, 0 }, | |
59 | |
60 { ngx_string("bytes_sent"), NULL, ngx_stream_variable_bytes_sent, | |
61 0, 0, 0 }, | |
62 | |
63 { ngx_string("connection"), NULL, | |
64 ngx_stream_variable_connection, 0, 0, 0 }, | |
19 | 65 |
20 { ngx_string("nginx_version"), NULL, ngx_stream_variable_nginx_version, | 66 { ngx_string("nginx_version"), NULL, ngx_stream_variable_nginx_version, |
21 0, 0, 0 }, | 67 0, 0, 0 }, |
68 | |
69 { ngx_string("hostname"), NULL, ngx_stream_variable_hostname, | |
70 0, 0, 0 }, | |
71 | |
72 { ngx_string("pid"), NULL, ngx_stream_variable_pid, | |
73 0, 0, 0 }, | |
74 | |
75 { ngx_string("msec"), NULL, ngx_stream_variable_msec, | |
76 0, NGX_STREAM_VAR_NOCACHEABLE, 0 }, | |
77 | |
78 { ngx_string("time_iso8601"), NULL, ngx_stream_variable_time_iso8601, | |
79 0, NGX_STREAM_VAR_NOCACHEABLE, 0 }, | |
80 | |
81 { ngx_string("time_local"), NULL, ngx_stream_variable_time_local, | |
82 0, NGX_STREAM_VAR_NOCACHEABLE, 0 }, | |
22 | 83 |
23 { ngx_null_string, NULL, NULL, 0, 0, 0 } | 84 { ngx_null_string, NULL, NULL, 0, 0, 0 } |
24 }; | 85 }; |
25 | 86 |
26 | 87 |
257 return vv; | 318 return vv; |
258 } | 319 } |
259 | 320 |
260 | 321 |
261 static ngx_int_t | 322 static ngx_int_t |
323 ngx_stream_variable_binary_remote_addr(ngx_stream_session_t *s, | |
324 ngx_stream_variable_value_t *v, uintptr_t data) | |
325 { | |
326 struct sockaddr_in *sin; | |
327 #if (NGX_HAVE_INET6) | |
328 struct sockaddr_in6 *sin6; | |
329 #endif | |
330 | |
331 switch (s->connection->sockaddr->sa_family) { | |
332 | |
333 #if (NGX_HAVE_INET6) | |
334 case AF_INET6: | |
335 sin6 = (struct sockaddr_in6 *) s->connection->sockaddr; | |
336 | |
337 v->len = sizeof(struct in6_addr); | |
338 v->valid = 1; | |
339 v->no_cacheable = 0; | |
340 v->not_found = 0; | |
341 v->data = sin6->sin6_addr.s6_addr; | |
342 | |
343 break; | |
344 #endif | |
345 | |
346 default: /* AF_INET */ | |
347 sin = (struct sockaddr_in *) s->connection->sockaddr; | |
348 | |
349 v->len = sizeof(in_addr_t); | |
350 v->valid = 1; | |
351 v->no_cacheable = 0; | |
352 v->not_found = 0; | |
353 v->data = (u_char *) &sin->sin_addr; | |
354 | |
355 break; | |
356 } | |
357 | |
358 return NGX_OK; | |
359 } | |
360 | |
361 | |
362 static ngx_int_t | |
363 ngx_stream_variable_remote_addr(ngx_stream_session_t *s, | |
364 ngx_stream_variable_value_t *v, uintptr_t data) | |
365 { | |
366 v->len = s->connection->addr_text.len; | |
367 v->valid = 1; | |
368 v->no_cacheable = 0; | |
369 v->not_found = 0; | |
370 v->data = s->connection->addr_text.data; | |
371 | |
372 return NGX_OK; | |
373 } | |
374 | |
375 | |
376 static ngx_int_t | |
377 ngx_stream_variable_remote_port(ngx_stream_session_t *s, | |
378 ngx_stream_variable_value_t *v, uintptr_t data) | |
379 { | |
380 ngx_uint_t port; | |
381 | |
382 v->len = 0; | |
383 v->valid = 1; | |
384 v->no_cacheable = 0; | |
385 v->not_found = 0; | |
386 | |
387 v->data = ngx_pnalloc(s->connection->pool, sizeof("65535") - 1); | |
388 if (v->data == NULL) { | |
389 return NGX_ERROR; | |
390 } | |
391 | |
392 port = ngx_inet_get_port(s->connection->sockaddr); | |
393 | |
394 if (port > 0 && port < 65536) { | |
395 v->len = ngx_sprintf(v->data, "%ui", port) - v->data; | |
396 } | |
397 | |
398 return NGX_OK; | |
399 } | |
400 | |
401 | |
402 static ngx_int_t | |
403 ngx_stream_variable_server_addr(ngx_stream_session_t *s, | |
404 ngx_stream_variable_value_t *v, uintptr_t data) | |
405 { | |
406 ngx_str_t str; | |
407 u_char addr[NGX_SOCKADDR_STRLEN]; | |
408 | |
409 str.len = NGX_SOCKADDR_STRLEN; | |
410 str.data = addr; | |
411 | |
412 if (ngx_connection_local_sockaddr(s->connection, &str, 0) != NGX_OK) { | |
413 return NGX_ERROR; | |
414 } | |
415 | |
416 str.data = ngx_pnalloc(s->connection->pool, str.len); | |
417 if (str.data == NULL) { | |
418 return NGX_ERROR; | |
419 } | |
420 | |
421 ngx_memcpy(str.data, addr, str.len); | |
422 | |
423 v->len = str.len; | |
424 v->valid = 1; | |
425 v->no_cacheable = 0; | |
426 v->not_found = 0; | |
427 v->data = str.data; | |
428 | |
429 return NGX_OK; | |
430 } | |
431 | |
432 | |
433 static ngx_int_t | |
434 ngx_stream_variable_server_port(ngx_stream_session_t *s, | |
435 ngx_stream_variable_value_t *v, uintptr_t data) | |
436 { | |
437 ngx_uint_t port; | |
438 | |
439 v->len = 0; | |
440 v->valid = 1; | |
441 v->no_cacheable = 0; | |
442 v->not_found = 0; | |
443 | |
444 if (ngx_connection_local_sockaddr(s->connection, NULL, 0) != NGX_OK) { | |
445 return NGX_ERROR; | |
446 } | |
447 | |
448 v->data = ngx_pnalloc(s->connection->pool, sizeof("65535") - 1); | |
449 if (v->data == NULL) { | |
450 return NGX_ERROR; | |
451 } | |
452 | |
453 port = ngx_inet_get_port(s->connection->local_sockaddr); | |
454 | |
455 if (port > 0 && port < 65536) { | |
456 v->len = ngx_sprintf(v->data, "%ui", port) - v->data; | |
457 } | |
458 | |
459 return NGX_OK; | |
460 } | |
461 | |
462 | |
463 static ngx_int_t | |
464 ngx_stream_variable_bytes_sent(ngx_stream_session_t *s, | |
465 ngx_stream_variable_value_t *v, uintptr_t data) | |
466 { | |
467 u_char *p; | |
468 | |
469 p = ngx_pnalloc(s->connection->pool, NGX_OFF_T_LEN); | |
470 if (p == NULL) { | |
471 return NGX_ERROR; | |
472 } | |
473 | |
474 v->len = ngx_sprintf(p, "%O", s->connection->sent) - p; | |
475 v->valid = 1; | |
476 v->no_cacheable = 0; | |
477 v->not_found = 0; | |
478 v->data = p; | |
479 | |
480 return NGX_OK; | |
481 } | |
482 | |
483 | |
484 static ngx_int_t | |
485 ngx_stream_variable_connection(ngx_stream_session_t *s, | |
486 ngx_stream_variable_value_t *v, uintptr_t data) | |
487 { | |
488 u_char *p; | |
489 | |
490 p = ngx_pnalloc(s->connection->pool, NGX_ATOMIC_T_LEN); | |
491 if (p == NULL) { | |
492 return NGX_ERROR; | |
493 } | |
494 | |
495 v->len = ngx_sprintf(p, "%uA", s->connection->number) - p; | |
496 v->valid = 1; | |
497 v->no_cacheable = 0; | |
498 v->not_found = 0; | |
499 v->data = p; | |
500 | |
501 return NGX_OK; | |
502 } | |
503 | |
504 | |
505 static ngx_int_t | |
262 ngx_stream_variable_nginx_version(ngx_stream_session_t *s, | 506 ngx_stream_variable_nginx_version(ngx_stream_session_t *s, |
263 ngx_stream_variable_value_t *v, uintptr_t data) | 507 ngx_stream_variable_value_t *v, uintptr_t data) |
264 { | 508 { |
265 v->len = sizeof(NGINX_VERSION) - 1; | 509 v->len = sizeof(NGINX_VERSION) - 1; |
266 v->valid = 1; | 510 v->valid = 1; |
267 v->no_cacheable = 0; | 511 v->no_cacheable = 0; |
268 v->not_found = 0; | 512 v->not_found = 0; |
269 v->data = (u_char *) NGINX_VERSION; | 513 v->data = (u_char *) NGINX_VERSION; |
514 | |
515 return NGX_OK; | |
516 } | |
517 | |
518 | |
519 static ngx_int_t | |
520 ngx_stream_variable_hostname(ngx_stream_session_t *s, | |
521 ngx_stream_variable_value_t *v, uintptr_t data) | |
522 { | |
523 v->len = ngx_cycle->hostname.len; | |
524 v->valid = 1; | |
525 v->no_cacheable = 0; | |
526 v->not_found = 0; | |
527 v->data = ngx_cycle->hostname.data; | |
528 | |
529 return NGX_OK; | |
530 } | |
531 | |
532 | |
533 static ngx_int_t | |
534 ngx_stream_variable_pid(ngx_stream_session_t *s, | |
535 ngx_stream_variable_value_t *v, uintptr_t data) | |
536 { | |
537 u_char *p; | |
538 | |
539 p = ngx_pnalloc(s->connection->pool, NGX_INT64_LEN); | |
540 if (p == NULL) { | |
541 return NGX_ERROR; | |
542 } | |
543 | |
544 v->len = ngx_sprintf(p, "%P", ngx_pid) - p; | |
545 v->valid = 1; | |
546 v->no_cacheable = 0; | |
547 v->not_found = 0; | |
548 v->data = p; | |
549 | |
550 return NGX_OK; | |
551 } | |
552 | |
553 | |
554 static ngx_int_t | |
555 ngx_stream_variable_msec(ngx_stream_session_t *s, | |
556 ngx_stream_variable_value_t *v, uintptr_t data) | |
557 { | |
558 u_char *p; | |
559 ngx_time_t *tp; | |
560 | |
561 p = ngx_pnalloc(s->connection->pool, NGX_TIME_T_LEN + 4); | |
562 if (p == NULL) { | |
563 return NGX_ERROR; | |
564 } | |
565 | |
566 tp = ngx_timeofday(); | |
567 | |
568 v->len = ngx_sprintf(p, "%T.%03M", tp->sec, tp->msec) - p; | |
569 v->valid = 1; | |
570 v->no_cacheable = 0; | |
571 v->not_found = 0; | |
572 v->data = p; | |
573 | |
574 return NGX_OK; | |
575 } | |
576 | |
577 | |
578 static ngx_int_t | |
579 ngx_stream_variable_time_iso8601(ngx_stream_session_t *s, | |
580 ngx_stream_variable_value_t *v, uintptr_t data) | |
581 { | |
582 u_char *p; | |
583 | |
584 p = ngx_pnalloc(s->connection->pool, ngx_cached_http_log_iso8601.len); | |
585 if (p == NULL) { | |
586 return NGX_ERROR; | |
587 } | |
588 | |
589 ngx_memcpy(p, ngx_cached_http_log_iso8601.data, | |
590 ngx_cached_http_log_iso8601.len); | |
591 | |
592 v->len = ngx_cached_http_log_iso8601.len; | |
593 v->valid = 1; | |
594 v->no_cacheable = 0; | |
595 v->not_found = 0; | |
596 v->data = p; | |
597 | |
598 return NGX_OK; | |
599 } | |
600 | |
601 | |
602 static ngx_int_t | |
603 ngx_stream_variable_time_local(ngx_stream_session_t *s, | |
604 ngx_stream_variable_value_t *v, uintptr_t data) | |
605 { | |
606 u_char *p; | |
607 | |
608 p = ngx_pnalloc(s->connection->pool, ngx_cached_http_log_time.len); | |
609 if (p == NULL) { | |
610 return NGX_ERROR; | |
611 } | |
612 | |
613 ngx_memcpy(p, ngx_cached_http_log_time.data, ngx_cached_http_log_time.len); | |
614 | |
615 v->len = ngx_cached_http_log_time.len; | |
616 v->valid = 1; | |
617 v->no_cacheable = 0; | |
618 v->not_found = 0; | |
619 v->data = p; | |
270 | 620 |
271 return NGX_OK; | 621 return NGX_OK; |
272 } | 622 } |
273 | 623 |
274 | 624 |