Reputation: 4646
I am learning networking in c by looking at the code of a working packet sniffer within Microsoft Visual Basic 2013.
Below is code that creates a pointer to a hostent structure, obtains the localhost hostname, and loads it into the hostent structure called local.
struct hostent *local;
gethostname(hostname, sizeof(hostname))
local = gethostbyname(hostname);
The next part enables the address to be printed in dotted decimal notation.
for (i = 0; local->h_addr_list[i] != 0; ++i)
{
memcpy(&addr, local->h_addr_list[i], sizeof(struct in_addr));
printf(" Interface Number : %d Address : %s\n",i,inet_ntoa(addr));
}
Now, I want to understand how all this works and more . . .
Say I want to understand inet_ntoa(), I right-click and choose Go To Definition or Go To Declaration and it sends me to WinSock2.h that shows:
inet_ntoa(
__in struct in_addr in
);
This appears to be show me the parameter but not the workings of the function or the return value. This means I have to refer to the MSDN to understand what is happening every time.
My question is: Where is the code to read what is happening so I don't have to use the docs?
E.g. where is the inet_ntoa
function contents?
Upvotes: 0
Views: 96
Reputation: 218
Here you are:
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
/* The interface of this function is completely stupid, it requires a
static buffer. We relax this a bit in that we allow one buffer for
each thread. */
static __thread char buffer[18];
char *inet_ntoa (struct in_addr in)
{
unsigned char *bytes = (unsigned char *) ∈
__snprintf (buffer, sizeof (buffer), "%d.%d.%d.%d",
bytes[0], bytes[1], bytes[2], bytes[3]);
return buffer;
}
Taken from inet_ntoa.c, glibc 2.23
Remember that the glibc is open source, so if you want to explore a bit and learn what's going in under the hood, don't hesitate and download it !
Regards
Upvotes: 3