Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TInetAddress.cxx
Go to the documentation of this file.
1// @(#)root/base:$Id$
2// Author: Fons Rademakers 16/12/96
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12/** \class TInetAddress
13\ingroup Base
14
15This class represents an Internet Protocol (IP) address.
16*/
17
18#include "TInetAddress.h"
19#include "TBuffer.h"
20
22
23////////////////////////////////////////////////////////////////////////////////
24/// Default ctor. Used in case of unknown host. Not a valid address.
25
27{
28 fHostname = "UnknownHost";
29 AddAddress(0);
30 fFamily = -1;
31 fPort = -1;
32}
33
34////////////////////////////////////////////////////////////////////////////////
35/// Create TInetAddress. Private ctor. TInetAddress objects can only
36/// be created via the friend classes TSystem, TServerSocket and TSocket.
37/// Use the IsValid() method to check the validity of a TInetAddress.
38
39TInetAddress::TInetAddress(const char *host, UInt_t addr, Int_t family, Int_t port)
40{
41 AddAddress(addr);
42 if (!strcmp(host, "????") || !strcmp(host, "UnNamedHost"))
44 else
45 fHostname = host;
46 fFamily = family;
47 fPort = port;
48}
49
50////////////////////////////////////////////////////////////////////////////////
51/// TInetAddress copy ctor.
52
54{
55 fHostname = adr.fHostname;
56 fFamily = adr.fFamily;
57 fPort = adr.fPort;
59 fAliases = adr.fAliases;
60}
61
62////////////////////////////////////////////////////////////////////////////////
63/// TInetAddress assignment operator.
64
66{
67 if (this != &rhs) {
69 fHostname = rhs.fHostname;
70 fFamily = rhs.fFamily;
71 fPort = rhs.fPort;
73 fAliases = rhs.fAliases;
74 }
75 return *this;
76}
77
78////////////////////////////////////////////////////////////////////////////////
79/// Returns the raw IP address in host byte order. The highest
80/// order byte position is in addr[0]. To be prepared for 64-bit
81/// IP addresses an array of bytes is returned.
82/// User must delete allocated memory.
83
85{
86 UChar_t *addr = new UChar_t[4];
87
88 addr[0] = (UChar_t) ((fAddresses[0] >> 24) & 0xFF);
89 addr[1] = (UChar_t) ((fAddresses[0] >> 16) & 0xFF);
90 addr[2] = (UChar_t) ((fAddresses[0] >> 8) & 0xFF);
91 addr[3] = (UChar_t) (fAddresses[0] & 0xFF);
92
93 return addr;
94}
95
96////////////////////////////////////////////////////////////////////////////////
97/// Returns the IP address string "%d.%d.%d.%d", use it to convert
98/// alternative addresses obtained via GetAddresses().
99/// Copy string immediately, it will be reused. Static function.
100
102{
103 return Form("%d.%d.%d.%d", (addr >> 24) & 0xFF,
104 (addr >> 16) & 0xFF,
105 (addr >> 8) & 0xFF,
106 addr & 0xFF);
107}
108
109////////////////////////////////////////////////////////////////////////////////
110/// Returns the IP address string "%d.%d.%d.%d".
111/// Copy string immediately, it will be reused.
112
114{
115 return GetHostAddress(fAddresses[0]);
116}
117
118////////////////////////////////////////////////////////////////////////////////
119/// Print internet address as string.
120
122{
123 if (fPort == -1)
124 Printf("%s/%s (not connected)", GetHostName(), GetHostAddress());
125 else
126 Printf("%s/%s (port %d)", GetHostName(), GetHostAddress(), fPort);
127
128 int i = 0;
129 AddressList_t::const_iterator ai;
130 for (ai = fAddresses.begin(); ai != fAddresses.end(); ++ai) {
131 if (!i) printf("%s:", fAddresses.size() == 1 ? "Address" : "Addresses");
132 printf(" %s", GetHostAddress(*ai));
133 i++;
134 }
135 if (i) printf("\n");
136
137 i = 0;
138 AliasList_t::const_iterator ali;
139 for (ali = fAliases.begin(); ali != fAliases.end(); ++ali) {
140 if (!i) printf("%s:", fAliases.size() == 1 ? "Alias" : "Aliases");
141 printf(" %s", ali->Data());
142 i++;
143 }
144 if (i) printf("\n");
145}
146
147////////////////////////////////////////////////////////////////////////////////
148/// Add alternative address to list of addresses.
149
151{
152 fAddresses.push_back(addr);
153}
154
155////////////////////////////////////////////////////////////////////////////////
156/// Add alias to list of aliases.
157
158void TInetAddress::AddAlias(const char *alias)
159{
160 fAliases.push_back(TString(alias));
161}
162
163////////////////////////////////////////////////////////////////////////////////
164/// Stream an object of class TInetAddress.
165
166void TInetAddress::Streamer(TBuffer &R__b)
167{
168 if (R__b.IsReading()) {
169 UInt_t R__s, R__c;
170 Version_t R__v = R__b.ReadVersion(&R__s, &R__c); if (R__v) { }
171 if (R__v > 2) {
172 R__b.ReadClassBuffer(TInetAddress::Class(), this, R__v, R__s, R__c);
173 return;
174 }
175 // process old versions before automatic schema evolution
176 UInt_t address;
177 TObject::Streamer(R__b);
178 fHostname.Streamer(R__b);
179 R__b >> address;
180 R__b >> fFamily;
181 R__b >> fPort;
182 if (R__v == 1)
183 fAddresses.push_back(address);
184 if (R__v > 1) {
186 R__stl1.clear();
187 int R__i, R__n;
188 R__b >> R__n;
189 R__stl1.reserve(R__n);
190 for (R__i = 0; R__i < R__n; R__i++) {
191 unsigned int R__t1;
192 R__b >> R__t1;
193 R__stl1.push_back(R__t1);
194 }
196 R__stl2.clear();
197 R__b >> R__n;
198 R__stl2.reserve(R__n);
199 for (R__i = 0; R__i < R__n; R__i++) {
200 TString R__t2;
201 R__t2.Streamer(R__b);
202 R__stl2.push_back(R__t2);
203 }
204 }
205 R__b.CheckByteCount(R__s, R__c, TInetAddress::IsA());
206 } else {
207 R__b.WriteClassBuffer(TInetAddress::Class(), this);
208 }
209}
short Version_t
Definition RtypesCore.h:65
unsigned char UChar_t
Definition RtypesCore.h:38
unsigned int UInt_t
Definition RtypesCore.h:46
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:364
char * Form(const char *fmt,...)
void Printf(const char *fmt,...)
Buffer base class used for serializing objects.
Definition TBuffer.h:43
virtual Int_t ReadClassBuffer(const TClass *cl, void *pointer, const TClass *onfile_class=0)=0
virtual Version_t ReadVersion(UInt_t *start=0, UInt_t *bcnt=0, const TClass *cl=0)=0
virtual Int_t CheckByteCount(UInt_t startpos, UInt_t bcnt, const TClass *clss)=0
Bool_t IsReading() const
Definition TBuffer.h:86
virtual Int_t WriteClassBuffer(const TClass *cl, void *pointer)=0
This class represents an Internet Protocol (IP) address.
TInetAddress & operator=(const TInetAddress &rhs)
TInetAddress assignment operator.
AddressList_t fAddresses
std::vector< UInt_t > AddressList_t
const char * GetHostName() const
void Print(Option_t *option="") const
Print internet address as string.
AliasList_t fAliases
void AddAlias(const char *alias)
Add alias to list of aliases.
std::vector< TString > AliasList_t
UChar_t * GetAddressBytes() const
Returns the raw IP address in host byte order.
void AddAddress(UInt_t addr)
Add alternative address to list of addresses.
const char * GetHostAddress() const
Returns the IP address string "%d.%d.%d.%d".
TInetAddress()
Default ctor. Used in case of unknown host. Not a valid address.
TString fHostname
Mother of all ROOT objects.
Definition TObject.h:41
TObject & operator=(const TObject &rhs)
TObject assignment operator.
Definition TObject.h:298
Basic string class.
Definition TString.h:136