Logo AND Algorithmique Numérique Distribuée

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