7 /* compare resource record with socket address
11 /* int dns_rr_eq_sa(DNS_RR *rr, struct sockaddr *sa)
13 /* struct sockaddr *sa;
15 /* int DNS_RR_EQ_SA(DNS_RR *rr, struct sockaddr *sa)
17 /* struct sockaddr *sa;
19 /* dns_rr_eq_sa() compares a DNS resource record with a socket
20 /* address. The result is non-zero when the resource type
21 /* matches the socket address family, and when the network
22 /* address information is identical.
24 /* DNS_RR_EQ_SA() is an unsafe macro version for those who live fast.
28 /* DNS resource record pointer.
30 /* Binary address pointer.
32 /* Panic: unknown socket address family.
36 /* The Secure Mailer license must be distributed with this software.
39 /* IBM T.J. Watson Research
41 /* Yorktown Heights, NY 10598, USA
44 /* System libraries. */
48 /* Utility library. */
51 #include <sock_addr.h>
57 /* dns_rr_eq_sa - compare resource record with socket address */
59 int dns_rr_eq_sa(DNS_RR
*rr
, struct sockaddr
* sa
)
61 const char *myname
= "dns_rr_eq_sa";
63 if (sa
->sa_family
== AF_INET
) {
64 return (rr
->type
== T_A
65 && SOCK_ADDR_IN_ADDR(sa
).s_addr
== IN_ADDR(rr
->data
).s_addr
);
67 } else if (sa
->sa_family
== AF_INET6
) {
68 return (rr
->type
== T_AAAA
69 && memcmp((char *) &SOCK_ADDR_IN6_ADDR(sa
),
70 rr
->data
, rr
->data_len
) == 0);
73 msg_panic("%s: unsupported socket address family type: %d",
74 myname
, sa
->sa_family
);
79 * Stand-alone test program.
83 #include <myaddrinfo.h>
84 #include <inet_proto.h>
86 static const char *myname
;
88 static NORETURN
usage(void)
90 msg_fatal("usage: %s hostname address", myname
);
93 int main(int argc
, char **argv
)
95 MAI_HOSTADDR_STR hostaddr
;
97 struct addrinfo
*res0
;
98 struct addrinfo
*res1
;
107 inet_proto_init(argv
[0], INET_PROTO_NAME_ALL
);
113 if ((aierr
= hostaddr_to_sockaddr(argv
[1], (char *) 0, 0, &res1
)) != 0)
114 msg_fatal("host address %s: %s", argv
[1], MAI_STRERROR(aierr
));
115 if ((rr
= dns_sa_to_rr(argv
[1], 0, res1
->ai_addr
)) == 0)
116 msg_fatal("dns_sa_to_rr: %m");
119 if ((aierr
= hostname_to_sockaddr(argv
[0], (char *) 0, 0, &res0
)) != 0)
120 msg_fatal("host name %s: %s", argv
[0], MAI_STRERROR(aierr
));
121 for (res
= res0
; res
!= 0; res
= res
->ai_next
) {
122 SOCKADDR_TO_HOSTADDR(res
->ai_addr
, res
->ai_addrlen
,
123 &hostaddr
, (MAI_SERVPORT_STR
*) 0, 0);
124 vstream_printf("%s =?= %s\n", hostaddr
.buf
, argv
[1]);
125 vstream_printf("tested by function: %s\n",
126 dns_rr_eq_sa(rr
, res
->ai_addr
) ?
128 vstream_printf("tested by macro: %s\n",
129 DNS_RR_EQ_SA(rr
, res
->ai_addr
) ?
134 vstream_fflush(VSTREAM_OUT
);