OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
wskhostm.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
15
17
18//
19//
20//
21DEFINE_RESPONSE_TABLE1(THostInfoWindow, TWindow)
24
25//
26/// The HostInfoWindow requires a HostInfoManager, so it can relay messages to it.
27//
29:
30 TWindow(0, _T("HostInfo Window")),
31 HostInfoManagerParent(hostInfoManagerParent)
32{
33 Attr.Style = 0; // Turn off WS_CHILD (the default) style).
34}
35
36//
37/// This is a relay function.
38//
44
45//----------------------------------------------------------------------------
46
47//
48/// Constructor of THostEntry object. Initializes members describing host to 0.
49//
51{
52 h_name = 0;
53 h_aliases = 0;
54 h_addrtype = 0;
55 h_length = 0;
56 h_addr_list = 0;
57}
58
59//
60/// Returns the number of pointers to addresses in the hostent (parent class of
61/// THostEntry).
62//
64{
65 int i = 0;
66 while (h_addr_list[i])
67 i++;
68
69 return i;
70}
71
72//
73/// This function returns the indexed internet address in unsigned long form. The
74/// index must be between 0 and the number of address counts (see
75/// GetAddressCount()).
76///
77/// \note This function is FLAWED in that it is internet addressing-specific
78/// (AF_INET/PF_INET).
79/// The proper way to implement this function would be to look at h_addrtype
80/// and h_length to determine the nature of the address type and return
81/// something useful or make a derived class that knows about each address
82/// family. (...)
83//
85{
87
88 if (index >= addressCount)
89 return 0; //The caller asked for an address index that is out of range.
90
91 return *((ulong*)h_addr_list[index]);
92}
93
94//----------------------------------------------------------------------------
95// THostInfoManager
96//
97
98//
99/// This function initializes the hidden window.
100//
102:
103 HostWindow(this)
104{
105 HostEntry = (THostEntry*)&HostInfoBuffer;
106 HostRequest = 0;
107 LastError = 0;
108 HostRequestCompleted = false;
109 try {
111 }
112 catch (...) {
113 //::MessageBeep(10); // !CQ Do something real here?
114 }
115}
116
117//
118/// With this destructor, you need to clear any pending requests before the
119/// deletion.
120//
122{
123 if (HostRequest && !HostRequestCompleted) // If there is an outstanding request...
125}
126
127//
128/// This function returns the name of the computer on which this program is running.
129/// The name parameter is set to the name. The return value is either WINSOCK_ERROR
130/// or WINSOCK_NOERROR. You can call THostInfoManager::GetLastError() to get the
131/// actual error value. name is a pointer to a preallocated buffer of minimum size
132/// of nameLength. nameLength should be at least N_MAX_HOST_NAME.
133//
142
143//
144/// This function effectively converts szHostName to szHostAddress. If you have a
145/// name such as "joe_schmoe@borland.com" and you want the dotted-decimal IP address
146/// for it, you can call this function. This function assumes that there is enough
147/// space in szHostAddress for the address. This function, like most of Windows
148/// Sockets, currently works only with IP addresses. Thus, the szHostAddress is
149/// always going to be dotted-decimal format in Windows Sockets.
150/// Note that when using the inet_ntoa() function, the char* string returned resides
151/// in Windows Sockets memory space, the szHostAddress returned is allocated and
152/// owned by the caller of this function and can be manipulated any way the caller
153/// wants.
154/// This function returns an error value of WINSOCK_ERROR or WINSOCK_NOERROR.
155//
165
166//
167/// This function effectively converts szHostName to a socket address. If you have a
168/// name such as "joe_schmoe@borland.com," and you want the TSocketAddress for it,
169/// you can call this function. This function, like most of Windows Sockets,
170/// currently works only with IP addresses. Thus, the szAddress is always going to
171/// be dotted-decimal format in Windows Sockets. The szHostName is a string
172/// parameter that specifies the host of which to get the address.
173/// This function returns an error value of WINSOCK_ERROR or WINSOCK_NOERROR.
174//
184
185//
186/// The caller of this function supplies a pointer to be assigned by this function.
187/// The caller need not allocate space for any THostEntry structure. Because of
188/// this, the data needs to be read immediately or copied for later use. hEntry is a
189/// pointer passed by reference. sAddress is a preallocated SocketAddress reference.
190/// Due to the design of the socket API, the call to gethostbyaddr currently
191/// requires a pointer to the Internet address, rather than a sockaddr or even a
192/// sockaddr_in. Because passing a ulong pointer would most likely not work (for
193/// example, if the socket API were to support something other than IP), this issue
194/// is fixed by making a sockaddr interface to this API. The address is in network
195/// byte ordering.
196//
198{
200 tempSockAddrIn->sin_addr.s_addr = tempSockAddrIn->sin_addr.s_addr;
201
203 (const char *)&tempSockAddrIn->sin_addr.s_addr,
204 sizeof(ulong), PF_INET);
205 if (!entry) {
207 return WINSOCK_ERROR;
208 }
209 return WINSOCK_NOERROR;
210}
211
212//
213/// Windows Sockets can block a call until the other end finishes the transaction.
214/// hEntry is a pointer passed by reference. The system will change that pointer to
215/// point to an internal Windows Sockets data structure. The contents must not be
216/// modified. szName is a preallocated string that holds a string. The address of
217/// the host can be in string format or in binary format.
218///
219/// The caller of this function passes a pointer to a THostEntry struct, for
220/// example:
221/// \code
222/// THostEntry* tempTHostEntry;
223/// GetHostInfo(tempTHostEntry, "JoeShmoe@anywhere.com");
224/// printf("%s", tempTHostEntry->h_name); //h_name should be "joeSchmoe@anywhere.com"
225/// \endcode
235
236//
237/// The caller can use this call to cancel the last pending request.
238//
240{
241 if (!hostRequest)
243
244 if (!hostRequest) {
245 LastError = WSAEINVAL; // There is no handle to use.
246 return WINSOCK_ERROR;
247 }
248
251 return WINSOCK_ERROR;
252 }
253
254 return WINSOCK_NOERROR;
255}
256
257//
258/// The TheHostRequest parameter is returned to the caller with the asynchrous
259/// request handle. sAddress needs to be in network byte ordering. Note that due to
260/// the design of this class, you cannot have two outstanding service requests that
261/// get notified directly to this class. You can use the hwnd-specific notification
262/// version of this function to manage multiple requests. You can also create more
263/// than one instance of this class. The service is complete when
264/// HostRequestCompleted is true. Look at LastError in this case to see if there was
265/// an error.
266///
267/// Do not issue any asynchronous calls that post to this class hwnd until the
268/// previous request is completed. The alternative is to create multiple
269/// THostInfoManagers or manage the call-backs yourself. See the comments about the
270/// non-asynchronous version of this call (THostInfoManager::GetHostInfo) for more
271/// information.
272//
290
291//
292/// Returns the same information as the other versions of GetHostInfoAsync. The
293/// difference is that the host name can be a string, rather than a TSocketAddress.
294//
308
309//
310/// This function notifies the given window that a request has completed. nMessage
311/// is the message that the wndNotify will receive. It defaults to
312/// MSG_HOST_INFO_NOTIFY, which is defined in the THostInfoManager's header file.
313/// The chBuffer is a pointer to the buffer that will be filled in with a hostent.
314/// It needs to be at least MAXGETHOSTSTRUCT bytes. If chBuffer is 0 (or not
315/// specified), the THostInfoManager's internal buffer is used. The hTheHostRequest
316/// will hold a handle that the caller can use to reference the request on
317/// call-back. wParam will be equal to the hService returned.
318/// WSAGETSYNCERROR (lParam) holds an error, if any (0 is OK). WSAGETSYNCBUFLEN
319/// (lParam) holds actual length of the buffer. When this function returns,
320/// myTHostEntry holds the appropriate information. Since this information belongs
321/// to this object, you can delay reading it as long as you want. Note that while
322/// the sAddress should be passed in network byte ordering, the output on callback
323/// is also in network ordering.
324//
347
348//
349/// This function notifies the given window that a request has completed, wndNotify
350/// is the window that will get the message that the request has completed. nMessage
351/// is the message that the wndNotify will receive. It defaults to
352/// MSG_HOST_INFO_NOTIFY, which is defined in the THostInfoManager's header file.
353/// hTheHostRequest is the asynchrous request handle that will be a reference to the
354/// request. szName is the name of the host, as in "coyote@acme.com." The chBuffer
355/// is a pointer to buffer that will be filled in with a hostent. It needs to be at
356/// least MAXGETHOSTSTRUCT bytes. If chBuffer is 0 (or not specified), the
357/// THostInfoManager's internal buffer will be used. The returned address is in
358/// network byte ordering.
359//
376
377//
378/// Given a THostEntry*, this function converts it to a dotted-decimal szAddress.
379/// Because Windows Sockets supports only IP addressing, this function uses IP
380/// addressing and the address is always dotted-decimal. The return value is
381/// WINSOCK_ERROR or WINSOCK_NOERROR.
382//
384{
386 tempInAddr.s_addr = *((ulong*)entry->h_addr);
387 //else: = *((ulong*)hEntry->h_addr_list[0])
388
390 if (!tempAddress)
391 return WINSOCK_ERROR;
393 return WINSOCK_NOERROR;
394}
395
396//
397/// Given a THostEntry*, this function converts it to a socket address. Because
398/// Windows Sockets supports only IP addressing, this function uses IP addressing
399/// and the address is an INetSocketAddress. The return value is WINSOCK_ERROR or
400/// WINSOCK_NOERROR.
401//
408
409//
410/// This function is called whenever an asynchronous request is completed. You may
411/// want to override this function in your THostInfoManager-derived class. If you
412/// do, you must call the base version.
413//
415{
416 int error = WSAGETASYNCERROR(result);
417 if (error != 0)
419 HostRequestCompleted = true; // Of course, there may have been an error.
420}
421
422} // OWL namespace
423
424/* ========================================================================== */
425
THostEntry encapsulates the attributes of a host (hostent).
Definition wskhostm.h:67
int GetAddressCount()
Returns the number of pointers to addresses in the hostent (parent class of THostEntry).
Definition wskhostm.cpp:63
ulong GetNthINetAddress(int nIndex=0)
An internet addressing -specific function.
Definition wskhostm.cpp:84
THostEntry()
Constructor of THostEntry object. Initializes members describing host to 0.
Definition wskhostm.cpp:50
The THostInfoManager class (and its friend class THostInfoWindow) encapsulate the Winsock database fu...
Definition wskhostm.h:87
THostInfoManager()
This function initializes the hidden window.
Definition wskhostm.cpp:101
int CancelHostRequest(HANDLE hTheHostRequest=0)
The caller can use this call to cancel the last pending request.
Definition wskhostm.cpp:239
static int HostEntryToAddress(THostEntry *hEntry, char *szAddress)
Given a THostEntry*, this function converts it to a dotted-decimal szAddress.
Definition wskhostm.cpp:383
int GetHostAddress(char *szHostAddress, const char *szHostName)
This function effectively converts szHostName to szHostAddress.
Definition wskhostm.cpp:156
HANDLE HostRequest
Handle of host to get info about.
Definition wskhostm.h:119
int LastError
Last error code.
Definition wskhostm.h:120
bool HostRequestCompleted
Flag if host completed last request.
Definition wskhostm.h:118
char HostInfoBuffer[MAXGETHOSTSTRUCT]
Used for calls to WSAAsync...()
Definition wskhostm.h:121
int GetHostInfo(THostEntry *&hEntry, const TSocketAddress &sAddress)
The caller of this function supplies a pointer to be assigned by this function.
Definition wskhostm.cpp:197
int GetHostName(char *name, int nameLength=N_MAX_HOST_NAME)
This function returns the name of the computer on which this program is running.
Definition wskhostm.cpp:134
int GetHostInfoAsync(HANDLE &hTheHostRequest, TSocketAddress &sAddress)
The TheHostRequest parameter is returned to the caller with the asynchrous request handle.
Definition wskhostm.cpp:273
THostInfoWindow HostWindow
Hidden window to catch notifications.
Definition wskhostm.h:122
virtual ~THostInfoManager()
With this destructor, you need to clear any pending requests before the deletion.
Definition wskhostm.cpp:121
void SetHostRequestCompleted(int error)
This function is called whenever an asynchronous request is completed.
Definition wskhostm.cpp:414
A private class created by THostInfoManager to catch WinSock messages.
Definition wskhostm.h:48
TResult DoNotification(TParam1, TParam2)
This is a relay function.
Definition wskhostm.cpp:39
The TINetSocketAddress class encapsulates a Winsock Internet address.
Definition wskaddr.h:63
The TSocketAddress class stores a Winsock socket address.
Definition wskaddr.h:38
static struct hostent * gethostbyname(LPCSTR name)
Definition winsock.cpp:222
static HANDLE WSAAsyncGetHostByAddr(HWND hWnd, uint msg, LPCSTR addr, int len, int type, char *buf, int bufLen)
Definition winsock.cpp:375
static int gethostname(char *name, int nameLen)
Definition winsock.cpp:230
static HANDLE WSAAsyncGetHostByName(HWND hWnd, uint msg, LPCSTR name, char *buf, int bufLen)
Definition winsock.cpp:366
static struct hostent * gethostbyaddr(LPCSTR addr, int len, int type)
Definition winsock.cpp:214
static int WSAGetLastError(void)
Definition winsock.cpp:294
static int WSACancelAsyncRequest(HANDLE hTaskHandle)
Definition winsock.cpp:384
static char * inet_ntoa(struct in_addr in)
Definition winsock.cpp:109
TWindow, derived from TEventHandler and TStreamableBase, provides window-specific behavior and encaps...
Definition window.h:414
virtual bool Create()
Creates the window interface element to be associated with this ObjectWindows interface element.
Definition window.cpp:2399
#define _T(x)
Definition cygwin.h:51
#define DEFINE_RESPONSE_TABLE1(cls, base)
Macro to define a response table for a class with one base.
Definition eventhan.h:492
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
unsigned long ulong
Definition number.h:26
LPARAM TParam2
Second parameter type.
Definition dispatch.h:55
WPARAM TParam1
First parameter type.
Definition dispatch.h:54
OWL_DIAGINFO
Definition animctrl.cpp:14
END_RESPONSE_TABLE
Definition button.cpp:26
unsigned int uint
Definition number.h:25
General definitions used by all ObjectWindows programs.
#define STATIC_CAST(targetType, object)
Definition defs.h:271
#define EV_MESSAGE(message, method)
Response table entry for raw message handling Uses a dispatcher that just forwards WPARAM and LPARAM.
Definition windowev.h:113
Main header of the Winsock OWL subsystem.
#define WINSOCK_NOERROR
Definition wskhostm.h:30
#define MSG_HOST_INFO_NOTIFY
Definition wskhostm.h:37
#define WINSOCK_ERROR
Definition wskhostm.h:31