mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Ryan Ward <rwardd@outlook.com.au>
To: Szabolcs Nagy <nsz@port70.net>, Rich Felker <dalias@libc.org>
Cc: "musl@lists.openwall.com" <musl@lists.openwall.com>
Subject: Re: [musl] Adding dns/resolver tests to libc-test
Date: Tue, 27 Aug 2024 15:30:37 +0000	[thread overview]
Message-ID: <SG2PR01MB4566A3271510061330286E57F0942@SG2PR01MB4566.apcprd01.prod.exchangelabs.com> (raw)
In-Reply-To: <20240818093842.GD3766212@port70.net>


[-- Attachment #1.1: Type: text/plain, Size: 1286 bytes --]

Hello,

I have made an attempt at fleshing out the resolver tests, please see the file attached. It still needs some polishing, and I am keen to keep building out the necessary tests, attached is a simple rudimentary res_query() test. I need to improve the packet and RR parsing, but sending this out to get some early feedback before I build out more tests.

I have just been building the test and the unshare-ns.c framework with the libc-test build system, executing unshare-ns.exe and passing in resolv_query.exe as intended.

Thanks,
rw

________________________________
From: Szabolcs Nagy <nsz@port70.net>
Sent: Sunday, 18 August 2024 9:38 AM
To: Rich Felker <dalias@libc.org>
Cc: musl@lists.openwall.com <musl@lists.openwall.com>
Subject: Re: [musl] Adding dns/resolver tests to libc-test

* Rich Felker <dalias@libc.org> [2024-08-17 22:03:28 -0400]:
> I've been working on a framework to allow testing of libc resolver/dns
> functionality in libc-test, on Linux-based hosts, provided they have
> user-namespace functionality. The intent is that these tests would be
> made conditional on __linux__ or similar, with the freedom to add
> equivalent setup for other systems in the future if desired.

thanks. im travelling now will look at it in a few days

[-- Attachment #1.2: Type: text/html, Size: 3689 bytes --]

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: resolv_query.c --]
[-- Type: text/x-csrc; name="resolv_query.c", Size: 4297 bytes --]

#include <stdio.h>
#include <unistd.h>
#include <sched.h>
#include <sys/mount.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <stdlib.h> 
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <net/if.h>
#include <netinet/in.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <pthread.h>
#include <resolv.h>
#include <netdb.h>
#include "test.h"

#define TEST(c, ...) \
	( (c) || (t_error(#c " failed: " __VA_ARGS__),0) )

// Example IP
#define EXPECTED_IP_ADDRESS "192.168.17.1"

// Simple answer resource record
#define EXAMPLE_ANSWER_RR "\xc0\x0c" \
                          "\x00\x01" \
                          "\x00\x01" \
                          "\x00\x00\x02\x58" \
                          "\x00\x04" \
                          "\xc0\xa8\x11\x01" // IPv4 192.168.17.1

// Wait until serving thread is ready to receive
pthread_barrier_t sync_barrier;

static size_t construct_response(uint16_t id, unsigned char *question, unsigned char *response)
{
    HEADER dns_header;
    const size_t dns_header_size = sizeof(dns_header);
    const size_t expected_question_size = 17U;
    memset(&dns_header, 0, dns_header_size);

    dns_header.id = id;
    dns_header.qr = 0x01U;
    dns_header.rd = 0x01U;
    dns_header.ra = 0x01U;
    dns_header.qdcount = 0x0100U; // 1 question
    dns_header.ancount = 0x0100U; // 1 answer

    memcpy(response, &dns_header, sizeof(dns_header));
    memcpy(&response[dns_header_size], &question[dns_header_size], expected_question_size);
    unsigned char answer_buffer[] = EXAMPLE_ANSWER_RR;

    memcpy(&response[dns_header_size + expected_question_size], &answer_buffer[0], sizeof(answer_buffer) - 1);

    return dns_header_size + expected_question_size + sizeof(answer_buffer) - 1; //ignore null terminator
}

static int bind_to_socket(int s)
{
    struct sockaddr_in dns_server;

    memset(&dns_server, 0, sizeof(dns_server));
    dns_server.sin_addr.s_addr = inet_addr("127.0.0.1");
    dns_server.sin_family = AF_INET;
    dns_server.sin_port = htons(53);

    return bind(s, (struct sockaddr*)&dns_server, sizeof(dns_server));
}

static void set_environment(void)
{
    FILE *ft = fopen("/etc/resolv.conf", "w");
    if (ft == NULL) t_error("unable to open namespaced resolv.conf\n");
    fprintf(ft, "nameserver 127.0.0.1");
    fclose(ft);
    
    ft = fopen("/etc/hosts", "w");
    if (ft == NULL) t_error("unable to open namespaced resolv.conf\n");
    fprintf(ft, "127.0.0.1 localhost");
    fclose(ft);
}

void *dns_server(void *arguments)
{
    int s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    int status = bind_to_socket(s);
    
    struct sockaddr_in from = {0};
    socklen_t from_length = sizeof(from);
    unsigned char packet_buffer[NS_PACKETSZ];
    pthread_barrier_wait(&sync_barrier);
    int packet_size = recvfrom(s, packet_buffer, NS_PACKETSZ, 0,
                              (struct sockaddr*)&from, &from_length);

    unsigned char response_buffer[NS_PACKETSZ] = {0};
    const uint16_t response_id = (packet_buffer[1] << 8) | packet_buffer[0];
    size_t response_size = construct_response(response_id, packet_buffer, response_buffer);
    status = sendto(s, response_buffer, response_size, 0, (struct sockaddr*)&from, from_length);
    
    return 0; 
}

int main(int argc, char **argv)
{
    pthread_barrier_init(&sync_barrier, NULL, 2);
    set_environment();

    pthread_t dns_thread;
    int status = pthread_create(&dns_thread, 0, dns_server, 0);
    pthread_barrier_wait(&sync_barrier);
    
    unsigned char res_buffer[NS_PACKETSZ] = {0};
    char *query = "example.com";
    int length = res_query(query, C_ANY, T_PTR, res_buffer, sizeof(res_buffer));
   
    // Simple test, expect the resulting IP to be in the last four bytes of 
    // the buffer
    struct in_addr returned_address = {
        .s_addr = res_buffer[length - 1] << 24
                | res_buffer[length - 2] << 16
                | res_buffer[length - 3] << 8
                | res_buffer[length - 4]
    };
    
    TEST(!strcmp(inet_ntoa(returned_address), EXPECTED_IP_ADDRESS), 
         "Expected ip address %s, got %s\n", EXPECTED_IP_ADDRESS, inet_ntoa(returned_address));
    
    void *thread_return;
    pthread_join(dns_thread, &thread_return);

    return t_status;
}


  reply	other threads:[~2024-08-27 15:30 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-18  2:03 Rich Felker
2024-08-18  9:38 ` Szabolcs Nagy
2024-08-27 15:30   ` Ryan Ward [this message]
2024-08-27 21:34     ` Rich Felker
2024-09-09 14:46       ` Ryan Ward
2024-09-11 20:38         ` Szabolcs Nagy
2024-09-11 21:33           ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=SG2PR01MB4566A3271510061330286E57F0942@SG2PR01MB4566.apcprd01.prod.exchangelabs.com \
    --to=rwardd@outlook.com.au \
    --cc=dalias@libc.org \
    --cc=musl@lists.openwall.com \
    --cc=nsz@port70.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).