]> git.saurik.com Git - wxWidgets.git/blob - src/common/sckaddr.cpp
Committing in .
[wxWidgets.git] / src / common / sckaddr.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: sckaddr.cpp
3 // Purpose: Network address manager
4 // Author: Guilhem Lavaux
5 // Modified by:
6 // Created: 26/04/97
7 // RCS-ID: $Id$
8 // Copyright: (c) 1997, 1998 Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "sckaddr.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #if wxUSE_SOCKETS
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <ctype.h>
28
29 #if !defined(__MWERKS__) && !defined(__SALFORDC__)
30 #include <memory.h>
31 #endif
32
33 #include "wx/defs.h"
34 #include "wx/object.h"
35 #include "wx/log.h"
36 #include "wx/intl.h"
37 #include "wx/gsocket.h"
38 #include "wx/sckaddr.h"
39
40 IMPLEMENT_ABSTRACT_CLASS(wxSockAddress, wxObject)
41 IMPLEMENT_DYNAMIC_CLASS(wxIPV4address, wxSockAddress)
42 #ifdef ENABLE_IPV6
43 IMPLEMENT_DYNAMIC_CLASS(wxIPV6address, wxSockAddress)
44 #endif
45 #if defined(__UNIX__) && !defined(__WXMAC__)
46 IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress, wxSockAddress)
47 #endif
48
49 // ---------------------------------------------------------------------------
50 // wxIPV4address
51 // ---------------------------------------------------------------------------
52
53 wxSockAddress::wxSockAddress()
54 {
55 m_address = GAddress_new();
56 }
57
58 wxSockAddress::wxSockAddress(const wxSockAddress& other)
59 {
60 m_address = GAddress_copy(other.m_address);
61 }
62
63 wxSockAddress::~wxSockAddress()
64 {
65 GAddress_destroy(m_address);
66 }
67
68 void wxSockAddress::SetAddress(GAddress *address)
69 {
70 GAddress_destroy(m_address);
71 m_address = GAddress_copy(address);
72 }
73
74 wxSockAddress& wxSockAddress::operator=(const wxSockAddress& addr)
75 {
76 SetAddress(addr.GetAddress());
77 return *this;
78 }
79
80 void wxSockAddress::Clear()
81 {
82 GAddress_destroy(m_address);
83 m_address = GAddress_new();
84 }
85
86 // ---------------------------------------------------------------------------
87 // wxIPV4address
88 // ---------------------------------------------------------------------------
89
90 wxIPV4address::wxIPV4address()
91 {
92 }
93
94 wxIPV4address::wxIPV4address(const wxIPV4address& other)
95 : wxSockAddress(other)
96 {
97 }
98
99 wxIPV4address::~wxIPV4address()
100 {
101 }
102
103 bool wxIPV4address::Hostname(const wxString& name)
104 {
105 // Some people are sometimes fool.
106 if (name == wxT(""))
107 {
108 wxLogWarning( _("Trying to solve a NULL hostname: giving up") );
109 return FALSE;
110 }
111
112 return (GAddress_INET_SetHostName(m_address, name.mb_str()) == GSOCK_NOERROR);
113 }
114
115 bool wxIPV4address::Hostname(unsigned long addr)
116 {
117 return (GAddress_INET_SetHostAddress(m_address, addr) == GSOCK_NOERROR);
118 }
119
120 bool wxIPV4address::Service(const wxString& name)
121 {
122 return (GAddress_INET_SetPortName(m_address, name.mb_str(), "tcp") == GSOCK_NOERROR);
123 }
124
125 bool wxIPV4address::Service(unsigned short port)
126 {
127 return (GAddress_INET_SetPort(m_address, port) == GSOCK_NOERROR);
128 }
129
130 bool wxIPV4address::LocalHost()
131 {
132 return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
133 }
134
135 bool wxIPV4address::AnyAddress()
136 {
137 return (GAddress_INET_SetAnyAddress(m_address) == GSOCK_NOERROR);
138 }
139
140 wxString wxIPV4address::Hostname()
141 {
142 char hostname[1024];
143
144 hostname[0] = 0;
145 GAddress_INET_GetHostName(m_address, hostname, 1024);
146 return wxString(hostname);
147 }
148
149 unsigned short wxIPV4address::Service()
150 {
151 return GAddress_INET_GetPort(m_address);
152 }
153
154 #if 0
155 // ---------------------------------------------------------------------------
156 // wxIPV6address
157 // ---------------------------------------------------------------------------
158
159 wxIPV6address::wxIPV6address()
160 : wxSockAddress()
161 {
162 }
163
164 wxIPV6address::wxIPV6address(const wxIPV6address& other)
165 : wxSockAddress(other)
166 {
167 }
168
169 wxIPV6address::~wxIPV6address()
170 {
171 }
172
173 bool wxIPV6address::Hostname(const wxString& name)
174 {
175 return (GAddress_INET_SetHostName(m_address, name.fn_str()) == GSOCK_NOERROR);
176 }
177
178 bool wxIPV6address::Hostname(unsigned char addr[16])
179 {
180 return TRUE;
181 }
182
183 bool wxIPV6address::Service(const char *name)
184 {
185 return (GAddress_INET_SetPortName(m_address, name.fn_str()) == GSOCK_NOERROR);
186 }
187
188 bool wxIPV6address::Service(unsigned short port)
189 {
190 return (GAddress_INET_SetPort(m_address, port) == GSOCK_NOERROR);
191 }
192
193 bool wxIPV6address::LocalHost()
194 {
195 return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
196 }
197
198 const wxString& wxIPV6address::Hostname()
199 {
200 return wxString(GAddress_INET_GetHostName(m_address));
201 }
202
203 unsigned short wxIPV6address::Service()
204 {
205 return GAddress_INET_GetPort(m_address);
206 }
207
208 #endif // 0
209
210 #if defined(__UNIX__) && !defined(__WXMAC__)
211
212 // ---------------------------------------------------------------------------
213 // wxUNIXaddress
214 // ---------------------------------------------------------------------------
215
216 wxUNIXaddress::wxUNIXaddress()
217 : wxSockAddress()
218 {
219 }
220
221 wxUNIXaddress::wxUNIXaddress(const wxUNIXaddress& other)
222 : wxSockAddress(other)
223 {
224 }
225
226 wxUNIXaddress::~wxUNIXaddress()
227 {
228 }
229
230 void wxUNIXaddress::Filename(const wxString& fname)
231 {
232 GAddress_UNIX_SetPath(m_address, fname.fn_str());
233 }
234
235 wxString wxUNIXaddress::Filename()
236 {
237 char path[1024];
238
239 path[0] = 0;
240 GAddress_UNIX_GetPath(m_address, path, 1024);
241 return wxString(path);
242 }
243
244 #endif // __UNIX__
245
246 #endif
247 // wxUSE_SOCKETS