]>
Commit | Line | Data |
---|---|---|
f4ada568 GL |
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__ | |
2df7be7f | 13 | #pragma implementation "sckaddr.h" |
f4ada568 GL |
14 | #endif |
15 | ||
fcc6dddd JS |
16 | // For compilers that support precompilation, includes "wx.h". |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
2df7be7f | 20 | #pragma hdrstop |
fcc6dddd JS |
21 | #endif |
22 | ||
35a4dab7 GL |
23 | #if wxUSE_SOCKETS |
24 | ||
f4ada568 GL |
25 | #include <stdio.h> |
26 | #include <stdlib.h> | |
27 | #include <ctype.h> | |
ce3ed50d JS |
28 | |
29 | #if !defined(__MWERKS__) && !defined(__SALFORDC__) | |
f4ada568 | 30 | #include <memory.h> |
469e1e5c | 31 | #endif |
f4ada568 | 32 | |
3096bd2f VZ |
33 | #include "wx/defs.h" |
34 | #include "wx/object.h" | |
35 | #include "wx/log.h" | |
58c837a4 | 36 | #include "wx/intl.h" |
3096bd2f VZ |
37 | #include "wx/gsocket.h" |
38 | #include "wx/sckaddr.h" | |
f4ada568 | 39 | |
f4ada568 GL |
40 | IMPLEMENT_ABSTRACT_CLASS(wxSockAddress, wxObject) |
41 | IMPLEMENT_DYNAMIC_CLASS(wxIPV4address, wxSockAddress) | |
42 | #ifdef ENABLE_IPV6 | |
43 | IMPLEMENT_DYNAMIC_CLASS(wxIPV6address, wxSockAddress) | |
44 | #endif | |
0cbb9297 | 45 | #if defined(__UNIX__) && !defined(__WXMAC__) |
f4ada568 GL |
46 | IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress, wxSockAddress) |
47 | #endif | |
f4ada568 | 48 | |
a324a7bc GL |
49 | // --------------------------------------------------------------------------- |
50 | // wxIPV4address | |
51 | // --------------------------------------------------------------------------- | |
52 | ||
53 | wxSockAddress::wxSockAddress() | |
69919241 | 54 | { |
a324a7bc | 55 | m_address = GAddress_new(); |
f4ada568 GL |
56 | } |
57 | ||
1539c2f5 VZ |
58 | wxSockAddress::wxSockAddress(const wxSockAddress& other) |
59 | { | |
60 | m_address = GAddress_copy(other.m_address); | |
61 | } | |
62 | ||
a324a7bc | 63 | wxSockAddress::~wxSockAddress() |
f4ada568 | 64 | { |
a324a7bc | 65 | GAddress_destroy(m_address); |
f4ada568 GL |
66 | } |
67 | ||
a324a7bc | 68 | void wxSockAddress::SetAddress(GAddress *address) |
f4ada568 | 69 | { |
a324a7bc GL |
70 | GAddress_destroy(m_address); |
71 | m_address = GAddress_copy(address); | |
f4ada568 GL |
72 | } |
73 | ||
1539c2f5 | 74 | wxSockAddress& wxSockAddress::operator=(const wxSockAddress& addr) |
f4ada568 | 75 | { |
a324a7bc GL |
76 | SetAddress(addr.GetAddress()); |
77 | return *this; | |
f4ada568 GL |
78 | } |
79 | ||
a324a7bc | 80 | void wxSockAddress::Clear() |
acd15a3f | 81 | { |
a324a7bc GL |
82 | GAddress_destroy(m_address); |
83 | m_address = GAddress_new(); | |
f4ada568 | 84 | } |
f4ada568 | 85 | |
a324a7bc GL |
86 | // --------------------------------------------------------------------------- |
87 | // wxIPV4address | |
88 | // --------------------------------------------------------------------------- | |
89 | ||
90 | wxIPV4address::wxIPV4address() | |
1539c2f5 VZ |
91 | { |
92 | } | |
93 | ||
94 | wxIPV4address::wxIPV4address(const wxIPV4address& other) | |
95 | : wxSockAddress(other) | |
f4ada568 | 96 | { |
a324a7bc | 97 | } |
f4ada568 | 98 | |
a324a7bc GL |
99 | wxIPV4address::~wxIPV4address() |
100 | { | |
101 | } | |
f4ada568 | 102 | |
a324a7bc GL |
103 | bool wxIPV4address::Hostname(const wxString& name) |
104 | { | |
f61815af | 105 | // Some people are sometimes fool. |
acd15a3f | 106 | if (name == wxT("")) |
58c837a4 RR |
107 | { |
108 | wxLogWarning( _("Trying to solve a NULL hostname: giving up") ); | |
f61815af GL |
109 | return FALSE; |
110 | } | |
ce22d615 | 111 | m_origHostname = name; |
f6bcfd97 | 112 | return (GAddress_INET_SetHostName(m_address, name.mb_str()) == GSOCK_NOERROR); |
f4ada568 GL |
113 | } |
114 | ||
115 | bool wxIPV4address::Hostname(unsigned long addr) | |
116 | { | |
ce22d615 RD |
117 | bool rv = (GAddress_INET_SetHostAddress(m_address, addr) == GSOCK_NOERROR); |
118 | if (rv) | |
119 | m_origHostname = Hostname(); | |
120 | else | |
121 | m_origHostname = ""; | |
122 | return rv; | |
f4ada568 GL |
123 | } |
124 | ||
125 | bool wxIPV4address::Service(const wxString& name) | |
126 | { | |
f6bcfd97 | 127 | return (GAddress_INET_SetPortName(m_address, name.mb_str(), "tcp") == GSOCK_NOERROR); |
f4ada568 GL |
128 | } |
129 | ||
130 | bool wxIPV4address::Service(unsigned short port) | |
131 | { | |
a324a7bc | 132 | return (GAddress_INET_SetPort(m_address, port) == GSOCK_NOERROR); |
f4ada568 GL |
133 | } |
134 | ||
135 | bool wxIPV4address::LocalHost() | |
136 | { | |
a324a7bc | 137 | return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR); |
f4ada568 GL |
138 | } |
139 | ||
d3ea6527 GRG |
140 | bool wxIPV4address::AnyAddress() |
141 | { | |
142 | return (GAddress_INET_SetAnyAddress(m_address) == GSOCK_NOERROR); | |
143 | } | |
144 | ||
f4ada568 GL |
145 | wxString wxIPV4address::Hostname() |
146 | { | |
a324a7bc | 147 | char hostname[1024]; |
f4ada568 | 148 | |
a324a7bc GL |
149 | hostname[0] = 0; |
150 | GAddress_INET_GetHostName(m_address, hostname, 1024); | |
151 | return wxString(hostname); | |
f4ada568 GL |
152 | } |
153 | ||
a324a7bc | 154 | unsigned short wxIPV4address::Service() |
f4ada568 | 155 | { |
acd15a3f | 156 | return GAddress_INET_GetPort(m_address); |
f4ada568 GL |
157 | } |
158 | ||
ce22d615 RD |
159 | wxSockAddress *wxIPV4address::Clone() const |
160 | { | |
161 | wxIPV4address *addr = new wxIPV4address(*this); | |
162 | addr->m_origHostname = m_origHostname; | |
163 | return addr; | |
164 | } | |
165 | ||
5a96d2f4 | 166 | #if 0 |
a324a7bc GL |
167 | // --------------------------------------------------------------------------- |
168 | // wxIPV6address | |
169 | // --------------------------------------------------------------------------- | |
f4ada568 GL |
170 | |
171 | wxIPV6address::wxIPV6address() | |
a324a7bc | 172 | : wxSockAddress() |
f4ada568 | 173 | { |
f4ada568 GL |
174 | } |
175 | ||
1539c2f5 VZ |
176 | wxIPV6address::wxIPV6address(const wxIPV6address& other) |
177 | : wxSockAddress(other) | |
178 | { | |
179 | } | |
180 | ||
f4ada568 GL |
181 | wxIPV6address::~wxIPV6address() |
182 | { | |
183 | } | |
184 | ||
f4ada568 GL |
185 | bool wxIPV6address::Hostname(const wxString& name) |
186 | { | |
a324a7bc | 187 | return (GAddress_INET_SetHostName(m_address, name.fn_str()) == GSOCK_NOERROR); |
f4ada568 GL |
188 | } |
189 | ||
190 | bool wxIPV6address::Hostname(unsigned char addr[16]) | |
191 | { | |
f4ada568 GL |
192 | return TRUE; |
193 | } | |
194 | ||
195 | bool wxIPV6address::Service(const char *name) | |
196 | { | |
a324a7bc | 197 | return (GAddress_INET_SetPortName(m_address, name.fn_str()) == GSOCK_NOERROR); |
f4ada568 GL |
198 | } |
199 | ||
200 | bool wxIPV6address::Service(unsigned short port) | |
201 | { | |
a324a7bc | 202 | return (GAddress_INET_SetPort(m_address, port) == GSOCK_NOERROR); |
f4ada568 GL |
203 | } |
204 | ||
205 | bool wxIPV6address::LocalHost() | |
206 | { | |
a324a7bc | 207 | return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR); |
f4ada568 GL |
208 | } |
209 | ||
210 | const wxString& wxIPV6address::Hostname() | |
211 | { | |
a324a7bc | 212 | return wxString(GAddress_INET_GetHostName(m_address)); |
f4ada568 GL |
213 | } |
214 | ||
215 | unsigned short wxIPV6address::Service() | |
216 | { | |
acd15a3f | 217 | return GAddress_INET_GetPort(m_address); |
f4ada568 GL |
218 | } |
219 | ||
1539c2f5 | 220 | #endif // 0 |
f4ada568 | 221 | |
0cbb9297 | 222 | #if defined(__UNIX__) && !defined(__WXMAC__) |
1539c2f5 | 223 | |
a324a7bc GL |
224 | // --------------------------------------------------------------------------- |
225 | // wxUNIXaddress | |
226 | // --------------------------------------------------------------------------- | |
f4ada568 GL |
227 | |
228 | wxUNIXaddress::wxUNIXaddress() | |
1539c2f5 VZ |
229 | : wxSockAddress() |
230 | { | |
231 | } | |
232 | ||
233 | wxUNIXaddress::wxUNIXaddress(const wxUNIXaddress& other) | |
234 | : wxSockAddress(other) | |
f4ada568 | 235 | { |
f4ada568 GL |
236 | } |
237 | ||
238 | wxUNIXaddress::~wxUNIXaddress() | |
239 | { | |
240 | } | |
241 | ||
f4ada568 GL |
242 | void wxUNIXaddress::Filename(const wxString& fname) |
243 | { | |
a324a7bc | 244 | GAddress_UNIX_SetPath(m_address, fname.fn_str()); |
f4ada568 GL |
245 | } |
246 | ||
247 | wxString wxUNIXaddress::Filename() | |
248 | { | |
a324a7bc | 249 | char path[1024]; |
f4ada568 | 250 | |
a324a7bc GL |
251 | path[0] = 0; |
252 | GAddress_UNIX_GetPath(m_address, path, 1024); | |
253 | return wxString(path); | |
f4ada568 GL |
254 | } |
255 | ||
1539c2f5 | 256 | #endif // __UNIX__ |
35a4dab7 | 257 | |
acd15a3f | 258 | #endif |
35a4dab7 | 259 | // wxUSE_SOCKETS |