Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files 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 \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 
22 ClassImp(TInetAddress);
23 
24 ////////////////////////////////////////////////////////////////////////////////
25 /// Default ctor. Used in case of unknown host. Not a valid address.
26 
27 TInetAddress::TInetAddress()
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"))
44  fHostname = GetHostAddress();
45  else
46  fHostname = host;
47  fFamily = family;
48  fPort = port;
49 }
50 
51 ////////////////////////////////////////////////////////////////////////////////
52 /// TInetAddress copy ctor.
53 
54 TInetAddress::TInetAddress(const TInetAddress &adr) : TObject(adr)
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 
66 TInetAddress& TInetAddress::operator=(const TInetAddress &rhs)
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 
85 UChar_t *TInetAddress::GetAddressBytes() const
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 
102 const char *TInetAddress::GetHostAddress(UInt_t addr)
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 
122 void TInetAddress::Print(Option_t *) const
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 
151 void TInetAddress::AddAddress(UInt_t addr)
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) {
186  TInetAddress::AddressList_t &R__stl1 = fAddresses;
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  }
196  TInetAddress::AliasList_t &R__stl2 = fAliases;
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 {
208  R__b.WriteClassBuffer(TInetAddress::Class(), this);
209  }
210 }