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
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
38TInetAddress::TInetAddress(const char *host, UInt_t addr, Int_t family, Int_t port)
39{
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) {
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
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
158{
159 fAliases.push_back(TString(alias));
160}
161
162////////////////////////////////////////////////////////////////////////////////
163/// Stream an object of class TInetAddress.
164
166{
167 if (R__b.IsReading()) {
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;
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++) {
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 {
206 R__b.WriteClassBuffer(TInetAddress::Class(), this);
207 }
208}
short Version_t
Class version identifier (short)
Definition RtypesCore.h:79
unsigned char UChar_t
Unsigned Character 1 byte (unsigned char)
Definition RtypesCore.h:52
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2495
void Printf(const char *fmt,...)
Formats a string in a circular formatting buffer and prints the string.
Definition TString.cxx:2509
Buffer base class used for serializing objects.
Definition TBuffer.h:43
This class represents an Internet Protocol (IP) address.
TInetAddress & operator=(const TInetAddress &rhs)
TInetAddress assignment operator.
TClass * IsA() const override
AddressList_t fAddresses
void Streamer(TBuffer &) override
Stream an object of class TInetAddress.
std::vector< UInt_t > AddressList_t
static TClass * Class()
const char * GetHostName() const
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.
void Print(Option_t *option="") const override
Print internet address as string.
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) noexcept
TObject assignment operator.
Definition TObject.h:299
virtual void Streamer(TBuffer &)
Stream an object of class TObject.
Definition TObject.cxx:972
Basic string class.
Definition TString.h:138
virtual void Streamer(TBuffer &)
Stream a string object.
Definition TString.cxx:1418