PDA

View Full Version : [Problema] che struttura per una query al dns?


sephiro
22-04-2003, 18:26
Salve,
sto sviluppando per motivi didattici un piccolo programmino che non fa altro che intercettare le query di una certa macchina al dns, ed inviare una risposta manipolata.
Il mio problema e' che per catturare una query e per creare una response uso questa struttura:

struct dns_header {
// Header DNS
u_int16_t id; /* query identification number */
unsigned qr: 1; /* response flag */
unsigned opcode: 4; /* purpose of message */
unsigned aa: 1; /* authoritative answer */
unsigned tc: 1; /* truncated message */
unsigned rd: 1; /* recursion desired */
unsigned ra: 1; /* recursion available */
unsigned unused :1; /* unused bits (MBZ as of 4.9.3a3) */
unsigned ad: 1; /* authentic data from named */
unsigned cd: 1; /* checking disabled by resolver */
unsigned rcode: 4; /* response code */
u_int16_t qdcount; /* number of question entries */
u_int16_t ancount; /* number of answer entries */
u_int16_t nscount; /* number of authority entries */
u_int16_t arcount; /* number of resource entries */
};

struct rr {
char * rd;
char name[256];
unsigned short int type;
unsigned short int classe;
unsigned long int ttl;
unsigned short int rdlenght;
};

struct query {
char name[256];
unsigned short int type;
unsigned short int classe;
};

struct dns_packet {
struct dns_header head;
struct query question;
struct rr answer;
struct rr authoritative;
//struct rr additional;
};


purtroppo quando invio la risposta il pacchetto viene classificato come "bad packet" e quindi scartato.
Ho appurato che il problema sta nella struttura non adatta, in quanto tutto il resto funziona regolarmente.
Suggerimenti? Il linguaggio usato e' il C. :)

cionci
23-04-2003, 09:50
Ho scopiazzato queste strutture da un programma per Windows che fa query ad un server DNS... Non so quanto ti possa tornare utile, ma le metto lo stesso...


typedef unsigned char BYTE;
typedef unsigned char UCHAR;
typedef void *LPVOID;

#define DNS_FLAGS_QR 0x8000 //Query or Response
#define DNS_FLAGS_OPCODE 0x7800 //Operation Code
#define DNS_FLAGS_AA 0x0400 //Authoritative Answer
#define DNS_FLAGS_TC 0x0200 //Truncated
#define DNS_FLAGS_RD 0x0100 //Recursion Desired
#define DNS_FLAGS_RA 0x0080 //Recursion Available
#define DNS_FLAGS_Z 0x0070 //Reserved
#define DNS_FLAGS_RCODE 0x000F //Response Code

#define DNS_TYPE_A 1
#define DNS_TYPE_NS 2
#define DNS_TYPE_MD 3
#define DNS_TYPE_MF 4
#define DNS_TYPE_CNAME 5
#define DNS_TYPE_SOA 6
#define DNS_TYPE_MB 7
#define DNS_TYPE_MG 8
#define DNS_TYPE_MR 9
#define DNS_TYPE_NULL 10
#define DNS_TYPE_WKS 11
#define DNS_TYPE_PTR 12
#define DNS_TYPE_HINFO 13
#define DNS_TYPE_MINFO 14
#define DNS_TYPE_MX 15
#define DNS_TYPE_TXT 16
#define DNS_TYPE_RP 17
#define DNS_TYPE_AFSDB 18
#define DNS_TYPE_X25 19
#define DNS_TYPE_ISDN 20
#define DNS_TYPE_RT 21
#define DNS_TYPE_NSAP 22
#define DNS_TYPE_NSAP_PTR 23
#define DNS_TYPE_SIG 24
#define DNS_TYPE_KEY 25
#define DNS_TYPE_PX 26
#define DNS_TYPE_GPOS 27
#define DNS_TYPE_AAAA 28
#define DNS_TYPE_LOC 29

#define DNS_CLASS_IN 1
#define DNS_CLASS_CS 2
#define DNS_CLASS_CH 3
#define DNS_CLASS_HS 4

#define DNS_ERR_NONE 0
#define DNS_ERR_FORMAT 1
#define DNS_ERR_SERVER 2
#define DNS_ERR_NAME 3
#define DNS_ERR_NOSUPP 4
#define DNS_ERR_REFUSED 5

#define DNS_STANDARD 0
#define DNS_INVERSE 1
#define DNS_STATUS 2

typedef struct {
USHORT ID;
USHORT Flags;
/*
0... .... .... .... = Query/Response
.000 0... .... .... = Operation Code
.... .0.. .... .... = Authoritative Answer
.... ..0. .... .... = Truncated
.... ...0 .... .... = Recursion Desired
.... .... 0... .... = Recursion Available
.... .... .000 .... = Z
.... .... .... 0000 = Response Code
*/
USHORT QDCount;
USHORT ANCount;
USHORT NSCount;
USHORT ARCount;
} DNS_HEADER;

typedef struct {
UCHAR* Name;
USHORT Type;
USHORT Class;
} DNS_QUESTION;

typedef struct {
UCHAR* Name;
USHORT Type;
USHORT Class;
long TTL;
USHORT RDLength;
LPVOID pData;
} DNS_RECORD;

typedef struct {
DNS_HEADER header;
DNS_QUESTION* Question;
DNS_RECORD* Answer;
DNS_RECORD* Authority;
DNS_RECORD* Additional;
} DNS_PACKET;

typedef struct {
UCHAR* MName;
UCHAR* RName;
ULONG Serial;
ULONG Refresh;
ULONG Retry;
ULONG Expire;
ULONG Minimum;
} DNS_SOA;

typedef struct {
UCHAR* Address;
BYTE Protocol;
LPVOID BitMap;
} DNS_WKS;

typedef struct {
UCHAR* CPU;
UCHAR* OS;
} DNS_HINFO;

typedef struct {
UCHAR* RMailBX;
UCHAR* EMailBX;
} DNS_MINFO;

typedef struct {
USHORT Preference;
UCHAR* Exchange;
} DNS_MX;

typedef struct {
UCHAR* mbox_dname;
UCHAR* txt_dname;
} DNS_RP;

typedef struct {
USHORT SubType;
UCHAR* HostName;
} DNS_AFSDB;

typedef struct {
UCHAR* address;
UCHAR* sa;
} DNS_ISDN;

typedef struct {
USHORT Preference;
UCHAR* Intermediate_Host;
} DNS_RT;

typedef struct {
USHORT ip6[8];
} DNS_AAAA;

typedef struct {
BYTE Version;
BYTE Size;
BYTE Horiz_Pre;
BYTE Vert_Pre;
ULONG Latitude;
ULONG Longitude;
ULONG Altitude;
} DNS_LOC;