Path: news.cs.au.dk!not-for-mail From: Erik Corry Newsgroups: comp.lang.beta Subject: Re: What about gethostbyaddr(ess)? Date: 7 Mar 2000 20:56:46 -0000 Organization: University of Aarhus, Department of Computer Science (DAIMI) Lines: 151 Approved: mailtonews@cs.au.dk Distribution: world Message-ID: <20000307205646.13463.qmail@noatun.mjolner.dk> Reply-To: Erik Corry NNTP-Posting-Host: daimi.cs.au.dk X-Trace: xinwen.cs.au.dk 952462622 13051572 255.255.255.255 (7 Mar 2000 20:57:02 GMT) X-Complaints-To: news@cs.au.dk NNTP-Posting-Date: 7 Mar 2000 20:57:02 GMT Xref: news.cs.au.dk comp.lang.beta:12259 Hi Sascha Kimmel, > Hi, > > I've just found out that there's no such function "gethostbyaddress" > available in ~beta/process. > So what I need is the following: > e.g. > localhost_IP_address->getHostByAddress->putLine; > This should - in many cases - lead to "localhost". > > Could you please tell me HOW to achieve that? > Thanks in advance! You can't do this entirely in BETA, you have to use a little C, mainly because the constant AF_INET is system-specific and defined as a C preprocessor token. I added getHostByAddress to the next version of the BETA process library, here are the changes in context diff format. If you have the 'patch' program installed you can make these changes automatically by saving this email (with DOS lineendings) and doing: cd betahomedirectory/process patch -p1 < thisemail.txt otherwise you can do it manually, simply by adding the lines marked with '+' to the files mentioned in the lines marked '+++' at about the line numbers marked '@@'. See http://www.gnu.org/manual/diffutils/html_mono/diff.html#SEC20 for details. Index: process/commaddress.bet diff -u process/commaddress.bet:1.6 process/commaddress.bet:1.7 --- process/commaddress.bet:1.6 Thu Aug 6 13:55:34 1998 +++ process/commaddress.bet Tue Mar 7 20:17:22 2000 @@ -367,6 +367,17 @@ exit inadr #); +(* Look up the name of a given IPv4 address. *) +gethostbyaddr: + (# + notfound:< Exception; + name: ^Text; + inadr: @Integer; + enter inadr + <> + exit name[] + #); + (* Find the name and IPv4 address of this host. *) thisHost: (# name: ^Text; Index: process/private/commaddressbody.bet diff -u process/private/commaddressbody.bet:1.7 process/private/commaddressbody.bet:1.8 --- process/private/commaddressbody.bet:1.7 Wed Jan 6 16:27:27 1999 +++ process/private/commaddressbody.bet Tue Mar 7 20:17:22 2000 @@ -489,6 +489,16 @@ else INNER if); + +--- CommAddrgethostbyaddr:dopart --- +do initSockets; + &Text[] -> name[]; + inadr -> inetAddr2host -> name; + (if name.length = 0 then + notfound + else + INNER + if); --- CommAddrthisHost:dopart --- do initSockets; Index: process/private/socketsint.bet diff -u process/private/socketsint.bet:1.12 process/private/socketsint.bet:1.13 --- process/private/socketsint.bet:1.12 Mon Dec 7 13:09:08 1998 +++ process/private/socketsint.bet Tue Mar 7 20:17:23 2000 @@ -30,6 +30,18 @@ exit inetAddr #); +inetAddr2host: external + (* Find the hostname (text) of the host with the internet + * address inetAddr. In case of failure a pointer to a zero + * length string is returned. + *) + (# inetAddr: @integer; + hostname: [1]@char; + enter inetAddr + do 'inetAddr2host' -> callC + exit hostname + #); + nameOfThisHost: external (# pointerToErrorCode: @integer; hostname: [1]@char; That's the end of the diff. That BETA code requires a little C code to work, like I said. In the next version of BETA it will be automatically there, but for now you will have to add it manually. You can do that by adding the following to the top of your BETA file (just after the INCLUDE directives: BUILD default 'stuff.o' 'stuff.c' '$CC -c -o $0 $1'; and then putting the following in stuff.c: /* * may need to tune these for your C compiler installation */ #include #include /* * Find host by number. Takes IP-address in host-byteorder. * Returns host name as string. */ char const *inetAddr2host(signed long addr) { struct hostent *pHostInfo; char const *hostname; if (addr == 0x7f000001) { hostname = "localhost"; } else { struct in_addr inaddr; inaddr.s_addr = htonl(addr); pHostInfo = gethostbyaddr((const char *)(&inaddr), sizeof(inaddr), AF_INET); if (pHostInfo) { hostname = pHostInfo->h_name; } else { return ""; } } return hostname; } If you use MCVS instead of gcc then you may need to tune the C code parts, in particular you will need to include instead of and you will need to use a different compiler and perhaps options to compile the C code. Good luck. -- Erik Corry Mjølner Informatics