BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file BConnectionGeneric.h |
||
3 | * @author Ambroz Bizjak <ambrop7@gmail.com> |
||
4 | * |
||
5 | * @section LICENSE |
||
6 | * |
||
7 | * Redistribution and use in source and binary forms, with or without |
||
8 | * modification, are permitted provided that the following conditions are met: |
||
9 | * 1. Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * 2. Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * 3. Neither the name of the author nor the |
||
15 | * names of its contributors may be used to endorse or promote products |
||
16 | * derived from this software without specific prior written permission. |
||
17 | * |
||
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
21 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
||
22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||
24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
||
25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||
27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
28 | */ |
||
29 | |||
30 | #ifndef BADVPN_BCONNECTION_GENERIC_H |
||
31 | #define BADVPN_BCONNECTION_GENERIC_H |
||
32 | |||
33 | #include <string.h> |
||
34 | |||
35 | #include <misc/debug.h> |
||
36 | #include <misc/strdup.h> |
||
37 | #include <misc/memref.h> |
||
38 | #include <base/BLog.h> |
||
39 | #include <system/BConnection.h> |
||
40 | |||
41 | #define BCONNECTION_ADDR_TYPE_BADDR 1 |
||
42 | #define BCONNECTION_ADDR_TYPE_UNIX 2 |
||
43 | |||
44 | struct BConnection_addr { |
||
45 | int type; |
||
46 | union { |
||
47 | BAddr baddr; |
||
48 | struct { |
||
49 | const char *str; |
||
50 | size_t len; |
||
51 | } unix_socket_path; |
||
52 | } u; |
||
53 | }; |
||
54 | |||
55 | static struct BConnection_addr BConnection_addr_baddr (BAddr baddr) |
||
56 | { |
||
57 | struct BConnection_addr addr; |
||
58 | addr.type = BCONNECTION_ADDR_TYPE_BADDR; |
||
59 | addr.u.baddr = baddr; |
||
60 | return addr; |
||
61 | } |
||
62 | |||
63 | static struct BConnection_addr BConnection_addr_unix (MemRef unix_socket_path) |
||
64 | { |
||
65 | struct BConnection_addr addr; |
||
66 | addr.type = BCONNECTION_ADDR_TYPE_UNIX; |
||
67 | addr.u.unix_socket_path.str = unix_socket_path.ptr; |
||
68 | addr.u.unix_socket_path.len = unix_socket_path.len; |
||
69 | return addr; |
||
70 | } |
||
71 | |||
72 | static int BListener_InitGeneric (BListener *o, struct BConnection_addr addr, BReactor *reactor, void *user, |
||
73 | BListener_handler handler) WARN_UNUSED; |
||
74 | static int BConnector_InitGeneric (BConnector *o, struct BConnection_addr addr, BReactor *reactor, void *user, |
||
75 | BConnector_handler handler) WARN_UNUSED; |
||
76 | |||
77 | static int BListener_InitGeneric (BListener *o, struct BConnection_addr addr, BReactor *reactor, void *user, |
||
78 | BListener_handler handler) |
||
79 | { |
||
80 | ASSERT(handler) |
||
81 | BNetwork_Assert(); |
||
82 | ASSERT(addr.type != BCONNECTION_ADDR_TYPE_UNIX || !memchr(addr.u.unix_socket_path.str, '\0', addr.u.unix_socket_path.len)) |
||
83 | |||
84 | switch (addr.type) { |
||
85 | case BCONNECTION_ADDR_TYPE_BADDR: { |
||
86 | return BListener_Init(o, addr.u.baddr, reactor, user, handler); |
||
87 | } break; |
||
88 | |||
89 | case BCONNECTION_ADDR_TYPE_UNIX: { |
||
90 | #ifdef BADVPN_USE_WINAPI |
||
91 | BLog_LogToChannel(BLOG_CHANNEL_BConnection, BLOG_ERROR, "unix sockets not supported"); |
||
92 | return 0; |
||
93 | #else |
||
94 | char *str = b_strdup_bin(addr.u.unix_socket_path.str, addr.u.unix_socket_path.len); |
||
95 | if (!str) { |
||
96 | BLog_LogToChannel(BLOG_CHANNEL_BConnection, BLOG_ERROR, "b_strdup_bin failed"); |
||
97 | return 0; |
||
98 | } |
||
99 | int res = BListener_InitUnix(o, str, reactor, user, handler); |
||
100 | free(str); |
||
101 | return res; |
||
102 | #endif |
||
103 | } break; |
||
104 | |||
105 | default: |
||
106 | ASSERT(0); |
||
107 | return 0; |
||
108 | } |
||
109 | } |
||
110 | |||
111 | static int BConnector_InitGeneric (BConnector *o, struct BConnection_addr addr, BReactor *reactor, void *user, |
||
112 | BConnector_handler handler) |
||
113 | { |
||
114 | ASSERT(handler) |
||
115 | BNetwork_Assert(); |
||
116 | ASSERT(addr.type != BCONNECTION_ADDR_TYPE_UNIX || !memchr(addr.u.unix_socket_path.str, '\0', addr.u.unix_socket_path.len)) |
||
117 | |||
118 | switch (addr.type) { |
||
119 | case BCONNECTION_ADDR_TYPE_BADDR: { |
||
120 | return BConnector_Init(o, addr.u.baddr, reactor, user, handler); |
||
121 | } break; |
||
122 | |||
123 | case BCONNECTION_ADDR_TYPE_UNIX: { |
||
124 | #ifdef BADVPN_USE_WINAPI |
||
125 | BLog_LogToChannel(BLOG_CHANNEL_BConnection, BLOG_ERROR, "unix sockets not supported"); |
||
126 | return 0; |
||
127 | #else |
||
128 | char *str = b_strdup_bin(addr.u.unix_socket_path.str, addr.u.unix_socket_path.len); |
||
129 | if (!str) { |
||
130 | BLog_LogToChannel(BLOG_CHANNEL_BConnection, BLOG_ERROR, "b_strdup_bin failed"); |
||
131 | return 0; |
||
132 | } |
||
133 | int res = BConnector_InitUnix(o, str, reactor, user, handler); |
||
134 | free(str); |
||
135 | return res; |
||
136 | #endif |
||
137 | } break; |
||
138 | |||
139 | default: |
||
140 | ASSERT(0); |
||
141 | return 0; |
||
142 | } |
||
143 | } |
||
144 | |||
145 | #endif |