How might I perform DNS lookups using C/C++ on Linux? - c

How do I get similar functionality to the host command using a c api (or any other language for that matter)? I need more information than just an IP address given by gethostbyname(); specifically, the SMTP-related data.

If a blocking (synchronous) query is ok, just use res_query(), and link your program with -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.

I know that the question is old, but I have long searched a dns library, and all answers here just stubs me. I think libraries like adns/udns have written not for human beings. And FireDNS for a long time have not working download links.
I have found poslib as the best dns library with very easy interface.

I like adns because it allows for asynchronous requests

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:
adns
udns
dns.c
FireDNS (as mentioned by ko-dos)

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.

You can also try c-ares library https://c-ares.haxx.se/, which allows to send asynchronous DNS queries. It also comes with adig - its own version of dig utility for querying DNS. You can check it to see how to parse DNS reply: adig.c source

Related

Zip file using C program

Is there any option to zip the file using C programme without using any external application (like Zip..)?
Of course, try using zlib. Or other libraries for compression. You can also code the compression algorithm yourself, but that might be too time consuming. Since you did not specify why do you need this, i can't give you a better answer.
Please find the link for some of the compression libraries available. It depends on the system you are using and the features you want. Zlib is one which i would recommend because of the configurable features and user manual.
Have you checked 7-zip. It has a complete SDK, with example sources, and a lot of functionality. It is Open-source, and free :)
Take a look here
A good wrapper around it is SevenZipSharp.
Sample usage:
SevenZipCompressor compressor;
compressor = new SevenZipCompressor();
compressor.CompressionLevel = CompressionLevel.Ultra;
compressor.CompressionMethod = CompressionMethod.Lzma;
compressor.CompressionMode = CompressionMode.Create;
compressor.CompressFiles(archiveName, files2Add.ToArray());

Access Client Write Key & Server Write Key from OpenSSL C API

I have a need to access the encryption (cipher, write) keys that are generated from the master key that is generated from the OpenSSL C API. I know I can access the master key using the SSL struct as follows:
ssl->session->master_key
Unfortunately looking at the OpenSSL code has not gotten me very far as the API is not very well documented and looking at GDB has been a pain as well. Are they exposed anywhere?
Thanks
I am working on the same thing as well.
Well for the SSlv3 connections, you can access the key block by,
ssl->s3->tmp.key_block
However, I managed to get this at the end of ssl3_setup_key_block method which is defined in s3_enc.c file. I mean I changed the source code and recompiled openssl. So I don't know yet if it changes or becomes lost after returning from that function.
This key block is a string and as defined in http://www.ietf.org/rfc/rfc2246.txt its structure is;
{
clientWriteMACSecret,
serverWriteMACSecret,
clientWriteKey,
serverWriteKey,
clientWriteIV,
serverWriteIV
}
The length of each section varies depending on the used cipher suite.
For SSLv2 connections I suppose you can again access these areas by calling ssl->s2->something as well. s2 is an object of ssl2_state_st structure which is defined in ssl2.h. You can look that up from there.
I don't know how it is for TLSv1 connections. But there is also a d1 variable in the SSL structure.
As a personal note, openssl source code is really a bunch of trash. No good code comments, no good documentation. Wandering inside the source code in order to find the simplest thing is a pain.

Querying full DNS record

I do alot of programming in *nix using C gcc. I know how to do a basic gethostbyname(). BUt what if I wanted to pull down the entire DNS record. More to the point, is there a function that I'm missing that allows you to specify the specific record that you want to pull? Or will I need to do this manually through a UDP socket?
No, there is no such function in standard C or POSIX (and even the gethostbyname function, contrary to what you may think, is not a function for querying DNS - it can use any other way to get the address, like /etc/hosts or mDNS, whatever).
You might want to look at some DNS-specific libraries, like ldns, libbind or libdjbdns.
See also Code to do a direct DNS lookup.

Using the name resolver of resolv.h with IPv6

