| 1 |
\input texinfo @c -*-texinfo-*- |
| 2 |
@c %**start of header |
| 3 |
@setfilename assuan.info |
| 4 |
|
| 5 |
@macro copyrightnotice |
| 6 |
Copyright @copyright{} 2001--2013 Free Software Foundation, Inc. @* |
| 7 |
Copyright @copyright{} 2001--2015 g10 Code GmbH |
| 8 |
@end macro |
| 9 |
@macro permissionnotice |
| 10 |
Permission is granted to copy, distribute and/or modify this document |
| 11 |
under the terms of the GNU General Public License as published by the |
| 12 |
Free Software Foundation; either version 3 of the License, or (at your |
| 13 |
option) any later version. The text of the license can be found in the |
| 14 |
section entitled ``Copying''. |
| 15 |
@end macro |
| 16 |
|
| 17 |
@include version.texi |
| 18 |
|
| 19 |
@settitle Developing with Assuan |
| 20 |
|
| 21 |
@c Create a separate index for command line options. |
| 22 |
@defcodeindex op |
| 23 |
@c Merge the standard indexes into a single one. |
| 24 |
@syncodeindex fn cp |
| 25 |
@syncodeindex vr cp |
| 26 |
@syncodeindex ky cp |
| 27 |
@syncodeindex pg cp |
| 28 |
@syncodeindex tp cp |
| 29 |
|
| 30 |
@c A simple macro for optional variables. |
| 31 |
@macro ovar{varname} |
| 32 |
@r{[}@var{\varname\}@r{]} |
| 33 |
@end macro |
| 34 |
|
| 35 |
@c printing stuff taken from gcc. |
| 36 |
@macro gnupgtabopt{body} |
| 37 |
@code{\body\} |
| 38 |
@end macro |
| 39 |
@macro gnupgoptlist{body} |
| 40 |
@smallexample |
| 41 |
\body\ |
| 42 |
@end smallexample |
| 43 |
@end macro |
| 44 |
@c Makeinfo handles the above macro OK, TeX needs manual line breaks; |
| 45 |
@c they get lost at some point in handling the macro. But if @macro is |
| 46 |
@c used here rather than @alias, it produces double line breaks. |
| 47 |
@iftex |
| 48 |
@alias gol = * |
| 49 |
@end iftex |
| 50 |
@ifnottex |
| 51 |
@macro gol |
| 52 |
@end macro |
| 53 |
@end ifnottex |
| 54 |
|
| 55 |
|
| 56 |
@c Change the font used for @def... commands, since the default |
| 57 |
@c proportional one used is bad for names starting __. |
| 58 |
@tex |
| 59 |
\gdef\mysetfont#1#2#3#4{\font#1=\fontprefix#2#3 scaled #4} |
| 60 |
\global\mysetfont\defbf\ttbshape{10}{\magstep1} |
| 61 |
@end tex |
| 62 |
|
| 63 |
@c %**end of header |
| 64 |
|
| 65 |
@ifnottex |
| 66 |
@dircategory GNU Libraries |
| 67 |
@direntry |
| 68 |
* Assuan: (assuan). An IPC library for non-persistent servers. |
| 69 |
@end direntry |
| 70 |
This file documents the use and the internals of Assuan. |
| 71 |
|
| 72 |
This is Edition @value{EDITION}, last updated @value{UPDATED}, of |
| 73 |
@cite{The `Developing with Assuan' Manual}, for Version @value{VERSION}. |
| 74 |
@sp 1 |
| 75 |
Published by the Free Software Foundation@* |
| 76 |
51 Franklin Street, Fifth Floor@* |
| 77 |
Boston, MA 02110-1301 USA |
| 78 |
@sp 1 |
| 79 |
@copyrightnotice{} |
| 80 |
@sp 1 |
| 81 |
@permissionnotice{} |
| 82 |
@end ifnottex |
| 83 |
|
| 84 |
@setchapternewpage odd |
| 85 |
|
| 86 |
@titlepage |
| 87 |
@title Developing with Assuan |
| 88 |
@subtitle Version @value{VERSION} |
| 89 |
@subtitle @value{UPDATED} |
| 90 |
@author by Werner Koch and Marcus Brinkmann |
| 91 |
@author @code{@{wk,mb@}@@g10code.com} |
| 92 |
|
| 93 |
@page |
| 94 |
@vskip 0pt plus 1filll |
| 95 |
@copyrightnotice{} |
| 96 |
@sp 2 |
| 97 |
@permissionnotice{} |
| 98 |
@end titlepage |
| 99 |
@summarycontents |
| 100 |
@contents |
| 101 |
@page |
| 102 |
|
| 103 |
|
| 104 |
@ifnottex |
| 105 |
@node Top |
| 106 |
@top Introduction |
| 107 |
@cindex introduction |
| 108 |
|
| 109 |
This manual documents how to exploit the Assuan library, a simple |
| 110 |
interprocess communcation library. |
| 111 |
@end ifnottex |
| 112 |
|
| 113 |
@menu |
| 114 |
* Introduction:: An introduction to and the motivation behind Assuan. |
| 115 |
* Assuan:: Description of the Assuan protocol. |
| 116 |
* Implementation:: Overview of the implementation. |
| 117 |
* Preparation:: What you should do before using the library. |
| 118 |
* Generalities:: Initialization code and data types used. |
| 119 |
* Client code:: How to develop an Assuan client. |
| 120 |
* Server code:: How to develop an Assuan server. |
| 121 |
* External I/O Loop:: How to use external I/O event loops. |
| 122 |
* Utilities:: Utility functions. |
| 123 |
* Socket wrappers:: Socket wrapper functions. |
| 124 |
|
| 125 |
Miscellaneous |
| 126 |
|
| 127 |
* Library Copying:: GNU Lesser General Public License says |
| 128 |
how you can copy and share Assuan. |
| 129 |
* Copying:: How you can copy and share this manual. |
| 130 |
|
| 131 |
Indices |
| 132 |
|
| 133 |
* Index:: Index of concepts and symbol names. |
| 134 |
|
| 135 |
@end menu |
| 136 |
|
| 137 |
|
| 138 |
@c |
| 139 |
@c I N T R O |
| 140 |
@c |
| 141 |
@node Introduction |
| 142 |
@chapter Introduction to Assuan |
| 143 |
|
| 144 |
Assuan is an extensible inter-process communication (IPC) protocol and |
| 145 |
library. It is designed for point-to-point communication and it |
| 146 |
doesn't provide a naming system. To contact a server, either the |
| 147 |
client must know how to locate the server, e.g., via a well-known Unix |
| 148 |
domain socket, or, if the server is transient, how to start it. In |
| 149 |
the latter case, Assuan provides functionality to start the server |
| 150 |
process. |
| 151 |
|
| 152 |
In Assuan, communication is typically either via a pipe or a Unix |
| 153 |
domain socket. This method is neither elegant nor efficient, |
| 154 |
especially when there is a lot of data spread across several |
| 155 |
transactions. Not only is there a penalty for an increased number of |
| 156 |
context switches, but a significant amount of data is @var{memcpy}ed |
| 157 |
from the client to a file descriptor and from the file descriptor to |
| 158 |
the server. Despite these and other disadvantages, this type of |
| 159 |
client/server communication is useful: the client is separated from |
| 160 |
the server: they run in different address spaces. This is especially |
| 161 |
important in situations where the server must have a known degree of |
| 162 |
reliability and data must be protected: as the Assuan protocol is well |
| 163 |
defined and clients cannot corrupt the servers' address space, |
| 164 |
auditing becomes much easier. |
| 165 |
|
| 166 |
Assuan was developed for use by the GNU Privacy Guard (GnuPG) to |
| 167 |
prevent potentially buggy clients from unwittingly corrupting |
| 168 |
sensitive transactions or compromising data such as a secret key. |
| 169 |
Assuan permits the servers, which do the actual work, e.g., encryption |
| 170 |
and decryption of data using a secret key, to be developed |
| 171 |
independently of the user interfaces, e.g., mail clients and other |
| 172 |
encryption front ends. Like a shared library, the interface is well |
| 173 |
defined and any number of front ends can use it; however, unlike a |
| 174 |
shared library, the client cannot see or touch the server's data. As |
| 175 |
with any modular system, Assuan helps keep the components small, |
| 176 |
understandable and less error prone. |
| 177 |
|
| 178 |
Assuan is not, however, limited to use with GnuPG servers and clients: |
| 179 |
it was designed to be flexible enough to meet the demands of many |
| 180 |
transaction-based environments. |
| 181 |
|
| 182 |
@node Assuan |
| 183 |
@chapter Description of the Assuan protocol. |
| 184 |
|
| 185 |
The architecture of the modular GnuPG system is based on several |
| 186 |
highly specialized modules which form a network of clients and |
| 187 |
servers. A common framework for intermodule communication is |
| 188 |
therefore needed and implemented as a library. |
| 189 |
|
| 190 |
Goals: |
| 191 |
|
| 192 |
@itemize @bullet |
| 193 |
@item Common framework for module communication |
| 194 |
@item Easy debugging |
| 195 |
@item Easy module testing |
| 196 |
@item Extensible |
| 197 |
@item Optional authentication and encryption facility |
| 198 |
@item Usable to access external hardware |
| 199 |
@end itemize |
| 200 |
|
| 201 |
|
| 202 |
Design criteria: |
| 203 |
|
| 204 |
@itemize @bullet |
| 205 |
@item Client/Server with back channel |
| 206 |
@item Use a mainly text based protocol |
| 207 |
@item Escape certain control characters |
| 208 |
@item Allow indefinite data length |
| 209 |
@item Request confidentiality for parts of the communication |
| 210 |
@item Dummy module to allow direct linking of client and server |
| 211 |
@item Inline data or descriptor passing for bulk data |
| 212 |
@item No protection against DoS needed |
| 213 |
@item Subliminal channels are not an issue |
| 214 |
@end itemize |
| 215 |
|
| 216 |
|
| 217 |
@node Implementation |
| 218 |
@chapter Implementation |
| 219 |
|
| 220 |
The implementation is line based with a maximum line size of 1000 |
| 221 |
octets. The default IPC mechanism is Unix Domain Sockets. |
| 222 |
|
| 223 |
On connect, the server responds either with okay or an error status. |
| 224 |
To perform an authentication check, the server may send an Inquiry |
| 225 |
response prior to the first Okay. It may also issue Status messages. |
| 226 |
The server must check that the client is allowed to connect. This is |
| 227 |
done by requesting the credentials for the peer and comparing them |
| 228 |
with the server's credentials. This avoids attacks based on wrong |
| 229 |
socket permissions. |
| 230 |
|
| 231 |
The server may choose to delay the first response in case of an error. |
| 232 |
The server, however, never closes the connection, however, the lower |
| 233 |
protocol may do so after some time of inactivity or when the |
| 234 |
connection enters an error state. |
| 235 |
|
| 236 |
All textual messages are assumed to be in UTF-8 unless otherwise noted. |
| 237 |
|
| 238 |
|
| 239 |
@menu |
| 240 |
* Server responses:: Description of server responses. |
| 241 |
* Client requests:: Description of client requests. |
| 242 |
* Error codes:: List of error and status codes. |
| 243 |
@end menu |
| 244 |
|
| 245 |
|
| 246 |
@node Server responses |
| 247 |
@section Server responses |
| 248 |
|
| 249 |
@table @code |
| 250 |
@item OK [<arbitrary debugging information>] |
| 251 |
Request was successful. |
| 252 |
|
| 253 |
@item ERR @var{errorcode} [<human readable error description>] |
| 254 |
Request could not be fulfilled. The possible error codes are defined |
| 255 |
by @code{libgpg-error}. |
| 256 |
|
| 257 |
@item S @var{keyword} <status information depending on keyword> |
| 258 |
Informational output by the server, which is still processing the |
| 259 |
request. A client may not send such lines to the server while |
| 260 |
processing an Inquiry command. @var{keyword} shall start with a |
| 261 |
letter or an underscore. |
| 262 |
|
| 263 |
@item # <string> |
| 264 |
Comment line issued only for debugging purposes. Totally ignored. |
| 265 |
|
| 266 |
@item D <raw data> |
| 267 |
Raw data returned to client. There must be exactly one space after the |
| 268 |
'D'. The values for '%', CR and LF must be percent escaped; these are |
| 269 |
encoded as %25, %0D and %0A, respectively. Only uppercase letters |
| 270 |
should be used in the hexadecimal representation. Other characters |
| 271 |
may be percent escaped for easier debugging. All Data lines are |
| 272 |
considered one data stream up to the OK or ERR response. Status and |
| 273 |
Inquiry Responses may be mixed with the Data lines. |
| 274 |
|
| 275 |
@item INQUIRE @var{keyword} <parameters> |
| 276 |
The server needs further information from the client. The client |
| 277 |
should respond with data (using the ``D'' command and terminated by |
| 278 |
``END''). Alternatively, the client may cancel the current operation |
| 279 |
by responding with ``CAN''. |
| 280 |
@end table |
| 281 |
|
| 282 |
Consider the following examples (lines prefixed with S indicate text |
| 283 |
that the server sends; lines prefixed with C indicate text that the |
| 284 |
client sends): |
| 285 |
|
| 286 |
@example |
| 287 |
S: INQUIRE foo |
| 288 |
C: D foo bar |
| 289 |
C: D bar baz |
| 290 |
C: END |
| 291 |
[Server continues normal work] |
| 292 |
@end example |
| 293 |
|
| 294 |
This implements a callback to the client: |
| 295 |
|
| 296 |
@example |
| 297 |
S: INQUIRE foo |
| 298 |
C: END |
| 299 |
[Server continues] |
| 300 |
@end example |
| 301 |
|
| 302 |
and: |
| 303 |
|
| 304 |
@example |
| 305 |
S: INQUIRE foo |
| 306 |
C: CAN |
| 307 |
[Server terminates the operaion and in most cases returns an ERR to the client.] |
| 308 |
@end example |
| 309 |
|
| 310 |
But, CAN may also mean ``I have no data for you, try to get it from |
| 311 |
elsewhere.'' |
| 312 |
|
| 313 |
|
| 314 |
Note: lines longer than 1000 bytes should be treated as a |
| 315 |
communication error. (The rationale for having a line length limit is |
| 316 |
to allow for easier multiplexing of several channels.) |
| 317 |
|
| 318 |
@node Client requests |
| 319 |
@section Client requests |
| 320 |
|
| 321 |
The server waits for client requests after sending an Okay or Error. |
| 322 |
The client should not issue a request in other cases. |
| 323 |
|
| 324 |
@example |
| 325 |
@var{command} <parameters> |
| 326 |
@end example |
| 327 |
|
| 328 |
@var{command} is a one word string without preceding white space. |
| 329 |
Parameters are command specific, CR, LF and the percent signs should |
| 330 |
be percent escaped as described above. To send a backslash as the |
| 331 |
last character it should also be percent escaped. Percent escaping is |
| 332 |
allowed anywhere in the parameters but not in the command. The line |
| 333 |
ends with a CR, LF pair or just a LF. |
| 334 |
|
| 335 |
Not yet implemented feature: If there is a need for a parameter list |
| 336 |
longer than the line length limit (1000 characters including command |
| 337 |
and CR, LF), the last character of the line (right before the CR/LF or |
| 338 |
LF) must be a unescaped (i.e., literal) backslash. The following line |
| 339 |
is then expected to be a continuation of the line with the backslash |
| 340 |
replaced by a blank and the line ending removed. |
| 341 |
|
| 342 |
@example |
| 343 |
D <raw data> |
| 344 |
@end example |
| 345 |
|
| 346 |
Sends raw data to the server. There must be exactly one space after |
| 347 |
the 'D'. The values for '%', CR and LF must be percent escaped. |
| 348 |
These are encoded as %25, %0D and %0A, respectively. Only uppercase |
| 349 |
letters should be used in the hexadecimal representation. Other |
| 350 |
characters may be percent escaped for easier debugging. All Data |
| 351 |
lines are considered one data stream up to the @code{OK} or @code{ERR} |
| 352 |
response. Status and Inquiry Responses may be mixed with the Data |
| 353 |
lines. |
| 354 |
|
| 355 |
@example |
| 356 |
END |
| 357 |
@end example |
| 358 |
|
| 359 |
Lines beginning with a @code{#} or empty lines are ignored. This is |
| 360 |
useful to comment test scripts. |
| 361 |
|
| 362 |
Although the commands are application specific, some of them are used |
| 363 |
by all protocols and partly supported by the Assuan library: |
| 364 |
|
| 365 |
@table @code |
| 366 |
@item BYE |
| 367 |
Close the connection. The server will respond with @code{OK}. |
| 368 |
|
| 369 |
@item RESET |
| 370 |
Reset the connection but not any existing authentication. The server |
| 371 |
should release all resources associated with the connection. |
| 372 |
|
| 373 |
@item END |
| 374 |
Used by a client to mark the end of raw data. The server may send |
| 375 |
@code{END} to indicate a partial end of data. |
| 376 |
|
| 377 |
@item HELP |
| 378 |
Lists all commands that the server understands as comment lines on the |
| 379 |
status channel. |
| 380 |
|
| 381 |
@item QUIT |
| 382 |
Reserved for future extensions. |
| 383 |
|
| 384 |
@item OPTION |
| 385 |
Set options for the connection. The syntax of such a line is |
| 386 |
@display |
| 387 |
OPTION @var{name} [ [=] @var{value} ] |
| 388 |
@end display |
| 389 |
Leading and trailing spaces around @var{name} and @var{value} are |
| 390 |
allowed but should be ignored. For compatibility reasons, @var{name} |
| 391 |
may be prefixed with two dashes. The use of the equal sign is optional |
| 392 |
but suggested if @var{value} is given. |
| 393 |
|
| 394 |
@item CANCEL |
| 395 |
This command is reserved for future extensions. |
| 396 |
|
| 397 |
@item AUTH |
| 398 |
This command is reserved for future extensions. Not yet specified as |
| 399 |
we don't implement it in the first phase. See Werner's mail to gpa-dev on |
| 400 |
2001-10-25 about the rationale for measurements against local attacks. |
| 401 |
|
| 402 |
@item NOP |
| 403 |
No operation. Returns OK without any action. |
| 404 |
@end table |
| 405 |
|
| 406 |
|
| 407 |
@node Error codes |
| 408 |
@section Error codes |
| 409 |
|
| 410 |
Libassuan is used with gpg-error style error codes. It is recommended |
| 411 |
to set the error source to a different value from the default |
| 412 |
@code{GPG_ERR_SOURCE_UNKNOWN} by calling @ref{function |
| 413 |
assuan_set_gpg_err_source} early. |
| 414 |
|
| 415 |
|
| 416 |
@c |
| 417 |
@c P R E P A R A T I O N |
| 418 |
@c |
| 419 |
@node Preparation |
| 420 |
@chapter Preparation |
| 421 |
|
| 422 |
To use @sc{Assuan}, you have to make some changes to your |
| 423 |
sources and the build system. The necessary changes are small and |
| 424 |
explained in the following sections. |
| 425 |
|
| 426 |
|
| 427 |
@menu |
| 428 |
* Header:: What header file you need to include. |
| 429 |
* Building sources:: How to build sources using the library. |
| 430 |
* Automake:: How to build sources with the help of Automake. |
| 431 |
* Multi Threading:: How @code{libassuan} can be used in a MT environment. |
| 432 |
@end menu |
| 433 |
|
| 434 |
@node Header |
| 435 |
@section Header |
| 436 |
|
| 437 |
All interfaces (data types and functions) of @code{libassuan} are defined |
| 438 |
in the header file @file{assuan.h}. You must include this in all source |
| 439 |
files using the library, either directly or through some other header |
| 440 |
file, like this: |
| 441 |
|
| 442 |
@example |
| 443 |
#include <assuan.h> |
| 444 |
@end example |
| 445 |
|
| 446 |
The namespace of @code{libassuan} is @code{assuan_*} for function |
| 447 |
and type names and @code{ASSUAN*} for other symbols. In addition the |
| 448 |
same name prefixes with one prepended underscore are reserved for |
| 449 |
internal use and should never be used by an application. |
| 450 |
|
| 451 |
Because @code{libassuan} makes use of the GPG Error library, using |
| 452 |
@code{libassuan} will also use the @code{GPG_ERR_*} namespace |
| 453 |
directly, and the @code{gpg_err*} and @code{gpg_str*} namespaces |
| 454 |
indirectly. |
| 455 |
|
| 456 |
|
| 457 |
@node Building sources |
| 458 |
@section Building sources |
| 459 |
|
| 460 |
If you want to compile a source file including the @file{assuan.h} |
| 461 |
header file, you must make sure that the compiler can find it in the |
| 462 |
directory hierarchy. This is accomplished by adding the path to the |
| 463 |
directory in which the header file is located to the compilers include |
| 464 |
file search path (via the @option{-I} option). |
| 465 |
|
| 466 |
However, the path to the include file is determined at the time the |
| 467 |
source is configured. To solve this problem, @code{libassuan} ships |
| 468 |
with @code{libassuan.pc} file, that knows the path to the include file |
| 469 |
and other configuration options. The options that need to be added to |
| 470 |
the compiler invocation at compile time are output by the |
| 471 |
@option{--cflags} option to @command{pkg-config libassuan}. The following |
| 472 |
example shows how it can be used at the command line: |
| 473 |
|
| 474 |
@example |
| 475 |
gcc -c foo.c $(pkg-config --cflags libassuan) |
| 476 |
@end example |
| 477 |
|
| 478 |
Adding the output of @samp{pkg-config --cflags libassuan} to the compiler's |
| 479 |
command line will ensure that the compiler can find the @file{assuan.h} |
| 480 |
header file. |
| 481 |
|
| 482 |
A similar problem occurs when linking the program with the library. |
| 483 |
Again, the compiler/linker has to find the library files. For this to |
| 484 |
work, the path to the library files has to be added to the library |
| 485 |
search path (via the @option{-L} option). For this, the option |
| 486 |
@option{--libs} to @command{pkg-config libassuan} can be used. For |
| 487 |
convenience, this option also outputs all other options that are |
| 488 |
required to link the program with the @code{libassuan} libraries (in |
| 489 |
particular, the @option{-lassuan} option). The example shows how to |
| 490 |
link @file{foo.o} with the @code{libassuan} library to a program |
| 491 |
@command{foo}. |
| 492 |
|
| 493 |
@example |
| 494 |
gcc -o foo foo.o $(pkg-config --libs libassuan) |
| 495 |
@end example |
| 496 |
|
| 497 |
You can also combine both examples to a single command by specifying |
| 498 |
both options to @command{pkg-config libassuan}: |
| 499 |
|
| 500 |
@example |
| 501 |
gcc -o foo foo.c $(pkg-config --cflags --libs libassuan) |
| 502 |
@end example |
| 503 |
|
| 504 |
|
| 505 |
@node Automake |
| 506 |
@section Building sources using Automake |
| 507 |
|
| 508 |
It is much easier if you use GNU Automake instead of writing your own |
| 509 |
Makefiles. If you do that you do not have to worry about finding and |
| 510 |
invoking the @command{pkg-config} script at all. |
| 511 |
|
| 512 |
You can use @code{PKG_CHECK_MODULES} macro, or, @code{libassuan} also |
| 513 |
provides an Automake macro that does all the work for you. |
| 514 |
|
| 515 |
@defmac AM_PATH_LIBASSUAN (@ovar{minimum-version}, @ovar{action-if-found}, @ovar{action-if-not-found}) |
| 516 |
Check whether @code{libassuan} (at least version |
| 517 |
@var{minimum-version}, if given) exists on the host system. If it is |
| 518 |
found, execute @var{action-if-found}, otherwise do |
| 519 |
@var{action-if-not-found}, if given. |
| 520 |
|
| 521 |
Additionally, the function defines @code{LIBASSUAN_CFLAGS} to the |
| 522 |
flags needed for compilation of the program to find the |
| 523 |
@file{assuan.h} header file, and @code{LIBASSUAN_LIBS} to the linker |
| 524 |
flags needed to link the program to the @code{libassuan} library. |
| 525 |
@end defmac |
| 526 |
|
| 527 |
You can use the defined Autoconf variables like this in your |
| 528 |
@file{Makefile.am}: |
| 529 |
|
| 530 |
@example |
| 531 |
AM_CPPFLAGS = $(LIBASSUAN_CFLAGS) |
| 532 |
LDADD = $(LIBASSUAN_LIBS) |
| 533 |
@end example |
| 534 |
|
| 535 |
|
| 536 |
@node Multi Threading |
| 537 |
@section Multi Threading |
| 538 |
|
| 539 |
The @code{libassuan} library is designed so that it can be used in a |
| 540 |
threaded application, if some rules are followed. |
| 541 |
|
| 542 |
@itemize @bullet |
| 543 |
@item Run the initialization functions before you actually start |
| 544 |
to use threads. Specifically, the functions |
| 545 |
@code{assuan_set_gpg_err_source}, @code{assuan_set_malloc_hooks} and |
| 546 |
@code{assuan_set_log_cb} should not be called concurrently with |
| 547 |
@code{assuan_new}. Use @code{assuan_new_ext} instead or ensure proper |
| 548 |
serialization. |
| 549 |
@item Only one thread at a time may access an @code{libassuan} context. |
| 550 |
@item If you use the default log handler, use |
| 551 |
@code{assuan_set_assuan_log_stream} to setup a default log stream. |
| 552 |
@item If you have callback functions shared by multiple functions, |
| 553 |
the callback function must be reentrant for that purpose. |
| 554 |
@code{libassuan} does not serialize invocation of callback functions |
| 555 |
across contexts. |
| 556 |
@end itemize |
| 557 |
|
| 558 |
|
| 559 |
@c |
| 560 |
@c G E N E R A L I T I E S |
| 561 |
@c |
| 562 |
@node Generalities |
| 563 |
@chapter Generalities |
| 564 |
|
| 565 |
@menu |
| 566 |
* Data Types:: Data types used by @code{libassuan}. |
| 567 |
* Initializing the library:: How to initialize the library. |
| 568 |
* Default Log Handler:: How to configure the default log handler. |
| 569 |
* Contexts:: How to work with contexts. |
| 570 |
* Reading and Writing:: How to communicate with the peer. |
| 571 |
@end menu |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
@node Data Types |
| 576 |
@section Data Types used by the library |
| 577 |
|
| 578 |
@sc{Assuan} uses a so-called context to store a connection's state. |
| 579 |
The following data type is used for that: |
| 580 |
|
| 581 |
@deftp {Data type} assuan_context_t |
| 582 |
The @code{assuan_context_t} type is a pointer to an object maintained |
| 583 |
internally by the library. Contexts are allocated with |
| 584 |
@code{assuan_new} or @code{assuan_new_ext} and released with |
| 585 |
@code{assuan_release}. Other functions take this data type to access |
| 586 |
the state created by these functions. |
| 587 |
@end deftp |
| 588 |
|
| 589 |
|
| 590 |
@deftp {Data type} assuan_fd_t |
| 591 |
The @code{assuan_fd_t} is a file descriptor (in Unix) or a system |
| 592 |
handle (in Windows). The special value @code{ASSUAN_INVALID_FD} is |
| 593 |
used to specify invalid Assuan file descriptors. |
| 594 |
@end deftp |
| 595 |
|
| 596 |
|
| 597 |
@deftypefun assuan_fd_t assuan_fdopen (@w{int @var{fd}}) |
| 598 |
Create an assuan file descriptor from a POSIX (libc) file descriptor |
| 599 |
@var{fd}. On Unix, this is equivalent to @code{dup}, while on Windows |
| 600 |
this will retrieve the underlying system handle with |
| 601 |
@code{_get_osfhandle} and duplicate that. |
| 602 |
@end deftypefun |
| 603 |
|
| 604 |
|
| 605 |
@node Initializing the library |
| 606 |
@section Initializing the library |
| 607 |
|
| 608 |
Libassuan makes use of Libgpg-error and assumes that Libgpg-error has |
| 609 |
been initialized. In general @code{gpgrt_check_version} should be |
| 610 |
called to guarantee this; the Libgpg-error manual for details. |
| 611 |
|
| 612 |
Libassuan itself requires no initialization. There are however some |
| 613 |
initialization hooks provided which are often useful. These should be |
| 614 |
called as early as possible and in a multi-threaded application before |
| 615 |
a second thread is created. |
| 616 |
|
| 617 |
These functions initialize default values that are used at context |
| 618 |
creation with @code{assuan_new}. As there can only be one default, |
| 619 |
all values can also be set directly with @code{assuan_new_ext} or with |
| 620 |
context-specific functions after context creation. |
| 621 |
|
| 622 |
If your application uses its own memory allocation functions or wrappers |
| 623 |
it is good idea to tell @code{libassuan} about it so it can make use of the |
| 624 |
same functions or wrappers: |
| 625 |
|
| 626 |
@deftp {Data type} {struct assuan_malloc_hooks} |
| 627 |
This structure is used to store the memory allocation callback |
| 628 |
interface functions. It has the following members, whose semantics |
| 629 |
are identical to the corresponding system functions: |
| 630 |
|
| 631 |
@table @code |
| 632 |
@item void *(*malloc) (size_t cnt) |
| 633 |
This is the function called by @sc{Assuan} to allocate memory for a context. |
| 634 |
|
| 635 |
@item void *(*realloc) (void *ptr, size_t cnt) |
| 636 |
This is the function called by @sc{Assuan} to reallocate memory for a context. |
| 637 |
|
| 638 |
@item void (*free) (void *ptr) |
| 639 |
This is the function called by @sc{Assuan} to release memory for a context. |
| 640 |
@end table |
| 641 |
@end deftp |
| 642 |
|
| 643 |
@deftp {Data type} {assuan_malloc_hooks_t} |
| 644 |
This is a pointer to a @code{struct assuan_malloc_hooks}. |
| 645 |
@end deftp |
| 646 |
|
| 647 |
@deftypefun void assuan_set_malloc_hooks (@w{assuan_malloc_hooks_t @var{malloc_hooks}}) |
| 648 |
This function sets the default allocation hooks for new contexts |
| 649 |
allocated with @code{assuan_new}. You need to provide all three |
| 650 |
functions. Those functions need to behave exactly as their standard |
| 651 |
counterparts @code{malloc}, @code{realloc} and @code{free}. If you |
| 652 |
write your own functions, please take care to set @code{errno} |
| 653 |
whenever an error has occurred. |
| 654 |
@end deftypefun |
| 655 |
|
| 656 |
@deftypefun assuan_malloc_hooks_t assuan_get_malloc_hooks () |
| 657 |
This function gets the default allocation hooks for new contexts |
| 658 |
allocated with @code{assuan_new}. The result structure is statically |
| 659 |
allocated and should not be modified. |
| 660 |
@end deftypefun |
| 661 |
|
| 662 |
The @sc{Assuan} library uses @code{libgpg-error} error values, which |
| 663 |
consist and error code and an error source. The default source used |
| 664 |
by contexts allocated with @code{assuan_new} can be set with the |
| 665 |
following function. |
| 666 |
|
| 667 |
@anchor{function assuan_set_gpg_err_source} |
| 668 |
@deftypefun void assuan_set_gpg_err_source (@w{gpg_err_source_t @var{err_source}}) |
| 669 |
This function sets the default error source for errors generated by |
| 670 |
contexts allocated with @code{assuan_new}. |
| 671 |
|
| 672 |
One way to call this function is |
| 673 |
@smallexample |
| 674 |
assuan_set_gpg_err_source (GPG_ERR_SOURCE_DEFAULT); |
| 675 |
@end smallexample |
| 676 |
@end deftypefun |
| 677 |
|
| 678 |
@deftypefun gpg_err_source_t assuan_get_gpg_err_source (void) |
| 679 |
This function gets the default error source for errors generated by |
| 680 |
contexts allocated with @code{assuan_new}. |
| 681 |
@end deftypefun |
| 682 |
|
| 683 |
@noindent |
| 684 |
To integrate assuan logging and diagnostics into your own logging |
| 685 |
system, you may use the following two functions: |
| 686 |
|
| 687 |
@deftp {Data type} {int (*assuan_log_cb_t) (@w{assuan_context_t @var{ctx}}, @w{void *@var{hook_value}}, @w{unsigned int @var{cat}}, @w{const char *@var{msg}})} |
| 688 |
The user-provided callback function takes a context @var{ctx}, for |
| 689 |
which the message @var{msg} was generated, and a hook value |
| 690 |
@var{hook_value} that was supplied when the log handler was registered |
| 691 |
for the context with @code{assuan_set_log_cb}, and a category |
| 692 |
@var{cat}. The category is one of: |
| 693 |
|
| 694 |
@table @code |
| 695 |
@item ASSUAN_LOG_INIT |
| 696 |
@item ASSUAN_LOG_CTX |
| 697 |
@item ASSUAN_LOG_ENGINE |
| 698 |
@item ASSUAN_LOG_DATA |
| 699 |
RFU |
| 700 |
@item ASSUAN_LOG_SYSIO |
| 701 |
Log lowlevel I/O data. |
| 702 |
@item ASSUAN_LOG_CONTROL |
| 703 |
Log the control channel. |
| 704 |
@end table |
| 705 |
|
| 706 |
The user may then, depending on the category, write the message to a |
| 707 |
log file or treat it in some other way. |
| 708 |
|
| 709 |
If @var{msg} is a null pointer, then no message should be logged, but |
| 710 |
the function should return 1 if it is interested in log messages with |
| 711 |
the category @var{cat}. If it is not interested, 0 should be |
| 712 |
returned. This allows @code{libassuan} to suppress the generation of |
| 713 |
expensive debug output. |
| 714 |
@end deftp |
| 715 |
|
| 716 |
@deftypefun void assuan_set_log_cb (@w{assuan_log_cb_t @var{log_cb}}, @w{void *@var{log_cb_data}}) |
| 717 |
This function sets the default logging handler for log messages |
| 718 |
generated by contexts allocated with @code{assuan_new}. |
| 719 |
@end deftypefun |
| 720 |
|
| 721 |
@deftypefun void assuan_get_log_cb (@w{assuan_log_cb_t *@var{log_cb}}, @w{void **@var{log_cb_data}}) |
| 722 |
This function gets the default logging handler for log messages |
| 723 |
generated by contexts allocated with @code{assuan_new}. |
| 724 |
@end deftypefun |
| 725 |
|
| 726 |
You do not need to set a log handler, as @sc{Assuan} provides a |
| 727 |
configurable default log handler that should be suitable for most |
| 728 |
purposes. Logging can be disabled completely by setting the log |
| 729 |
handler to a null pointer. |
| 730 |
|
| 731 |
@node Default Log Handler |
| 732 |
@section Default Log Handler |
| 733 |
|
| 734 |
The default log handler can be configured by the following functions: |
| 735 |
|
| 736 |
@deftypefun void assuan_set_assuan_log_prefix (@w{const char *@var{text}}) |
| 737 |
Set the prefix to be used at the start of a line emitted by assuan |
| 738 |
on the log stream to @var{text}. The default is the empty string. |
| 739 |
@end deftypefun |
| 740 |
|
| 741 |
|
| 742 |
@deftypefun @w{const char *} assuan_get_assuan_log_prefix (void) |
| 743 |
Return the prefix to be used at the start of a line emitted by assuan |
| 744 |
on the log stream. The default implementation returns the empty |
| 745 |
string. |
| 746 |
@end deftypefun |
| 747 |
|
| 748 |
|
| 749 |
@deftypefun void assuan_set_assuan_log_stream (FILE *@var{fp}) |
| 750 |
This sets the default log stream to which @code{libassuan} should log messages not |
| 751 |
associated with a specific context to @var{fp}. The default is to log |
| 752 |
to @code{stderr}. This default value is also changed by using |
| 753 |
@code{assuan_set_log_stream} (to set a logging stream for a specific |
| 754 |
context) unless this function has been used. Obviously this is not |
| 755 |
thread-safe and thus it is highly recommended to use this function to |
| 756 |
setup a proper default. |
| 757 |
@end deftypefun |
| 758 |
|
| 759 |
|
| 760 |
@deftypefun @w{FILE *} assuan_get_assuan_log_stream (void) |
| 761 |
Return the stream which is currently being using for global logging. |
| 762 |
@end deftypefun |
| 763 |
|
| 764 |
The log stream used by the default log handler can also be set on a |
| 765 |
per context basis. |
| 766 |
|
| 767 |
@deftypefun void assuan_set_log_stream (@w{assuan_context_t @var{ctx}}, @w{FILE *@var{fp}}) |
| 768 |
Enable debugging for the context @var{ctx} and write all debugging |
| 769 |
output to the stdio stream @var{fp}. If the default log stream (used |
| 770 |
for non-context specific events) has not yet been set, a call to this |
| 771 |
functions implicitly sets this stream also to @var{fp}. |
| 772 |
@end deftypefun |
| 773 |
|
| 774 |
|
| 775 |
@node Contexts |
| 776 |
@section How to work with contexts |
| 777 |
|
| 778 |
Some operations work globally on the library, but most operate in a |
| 779 |
context, which saves state across operations. To allow the use of |
| 780 |
@code{libassuan} in mixed environments, such as in a library using |
| 781 |
GPGME and an application using GPGME, the context is very extensive |
| 782 |
and covers utilitary information like memory allocation callbacks as |
| 783 |
well as specific information associated with client/server operations. |
| 784 |
|
| 785 |
@anchor{function assuan_new} |
| 786 |
@deftypefun gpg_error_t assuan_new (@w{assuan_context_t *@var{ctx_p}}) |
| 787 |
The function @code{assuan_new} creates a new context, using the global |
| 788 |
default memory allocation, log handler and @code{libgpg-error} source. |
| 789 |
It is equivalent to |
| 790 |
|
| 791 |
@smallexample |
| 792 |
gpg_error_t err; |
| 793 |
assuan_log_cb_t log_cb; |
| 794 |
void *log_cb_data; |
| 795 |
|
| 796 |
assuan_get_log_cb (&log_cb, &log_cb_data); |
| 797 |
err = assuan_new_ext (ctx_p, assuan_get_gpg_err_source (), |
| 798 |
assuan_get_malloc_hooks (), log_cb, log_cb_data); |
| 799 |
@end smallexample |
| 800 |
|
| 801 |
As you can see, this is not thread-safe. Take care not to modify the |
| 802 |
memory allocation hooks or log callback handler concurrently with |
| 803 |
@code{assuan_new}. |
| 804 |
|
| 805 |
The function returns an error if a memory allocation error occurs, and |
| 806 |
0 with the new context in @var{ctx_p} otherwise. |
| 807 |
@end deftypefun |
| 808 |
|
| 809 |
@deftypefun gpg_error_t assuan_new_ext (@w{assuan_context_t *@var{ctx_p}}, @w{gpg_err_source_t @var{err_source}}, @w{assuan_malloc_hooks_t @var{malloc_hooks}}, @w{assuan_log_cb_t @var{log_cb}}, @w{void *@var{log_cb_data}}) |
| 810 |
The function @code{assuan_new_ext} creates a new context using the |
| 811 |
supplied @code{libgpg-error} error source @var{err_source}, the memory |
| 812 |
allocation hooks @var{malloc_hooks} and the log handler @var{log_cb} |
| 813 |
with the user data @var{log_cb_data}. |
| 814 |
@end deftypefun |
| 815 |
|
| 816 |
After the context has been used, it can be destroyed again. |
| 817 |
|
| 818 |
@deftypefun void assuan_release (assuan_context_t ctx) |
| 819 |
The function @code{assuan_release} destroys the context CTX and |
| 820 |
releases all associated resources. |
| 821 |
@end deftypefun |
| 822 |
|
| 823 |
Other properties of the context beside the memory allocation handler, |
| 824 |
the log handler, and the @code{libgpg-error} source can be set after |
| 825 |
context creation. Here are some of them: |
| 826 |
|
| 827 |
@deftypefun void assuan_set_pointer (@w{assuan_context_t @var{ctx}}, @w{void *@var{pointer}}) |
| 828 |
|
| 829 |
Store the arbitrary pointer value @var{pointer} into the context |
| 830 |
@var{ctx}. This is useful to provide command handlers with additional |
| 831 |
application context. |
| 832 |
@end deftypefun |
| 833 |
|
| 834 |
@deftypefun void* assuan_get_pointer (@w{assuan_context_t @var{ctx}}) |
| 835 |
|
| 836 |
This returns the pointer for context @var{ctx} which has been set using |
| 837 |
the above function. A common way to use it is by setting the pointer |
| 838 |
before starting the processing loop and to retrieve it right at the |
| 839 |
start of a command handler: |
| 840 |
@smallexample |
| 841 |
static int |
| 842 |
cmd_foo (assuan_context_t ctx, char *line) |
| 843 |
@{ |
| 844 |
ctrl_t ctrl = assuan_get_pointer (ctx); |
| 845 |
... |
| 846 |
@} |
| 847 |
@end smallexample |
| 848 |
@end deftypefun |
| 849 |
|
| 850 |
|
| 851 |
@deftypefun void assuan_set_flag (@w{assuan_context_t @var{ctx}}, @w{assuan_flag_t @var{flag}}, @w{int @var{value}}) |
| 852 |
|
| 853 |
Set the the @var{flag} for context @var{ctx} to @var{value}. Values for |
| 854 |
flags are usually 1 or 0 but certain flags might need other values. |
| 855 |
|
| 856 |
@deftp {Data type} assuan_flag_t |
| 857 |
The flags are all named and collected in an @code{enum} for better readability. |
| 858 |
Available flags are: |
| 859 |
|
| 860 |
@table @code |
| 861 |
@item ASSUAN_NO_WAITPID |
| 862 |
When using a pipe server, by default Libassuan will wait for the forked |
| 863 |
process to die in @code{assuan_release}. In certain cases this is |
| 864 |
not desirable. By setting this flag, a call to @code{waitpid} will be |
| 865 |
suppressed and the caller is responsible to cleanup the child process. |
| 866 |
@item ASSUAN_CONFIDENTIAL |
| 867 |
Use to return the state of the confidential logging mode. |
| 868 |
@item ASSUAN_NO_FIXSIGNALS |
| 869 |
Do not modify signal handler for @code{SIGPIPE}. |
| 870 |
@item ASSUAN_CONVEY_COMMENTS |
| 871 |
If enabled comment lines are passed to the status callback of the |
| 872 |
@code{assuan_transact}. |
| 873 |
@item ASSUAN_FORCE_CLOSE |
| 874 |
Setting this flag forces the next command to assume that the |
| 875 |
connection has been closed. This breaks the command processing loop |
| 876 |
and may be used as an implicit BYE command. @var{value} is ignored |
| 877 |
and thus it is not possible to clear this flag. |
| 878 |
@end table |
| 879 |
@end deftp |
| 880 |
@end deftypefun |
| 881 |
|
| 882 |
@deftypefun int assuan_get_flag (@w{assuan_context_t @var{ctx}}, @w{assuan_flag_t @var{flag}}) |
| 883 |
Return the value of @var{flag} in context @var{ctx}. |
| 884 |
@end deftypefun |
| 885 |
|
| 886 |
|
| 887 |
@deftypefun void assuan_begin_confidential (@w{assuan_context_t @var{ctx}}) |
| 888 |
Put the logging feature into confidential mode. This is to avoid |
| 889 |
logging of sensitive data. |
| 890 |
|
| 891 |
This is identical to: |
| 892 |
@smallexample |
| 893 |
assuan_set_flag (ctx, ASSUAN_CONFIDENTIAL, 1); |
| 894 |
@end smallexample |
| 895 |
@end deftypefun |
| 896 |
|
| 897 |
|
| 898 |
@deftypefun void assuan_end_confidential (@w{assuan_context_t @var{ctx}}) |
| 899 |
Get the logging feature out of confidential mode. All data will be |
| 900 |
logged again (if logging is enabled). |
| 901 |
|
| 902 |
This is identical to: |
| 903 |
@smallexample |
| 904 |
assuan_set_flag (ctx, ASSUAN_CONFIDENTIAL, 0); |
| 905 |
@end smallexample |
| 906 |
@end deftypefun |
| 907 |
|
| 908 |
|
| 909 |
@deftp {Data type} {struct assuan_system_hooks} |
| 910 |
This structure is used to store the system callback interface |
| 911 |
functions. It has the following members, whose semantics are similar |
| 912 |
to the corresponding system functions, but not exactly equivalent. |
| 913 |
|
| 914 |
@table @code |
| 915 |
@item int version |
| 916 |
The user should set this to @code{ASSUAN_SYSTEM_HOOKS_VERSION}. This |
| 917 |
indicates to the library which members of this structure are present |
| 918 |
in case of future extensions. The user should initialize the whole |
| 919 |
structure with zero bytes. |
| 920 |
|
| 921 |
@item void (*usleep) (assuan_context_t ctx, unsigned int usec) |
| 922 |
This is the function called by @sc{Assuan} to sleep for @code{USEC} |
| 923 |
microseconds. |
| 924 |
|
| 925 |
@item int (*pipe) (assuan_context_t ctx, assuan_fd_t fd[2], int inherit_idx) |
| 926 |
This is the function called by @sc{Assuan} to create a pipe. The |
| 927 |
returned file descriptor @code{fd[inherit_idx]} must be inheritable by |
| 928 |
the child process (under Windows, this requires some extra work). |
| 929 |
|
| 930 |
@item int (*close) (assuan_context_t ctx, assuan_fd_t fd) |
| 931 |
This is the function called by @sc{Assuan} to close a file descriptor |
| 932 |
created through the system functions. |
| 933 |
|
| 934 |
@item ssize_t (*read) (assuan_context_t ctx, assuan_fd_t fd, void *buffer, size_t size) |
| 935 |
This is the function called by @sc{Assuan} to read data from a file |
| 936 |
descriptor. It is functionally equivalent to the system @code{read} |
| 937 |
function. |
| 938 |
|
| 939 |
@item ssize_t (*write) (assuan_context_t ctx, assuan_fd_t fd, const void *buffer, size_t size) |
| 940 |
This is the function called by @sc{Assuan} to write data to a file |
| 941 |
descriptor. It is functionally equivalent to the system @code{write} |
| 942 |
function. |
| 943 |
|
| 944 |
@item int (*recvmsg) (assuan_context_t ctx, assuan_fd_t fd, assuan_msghdr_t msg, int flags) |
| 945 |
This is the function called by @sc{Assuan} to receive a message from a |
| 946 |
file descriptor. It is functionally equivalent to the system |
| 947 |
@code{recvmsg} function. |
| 948 |
|
| 949 |
@item int (*sendmsg) (assuan_context_t ctx, assuan_fd_t fd, const assuan_msghdr_t msg, int flags); |
| 950 |
This is the function called by @sc{Assuan} to send a message to a |
| 951 |
file descriptor. It is functionally equivalent to the system |
| 952 |
@code{sendmsg} function. |
| 953 |
|
| 954 |
@item int (*spawn) (assuan_context_t ctx, assuan_pid_t *r_pid, const char *name, const char **argv, assuan_fd_t fd_in, assuan_fd_t fd_out, assuan_fd_t *fd_child_list, void (*atfork) (void *opaque, int reserved), void *atforkvalue, unsigned int flags) |
| 955 |
This is the function called by @sc{Assuan} to spawn a child process. |
| 956 |
The @code{stdin} and @code{stdout} file descriptors are provided in |
| 957 |
@code{fd_in} and @code{fd_out} respectively, but can be set to |
| 958 |
@code{ASSUAN_INVALID_FD}, in which case they are set to |
| 959 |
@code{/dev/null}. On systems which use @code{fork} and @code{exec}, |
| 960 |
the @code{atfork} function should be called with @code{atforkvalue} |
| 961 |
and @code{0} for flags in the child process right after @code{fork} |
| 962 |
returns. @code{fd_child_list} is a @code{ASSUAN_INVALID_FD} |
| 963 |
terminated array (or @code{NULL}) and specifies file descriptors to be |
| 964 |
inherited by the child process. |
| 965 |
|
| 966 |
A special situation occurs if @code{name} is a null pointer, in which |
| 967 |
case the process should just fork but not call @code{exec}. In this |
| 968 |
case, @code{*argv} should be set to @code{"client"} in the parent |
| 969 |
process and @code{"server"} in the child process. |
| 970 |
|
| 971 |
Flags is the bit-wise OR of some (or none) of the following flags: |
| 972 |
|
| 973 |
@table @code |
| 974 |
@item ASSUAN_SPAWN_DETACHED |
| 975 |
If set and there is a need to start the server it will be started as a |
| 976 |
background process. This flag is useful under W32 systems, so that no |
| 977 |
new console is created and pops up a console window when starting the |
| 978 |
server. On W32CE systems this flag is ignored. |
| 979 |
@end table |
| 980 |
|
| 981 |
@item assuan_pid_t (*waitpid) (assuan_context_t ctx, assuan_pid_t pid, int action, int *status, int options) |
| 982 |
This is the function called by @sc{Assuan} to wait for the spawned |
| 983 |
child process @var{pid} to exit, or, if @var{action} is 1, to just |
| 984 |
release all resources associated with @var{pid} (required on Windows |
| 985 |
platforms). If @var{action} is 0, this is equivalent to @code{waitpid}. |
| 986 |
|
| 987 |
@item int (*socketpair) (assuan_context_t ctx, int namespace, int style, int protocol, assuan_fd_t filedes[2]) |
| 988 |
This is the function called by @sc{Assuan} to create a socketpair. It |
| 989 |
is equivalent to @code{socketpair}. |
| 990 |
@end table |
| 991 |
@end deftp |
| 992 |
|
| 993 |
|
| 994 |
@deftypefun void assuan_set_system_hooks (@w{assuan_system_hooks_t @var{system_hooks}}) |
| 995 |
Set the default system hooks to use. There is currently no way to |
| 996 |
reset to the default system hooks. |
| 997 |
@end deftypefun |
| 998 |
|
| 999 |
@deftypefun void assuan_sock_set_system_hooks (@w{assuan_system_hooks_t @var{system_hooks}}) |
| 1000 |
The socket subsystem uses an internal context which uses the default |
| 1001 |
system hooks. This function allows to change these system hooks. The |
| 1002 |
function is not thread-safe and only useful if a certain order of |
| 1003 |
assuan and assuan socket initializations are required. |
| 1004 |
@end deftypefun |
| 1005 |
|
| 1006 |
@deftypefun void assuan_ctx_set_system_hooks (@w{assuan_context_t @var{ctx}}, @w{assuan_system_hooks_t @var{system_hooks}}) |
| 1007 |
Set the system hooks for context @var{ctx}. There is currently no way |
| 1008 |
to reset to the default system hooks, create a new context for that. |
| 1009 |
@end deftypefun |
| 1010 |
|
| 1011 |
|
| 1012 |
The following system hook collections are defined by the library for |
| 1013 |
your convenience: |
| 1014 |
|
| 1015 |
@table @code |
| 1016 |
@item ASSUAN_SYSTEM_NPTH |
| 1017 |
System hooks suitable for use with the nPth library. |
| 1018 |
@item ASSUAN_SYSTEM_NPTH_IMPL |
| 1019 |
The implementation of system hooks for use with the nPth library. |
| 1020 |
This must be invoked once somewhere in the application, and defines |
| 1021 |
the structure that is referenced by @code{ASSUAN_SYSTEM_NPTH}. |
| 1022 |
@end table |
| 1023 |
|
| 1024 |
|
| 1025 |
@node Reading and Writing |
| 1026 |
@section How to communicate with the peer |
| 1027 |
|
| 1028 |
What would be an IPC library without the ability to read and write |
| 1029 |
data? Not very useful. Libassuan has high level functions to take |
| 1030 |
care of of the more boring stuff, but eventually data needs to be |
| 1031 |
written and read. |
| 1032 |
|
| 1033 |
@noindent |
| 1034 |
The basic read and write functions are: |
| 1035 |
|
| 1036 |
@deftypefun gpg_error_t assuan_read_line (@w{assuan_context_t @var{ctx}}, @w{char **@var{line}}, @w{size_t *@var{linelen}}) |
| 1037 |
|
| 1038 |
Read the next line written by the peer to the control channel and store |
| 1039 |
a pointer to the buffer holding that line at the address @var{line}. |
| 1040 |
The valid length of the lines is stored at the address of @var{linelen}. |
| 1041 |
This buffer is valid until the next read operation on the same context |
| 1042 |
@var{ctx}. You may modify the context of this buffer. The buffer is |
| 1043 |
invalid (i.e., must not be used) if an error is returned. This function |
| 1044 |
returns @code{0} on success or an error value. |
| 1045 |
@end deftypefun |
| 1046 |
|
| 1047 |
@deftypefun gpg_error_t assuan_write_line (@w{assuan_context_t @var{ctx}}, @w{const char *@var{line}}) |
| 1048 |
|
| 1049 |
Write the string @var{line} to the other end on the control channel. |
| 1050 |
This string needs to be a proper formatted Assuan protocol line and |
| 1051 |
should not include a linefeed. Sending linefeed or @code{Nul} |
| 1052 |
characters is not possible and not allowed by the assuan protocol. This |
| 1053 |
function shall not be used for sending data (@code{D}) lines. This |
| 1054 |
function returns @code{0} on success or an error value. |
| 1055 |
@end deftypefun |
| 1056 |
|
| 1057 |
@noindent |
| 1058 |
To actually send bulk data lines a specialized function is available: |
| 1059 |
|
| 1060 |
@deftypefun gpg_error_t assuan_send_data (@w{assuan_context_t @var{ctx}}, @w{const void *@var{buffer}}, @w{size_t @var{length}}) |
| 1061 |
|
| 1062 |
This function is used by a server or a client to send @var{length} bytes |
| 1063 |
of bulk data in @var{buffer} to the other end on the control channel. |
| 1064 |
The data will be escaped as required by the Assuan protocol and may get |
| 1065 |
buffered until a line is full. To flush any pending data, @var{buffer} |
| 1066 |
may be passed as @code{NULL} and @var{length} be @code{0}. |
| 1067 |
|
| 1068 |
@noindent |
| 1069 |
When used by a client, this flush operation does also send the |
| 1070 |
@code{END} command to terminate the response on an @command{INQUIRE} |
| 1071 |
request. Note that the function @code{assuan_transact} takes care of |
| 1072 |
sending this @code{END} itself. |
| 1073 |
|
| 1074 |
@noindent |
| 1075 |
This function returns @code{0} on success or an error value. |
| 1076 |
@end deftypefun |
| 1077 |
|
| 1078 |
The input and output of data can be controlled at a higher level using |
| 1079 |
an I/O monitor. |
| 1080 |
|
| 1081 |
@deftp {Data type} {unsigned int (*assuan_io_monitor_t) (@w{assuan_context_t @var{ctx}}, @w{void *@var{hook_value}}, @w{int @var{inout}}, @w{const char *@var{line}}, @w{size_t @var{linelen}})} |
| 1082 |
The monitor function is called right after a line has been received, |
| 1083 |
if @var{inout} is @code{ASSUAN_IO_FROM_PEER}, or just before it is |
| 1084 |
send, if @var{inout} is @code{ASSUAN_IO_TO_PEER}. The |
| 1085 |
@var{hook_value} is provided by the user when registering the I/O |
| 1086 |
monitor function with a context using @code{assuan_set_io_monitor}. |
| 1087 |
The callback function should return the bitwise OR of some (or none) of the |
| 1088 |
following flags: |
| 1089 |
|
| 1090 |
@table @code |
| 1091 |
@item ASSUAN_IO_MONITOR_NOLOG |
| 1092 |
Active logging of this line is suppressed. This can reduce debug |
| 1093 |
output in the case of a frequent message. |
| 1094 |
@item ASSUAN_IO_MONITOR_IGNORE |
| 1095 |
The whole output line is discarded. |
| 1096 |
@end table |
| 1097 |
@end deftp |
| 1098 |
|
| 1099 |
@deftypefun void assuan_set_io_monitor (@w{assuan_context_t @var{ctx}}, @w{assuan_io_monitor_t @var{io_monitor}}, @w{void *@var{hook_data}}) |
| 1100 |
This function registers an I/O monitor @var{io_monitor} for the |
| 1101 |
context @var{ctx} with the hook value @var{hook_data}. |
| 1102 |
@end deftypefun |
| 1103 |
|
| 1104 |
|
| 1105 |
@c |
| 1106 |
@c C L I E N T C O D E |
| 1107 |
@c |
| 1108 |
@node Client code |
| 1109 |
@chapter How to develop an Assuan client |
| 1110 |
|
| 1111 |
Depending on the type of the server you want to connect you need to use |
| 1112 |
different functions. |
| 1113 |
|
| 1114 |
If the peer is not a simple pipe server but one using full-duplex |
| 1115 |
sockets, the full-fledged variant of the above function should be |
| 1116 |
used: |
| 1117 |
|
| 1118 |
@deftypefun gpg_error_t assuan_pipe_connect (@w{assuan_context_t @var{ctx}},@w{const char *@var{name}}, @w{const char *@var{argv}[]}, @w{assuan_fd_t *@var{fd_child_list}}, @w{void (*@var{atfork}) (void *, int)}, @w{void *@var{atforkvalue}}, @w{unsigned int @var{flags}}) |
| 1119 |
|
| 1120 |
A call to this functions forks the current process and executes the |
| 1121 |
program @var{name}, passing the arguments given in the NULL-terminated |
| 1122 |
list @var{argv}. A list of file descriptors not to be closed may be |
| 1123 |
given using the @code{ASSUAN_INVALID_FD} terminated array @var{fd_child_list}. |
| 1124 |
|
| 1125 |
If @var{name} is a null pointer, only a fork but no exec is done. Thus |
| 1126 |
the child continues to run. However all file descriptors are closed and |
| 1127 |
some special environment variables are set. To let the caller detect |
| 1128 |
whether the child or the parent continues, the parent returns with |
| 1129 |
@code{"client"} returned in @var{argv} and the child returns with |
| 1130 |
@code{"server"} in @var{argv}. This feature is only available on POSIX |
| 1131 |
platforms. |
| 1132 |
|
| 1133 |
If @var{atfork} is not NULL, this function is called in the child right |
| 1134 |
after the fork and the value @var{atforkvalue} is passed as the first |
| 1135 |
argument. That function should only act if the second argument it |
| 1136 |
received is @code{0}. Such a fork callback is useful to release |
| 1137 |
additional resources not to be used by the child. |
| 1138 |
|
| 1139 |
@noindent |
| 1140 |
@var{flags} is a bit vector and controls how the function acts: |
| 1141 |
|
| 1142 |
@table @code |
| 1143 |
@item ASSUAN_PIPE_CONNECT_FDPASSING |
| 1144 |
If cleared a simple pipe based server is expected. If set a server |
| 1145 |
based on full-duplex pipes is expected. Such pipes are usually |
| 1146 |
created using the @code{socketpair} function. It also enables |
| 1147 |
features only available with such servers. |
| 1148 |
|
| 1149 |
@item ASSUAN_PIPE_CONNECT_DETACHED |
| 1150 |
If set and there is a need to start the server it will be started as a |
| 1151 |
background process. This flag is useful under W32 systems, so that no |
| 1152 |
new console is created and pops up a console window when starting the |
| 1153 |
server. On W32CE systems this flag is ignored. |
| 1154 |
@end table |
| 1155 |
@end deftypefun |
| 1156 |
|
| 1157 |
|
| 1158 |
If you are using a long running server listening either on a TCP or a |
| 1159 |
Unix domain socket, the following function is used to connect to the server: |
| 1160 |
|
| 1161 |
@deftypefun gpg_error_t assuan_socket_connect (@w{assuan_context_t @var{ctx}}, @w{const char *@var{name}}, @w{pid_t @var{server_pid}}, @w{unsigned int @var{flags}}) |
| 1162 |
|
| 1163 |
Make a connection to the Unix domain socket @var{name} using the |
| 1164 |
already-initialized Assuan context at @var{ctx}. @var{server_pid} is |
| 1165 |
currently not used but may become handy in the future; if you don't |
| 1166 |
know the server's process ID (PID), pass @code{ASSUAN_INVALID_PID}. |
| 1167 |
With @var{flags} set to @code{ASSUAN_SOCKET_CONNECT_FDPASSING}, |
| 1168 |
@code{sendmsg} and @code{recvmesg} are used for input and output and |
| 1169 |
thereby enable the use of descriptor passing. |
| 1170 |
|
| 1171 |
Connecting to a TCP server is not yet implemented. Standard URL |
| 1172 |
schemes are reserved for @var{name} specifying a TCP server. |
| 1173 |
@end deftypefun |
| 1174 |
|
| 1175 |
Now that we have a connection to the server, all work may be |
| 1176 |
conveniently done using a couple of callbacks and the transact |
| 1177 |
function: |
| 1178 |
|
| 1179 |
@deftypefun gpg_error_t assuan_transact (@w{assuan_context_t @var{ctx}}, @w{const char *@var{command}}, @w{gpg_error_t (*@var{data_cb})(void *, const void *, size_t)}, @w{void *@var{data_cb_arg}}, @w{gpg_error_t (*@var{inquire_cb})(void*, const char *)}, @w{void *@var{inquire_cb_arg}}, @w{gpg_error_t (*@var{status_cb})(void*, const char *)}, @w{void *@var{status_cb_arg}}) |
| 1180 |
|
| 1181 |
Here @var{ctx} is the Assuan context opened by one of the connect |
| 1182 |
calls. @var{command} is the actual Assuan command string. It |
| 1183 |
shall not end with a line feed and its length is limited to |
| 1184 |
@code{ASSUAN_LINELENGTH} (~1000 bytes) |
| 1185 |
|
| 1186 |
@var{data_cb} is called by Libassuan for data lines; @var{data_cb_arg} |
| 1187 |
is passed to it along with the data and the length. [FIXME: needs |
| 1188 |
more documentation]. |
| 1189 |
|
| 1190 |
@var{inquire_cb} is called by Libassuan when the server requests |
| 1191 |
additional information from the client while processing the command. |
| 1192 |
This callback shall check the provided inquiry name and send the data |
| 1193 |
as requested back using the @code{assuan_send_data}. The server |
| 1194 |
passed @var{inquiry_cb_arg} along with the inquiry name to the |
| 1195 |
callback. |
| 1196 |
|
| 1197 |
@var{status_cb} is called by Libassuan for each status line it receives |
| 1198 |
from the server. @var{status_cb_arg} is passed along with the status |
| 1199 |
line to the callback. |
| 1200 |
|
| 1201 |
The function returns @code{0} success or an error value. The error value |
| 1202 |
may be the one one returned by the server in error lines or one |
| 1203 |
generated by the callback functions. |
| 1204 |
@end deftypefun |
| 1205 |
|
| 1206 |
Libassuan supports descriptor passing on some platforms. The next two |
| 1207 |
functions are used with this feature: |
| 1208 |
|
| 1209 |
@anchor{function assuan_sendfd} |
| 1210 |
@deftypefun gpg_error_t assuan_sendfd (@w{assuan_context_t @var{ctx}}, @w{assuan_fd_t @var{fd}}) |
| 1211 |
|
| 1212 |
Send the descriptor @var{fd} to the peer using the context @var{ctx}. |
| 1213 |
The descriptor must be sent before the command is issued that makes |
| 1214 |
use of the descriptor. |
| 1215 |
|
| 1216 |
Note that calling this function with a @var{ctx} of @code{NULL} and |
| 1217 |
@var{fd} of @code{ASSUAN_INVALID_FD} can be used as a runtime test to |
| 1218 |
check whether descriptor passing is available on the platform: |
| 1219 |
@code{0} is returned if descriptor passing is available, otherwise an |
| 1220 |
error with the error code @code{GPG_ERR_NOT_IMPLEMENTED} is returned. |
| 1221 |
@end deftypefun |
| 1222 |
|
| 1223 |
@anchor{function assuan_receivefd} |
| 1224 |
@deftypefun gpg_error_t assuan_receivefd (@w{assuan_context_t @var{ctx}}, @w{assuan_fd_t *@var{fd}}) |
| 1225 |
|
| 1226 |
Receive a descriptor pending for the context @var{ctx} from the peer. |
| 1227 |
The descriptor must be pending before this function is called. To |
| 1228 |
accomplish this, the peer needs to use @code{assuan_sendfd} before the |
| 1229 |
trigger is sent (e.g., using @code{assuan_write_line ("INPUT FD")}. |
| 1230 |
@end deftypefun |
| 1231 |
|
| 1232 |
|
| 1233 |
@c |
| 1234 |
@c S E R V E R C O D E |
| 1235 |
@c |
| 1236 |
@node Server code |
| 1237 |
@chapter How to develop an Assuan server |
| 1238 |
|
| 1239 |
Implementing a server for Assuan is a bit more complex than a client. |
| 1240 |
However, it is a straightforward task we are going to explain using a |
| 1241 |
commented example. |
| 1242 |
|
| 1243 |
@noindent |
| 1244 |
The list of the implemented server commands is defined by a table like: |
| 1245 |
|
| 1246 |
@smallexample |
| 1247 |
static struct @{ |
| 1248 |
const char *name; |
| 1249 |
int (*handler) (assuan_context_t, char *line); |
| 1250 |
@} command_table[] = @{ |
| 1251 |
@{ "FOO", cmd_foo @}, |
| 1252 |
@{ "BAR", cmd_bar @}, |
| 1253 |
@{ "INPUT", NULL @}, |
| 1254 |
@{ "OUTPUT", NULL @}, |
| 1255 |
@{ NULL @}@}; |
| 1256 |
@end smallexample |
| 1257 |
|
| 1258 |
For convenience this table is usually put after the actual command |
| 1259 |
handlers (@code{cmd_foo}, @code{cmd_bar}) or even put inside |
| 1260 |
@code{command_handler} (see below). Note that the commands |
| 1261 |
@code{INPUT} and @code{OUTPUT} do not require a handler because |
| 1262 |
Libassuan provides a default handler for them. It is however possible |
| 1263 |
to assign a custom handler. |
| 1264 |
|
| 1265 |
A prerequisite for this example code is that a client has already |
| 1266 |
connected to the server. Often there are two modes combined in one |
| 1267 |
program: A pipe-based server, where a client has forked the server |
| 1268 |
process, or a Unix domain socket based server that is listening on the |
| 1269 |
socket. |
| 1270 |
|
| 1271 |
@example |
| 1272 |
void |
| 1273 |
command_handler (int fd) |
| 1274 |
@{ |
| 1275 |
gpg_error_t rc; |
| 1276 |
int i; |
| 1277 |
assuan_context_t ctx; |
| 1278 |
|
| 1279 |
rc = assuan_new (&ctx); |
| 1280 |
if (rc) |
| 1281 |
@{ |
| 1282 |
fprintf (stderr, "server context creation failed: %s\n", |
| 1283 |
gpg_strerror(rc)); |
| 1284 |
return; |
| 1285 |
@} |
| 1286 |
|
| 1287 |
if (fd == -1) |
| 1288 |
@{ |
| 1289 |
assuan_fd_t filedes[2]; |
| 1290 |
|
| 1291 |
filedes[0] = assuan_fd_from_posix_fd (0); |
| 1292 |
filedes[1] = assuan_fd_from_posix_fd (1); |
| 1293 |
rc = assuan_init_pipe_server (ctx, filedes); |
| 1294 |
@} |
| 1295 |
else |
| 1296 |
rc = assuan_init_socket_server (ctx, fd, ASSUAN_SOCKET_SERVER_ACCEPTED); |
| 1297 |
if (rc) |
| 1298 |
@{ |
| 1299 |
fprintf (stderr, "server init failed: %s\n", gpg_strerror (rc)); |
| 1300 |
return; |
| 1301 |
@} |
| 1302 |
@end example |
| 1303 |
|
| 1304 |
@noindent |
| 1305 |
This is the first part of the command handler. We start off by |
| 1306 |
allocating a new Assuan context with @code{assuan_new}. |
| 1307 |
@xref{function assuan_new}. |
| 1308 |
|
| 1309 |
In case this is called as a pipe based server, @var{fd} will be based |
| 1310 |
as @var{fd} and the code assumes that the server's @code{stdin} and |
| 1311 |
@code{stdout} file handles are connected to a pipe. The |
| 1312 |
initialization is thus done using the function: |
| 1313 |
|
| 1314 |
@deftypefun gpg_error_t assuan_init_pipe_server (@w{assuan_context_t @var{ctx}}, @w{assuan_fd_t @var{filedes}[2]}) |
| 1315 |
|
| 1316 |
This function takes the two file descriptors from @var{filedes} and |
| 1317 |
returns a new Assuan context at @var{r_ctx}. As usual, a return value |
| 1318 |
of @code{0} indicates success and a failure is indicated by |
| 1319 |
returning an error value. In case of error, @code{NULL} will be stored |
| 1320 |
at @var{r_ctx}. |
| 1321 |
|
| 1322 |
In case the server has been called using a bi-directional pipe |
| 1323 |
(socketpair), @var{filedes} is ignored and the file descriptor is |
| 1324 |
taken from the environment variable @env{_assuan_connection_fd}. You |
| 1325 |
generally don't need to know this, because @code{assuan_pipe_connect}, |
| 1326 |
which is called by the client to connect to such a server, |
| 1327 |
automagically sets this variable. |
| 1328 |
@end deftypefun |
| 1329 |
|
| 1330 |
@deftypefun gpg_error_t assuan_init_socket_server (@w{assuan_context_t @var{ctx}}, @w{assuan_fd_t @var{fd}}, @w{unsigned int @var{flags}}) |
| 1331 |
|
| 1332 |
This function takes the file descriptor @var{fd}, which is expected to |
| 1333 |
be associated with a socket, and an Assuan context @var{ctx}. The |
| 1334 |
following bits are currently defined for @var{flags}: |
| 1335 |
|
| 1336 |
@table @code |
| 1337 |
@item ASSUAN_SOCKET_SERVER_FDPASSING |
| 1338 |
If set, @code{sendmsg} and @code{recvmesg} are used for input and |
| 1339 |
output, which enables the use of descriptor passing. |
| 1340 |
@item ASSUAN_SOCKET_SERVER_ACCEPTED |
| 1341 |
If set, @var{fd} refers to an already accepted socket. That is, |
| 1342 |
Libassuan won't call @var{accept} for it. It is suggested to set this |
| 1343 |
bit as it allows better control of the connection state. |
| 1344 |
@end table |
| 1345 |
|
| 1346 |
As usual, a return value of @code{0} indicates success and a failure |
| 1347 |
is indicated by returning an error value. |
| 1348 |
@end deftypefun |
| 1349 |
|
| 1350 |
@noindent |
| 1351 |
On the Windows platform the following function needs to be called after |
| 1352 |
@code{assuan_init_socket_server}: |
| 1353 |
|
| 1354 |
@deftypefun void assuan_set_sock_nonce ( @ |
| 1355 |
@w{assuan_context_t @var{ctx}}, @ |
| 1356 |
@w{assuan_sock_nonce_t *@var{nonce}}) |
| 1357 |
|
| 1358 |
Save a copy of @var{nonce} in context @var{ctx}. This should be used |
| 1359 |
to register the server's nonce with a context established by |
| 1360 |
@code{assuan_init_socket_server}. It is technically only needed for |
| 1361 |
Windows, but it does no harm to use it on other systems. |
| 1362 |
@end deftypefun |
| 1363 |
|
| 1364 |
|
| 1365 |
@noindent |
| 1366 |
After error checking, the implemented assuan commands are registered with |
| 1367 |
the server. |
| 1368 |
|
| 1369 |
@example |
| 1370 |
for (i = 0; command_table[i].name; i++) |
| 1371 |
@{ |
| 1372 |
rc = assuan_register_command (ctx, |
| 1373 |
command_table[i].name, |
| 1374 |
command_table[i].handler, NULL); |
| 1375 |
if (rc) |
| 1376 |
@{ |
| 1377 |
fprintf (stderr, "register failed: %s\n", gpg_strerror (rc)); |
| 1378 |
assuan_release (ctx); |
| 1379 |
return; |
| 1380 |
@} |
| 1381 |
@} |
| 1382 |
@end example |
| 1383 |
|
| 1384 |
@deftp {Data type} {gpg_error_t (*assuan_handler_t) (@w{assuan_context_t @var{ctx}}, @w{char *@var{line}})} |
| 1385 |
This is the function invoked by @sc{Assuan} for various command |
| 1386 |
related callback functions. Some of these callback functions have a |
| 1387 |
different type, but most use @code{assuan_handler_t}. |
| 1388 |
@end deftp |
| 1389 |
|
| 1390 |
@deftypefun gpg_error_t assuan_register_command (@w{assuan_context_t @var{ctx}}, @w{const char *@var{cmd_string}}, @w{assuan_handler_t @var{handler}}, @w{const char *@var{help_string}}) |
| 1391 |
|
| 1392 |
This registers the command named @var{cmd_string} with the Assuan |
| 1393 |
context @var{ctx}. @var{handler} is the function called by Libassuan |
| 1394 |
if this command is received from the client. @var{NULL} may be used |
| 1395 |
for @var{handler} to use a default handler (this only works with a few |
| 1396 |
pre-defined commands). Note that several default handlers have |
| 1397 |
already been registered when the context has been created: @code{NOP}, |
| 1398 |
@code{CANCEL}, @code{OPTION}, @code{BYE}, @code{AUTH}, @code{RESET} |
| 1399 |
and @code{END}. It is possible, but not recommended, to override |
| 1400 |
these commands. |
| 1401 |
|
| 1402 |
@var{help_string} is a help string that is used for automatic |
| 1403 |
documentation. It should contain a usage line followed by an empty |
| 1404 |
line and a complete description. |
| 1405 |
@end deftypefun |
| 1406 |
|
| 1407 |
@deftypefun gpg_error_t assuan_register_post_cmd_notify (@w{assuan_context_t @var{ctx}}, @w{void (*@var{fnc})(assuan_context_t)}, @w{gpg_error_t @var{err}}) |
| 1408 |
|
| 1409 |
Register a function to be called right after a command has been |
| 1410 |
processed. @var{err} is the result code from the last internal assuan |
| 1411 |
operation and not the one returned by the handler. It may be used for |
| 1412 |
command-related cleanup. |
| 1413 |
@end deftypefun |
| 1414 |
|
| 1415 |
@deftypefun gpg_error_t assuan_register_bye_notify (@w{assuan_context_t @var{ctx}}, @w{assuan_handler_t @var{handler}}) |
| 1416 |
|
| 1417 |
Register function @var{fnc} with context @var{ctx} to be called right |
| 1418 |
before the standard handler for the @code{BYE} command is being called. |
| 1419 |
@end deftypefun |
| 1420 |
|
| 1421 |
@deftypefun gpg_error_t assuan_register_reset_notify (@w{assuan_context_t @var{ctx}}, @w{assuan_handler_t @var{handler}}) |
| 1422 |
|
| 1423 |
Register function @var{fnc} with context @var{ctx} to be called right |
| 1424 |
before the standard handler for the @code{RESET} command is being called. |
| 1425 |
@end deftypefun |
| 1426 |
|
| 1427 |
@deftypefun gpg_error_t assuan_register_cancel_notify (@w{assuan_context_t @var{ctx}}, @w{assuan_handler_t @var{handler}}) |
| 1428 |
|
| 1429 |
Register function @var{fnc} with context @var{ctx} to be called right |
| 1430 |
before the standard handler for the @code{RESET} command is being called. |
| 1431 |
@end deftypefun |
| 1432 |
|
| 1433 |
@deftypefun gpg_error_t assuan_register_option_handler (@w{assuan_context_t @var{ctx}}, @w{gpg_error_t (*@var{fnc})(assuan_context_t, const char*, const char*)}) |
| 1434 |
|
| 1435 |
Register function @var{fnc} with context @var{ctx} for processing |
| 1436 |
options. That function is being called with the context, the name and |
| 1437 |
the value of the option. Leading and trailing spaces are removed from |
| 1438 |
the name and the value. The optional leading two dashes of the name |
| 1439 |
are removed as well. If no value has been given, an empty string is |
| 1440 |
passed. The function needs to return @code{0} on success or an error |
| 1441 |
code. |
| 1442 |
|
| 1443 |
@end deftypefun |
| 1444 |
|
| 1445 |
@deftypefun gpg_error_t assuan_register_input_notify (@w{assuan_context_t @var{ctx}}, @w{assuan_handler_t @var{handler}}) |
| 1446 |
|
| 1447 |
Although the input function may be overridden with a custom handler, it |
| 1448 |
is often more convenient to use the default handler and to know whether |
| 1449 |
an @code{INPUT} command has been seen and successfully parsed. The second |
| 1450 |
argument passed to that function is the entire line. Because that line |
| 1451 |
has already been parsed when the function gets called, a file descriptor |
| 1452 |
set with the @code{INPUT} command may already be used. That file |
| 1453 |
descriptor is available by calling @code{assuan_get_input_fd}. If the |
| 1454 |
notification function returns an error, the input fd does not change. |
| 1455 |
@end deftypefun |
| 1456 |
|
| 1457 |
@deftypefun gpg_error_t assuan_register_output_notify (@w{assuan_context_t @var{ctx}}, @w{assuan_handler_t @var{handler}}) |
| 1458 |
|
| 1459 |
Although the output function may be overridden with a custom handler, it |
| 1460 |
is often more convenient to use the default handler and to know whether |
| 1461 |
an @code{OUTPUT} command has been seen and successfully parsed. The second |
| 1462 |
argument passed to that function is the entire line. Because that line |
| 1463 |
has already been parsed when the function gets called, a file descriptor |
| 1464 |
set with the @code{OUTPUT} command may already be used. That file |
| 1465 |
descriptor is available by calling @code{assuan_get_output_fd}. If the |
| 1466 |
notification function returns an error, the output fd does not change. |
| 1467 |
@end deftypefun |
| 1468 |
|
| 1469 |
@deftypefun gpg_error_t assuan_set_hello_line (@w{assuan_context_t @var{ctx}}, @w{const char *@var{line}}) |
| 1470 |
|
| 1471 |
This is not actually a register function but may be called also after |
| 1472 |
registering commands. It changes the ``Hello'' line, sent by the |
| 1473 |
server to the client as a first response, from a default string to the |
| 1474 |
string @var{line}. For logging purposes, it is often useful to use |
| 1475 |
such a custom hello line which may tell version numbers and such. |
| 1476 |
Linefeeds are allowed in this string, however, each line needs to be |
| 1477 |
shorter than the Assuan line length limit. |
| 1478 |
@end deftypefun |
| 1479 |
|
| 1480 |
@noindent |
| 1481 |
Now that everything has been setup, we can start to process our |
| 1482 |
clients requests. |
| 1483 |
|
| 1484 |
@example |
| 1485 |
for (;;) |
| 1486 |
@{ |
| 1487 |
rc = assuan_accept (ctx); |
| 1488 |
if (rc == -1) |
| 1489 |
break; |
| 1490 |
else if (rc) |
| 1491 |
@{ |
| 1492 |
fprintf (stderr, "accept problem: %s\n", gpg_strerror (rc)); |
| 1493 |
break; |
| 1494 |
@} |
| 1495 |
|
| 1496 |
rc = assuan_process (ctx); |
| 1497 |
if (rc) |
| 1498 |
@{ |
| 1499 |
fprintf (stderr, "processing failed: %s\n", gpg_strerror (rc)); |
| 1500 |
continue; |
| 1501 |
@} |
| 1502 |
@} |
| 1503 |
assuan_release (ctx); |
| 1504 |
@} |
| 1505 |
@end example |
| 1506 |
|
| 1507 |
@noindent |
| 1508 |
For future extensibility and to properly detect the end of the |
| 1509 |
connection the core of the server should loop over the |
| 1510 |
accept and process calls. |
| 1511 |
|
| 1512 |
@deftypefun gpg_error_t assuan_accept (@w{assuan_context_t @var{ctx}}) |
| 1513 |
|
| 1514 |
A call to this function cancel any existing connection and waits for a |
| 1515 |
connection from a client (that might be skipped, depending on the type |
| 1516 |
of the server). The initial handshake is performed which may include an |
| 1517 |
initial authentication or encryption negotiation. On success @code{0} |
| 1518 |
is returned. An error value will be returned if the connection could for |
| 1519 |
some reason not be established. An error code of @code{GPG_ERR_EOF} indicates |
| 1520 |
the end of the connection. |
| 1521 |
@end deftypefun |
| 1522 |
|
| 1523 |
@deftypefun gpg_error_t assuan_process (@w{assuan_context_t @var{ctx}}) |
| 1524 |
|
| 1525 |
This function is used to handle the Assuan protocol after a connection |
| 1526 |
has been established using @code{assuan_accept}. It is the main |
| 1527 |
protocol handler responsible for reading the client commands and calling |
| 1528 |
the appropriate handlers. The function returns @code{0} on success or |
| 1529 |
an error value if something went seriously wrong. Error values from the |
| 1530 |
individual command handlers, i.e., operational error, are not seen here. |
| 1531 |
@end deftypefun |
| 1532 |
|
| 1533 |
@noindent |
| 1534 |
That is all needed for the server code. You only need to come up with |
| 1535 |
the code for the individual command handlers. Take care that the line |
| 1536 |
passed to the command handlers is allocated statically within the |
| 1537 |
context and calls to Assuan functions may modify that line. You are |
| 1538 |
also allowed to modify that line which makes parsing much easier. |
| 1539 |
|
| 1540 |
|
| 1541 |
@c |
| 1542 |
@c E x t e r n a l I / O L o o p s |
| 1543 |
@c |
| 1544 |
@node External I/O Loop |
| 1545 |
@chapter How to use external I/O event loops |
| 1546 |
|
| 1547 |
The above implementations of an Assuan client and server are |
| 1548 |
synchronous, insofar as the main routines block until a request or |
| 1549 |
client connection is completely processed. In some programs, for |
| 1550 |
example GUI applications, this is undesirable. Instead, Assuan |
| 1551 |
operations should be non-blocking, and the caller should be able to |
| 1552 |
poll all involved file descriptors to determine when the next Assuan |
| 1553 |
function can be invoked without blocking. |
| 1554 |
|
| 1555 |
To make this possible, client and server have to adhere to some rules: |
| 1556 |
@itemize @bullet |
| 1557 |
@item |
| 1558 |
Either partner should always write full lines. If partial lines are |
| 1559 |
written, the remainder of the line should be sent without delay. |
| 1560 |
@item |
| 1561 |
Either partner should eagerly receive status messages. While |
| 1562 |
receiving and sending bulk data may be delayed, the status |
| 1563 |
communication channel is different: Both partners may send status |
| 1564 |
messages in blocking mode at any time the protocol allows them to send |
| 1565 |
such status messages. To ensure that these send operations do not |
| 1566 |
actually block the sender, the recipient must be ready to receive |
| 1567 |
without undue delay. |
| 1568 |
@item |
| 1569 |
If descriptor passing is used over a socket, the descriptor must be |
| 1570 |
sent after the corresponding command without undue delay. |
| 1571 |
@end itemize |
| 1572 |
|
| 1573 |
Together, these restrictions allow to limit the need for asynchronous |
| 1574 |
I/O operations to bulk data and the inbound status file descriptor. |
| 1575 |
|
| 1576 |
In addition to the above rules, client and server should adhere to the |
| 1577 |
following implementation guidelines. |
| 1578 |
|
| 1579 |
@menu |
| 1580 |
* External I/O Loop Client:: External I/O event loops in the client. |
| 1581 |
* External I/O Loop Server:: External I/O event loops in the server. |
| 1582 |
@end menu |
| 1583 |
|
| 1584 |
@node External I/O Loop Client |
| 1585 |
@section External I/O event loops in the client. |
| 1586 |
|
| 1587 |
The reference implementation for using external I/O event loops in the |
| 1588 |
client is the GPGME library, which exports its own external I/O event |
| 1589 |
loop mechanism and utilizes the Assuan library transparently for the |
| 1590 |
user. The following steps document how GPGME achieves this. |
| 1591 |
|
| 1592 |
@enumerate |
| 1593 |
@item |
| 1594 |
Before connecting, set up pipes for bulk data transfer (using the |
| 1595 |
INPUT/OUTPUT commands, for example). These are passed to the server |
| 1596 |
either by inheritance (using a pipe server) or by FD passing (using a |
| 1597 |
socket server). |
| 1598 |
|
| 1599 |
@item |
| 1600 |
Then you need to connect to the server. GPGME uses a pipe server, so |
| 1601 |
it just spawns a server process, which is a non-blocking operation. |
| 1602 |
FIXME: Currently, using a client with external event loop over a |
| 1603 |
socket connection is not supported. It is easy to support (we just |
| 1604 |
need a variation of @code{assuan_socket_connect} which takes an |
| 1605 |
already connected socket FD and turns it into an Assuan context), so |
| 1606 |
if you need this let us know. |
| 1607 |
|
| 1608 |
@item |
| 1609 |
After connecting, get the inbound status FD with |
| 1610 |
@code{assuan_get_active_fds} (the first one returned is the status |
| 1611 |
FD). This FD can be duplicated if it is convenient (GPGME does this |
| 1612 |
to be able to close this FD and associated callback handlers without |
| 1613 |
disrupting Assuan's internals). |
| 1614 |
|
| 1615 |
@item |
| 1616 |
Then register the Assuan inbound status FD and all bulk data FDs with |
| 1617 |
the I/O event mechanism. In general, this requires setting up |
| 1618 |
callback handlers for these FDs and registering them with the main |
| 1619 |
event loop. |
| 1620 |
|
| 1621 |
@item |
| 1622 |
When bulk data FDs become ready, you can simply perform the |
| 1623 |
corresponding read or write operations. When the inbound status FD |
| 1624 |
becomes ready, you can receive the next server line with |
| 1625 |
assuan_read_line(). |
| 1626 |
|
| 1627 |
@item |
| 1628 |
You should close and unregister the bulk data FDs when you wrote all |
| 1629 |
data (for outbound FDs) or receive an EOF (for inbound FDs). When you |
| 1630 |
receive an ERR from the server, or an OK for the final operation, you |
| 1631 |
can unregister the inbound status FD and call @code{assuan_release}. |
| 1632 |
|
| 1633 |
@item |
| 1634 |
As noted above, all send operations on the outbound status FD are done |
| 1635 |
immediate with blocking. In GPGME, this has never caused any problems. |
| 1636 |
|
| 1637 |
@item |
| 1638 |
The @code{INQUIRE} function can be handled in two ways: If the |
| 1639 |
requested data is immediately available, the client can just send the |
| 1640 |
data blockingly. If the requested data needs to be fetched from a |
| 1641 |
blocking source, a callback handler can be registered for the FD with |
| 1642 |
the main event loop. GPGME does not support the @code{INQUIRE} |
| 1643 |
function, so we do not have any practical experience with this. |
| 1644 |
@end enumerate |
| 1645 |
|
| 1646 |
Currently, the client can not cancel a pending operation gracefully. |
| 1647 |
It can, however, disconnect from the server at any time. It is the |
| 1648 |
responsibility of the server to periodically send status messages to |
| 1649 |
the client to probe if the connection remains alive. |
| 1650 |
|
| 1651 |
|
| 1652 |
@node External I/O Loop Server |
| 1653 |
@section External I/O event loops in the server. |
| 1654 |
|
| 1655 |
Currently, no Assuan server exists which uses external I/O event |
| 1656 |
loops. However, the following guidelines should lead to a usable |
| 1657 |
implementation: |
| 1658 |
|
| 1659 |
@enumerate |
| 1660 |
@item |
| 1661 |
For socket servers: You can not use @code{assuan_accept}, so you |
| 1662 |
should just implement the bind/connect/listen/accept stage yourself. |
| 1663 |
You can register the listen FD with your main event loop, accept the |
| 1664 |
connection when it becomes ready, and finally call |
| 1665 |
@code{assuan_init_socket_server} with the final argument being |
| 1666 |
@code{ASSUAN_SOCKET_SERVER_ACCEPTED} to create an Assuan context for this |
| 1667 |
connection. This way you can also handle multiple connections in |
| 1668 |
parallel. The reference implementation for this approach is DirMngr. |
| 1669 |
|
| 1670 |
For pipe servers: @code{assuan_init_pipe_server} creates an Assuan |
| 1671 |
context valid for the pipe FDs. |
| 1672 |
|
| 1673 |
@item |
| 1674 |
Once you have a context for a single connection, you can get the |
| 1675 |
inbound status FD with @code{assuan_get_active_fds} (the first one |
| 1676 |
returned is the status FD). This FD can be duplicated if it is |
| 1677 |
convenient. Every time the inbound status FD is readable, you should |
| 1678 |
invoke the function @code{assuan_process_next} (see below) to process |
| 1679 |
the next incoming message. @code{assuan_process_next} processes as |
| 1680 |
many status lines as can be received by a single @code{read} |
| 1681 |
operation. When it returns, the inbound status FD may still be |
| 1682 |
readable, but Assuan does not check this. |
| 1683 |
|
| 1684 |
The function @code{assuan_process_next} returns 0 if it can not make |
| 1685 |
progress reliably, and it returns true in @code{done} if the client |
| 1686 |
closed the connection. See below for more information on this |
| 1687 |
function. |
| 1688 |
|
| 1689 |
@item |
| 1690 |
The command will be dispatched by @code{assuan_process_next} just as |
| 1691 |
with @code{assuan_process}, however, you will want to implement the |
| 1692 |
command handlers in such a way that they do not block. For example, |
| 1693 |
the command handler may just register the bulk data FDs with the main |
| 1694 |
event loop and return. |
| 1695 |
|
| 1696 |
When the command is finished, irregardless if this happens directly in |
| 1697 |
the command handler or later, you must call @code{assuan_process_done} |
| 1698 |
with an appropriate error value (or 0 for success) to return an |
| 1699 |
appropriate status line to the client. You can do this at the end of |
| 1700 |
the command handler, for example by ending it with @code{return |
| 1701 |
assuan_process_done (error_code);}. Another possibility is to invoke |
| 1702 |
@code{assuan_process_done} from the place in the code which closes the |
| 1703 |
last active bulk FD registered with the main event loop for this |
| 1704 |
operation. |
| 1705 |
@end enumerate |
| 1706 |
|
| 1707 |
It is not possible to use @code{assuan_inquire} in a command handler, |
| 1708 |
as this function blocks on receiving the inquired data from the |
| 1709 |
client. Instead, the asynchronous version @code{assuan_inquire_ext} |
| 1710 |
needs to be used (see below), which invokes a callback when the client |
| 1711 |
provided the inquired data. A typical usage would be for the command |
| 1712 |
handler to register a continuation with @code{assuan_inquire_ext} and |
| 1713 |
return 0. Eventually, the continuation would be invoked by |
| 1714 |
@code{assuan_process_next} when the client data arrived. The |
| 1715 |
continuation could complete the command and eventually call |
| 1716 |
@code{assuan_process_done}. |
| 1717 |
|
| 1718 |
Cancellation is supported by returning an appropriate error value to |
| 1719 |
the client with @code{assuan_process_done}. For long running |
| 1720 |
operations, the server should send progress status messages to the |
| 1721 |
client in regular intervals to notice when the client disconnects. |
| 1722 |
|
| 1723 |
@deftypefun gpg_error_t assuan_process_next (@w{assuan_context_t @var{ctx}}, @w{int *@var{done}}) |
| 1724 |
This is the same as @code{assuan_process} but the caller has to |
| 1725 |
provide the outer loop. He should loop as long as the return code is |
| 1726 |
zero and @var{done} is false. |
| 1727 |
@end deftypefun |
| 1728 |
|
| 1729 |
@deftypefun gpg_error_t assuan_process_done (@w{assuan_context_t @var{ctx}}, @w{gpg_error_t @var{rc}}) |
| 1730 |
Finish a pending command and return the error code @var{rc} to the |
| 1731 |
client. |
| 1732 |
@end deftypefun |
| 1733 |
|
| 1734 |
@deftypefun gpg_error_t assuan_inquire_ext (@w{assuan_context_t @var{ctx}}, @w{const char *@var{keyword}}, @w{size_t @var{maxlen}}, @w{gpg_error_t (*@var{cb}) (void *cb_data, gpg_error_t rc, unsigned char *buffer, size_t buffer_len)}, @w{void *@var{cb_data}}) |
| 1735 |
This is similar to @code{assuan_inquire} but the caller has to provide |
| 1736 |
the outer loop (using @code{assuan_process_next}). The caller should |
| 1737 |
specify a continuation with @var{cb}, which receives @var{cb_data} as |
| 1738 |
its first argument, and the error value as well as the inquired data as |
| 1739 |
its remaining arguments. |
| 1740 |
@end deftypefun |
| 1741 |
|
| 1742 |
|
| 1743 |
|
| 1744 |
@c |
| 1745 |
@c U T I L I T I E S |
| 1746 |
@c |
| 1747 |
@node Utilities |
| 1748 |
@chapter Utility functions |
| 1749 |
|
| 1750 |
@noindent |
| 1751 |
There are a lot of helper functions to make writing Assuan code easier. |
| 1752 |
Some of these functions provide information not available with the |
| 1753 |
general functions. |
| 1754 |
|
| 1755 |
|
| 1756 |
@deftypefun gpg_error_t assuan_write_status (@w{assuan_context_t @var{ctx}}, @w{const char *@var{keyword}}, @w{const char *@var{text}}) |
| 1757 |
|
| 1758 |
This is a convenience function for a server to send a status line. You |
| 1759 |
need to pass it the @var{keyword} and the content of the status line in |
| 1760 |
@var{text}. |
| 1761 |
@end deftypefun |
| 1762 |
|
| 1763 |
|
| 1764 |
@deftypefun gpg_error_t assuan_inquire (@w{assuan_context_t @var{ctx}}, @w{const char *@var{keyword}}, @w{unsigned char **@var{r_buffer}}, @w{size_t *@var{r_length}}, @w{size_t @var{maxlen}}) |
| 1765 |
|
| 1766 |
A server may use this function to request specific data from a client. |
| 1767 |
This function sends an 'INQUIRE' command back to the client and |
| 1768 |
returns the client's response in a newly allocated buffer. You need |
| 1769 |
to pass at least the server's context (@var{ctx}) and a description of |
| 1770 |
the required data (@var{keyword}). All other parameters may be |
| 1771 |
@code{NULL} or @code{0}, but this is rarely useful. |
| 1772 |
|
| 1773 |
On success the result is stored in a newly allocated buffer stored at |
| 1774 |
@var{r_buffer}. The length of the data is stored at @var{r_length}. |
| 1775 |
If @var{maxlen} has not been given as @code{0}, it specifies an upper |
| 1776 |
size limit of the expected data. If the client returns too much |
| 1777 |
data the function fails and an error with the error code |
| 1778 |
@code{GPG_ERR_ASS_TOO_MUCH_DATA} will be returned. |
| 1779 |
@end deftypefun |
| 1780 |
|
| 1781 |
|
| 1782 |
@deftypefun FILE* assuan_get_data_fp (@w{assuan_context_t @var{ctx}}) |
| 1783 |
|
| 1784 |
Return a stdio stream for the Assuan context @var{ctx}. This stream may |
| 1785 |
then be used for data output (assuan_write_data). The stream is valid |
| 1786 |
until the end of the current handler. Calling @code{fclose} for that stream is |
| 1787 |
not required. Assuan does all the buffering needed to insert the status |
| 1788 |
line as well as the required line wrapping and quoting for data lines. |
| 1789 |
|
| 1790 |
This function is only available on systems supporting either |
| 1791 |
@code{funopen} or @code{fopencookie}. If it is not supported @code{NULL} |
| 1792 |
is returned and @code{errno} is set to @code{ENOSYS}. |
| 1793 |
@end deftypefun |
| 1794 |
|
| 1795 |
|
| 1796 |
@deftypefun gpg_error_t assuan_set_okay_line (@w{assuan_context_t @var{ctx}}, @w{const char *@var{line}}) |
| 1797 |
|
| 1798 |
Set the text used for the next @code{OK} response to @var{line}. This is |
| 1799 |
sometimes useful to send additional human readable information along |
| 1800 |
with the OK line. The string is automatically reset at the end of the |
| 1801 |
current handler. |
| 1802 |
@end deftypefun |
| 1803 |
|
| 1804 |
|
| 1805 |
@deftypefun gpg_error_t assuan_command_parse_fd (@w{assuan_context_t @var{ctx}}, @w{char *@var{line}}, @w{assuan_fd_t *@var{rfd}}) |
| 1806 |
|
| 1807 |
This is the core of the default @code{INPUT} and @code{OUTPUT} |
| 1808 |
handler. It may be used in custom commands as well to negotiate a |
| 1809 |
file descriptor. If @var{line} contains @code{FD=@var{n}}, it returns |
| 1810 |
@var{n} in @var{rfd} assuming a local file descriptor. If @var{line} |
| 1811 |
contains just @code{FD} it returns a file descriptor at @var{rfd}; |
| 1812 |
this file descriptor needs to have been sent by the client right |
| 1813 |
before using @code{assuan_sendfd}. |
| 1814 |
|
| 1815 |
On W32 systems the returned file descriptor is a system handle and not a |
| 1816 |
libc low level I/O file descriptor. Thus applications need to use |
| 1817 |
@code{_open_osfhandle} before they can pass this descriptor to standard |
| 1818 |
functions like @code{fdopen} or @code{dup}. |
| 1819 |
|
| 1820 |
@end deftypefun |
| 1821 |
|
| 1822 |
@deftypefun @w{const char *} assuan_get_command_name (@w{assuan_context_t @var{ctx}}) |
| 1823 |
|
| 1824 |
Return the name of the command currently processed by a handler. |
| 1825 |
The returned string is valid until the next call to an Assuan |
| 1826 |
function on the same context. Returns @code{NULL} if no handler is |
| 1827 |
executed or the command is not known. |
| 1828 |
@end deftypefun |
| 1829 |
|
| 1830 |
|
| 1831 |
@deftypefun assuan_fd_t assuan_get_input_fd (@w{assuan_context_t @var{ctx}}) |
| 1832 |
|
| 1833 |
Return the file descriptor sent by the client using the last @code{INPUT} |
| 1834 |
command. Returns @code{ASSUAN_INVALID_FD} if no file descriptor is available. |
| 1835 |
@end deftypefun |
| 1836 |
|
| 1837 |
@deftypefun assuan_fd_t assuan_get_output_fd (@w{assuan_context_t @var{ctx}}) |
| 1838 |
|
| 1839 |
Return the file descriptor sent by the client using the last |
| 1840 |
@code{OUTPUT} command. Returns @code{ASSUAN_INVALID_FD} if no file descriptor is |
| 1841 |
available. |
| 1842 |
@end deftypefun |
| 1843 |
|
| 1844 |
@deftypefun gpg_error_t assuan_close_input_fd (@w{assuan_context_t @var{ctx}}) |
| 1845 |
|
| 1846 |
Close the file descriptor set by the last @code{INPUT} command. This |
| 1847 |
function has the advantage over a simple @code{close} that it can do |
| 1848 |
some sanity checks and make sure that a following |
| 1849 |
@code{assuan_get_input_fd} won't return an already closed descriptor. |
| 1850 |
@end deftypefun |
| 1851 |
|
| 1852 |
@deftypefun gpg_error_t assuan_close_output_fd (@w{assuan_context_t @var{ctx}}) |
| 1853 |
|
| 1854 |
Close the file descriptor set by the last @code{OUTPUT} command. This |
| 1855 |
function has the advantage over a simple @code{close} that it can do |
| 1856 |
some sanity checks and make sure that a following |
| 1857 |
@code{assuan_get_input_fd} won't return an already closed descriptor. |
| 1858 |
@end deftypefun |
| 1859 |
|
| 1860 |
@deftypefun gpg_error_t assuan_set_error (@w{assuan_context_t @var{ctx}}, @w{gpg_error_t @var{err}}, @w{const char *@var{text}}) |
| 1861 |
This is a helper to provide a more descriptive error text with @code{ERR} |
| 1862 |
lines. For this to work, the text needs to be stored in the context |
| 1863 |
@var{ctx} while still being in the command handler. This function is |
| 1864 |
commonly called this way |
| 1865 |
@smallexample |
| 1866 |
return assuan_set_error (ctx, err, "commands needs 5 arguments"); |
| 1867 |
@end smallexample |
| 1868 |
The value @var{err} is passed through and thus the return value of the |
| 1869 |
command handler in the example. The provided text further explains |
| 1870 |
that error to humans. |
| 1871 |
@end deftypefun |
| 1872 |
|
| 1873 |
|
| 1874 |
|
| 1875 |
@deftypefun pid_t assuan_get_pid (@w{assuan_context_t @var{ctx}}) |
| 1876 |
|
| 1877 |
This function returns the pid of the connected connected peer. If |
| 1878 |
that pid is not known @code{ASSUAN_INVALID_PID} is returned. Note |
| 1879 |
that it is not always possible to learn the pid of the other |
| 1880 |
process. For a pipe based server the client knows it instantly and a |
| 1881 |
mechanism is in place to let the server learn it. For socket based |
| 1882 |
servers the pid is only available on systems providing the |
| 1883 |
@code{SO_PEERCRED} socket option @footnote{to our knowledge only the |
| 1884 |
Linux kernel has this feature}. |
| 1885 |
@end deftypefun |
| 1886 |
|
| 1887 |
|
| 1888 |
@deftp {Data type} {assuan_peercred_t} |
| 1889 |
This structure is used to store the peer credentials. The available |
| 1890 |
members depend on the operating system. |
| 1891 |
|
| 1892 |
@table @code |
| 1893 |
@item pid_t pid |
| 1894 |
The process ID of the peer. |
| 1895 |
|
| 1896 |
@item uid_t uid |
| 1897 |
The user ID of the peer process. |
| 1898 |
|
| 1899 |
@item gid_t gid |
| 1900 |
The group ID of the peer process. |
| 1901 |
@end table |
| 1902 |
@end deftp |
| 1903 |
|
| 1904 |
|
| 1905 |
@deftypefun gpg_error_t assuan_get_peercred (@w{assuan_context_t @var{ctx}}, @w{assuan_peercred_t *@var{peercred}}) |
| 1906 |
Return user credentials of the peer. This will work only on certain |
| 1907 |
systems and only when connected over a socket. On success, a pointer |
| 1908 |
to the peer credentials is stored in @var{peercred}. The information |
| 1909 |
is only valid as long as the state of the connection is unchanged (at |
| 1910 |
least until the next assuan call to the same context). |
| 1911 |
|
| 1912 |
As of now only the server is able to retrieve this information. Note, |
| 1913 |
that for getting the pid of the peer @code{assuan_get_pid} is usually |
| 1914 |
better suited. |
| 1915 |
@end deftypefun |
| 1916 |
|
| 1917 |
|
| 1918 |
@deftypefun int assuan_get_active_fds (@w{assuan_context_t @var{ctx}}, @w{int @var{what}}, @w{assuan_fd_t *@var{fdarray}}, @w{int @var{fdarraysize}}) |
| 1919 |
|
| 1920 |
Return all active file descriptors for the context @var{ctx}. This |
| 1921 |
function can be used to select on the file descriptors and to call |
| 1922 |
@code{assuan_process_next} if there is an active one. The first |
| 1923 |
descriptor in the array is the one used for the command connection. |
| 1924 |
Currently @var{what} needs to be @code{0} to return descriptors used for |
| 1925 |
reading, @code{1} will eventually be used to return descriptors used for |
| 1926 |
writing. @var{fdarray} is an array of integers provided by the caller; |
| 1927 |
@var{fdarraysize} gives the size of that array. |
| 1928 |
|
| 1929 |
On success the number of active descriptors are returned. These active |
| 1930 |
descriptors are then stored in @var{fdarray}. On error @code{-1} is |
| 1931 |
returned; the most likely reason for this is a too small @var{fdarray}. |
| 1932 |
|
| 1933 |
Note that on W32 systems the returned file descriptor is a system handle |
| 1934 |
and not a libc low level I/O file descriptor. |
| 1935 |
@end deftypefun |
| 1936 |
|
| 1937 |
|
| 1938 |
@deftypefun int assuan_pending_line (@w{assuan_context_t @var{ctx}}) |
| 1939 |
A call to this function return true if a full line has been buffered and |
| 1940 |
thus an entire assuan line may be read without triggering any actual |
| 1941 |
I/O. |
| 1942 |
@end deftypefun |
| 1943 |
|
| 1944 |
|
| 1945 |
|
| 1946 |
@c |
| 1947 |
@c S O C K E T W R A P P E R S |
| 1948 |
@c |
| 1949 |
@node Socket wrappers |
| 1950 |
@chapter Socket wrapper functions |
| 1951 |
|
| 1952 |
@noindent |
| 1953 |
It is sometimes useful to support Unix domain sockets on Windows. To do |
| 1954 |
this in a portable way, Assuan provides a set of wrapper functions which |
| 1955 |
may be used on any system but will enhance Windows to support these |
| 1956 |
socket types. The actual implementation is based on local TCP sockets |
| 1957 |
and fully transparent for the client. Server code needs to utilize two |
| 1958 |
extra functions to check the permissions. |
| 1959 |
|
| 1960 |
@deftypefun gpg_error_t assuan_sock_init (void) |
| 1961 |
Initialize the socket wrappers. Must be called once at startup if any |
| 1962 |
of the socket wrapper functions are used. |
| 1963 |
@end deftypefun |
| 1964 |
|
| 1965 |
@deftypefun gpg_error_t assuan_sock_deinit (void) |
| 1966 |
Deinitialize the socket wrappers. |
| 1967 |
@end deftypefun |
| 1968 |
|
| 1969 |
@deftypefun int assuan_sock_close (@w{assuan_fd_t @var{fd}}) |
| 1970 |
Wrapper for close which does a closesocket on Windows if needed. |
| 1971 |
@end deftypefun |
| 1972 |
|
| 1973 |
@deftypefun assuan_fd_t assuan_sock_new (@w{int @var{domain}}, @w{int @var{type}}, @w{int @var{proto}}); |
| 1974 |
Wrapper around socket. |
| 1975 |
@end deftypefun |
| 1976 |
|
| 1977 |
@deftypefun assuan_fd_t assuan_sock_accept (@w{assuan_fd_t @var{sockfd}}, @ |
| 1978 |
@w{struct sockaddr *@var{addr}}, @ |
| 1979 |
@w{socklen_t *@var{p_addrlen}}) |
| 1980 |
|
| 1981 |
Wrapper around accept. |
| 1982 |
@end deftypefun |
| 1983 |
|
| 1984 |
|
| 1985 |
@deftypefun int assuan_sock_connect (@w{assuan_fd_t @var{sockfd}}, @ |
| 1986 |
@w{struct sockaddr *@var{addr}}, @ |
| 1987 |
@w{int @var{addrlen}}) |
| 1988 |
|
| 1989 |
Wrapper around connect. For Unix domain sockets under Windows this |
| 1990 |
function also does a write immediately after the the connect to send the |
| 1991 |
nonce as read from the socket's file. Under Unix this function check |
| 1992 |
whether the socket file is a redirection file and connects to the |
| 1993 |
redirected socket instead; see @code{assuan_sock_set_sockaddr_un} for |
| 1994 |
details on the redirection file format. |
| 1995 |
@end deftypefun |
| 1996 |
|
| 1997 |
|
| 1998 |
@deftypefun int assuan_sock_connect_byname (@w{const char * @var{host}}, @ |
| 1999 |
@w{unsigned short @var{port}}, @ |
| 2000 |
@w{int @var{timeout}}, @ |
| 2001 |
@w{const char *@var{credentials}}, @ |
| 2002 |
@w{unsigned int @var{flags}}) |
| 2003 |
|
| 2004 |
Directly connect to @var{port} on @var{host} given as a name. The |
| 2005 |
current implementation requires that @var{flags} has either |
| 2006 |
@code{ASSUAN_SOCK_SOCKS} or @code{ASSUAN_SOCK_TOR} set. On success a |
| 2007 |
new TCP STREAM socket is returned; on error @code{ASSUAN_INVALID_FD} |
| 2008 |
and ERRNO set. If @var{credentials} is not @code{NULL}, it is a |
| 2009 |
string used for password based SOCKS authentication. Username and |
| 2010 |
password are separated by a colon. @var{timeout} specifies timeout |
| 2011 |
value (in miliseconds) for the connection. @var{timeout} with zero |
| 2012 |
means no timeout (for client side, the SOCKS server may timeout). |
| 2013 |
@var{timeout} with -1 means no wait. To test whether the proxy is |
| 2014 |
available @var{host} and @var{port} may be given as NULL/0: If the |
| 2015 |
proxy is available the function returns a valid socket which is in the |
| 2016 |
state after credentials sub-negotiation. The caller now knows that |
| 2017 |
the SOCKS proxy is available and has been authenticated; normally the |
| 2018 |
caller closes the socket then. |
| 2019 |
@end deftypefun |
| 2020 |
|
| 2021 |
|
| 2022 |
@deftypefun int assuan_sock_bind ( @ |
| 2023 |
@w{assuan_fd_t @var{sockfd}}, @ |
| 2024 |
@w{struct sockaddr *@var{addr}}, @ |
| 2025 |
@w{int @var{addrlen}}) |
| 2026 |
|
| 2027 |
Wrapper around bind. Under Windows this creates a file and writes the |
| 2028 |
port number and a random nonce to this file. |
| 2029 |
@end deftypefun |
| 2030 |
|
| 2031 |
@deftypefun int assuan_sock_set_sockaddr_un ( @ |
| 2032 |
@w{const char *@var{fname}}, @ |
| 2033 |
@w{struct sockaddr *@var{addr}}, @ |
| 2034 |
@w{int *@var{r_redirected}}) |
| 2035 |
|
| 2036 |
This is a helper function to initialize the Unix socket domain address |
| 2037 |
structure @var{addr} and store the file name @var{fname} there. If |
| 2038 |
@var{r_redirected} is not NULL the function checks whether @var{fname} |
| 2039 |
already exists, is a regular file, and not a socket. In that case |
| 2040 |
@var{fname} is read to see whether this is a redirection to a socket |
| 2041 |
file. If that is the case 1 is stored at @var{r_redirected}. If the |
| 2042 |
file does not look like a redirection file 0 will be stored there and |
| 2043 |
@var{fname} will be used in the regular way. |
| 2044 |
|
| 2045 |
The format of a redirection file is |
| 2046 |
|
| 2047 |
@example |
| 2048 |
%Assuan% |
| 2049 |
socket=@var{name} |
| 2050 |
@end example |
| 2051 |
|
| 2052 |
With @var{name} being is the actual socket to use. No white spaces |
| 2053 |
are allowed, both lines must be terminated by a single linefeed, and |
| 2054 |
extra lines are not allowed. Environment variables are interpreted in |
| 2055 |
@var{name} if given in @code{$@{VAR@}} notation. No escape characters |
| 2056 |
are defined; if the string @code{$@{} shall be used in file name, an |
| 2057 |
environment variable with that content may be used. The length of the |
| 2058 |
redirection file is limited to 511 bytes which is more than sufficient |
| 2059 |
for any known implementation of Unix domain sockets. |
| 2060 |
@end deftypefun |
| 2061 |
|
| 2062 |
|
| 2063 |
@deftypefun int assuan_sock_get_nonce ( @ |
| 2064 |
@w{struct sockaddr *@var{addr}}, @ |
| 2065 |
@w{int @var{addrlen}}, @ |
| 2066 |
@w{assuan_sock_nonce_t *@var{nonce}}) |
| 2067 |
|
| 2068 |
This is used by the server after a bind to return the random nonce. To |
| 2069 |
keep the code readable this may also be used on POSIX system. |
| 2070 |
@end deftypefun |
| 2071 |
|
| 2072 |
@deftypefun int assuan_sock_check_nonce ( @ |
| 2073 |
@w{assuan_fd_t @var{fd}}, @ |
| 2074 |
@w{assuan_sock_nonce_t *@var{nonce}}) |
| 2075 |
|
| 2076 |
If the option @code{ASSUAN_SOCKET_SERVER_ACCEPTED} has been used, |
| 2077 |
Libassuan has no way to check the nonce of the server. Thus an explicit |
| 2078 |
check of the saved nonce using this function is required. If this |
| 2079 |
function fails the server should immediately drop the connection. This |
| 2080 |
function may not be used if Libassuan does the accept call itself |
| 2081 |
(i.e., @code{ASSUAN_SOCKET_SERVER_ACCEPTED} has not been used) because |
| 2082 |
in this case Libassuan calls this function internally. See also |
| 2083 |
@code{assuan_set_sock_nonce}. |
| 2084 |
|
| 2085 |
Actually this mechanism is only required on Windows but for cleanness of |
| 2086 |
code it may be used on POSIX systems as well, where this function is |
| 2087 |
a nop. |
| 2088 |
@end deftypefun |
| 2089 |
|
| 2090 |
To control certain properties of the wrapper two additional functions |
| 2091 |
are provided: |
| 2092 |
|
| 2093 |
@deftypefun int assuan_sock_set_flag ( @ |
| 2094 |
@w{assuan_fd_t @var{fd}}, @ |
| 2095 |
@w{const char *@var{name}}, @ |
| 2096 |
@w{int @var{value}}) |
| 2097 |
|
| 2098 |
Set the flags @var{name} for socket @var{fd} to @var{value}. See |
| 2099 |
below for a list of valid names. Returns 0 on success; on failure |
| 2100 |
sets ERRNO and returns -1. |
| 2101 |
@end deftypefun |
| 2102 |
|
| 2103 |
@deftypefun int assuan_sock_get_flag ( @ |
| 2104 |
@w{assuan_fd_t @var{fd}}, @ |
| 2105 |
@w{const char *@var{name}}, @ |
| 2106 |
@w{int *@var{r_value}}) |
| 2107 |
|
| 2108 |
Store the current value of the flag @var{name} for socket @var{fd} at |
| 2109 |
@var{r_value}. See below for a list of valid names. Returns 0 on |
| 2110 |
success; on failure sets ERRNO and returns -1. |
| 2111 |
@end deftypefun |
| 2112 |
|
| 2113 |
The supported flags are: |
| 2114 |
|
| 2115 |
@table @code |
| 2116 |
@item cygwin |
| 2117 |
This flag has an effect only on Windows. If the value is 1, the |
| 2118 |
socket is set into Cygwin mode so that Cygwin clients can connect to |
| 2119 |
such a socket. This flag needs to be set before a bind and should not |
| 2120 |
be changed during the lifetime of the socket. There is no need to set |
| 2121 |
this flag for connecting to a Cygwin style socket because no state is |
| 2122 |
required at the client. On non-Windows platforms setting this flag is |
| 2123 |
ignored, reading the flag always returns a value of 0. |
| 2124 |
|
| 2125 |
@item tor-mode |
| 2126 |
@itemx socks |
| 2127 |
If @var{value} is 1 globally enable SOCKS5 mode for new connections |
| 2128 |
using IPv6 or IPv4. @var{fd} must be set to @code{ASSUAN_INVALID_FD} A |
| 2129 |
future extension may allow to disable SOCKS5 mode for a specified |
| 2130 |
socket but globally disabling SOCKS5 mode is not possible. Using the |
| 2131 |
flag ``tor-mode'' expects the SOCKS5 proxy to listen on port 9050, the |
| 2132 |
flag ``socks'' expects the proxy to listen on port 1080. |
| 2133 |
|
| 2134 |
Connections to the loopback address are not routed though the SOCKS |
| 2135 |
proxy. UDP requests are not supported at all. The proxy will be |
| 2136 |
connected at address 127.0.0.1; an IPv6 connection to the proxy is not |
| 2137 |
yet supported. |
| 2138 |
|
| 2139 |
@end table |
| 2140 |
|
| 2141 |
|
| 2142 |
@c --------------------------------------------------------------------- |
| 2143 |
@c Legal BS |
| 2144 |
@c --------------------------------------------------------------------- |
| 2145 |
|
| 2146 |
@include lgpl.texi |
| 2147 |
@include gpl.texi |
| 2148 |
|
| 2149 |
|
| 2150 |
@c --------------------------------------------------------------------- |
| 2151 |
@c Indexes |
| 2152 |
@c --------------------------------------------------------------------- |
| 2153 |
|
| 2154 |
@node Index |
| 2155 |
@unnumbered Index |
| 2156 |
|
| 2157 |
@printindex cp |
| 2158 |
|
| 2159 |
@c --------------------------------------------------------------------- |
| 2160 |
@c Epilogue |
| 2161 |
@c --------------------------------------------------------------------- |
| 2162 |
|
| 2163 |
@bye |
| 2164 |
|
| 2165 |
|