When gethostbyname() was used in a dynamic library with a dependency
on libsocket and libc it failed to return the IP address correctly in
hostent->h_addr_list[0], giving 255.255.255.255 instead of the correct
address for my machine, frodo, of 192.168.0.130.
The library code is in libbad.c and the test driver in t0001.c below.
When compiled this way, it failed.
cc -w3 -g -c -belf -o t0001dynlib.o t0001.c
cc -w3 -g -c -belf -o libbaddynlib.o libbad.c
cc -G -belf -o libbaddynlib.so libbaddynlib.o -lsocket -lc
LD_RUN_PATH=$PWD cc -belf -o asdynlib-fail t0001dynlib.o -L$PWD -lbaddynlib
When compiled this way (without the -lsocket/-l on the dynamic library line it)
it worked.
cc -w3 -g -c -belf -o t0001dynlib.o t0001.c
cc -w3 -g -c -belf -o libbaddynlib.o libbad.c
cc -G -belf -o libbaddynlib.so libbaddynlib.o
LD_RUN_PATH=$PWD cc -belf -o asdynlib-works t0001dynlib.o -L$PWD -lbaddynlib -lsocket -lc
I had this problem on OpenServer 5.0.6a with version 5.1.2A of the linker.
To fix I applied oss646a and that seemed to fix everything. Presumably the
same fix will work for 5.0.5a, although I couldn't test the original working
version as that would not compile without -lsocket -lc for the dynamic
library.
os001:jonm $ cat libbad.c
#include <netdb.h>
struct hostent *my_gethostbyname(char *name) {
struct hostent *my_hostent;
my_hostent = gethostbyname(name);
return my_hostent;
}
os001:jonm $ cat t0001.c
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
int main(void) {
int errs = 0;
struct hostent *h;
struct hostent *my_gethostbyname(char*);
h = my_gethostbyname("localhost");
if (h == NULL) {
fprintf(stderr, "localhost FAIL - gethostbyname\n");
errs++;
} else if (h->h_addr[0] == -1 && h->h_addr[1] == -1 &&
h->h_addr[2] == -1 && h->h_addr[3] == -1) {
fprintf(stderr, "localhost FAIL - IP address\n");
errs++;
} else {
fprintf(stderr, "localhost OK - %d.%d.%d.%d\n",
h->h_addr[0], h->h_addr[1], h->h_addr[2], h->h_addr[3]);
}
h = my_gethostbyname("frodo");
if (h == NULL) {
fprintf(stderr, "frodo FAIL - gethostbyname\n");
errs++;
} else if (h->h_addr[0] == -1 && h->h_addr[1] == -1 &&
h->h_addr[2] == -1 && h->h_addr[3] == -1) {
fprintf(stderr, "frodo FAIL - IP address\n");
errs++;
} else {
fprintf(stderr, "frodo OK - %s\n", inet_ntoa(*(struct in_addr *)h->h_addr));
}
return (errs != 0);
}
jonm@alchemetrics.co.uk |