Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
NetHost.cpp
Go to the documentation of this file.
1 /* Project nGenEx
2  Destroyer Studios LLC
3  Copyright © 1997-2004. All Rights Reserved.
4 
5  SUBSYSTEM: NetEx.lib
6  FILE: NetHost.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Network Host
13 */
14 
15 
16 #include "MemDebug.h"
17 #include "NetHost.h"
18 #include "NetLayer.h"
19 #include <ctype.h>
20 
22 {
23  char host_name[256];
24  gethostname(host_name, sizeof(host_name));
25 
26  Init(host_name);
27 }
28 
29 NetHost::NetHost(const char* host_name)
30 {
31  Init(host_name);
32 }
33 
34 void NetHost::Init(const char* host_name)
35 {
36  if (host_name && *host_name) {
37  HOSTENT* h = 0;
38 
39  if (isdigit(*host_name)) {
40  DWORD addr = inet_addr(host_name);
41  h = gethostbyaddr((const char*) &addr, 4, AF_INET);
42  }
43  else {
44  h = gethostbyname(host_name);
45  }
46 
47  if (h) {
48  name = h->h_name;
49 
50  char** alias = h->h_aliases;
51  while (*alias) {
52  aliases.append(new Text(*alias));
53  alias++;
54  }
55 
56  char** addr = h->h_addr_list;
57  while (*addr) {
58  NetAddr* pna = new(__FILE__,__LINE__) NetAddr(**(DWORD**) addr);
59  if (pna)
60  addresses.append(pna);
61  addr++;
62  }
63  }
64  }
65 }
66 
68 {
69  if (&n != this) {
70  NetHost& nh = (NetHost&) n;
71 
72  name = nh.name;
73 
74  ListIter<Text> alias = nh.aliases;
75  while (++alias)
76  aliases.append(new Text(*alias.value()));
77 
78  ListIter<NetAddr> addr = nh.addresses;
79  while (++addr)
80  addresses.append(new NetAddr(*addr.value()));
81  }
82 }
83 
85 {
86  aliases.destroy();
87  addresses.destroy();
88 }
89 
90 const char*
92 {
93  return name;
94 }
95 
96 NetAddr
98 {
99  if (addresses.size())
100  return *(addresses[0]);
101 
102  return NetAddr((DWORD) 0);
103 }