]> git.saurik.com Git - wxWidgets.git/blob - src/common/sckaddr.cpp
GNOME/KDE integration for wxMimeTypeManager
[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 #if !USE_SHARED_LIBRARY
41 IMPLEMENT_ABSTRACT_CLASS(wxSockAddress, wxObject)
42 IMPLEMENT_DYNAMIC_CLASS(wxIPV4address, wxSockAddress)
43 #ifdef ENABLE_IPV6
44 IMPLEMENT_DYNAMIC_CLASS(wxIPV6address, wxSockAddress)
45 #endif
46 #ifdef __UNIX__
47 IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress, wxSockAddress)
48 #endif
49 #endif
50
51 // ---------------------------------------------------------------------------
52 // wxIPV4address
53 // ---------------------------------------------------------------------------
54
55 wxSockAddress::wxSockAddress()
56 {
57 m_address = GAddress_new();
58 }
59
60 wxSockAddress::~wxSockAddress()
61 {
62 GAddress_destroy(m_address);
63 }
64
65 void wxSockAddress::SetAddress(GAddress *address)
66 {
67 GAddress_destroy(m_address);
68 m_address = GAddress_copy(address);
69 }
70
71 const wxSockAddress& wxSockAddress::operator=(const wxSockAddress& addr)
72 {
73 SetAddress(addr.GetAddress());
74 return *this;
75 }
76
77 void wxSockAddress::CopyObject(wxObject& dest) const
78 {
79 wxSockAddress *addr = (wxSockAddress *)&dest;
80
81 wxObject::CopyObject(dest);
82 addr->SetAddress(GetAddress());
83 }
84
85 void wxSockAddress::Clear()
86 {
87 GAddress_destroy(m_address);
88 m_address = GAddress_new();
89 }
90
91 // ---------------------------------------------------------------------------
92 // wxIPV4address
93 // ---------------------------------------------------------------------------
94
95 wxIPV4address::wxIPV4address()
96 : wxSockAddress()
97 {
98 }
99
100 wxIPV4address::~wxIPV4address()
101 {
102 }
103
104 bool wxIPV4address::Hostname(const wxString& name)
105 {
106 // Some people are sometimes fool.
107 if (name == wxT(""))
108 {
109 wxLogWarning( _("Trying to solve a NULL hostname: giving up") );
110 return FALSE;
111 }
112
113 return (GAddress_INET_SetHostName(m_address, name.fn_str()) == GSOCK_NOERROR);
114 }
115
116 bool wxIPV4address::Hostname(unsigned long addr)
117 {
118 return (GAddress_INET_SetHostAddress(m_address, addr) == GSOCK_NOERROR);
119 }
120
121 bool wxIPV4address::Service(const wxString& name)
122 {
123 return (GAddress_INET_SetPortName(m_address, name.fn_str(), "tcp") == GSOCK_NOERROR);
124 }
125
126 bool wxIPV4address::Service(unsigned short port)
127 {
128 return (GAddress_INET_SetPort(m_address, port) == GSOCK_NOERROR);
129 }
130
131 bool wxIPV4address::LocalHost()
132 {
133 return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
134 }
135
136 wxString wxIPV4address::Hostname()
137 {
138 char hostname[1024];
139
140 hostname[0] = 0;
141 GAddress_INET_GetHostName(m_address, hostname, 1024);
142 return wxString(hostname);
143 }
144
145 unsigned short wxIPV4address::Service()
146 {
147 return GAddress_INET_GetPort(m_address);
148 }
149
150 #if 0
151 // ---------------------------------------------------------------------------
152 // wxIPV6address
153 // ---------------------------------------------------------------------------
154
155 wxIPV6address::wxIPV6address()
156 : wxSockAddress()
157 {
158 }
159
160 wxIPV6address::~wxIPV6address()
161 {
162 }
163
164 bool wxIPV6address::Hostname(const wxString& name)
165 {
166 return (GAddress_INET_SetHostName(m_address, name.fn_str()) == GSOCK_NOERROR);
167 }
168
169 bool wxIPV6address::Hostname(unsigned char addr[16])
170 {
171 return TRUE;
172 }
173
174 bool wxIPV6address::Service(const char *name)
175 {
176 return (GAddress_INET_SetPortName(m_address, name.fn_str()) == GSOCK_NOERROR);
177 }
178
179 bool wxIPV6address::Service(unsigned short port)
180 {
181 return (GAddress_INET_SetPort(m_address, port) == GSOCK_NOERROR);
182 }
183
184 bool wxIPV6address::LocalHost()
185 {
186 return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
187 }
188
189 const wxString& wxIPV6address::Hostname()
190 {
191 return wxString(GAddress_INET_GetHostName(m_address));
192 }
193
194 unsigned short wxIPV6address::Service()
195 {
196 return GAddress_INET_GetPort(m_address);
197 }
198
199 #endif
200
201 #ifdef __UNIX__
202 // ---------------------------------------------------------------------------
203 // wxUNIXaddress
204 // ---------------------------------------------------------------------------
205
206 wxUNIXaddress::wxUNIXaddress()
207 : wxSockAddress()
208 {
209 }
210
211 wxUNIXaddress::~wxUNIXaddress()
212 {
213 }
214
215 void wxUNIXaddress::Filename(const wxString& fname)
216 {
217 GAddress_UNIX_SetPath(m_address, fname.fn_str());
218 }
219
220 wxString wxUNIXaddress::Filename()
221 {
222 char path[1024];
223
224 path[0] = 0;
225 GAddress_UNIX_GetPath(m_address, path, 1024);
226 return wxString(path);
227 }
228
229 #endif
230
231 #endif
232 // wxUSE_SOCKETS