1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
|
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>
<TITLE>nevrax.org : docs</TITLE>
<LINK REL=stylesheet TYPE="text/css" HREF="http://www.nevrax.org/inc/css/nevrax.css">
<link href="doxygen.css" rel="stylesheet" type="text/css">
</HEAD>
<BODY MARGINHEIGHT="0" MARGINWIDTH="0">
<!-- uplinks -->
<TABLE CELLSPACING=0 CELLPADDING=0 BORDER=0>
<TR>
<TD WIDTH=16><IMG SRC="http://www.nevrax.org/inc/img/pixel.gif" WIDTH="16" HEIGHT="16" BORDER=0 ALT=""></TD>
<TD WIDTH=140 BGCOLOR=#dddddd><IMG SRC="http://www.nevrax.org/inc/img/pixel.gif" WIDTH="140" HEIGHT="16" BORDER=0 ALT=""></TD>
<TD WIDTH=16><IMG SRC="http://www.nevrax.org/inc/img/pixel.gif" WIDTH="16" HEIGHT="16" BORDER=0 ALT=""></TD>
<TD><IMG width=6 height=14 SRC="http://www.nevrax.org/inc/img/reddots.gif" ALT="#" VSPACE=2 HSPACE=2 BORDER=0 ></TD><TD VALIGN=middle> <A CLASS=uplinks HREF=http://www.nevrax.org><b>Home</B></FONT></A> </TD>
<TD><IMG width=6 height=14 SRC="http://www.nevrax.org/inc/img/reddots.gif" ALT="#" VSPACE=2 HSPACE=2 BORDER=0 ></TD><TD VALIGN=middle> <A CLASS=uplinks HREF=http://www.nevrax.com><b>nevrax.com</B></FONT></A> </TD>
</TR>
</TABLE>
<!-- banner Nevrax -->
<TABLE CELLSPACING=0 CELLPADDING=0 BORDER=0 WIDTH=100%>
<TR><TD BGCOLOR="#000000" BACKGROUND="http://www.nevrax.org/inc/img/black_banner.jpg"><A HREF="http://www.nevrax.org"><IMG SRC="http://www.nevrax.org/inc/img/nevrax.gif" WIDTH="170" HEIGHT="45" BORDER=0 ALT="Nevrax" ></A></TD></TR>
</TABLE>
<!-- main table -->
<TABLE CELLSPACING=0 CELLPADDING=0 BORDER=0 height=100%>
<TR>
<TD WIDTH=16><IMG SRC="http://www.nevrax.org/inc/img/pixel.gif" WIDTH="16" HEIGHT="10" BORDER=0 ALT=""></TD>
<TD WIDTH=140 BGCOLOR=#dddddd VALIGN=TOP ALIGN=middle><IMG SRC="http://www.nevrax.org/inc/img/pixel.gif" WIDTH="140" HEIGHT="10" BORDER=0 ALT="">
<!------ Begin Box ------>
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 BGCOLOR=black><TR><TD><TABLE border=0 cellspacing=2 cellpadding=0 width=120><tr><TD ALIGN=middle bgcolor=black>
<FONT COLOR=white FACE="sans-serif"><B>Nevrax.org</B></FONT></TD></TR><tr><td colspan=2 bgcolor=#FFFFFF>
<TABLE cellspacing=0 cellpadding=1 border=0>
<tr><td ALIGN=middle><a class='linkbox' href="http://www.nevrax.org/news/" TITLE="Rubrique news"><img width=13 height=15 hspace=5 border=0 src=http://www.nevrax.org/inc/img/picto-news.gif ALT=#></A></td><td><a class='linkbox' href="http://www.nevrax.org/news/" TITLE="News">News</a></td></tr>
<tr><td ALIGN=middle><a class='linkbox' href="http://www.nevrax.org/mail/" TITLE="Rubrique mail"><img width=15 height=11 hspace=5 border=0 src=http://www.nevrax.org/inc/img/picto-mail.gif ALT=#></A></td><td><a class='linkbox' href="http://www.nevrax.org/mail/" TITLE="Mailing list archive">Mailing-list</a></td></tr>
<tr><td ALIGN=middle><a class='linkbox' href="http://www.nevrax.org/docs/" TITLE="Rubrique docs"><img width=14 height=16 hspace=5 border=0 src=http://www.nevrax.org/inc/img/picto-docs.gif ALT=#></A></td><td><a class='linkbox' href="http://www.nevrax.org/docs/" TITLE="Documentation">Documentation</a></td></tr>
<tr><td ALIGN=middle><a class='linkbox' href="http://www.nevrax.org/cvs/" TITLE="Rubrique cvs"><img width=13 height=17 hspace=5 border=0 src=http://www.nevrax.org/inc/img/picto-cvs.gif ALT=#></A></td><td><a class='linkbox' href="http://www.nevrax.org/cvs/" TITLE="CVS Web">CVS</a></td></tr>
<tr><td ALIGN=middle><a class='linkbox' href="http://www.nevrax.org/bugs/" TITLE="Rubrique bugs"><img width=20 height=16 hspace=5 border=0 src=http://www.nevrax.org/inc/img/picto-bugs.gif ALT=#></A></td><td><a class='linkbox' href="http://www.nevrax.org/bugs/" TITLE="Bugtracking">Bugs</a></td></tr>
<tr><td ALIGN=middle><a class='linkbox' href="http://www.nevrax.org/GPL.php3" TITLE="Rubrique license"><img width=18 height=12 hspace=5 border=0 src=http://www.nevrax.org/inc/img/picto-gpl.gif ALT=#></A></td><td><a class='linkbox' href="http://www.nevrax.org/GPL.php3" TITLE="License">License</a></td></tr>
</TABLE>
</TD></TR></TABLE></TD></TR></TABLE>
<!------ End Box ------>
</TD>
<TD WIDTH=15><IMG SRC="http://www.nevrax.org/inc/img/pixel.gif" WIDTH="16" HEIGHT="16" BORDER=0 ALT=""></TD>
<TD ALIGN=left valign=top><IMG SRC="http://www.nevrax.org/inc/img/pixel.gif" WIDTH="140" HEIGHT="10" BORDER=0 ALT="">
<!-- title -->
<TABLE background="http://www.nevrax.org/inc/img/redline.gif" CELLSPACING=0 CELLPADDING=0 BORDER=0 width=100%><tr><td>
<A HREF="http://www.nevrax.org/docs/"><img src="http://www.nevrax.org/inc/img/t_docs.gif" ALT="Docs" HEIGHT=20 BORDER=0></A>
</td><td><IMG SRC="http://www.nevrax.org/inc/img/pixel.gif" WIDTH="1" HEIGHT="1" BORDER=0 ALT="">
</td></tr></table>
<!-- block -->
<TABLE bgcolor="#dddddd" CELLSPACING=0 CELLPADDING=0 BORDER=0 width=100%><tr><td width=1% valign=middle><img width=6 height=14 hspace=2 vspace=2 src="http://www.nevrax.org/inc/img/reddots.gif"></TD>
<TD><B>Documentation</B></TD>
<TD ALIGN=RIGHT> </td>
</tr></table>
<!-- Generated by Doxygen 1.2.14 -->
<center>
<a class="qindex" href="index.html">Main Page</a> <a class="qindex" href="namespaces.html">Namespace List</a> <a class="qindex" href="hierarchy.html">Class Hierarchy</a> <a class="qindex" href="classes.html">Alphabetical List</a> <a class="qindex" href="annotated.html">Compound List</a> <a class="qindex" href="files.html">File List</a> <a class="qindex" href="namespacemembers.html">Namespace Members</a> <a class="qindex" href="functions.html">Compound Members</a> <a class="qindex" href="globals.html">File Members</a> <a class="qindex" href="pages.html">Related Pages</a> <a class="qindexRef" doxygen="_cgi:http://www.nevrax.org/cgi-bin/nel-search.cgi" href="http://www.nevrax.org/cgi-bin/nel-search.cgi">Search</a> </center>
<hr><h1>inet_address.cpp</h1><a href="inet__address_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001
00007 <font class="comment">/* Copyright, 2000 Nevrax Ltd.</font>
00008 <font class="comment"> *</font>
00009 <font class="comment"> * This file is part of NEVRAX NEL.</font>
00010 <font class="comment"> * NEVRAX NEL is free software; you can redistribute it and/or modify</font>
00011 <font class="comment"> * it under the terms of the GNU General Public License as published by</font>
00012 <font class="comment"> * the Free Software Foundation; either version 2, or (at your option)</font>
00013 <font class="comment"> * any later version.</font>
00014 <font class="comment"></font>
00015 <font class="comment"> * NEVRAX NEL is distributed in the hope that it will be useful, but</font>
00016 <font class="comment"> * WITHOUT ANY WARRANTY; without even the implied warranty of</font>
00017 <font class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU</font>
00018 <font class="comment"> * General Public License for more details.</font>
00019 <font class="comment"></font>
00020 <font class="comment"> * You should have received a copy of the GNU General Public License</font>
00021 <font class="comment"> * along with NEVRAX NEL; see the file COPYING. If not, write to the</font>
00022 <font class="comment"> * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,</font>
00023 <font class="comment"> * MA 02111-1307, USA.</font>
00024 <font class="comment"> */</font>
00025
00026 <font class="preprocessor">#include "<a class="code" href="stdnet_8h.html">stdnet.h</a>"</font>
00027
00028 <font class="preprocessor">#include "<a class="code" href="common_8h.html">nel/misc/common.h</a>"</font>
00029
00030 <font class="preprocessor">#include "<a class="code" href="inet__address_8h.html">nel/net/inet_address.h</a>"</font>
00031 <font class="preprocessor">#include "<a class="code" href="sock_8h.html">nel/net/sock.h</a>"</font>
00032
00033 <font class="preprocessor">#ifdef NL_OS_WINDOWS</font>
00034 <font class="preprocessor"></font>
00035 <font class="preprocessor">#include <winsock2.h></font>
00036
00037 <font class="preprocessor">#elif defined NL_OS_UNIX</font>
00038 <font class="preprocessor"></font>
00039 <font class="preprocessor">#include <<a class="code" href="unistd_8h.html">unistd.h</a>></font>
00040 <font class="preprocessor">#include <sys/socket.h></font>
00041 <font class="preprocessor">#include <arpa/inet.h></font>
00042 <font class="preprocessor">#include <netinet/in.h></font>
00043 <font class="preprocessor">#include <netdb.h></font>
00044
00045 <font class="preprocessor">#define WSAGetLastError() 0</font>
00046 <font class="preprocessor"></font><font class="preprocessor">#define SOCKET_ERROR -1</font>
00047 <font class="preprocessor"></font>
00048 <font class="preprocessor">#endif</font>
00049 <font class="preprocessor"></font>
00050
00051 <font class="keyword">using</font> <font class="keyword">namespace </font>std;
00052 <font class="keyword">using</font> <font class="keyword">namespace </font>NLMISC;
00053
00054
00055 <font class="keyword">namespace </font>NLNET
00056 {
00057
<a name="l00058"></a><a class="code" href="classNLNET_1_1CInetAddress.html#p0">00058</a> <font class="keywordtype">bool</font> CInetAddress::RetrieveNames = <font class="keyword">false</font>;
00059
00060
00061 <font class="comment">/*</font>
00062 <font class="comment"> * Constructor</font>
00063 <font class="comment"> */</font>
<a name="l00064"></a><a class="code" href="classNLNET_1_1CInetAddress.html#a0">00064</a> CInetAddress::CInetAddress()
00065 {
00066 <a class="code" href="classNLNET_1_1CInetAddress.html#c0">init</a>();
00067 <a class="code" href="classNLNET_1_1CInetAddress.html#o1">_SockAddr</a>->sin_port = 0; <font class="comment">// same as htons(0)</font>
00068 memset( &<a class="code" href="classNLNET_1_1CInetAddress.html#o1">_SockAddr</a>->sin_addr, 0, <font class="keyword">sizeof</font>(in_addr) ); <font class="comment">// same as htonl(INADDR_ANY)</font>
00069 }
00070
00071
00072 <font class="comment">/*</font>
00073 <font class="comment"> * Constructor with ip address, port=0</font>
00074 <font class="comment"> */</font>
<a name="l00075"></a><a class="code" href="classNLNET_1_1CInetAddress.html#b0">00075</a> CInetAddress::CInetAddress( <font class="keyword">const</font> in_addr *ip )
00076 {
00077 <a class="code" href="classNLNET_1_1CInetAddress.html#c0">init</a>();
00078 <a class="code" href="classNLNET_1_1CInetAddress.html#o1">_SockAddr</a>->sin_port = 0;
00079 memcpy( &<a class="code" href="classNLNET_1_1CInetAddress.html#o1">_SockAddr</a>->sin_addr, ip, <font class="keyword">sizeof</font>(in_addr) );
00080
00081 <font class="comment">// get the host name to be displayed</font>
00082 hostent *phostent = gethostbyaddr( (<font class="keywordtype">char</font>*)&ip->s_addr, 4, AF_INET );
00083 <font class="keywordflow">if</font> ( phostent == NULL )
00084 {
00085 <a class="code" href="classNLNET_1_1CInetAddress.html#o0">_HostName</a> = <a class="code" href="classNLNET_1_1CInetAddress.html#a14">ipAddress</a>();
00086 }
00087 <font class="keywordflow">else</font>
00088 {
00089 <a class="code" href="classNLNET_1_1CInetAddress.html#o0">_HostName</a> = string( phostent->h_name );
00090 }
00091 <a class="code" href="classNLNET_1_1CInetAddress.html#o2">_Valid</a> = <font class="keyword">true</font>;
00092 }
00093
00094
00095 <font class="comment">/*</font>
00096 <font class="comment"> * Alternate constructor (calls setByName())</font>
00097 <font class="comment"> */</font>
<a name="l00098"></a><a class="code" href="classNLNET_1_1CInetAddress.html#a1">00098</a> CInetAddress::CInetAddress( <font class="keyword">const</font> std::string& hostName, <a class="code" href="types__nl_8h.html#a8">uint16</a> port )
00099 {
00100 <a class="code" href="classNLNET_1_1CInetAddress.html#c0">init</a>();
00101 <a class="code" href="classNLNET_1_1CInetAddress.html#a7">setPort</a>( <a class="code" href="classNLNET_1_1CInetAddress.html#a16">port</a> );
00102 <a class="code" href="classNLNET_1_1CInetAddress.html#a6">setByName</a>( <a class="code" href="classNLNET_1_1CInetAddress.html#a15">hostName</a> );
00103 }
00104
00105
00106 <font class="comment">/*</font>
00107 <font class="comment"> * Alternate constructor (calls setNameAndPort())</font>
00108 <font class="comment"> */</font>
<a name="l00109"></a><a class="code" href="classNLNET_1_1CInetAddress.html#a2">00109</a> CInetAddress::CInetAddress( <font class="keyword">const</font> std::string& hostNameAndPort )
00110 {
00111 <a class="code" href="classNLNET_1_1CInetAddress.html#c0">init</a>();
00112 <a class="code" href="classNLNET_1_1CInetAddress.html#a8">setNameAndPort</a>( hostNameAndPort );
00113 }
00114
00115
00116 <font class="comment">/*</font>
00117 <font class="comment"> * Copy constructor</font>
00118 <font class="comment"> */</font>
<a name="l00119"></a><a class="code" href="classNLNET_1_1CInetAddress.html#a3">00119</a> CInetAddress::CInetAddress( <font class="keyword">const</font> CInetAddress& other )
00120 {
00121 <a class="code" href="classNLNET_1_1CInetAddress.html#c0">init</a>();
00122 <a class="code" href="classNLNET_1_1CInetAddress.html#o0">_HostName</a> = other._HostName;
00123 memcpy( <a class="code" href="classNLNET_1_1CInetAddress.html#o1">_SockAddr</a>, other._SockAddr, <font class="keyword">sizeof</font>( *<a class="code" href="classNLNET_1_1CInetAddress.html#o1">_SockAddr</a> ) );
00124 <a class="code" href="classNLNET_1_1CInetAddress.html#o2">_Valid</a> = other._Valid;
00125 }
00126
00127
00128 <font class="comment">/*</font>
00129 <font class="comment"> * Assignment operator</font>
00130 <font class="comment"> */</font>
<a name="l00131"></a><a class="code" href="classNLNET_1_1CInetAddress.html#a4">00131</a> CInetAddress& CInetAddress::operator=( <font class="keyword">const</font> CInetAddress& other )
00132 {
00133 <a class="code" href="classNLNET_1_1CInetAddress.html#o0">_HostName</a> = other._HostName;
00134 memcpy( <a class="code" href="classNLNET_1_1CInetAddress.html#o1">_SockAddr</a>, other._SockAddr, <font class="keyword">sizeof</font>( *<a class="code" href="classNLNET_1_1CInetAddress.html#o1">_SockAddr</a> ) );
00135 <a class="code" href="classNLNET_1_1CInetAddress.html#o2">_Valid</a> = other._Valid;
00136 <font class="keywordflow">return</font> *<font class="keyword">this</font>;
00137 }
00138
00139
00140 <font class="comment">/*</font>
00141 <font class="comment"> * Comparison == operator</font>
00142 <font class="comment"> */</font>
<a name="l00143"></a><a class="code" href="classNLNET_1_1CInetAddress.html#a98">00143</a> <font class="keywordtype">bool</font> <a class="code" href="namespaceNLNET.html#a98">operator==</a>( <font class="keyword">const</font> CInetAddress& a1, <font class="keyword">const</font> CInetAddress& a2 )
00144 {
00145 <font class="comment">// Compares the sockaddr structure except the last 8 bytes equal to zero.</font>
00146 <font class="keywordflow">return</font> ( memcmp( a1._SockAddr, a2._SockAddr, <font class="keyword">sizeof</font>(sockaddr_in)-8 ) == 0 );
00147 }
00148
00149
00150 <font class="comment">/*</font>
00151 <font class="comment"> * Comparison < operator</font>
00152 <font class="comment"> */</font>
<a name="l00153"></a><a class="code" href="classNLNET_1_1CInetAddress.html#a99">00153</a> <font class="keywordtype">bool</font> <a class="code" href="namespaceNLNET.html#a99">operator<</a>( <font class="keyword">const</font> CInetAddress& a1, <font class="keyword">const</font> CInetAddress& a2 )
00154 {
00155 <font class="preprocessor">#ifdef NL_OS_WINDOWS</font>
00156 <font class="preprocessor"></font> <font class="keywordflow">if</font> ( a1._SockAddr->sin_addr.S_un.S_addr == a2._SockAddr->sin_addr.S_un.S_addr )
00157 {
00158 <font class="keywordflow">return</font> ( a1.port() < a2.port() );
00159 }
00160 <font class="keywordflow">else</font>
00161 {
00162 <font class="keywordflow">return</font> ( a1._SockAddr->sin_addr.S_un.S_addr < a2._SockAddr->sin_addr.S_un.S_addr );
00163 }
00164 <font class="preprocessor">#elif defined NL_OS_UNIX</font>
00165 <font class="preprocessor"></font> <font class="keywordflow">if</font> ( a1._SockAddr->sin_addr.s_addr == a2._SockAddr->sin_addr.s_addr )
00166 {
00167 <font class="keywordflow">return</font> ( a1.port() < a2.port() );
00168 }
00169 <font class="keywordflow">else</font>
00170 {
00171 <font class="keywordflow">return</font> ( a1._SockAddr->sin_addr.s_addr < a2._SockAddr->sin_addr.s_addr );
00172 }
00173 <font class="preprocessor">#endif </font>
00174 <font class="preprocessor"></font>}
00175
00176
00177 <font class="comment">/*</font>
00178 <font class="comment"> * Constructor contents</font>
00179 <font class="comment"> */</font>
<a name="l00180"></a><a class="code" href="classNLNET_1_1CInetAddress.html#c0">00180</a> <font class="keywordtype">void</font> CInetAddress::init()
00181 {
00182 CSock::initNetwork();
00183
00184 <a class="code" href="classNLNET_1_1CInetAddress.html#o2">_Valid</a> = <font class="keyword">false</font>;
00185
00186 <a class="code" href="classNLNET_1_1CInetAddress.html#o1">_SockAddr</a> = <font class="keyword">new</font> sockaddr_in;
00187 <a class="code" href="classNLNET_1_1CInetAddress.html#o1">_SockAddr</a>->sin_family = AF_INET;
00188 memset( <a class="code" href="classNLNET_1_1CInetAddress.html#o1">_SockAddr</a>->sin_zero, 0, 8 );
00189 }
00190
00191
00192 <font class="comment">/*</font>
00193 <font class="comment"> * Destructor</font>
00194 <font class="comment"> */</font>
<a name="l00195"></a><a class="code" href="classNLNET_1_1CInetAddress.html#a5">00195</a> CInetAddress::~CInetAddress()
00196 {
00197 <font class="keyword">delete</font> <a class="code" href="classNLNET_1_1CInetAddress.html#o1">_SockAddr</a>;
00198 <font class="comment">// _Valid = false;</font>
00199 }
00200
00201 <font class="comment">/*</font>
00202 <font class="comment"> * Sets hostname and port (ex: www.nevrax.com:80)</font>
00203 <font class="comment"> */</font>
<a name="l00204"></a><a class="code" href="classNLNET_1_1CInetAddress.html#a8">00204</a> <font class="keywordtype">void</font> CInetAddress::setNameAndPort( <font class="keyword">const</font> std::string& hostNameAndPort )
00205 {
00206 <a class="code" href="types__nl_8h.html#a10">uint32</a> pos = hostNameAndPort.find_first_of (<font class="charliteral">':'</font>);
00207 <font class="keywordflow">if</font> (pos != string::npos)
00208 {
00209 <a class="code" href="classNLNET_1_1CInetAddress.html#a7">setPort</a>( atoi(hostNameAndPort.substr(pos + 1).c_str()) );
00210 }
00211 <font class="keywordflow">else</font>
00212 {
00213 <a class="code" href="classNLNET_1_1CInetAddress.html#a7">setPort</a>( 0 );
00214 }
00215
00216 <font class="comment">// if pos == -1, it will copy all the string</font>
00217 <a class="code" href="classNLNET_1_1CInetAddress.html#a6">setByName</a>( hostNameAndPort.substr (0, pos) );
00218 }
00219
00220
00221 <font class="comment">/*</font>
00222 <font class="comment"> * Resolves a name</font>
00223 <font class="comment"> */</font>
<a name="l00224"></a><a class="code" href="classNLNET_1_1CInetAddress.html#a6">00224</a> CInetAddress& CInetAddress::setByName( <font class="keyword">const</font> std::string& hostName )
00225 {
00226 <font class="comment">// Try to convert directly for addresses such as a.b.c.d</font>
00227 in_addr iaddr;
00228 <font class="preprocessor">#ifdef NL_OS_WINDOWS</font>
00229 <font class="preprocessor"></font> iaddr.S_un.S_addr = inet_addr( <a class="code" href="classNLNET_1_1CInetAddress.html#a15">hostName</a>.c_str() );
00230 <font class="keywordflow">if</font> ( iaddr.S_un.S_addr == INADDR_NONE )
00231 <font class="preprocessor">#elif defined NL_OS_UNIX</font>
00232 <font class="preprocessor"></font> iaddr.s_addr = inet_addr( <a class="code" href="classNLNET_1_1CInetAddress.html#a15">hostName</a>.c_str() );
00233 <font class="keywordflow">if</font> ( iaddr.s_addr == INADDR_NONE )
00234 <font class="preprocessor">#endif</font>
00235 <font class="preprocessor"></font> {
00236
00237 <font class="comment">// Otherwise use the traditional DNS look-up</font>
00238 hostent *phostent = gethostbyname( <a class="code" href="classNLNET_1_1CInetAddress.html#a15">hostName</a>.c_str() );
00239 <font class="keywordflow">if</font> ( phostent == NULL )
00240 {
00241 <a class="code" href="classNLNET_1_1CInetAddress.html#o2">_Valid</a> = <font class="keyword">false</font>;
00242 <a class="code" href="debug_8h.html#a0">nldebug</a>( <font class="stringliteral">"LNETL0: Network error: resolution of hostname '%s' failed"</font>, <a class="code" href="classNLNET_1_1CInetAddress.html#a15">hostName</a>.c_str() );
00243 <font class="comment">// return *this;</font>
00244 <font class="keywordflow">throw</font> ESocket( (string(<font class="stringliteral">"Hostname resolution failed for "</font>)+<a class="code" href="classNLNET_1_1CInetAddress.html#a15">hostName</a>).c_str() );
00245 }
00246 <a class="code" href="classNLNET_1_1CInetAddress.html#o0">_HostName</a> = string( phostent->h_name );
00247 memcpy( &<a class="code" href="classNLNET_1_1CInetAddress.html#o1">_SockAddr</a>->sin_addr, phostent->h_addr, <font class="keyword">sizeof</font>(in_addr) );
00248 }
00249 <font class="keywordflow">else</font>
00250 {
00251 <a class="code" href="classNLNET_1_1CInetAddress.html#o0">_HostName</a> = <a class="code" href="classNLNET_1_1CInetAddress.html#a15">hostName</a>;
00252 memcpy( &<a class="code" href="classNLNET_1_1CInetAddress.html#o1">_SockAddr</a>->sin_addr, &iaddr, <font class="keyword">sizeof</font>(iaddr) );
00253 }
00254 <a class="code" href="classNLNET_1_1CInetAddress.html#o2">_Valid</a> = <font class="keyword">true</font>;
00255 <font class="keywordflow">return</font> *<font class="keyword">this</font>;
00256 }
00257
00258
00259 <font class="comment">/*</font>
00260 <font class="comment"> * Sets port</font>
00261 <font class="comment"> */</font>
<a name="l00262"></a><a class="code" href="classNLNET_1_1CInetAddress.html#a7">00262</a> <font class="keywordtype">void</font> CInetAddress::setPort( <a class="code" href="types__nl_8h.html#a8">uint16</a> port )
00263 {
00264 <a class="code" href="classNLNET_1_1CInetAddress.html#o1">_SockAddr</a>->sin_port = htons( <a class="code" href="classNLNET_1_1CInetAddress.html#a16">port</a> );
00265
00266 }
00267
00268
00269 <font class="comment">/* Sets internal socket address directly (contents is copied).</font>
00270 <font class="comment"> * It also retrieves the host name if CInetAddress::RetrieveNames is true.</font>
00271 <font class="comment"> */</font>
<a name="l00272"></a><a class="code" href="classNLNET_1_1CInetAddress.html#a9">00272</a> <font class="keywordtype">void</font> CInetAddress::setSockAddr( <font class="keyword">const</font> sockaddr_in* saddr )
00273 {
00274 memcpy( <a class="code" href="classNLNET_1_1CInetAddress.html#o1">_SockAddr</a>, saddr, <font class="keyword">sizeof</font>(*saddr) );
00275
00276 <font class="comment">// Get host name</font>
00277 <font class="comment">// Warning: when it can't find it, it take more than 4 seconds</font>
00278 <font class="keywordflow">if</font> ( CInetAddress::RetrieveNames )
00279 {
00280 hostent *phostent = gethostbyaddr( (<font class="keywordtype">char</font>*)&saddr->sin_addr.s_addr, 4, AF_INET );
00281 <font class="keywordflow">if</font> ( phostent == NULL )
00282 {
00283 <a class="code" href="classNLNET_1_1CInetAddress.html#o0">_HostName</a> = <a class="code" href="classNLNET_1_1CInetAddress.html#a14">ipAddress</a>();
00284 }
00285 <font class="keywordflow">else</font>
00286 {
00287 <a class="code" href="classNLNET_1_1CInetAddress.html#o0">_HostName</a> = string( phostent->h_name );
00288 }
00289 }
00290 <a class="code" href="classNLNET_1_1CInetAddress.html#o2">_Valid</a> = <font class="keyword">true</font>;
00291 }
00292
00293
00294 <font class="comment">/*</font>
00295 <font class="comment"> * Returns if object (address and port) is valid</font>
00296 <font class="comment"> */</font>
<a name="l00297"></a><a class="code" href="classNLNET_1_1CInetAddress.html#a10">00297</a> <font class="keywordtype">bool</font> CInetAddress::isValid()<font class="keyword"> const</font>
00298 <font class="keyword"></font>{
00299 <font class="keywordflow">return</font> ( <a class="code" href="classNLNET_1_1CInetAddress.html#o2">_Valid</a> && <a class="code" href="classNLNET_1_1CInetAddress.html#o1">_SockAddr</a>->sin_port!=0 ); <font class="comment">// same as ntohs(0)</font>
00300 }
00301
00302
00303 <font class="comment">/*</font>
00304 <font class="comment"> * Returns internal socket address (read only)</font>
00305 <font class="comment"> */</font>
<a name="l00306"></a><a class="code" href="classNLNET_1_1CInetAddress.html#a11">00306</a> <font class="keyword">const</font> sockaddr_in *CInetAddress::sockAddr()<font class="keyword"> const</font>
00307 <font class="keyword"></font>{
00308 <font class="keywordflow">return</font> <a class="code" href="classNLNET_1_1CInetAddress.html#o1">_SockAddr</a>;
00309 }
00310
00311
00312 <font class="comment">/*</font>
00313 <font class="comment"> * Returns internal IP address</font>
00314 <font class="comment"> */</font>
<a name="l00315"></a><a class="code" href="classNLNET_1_1CInetAddress.html#a12">00315</a> <a class="code" href="types__nl_8h.html#a10">uint32</a> CInetAddress::internalIPAddress()<font class="keyword"> const</font>
00316 <font class="keyword"></font>{
00317 <font class="keywordflow">return</font> <a class="code" href="classNLNET_1_1CInetAddress.html#o1">_SockAddr</a>->sin_addr.s_addr;
00318 }
00319
<a name="l00320"></a><a class="code" href="classNLNET_1_1CInetAddress.html#a13">00320</a> <a class="code" href="types__nl_8h.html#a10">uint32</a> CInetAddress::internalNetAddress()<font class="keyword"> const</font>
00321 <font class="keyword"></font>{
00322 <a class="code" href="types__nl_8h.html#a10">uint32</a> ip = <a class="code" href="classNLNET_1_1CInetAddress.html#a12">internalIPAddress</a>();
00323 <font class="keywordflow">if</font> ((ip&0x00000080) == 0)
00324 {
00325 <font class="comment">// A class</font>
00326 <font class="keywordflow">return</font> ip & 0x000000FF;
00327 }
00328 <font class="keywordflow">else</font> <font class="keywordflow">if</font> ((ip&0x00000040) == 0)
00329 {
00330 <font class="comment">// B class</font>
00331 <font class="keywordflow">return</font> ip & 0x0000FFFF;
00332 }
00333 <font class="keywordflow">else</font> <font class="keywordflow">if</font> ((ip&0x00000020) == 0)
00334 {
00335 <font class="comment">// C class</font>
00336 <font class="keywordflow">return</font> ip & 0x00FFFFFF;
00337 }
00338 <font class="keywordflow">else</font> <font class="keywordflow">if</font> ((ip&0x00000010) == 0)
00339 {
00340 <font class="comment">// D class</font>
00341 <font class="keywordflow">return</font> ip & 0xFFFFFFFF;
00342 }
00343 <font class="keywordflow">else</font>
00344 {
00345 <font class="keywordflow">return</font> ip;
00346 }
00347 }
00348
00349
00350
00351
00352 <font class="comment">/*</font>
00353 <font class="comment"> * Returns readable IP address. (ex: "195.68.21.195")</font>
00354 <font class="comment"> */</font>
<a name="l00355"></a><a class="code" href="classNLNET_1_1CInetAddress.html#a14">00355</a> string CInetAddress::ipAddress()<font class="keyword"> const</font>
00356 <font class="keyword"></font>{
00357 <font class="comment">/*stringstream ss; // or use inet_ntoa</font>
00358 <font class="comment"> ss << inet_ntoa ( _SockAddr->sin_addr );</font>
00359 <font class="comment"> return ss.str();*/</font>
00360 <font class="keywordflow">return</font> string( inet_ntoa( <a class="code" href="classNLNET_1_1CInetAddress.html#o1">_SockAddr</a>->sin_addr ) );
00361 }
00362
00363
00364 <font class="comment">/*</font>
00365 <font class="comment"> * Returns host name. (ex: "www.nevrax.org")</font>
00366 <font class="comment"> */</font>
<a name="l00367"></a><a class="code" href="classNLNET_1_1CInetAddress.html#a15">00367</a> <font class="keyword">const</font> string& CInetAddress::hostName()<font class="keyword"> const</font>
00368 <font class="keyword"></font>{
00369 <font class="keywordflow">return</font> <a class="code" href="classNLNET_1_1CInetAddress.html#o0">_HostName</a>;
00370 }
00371
00372
00373 <font class="comment">/*</font>
00374 <font class="comment"> * Returns port</font>
00375 <font class="comment"> */</font>
<a name="l00376"></a><a class="code" href="classNLNET_1_1CInetAddress.html#a16">00376</a> <a class="code" href="types__nl_8h.html#a8">uint16</a> CInetAddress::port()<font class="keyword"> const</font>
00377 <font class="keyword"></font>{
00378 <font class="keywordflow">return</font> ntohs( <a class="code" href="classNLNET_1_1CInetAddress.html#o1">_SockAddr</a>->sin_port );
00379 }
00380
00381
00382 <font class="comment">/*</font>
00383 <font class="comment"> * Returns hostname and port as a string. (ex: "www.nevrax.org:80 (195.68.21.195)")</font>
00384 <font class="comment"> */</font>
<a name="l00385"></a><a class="code" href="classNLNET_1_1CInetAddress.html#a17">00385</a> std::string CInetAddress::asString()<font class="keyword"> const</font>
00386 <font class="keyword"></font>{
00387 stringstream ss;
00388 ss << <a class="code" href="classNLNET_1_1CInetAddress.html#a15">hostName</a>() << <font class="stringliteral">":"</font> << <a class="code" href="classNLNET_1_1CInetAddress.html#a16">port</a>() << <font class="stringliteral">" ("</font> << <a class="code" href="classNLNET_1_1CInetAddress.html#a14">ipAddress</a>() << <font class="stringliteral">")"</font>;
00389 <font class="keywordflow">return</font> ss.str();
00390 }
00391
00392
00393 <font class="comment">/*</font>
00394 <font class="comment"> * Returns IP address and port as a string. (ex: "195.68.21.195:80")</font>
00395 <font class="comment"> */</font>
<a name="l00396"></a><a class="code" href="classNLNET_1_1CInetAddress.html#a18">00396</a> std::string CInetAddress::asIPString()<font class="keyword"> const</font>
00397 <font class="keyword"></font>{
00398 stringstream ss;
00399 ss << <a class="code" href="classNLNET_1_1CInetAddress.html#a14">ipAddress</a>() << <font class="stringliteral">":"</font> << <a class="code" href="classNLNET_1_1CInetAddress.html#a16">port</a>();
00400 <font class="keywordflow">return</font> ss.str();
00401 }
00402
00403
00404 <font class="comment">/*</font>
00405 <font class="comment"> * Serialize</font>
00406 <font class="comment"> */</font>
<a name="l00407"></a><a class="code" href="classNLNET_1_1CInetAddress.html#a19">00407</a> <font class="keywordtype">void</font> CInetAddress::serial( <a class="code" href="classNLMISC_1_1IStream.html">NLMISC::IStream</a>& <a class="code" href="driver__opengl__extension__def_8h.html#a260">s</a> )
00408 {
00409 <a class="code" href="classNLMISC_1_1CMemStream.html">NLMISC::CMemStream</a> *ms = dynamic_cast<NLMISC::CMemStream*>(&s);
00410 <font class="keywordflow">if</font> ( ms && ms-><a class="code" href="classNLMISC_1_1CMemStream.html#a4">stringMode</a>() )
00411 {
00412 <font class="comment">// String stream</font>
00413 string addrs;
00414 <font class="keywordflow">if</font> ( ms-><a class="code" href="classNLMISC_1_1IStream.html#a4">isReading</a>() )
00415 {
00416 ms-><a class="code" href="classNLMISC_1_1CMemStream.html#a21">serial</a>( addrs );
00417 <a class="code" href="classNLNET_1_1CInetAddress.html#a8">setNameAndPort</a>( addrs );
00418 }
00419 <font class="keywordflow">else</font>
00420 {
00421 addrs = <a class="code" href="classNLNET_1_1CInetAddress.html#a18">asIPString</a>();
00422 ms-><a class="code" href="classNLMISC_1_1CMemStream.html#a21">serial</a>( addrs );
00423 }
00424 s.<a class="code" href="classNLMISC_1_1IStream.html#a5">serial</a>( <a class="code" href="classNLNET_1_1CInetAddress.html#o2">_Valid</a> );
00425 }
00426 <font class="keywordflow">else</font>
00427 {
00428 <font class="comment">// Binary stream</font>
00429 s.<a class="code" href="classNLMISC_1_1IStream.html#z320_0">serialBuffer</a>( (<a class="code" href="types__nl_8h.html#a6">uint8</a>*)<a class="code" href="classNLNET_1_1CInetAddress.html#o1">_SockAddr</a>, <font class="keyword">sizeof</font>(*<a class="code" href="classNLNET_1_1CInetAddress.html#o1">_SockAddr</a>) ); <font class="comment">// this is possible only because the contents of _SockAddr is platform-independant !</font>
00430 s.<a class="code" href="classNLMISC_1_1IStream.html#a5">serial</a>( <a class="code" href="classNLNET_1_1CInetAddress.html#o2">_Valid</a> );
00431
00432 <font class="keywordflow">if</font>(_Valid)
00433 {
00434 <font class="comment">// retreive the fullname</font>
00435 <a class="code" href="classNLNET_1_1CInetAddress.html#a9">setSockAddr</a> (<a class="code" href="classNLNET_1_1CInetAddress.html#o1">_SockAddr</a>);
00436 }
00437
00438 }
00439 }
00440
00441
00442 <font class="comment">/*</font>
00443 <font class="comment"> * Creates a CInetAddress object with local host address, port=0</font>
00444 <font class="comment"> */</font>
<a name="l00445"></a><a class="code" href="classNLNET_1_1CInetAddress.html#d0">00445</a> CInetAddress CInetAddress::localHost()
00446 {
00447 <font class="keyword">const</font> <a class="code" href="types__nl_8h.html#a14">uint</a> maxlength = 80;
00448 <font class="keywordtype">char</font> localhost [maxlength];
00449 <font class="keywordflow">if</font> ( gethostname( localhost, maxlength ) != 0 )
00450 <font class="keywordflow">throw</font> ESocket( <font class="stringliteral">"Unable to get local hostname"</font> );
00451 <a class="code" href="classNLNET_1_1CInetAddress.html#a0">CInetAddress</a> localaddr = <a class="code" href="classNLNET_1_1CInetAddress.html#a0">CInetAddress</a>( string(localhost) );
00452
00453 <font class="keywordflow">if</font> ( localaddr.ipAddress() == <font class="stringliteral">"127.0.0.1"</font> )
00454 {
00455 <a class="code" href="debug_8h.html#a2">nlwarning</a> (<font class="stringliteral">"*** No network card detected! using localhost (127.0.0.1)"</font>);
00456 }
00457
00458 <font class="keywordflow">return</font> localaddr;
00459 }
00460
00461
00462 <font class="comment">/* Returns the list of the local host addresses (with port=0)</font>
00463 <font class="comment"> * (especially useful if the host is multihomed)</font>
00464 <font class="comment"> */</font>
<a name="l00465"></a><a class="code" href="classNLNET_1_1CInetAddress.html#d1">00465</a> std::vector<CInetAddress> CInetAddress::localAddresses()
00466 {
00467 <font class="comment">// 1. Get local host name</font>
00468 <font class="keyword">const</font> <a class="code" href="types__nl_8h.html#a14">uint</a> maxlength = 80;
00469 <font class="keywordtype">char</font> localhost [maxlength];
00470 <font class="keywordflow">if</font> ( gethostname( localhost, maxlength ) == SOCKET_ERROR )
00471 {
00472 <font class="keywordflow">throw</font> ESocket( <font class="stringliteral">"Unable to get local hostname"</font> );
00473 }
00474
00475 <font class="comment">// 2. Get address list</font>
00476 hostent *phostent = gethostbyname( localhost );
00477 <font class="keywordflow">if</font> ( phostent == NULL )
00478 {
00479 <font class="keywordflow">throw</font> ESocket( (string(<font class="stringliteral">"Hostname resolution failed for "</font>)+string(localhost)).c_str() );
00480 }
00481 <a class="code" href="types__nl_8h.html#a14">uint</a> i;
00482 vector<CInetAddress> vect;
00483 <font class="keywordflow">for</font> ( i=0; phostent->h_addr_list[i]!=0; ++i )
00484 {
00485 vect.push_back( <a class="code" href="classNLNET_1_1CInetAddress.html#a0">CInetAddress</a>( (<font class="keyword">const</font> in_addr*)(phostent->h_addr_list[i]) ) );
00486 }
00487
00488 <font class="keywordflow">if</font>(vect.empty())
00489 {
00490 <font class="keywordflow">throw</font> ESocket( (string(<font class="stringliteral">"No network card detected for "</font>)+string(localhost)).c_str() );
00491 }
00492
00493 <font class="keywordflow">return</font> vect;
00494 }
00495
<a name="l00496"></a><a class="code" href="classNLNET_1_1CInetAddress.html#a20">00496</a> <font class="keywordtype">bool</font> CInetAddress::is127001 ()<font class="keyword"> const </font>
00497 <font class="keyword"></font>{
00498 <font class="keywordflow">return</font> (<a class="code" href="classNLNET_1_1CInetAddress.html#a12">internalIPAddress</a> () == htonl(0x7F000001));
00499 }
00500
00501
00502 std::string <a class="code" href="namespaceNLNET.html#a85">vectorCInetAddressToString</a>(<font class="keyword">const</font> std::vector<CInetAddress> &addrs)
00503 {
00504 string str;
00505
00506 <font class="keywordflow">for</font> (<a class="code" href="types__nl_8h.html#a14">uint</a> i = 0; i < addrs.size(); i++)
00507 {
00508 <font class="keywordflow">if</font> (i != 0)
00509 str += <font class="stringliteral">" "</font>;
00510 str += addrs[i].asString().c_str ();
00511 }
00512 <font class="keywordflow">return</font> str;
00513 }
00514
00515 <a class="code" href="types__nl_8h.html#a10">uint32</a> <a class="code" href="namespaceNLNET.html#a83">stringToInternalIPAddress</a> (<font class="keyword">const</font> std::string &addr)
00516 {
00517 <font class="keywordflow">return</font> inet_addr( addr.c_str() );
00518 }
00519
00520 std::string <a class="code" href="namespaceNLNET.html#a84">internalIPAddressToString</a> (<a class="code" href="types__nl_8h.html#a10">uint32</a> addr)
00521 {
00522 string res;
00523 res = toString((addr)&0xFF);
00524 res += <font class="stringliteral">"."</font>;
00525 res += toString((addr>>8)&0xFF);
00526 res += <font class="stringliteral">"."</font>;
00527 res += toString((addr>>16)&0xFF);
00528 res += <font class="stringliteral">"."</font>;
00529 res += toString((addr>>24)&0xFF);
00530 <font class="keywordflow">return</font> res;
00531 }
00532
00533
00534 } <font class="comment">// NLNET</font>
</pre></div>
<!-- footer -->
<BR><FONT Size=+5> </FONT>
</TD>
<TD WIDTH=15><IMG SRC=http://www.nevrax.org/inc/img/pixel.gif WIDTH=15 HEIGHT=15 BORDER=0 ALT=""></TD>
</TR>
</TABLE>
</BODY>
</HTML>
|