1 // =============================================================================
2 // PROJECT CHRONO - http://projectchrono.org
3 //
4 // Copyright (c) 2014 projectchrono.org
5 // All rights reserved.
6 //
7 // Use of this source code is governed by a BSD-style license that can be found
8 // in the LICENSE file at the top level of the distribution and at
9 // http://projectchrono.org/license-chrono.txt.
10 //
11 // =============================================================================
12 // Authors: Alessandro Tasora
13 // =============================================================================
14 
15 #ifndef CHHOSTINFO_H
16 #define CHHOSTINFO_H
17 
18 /*
19   Liyang Yu, Jan 9th, 2004, version 0.0
20 
21   this is to implement the domain and IP address resolution.
22 
23   3 cases are considered:
24 
25   1. a host name is given (a host name looks like "www.delta.com"), query
26      the IP address of the host
27 
28   2. an IP address is given (an IP address looks like 10.6.17.184), query
29      the host name
30 
31   in the above two cases, the IP address and the host name are the same thing:
32   since IP address is hard to remember, they are usually aliased by a name, and this
33   name is known as the host name.
34 
35   3. nothing is given. in other words, we don't know the host name or the IP address.
36      in this case, the standard host name for the current processor is used
37 */
38 
39 #include <string>
40 
41 #include "chrono_cosimulation/ChApiCosimulation.h"
42 
43 // This version is for both Windows and UNIX, the following statements
44 // are used to set the flags WINDOWS_XP or UNIX that in these few files of 'socket'
45 // code are used for conditional compilation
46 #if (defined _WIN32)
47 #define WINDOWS_XP
48 #endif
49 #if (defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__APPLE__))
50 #define UNIX
51 #endif
52 
53 #ifdef UNIX
54 #include <arpa/inet.h>
55 #include <netdb.h>
56 #include <netinet/in.h>
57 #include <sys/socket.h>
58 #else
59 #include <winsock2.h>
60 #endif
61 #include <cstdio>
62 
63 namespace chrono {
64 namespace cosimul {
65 
66 enum hostType { NAME, ADDRESS };
67 const int HOST_NAME_LENGTH = 64;
68 
69 /// Class for storing information about a TCP host
70 /// in socket communication, ex with an IP address.
71 
72 class ChApiCosimulation ChHostInfo {
73   private:
74 #ifdef UNIX
75     char searchHostDB;  ///< search the host database flag
76 #endif
77 
78     struct hostent* hostPtr;  ///< Entry within the host address database
79 
80   public:
81     /// Default constructor
82     ChHostInfo();
83 
84     /// Retrieves the host entry based on the host name or address.
85     ChHostInfo(const std::string& hostName, hostType type);
86 
87     /// Destructor.  Closes the host entry database.
~ChHostInfo()88     ~ChHostInfo() {
89 #ifdef UNIX
90         endhostent();
91 #endif
92     }
93 
94 #ifdef UNIX
95 
96     /// Retrieves the next host entry in the database.
97     char getNextHost();
98 
99     /// Opens the host entry database.
openHostDb()100     void openHostDb() {
101         endhostent();
102         searchHostDB = 1;
103         sethostent(1);
104     }
105 
106 #endif
107 
108     /// Retrieves the hosts IP address in dot x.y.z.w notation.
109     char* getHostIPAddress();
110 
111     /// Retrieves the hosts name.
getHostName()112     char* getHostName() { return hostPtr->h_name; }
113 
114   private:
115 #ifdef WINDOWS_XP
116     void detectErrorGethostbyname(int*, std::string&);
117     void detectErrorGethostbyaddr(int*, std::string&);
118 #endif
119 };
120 
121 }  // end namespace cosimul
122 }  // end namespace chrono
123 
124 #endif
125