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