BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file ncd-request.c |
||
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 | #include <stdio.h> |
||
31 | #include <string.h> |
||
32 | #include <stdlib.h> |
||
33 | #include <unistd.h> |
||
34 | |||
35 | #include <misc/string_begins_with.h> |
||
36 | #include <base/BLog.h> |
||
37 | #include <base/DebugObject.h> |
||
38 | #include <system/BNetwork.h> |
||
39 | #include <system/BReactor.h> |
||
40 | #include <system/BAddr.h> |
||
41 | #include <ncd/NCDValParser.h> |
||
42 | #include <ncd/NCDValGenerator.h> |
||
43 | #include <ncd/extra/NCDRequestClient.h> |
||
44 | |||
45 | #include <generated/blog_channel_ncd_request.h> |
||
46 | |||
47 | static void client_handler_error (void *user); |
||
48 | static void client_handler_connected (void *user); |
||
49 | static void request_handler_sent (void *user); |
||
50 | static void request_handler_reply (void *user, NCDValMem reply_mem, NCDValRef reply_value); |
||
51 | static void request_handler_finished (void *user, int is_error); |
||
52 | static int write_all (int fd, const uint8_t *data, size_t len); |
||
53 | static int make_connect_addr (const char *str, struct BConnection_addr *out_addr); |
||
54 | |||
55 | NCDStringIndex string_index; |
||
56 | NCDValMem request_mem; |
||
57 | NCDValRef request_value; |
||
58 | BReactor reactor; |
||
59 | NCDRequestClient client; |
||
60 | NCDRequestClientRequest request; |
||
61 | int have_request; |
||
62 | |||
63 | int main (int argc, char *argv[]) |
||
64 | { |
||
65 | int res = 1; |
||
66 | |||
67 | if (argc != 3) { |
||
68 | fprintf(stderr, "Usage: %s < unix:<socket_path> / tcp:<address>:<port> > <request_payload>\n", (argc > 0 ? argv[0] : "")); |
||
69 | goto fail0; |
||
70 | } |
||
71 | |||
72 | char *connect_address = argv[1]; |
||
73 | char *request_payload_string = argv[2]; |
||
74 | |||
75 | BLog_InitStderr(); |
||
76 | |||
77 | BTime_Init(); |
||
78 | |||
79 | if (!NCDStringIndex_Init(&string_index)) { |
||
80 | BLog(BLOG_ERROR, "NCDStringIndex_Init failed"); |
||
81 | goto fail01; |
||
82 | } |
||
83 | |||
84 | NCDValMem_Init(&request_mem, &string_index); |
||
85 | |||
86 | if (!NCDValParser_Parse(MemRef_MakeCstr(request_payload_string), &request_mem, &request_value)) { |
||
87 | BLog(BLOG_ERROR, "BReactor_Init failed"); |
||
88 | goto fail1; |
||
89 | } |
||
90 | |||
91 | if (!BNetwork_GlobalInit()) { |
||
92 | BLog(BLOG_ERROR, "BNetwork_Init failed"); |
||
93 | goto fail1; |
||
94 | } |
||
95 | |||
96 | if (!BReactor_Init(&reactor)) { |
||
97 | BLog(BLOG_ERROR, "BReactor_Init failed"); |
||
98 | goto fail1; |
||
99 | } |
||
100 | |||
101 | struct BConnection_addr addr; |
||
102 | if (!make_connect_addr(connect_address, &addr)) { |
||
103 | goto fail2; |
||
104 | } |
||
105 | |||
106 | if (!NCDRequestClient_Init(&client, addr, &reactor, &string_index, NULL, client_handler_error, client_handler_connected)) { |
||
107 | BLog(BLOG_ERROR, "NCDRequestClient_Init failed"); |
||
108 | goto fail2; |
||
109 | } |
||
110 | |||
111 | have_request = 0; |
||
112 | |||
113 | res = BReactor_Exec(&reactor); |
||
114 | |||
115 | if (have_request) { |
||
116 | NCDRequestClientRequest_Free(&request); |
||
117 | } |
||
118 | NCDRequestClient_Free(&client); |
||
119 | fail2: |
||
120 | BReactor_Free(&reactor); |
||
121 | fail1: |
||
122 | NCDValMem_Free(&request_mem); |
||
123 | NCDStringIndex_Free(&string_index); |
||
124 | fail01: |
||
125 | BLog_Free(); |
||
126 | fail0: |
||
127 | DebugObjectGlobal_Finish(); |
||
128 | return res; |
||
129 | } |
||
130 | |||
131 | static int make_connect_addr (const char *str, struct BConnection_addr *out_addr) |
||
132 | { |
||
133 | size_t i; |
||
134 | |||
135 | if (i = string_begins_with(str, "unix:")) { |
||
136 | *out_addr = BConnection_addr_unix(MemRef_MakeCstr(str + i)); |
||
137 | } |
||
138 | else if (i = string_begins_with(str, "tcp:")) { |
||
139 | BAddr baddr; |
||
140 | if (!BAddr_Parse2(&baddr, (char *)str + i, NULL, 0, 1)) { |
||
141 | BLog(BLOG_ERROR, "failed to parse tcp address"); |
||
142 | return 0; |
||
143 | } |
||
144 | |||
145 | *out_addr = BConnection_addr_baddr(baddr); |
||
146 | } |
||
147 | else { |
||
148 | BLog(BLOG_ERROR, "address must start with unix: or tcp:"); |
||
149 | return 0; |
||
150 | } |
||
151 | |||
152 | return 1; |
||
153 | } |
||
154 | |||
155 | static void client_handler_error (void *user) |
||
156 | { |
||
157 | BLog(BLOG_ERROR, "client error"); |
||
158 | |||
159 | BReactor_Quit(&reactor, 1); |
||
160 | } |
||
161 | |||
162 | static void client_handler_connected (void *user) |
||
163 | { |
||
164 | ASSERT(!have_request) |
||
165 | |||
166 | if (!NCDRequestClientRequest_Init(&request, &client, request_value, NULL, request_handler_sent, request_handler_reply, request_handler_finished)) { |
||
167 | BLog(BLOG_ERROR, "NCDRequestClientRequest_Init failed"); |
||
168 | BReactor_Quit(&reactor, 1); |
||
169 | return; |
||
170 | } |
||
171 | |||
172 | have_request = 1; |
||
173 | } |
||
174 | |||
175 | static void request_handler_sent (void *user) |
||
176 | { |
||
177 | ASSERT(have_request) |
||
178 | } |
||
179 | |||
180 | static void request_handler_reply (void *user, NCDValMem reply_mem, NCDValRef reply_value) |
||
181 | { |
||
182 | ASSERT(have_request) |
||
183 | |||
184 | char *str = NCDValGenerator_Generate(reply_value); |
||
185 | if (!str) { |
||
186 | BLog(BLOG_ERROR, "NCDValGenerator_Generate failed"); |
||
187 | goto fail0; |
||
188 | } |
||
189 | |||
190 | if (!write_all(1, (uint8_t *)str, strlen(str))) { |
||
191 | goto fail1; |
||
192 | } |
||
193 | if (!write_all(1, (const uint8_t *)"\n", 1)) { |
||
194 | goto fail1; |
||
195 | } |
||
196 | |||
197 | free(str); |
||
198 | NCDValMem_Free(&reply_mem); |
||
199 | return; |
||
200 | |||
201 | fail1: |
||
202 | free(str); |
||
203 | fail0: |
||
204 | NCDValMem_Free(&reply_mem); |
||
205 | BReactor_Quit(&reactor, 1); |
||
206 | } |
||
207 | |||
208 | static void request_handler_finished (void *user, int is_error) |
||
209 | { |
||
210 | if (is_error) { |
||
211 | BLog(BLOG_ERROR, "request error"); |
||
212 | BReactor_Quit(&reactor, 1); |
||
213 | return; |
||
214 | } |
||
215 | |||
216 | BReactor_Quit(&reactor, 0); |
||
217 | } |
||
218 | |||
219 | static int write_all (int fd, const uint8_t *data, size_t len) |
||
220 | { |
||
221 | while (len > 0) { |
||
222 | ssize_t res = write(fd, data, len); |
||
223 | if (res <= 0) { |
||
224 | BLog(BLOG_ERROR, "write failed"); |
||
225 | return 0; |
||
226 | } |
||
227 | data += res; |
||
228 | len -= res; |
||
229 | } |
||
230 | |||
231 | return 1; |
||
232 | } |