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