1 
2 /* Web Polygraph       http://www.web-polygraph.org/
3  * Copyright 2003-2011 The Measurement Factory
4  * Licensed under the Apache License, Version 2.0 */
5 
6 #ifndef POLYGRAPH__RUNTIME_HOSTMAP_H
7 #define POLYGRAPH__RUNTIME_HOSTMAP_H
8 
9 #include "xstd/Array.h"
10 #include "xstd/NetAddr.h"
11 #include "runtime/httpHdrs.h"
12 
13 class ContentSel;
14 class ServerRep;
15 class SslWrap;
16 class ObjUniverse;
17 class HttpCookies;
18 class HostsBasedSym;
19 
20 // Host configuration record
21 class HostCfg {
22 	public:
23 		HostCfg(const NetAddr &anAddr);
24 
25 	public:
26 		const NetAddr theAddr;
27 		Agent::Protocol theProtocol;
28 		ContentSel *theContent;    // can be shared among HostCfgs
29 		ServerRep *theServerRep;
30 		ObjUniverse *theUniverse; // for visible servers only
31 		const HostsBasedSym *theHostsBasedCfg; // Proxy for SSL-to-proxy
32 		bool isSslActive; // whether SSL is configured and supported
33 };
34 
35 // a NetAddr <-> Host configuration map
36 // add-only operation
37 class HostMap {
38 	public:
39 		HostMap(int aCapacity); // cap may be adjusted a bit
40 		~HostMap();
41 
hostCount()42 		int hostCount() const { return theCount; }
iterationCount()43 		int iterationCount() const { return theStaticIndex.count() + theDynamicIndex.count(); }
44 
45 		HostCfg *at(int idx) const; // idx may be out of bounds
46 		HostCfg *at(const NetAddr &addr);
47 
48 		ServerRep *serverRepAt(int idx);
49 		bool sslActive(const NetAddr &addr);
50 		ObjUniverse *findUniverse(const NetAddr &addr);
51 		ObjUniverse *findUniverseAt(int idx);
52 
53 		HostCfg *find(const NetAddr &addr);
54 		HostCfg *find(const NetAddr &addr, int &idx);
55 
56 		// If `addr` is a domain not in the HostMap, then uses TheAddrMap to map
57 		// `addr` to an IP address and then find a HostCfg for that IP instead.
58 		// Randomly selects an IP if TheAddrMap maps `addr` to many IPs!
59 		HostCfg *mapAndFind(const NetAddr &addr, int &idx);
60 
61 		bool findIdx(const NetAddr &addr, int &idx);
62 		HostCfg *addAt(int idx, const NetAddr &addr);
63 
64 	protected:
capacity()65 		int capacity() const { return theStaticIndex.capacity(); }
66 		bool findIdxInIndex(const Array<HostCfg*> &arr, const NetAddr &addr, int &idx) const;
67 		bool endSearch(const Array<HostCfg*> &arr, const NetAddr &addr, int idx, bool &res) const;
68 		int hash0(const NetAddr &addr) const;
69 		int hash1(const NetAddr &addr) const;
70 
71 	protected:
72 		Array<HostCfg*> theStaticIndex; // non-dynamic host addresses
73 		Array<HostCfg*> theDynamicIndex; // dynamic host addresses
74 		int theCount;  // entries in both hashes
75 };
76 
77 extern HostMap *TheHostMap; // move to globals?
78 
79 #endif
80