I write or modify programs which perform name resolution and need a
good control of the process. So I do not use getaddrinfo(), I go
deeper and use res_query() / res_send() / etc in resolv.h, documented
in resolver(3).
Although not documented, the common way to set the resolver used is to
update _res.nsaddr_list. But this array, defined in resolv.h, stores
struct sockaddr_in, that is IPv4 addresses only. (IPv6 addresses
are struct sockaddr_in6, a family-independant system would use struct sockaddr.)
I'm looking for a way (preferrably portable, at least among the
various Unix) to tell _res that I want also IPv6 addresses.
Apparently, a long time ago, there was in FreeBSD a _res_ext with this
ability but I cannot find it anymore in a recent FreeBSD 7 (grep
_res_ext /usr/include/resolv.h finds nothing). You can still find
code which uses it (try yourself with Google Codesearch).
Thanks to Alnitak, I noticed it is apparently now _res._ext and not .res_ext. I wonder where these sort of things are documented or announced... I have no idea how portable _res._ext is. I can find it on Debian and FreeBSD. It seems there are few programs which use it.
Stéphane - if your resolv.h doesn't include any support for sockaddr_in6 then that suggests that on your particular O/S the resolver does not itself support IPv6 transport.
I've checked some of my systems here:
MacOS X 10.5.6 - supports the BIND 9 library, which has a res_setservers() function which can take IPv6 addresses, no _res._ext extension.
CentOS 5.2 - has the _res._ext extension, although there's no mention of IPv6 in the man page for resolv.conf except that there's a setting to tell the resolver to return AAAA records before looking for A records for gethostbyname().
EDIT - also, the CVS repository for FreeBSD suggests that FreeBSD 7.0 (see tag FREEBSD_7_0_0_RELEASE) does also support res_setservers() from Bind 9.
glibc:
res_setservers: no
__res_state._u._ext.nsaddrs
__res_state._u._ext.nsmap
set the latter to MAXNS+1 according to:
http://sourceware.org/ml/libc-hacker/2002-05/msg00035.html
BSD-libc:
res_setservers: yes
__res_state._u._ext.__res_state_ext
Seems messy to me and you'll probably need autoconf.

Is the Win32 API function definition available in a "database"/XML format?

I'm looking for a list of win32 API in some "database"/XML format.
I'd need it to easily create a "conversion layer" between win32 API and the higher level language I'm using (harbour/xharbour). Since this runs Pcode, it is necessary to transform parameters to C standard...
Instead of doing manual code write, I'd like to automate the process...
just for example, the windows API definition (taken from MSDN)
DWORD WINAPI GetSysColor(
__in int nIndex
);
should be transformed in
HB_FUNC( GETSYSCOLOR )
{
hb_retnl( (LONG) GetSysColor( hb_parni( 1 ) ) );
}
AFAIK, pinvoke.net only stores text data with the PInvoke definition for the call. Not very useful if what you want is something to use as a pre-parsed database of APIs.
Probably you could create an small parser that will take the include file and translate it to what you need. In that case, I'd recommend using lcc-win32's include files, as they are pretty much fat-free/no-BS version of the SDK headers (they don't come with a bunch of special reserved words you'd have to ignore, etc.)
Of course, you have Microsoft Platform SDK, but it is in raw .h C code, so hard to parse!
Similar work have been done by VB users (and Delphi users and probably for some other languages), for example ApiViewer has such database, but in some proprietary binary format (.apv extension), so you might have to reverse-engineer it.
Similarly, there is an API-Guide, which was hosted at Allapi.net but the later seems to be a parking site now. It used .api files (again binary-proprietary).
About the closest thing I know of would be: http://pinvoke.net/
Maybe they would share their data with you?
They have a VS tool that accesses this data, so it may be a webservice. You might even be able to sniff that out.
There seems to be some database (and an app for using it, named "PInvoke Interop Assistant") at:
https://github.com/jaredpar/pinvoke/tree/master/StorageGenerator/Data
although I'm not sure what's the license for now — thus I've asked the authors.

Resources