Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
missing header
[simgrid.git] / src / gras / Virtu / rl_dns.c
1 /* $Id$ */
2
3 /* rl_dns - name resolution (real life)                                     */
4
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved.            */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "gras/Virtu/virtu_rl.h"
11 #include <netdb.h>  /* {end,set}hostent() gethostby{addr,name}() */
12 #include <unistd.h> /* gethostname */
13
14 /* A portable DNS resolver is a nightmare to do in a portable manner.
15    keep it simple/stupid for now. */
16
17 const char *gras_os_myname(void) {
18   static char *myname = NULL;
19  
20   if (myname)
21     return (const char*) myname; 
22  
23   myname = xbt_new(char, 255);
24     
25   if (gethostname(myname, 255) == -1) {
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   }
41    
42   myname[254] = '\0';
43  
44   return myname;
45 }
46