OWLNext    7.0
Borland's Object Windows Library for the modern age
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
wskaddr.cpp
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// ObjectWindows
3// Copyright (c) 1995, 1996 by Borland International, All Rights Reserved
4//
5/// \file
6/// Winsock for OWL subsystem.
7/// Based on work by Paul Pedriana, 70541.3223@compuserve.com
8//----------------------------------------------------------------------------
9#include <owl/pch.h>
10#include <owl/defs.h>
11#include <owl/winsock.h>
12
13namespace owl {
14
16
17//
18/// Only the family is specified. The rest of the data is currently undefined.
19//
24
25//
26/// The argument is a socket address in network byte ordering.
27//
29{
31}
32
33//
34/// The argument address should be in network byte ordering.
35//
40
41//
42/// The argument address should be in network byte ordering.
43//
48
49//
50/// The argument address should be in network byte ordering.
51//
53{
54 SetAddress(src.sa_family, src.sa_data, 14);
55 return *this;
56}
57
58//
59/// Both addresses should be in the same byte ordering as each other.
60/// We just happen to know for a fact that the sockaddr's have same data as
61/// TSocketAddress's.
62//
64{
65 if (address1.sa_family != address2.sa_family)
66 return false;
67
68 for (int i = 0; i < 14; i++) {
69 if (address1.sa_data[i] != address2.sa_data[i])
70 return false;
71 }
72 return true;
73}
74
75//
76/// 'newFamily' is AF_INET, etc. 'data' is a pointer to data in the same format as
77/// sockaddr.sa_data[]. 'dataLength' is the length of data. It is limited to 14
78/// bytes in WinSock 1.1. The passed data should be in network byte ordering.
79//
81{
83 for (int i = 0; i < dataLength && i < 14; i++)
84 sa_data[i] = data[i];
85}
86
87//----------------------------------------------------------------------------
88
89//
90/// This function constructs an object. It is an empty constructor. It is useful for
91/// creating a TInetSocketAddress, but delaying the actual assignment of addressing
92/// data until later.
93//
95:
97{
98 SetAddress(AF_INET, 0, INADDR_ANY); // default values. Can be changed later.
99}
100
101//
102/// This function constructs an object. The argument should be in network byte
103/// ordering.
104//
110
111//
112/// This function constructs an object. You should pass nNewPort in network byte
113/// ordering. lNewAddress is in the format for numerical IP addressing (for example,
114/// 132.212.43.1). It cannot be in the form user\@place. nNewFamily is in the form
115/// AF_INET, etc.
116///
117/// All arguments should be in network byte ordering. newFamily is an
118/// enumeration, so there is no specific byte ordering. Note that address can
119/// also be INADDR_ANY, INADDR_LOOPBACK, INADDR_BROADCAST, INADDR_NONE. Also
120/// note that in winsock.h (1.1), Microsoft defines these addresses in network
121/// byte ordering format, so you don't need to convert to network byte ordering
122/// upon passing to this function or any other that expects network byte
123/// ordering.
124//
129
130//
131/// This function constructs an object. All arguments should be in network byte
132/// ordering. nNewFamily is an enumeration, so there is no specific byte ordering.
133/// \note The address can also be INADDR_ANY, INADDR_LOOPBACK, INADDR_BROADCAST, or
134/// INADDR_NONE.
135///
136/// Also note that in Windows Sockets, Microsoft defines these addresses using
137/// network byte ordering format. Therefore, you don't need to convert to network
138/// byte ordering upon passing to this function, or when passing to any other
139/// function that expects network byte ordering.
140///
141/// newPort should be passed in network byte ordering. newAddress is in the
142/// format of numerical IP addressing (e.g. "132.212.43.1"). It cannot be in
143/// the form of "user\@place" nFamily is in the form of AF_INET, etc.
144//
150
151//
152/// The argument should be in the same byte ordering (network) as this object.
153//
155{
156 TSocketAddress::SetAddress(newSockaddr.sa_family, newSockaddr.sa_data, 14);
157 return *this;
158}
159
160//
161/// This function accepts a character string IP address (for example,
162/// 162.132.211.204) and converts it to an IP address (ulong) if possible. The
163/// returned value is in network byte ordering. It should be INADDR_NONE if there
164/// was an error.
165//
170
171//
172/// This function accepts a ulong in the form of an IP binary address and converts
173/// it to a character string IP address (for example, 123.213.132.122), if possible.
174/// The lAddress parameter must be given in network byte ordering to work properly.
175/// Note that this function returns a character pointer to a string allocated by the
176/// system. The caller must immediately copy this data, and cannot modify or
177/// deallocate it. This restriction is imposed by Windows Sockets.
178//
188
189//
190/// This function can be used to tell if a character string points to an address in
191/// dotted-decimal IP format (for example, 162.132.211.204) or in name format (for
192/// example, hillary_clinton@wh.com).
193/// You could call this function if the user typed in a destination address in one
194/// of either of the above formats and you need to know which one it is, so you can
195/// convert it to a ulong IP address. If the address is dotted-decimal, you can
196/// simply use the TINetSocketAddress::ConvertAddress() function. Otherwise, you
197/// need to use one of the HostInfoManager functions.
198/// The szAddress is a pointer to a string that can be in any format, but most
199/// likely, one of either the dotted-decimal or the name formats mentioned above.
200/// This function's job is only to tell you if it is in dotted-decimal format or
201/// not. The return value is 1 if in dotted-decimal format, and 0 if not.
202//
203#if !defined(__GNUC__) //JJH removal of pragma warn for gcc
204#pragma warn -cln
205#endif
207{
210 return 0;
211 return 1;
212}
213#if !defined(__GNUC__) //JJH removal of pragma warn for gcc
214#pragma warn .cln
215#endif
216
217//
218/// This function makes an address out of the necessary IP address components.
219/// nNewFamily is an enumeration, for example, AF_INET. nNewPort is in network byte
220/// ordering, as is lNewAddress.
221//
230
231//
232/// This function returns the port in network byte ordering.
233//
235{
236 return ((sockaddr_in*)this)->sin_port;
237}
238
239//
240/// This function returns an IP binary address in network byte ordering.
241//
243{
244 return ((sockaddr_in*)this)->sin_addr.s_addr;
245}
246
247#if !defined(__GNUC__) //JJH removal of pragma warn for gcc
248#pragma warn -cln
249#endif
250
251/// This function returns 0 if the network cannot be determined.
253{
255
257 if (theINetClass == ClassA)
258 subnetMask = IN_CLASSA_NET; //0xFF000000L;
259 else if (theINetClass == ClassB)
260 subnetMask = IN_CLASSB_NET; //0xFFFF0000L;
261 else if (theINetClass == ClassC)
262 subnetMask = IN_CLASSC_NET; //0xFFFFFF00L;
263 else // ClassUnknown
264 return 0;
265
266 return GetNetwork(subnetMask);
267}
268#if !defined(__GNUC__) //JJH removal of pragma warn for gcc
269#pragma warn .cln
270#endif
271
272//
273/// This function returns the node component of the address with the subnet masked out.
274//
276{
278
280 if (theINetClass == ClassA)
281 subnetMask = 0xFF000000L;
282 else if (theINetClass == ClassB)
283 subnetMask = 0xFFFF0000L;
284 else if (theINetClass == ClassC)
285 subnetMask = 0xFFFFFF00L;
286 else //ClassUnknown
287 return 0;
288
289 return GetNode(subnetMask);
290}
291
292//
293/// This function sets the port of the addressing. It expects the argument to be in
294/// network byte ordering.
295//
297{
298 ((sockaddr_in*)this)->sin_port = port;
299}
300
301//
302/// This function sets the network address. It expects the argument to be in network
303/// byte ordering.
304//
306{
307 ((sockaddr_in*)this)->sin_addr.s_addr = address;
308}
309
310//
311/// This function sets the network address.
312//
317
318//
319/// This is an internal function used to zero out the unused data of the address.
320//
322{
323 for (int i = 0; i < 8; i++)
324 ((sockaddr_in*)this)->sin_zero[i] = 0;
325}
326
327#if !defined(__GNUC__) //JJH removal of pragma warn for gcc
328#pragma warn -cln
329#endif
330
331/// This function uses Windows Sockets macros to get standard class information. It
332/// does not know about subnetting or any classes beyond class C.
334{
336 return ClassA;
338 return ClassB;
340 return ClassC;
341 return ClassUnknown;
342}
343#if !defined(__GNUC__) //JJH removal of pragma warn for gcc
344#pragma warn .cln
345#endif
346
347} // OWL namespace
348/* ========================================================================== */
349
The TINetSocketAddress class encapsulates a Winsock Internet address.
Definition wskaddr.h:63
TINetClass
TINetClass is an enumeration that specifies which network class it belongs to.
Definition wskaddr.h:73
@ ClassUnknown
Unknown class net addressing.
Definition wskaddr.h:77
@ ClassB
B class net addressing.
Definition wskaddr.h:75
@ ClassA
A class net addressing.
Definition wskaddr.h:74
@ ClassC
C class net addressing.
Definition wskaddr.h:76
ulong GetNetworkAddress() const
This function returns an IP binary address in network byte ordering.
Definition wskaddr.cpp:242
ushort GetPort() const
This function returns the port in network byte ordering.
Definition wskaddr.cpp:234
ulong GetNetwork() const
This function returns 0 if the network cannot be determined.
Definition wskaddr.cpp:252
void SetFiller()
This is an internal function used to zero out the unused data of the address.
Definition wskaddr.cpp:321
TINetSocketAddress()
This function constructs an object.
Definition wskaddr.cpp:94
static short IsAddressDottedDecimal(const char *address)
This function can be used to tell if a character string points to an address in dotted-decimal IP for...
Definition wskaddr.cpp:206
ulong GetNode() const
This function returns the node component of the address with the subnet masked out.
Definition wskaddr.cpp:275
void SetNetworkAddress(ulong address)
This function sets the network address.
Definition wskaddr.cpp:305
void SetAddress(ushort newFamily, ushort newPort, ulong newAddress)
This function makes an address out of the necessary IP address components.
Definition wskaddr.cpp:222
static ulong ConvertAddress(const char *address)
This function accepts a character string IP address (for example, 162.132.211.204) and converts it to...
Definition wskaddr.cpp:166
TINetClass GetClass() const
This function uses Windows Sockets macros to get standard class information.
Definition wskaddr.cpp:333
void SetPort(ushort port)
This function sets the port of the addressing.
Definition wskaddr.cpp:296
TINetSocketAddress & operator=(const sockaddr &src)
The argument should be in the same byte ordering (network) as this object.
Definition wskaddr.cpp:154
The TSocketAddress class stores a Winsock socket address.
Definition wskaddr.h:38
void SetFamily(ushort family)
Sets the family of addressing this address belongs to.
Definition wskaddr.h:127
TSocketAddress & operator=(const sockaddr &src)
The argument address should be in network byte ordering.
Definition wskaddr.cpp:52
TSocketAddress()
Only the family is specified. The rest of the data is currently undefined.
Definition wskaddr.cpp:20
void SetAddress(ushort family, const char *data, short dataLength)
'newFamily' is AF_INET, etc.
Definition wskaddr.cpp:80
static ulong inet_addr(const char *cp)
Definition winsock.cpp:102
static char * inet_ntoa(struct in_addr in)
Definition winsock.cpp:109
int operator==(const TBitSet< T > &bs1, const TBitSet< T > &bs2)
Definition bitset.h:86
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
unsigned short ushort
Definition number.h:24
unsigned long ulong
Definition number.h:26
OWL_DIAGINFO
Definition animctrl.cpp:14
General definitions used by all ObjectWindows programs.
Main header of the Winsock OWL subsystem.