NCBI C++ ToolKit
access_list.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: access_list.cpp 69944 2015-11-23 17:36:05Z satskyse $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Victor Joukov
27  *
28  * File Description:
29  * NetSchedule access list.
30  */
31 #include <ncbi_pch.hpp>
32 
33 #include "access_list.hpp"
34 #include "ns_handler.hpp"
35 
36 #include <connect/ncbi_socket.hpp>
37 
39 
40 
41 // is host allowed to connect
42 bool CNetScheduleAccessList::IsAllowed(unsigned ha) const
43 {
44  CReadLockGuard guard(m_Lock);
45 
46  if (m_Hosts.count() == 0)
47  return true;
48 
49  return m_Hosts[ha];
50 }
51 
52 
53 // Delimited lists of hosts allowed into the system
54 CJsonNode CNetScheduleAccessList::SetHosts(const string & host_names)
55 {
57  vector<string> hosts;
58  NStr::Split(host_names, ";, \n\r", hosts,
60 
61  CWriteLockGuard guard(m_Lock);
62  TNSBitVector old_hosts = m_Hosts;
63  vector<string> old_as_from_config = m_AsFromConfig;
64 
65  m_Hosts.clear();
66  m_AsFromConfig.clear();
67 
68  ITERATE(vector<string>, it, hosts) {
69  const string & hn = *it;
70 
71  if (NStr::CompareNocase(hn, "localhost") == 0) {
72  string my_name = CSocketAPI::gethostname();
73  unsigned int ha = CSocketAPI::gethostbyname(my_name);
74 
75  if (ha != 0) {
76  m_Hosts.set_bit(ha, true);
77  m_AsFromConfig.push_back(hn);
78  continue;
79  }
80  }
81 
82  unsigned int ha = CSocketAPI::gethostbyname(hn);
83  if (ha != 0) {
84  m_Hosts.set_bit(ha, true);
85  m_AsFromConfig.push_back(hn);
86  }
87  else
88  ERR_POST("'" << hn << "' is not a valid host name. Ignored.");
89  }
90 
91  if (old_hosts != m_Hosts) {
94 
95  for (vector<string>::const_iterator k = old_as_from_config.begin();
96  k != old_as_from_config.end(); ++k)
97  old_vals.AppendString(*k);
98  for (vector<string>::const_iterator k = m_AsFromConfig.begin();
99  k != m_AsFromConfig.end(); ++k)
100  new_vals.AppendString(*k);
101 
102  diff.Append(old_vals);
103  diff.Append(new_vals);
104  }
105  return diff;
106 }
107 
108 
109 // Used to print as a comma separated string - part of output
110 // or as output in the socket with leading 'OK:'
111 string CNetScheduleAccessList::Print(const string & prefix,
112  const string & separator) const
113 {
114  string s;
115  CReadLockGuard guard(m_Lock);
117 
118  for(; en.valid(); ++en) {
119  if (!s.empty())
120  s += separator;
121  s += prefix + CSocketAPI::gethostbyaddr(*en);
122  }
123  return s;
124 }
125 
126 
128 {
129  string ret;
130  CReadLockGuard guard(m_Lock);
131 
132  for (vector<string>::const_iterator k = m_AsFromConfig.begin();
133  k != m_AsFromConfig.end(); ++k) {
134  if (!ret.empty())
135  ret += ", ";
136  ret += *k;
137  }
138  return ret;
139 }
140 
141 
143 
JSON node abstraction.
static CJsonNode NewArrayNode()
Create a new JSON array node.
void AppendString(const string &value)
For an array node, add a string node at the end of the array.
void Append(CJsonNode::TInstance value)
For an array node, add a new element at the end of the array.
bool IsAllowed(unsigned int ha) const
Definition: access_list.cpp:42
string GetAsFromConfig(void) const
CJsonNode SetHosts(const string &host_names)
Definition: access_list.cpp:54
vector< string > m_AsFromConfig
Definition: access_list.hpp:71
string Print(const string &prefix, const string &separator) const
Constant iterator designed to enumerate "ON" bits.
Definition: bm.h:603
bool valid() const noexcept
Checks if iterator is still valid.
Definition: bm.h:283
Bitvector Bit-vector container with runtime compression of bits.
Definition: bm.h:115
bool set_bit(size_type n, bool val=true)
Sets bit n.
Definition: bm.h:4227
enumerator first() const
Returns enumerator pointing on the first non-zero bit.
Definition: bm.h:1871
void clear(const size_type *ids, size_type ids_size, bm::sort_order so=bm::BM_UNKNOWN)
clear list of bits in this bitset
Definition: bm.h:4149
size_type count() const noexcept
population count (count of ON bits)
Definition: bm.h:2401
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#define ERR_POST(message)
Error posting with file, line number information but without error codes.
Definition: ncbidiag.hpp:186
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static string gethostname(ESwitch log=eOff)
Return empty string on error.
static unsigned int gethostbyname(const string &host, ESwitch log=eOff)
Return 0 on error.
static string gethostbyaddr(unsigned int host, ESwitch log=eOff)
Return empty string on error.
static int CompareNocase(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n, const char *s2)
Case-insensitive compare of a substring with another string.
Definition: ncbistr.cpp:219
static list< string > & Split(const CTempString str, const CTempString delim, list< string > &arr, TSplitFlags flags=0, vector< SIZE_TYPE > *token_pos=NULL)
Split a string using specified delimiters.
Definition: ncbistr.cpp:3461
@ fSplit_Truncate
Definition: ncbistr.hpp:2501
@ fSplit_MergeDelimiters
Merge adjacent delimiters.
Definition: ncbistr.hpp:2498
static const char * prefix[]
Definition: pcregrep.c:405
Modified on Sat May 25 14:22:31 2024 by modify_doxy.py rev. 669887