1 | /// ----------------------------------------*- mode: C++; -*--
|
---|
2 | /// @file threadsafe_db.h
|
---|
3 | /// Thread-safe netdb access
|
---|
4 | /// ----------------------------------------------------------
|
---|
5 | /// $Id: threadsafe_db.h 2549 2007-04-02 22:17:37Z bless $
|
---|
6 | /// $HeadURL: https://svn.ipv6.tm.uka.de/nsis/protlib/trunk/include/threadsafe_db.h $
|
---|
7 | // ===========================================================
|
---|
8 | //
|
---|
9 | // Copyright (C) 2005-2007, all rights reserved by
|
---|
10 | // - Institute of Telematics, Universitaet Karlsruhe (TH)
|
---|
11 | //
|
---|
12 | // More information and contact:
|
---|
13 | // https://projekte.tm.uka.de/trac/NSIS
|
---|
14 | //
|
---|
15 | // This program is free software; you can redistribute it and/or modify
|
---|
16 | // it under the terms of the GNU General Public License as published by
|
---|
17 | // the Free Software Foundation; version 2 of the License
|
---|
18 | //
|
---|
19 | // This program is distributed in the hope that it will be useful,
|
---|
20 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
21 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
22 | // GNU General Public License for more details.
|
---|
23 | //
|
---|
24 | // You should have received a copy of the GNU General Public License along
|
---|
25 | // with this program; if not, write to the Free Software Foundation, Inc.,
|
---|
26 | // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
---|
27 | //
|
---|
28 | // ===========================================================
|
---|
29 | /** @ingroup netdb
|
---|
30 | *
|
---|
31 | * Thread-safe netdb access for linux.
|
---|
32 | *
|
---|
33 | * As the reentrant functions for netdb access seem not portable, I decided to
|
---|
34 | * write some wrappers for those functions I need. This is not
|
---|
35 | * object-oriented because the NetDB would be another singleton object and
|
---|
36 | * I only want some wrapper functions.
|
---|
37 | */
|
---|
38 |
|
---|
39 | #ifndef THREADSAFE_DB_H
|
---|
40 | #define THREADSAFE_DB_H
|
---|
41 |
|
---|
42 | #include <sys/types.h>
|
---|
43 | #include <pthread.h>
|
---|
44 | #include <netinet/in.h>
|
---|
45 | #include <string>
|
---|
46 |
|
---|
47 | #include "protlib_types.h"
|
---|
48 |
|
---|
49 | namespace protlib {
|
---|
50 |
|
---|
51 | /** @addtogroup netdb Thread-safe DB
|
---|
52 | * @{
|
---|
53 | */
|
---|
54 |
|
---|
55 | /// Thread-safe DB
|
---|
56 | /** This class provides class methods for accessing the protocol database
|
---|
57 | * and maybe other services from netdb.h in a thread-safe way.
|
---|
58 | */
|
---|
59 | class tsdb {
|
---|
60 | private:
|
---|
61 | /// init state
|
---|
62 | static bool is_init;
|
---|
63 | /// enable/disable name resolving via DNS
|
---|
64 | static bool resolvenames;
|
---|
65 |
|
---|
66 | /// netdb mutex
|
---|
67 | static pthread_mutex_t mutex;
|
---|
68 | // @{
|
---|
69 | /// last used IDs
|
---|
70 | static uint32 id32;
|
---|
71 | static uint64 id64;
|
---|
72 | // @}
|
---|
73 |
|
---|
74 | // standard protocol ids
|
---|
75 | static protocol_t udp_id;
|
---|
76 | static protocol_t tcp_id;
|
---|
77 | static protocol_t sctp_id;
|
---|
78 |
|
---|
79 | public:
|
---|
80 | /// initialize netdb
|
---|
81 | static void init(bool noresolving= false);
|
---|
82 | /// cleanup netdb resources
|
---|
83 | static void end();
|
---|
84 | /// get new 32bit-ID
|
---|
85 | static uint32 get_new_id32();
|
---|
86 | /// get new 64bit-ID
|
---|
87 | static uint64 get_new_id64();
|
---|
88 | /// get protocol name by number
|
---|
89 | static string getprotobynumber(protocol_t proto, bool *res = NULL);
|
---|
90 | /// get protocol number by name
|
---|
91 | static protocol_t getprotobyname(const string &pname, bool *res = NULL);
|
---|
92 | /// get protocol number by name
|
---|
93 | static protocol_t getprotobyname(const char* pname, bool *res = NULL);
|
---|
94 | /// get frequently used protocol numbers
|
---|
95 | static protocol_t get_udp_id() { return udp_id; }
|
---|
96 | static protocol_t get_tcp_id() { return tcp_id; }
|
---|
97 | static protocol_t get_sctp_id() { return sctp_id; }
|
---|
98 |
|
---|
99 | /// get user name
|
---|
100 | static string get_username(uid_t uid, bool *res = NULL);
|
---|
101 | /// get user ID
|
---|
102 | static uid_t get_userid(const char* uname, bool *res = NULL);
|
---|
103 | /// get user ID
|
---|
104 | static uid_t get_userid(const string& uname, bool *res = NULL);
|
---|
105 | /// get port name
|
---|
106 | static string get_portname(port_t port, protocol_t prot, bool *res = NULL);
|
---|
107 | /// get port number
|
---|
108 | static port_t get_portnumber(const char* pname, protocol_t prot, bool *res = NULL);
|
---|
109 | /// get port number
|
---|
110 | static port_t get_portnumber(const string& pname, protocol_t prot, bool *res = NULL);
|
---|
111 | /// lookup host name
|
---|
112 | static string get_hostname(const struct sockaddr* sa, bool *res);
|
---|
113 | static string get_hostname(const in_addr& in, bool *res = NULL);
|
---|
114 | static string get_hostname(const in6_addr& in, bool *res = NULL);
|
---|
115 |
|
---|
116 | }; // end class tsdb
|
---|
117 |
|
---|
118 | //@}
|
---|
119 |
|
---|
120 | } // end namespace protlib
|
---|
121 |
|
---|
122 | #endif
|
---|