From 01b268cbf57515c37cad7dde05b6503e8d64bd5f Mon Sep 17 00:00:00 2001 From: hpa Date: Tue, 28 Dec 2004 23:49:43 +0000 Subject: [PATCH] Fix using the resolver function from the API (with DS != CS); add test/sample file to do that; fix bug in chain.c. --- com32/modules/chain.c | 6 ++--- com32/samples/Makefile | 2 +- com32/samples/resolv.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ dnsresolv.inc | 36 ++++++++++++++++----------- dos/Makefile | 2 +- 5 files changed, 94 insertions(+), 19 deletions(-) create mode 100644 com32/samples/resolv.c diff --git a/com32/modules/chain.c b/com32/modules/chain.c index 9db30cb..eea48f1 100644 --- a/com32/modules/chain.c +++ b/com32/modules/chain.c @@ -29,6 +29,7 @@ #include #include #include +#include #define SECTOR 512 /* bytes/sector */ @@ -260,6 +261,8 @@ int main(void) int hd, drive, whichpart; static com32sys_t inreg; /* In bss, so zeroed automatically */ + openconsole(&dev_null_r, &dev_stdcon_w); + /* Parse command line */ while ( isspace(*cmdline) ) cmdline++; @@ -354,6 +357,3 @@ int main(void) bail: return 255; } - - - diff --git a/com32/samples/Makefile b/com32/samples/Makefile index 07d8156..97985af 100644 --- a/com32/samples/Makefile +++ b/com32/samples/Makefile @@ -39,7 +39,7 @@ LNXLIBS = ../libutil/libutil_lnx.a .SUFFIXES: .lss .c .o .elf .c32 .lnx -all: hello.c32 cat.c32 \ +all: hello.c32 cat.c32 resolv.c32 \ fancyhello.c32 fancyhello.lnx \ keytest.c32 keytest.lnx \ diff --git a/com32/samples/resolv.c b/com32/samples/resolv.c new file mode 100644 index 0000000..c82a974 --- /dev/null +++ b/com32/samples/resolv.c @@ -0,0 +1,67 @@ +#ident "$Id$" +/* ----------------------------------------------------------------------- * + * + * Copyright 2004 H. Peter Anvin - All Rights Reserved + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, Inc., 53 Temple Place Ste 330, + * Boston MA 02111-1307, USA; either version 2 of the License, or + * (at your option) any later version; incorporated herein by reference. + * + * ----------------------------------------------------------------------- */ + +/* + * resolv.c + * + * Resolve an IP address + */ + +#include +#include +#include +#include +#include + +uint32_t resolv(const char *name) +{ + com32sys_t reg; + + strcpy((char *)__com32.cs_bounce, name); + + memset(®, 0, sizeof reg); + reg.eax.w[0] = 0x0010; + reg.ebx.w[0] = OFFS(__com32.cs_bounce); + reg.es = SEG(__com32.cs_bounce); + + __intcall(0x22, ®, ®); + + if ( reg.eflags.l & EFLAGS_CF ) + return 0; + else + return reg.eax.l; +} + +int main(int argc, char *argv[]) +{ + uint32_t ip; + + openconsole(&dev_null_r, &dev_stdcon_w); + + if ( argc < 2 ) { + fputs("Usage: resolv hostname\n", stderr); + exit(1); + } + + ip = resolv(argv[1]); + + if ( ip ) { + printf("%s = %u.%u.%u.%u\n", argv[1], + (ip & 0xff), (ip >> 8) & 0xff, + (ip >> 16) & 0xff, (ip >> 24) & 0xff); + } else { + printf("%s not found\n", argv[1]); + } + + return 0; +} diff --git a/dnsresolv.inc b/dnsresolv.inc index 3181c0d..05d0fff 100644 --- a/dnsresolv.inc +++ b/dnsresolv.inc @@ -31,9 +31,6 @@ DNS_MAX_SERVERS equ 4 ; Max no of DNS servers ; On return, DI points to the first byte after the label set, ; and SI to the terminating byte. ; -; Assumes DS == ES. Change references to [bx] to [es:bx] to remove -; that assumption. -; ; On return, DX contains the number of dots encountered. ; dns_mangle: @@ -53,13 +50,13 @@ dns_mangle: jz .endstring cmp al,'.' je .isdot - inc byte [bx] + inc byte [es:bx] stosb jmp .getbyte .endstring: dec si dec dx ; We always counted one high - cmp byte [bx],0 + cmp byte [es:bx],0 jz .done xor al,al stosb @@ -128,11 +125,6 @@ dns_skiplabel: pop ax ret -; Actual resolver function -; Points to a null-terminated or :-terminated string in DS:SI -; and returns the name in EAX if it exists and can be found. -; If EAX = 0 on exit, the lookup failed. - ; DNS header format struc dnshdr .id: resw 1 @@ -186,19 +178,25 @@ pxe_udp_read_pkt_dns: LastDNSServer dw DNSServers +; Actual resolver function +; Points to a null-terminated or :-terminated string in DS:SI +; and returns the name in EAX if it exists and can be found. +; If EAX = 0 on exit, the lookup failed. + section .text dns_resolv: + push ds + push es push di push cx push dx - ; Initialize... - mov eax,[MyIP] - mov [pxe_udp_read_pkt_dns.dip],eax + push cs + pop es ; ES <- CS ; First, build query packet mov di,DNSSendBuf+dnshdr.flags - inc word [di-2] ; New query ID + inc word [es:di-2] ; New query ID mov ax,htons(0100h) ; Recursion requested stosw mov ax,htons(1) ; One question @@ -209,8 +207,16 @@ dns_resolv: stosw call dns_mangle ; Convert name to DNS labels + + push cs ; DS <- CS + pop ds + push si ; Save pointer to after DNS string + ; Initialize... + mov eax,[MyIP] + mov [pxe_udp_read_pkt_dns.dip],eax + and dx,dx jnz .fqdn ; If we have dots, assume it's FQDN dec di ; Remove final null @@ -247,6 +253,8 @@ dns_resolv: pop dx pop cx pop di + pop es + pop ds ret .moreservers: diff --git a/dos/Makefile b/dos/Makefile index ca17a64..f17e403 100644 --- a/dos/Makefile +++ b/dos/Makefile @@ -1,4 +1,4 @@ -CC = gcc -m32 -mregparm=3 -DREGPARM=3 -DDEBUG +CC = gcc -m32 -mregparm=3 -DREGPARM=3 LD = ld -m elf_i386 OBJCOPY = objcopy OPTFLAGS = -g -Os -march=i386 -falign-functions=0 -falign-jumps=0 -falign-loops=0 -fomit-frame-pointer -- 2.7.4