| 1 |
/* pipeconnect.c - Check the assuan_pipe_connect call. |
| 2 |
Copyright (C) 2006, 2009, 2010 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of Assuan. |
| 5 |
|
| 6 |
Assuan is free software; you can redistribute it and/or modify it |
| 7 |
under the terms of the GNU Lesser General Public License as |
| 8 |
published by the Free Software Foundation; either version 3 of |
| 9 |
the License, or (at your option) any later version. |
| 10 |
|
| 11 |
Assuan is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
Lesser General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU Lesser General Public |
| 17 |
License along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* |
| 21 |
This tests creates a program which starts an assuan server and runs |
| 22 |
some simple tests on it. The other program is actually the same |
| 23 |
program but called with the option --server. |
| 24 |
*/ |
| 25 |
|
| 26 |
#ifdef HAVE_CONFIG_H |
| 27 |
#include <config.h> |
| 28 |
#endif |
| 29 |
|
| 30 |
#include <stdio.h> |
| 31 |
#include <stdlib.h> |
| 32 |
#include <string.h> |
| 33 |
#include <assert.h> |
| 34 |
#include <errno.h> |
| 35 |
|
| 36 |
#ifdef HAVE_W32_SYSTEM |
| 37 |
#include <fcntl.h> |
| 38 |
#endif |
| 39 |
#include "../src/assuan.h" |
| 40 |
#include "common.h" |
| 41 |
|
| 42 |
static assuan_fd_t my_stdin = ASSUAN_INVALID_FD; |
| 43 |
static assuan_fd_t my_stdout = ASSUAN_INVALID_FD; |
| 44 |
static assuan_fd_t my_stderr = ASSUAN_INVALID_FD; |
| 45 |
|
| 46 |
|
| 47 |
static gpg_error_t |
| 48 |
cmd_echo (assuan_context_t ctx, char *line) |
| 49 |
{ |
| 50 |
log_info ("got ECHO command (%s)\n", line); |
| 51 |
|
| 52 |
assuan_send_data (ctx, line, strlen (line)); |
| 53 |
|
| 54 |
return 0; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
static gpg_error_t |
| 59 |
cmd_cat (assuan_context_t ctx, char *line) |
| 60 |
{ |
| 61 |
#ifdef HAVE_W32_SYSTEM |
| 62 |
assuan_fd_t assuan_fdin, assuan_fdout; |
| 63 |
#endif |
| 64 |
int fd, fdout; |
| 65 |
int c; |
| 66 |
FILE *fp, *fpout; |
| 67 |
int nbytes; |
| 68 |
|
| 69 |
log_info ("got CAT command (%s)\n", line); |
| 70 |
|
| 71 |
#ifdef HAVE_W32_SYSTEM |
| 72 |
assuan_fdin = assuan_get_input_fd (ctx); |
| 73 |
if (assuan_fdin == ASSUAN_INVALID_FD) |
| 74 |
return gpg_error (GPG_ERR_ASS_NO_INPUT); |
| 75 |
assuan_fdout = assuan_get_output_fd (ctx); |
| 76 |
if (assuan_fdout == ASSUAN_INVALID_FD) |
| 77 |
return gpg_error (GPG_ERR_ASS_NO_OUTPUT); |
| 78 |
fd = _open_osfhandle ((intptr_t)assuan_fdin, _O_RDONLY); |
| 79 |
if (fd < 0) |
| 80 |
return gpg_error (GPG_ERR_ASS_NO_INPUT); |
| 81 |
fdout = _open_osfhandle ((intptr_t)assuan_fdout, _O_WRONLY); |
| 82 |
if (fdout < 0) |
| 83 |
return gpg_error (GPG_ERR_ASS_NO_OUTPUT); |
| 84 |
#else |
| 85 |
fd = assuan_get_input_fd (ctx); |
| 86 |
if (fd == ASSUAN_INVALID_FD) |
| 87 |
return gpg_error (GPG_ERR_ASS_NO_INPUT); |
| 88 |
fdout = assuan_get_output_fd (ctx); |
| 89 |
if (fdout == ASSUAN_INVALID_FD) |
| 90 |
return gpg_error (GPG_ERR_ASS_NO_OUTPUT); |
| 91 |
#endif |
| 92 |
fp = fdopen (fd, "r"); |
| 93 |
if (!fp) |
| 94 |
{ |
| 95 |
log_error ("fdopen failed on input fd: %s\n", strerror (errno)); |
| 96 |
return gpg_error (GPG_ERR_ASS_GENERAL); |
| 97 |
} |
| 98 |
|
| 99 |
fpout = fdopen (fdout, "w"); |
| 100 |
if (!fpout) |
| 101 |
{ |
| 102 |
log_error ("fdopen failed on output fd: %s\n", strerror (errno)); |
| 103 |
fclose (fp); |
| 104 |
return gpg_error (GPG_ERR_ASS_GENERAL); |
| 105 |
} |
| 106 |
|
| 107 |
nbytes = 0; |
| 108 |
while ( (c=getc (fp)) != -1) |
| 109 |
{ |
| 110 |
putc (c, fpout); |
| 111 |
nbytes++; |
| 112 |
} |
| 113 |
log_info ("done printing %d bytes to output fd\n", nbytes); |
| 114 |
|
| 115 |
/* Fixme: This also closes the original fd. */ |
| 116 |
fclose (fp); |
| 117 |
fclose (fpout); |
| 118 |
return 0; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
static gpg_error_t |
| 123 |
server_register_commands (assuan_context_t ctx) |
| 124 |
{ |
| 125 |
static struct |
| 126 |
{ |
| 127 |
const char *name; |
| 128 |
gpg_error_t (*handler) (assuan_context_t, char *line); |
| 129 |
} table[] = |
| 130 |
{ |
| 131 |
{ "ECHO", cmd_echo }, |
| 132 |
{ "CAT", cmd_cat }, |
| 133 |
{ "INPUT", NULL }, |
| 134 |
{ "OUTPUT", NULL }, |
| 135 |
{ NULL, NULL } |
| 136 |
}; |
| 137 |
int i; |
| 138 |
gpg_error_t rc; |
| 139 |
|
| 140 |
for (i=0; table[i].name; i++) |
| 141 |
{ |
| 142 |
rc = assuan_register_command (ctx, table[i].name, table[i].handler, NULL); |
| 143 |
if (rc) |
| 144 |
return rc; |
| 145 |
} |
| 146 |
return 0; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
static void |
| 151 |
run_server (int enable_debug) |
| 152 |
{ |
| 153 |
int rc; |
| 154 |
assuan_context_t ctx; |
| 155 |
assuan_fd_t filedes[2]; |
| 156 |
|
| 157 |
filedes[0] = my_stdin; |
| 158 |
filedes[1] = my_stdout; |
| 159 |
|
| 160 |
rc = assuan_new (&ctx); |
| 161 |
if (rc) |
| 162 |
log_fatal ("assuan_new failed: %s\n", gpg_strerror (rc)); |
| 163 |
|
| 164 |
rc = assuan_init_pipe_server (ctx, filedes); |
| 165 |
if (rc) |
| 166 |
log_fatal ("assuan_init_pipe_server failed: %s\n", |
| 167 |
gpg_strerror (rc)); |
| 168 |
|
| 169 |
rc = server_register_commands (ctx); |
| 170 |
if (rc) |
| 171 |
log_fatal ("register_commands failed: %s\n", gpg_strerror(rc)); |
| 172 |
|
| 173 |
if (enable_debug) |
| 174 |
assuan_set_log_stream (ctx, stderr); |
| 175 |
|
| 176 |
for (;;) |
| 177 |
{ |
| 178 |
rc = assuan_accept (ctx); |
| 179 |
if (rc) |
| 180 |
{ |
| 181 |
if (rc != -1) |
| 182 |
log_error ("assuan_accept failed: %s\n", gpg_strerror (rc)); |
| 183 |
break; |
| 184 |
} |
| 185 |
|
| 186 |
log_info ("client connected. Client's pid is %ld\n", |
| 187 |
(long)assuan_get_pid (ctx)); |
| 188 |
|
| 189 |
rc = assuan_process (ctx); |
| 190 |
if (rc) |
| 191 |
log_error ("assuan_process failed: %s\n", gpg_strerror (rc)); |
| 192 |
} |
| 193 |
|
| 194 |
assuan_release (ctx); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
static gpg_error_t |
| 204 |
data_cb (void *opaque, const void *buffer, size_t length) |
| 205 |
{ |
| 206 |
(void)opaque; |
| 207 |
|
| 208 |
if (buffer) |
| 209 |
printf ("Received data `%.*s'\n", (int)length, (char*)buffer); |
| 210 |
return 0; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
static void |
| 215 |
run_client (const char *servername) |
| 216 |
{ |
| 217 |
gpg_error_t err; |
| 218 |
assuan_context_t ctx; |
| 219 |
assuan_fd_t no_close_fds[2]; |
| 220 |
const char *arglist[5]; |
| 221 |
|
| 222 |
no_close_fds[0] = assuan_fd_from_posix_fd (fileno (stderr)); |
| 223 |
no_close_fds[1] = ASSUAN_INVALID_FD; |
| 224 |
|
| 225 |
arglist[0] = servername; |
| 226 |
arglist[1] = "--server"; |
| 227 |
arglist[2] = debug? "--debug" : verbose? "--verbose":NULL; |
| 228 |
arglist[3] = NULL; |
| 229 |
|
| 230 |
err = assuan_new (&ctx); |
| 231 |
if (err) |
| 232 |
log_fatal ("assuan_new failed: %s\n", gpg_strerror (err)); |
| 233 |
|
| 234 |
err = assuan_pipe_connect (ctx, servername, arglist, no_close_fds, |
| 235 |
NULL, NULL, 0); |
| 236 |
if (err) |
| 237 |
{ |
| 238 |
log_error ("assuan_pipe_connect failed: %s\n", |
| 239 |
gpg_strerror (err)); |
| 240 |
assuan_release (ctx); |
| 241 |
return; |
| 242 |
} |
| 243 |
|
| 244 |
log_info ("server started; pid is %ld\n", |
| 245 |
(long)assuan_get_pid (ctx)); |
| 246 |
|
| 247 |
err = assuan_transact (ctx, "ECHO Your lucky number is 3552664958674928. " |
| 248 |
"Watch for it everywhere.", |
| 249 |
data_cb, NULL, NULL, NULL, NULL, NULL); |
| 250 |
if (err) |
| 251 |
{ |
| 252 |
log_error ("sending ECHO failed: %s\n", gpg_strerror (err)); |
| 253 |
return; |
| 254 |
} |
| 255 |
|
| 256 |
err = assuan_transact (ctx, "BYE", NULL, NULL, NULL, NULL, NULL, NULL); |
| 257 |
if (err) |
| 258 |
{ |
| 259 |
log_error ("sending BYE failed: %s\n", gpg_strerror (err)); |
| 260 |
return; |
| 261 |
} |
| 262 |
|
| 263 |
assuan_release (ctx); |
| 264 |
return; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
static void |
| 269 |
parse_std_file_handles (int *argcp, char ***argvp) |
| 270 |
{ |
| 271 |
(void)argcp; |
| 272 |
(void)argvp; |
| 273 |
my_stdin = assuan_fd_from_posix_fd (0); |
| 274 |
my_stdout = assuan_fd_from_posix_fd (1); |
| 275 |
my_stderr = assuan_fd_from_posix_fd (2); |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
/* |
| 280 |
M A I N |
| 281 |
*/ |
| 282 |
int |
| 283 |
main (int argc, char **argv) |
| 284 |
{ |
| 285 |
gpg_error_t err; |
| 286 |
const char *myname = "no-pgm"; |
| 287 |
int last_argc = -1; |
| 288 |
int server = 0; |
| 289 |
int silent_client = 0; |
| 290 |
int silent_server = 0; |
| 291 |
|
| 292 |
parse_std_file_handles (&argc, &argv); |
| 293 |
if (argc) |
| 294 |
{ |
| 295 |
myname = *argv; |
| 296 |
log_set_prefix (*argv); |
| 297 |
argc--; argv++; |
| 298 |
} |
| 299 |
while (argc && last_argc != argc ) |
| 300 |
{ |
| 301 |
last_argc = argc; |
| 302 |
if (!strcmp (*argv, "--help")) |
| 303 |
{ |
| 304 |
printf ("usage: %s [options]\n" |
| 305 |
"\n" |
| 306 |
"Options:\n" |
| 307 |
" --verbose Show what is going on\n" |
| 308 |
" --server Run in server mode\n", |
| 309 |
log_get_prefix ()); |
| 310 |
exit (0); |
| 311 |
} |
| 312 |
if (!strcmp (*argv, "--verbose")) |
| 313 |
{ |
| 314 |
verbose = 1; |
| 315 |
argc--; argv++; |
| 316 |
} |
| 317 |
else if (!strcmp (*argv, "--debug")) |
| 318 |
{ |
| 319 |
verbose = debug = 1; |
| 320 |
argc--; argv++; |
| 321 |
} |
| 322 |
else if (!strcmp (*argv, "--server")) |
| 323 |
{ |
| 324 |
server = 1; |
| 325 |
argc--; argv++; |
| 326 |
} |
| 327 |
else if (!strcmp (*argv, "--silent-server")) |
| 328 |
{ |
| 329 |
silent_server = 1; |
| 330 |
argc--; argv++; |
| 331 |
} |
| 332 |
else if (!strcmp (*argv, "--silent-client")) |
| 333 |
{ |
| 334 |
silent_client = 1; |
| 335 |
argc--; argv++; |
| 336 |
} |
| 337 |
else |
| 338 |
log_fatal ("invalid option `%s' (try --help)\n", *argv); |
| 339 |
} |
| 340 |
|
| 341 |
log_set_prefix (xstrconcat (log_get_prefix (), |
| 342 |
server? ".server":".client", NULL)); |
| 343 |
assuan_set_assuan_log_prefix (log_get_prefix ()); |
| 344 |
|
| 345 |
err = assuan_sock_init (); |
| 346 |
if (err) |
| 347 |
log_fatal ("socket init failed: %s\n", gpg_strerror (err)); |
| 348 |
|
| 349 |
if (server) |
| 350 |
{ |
| 351 |
log_info ("server started\n"); |
| 352 |
if (debug && !silent_server) |
| 353 |
assuan_set_assuan_log_stream (stderr); |
| 354 |
run_server (debug && !silent_server); |
| 355 |
log_info ("server finished\n"); |
| 356 |
} |
| 357 |
else |
| 358 |
{ |
| 359 |
log_info ("client started\n"); |
| 360 |
if (debug && !silent_client) |
| 361 |
assuan_set_assuan_log_stream (stderr); |
| 362 |
run_client (myname); |
| 363 |
log_info ("client finished\n"); |
| 364 |
} |
| 365 |
|
| 366 |
return errorcount ? 1 : 0; |
| 367 |
} |
| 368 |
|