fehQlibs 26
Qlibs
Loading...
Searching...
No Matches
dns_mx.c
Go to the documentation of this file.
1#include "stralloc.h"
2#include "byte.h"
3#include "uint_t.h"
4#include "dnsresolv.h"
5
14static char *q = 0;
15
16int dns_mx_packet(stralloc *out,const char *buf,unsigned int len)
17{
18 unsigned int pos;
19 char header[12];
20 char pref[2];
21 uint16 numanswers;
22 uint16 datalen;
23 int ranswers = 0;
24
25 if (!stralloc_copys(out,"")) return DNS_MEM;
26
27 pos = dns_packet_copy(buf,len,0,header,12); if (!pos) return DNS_ERR;
28 uint16_unpack_big(header + 6,&numanswers);
29 pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR;
30 pos += 4;
31
32 while (numanswers--) {
33 pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR;
34 pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) return DNS_ERR;
35 uint16_unpack_big(header + 8,&datalen);
36 if (byte_equal(header,2,DNS_T_MX))
37 if (byte_equal(header + 2,2,DNS_C_IN)) {
38 if (!dns_packet_copy(buf,len,pos,pref,2)) return DNS_ERR;
39 if (!dns_packet_getname(buf,len,pos + 2,&q)) return DNS_ERR;
40 if (!stralloc_catb(out,pref,2)) return DNS_MEM;
41 if (dns_domain_todot_cat(out,q) <= 0) return DNS_ERR;
42 if (!stralloc_0(out)) return DNS_MEM;
43 }
44 pos += datalen;
45 ++ranswers;
46 }
47
48 return ranswers;
49}
50
51int dns_mx(stralloc *out,const stralloc *fqdn)
52{
53 int rc = 0;
54
55 if (dns_domain_fromdot(&q,fqdn->s,fqdn->len) <= 0) return DNS_ERR;
56 if (dns_resolve(q,DNS_T_MX) >= 0) {
59 dns_domain_free(&q);
60 }
61
62 return rc;
63}
int dns_mx(stralloc *out, const stralloc *fqdn)
Definition: dns_mx.c:51
int dns_mx_packet(stralloc *out, const char *buf, unsigned int len)
Definition: dns_mx.c:16
additional types and pack routines
uint16_t uint16
Definition: uint_t.h:29
void uint16_unpack_big(char[16], uint16 *)
#define byte_equal(s, n, t)
Definition: byte.h:18
void dns_transmit_free(struct dns_transmit *)
Definition: dns_transmit.c:105
unsigned int dns_packet_copy(const char *, unsigned int, unsigned int, char *, unsigned int)
Definition: dns_packet.c:12
#define DNS_ERR
Definition: dnsresolv.h:44
int dns_domain_fromdot(char **, const char *, unsigned int)
Definition: dns_dfd.c:13
int dns_domain_todot_cat(stralloc *, const char *)
Definition: dns_dtda.c:11
#define DNS_C_IN
Definition: dnsresolv.h:60
int dns_resolve(const char *, const char *)
unsigned int dns_packet_skipname(const char *, unsigned int, unsigned int)
Definition: dns_packet.c:22
void dns_domain_free(char **)
Definition: dns_domain.c:24
unsigned int dns_packet_getname(const char *, unsigned int, unsigned int, char **)
Definition: dns_packet.c:39
#define DNS_MEM
Definition: dnsresolv.h:43
#define DNS_T_MX
Definition: dnsresolv.h:69
struct dns_transmit dns_resolve_tx
Definition: dns_resolve.c:14
int stralloc_catb(stralloc *, const char *, unsigned int)
Definition: stralloc.c:26
#define stralloc_0(sa)
Definition: stralloc.h:37
int stralloc_copys(stralloc *, const char *)
Definition: stralloc.c:79
unsigned int packetlen
Definition: dnsresolv.h:103
char * packet
Definition: dnsresolv.h:102
size_t len
Definition: stralloc.h:19
char * s
Definition: stralloc.h:18