Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
NetAddr.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: NetAddr.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Network Address
13 */
14 
15 
16 #include "MemDebug.h"
17 #include "NetAddr.h"
18 #include "NetHost.h"
19 #include "NetLayer.h"
20 #include <ctype.h>
21 
22 NetAddr::NetAddr(const char* host_name, WORD p)
23  : addr(0), port(p)
24 {
25  if (host_name && *host_name) {
26  HOSTENT* h = 0;
27 
28  if (isdigit(*host_name)) {
29  DWORD a = inet_addr(host_name);
30  h = gethostbyaddr((const char*) &a, 4, AF_INET);
31  }
32  else {
33  h = gethostbyname(host_name);
34  }
35 
36  if (h) {
37  if (h->h_addr_list) {
38  addr = **(DWORD**) (h->h_addr_list);
39  }
40  }
41  }
42 
43  Init();
44 }
45 
46 NetAddr::NetAddr(DWORD a, WORD p)
47  : addr(a), port(p)
48 {
49  Init();
50 }
51 
53  : addr(n.addr), port(n.port)
54 {
55  Init();
56 }
57 
58 // +--------------------------------------------------------------------+
59 
60 void
61 NetAddr::Init()
62 {
63  ZeroMemory(&sadr, sizeof(sadr));
64 
65  sadr.sin_family = AF_INET;
66  sadr.sin_port = ::htons(port);
67  sadr.sin_addr.s_addr = addr;
68 }
69 
70 void
72 {
73  addr = sadr.sin_addr.s_addr;
74  port = ::ntohs(sadr.sin_port);
75 }
76 
77 // +--------------------------------------------------------------------+
78 
79 sockaddr*
81 {
82  return (sockaddr*) &sadr;
83 }
84 
85 size_t
87 {
88  return sizeof(sadr);
89 }
90 
91 // +--------------------------------------------------------------------+
92 
93 void
94 NetAddr::SetSockAddr(sockaddr* s, int size)
95 {
96  if (s) {
97  ZeroMemory(&sadr, sizeof(sadr));
98 
99  if (size > sizeof(sadr))
100  CopyMemory(&sadr, s, sizeof(sadr));
101  else
102  CopyMemory(&sadr, s, size);
103 
105  }
106 }
107