#if 0 # # See http://fy.chalmers.se/~appro/LD_*-gallery/ for details. # # Following is Tru64 compiler line. Pre-load by setting _RLD_LIST # environment variable to "/path/to/bind_to.so:DEFAULT" and BIND_TO # environment variable - to interface name of interest, e.g.: # # env _RLD_LIST=`pwd`/bind_to.so:DEFAULT BIND_TO=virtual daemon # /bin/sh << EOS MODNAME=\`basename "$0" .c\` case `uname -s` in OSF1) (set -x; cc -O -shared -o \$MODNAME.so "$0" -B symbolic -init my_init -hidden_symbol my_init );; SunOS) (set -x; cc -O -Kpic -G -z text -o \$MODNAME.so "$0" -lsocket -lnsl );; esac EOS exit #endif #define gethostname __gethostname #define gethostbyaddr __gethostbyaddr #define sysinfo __sysinfo #define socket __socket #define bind __bind #define connect __connect #define sendto __sendto #define listen __listen #define uname __uname #ifdef __sun #define __gethostname _gethostname #define __gethostbyaddr _gethostbyaddr #define __sysinfo _sysinfo #define __socket _socket #define __bind _bind #define __connect _connect #define __sendto _sendto #define __listen _listen #define __uname _uname #endif #include #include #include #include #include #undef uname #undef listen #undef sendto #undef connect #undef bind #undef socket #undef sysinfo #undef gethostbyaddr #undef gethostname #include #include #include #include #include #include #include #include #include static fd_set socket_set; static struct sockaddr_in myself; static char *hostname; #ifdef __sun #undef __gethostbyaddr #define __gethostbyaddr gethostbyaddr_libnsl struct hostent *(*__gethostbyaddr) (const char *name, int len, int type); #endif #if defined(__SUNPRO_C) #pragma init(my_init) /* SPARCCompiler specific #pragma */ static #endif void my_init () { char *env=getenv("BIND_TO"); struct hostent *ret; #ifdef __sun void *libnsl; libnsl = dlopen ("libnsl.so.1",RTLD_LAZY); if (libnsl == NULL) fprintf (stderr,"Cannot dlopen(\"libnsl.so.1\")\n"), exit (255); __gethostbyaddr = (struct hostent *(*)(const char *,int,int)) dlsym (libnsl, "gethostbyaddr"); if (__gethostbyaddr == NULL) fprintf (stderr,"Cannot dlsym(\"gethostbyname\")\n"), exit (255); #endif if (env==NULL) fprintf (stderr,"${BIND_TO} is undefined\n"), exit(255); if (!(ret=gethostbyname (env))) fprintf (stderr,"unable to gethostbyname(\"%s\")\n",env), exit(255); memcpy (&myself.sin_addr,ret->h_addr,sizeof(myself.sin_addr)); myself.sin_family = AF_INET; if (isdigit(env[0])) { ret = __gethostbyaddr ((char *)&myself.sin_addr,sizeof(myself.sin_addr), myself.sin_family); if (ret != NULL) hostname = strdup (ret->h_name); } else hostname = strdup(env); } struct hostent *gethostbyaddr(const char *addr, int len, int type) { struct hostent *ret=__gethostbyaddr(addr,len,type); static struct hostent hostcopy; if (ret!=NULL && hostname && type==myself.sin_family && !memcmp (addr,&myself.sin_addr.s_addr,sizeof(myself.sin_addr))) { hostcopy = *ret, ret = &hostcopy, ret->h_name = hostname; } return ret; } long sysinfo(int command, char *buf, long count) { long ret=__sysinfo(command, buf, count); if (command==SI_HOSTNAME && ret>=0 && hostname) { if (count>0) strncpy (buf,hostname,count-1), buf[count-1]='\0'; return strlen (hostname) + 1; } return ret; } int gethostname(char *name, int len) { long ret=__sysinfo(SI_HOSTNAME, name, len); if (ret>=0 && hostname) strncpy (name,hostname,len); return ret==-1?-1:0; } int uname (struct utsname *name) { int ret=__uname (name); if (ret>=0 && hostname) strncpy (name->nodename,hostname,SYS_NMLN); return ret; } int socket (int domain, int type, int protocol) { int s = __socket (domain,type,protocol); if (s>=0 && s=0 && s=0 && s=0 && s=0 && s