Всегда используйте новый в C++. При необходимости в блоке невведенной памяти можно использовать оператор, новый непосредственно:
void *p = operator new(size);
...
operator delete(p);
Если блокировка (синхронно) запрос в порядке, просто используйте res_query ()
и свяжите свою программу с -lresolv.
len = res_query(host, C_IN, T_MX, &answer, sizeof(answer));
I'd suggest FireDNS. It's a very fast C library for all kinds of dns queries.
And I would add, unless you're writing a mail relay you almost certainly shouldn't be looking up MX records - you should be passing the mail on to a user-configured mail relay instead.
I don't think there is a function in the C standard library for this, but many scripting languages do have this functionality 'built in'. For example, Perl has the Net::DNS
package:
use Net::DNS;
my @mx = mx("example.com");
foreach $host (@mx) {
print $host;
}
If you need to do this in C, a quick google shows up a few C libraries out there which you can use: