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