Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill old $Id$ command dating from CVS
[simgrid.git] / src / gras / Virtu / rl_dns.c
1 /* rl_dns - name resolution (real life)                                     */
2
3 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved.            */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "gras/Virtu/virtu_rl.h"
9 #include "portable.h"
10
11 /* A portable DNS resolver is a nightmare to do in a portable manner.
12    keep it simple/stupid for now. */
13
14 const char *gras_os_myname(void)
15 {
16   static char *myname = NULL;
17
18   if (myname)
19     return (const char *) myname;
20
21   myname = xbt_new(char, 255);
22
23   if (gethostname(myname, 255) == -1) {
24 #ifdef HAVE_SYS_SOCKET_H
25     /* gethostname() failed! Trying with localhost instead. 
26        We first need to query the DNS to make sure localhost is resolved 
27        See the note in nws/Portability/dnsutil.c about {end,set}hostent() */
28     struct hostent *tmp;
29     sethostent(0);
30     tmp = gethostbyname("localhost");
31     endhostent();
32
33     if (tmp) {
34       strncat(myname, tmp->h_name, 255);
35     } else {
36       /* Erm. localhost cannot be resolved. There's something wrong in the user DNS setting */
37       sprintf(myname, "(misconfigured host)");
38     }
39 #else
40     sprintf(myname, "(misconfigured windows host)");
41 #endif
42   }
43
44   myname[254] = '\0';
45
46   return myname;
47 }