Dear community,
Please be advised that my C knowledge is limited to reading code here and there if I feel I have to. I have never written anything in C before.
I played a little with ipfw tables and I got curious why ipfw DNS-resolved something I added to an addr-type table.
The following compiles using
Here is my code:
Could someone assist me with my little programming exercise?
Please be advised that my C knowledge is limited to reading code here and there if I feel I have to. I have never written anything in C before.
I played a little with ipfw tables and I got curious why ipfw DNS-resolved something I added to an addr-type table.
The following compiles using
cc
but gives me a segmentation fault when it reaches the second printf
(I think). I'm sure I'm doing a basic mistake related to pointers... I cannot figure out how to access the contents of the struct
.Here is my code:
C:
#include <unistd.h> // gethostname()
#include <sys/param.h> // MAXHOSTNAMELEN
#include <stdio.h> // printf()
#include <netdb.h> // gethostbyname()
int main(void) {
char hostbuf[MAXHOSTNAMELEN]; // for gethostname(), not gethostBYname()
struct hostent *ptr; // gethostbyname(3) describes what hostent contains
/*
ipfw set 0 table ADDR add anything
ipfw resolved "anything" because of my "search" in resolv.conf(5) which
led me to gethostname(3) and gethostbyname(3)
the following seemed relevant which is why I'm trying to figure out
what is happening when I "add anything" to an ipfw table of type addr
$ grep -n gethostbyname /usr/src/sbin/ipfw/tables.c
135: if ((he = gethostbyname(host)) == NULL)
*/
const char ipfw_table_arg[] = "anything";
gethostname(hostbuf, sizeof(hostbuf));
printf("gethostname() says: %s\n", hostbuf);
gethostbyname(ipfw_table_arg);
printf("gethostbyname() says: %s\n", ptr->h_name);
/*
char *h_name
char **h_aliases
int h_addrtype
int h_length
char **h_addr_list;
*/
}
Could someone assist me with my little programming exercise?