service to intercept and modify DNS queries (and replies) of this system More...
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_protocols.h"
#include "dns.h"
#include "gnunet_dns_service.h"
#include "gnunet_statistics_service.h"
Go to the source code of this file.
Data Structures | |
struct | ClientRecord |
Entry we keep for each client. More... | |
struct | RequestRecord |
Entry we keep for each active request. More... | |
Macros | |
#define | DNS_PORT 53 |
Port number for DNS. More... | |
#define | LOG(kind, ...) GNUNET_log_from (kind, "dns", __VA_ARGS__); |
Generic logging shorthand. More... | |
Enumerations | |
enum | RequestPhase { RP_INIT , RP_REQUEST_MONITOR , RP_QUERY , RP_INTERNET_DNS , RP_MODIFY , RP_RESPONSE_MONITOR , RP_DROP } |
Phases each request goes through. More... | |
Functions | |
static void | cleanup_rr (struct RequestRecord *rr) |
We're done processing a DNS request, free associated memory. More... | |
static void | cleanup_task (void *cls) |
Task run during shutdown. More... | |
static void | request_done (struct RequestRecord *rr) |
We're done with some request, finish processing. More... | |
static void | send_request_to_client (struct RequestRecord *rr, struct ClientRecord *cr) |
Show the payload of the given request record to the client (and wait for a response). More... | |
static void | process_dns_result (void *cls, const struct GNUNET_TUN_DnsHeader *dns, size_t r) |
Callback called from DNSSTUB resolver when a resolution succeeded. More... | |
static void | next_phase (struct RequestRecord *rr) |
A client has completed its processing for this request. More... | |
static void * | client_connect_cb (void *cls, struct GNUNET_SERVICE_Client *client, struct GNUNET_MQ_Handle *mq) |
A client connected, setup our data structures. More... | |
static void | client_disconnect_cb (void *cls, struct GNUNET_SERVICE_Client *client, void *app_ctx) |
A client disconnected, clean up after it. More... | |
static void | handle_client_init (void *cls, const struct GNUNET_DNS_Register *reg) |
We got a new client. More... | |
static int | check_client_response (void *cls, const struct GNUNET_DNS_Response *resp) |
Check a response from a client. More... | |
static void | handle_client_response (void *cls, const struct GNUNET_DNS_Response *resp) |
Handle a response from a client. More... | |
static int | process_helper_messages (void *cls, const struct GNUNET_MessageHeader *message) |
Functions with this signature are called whenever a complete message is received by the tokenizer from the DNS hijack process. More... | |
static void | run (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg_, struct GNUNET_SERVICE_Handle *service) |
GNUNET_SERVICE_MAIN (GNUNET_OS_project_data_gnunet(), "dns", GNUNET_SERVICE_OPTION_NONE, &run, &client_connect_cb, &client_disconnect_cb, NULL, GNUNET_MQ_hd_fixed_size(client_init, GNUNET_MESSAGE_TYPE_DNS_CLIENT_INIT, struct GNUNET_DNS_Register, NULL), GNUNET_MQ_hd_var_size(client_response, GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE, struct GNUNET_DNS_Response, NULL), GNUNET_MQ_handler_end()) | |
Define "main" method using service macro. More... | |
Variables | |
static int | global_ret |
Global return value from 'main'. More... | |
static const struct GNUNET_CONFIGURATION_Handle * | cfg |
The configuration to use. More... | |
static struct GNUNET_STATISTICS_Handle * | stats |
Statistics. More... | |
static struct GNUNET_HELPER_Handle * | hijacker |
Handle to DNS hijacker helper process ("gnunet-helper-dns"). More... | |
static char * | helper_argv [8] |
Command-line arguments we are giving to the hijacker process. More... | |
static struct ClientRecord * | clients_head |
Head of DLL of clients we consult. More... | |
static struct ClientRecord * | clients_tail |
Tail of DLL of clients we consult. More... | |
static struct RequestRecord | requests [UINT16_MAX+1] |
Array of all open requests. More... | |
static uint64_t | request_id_gen |
Generator for unique request IDs. More... | |
static struct GNUNET_DNSSTUB_Context * | dnsstub |
Handle to the DNS Stub resolver. More... | |
service to intercept and modify DNS queries (and replies) of this system
For "secure" interaction with the legacy DNS system, we permit replies only to arrive within a 5s window (and they must match ports, IPs and request IDs). Furthermore, we let the OS pick a source port, opening up to 128 sockets per address family (IPv4 or IPv6). Those sockets are closed if they are not in use for 5s (which means they will be freshly randomized afterwards). For new requests, we pick a random slot in the array with 128 socket slots (and reuse an existing socket if the slot is still in use). Thus each request will be given one of 128 random source ports, and the 128 random source ports will also change "often" (less often if the system is very busy, each time if we are mostly idle). At the same time, the system will never use more than 256 UDP sockets.
Definition in file gnunet-service-dns.c.
#define DNS_PORT 53 |
Port number for DNS.
Definition at line 49 of file gnunet-service-dns.c.
#define LOG | ( | kind, | |
... | |||
) | GNUNET_log_from (kind, "dns", __VA_ARGS__); |
Generic logging shorthand.
Definition at line 55 of file gnunet-service-dns.c.
enum RequestPhase |
Phases each request goes through.
Enumerator | |
---|---|
RP_INIT | Request has just been received. |
RP_REQUEST_MONITOR | Showing the request to all monitor clients. If client list is empty, will enter QUERY phase. |
RP_QUERY | Showing the request to PRE-RESOLUTION clients to find an answer. If client list is empty, will trigger global DNS request. |
RP_INTERNET_DNS | Global Internet query is now pending. |
RP_MODIFY | Client (or global DNS request) has resulted in a response. Forward to all POST-RESOLUTION clients. If client list is empty, will enter RESPONSE_MONITOR phase. |
RP_RESPONSE_MONITOR | Showing the request to all monitor clients. If client list is empty, give the result to the hijacker (and be done). |
RP_DROP | Some client has told us to drop the request. |
Definition at line 62 of file gnunet-service-dns.c.
|
static |
We're done processing a DNS request, free associated memory.
rr | request to clean up |
Definition at line 252 of file gnunet-service-dns.c.
References RequestRecord::client_wait_list, RequestRecord::client_wait_list_length, GNUNET_array_grow, GNUNET_free, RequestRecord::payload, and RequestRecord::payload_length.
Referenced by cleanup_task(), next_phase(), request_done(), and send_request_to_client().
|
static |
Task run during shutdown.
cls | unused |
Definition at line 269 of file gnunet-service-dns.c.
References cleanup_rr(), dnsstub, GNUNET_DNSSTUB_stop(), GNUNET_free, GNUNET_HELPER_stop(), GNUNET_NO, GNUNET_STATISTICS_destroy(), helper_argv, hijacker, requests, and stats.
Referenced by run().
|
static |
We're done with some request, finish processing.
rr | request send to the network or just clean up. |
Definition at line 300 of file gnunet-service-dns.c.
References cleanup_rr(), RequestRecord::client_wait_list, RequestRecord::client_wait_list_length, GNUNET_TUN_UdpHeader::destination_port, RequestRecord::dst_addr, ETH_P_IPV4, ETH_P_IPV6, GNUNET_TUN_Layer2PacketHeader::flags, gettext_noop, GNUNET_ALIGN, GNUNET_array_grow, GNUNET_assert, GNUNET_break, GNUNET_ERROR_TYPE_DEBUG, GNUNET_HELPER_send(), GNUNET_MAX_MESSAGE_SIZE, GNUNET_memcpy, GNUNET_MESSAGE_TYPE_DNS_HELPER, GNUNET_NO, GNUNET_STATISTICS_update(), GNUNET_TUN_calculate_udp4_checksum(), GNUNET_TUN_calculate_udp6_checksum(), GNUNET_TUN_initialize_ipv4_header(), GNUNET_TUN_initialize_ipv6_header(), GNUNET_YES, hijacker, LOG, RequestRecord::payload, RequestRecord::payload_length, RequestRecord::phase, GNUNET_TUN_Layer2PacketHeader::proto, RequestRecord::request_id, RP_RESPONSE_MONITOR, GNUNET_TUN_UdpHeader::source_port, RequestRecord::src_addr, stats, GNUNET_MessageHeader::type, and udp.
Referenced by next_phase().
|
static |
Show the payload of the given request record to the client (and wait for a response).
rr | request to send to client |
cr | client to send the response to |
Definition at line 476 of file gnunet-service-dns.c.
References cleanup_rr(), env, GNUNET_break, GNUNET_ERROR_TYPE_DEBUG, GNUNET_MAX_MESSAGE_SIZE, GNUNET_memcpy, GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST, GNUNET_MQ_msg_extra, GNUNET_MQ_send(), LOG, ClientRecord::mq, RequestRecord::payload, RequestRecord::payload_length, GNUNET_DNS_Request::request_id, RequestRecord::request_id, and GNUNET_DNS_Request::reserved.
Referenced by next_phase().
|
static |
Callback called from DNSSTUB resolver when a resolution succeeded.
cls | NULL |
dns | the response itself |
r | number of bytes in dns |
Definition at line 732 of file gnunet-service-dns.c.
References gettext_noop, GNUNET_assert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_log, GNUNET_malloc, GNUNET_memcpy, GNUNET_NO, GNUNET_STATISTICS_update(), GNUNET_TUN_DnsHeader::id, LOG, next_phase(), RequestRecord::payload, RequestRecord::payload_length, RequestRecord::phase, RequestRecord::request_id, requests, RP_INTERNET_DNS, and stats.
Referenced by next_phase().
|
static |
A client has completed its processing for this request.
Move on.
rr | request to process further |
Definition at line 526 of file gnunet-service-dns.c.
References cleanup_rr(), RequestRecord::client_wait_list, RequestRecord::client_wait_list_length, clients_head, dnsstub, RequestRecord::dst_addr, ClientRecord::flags, gettext_noop, GNUNET_array_append, GNUNET_assert, GNUNET_break, GNUNET_DNS_FLAG_POST_RESOLUTION, GNUNET_DNS_FLAG_PRE_RESOLUTION, GNUNET_DNS_FLAG_REQUEST_MONITOR, GNUNET_DNS_FLAG_RESPONSE_MONITOR, GNUNET_DNSSTUB_resolve(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_NO, GNUNET_STATISTICS_update(), consensus-simulation::int, LOG, ClientRecord::next, next_phase(), RequestRecord::payload, RequestRecord::payload_length, RequestRecord::phase, process_dns_result(), request_done(), RequestRecord::request_id, RP_DROP, RP_INIT, RP_INTERNET_DNS, RP_MODIFY, RP_QUERY, RP_REQUEST_MONITOR, RP_RESPONSE_MONITOR, RequestRecord::rs, send_request_to_client(), and stats.
Referenced by client_disconnect_cb(), handle_client_response(), next_phase(), process_dns_result(), and process_helper_messages().
|
static |
A client connected, setup our data structures.
cls | unused |
client | handle of client that connected |
mq | message queue to talk to client |
struct ClientRecord
Definition at line 671 of file gnunet-service-dns.c.
References ClientRecord::client, clients_head, clients_tail, GNUNET_CONTAINER_DLL_insert, GNUNET_new, mq, and ClientRecord::mq.
|
static |
A client disconnected, clean up after it.
cls | unused |
client | handle of client that disconnected |
app_ctx | our struct ClientRecord |
Definition at line 695 of file gnunet-service-dns.c.
References RequestRecord::client_wait_list, RequestRecord::client_wait_list_length, clients_head, clients_tail, GNUNET_CONTAINER_DLL_remove, GNUNET_free, next_phase(), and requests.
|
static |
We got a new client.
Make sure all new DNS requests pass by its desk.
cls | the client |
reg | the init message |
Definition at line 776 of file gnunet-service-dns.c.
References ClientRecord::client, GNUNET_DNS_Register::flags, ClientRecord::flags, and GNUNET_SERVICE_client_continue().
|
static |
Check a response from a client.
cls | the client |
resp | the response |
Definition at line 794 of file gnunet-service-dns.c.
References GNUNET_OK.
|
static |
Handle a response from a client.
cls | the client |
resp | the response |
Definition at line 808 of file gnunet-service-dns.c.
References ClientRecord::client, RequestRecord::client_wait_list, RequestRecord::client_wait_list_length, GNUNET_DNS_Response::drop_flag, gettext_noop, GNUNET_array_grow, GNUNET_break, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_log, GNUNET_malloc, GNUNET_memcpy, GNUNET_NO, GNUNET_SERVICE_client_continue(), GNUNET_SERVICE_client_drop(), GNUNET_STATISTICS_update(), GNUNET_DNS_Response::header, LOG, next_phase(), payload, RequestRecord::payload, RequestRecord::payload_length, RequestRecord::phase, GNUNET_DNS_Response::request_id, RequestRecord::request_id, requests, RP_DROP, RP_INTERNET_DNS, RP_QUERY, RP_REQUEST_MONITOR, RP_RESPONSE_MONITOR, GNUNET_MessageHeader::size, and stats.
|
static |
Functions with this signature are called whenever a complete message is received by the tokenizer from the DNS hijack process.
cls | closure |
message | the actual message, a DNS request we should handle |
Definition at line 906 of file gnunet-service-dns.c.
References _, RequestRecord::client_wait_list, RequestRecord::client_wait_list_length, GNUNET_TUN_IPv4Header::destination_address, GNUNET_TUN_IPv6Header::destination_address, DNS_PORT, RequestRecord::dst_addr, ETH_P_IPV4, ETH_P_IPV6, gettext_noop, GNUNET_array_grow, GNUNET_assert, GNUNET_break, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_INFO, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_log, GNUNET_malloc, GNUNET_memcpy, GNUNET_NO, GNUNET_OK, GNUNET_STATISTICS_update(), GNUNET_TUN_IPv4Header::header_length, GNUNET_TUN_DnsHeader::id, LOG, GNUNET_TUN_IPv6Header::next_header, next_phase(), RequestRecord::payload, GNUNET_TUN_IPv6Header::payload_length, RequestRecord::payload_length, RequestRecord::phase, GNUNET_TUN_Layer2PacketHeader::proto, GNUNET_TUN_IPv4Header::protocol, RequestRecord::request_id, request_id_gen, requests, RP_INIT, GNUNET_MessageHeader::size, GNUNET_TUN_IPv4Header::source_address, GNUNET_TUN_IPv6Header::source_address, RequestRecord::src_addr, stats, GNUNET_TUN_IPv4Header::total_length, udp, GNUNET_TUN_IPv4Header::version, and GNUNET_TUN_IPv6Header::version.
Referenced by run().
|
static |
cls | closure |
cfg_ | configuration to use |
service | the initialized service |
Definition at line 1077 of file gnunet-service-dns.c.
References _, cfg, cleanup_task(), dnsstub, global_ret, GNUNET_CONFIGURATION_get_value_string(), GNUNET_CONFIGURATION_get_value_yesno(), GNUNET_DNSSTUB_add_dns_ip(), GNUNET_DNSSTUB_start(), GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_HELPER_start(), GNUNET_log, GNUNET_log_config_invalid(), GNUNET_NO, GNUNET_OK, GNUNET_OS_check_helper_binary(), GNUNET_OS_get_suid_binary_path(), GNUNET_OS_project_data_gnunet(), GNUNET_SCHEDULER_add_shutdown(), GNUNET_SCHEDULER_shutdown(), GNUNET_STATISTICS_create(), GNUNET_strdup, GNUNET_SYSERR, GNUNET_YES, helper_argv, hijacker, ipv6prefix, process_helper_messages(), and stats.
GNUNET_SERVICE_MAIN | ( | GNUNET_OS_project_data_gnunet() | , |
"dns" | , | ||
GNUNET_SERVICE_OPTION_NONE | , | ||
& | run, | ||
& | client_connect_cb, | ||
& | client_disconnect_cb, | ||
NULL | , | ||
GNUNET_MQ_hd_fixed_size(client_init, GNUNET_MESSAGE_TYPE_DNS_CLIENT_INIT, struct GNUNET_DNS_Register, NULL) | , | ||
GNUNET_MQ_hd_var_size(client_response, GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE, struct GNUNET_DNS_Response, NULL) | , | ||
GNUNET_MQ_handler_end() | |||
) |
Define "main" method using service macro.
|
static |
Global return value from 'main'.
Definition at line 198 of file gnunet-service-dns.c.
Referenced by run().
|
static |
|
static |
Statistics.
Definition at line 208 of file gnunet-service-dns.c.
Referenced by cleanup_task(), handle_client_response(), next_phase(), process_dns_result(), process_helper_messages(), request_done(), and run().
|
static |
Handle to DNS hijacker helper process ("gnunet-helper-dns").
Definition at line 213 of file gnunet-service-dns.c.
Referenced by cleanup_task(), request_done(), and run().
|
static |
Command-line arguments we are giving to the hijacker process.
Definition at line 218 of file gnunet-service-dns.c.
Referenced by cleanup_task(), and run().
|
static |
Head of DLL of clients we consult.
Definition at line 223 of file gnunet-service-dns.c.
Referenced by client_connect_cb(), client_disconnect_cb(), and next_phase().
|
static |
Tail of DLL of clients we consult.
Definition at line 228 of file gnunet-service-dns.c.
Referenced by client_connect_cb(), and client_disconnect_cb().
|
static |
Array of all open requests.
Definition at line 233 of file gnunet-service-dns.c.
Referenced by cleanup_task(), client_disconnect_cb(), handle_client_response(), process_dns_result(), and process_helper_messages().
|
static |
Generator for unique request IDs.
Definition at line 238 of file gnunet-service-dns.c.
Referenced by process_helper_messages().
|
static |
Handle to the DNS Stub resolver.
Definition at line 243 of file gnunet-service-dns.c.
Referenced by cleanup_task(), next_phase(), and run().