Blame doc/assuan.texi

Packit Service cbd1e2
\input texinfo                      @c -*-texinfo-*-
Packit Service cbd1e2
@c %**start of header
Packit Service cbd1e2
@setfilename assuan.info
Packit Service cbd1e2
Packit Service cbd1e2
@macro copyrightnotice
Packit Service cbd1e2
Copyright @copyright{} 2001--2013 Free Software Foundation, Inc. @*
Packit Service cbd1e2
Copyright @copyright{} 2001--2015 g10 Code GmbH
Packit Service cbd1e2
@end macro
Packit Service cbd1e2
@macro permissionnotice
Packit Service cbd1e2
Permission is granted to copy, distribute and/or modify this document
Packit Service cbd1e2
under the terms of the GNU General Public License as published by the
Packit Service cbd1e2
Free Software Foundation; either version 3 of the License, or (at your
Packit Service cbd1e2
option) any later version. The text of the license can be found in the
Packit Service cbd1e2
section entitled ``Copying''.
Packit Service cbd1e2
@end macro
Packit Service cbd1e2
Packit Service cbd1e2
@include version.texi
Packit Service cbd1e2
Packit Service cbd1e2
@settitle Developing with Assuan
Packit Service cbd1e2
Packit Service cbd1e2
@c Create a separate index for command line options.
Packit Service cbd1e2
@defcodeindex op
Packit Service cbd1e2
@c Merge the standard indexes into a single one.
Packit Service cbd1e2
@syncodeindex fn cp
Packit Service cbd1e2
@syncodeindex vr cp
Packit Service cbd1e2
@syncodeindex ky cp
Packit Service cbd1e2
@syncodeindex pg cp
Packit Service cbd1e2
@syncodeindex tp cp
Packit Service cbd1e2
Packit Service cbd1e2
@c A simple macro for optional variables.
Packit Service cbd1e2
@macro ovar{varname}
Packit Service cbd1e2
@r{[}@var{\varname\}@r{]}
Packit Service cbd1e2
@end macro
Packit Service cbd1e2
Packit Service cbd1e2
@c printing stuff taken from gcc.
Packit Service cbd1e2
@macro gnupgtabopt{body}
Packit Service cbd1e2
@code{\body\}
Packit Service cbd1e2
@end macro
Packit Service cbd1e2
@macro gnupgoptlist{body}
Packit Service cbd1e2
@smallexample
Packit Service cbd1e2
\body\
Packit Service cbd1e2
@end smallexample
Packit Service cbd1e2
@end macro
Packit Service cbd1e2
@c Makeinfo handles the above macro OK, TeX needs manual line breaks;
Packit Service cbd1e2
@c they get lost at some point in handling the macro.  But if @macro is
Packit Service cbd1e2
@c used here rather than @alias, it produces double line breaks.
Packit Service cbd1e2
@iftex
Packit Service cbd1e2
@alias gol = *
Packit Service cbd1e2
@end iftex
Packit Service cbd1e2
@ifnottex
Packit Service cbd1e2
@macro gol
Packit Service cbd1e2
@end macro
Packit Service cbd1e2
@end ifnottex
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@c Change the font used for @def... commands, since the default
Packit Service cbd1e2
@c proportional one used is bad for names starting __.
Packit Service cbd1e2
@tex
Packit Service cbd1e2
\gdef\mysetfont#1#2#3#4{\font#1=\fontprefix#2#3 scaled #4}
Packit Service cbd1e2
\global\mysetfont\defbf\ttbshape{10}{\magstep1}
Packit Service cbd1e2
@end tex
Packit Service cbd1e2
Packit Service cbd1e2
@c %**end of header
Packit Service cbd1e2
Packit Service cbd1e2
@ifnottex
Packit Service cbd1e2
@dircategory GNU Libraries
Packit Service cbd1e2
@direntry
Packit Service cbd1e2
* Assuan: (assuan).        An IPC library for non-persistent servers.
Packit Service cbd1e2
@end direntry
Packit Service cbd1e2
This file documents the use and the internals of Assuan.
Packit Service cbd1e2
Packit Service cbd1e2
This is Edition @value{EDITION}, last updated @value{UPDATED}, of
Packit Service cbd1e2
@cite{The `Developing with Assuan' Manual}, for Version @value{VERSION}.
Packit Service cbd1e2
@sp 1
Packit Service cbd1e2
Published by the Free Software Foundation@*
Packit Service cbd1e2
51 Franklin Street, Fifth Floor@*
Packit Service cbd1e2
Boston, MA 02110-1301 USA
Packit Service cbd1e2
@sp 1
Packit Service cbd1e2
@copyrightnotice{}
Packit Service cbd1e2
@sp 1
Packit Service cbd1e2
@permissionnotice{}
Packit Service cbd1e2
@end ifnottex
Packit Service cbd1e2
Packit Service cbd1e2
@setchapternewpage odd
Packit Service cbd1e2
Packit Service cbd1e2
@titlepage
Packit Service cbd1e2
@title Developing with Assuan
Packit Service cbd1e2
@subtitle Version @value{VERSION}
Packit Service cbd1e2
@subtitle @value{UPDATED}
Packit Service cbd1e2
@author by Werner Koch and Marcus Brinkmann
Packit Service cbd1e2
@author @code{@{wk,mb@}@@g10code.com}
Packit Service cbd1e2
Packit Service cbd1e2
@page
Packit Service cbd1e2
@vskip 0pt plus 1filll
Packit Service cbd1e2
@copyrightnotice{}
Packit Service cbd1e2
@sp 2
Packit Service cbd1e2
@permissionnotice{}
Packit Service cbd1e2
@end titlepage
Packit Service cbd1e2
@summarycontents
Packit Service cbd1e2
@contents
Packit Service cbd1e2
@page
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@ifnottex
Packit Service cbd1e2
@node Top
Packit Service cbd1e2
@top Introduction
Packit Service cbd1e2
@cindex introduction
Packit Service cbd1e2
Packit Service cbd1e2
This manual documents how to exploit the Assuan library, a simple
Packit Service cbd1e2
interprocess communcation library.
Packit Service cbd1e2
@end ifnottex
Packit Service cbd1e2
Packit Service cbd1e2
@menu
Packit Service cbd1e2
* Introduction::        An introduction to and the motivation behind Assuan.
Packit Service cbd1e2
* Assuan::              Description of the Assuan protocol.
Packit Service cbd1e2
* Implementation::      Overview of the implementation.
Packit Service cbd1e2
* Preparation::         What you should do before using the library.
Packit Service cbd1e2
* Generalities::        Initialization code and data types used.
Packit Service cbd1e2
* Client code::         How to develop an Assuan client.
Packit Service cbd1e2
* Server code::         How to develop an Assuan server.
Packit Service cbd1e2
* External I/O Loop::   How to use external I/O event loops.
Packit Service cbd1e2
* Utilities::           Utility functions.
Packit Service cbd1e2
* Socket wrappers::     Socket wrapper functions.
Packit Service cbd1e2
Packit Service cbd1e2
Miscellaneous
Packit Service cbd1e2
Packit Service cbd1e2
* Library Copying::     GNU Lesser General Public License says
Packit Service cbd1e2
                        how you can copy and share Assuan.
Packit Service cbd1e2
* Copying::             How you can copy and share this manual.
Packit Service cbd1e2
Packit Service cbd1e2
Indices
Packit Service cbd1e2
Packit Service cbd1e2
* Index::	        Index of concepts and symbol names.
Packit Service cbd1e2
Packit Service cbd1e2
@end menu
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@c
Packit Service cbd1e2
@c   I N T R O
Packit Service cbd1e2
@c
Packit Service cbd1e2
@node Introduction
Packit Service cbd1e2
@chapter Introduction to Assuan
Packit Service cbd1e2
Packit Service cbd1e2
Assuan is an extensible inter-process communication (IPC) protocol and
Packit Service cbd1e2
library.  It is designed for point-to-point communication and it
Packit Service cbd1e2
doesn't provide a naming system.  To contact a server, either the
Packit Service cbd1e2
client must know how to locate the server, e.g., via a well-known Unix
Packit Service cbd1e2
domain socket, or, if the server is transient, how to start it.  In
Packit Service cbd1e2
the latter case, Assuan provides functionality to start the server
Packit Service cbd1e2
process.
Packit Service cbd1e2
Packit Service cbd1e2
In Assuan, communication is typically either via a pipe or a Unix
Packit Service cbd1e2
domain socket.  This method is neither elegant nor efficient,
Packit Service cbd1e2
especially when there is a lot of data spread across several
Packit Service cbd1e2
transactions.  Not only is there a penalty for an increased number of
Packit Service cbd1e2
context switches, but a significant amount of data is @var{memcpy}ed
Packit Service cbd1e2
from the client to a file descriptor and from the file descriptor to
Packit Service cbd1e2
the server.  Despite these and other disadvantages, this type of
Packit Service cbd1e2
client/server communication is useful: the client is separated from
Packit Service cbd1e2
the server: they run in different address spaces.  This is especially
Packit Service cbd1e2
important in situations where the server must have a known degree of
Packit Service cbd1e2
reliability and data must be protected: as the Assuan protocol is well
Packit Service cbd1e2
defined and clients cannot corrupt the servers' address space,
Packit Service cbd1e2
auditing becomes much easier.
Packit Service cbd1e2
Packit Service cbd1e2
Assuan was developed for use by the GNU Privacy Guard (GnuPG) to
Packit Service cbd1e2
prevent potentially buggy clients from unwittingly corrupting
Packit Service cbd1e2
sensitive transactions or compromising data such as a secret key.
Packit Service cbd1e2
Assuan permits the servers, which do the actual work, e.g., encryption
Packit Service cbd1e2
and decryption of data using a secret key, to be developed
Packit Service cbd1e2
independently of the user interfaces, e.g., mail clients and other
Packit Service cbd1e2
encryption front ends.  Like a shared library, the interface is well
Packit Service cbd1e2
defined and any number of front ends can use it; however, unlike a
Packit Service cbd1e2
shared library, the client cannot see or touch the server's data.  As
Packit Service cbd1e2
with any modular system, Assuan helps keep the components small,
Packit Service cbd1e2
understandable and less error prone.
Packit Service cbd1e2
Packit Service cbd1e2
Assuan is not, however, limited to use with GnuPG servers and clients:
Packit Service cbd1e2
it was designed to be flexible enough to meet the demands of many
Packit Service cbd1e2
transaction-based environments.
Packit Service cbd1e2
Packit Service cbd1e2
@node Assuan
Packit Service cbd1e2
@chapter Description of the Assuan protocol.
Packit Service cbd1e2
Packit Service cbd1e2
The architecture of the modular GnuPG system is based on several
Packit Service cbd1e2
highly specialized modules which form a network of clients and
Packit Service cbd1e2
servers.  A common framework for intermodule communication is
Packit Service cbd1e2
therefore needed and implemented as a library.
Packit Service cbd1e2
Packit Service cbd1e2
Goals:
Packit Service cbd1e2
Packit Service cbd1e2
@itemize @bullet
Packit Service cbd1e2
@item Common framework for module communication
Packit Service cbd1e2
@item Easy debugging
Packit Service cbd1e2
@item Easy module testing
Packit Service cbd1e2
@item Extensible
Packit Service cbd1e2
@item Optional authentication and encryption facility
Packit Service cbd1e2
@item Usable to access external hardware
Packit Service cbd1e2
@end itemize
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
Design criteria:
Packit Service cbd1e2
Packit Service cbd1e2
@itemize @bullet
Packit Service cbd1e2
@item Client/Server with back channel
Packit Service cbd1e2
@item Use a mainly text based protocol
Packit Service cbd1e2
@item Escape certain control characters
Packit Service cbd1e2
@item Allow indefinite data length
Packit Service cbd1e2
@item Request confidentiality for parts of the communication
Packit Service cbd1e2
@item Dummy module to allow direct linking of client and server
Packit Service cbd1e2
@item Inline data or descriptor passing for bulk data
Packit Service cbd1e2
@item No protection against DoS needed
Packit Service cbd1e2
@item Subliminal channels are not an issue
Packit Service cbd1e2
@end itemize
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@node Implementation
Packit Service cbd1e2
@chapter Implementation
Packit Service cbd1e2
Packit Service cbd1e2
The implementation is line based with a maximum line size of 1000
Packit Service cbd1e2
octets.  The default IPC mechanism is Unix Domain Sockets.
Packit Service cbd1e2
Packit Service cbd1e2
On connect, the server responds either with okay or an error status.
Packit Service cbd1e2
To perform an authentication check, the server may send an Inquiry
Packit Service cbd1e2
response prior to the first Okay.  It may also issue Status messages.
Packit Service cbd1e2
The server must check that the client is allowed to connect.  This is
Packit Service cbd1e2
done by requesting the credentials for the peer and comparing them
Packit Service cbd1e2
with the server's credentials.  This avoids attacks based on wrong
Packit Service cbd1e2
socket permissions.
Packit Service cbd1e2
Packit Service cbd1e2
The server may choose to delay the first response in case of an error.
Packit Service cbd1e2
The server, however, never closes the connection, however, the lower
Packit Service cbd1e2
protocol may do so after some time of inactivity or when the
Packit Service cbd1e2
connection enters an error state.
Packit Service cbd1e2
Packit Service cbd1e2
All textual messages are assumed to be in UTF-8 unless otherwise noted.
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@menu
Packit Service cbd1e2
* Server responses::    Description of server responses.
Packit Service cbd1e2
* Client requests::     Description of client requests.
Packit Service cbd1e2
* Error codes::         List of error and status codes.
Packit Service cbd1e2
@end menu
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@node Server responses
Packit Service cbd1e2
@section Server responses
Packit Service cbd1e2
Packit Service cbd1e2
@table @code
Packit Service cbd1e2
@item OK  [<arbitrary debugging information>]
Packit Service cbd1e2
Request was successful.
Packit Service cbd1e2
Packit Service cbd1e2
@item ERR @var{errorcode} [<human readable error description>]
Packit Service cbd1e2
Request could not be fulfilled.  The possible error codes are defined
Packit Service cbd1e2
by @code{libgpg-error}.
Packit Service cbd1e2
Packit Service cbd1e2
@item S @var{keyword} <status information depending on keyword>
Packit Service cbd1e2
Informational output by the server, which is still processing the
Packit Service cbd1e2
request.  A client may not send such lines to the server while
Packit Service cbd1e2
processing an Inquiry command.  @var{keyword} shall start with a
Packit Service cbd1e2
letter or an underscore.
Packit Service cbd1e2
Packit Service cbd1e2
@item # <string>
Packit Service cbd1e2
Comment line issued only for debugging purposes.  Totally ignored.
Packit Service cbd1e2
Packit Service cbd1e2
@item D <raw data>
Packit Service cbd1e2
Raw data returned to client. There must be exactly one space after the
Packit Service cbd1e2
'D'.  The values for '%', CR and LF must be percent escaped; these are
Packit Service cbd1e2
encoded as %25, %0D and %0A, respectively.  Only uppercase letters
Packit Service cbd1e2
should be used in the hexadecimal representation.  Other characters
Packit Service cbd1e2
may be percent escaped for easier debugging.  All Data lines are
Packit Service cbd1e2
considered one data stream up to the OK or ERR response.  Status and
Packit Service cbd1e2
Inquiry Responses may be mixed with the Data lines.
Packit Service cbd1e2
Packit Service cbd1e2
@item INQUIRE @var{keyword} <parameters>
Packit Service cbd1e2
The server needs further information from the client.  The client
Packit Service cbd1e2
should respond with data (using the ``D'' command and terminated by
Packit Service cbd1e2
``END'').  Alternatively, the client may cancel the current operation
Packit Service cbd1e2
by responding with ``CAN''.
Packit Service cbd1e2
@end table
Packit Service cbd1e2
Packit Service cbd1e2
Consider the following examples (lines prefixed with S indicate text
Packit Service cbd1e2
that the server sends; lines prefixed with C indicate text that the
Packit Service cbd1e2
client sends):
Packit Service cbd1e2
Packit Service cbd1e2
@example
Packit Service cbd1e2
S: INQUIRE foo
Packit Service cbd1e2
C: D foo bar
Packit Service cbd1e2
C: D bar baz
Packit Service cbd1e2
C: END
Packit Service cbd1e2
[Server continues normal work]
Packit Service cbd1e2
@end example
Packit Service cbd1e2
Packit Service cbd1e2
This implements a callback to the client:
Packit Service cbd1e2
Packit Service cbd1e2
@example
Packit Service cbd1e2
S: INQUIRE foo
Packit Service cbd1e2
C: END
Packit Service cbd1e2
[Server continues]
Packit Service cbd1e2
@end example
Packit Service cbd1e2
Packit Service cbd1e2
and:
Packit Service cbd1e2
Packit Service cbd1e2
@example
Packit Service cbd1e2
S: INQUIRE foo
Packit Service cbd1e2
C: CAN
Packit Service cbd1e2
[Server terminates the operaion and in most cases returns an ERR to the client.]
Packit Service cbd1e2
@end example
Packit Service cbd1e2
Packit Service cbd1e2
But, CAN may also mean ``I have no data for you, try to get it from
Packit Service cbd1e2
elsewhere.''
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
Note: lines longer than 1000 bytes should be treated as a
Packit Service cbd1e2
communication error.  (The rationale for having a line length limit is
Packit Service cbd1e2
to allow for easier multiplexing of several channels.)
Packit Service cbd1e2
Packit Service cbd1e2
@node Client requests
Packit Service cbd1e2
@section Client requests
Packit Service cbd1e2
Packit Service cbd1e2
The server waits for client requests after sending an Okay or Error.
Packit Service cbd1e2
The client should not issue a request in other cases.
Packit Service cbd1e2
Packit Service cbd1e2
@example
Packit Service cbd1e2
@var{command} <parameters>
Packit Service cbd1e2
@end example
Packit Service cbd1e2
Packit Service cbd1e2
@var{command} is a one word string without preceding white space.
Packit Service cbd1e2
Parameters are command specific, CR, LF and the percent signs should
Packit Service cbd1e2
be percent escaped as described above.  To send a backslash as the
Packit Service cbd1e2
last character it should also be percent escaped.  Percent escaping is
Packit Service cbd1e2
allowed anywhere in the parameters but not in the command.  The line
Packit Service cbd1e2
ends with a CR, LF pair or just a LF.
Packit Service cbd1e2
Packit Service cbd1e2
Not yet implemented feature: If there is a need for a parameter list
Packit Service cbd1e2
longer than the line length limit (1000 characters including command
Packit Service cbd1e2
and CR, LF), the last character of the line (right before the CR/LF or
Packit Service cbd1e2
LF) must be a unescaped (i.e., literal) backslash. The following line
Packit Service cbd1e2
is then expected to be a continuation of the line with the backslash
Packit Service cbd1e2
replaced by a blank and the line ending removed.
Packit Service cbd1e2
Packit Service cbd1e2
@example
Packit Service cbd1e2
D <raw data>
Packit Service cbd1e2
@end example
Packit Service cbd1e2
Packit Service cbd1e2
Sends raw data to the server.  There must be exactly one space after
Packit Service cbd1e2
the 'D'.  The values for '%', CR and LF must be percent escaped.
Packit Service cbd1e2
These are encoded as %25, %0D and %0A, respectively.  Only uppercase
Packit Service cbd1e2
letters should be used in the hexadecimal representation.  Other
Packit Service cbd1e2
characters may be percent escaped for easier debugging.  All Data
Packit Service cbd1e2
lines are considered one data stream up to the @code{OK} or @code{ERR}
Packit Service cbd1e2
response.  Status and Inquiry Responses may be mixed with the Data
Packit Service cbd1e2
lines.
Packit Service cbd1e2
Packit Service cbd1e2
@example
Packit Service cbd1e2
END
Packit Service cbd1e2
@end example
Packit Service cbd1e2
Packit Service cbd1e2
Lines beginning with a @code{#} or empty lines are ignored.  This is
Packit Service cbd1e2
useful to comment test scripts.
Packit Service cbd1e2
Packit Service cbd1e2
Although the commands are application specific, some of them are used
Packit Service cbd1e2
by all protocols and partly supported by the Assuan library:
Packit Service cbd1e2
Packit Service cbd1e2
@table @code
Packit Service cbd1e2
@item BYE
Packit Service cbd1e2
Close the connection.  The server will respond with @code{OK}.
Packit Service cbd1e2
Packit Service cbd1e2
@item RESET
Packit Service cbd1e2
Reset the connection but not any existing authentication.  The server
Packit Service cbd1e2
should release all resources associated with the connection.
Packit Service cbd1e2
Packit Service cbd1e2
@item END
Packit Service cbd1e2
Used by a client to mark the end of raw data.  The server may send
Packit Service cbd1e2
@code{END} to indicate a partial end of data.
Packit Service cbd1e2
Packit Service cbd1e2
@item HELP
Packit Service cbd1e2
Lists all commands that the server understands as comment lines on the
Packit Service cbd1e2
status channel.
Packit Service cbd1e2
Packit Service cbd1e2
@item QUIT
Packit Service cbd1e2
Reserved for future extensions.
Packit Service cbd1e2
Packit Service cbd1e2
@item OPTION
Packit Service cbd1e2
Set options for the connection.  The syntax of such a line is
Packit Service cbd1e2
@display
Packit Service cbd1e2
  OPTION @var{name} [ [=] @var{value} ]
Packit Service cbd1e2
@end display
Packit Service cbd1e2
Leading and trailing spaces around @var{name} and @var{value} are
Packit Service cbd1e2
allowed but should be ignored.  For compatibility reasons, @var{name}
Packit Service cbd1e2
may be prefixed with two dashes.  The use of the equal sign is optional
Packit Service cbd1e2
but suggested if @var{value} is given.
Packit Service cbd1e2
Packit Service cbd1e2
@item CANCEL
Packit Service cbd1e2
This command is reserved for future extensions.
Packit Service cbd1e2
Packit Service cbd1e2
@item AUTH
Packit Service cbd1e2
This command is reserved for future extensions.  Not yet specified as
Packit Service cbd1e2
we don't implement it in the first phase.  See Werner's mail to gpa-dev on
Packit Service cbd1e2
2001-10-25 about the rationale for measurements against local attacks.
Packit Service cbd1e2
Packit Service cbd1e2
@item NOP
Packit Service cbd1e2
No operation.  Returns OK without any action.
Packit Service cbd1e2
@end table
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@node Error codes
Packit Service cbd1e2
@section Error codes
Packit Service cbd1e2
Packit Service cbd1e2
Libassuan is used with gpg-error style error codes.  It is recommended
Packit Service cbd1e2
to set the error source to a different value from the default
Packit Service cbd1e2
@code{GPG_ERR_SOURCE_UNKNOWN} by calling @ref{function
Packit Service cbd1e2
assuan_set_gpg_err_source} early.
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@c
Packit Service cbd1e2
@c           P R E P A R A T I O N
Packit Service cbd1e2
@c
Packit Service cbd1e2
@node Preparation
Packit Service cbd1e2
@chapter Preparation
Packit Service cbd1e2
Packit Service cbd1e2
To use @sc{Assuan}, you have to make some changes to your
Packit Service cbd1e2
sources and the build system.  The necessary changes are small and
Packit Service cbd1e2
explained in the following sections.
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@menu
Packit Service cbd1e2
* Header::                 What header file you need to include.
Packit Service cbd1e2
* Building sources::       How to build sources using the library.
Packit Service cbd1e2
* Automake::               How to build sources with the help of Automake.
Packit Service cbd1e2
* Multi Threading::        How @code{libassuan} can be used in a MT environment.
Packit Service cbd1e2
@end menu
Packit Service cbd1e2
Packit Service cbd1e2
@node Header
Packit Service cbd1e2
@section Header
Packit Service cbd1e2
Packit Service cbd1e2
All interfaces (data types and functions) of @code{libassuan} are defined
Packit Service cbd1e2
in the header file @file{assuan.h}.  You must include this in all source
Packit Service cbd1e2
files using the library, either directly or through some other header
Packit Service cbd1e2
file, like this:
Packit Service cbd1e2
Packit Service cbd1e2
@example
Packit Service cbd1e2
#include <assuan.h>
Packit Service cbd1e2
@end example
Packit Service cbd1e2
Packit Service cbd1e2
The namespace of @code{libassuan} is @code{assuan_*} for function
Packit Service cbd1e2
and type names and @code{ASSUAN*} for other symbols.  In addition the
Packit Service cbd1e2
same name prefixes with one prepended underscore are reserved for
Packit Service cbd1e2
internal use and should never be used by an application.
Packit Service cbd1e2
Packit Service cbd1e2
Because @code{libassuan} makes use of the GPG Error library, using
Packit Service cbd1e2
@code{libassuan} will also use the @code{GPG_ERR_*} namespace
Packit Service cbd1e2
directly, and the @code{gpg_err*} and @code{gpg_str*} namespaces
Packit Service cbd1e2
indirectly.
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@node Building sources
Packit Service cbd1e2
@section Building sources
Packit Service cbd1e2
Packit Service cbd1e2
If you want to compile a source file including the @file{assuan.h}
Packit Service cbd1e2
header file, you must make sure that the compiler can find it in the
Packit Service cbd1e2
directory hierarchy.  This is accomplished by adding the path to the
Packit Service cbd1e2
directory in which the header file is located to the compilers include
Packit Service cbd1e2
file search path (via the @option{-I} option).
Packit Service cbd1e2
Packit Service cbd1e2
However, the path to the include file is determined at the time the
Packit Service cbd1e2
source is configured.  To solve this problem, @code{libassuan} ships with
Packit Service cbd1e2
a small helper program @command{libassuan-config} that knows the path to
Packit Service cbd1e2
the include file and other configuration options.  The options that need
Packit Service cbd1e2
to be added to the compiler invocation at compile time are output by the
Packit Service cbd1e2
@option{--cflags} option to @command{libassuan-config}.  The following
Packit Service cbd1e2
example shows how it can be used at the command line:
Packit Service cbd1e2
Packit Service cbd1e2
@example
Packit Service cbd1e2
gcc -c foo.c $(libassuan-config --cflags)
Packit Service cbd1e2
@end example
Packit Service cbd1e2
Packit Service cbd1e2
Adding the output of @samp{libassuan-config --cflags} to the compiler's
Packit Service cbd1e2
command line will ensure that the compiler can find the @file{assuan.h}
Packit Service cbd1e2
header file.
Packit Service cbd1e2
Packit Service cbd1e2
A similar problem occurs when linking the program with the library.
Packit Service cbd1e2
Again, the compiler/linker has to find the library files.  For this to
Packit Service cbd1e2
work, the path to the library files has to be added to the library
Packit Service cbd1e2
search path (via the @option{-L} option).  For this, the option
Packit Service cbd1e2
@option{--libs} to @command{libassuan-config} can be used.  For
Packit Service cbd1e2
convenience, this option also outputs all other options that are
Packit Service cbd1e2
required to link the program with the @code{libassuan} libraries (in
Packit Service cbd1e2
particular, the @option{-lassuan} option).  The example shows how to
Packit Service cbd1e2
link @file{foo.o} with the @code{libassuan} library to a program
Packit Service cbd1e2
@command{foo}.
Packit Service cbd1e2
Packit Service cbd1e2
@example
Packit Service cbd1e2
gcc -o foo foo.o $(libassuan-config --libs)
Packit Service cbd1e2
@end example
Packit Service cbd1e2
Packit Service cbd1e2
You can also combine both examples to a single command by specifying
Packit Service cbd1e2
both options to @command{libassuan-config}:
Packit Service cbd1e2
Packit Service cbd1e2
@example
Packit Service cbd1e2
gcc -o foo foo.c $(libassuan-config --cflags --libs)
Packit Service cbd1e2
@end example
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@node Automake
Packit Service cbd1e2
@section Building sources using Automake
Packit Service cbd1e2
Packit Service cbd1e2
It is much easier if you use GNU Automake instead of writing your own
Packit Service cbd1e2
Makefiles.  If you do that you do not have to worry about finding and
Packit Service cbd1e2
invoking the @command{libassuan-config} script at all.  @code{libassuan}
Packit Service cbd1e2
provides an Automake macro that does all the work for you.
Packit Service cbd1e2
Packit Service cbd1e2
@defmac AM_PATH_LIBASSUAN (@ovar{minimum-version}, @ovar{action-if-found}, @ovar{action-if-not-found})
Packit Service cbd1e2
Check whether @code{libassuan} (at least version
Packit Service cbd1e2
@var{minimum-version}, if given) exists on the host system.  If it is
Packit Service cbd1e2
found, execute @var{action-if-found}, otherwise do
Packit Service cbd1e2
@var{action-if-not-found}, if given.
Packit Service cbd1e2
Packit Service cbd1e2
Additionally, the function defines @code{LIBASSUAN_CFLAGS} to the
Packit Service cbd1e2
flags needed for compilation of the program to find the
Packit Service cbd1e2
@file{assuan.h} header file, and @code{LIBASSUAN_LIBS} to the linker
Packit Service cbd1e2
flags needed to link the program to the @code{libassuan} library.
Packit Service cbd1e2
@end defmac
Packit Service cbd1e2
Packit Service cbd1e2
You can use the defined Autoconf variables like this in your
Packit Service cbd1e2
@file{Makefile.am}:
Packit Service cbd1e2
Packit Service cbd1e2
@example
Packit Service cbd1e2
AM_CPPFLAGS = $(LIBASSUAN_CFLAGS)
Packit Service cbd1e2
LDADD = $(LIBASSUAN_LIBS)
Packit Service cbd1e2
@end example
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@node Multi Threading
Packit Service cbd1e2
@section Multi Threading
Packit Service cbd1e2
Packit Service cbd1e2
The @code{libassuan} library is designed so that it can be used in a
Packit Service cbd1e2
threaded application, if some rules are followed.
Packit Service cbd1e2
Packit Service cbd1e2
@itemize @bullet
Packit Service cbd1e2
@item Run the initialization functions before you actually start
Packit Service cbd1e2
to use threads.  Specifically, the functions
Packit Service cbd1e2
@code{assuan_set_gpg_err_source}, @code{assuan_set_malloc_hooks} and
Packit Service cbd1e2
@code{assuan_set_log_cb} should not be called concurrently with
Packit Service cbd1e2
@code{assuan_new}.  Use @code{assuan_new_ext} instead or ensure proper
Packit Service cbd1e2
serialization.
Packit Service cbd1e2
@item Only one thread at a time may access an @code{libassuan} context.
Packit Service cbd1e2
@item If you use the default log handler, use
Packit Service cbd1e2
@code{assuan_set_assuan_log_stream} to setup a default log stream.
Packit Service cbd1e2
@item If you have callback functions shared by multiple functions,
Packit Service cbd1e2
the callback function must be reentrant for that purpose.
Packit Service cbd1e2
@code{libassuan} does not serialize invocation of callback functions
Packit Service cbd1e2
across contexts.
Packit Service cbd1e2
@end itemize
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@c
Packit Service cbd1e2
@c     G E N E R A L I T I E S
Packit Service cbd1e2
@c
Packit Service cbd1e2
@node Generalities
Packit Service cbd1e2
@chapter Generalities
Packit Service cbd1e2
Packit Service cbd1e2
@menu
Packit Service cbd1e2
* Data Types:: Data types used by @code{libassuan}.
Packit Service cbd1e2
* Initializing the library:: How to initialize the library.
Packit Service cbd1e2
* Default Log Handler:: How to configure the default log handler.
Packit Service cbd1e2
* Contexts:: How to work with contexts.
Packit Service cbd1e2
* Reading and Writing:: How to communicate with the peer.
Packit Service cbd1e2
@end menu
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@node Data Types
Packit Service cbd1e2
@section Data Types used by the library
Packit Service cbd1e2
Packit Service cbd1e2
@sc{Assuan} uses a so-called context to store a connection's state.
Packit Service cbd1e2
The following data type is used for that:
Packit Service cbd1e2
Packit Service cbd1e2
@deftp {Data type} assuan_context_t
Packit Service cbd1e2
The @code{assuan_context_t} type is a pointer to an object maintained
Packit Service cbd1e2
internally by the library.  Contexts are allocated with
Packit Service cbd1e2
@code{assuan_new} or @code{assuan_new_ext} and released with
Packit Service cbd1e2
@code{assuan_release}.  Other functions take this data type to access
Packit Service cbd1e2
the state created by these functions.
Packit Service cbd1e2
@end deftp
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@deftp {Data type} assuan_fd_t
Packit Service cbd1e2
The @code{assuan_fd_t} is a file descriptor (in Unix) or a system
Packit Service cbd1e2
handle (in Windows).  The special value @code{ASSUAN_INVALID_FD} is
Packit Service cbd1e2
used to specify invalid Assuan file descriptors.
Packit Service cbd1e2
@end deftp
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun assuan_fd_t assuan_fdopen (@w{int @var{fd}})
Packit Service cbd1e2
Create an assuan file descriptor from a POSIX (libc) file descriptor
Packit Service cbd1e2
@var{fd}.  On Unix, this is equivalent to @code{dup}, while on Windows
Packit Service cbd1e2
this will retrieve the underlying system handle with
Packit Service cbd1e2
@code{_get_osfhandle} and duplicate that.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@node Initializing the library
Packit Service cbd1e2
@section Initializing the library
Packit Service cbd1e2
Packit Service cbd1e2
Libassuan makes use of Libgpg-error and assumes that Libgpg-error has
Packit Service cbd1e2
been initialized.  In general @code{gpgrt_check_version} should be
Packit Service cbd1e2
called to guarantee this; the Libgpg-error manual for details.
Packit Service cbd1e2
Packit Service cbd1e2
Libassuan itself requires no initialization. There are however some
Packit Service cbd1e2
initialization hooks provided which are often useful.  These should be
Packit Service cbd1e2
called as early as possible and in a multi-threaded application before
Packit Service cbd1e2
a second thread is created.
Packit Service cbd1e2
Packit Service cbd1e2
These functions initialize default values that are used at context
Packit Service cbd1e2
creation with @code{assuan_new}.  As there can only be one default,
Packit Service cbd1e2
all values can also be set directly with @code{assuan_new_ext} or with
Packit Service cbd1e2
context-specific functions after context creation.
Packit Service cbd1e2
Packit Service cbd1e2
If your application uses its own memory allocation functions or wrappers
Packit Service cbd1e2
it is good idea to tell @code{libassuan} about it so it can make use of the
Packit Service cbd1e2
same functions or wrappers:
Packit Service cbd1e2
Packit Service cbd1e2
@deftp {Data type} {struct assuan_malloc_hooks}
Packit Service cbd1e2
This structure is used to store the memory allocation callback
Packit Service cbd1e2
interface functions.  It has the following members, whose semantics
Packit Service cbd1e2
are identical to the corresponding system functions:
Packit Service cbd1e2
Packit Service cbd1e2
@table @code
Packit Service cbd1e2
@item void *(*malloc) (size_t cnt)
Packit Service cbd1e2
This is the function called by @sc{Assuan} to allocate memory for a context.
Packit Service cbd1e2
Packit Service cbd1e2
@item void *(*realloc) (void *ptr, size_t cnt)
Packit Service cbd1e2
This is the function called by @sc{Assuan} to reallocate memory for a context.
Packit Service cbd1e2
Packit Service cbd1e2
@item void (*free) (void *ptr)
Packit Service cbd1e2
This is the function called by @sc{Assuan} to release memory for a context.
Packit Service cbd1e2
@end table
Packit Service cbd1e2
@end deftp
Packit Service cbd1e2
Packit Service cbd1e2
@deftp {Data type} {assuan_malloc_hooks_t}
Packit Service cbd1e2
This is a pointer to a @code{struct assuan_malloc_hooks}.
Packit Service cbd1e2
@end deftp
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun void assuan_set_malloc_hooks (@w{assuan_malloc_hooks_t @var{malloc_hooks}})
Packit Service cbd1e2
This function sets the default allocation hooks for new contexts
Packit Service cbd1e2
allocated with @code{assuan_new}.  You need to provide all three
Packit Service cbd1e2
functions.  Those functions need to behave exactly as their standard
Packit Service cbd1e2
counterparts @code{malloc}, @code{realloc} and @code{free}.  If you
Packit Service cbd1e2
write your own functions, please take care to set @code{errno}
Packit Service cbd1e2
whenever an error has occurred.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun assuan_malloc_hooks_t assuan_get_malloc_hooks ()
Packit Service cbd1e2
This function gets the default allocation hooks for new contexts
Packit Service cbd1e2
allocated with @code{assuan_new}.  The result structure is statically
Packit Service cbd1e2
allocated and should not be modified.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
The @sc{Assuan} library uses @code{libgpg-error} error values, which
Packit Service cbd1e2
consist and error code and an error source.  The default source used
Packit Service cbd1e2
by contexts allocated with @code{assuan_new} can be set with the
Packit Service cbd1e2
following function.
Packit Service cbd1e2
Packit Service cbd1e2
@anchor{function assuan_set_gpg_err_source}
Packit Service cbd1e2
@deftypefun void assuan_set_gpg_err_source (@w{gpg_err_source_t @var{err_source}})
Packit Service cbd1e2
This function sets the default error source for errors generated by
Packit Service cbd1e2
contexts allocated with @code{assuan_new}.
Packit Service cbd1e2
Packit Service cbd1e2
One way to call this function is
Packit Service cbd1e2
@smallexample
Packit Service cbd1e2
assuan_set_gpg_err_source (GPG_ERR_SOURCE_DEFAULT);
Packit Service cbd1e2
@end smallexample
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun gpg_err_source_t assuan_get_gpg_err_source (void)
Packit Service cbd1e2
This function gets the default error source for errors generated by
Packit Service cbd1e2
contexts allocated with @code{assuan_new}.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@noindent
Packit Service cbd1e2
To integrate assuan logging and diagnostics into your own logging
Packit Service cbd1e2
system, you may use the following two functions:
Packit Service cbd1e2
Packit Service cbd1e2
@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}})}
Packit Service cbd1e2
The user-provided callback function takes a context @var{ctx}, for
Packit Service cbd1e2
which the message @var{msg} was generated, and a hook value
Packit Service cbd1e2
@var{hook_value} that was supplied when the log handler was registered
Packit Service cbd1e2
for the context with @code{assuan_set_log_cb}, and a category
Packit Service cbd1e2
@var{cat}.  The category is one of:
Packit Service cbd1e2
Packit Service cbd1e2
@table @code
Packit Service cbd1e2
@item ASSUAN_LOG_INIT
Packit Service cbd1e2
@item ASSUAN_LOG_CTX
Packit Service cbd1e2
@item ASSUAN_LOG_ENGINE
Packit Service cbd1e2
@item ASSUAN_LOG_DATA
Packit Service cbd1e2
RFU
Packit Service cbd1e2
@item ASSUAN_LOG_SYSIO
Packit Service cbd1e2
Log lowlevel I/O data.
Packit Service cbd1e2
@item ASSUAN_LOG_CONTROL
Packit Service cbd1e2
Log the control channel.
Packit Service cbd1e2
@end table
Packit Service cbd1e2
Packit Service cbd1e2
The user may then, depending on the category, write the message to a
Packit Service cbd1e2
log file or treat it in some other way.
Packit Service cbd1e2
Packit Service cbd1e2
If @var{msg} is a null pointer, then no message should be logged, but
Packit Service cbd1e2
the function should return 1 if it is interested in log messages with
Packit Service cbd1e2
the category @var{cat}.  If it is not interested, 0 should be
Packit Service cbd1e2
returned.  This allows @code{libassuan} to suppress the generation of
Packit Service cbd1e2
expensive debug output.
Packit Service cbd1e2
@end deftp
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun void assuan_set_log_cb (@w{assuan_log_cb_t @var{log_cb}}, @w{void *@var{log_cb_data}})
Packit Service cbd1e2
This function sets the default logging handler for log messages
Packit Service cbd1e2
generated by contexts allocated with @code{assuan_new}.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun void assuan_get_log_cb (@w{assuan_log_cb_t *@var{log_cb}}, @w{void **@var{log_cb_data}})
Packit Service cbd1e2
This function gets the default logging handler for log messages
Packit Service cbd1e2
generated by contexts allocated with @code{assuan_new}.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
You do not need to set a log handler, as @sc{Assuan} provides a
Packit Service cbd1e2
configurable default log handler that should be suitable for most
Packit Service cbd1e2
purposes.  Logging can be disabled completely by setting the log
Packit Service cbd1e2
handler to a null pointer.
Packit Service cbd1e2
Packit Service cbd1e2
@node Default Log Handler
Packit Service cbd1e2
@section Default Log Handler
Packit Service cbd1e2
Packit Service cbd1e2
The default log handler can be configured by the following functions:
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun void assuan_set_assuan_log_prefix (@w{const char *@var{text}})
Packit Service cbd1e2
Set the prefix to be used at the start of a line emitted by assuan
Packit Service cbd1e2
on the log stream to @var{text}.  The default is the empty string.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun @w{const char *} assuan_get_assuan_log_prefix (void)
Packit Service cbd1e2
Return the prefix to be used at the start of a line emitted by assuan
Packit Service cbd1e2
on the log stream.  The default implementation returns the empty
Packit Service cbd1e2
string.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun void assuan_set_assuan_log_stream (FILE *@var{fp})
Packit Service cbd1e2
This sets the default log stream to which @code{libassuan} should log messages not
Packit Service cbd1e2
associated with a specific context to @var{fp}.  The default is to log
Packit Service cbd1e2
to @code{stderr}.  This default value is also changed by using
Packit Service cbd1e2
@code{assuan_set_log_stream} (to set a logging stream for a specific
Packit Service cbd1e2
context) unless this function has been used.  Obviously this is not
Packit Service cbd1e2
thread-safe and thus it is highly recommended to use this function to
Packit Service cbd1e2
setup a proper default.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun @w{FILE *} assuan_get_assuan_log_stream (void)
Packit Service cbd1e2
Return the stream which is currently being using for global logging.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
The log stream used by the default log handler can also be set on a
Packit Service cbd1e2
per context basis.
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun void assuan_set_log_stream (@w{assuan_context_t @var{ctx}}, @w{FILE *@var{fp}})
Packit Service cbd1e2
Enable debugging for the context @var{ctx} and write all debugging
Packit Service cbd1e2
output to the stdio stream @var{fp}.  If the default log stream (used
Packit Service cbd1e2
for non-context specific events) has not yet been set, a call to this
Packit Service cbd1e2
functions implicitly sets this stream also to @var{fp}.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@node Contexts
Packit Service cbd1e2
@section How to work with contexts
Packit Service cbd1e2
Packit Service cbd1e2
Some operations work globally on the library, but most operate in a
Packit Service cbd1e2
context, which saves state across operations.  To allow the use of
Packit Service cbd1e2
@code{libassuan} in mixed environments, such as in a library using
Packit Service cbd1e2
GPGME and an application using GPGME, the context is very extensive
Packit Service cbd1e2
and covers utilitary information like memory allocation callbacks as
Packit Service cbd1e2
well as specific information associated with client/server operations.
Packit Service cbd1e2
Packit Service cbd1e2
@anchor{function assuan_new}
Packit Service cbd1e2
@deftypefun gpg_error_t assuan_new (@w{assuan_context_t *@var{ctx_p}})
Packit Service cbd1e2
The function @code{assuan_new} creates a new context, using the global
Packit Service cbd1e2
default memory allocation, log handler and @code{libgpg-error} source.
Packit Service cbd1e2
It is equivalent to
Packit Service cbd1e2
Packit Service cbd1e2
@smallexample
Packit Service cbd1e2
gpg_error_t err;
Packit Service cbd1e2
assuan_log_cb_t log_cb;
Packit Service cbd1e2
void *log_cb_data;
Packit Service cbd1e2
Packit Service cbd1e2
assuan_get_log_cb (&log_cb, &log_cb_data);
Packit Service cbd1e2
err = assuan_new_ext (ctx_p, assuan_get_gpg_err_source (),
Packit Service cbd1e2
                      assuan_get_malloc_hooks (), log_cb, log_cb_data);
Packit Service cbd1e2
@end smallexample
Packit Service cbd1e2
Packit Service cbd1e2
As you can see, this is not thread-safe.  Take care not to modify the
Packit Service cbd1e2
memory allocation hooks or log callback handler concurrently with
Packit Service cbd1e2
@code{assuan_new}.
Packit Service cbd1e2
Packit Service cbd1e2
The function returns an error if a memory allocation error occurs, and
Packit Service cbd1e2
0 with the new context in @var{ctx_p} otherwise.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@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}})
Packit Service cbd1e2
The function @code{assuan_new_ext} creates a new context using the
Packit Service cbd1e2
supplied @code{libgpg-error} error source @var{err_source}, the memory
Packit Service cbd1e2
allocation hooks @var{malloc_hooks} and the log handler @var{log_cb}
Packit Service cbd1e2
with the user data @var{log_cb_data}.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
After the context has been used, it can be destroyed again.
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun void assuan_release (assuan_context_t ctx)
Packit Service cbd1e2
The function @code{assuan_release} destroys the context CTX and
Packit Service cbd1e2
releases all associated resources.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
Other properties of the context beside the memory allocation handler,
Packit Service cbd1e2
the log handler, and the @code{libgpg-error} source can be set after
Packit Service cbd1e2
context creation.  Here are some of them:
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun void assuan_set_pointer (@w{assuan_context_t @var{ctx}}, @w{void *@var{pointer}})
Packit Service cbd1e2
Packit Service cbd1e2
Store the arbitrary pointer value @var{pointer} into the context
Packit Service cbd1e2
@var{ctx}.  This is useful to provide command handlers with additional
Packit Service cbd1e2
application context.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun void* assuan_get_pointer (@w{assuan_context_t @var{ctx}})
Packit Service cbd1e2
Packit Service cbd1e2
This returns the pointer for context @var{ctx} which has been set using
Packit Service cbd1e2
the above function.  A common way to use it is by setting the pointer
Packit Service cbd1e2
before starting the processing loop and to retrieve it right at the
Packit Service cbd1e2
start of a command handler:
Packit Service cbd1e2
@smallexample
Packit Service cbd1e2
static int
Packit Service cbd1e2
cmd_foo (assuan_context_t ctx, char *line)
Packit Service cbd1e2
@{
Packit Service cbd1e2
  ctrl_t ctrl = assuan_get_pointer (ctx);
Packit Service cbd1e2
  ...
Packit Service cbd1e2
@}
Packit Service cbd1e2
@end smallexample
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun void assuan_set_flag (@w{assuan_context_t @var{ctx}}, @w{assuan_flag_t @var{flag}}, @w{int @var{value}})
Packit Service cbd1e2
Packit Service cbd1e2
Set the the @var{flag} for context @var{ctx} to @var{value}.  Values for
Packit Service cbd1e2
flags are usually 1 or 0 but certain flags might need other values.
Packit Service cbd1e2
Packit Service cbd1e2
@deftp {Data type} assuan_flag_t
Packit Service cbd1e2
The flags are all named and collected in an @code{enum} for better readability.
Packit Service cbd1e2
Available flags are:
Packit Service cbd1e2
Packit Service cbd1e2
@table @code
Packit Service cbd1e2
@item ASSUAN_NO_WAITPID
Packit Service cbd1e2
When using a pipe server, by default Libassuan will wait for the forked
Packit Service cbd1e2
process to die in @code{assuan_release}.  In certain cases this is
Packit Service cbd1e2
not desirable.  By setting this flag, a call to @code{waitpid} will be
Packit Service cbd1e2
suppressed and the caller is responsible to cleanup the child process.
Packit Service cbd1e2
@item ASSUAN_CONFIDENTIAL
Packit Service cbd1e2
Use to return the state of the confidential logging mode.
Packit Service cbd1e2
@item ASSUAN_NO_FIXSIGNALS
Packit Service cbd1e2
Do not modify signal handler for @code{SIGPIPE}.
Packit Service cbd1e2
@item ASSUAN_CONVEY_COMMENTS
Packit Service cbd1e2
If enabled comment lines are passed to the status callback of the
Packit Service cbd1e2
@code{assuan_transact}.
Packit Service cbd1e2
@item ASSUAN_FORCE_CLOSE
Packit Service cbd1e2
Setting this flag forces the next command to assume that the
Packit Service cbd1e2
connection has been closed.  This breaks the command processing loop
Packit Service cbd1e2
and may be used as an implicit BYE command.  @var{value} is ignored
Packit Service cbd1e2
and thus it is not possible to clear this flag.
Packit Service cbd1e2
@end table
Packit Service cbd1e2
@end deftp
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun int assuan_get_flag (@w{assuan_context_t @var{ctx}}, @w{assuan_flag_t @var{flag}})
Packit Service cbd1e2
Return the value of @var{flag} in context @var{ctx}.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun void assuan_begin_confidential (@w{assuan_context_t @var{ctx}})
Packit Service cbd1e2
Put the logging feature into confidential mode.  This is to avoid
Packit Service cbd1e2
logging of sensitive data.
Packit Service cbd1e2
Packit Service cbd1e2
This is identical to:
Packit Service cbd1e2
@smallexample
Packit Service cbd1e2
assuan_set_flag (ctx, ASSUAN_CONFIDENTIAL, 1);
Packit Service cbd1e2
@end smallexample
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun void assuan_end_confidential (@w{assuan_context_t @var{ctx}})
Packit Service cbd1e2
Get the logging feature out of confidential mode.  All data will be
Packit Service cbd1e2
logged again (if logging is enabled).
Packit Service cbd1e2
Packit Service cbd1e2
This is identical to:
Packit Service cbd1e2
@smallexample
Packit Service cbd1e2
assuan_set_flag (ctx, ASSUAN_CONFIDENTIAL, 0);
Packit Service cbd1e2
@end smallexample
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@deftp {Data type} {struct assuan_system_hooks}
Packit Service cbd1e2
This structure is used to store the system callback interface
Packit Service cbd1e2
functions.  It has the following members, whose semantics are similar
Packit Service cbd1e2
to the corresponding system functions, but not exactly equivalent.
Packit Service cbd1e2
Packit Service cbd1e2
@table @code
Packit Service cbd1e2
@item int version
Packit Service cbd1e2
The user should set this to @code{ASSUAN_SYSTEM_HOOKS_VERSION}.  This
Packit Service cbd1e2
indicates to the library which members of this structure are present
Packit Service cbd1e2
in case of future extensions.  The user should initialize the whole
Packit Service cbd1e2
structure with zero bytes.
Packit Service cbd1e2
Packit Service cbd1e2
@item  void (*usleep) (assuan_context_t ctx, unsigned int usec)
Packit Service cbd1e2
This is the function called by @sc{Assuan} to sleep for @code{USEC}
Packit Service cbd1e2
microseconds.
Packit Service cbd1e2
Packit Service cbd1e2
@item int (*pipe) (assuan_context_t ctx, assuan_fd_t fd[2], int inherit_idx)
Packit Service cbd1e2
This is the function called by @sc{Assuan} to create a pipe.  The
Packit Service cbd1e2
returned file descriptor @code{fd[inherit_idx]} must be inheritable by
Packit Service cbd1e2
the child process (under Windows, this requires some extra work).
Packit Service cbd1e2
Packit Service cbd1e2
@item int (*close) (assuan_context_t ctx, assuan_fd_t fd)
Packit Service cbd1e2
This is the function called by @sc{Assuan} to close a file descriptor
Packit Service cbd1e2
created through the system functions.
Packit Service cbd1e2
Packit Service cbd1e2
@item ssize_t (*read) (assuan_context_t ctx, assuan_fd_t fd, void *buffer, size_t size)
Packit Service cbd1e2
This is the function called by @sc{Assuan} to read data from a file
Packit Service cbd1e2
descriptor.  It is functionally equivalent to the system @code{read}
Packit Service cbd1e2
function.
Packit Service cbd1e2
Packit Service cbd1e2
@item ssize_t (*write) (assuan_context_t ctx, assuan_fd_t fd, const void *buffer, size_t size)
Packit Service cbd1e2
This is the function called by @sc{Assuan} to write data to a file
Packit Service cbd1e2
descriptor.  It is functionally equivalent to the system @code{write}
Packit Service cbd1e2
function.
Packit Service cbd1e2
Packit Service cbd1e2
@item int (*recvmsg) (assuan_context_t ctx, assuan_fd_t fd, assuan_msghdr_t msg, int flags)
Packit Service cbd1e2
This is the function called by @sc{Assuan} to receive a message from a
Packit Service cbd1e2
file descriptor.  It is functionally equivalent to the system
Packit Service cbd1e2
@code{recvmsg} function.
Packit Service cbd1e2
Packit Service cbd1e2
@item int (*sendmsg) (assuan_context_t ctx, assuan_fd_t fd, const assuan_msghdr_t msg, int flags);
Packit Service cbd1e2
This is the function called by @sc{Assuan} to send a message to a
Packit Service cbd1e2
file descriptor.  It is functionally equivalent to the system
Packit Service cbd1e2
@code{sendmsg} function.
Packit Service cbd1e2
Packit Service cbd1e2
@item int (*spawn) (assuan_context_t ctx, 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)
Packit Service cbd1e2
This is the function called by @sc{Assuan} to spawn a child process.
Packit Service cbd1e2
The @code{stdin} and @code{stdout} file descriptors are provided in
Packit Service cbd1e2
@code{fd_in} and @code{fd_out} respectively, but can be set to
Packit Service cbd1e2
@code{ASSUAN_INVALID_FD}, in which case they are set to
Packit Service cbd1e2
@code{/dev/null}.  On systems which use @code{fork} and @code{exec},
Packit Service cbd1e2
the @code{atfork} function should be called with @code{atforkvalue}
Packit Service cbd1e2
and @code{0} for flags in the child process right after @code{fork}
Packit Service cbd1e2
returns.  @code{fd_child_list} is a @code{ASSUAN_INVALID_FD}
Packit Service cbd1e2
terminated array (or @code{NULL}) and specifies file descriptors to be
Packit Service cbd1e2
inherited by the child process.
Packit Service cbd1e2
Packit Service cbd1e2
A special situation occurs if @code{name} is a null pointer, in which
Packit Service cbd1e2
case the process should just fork but not call @code{exec}.  In this
Packit Service cbd1e2
case, @code{*argv} should be set to @code{"client"} in the parent
Packit Service cbd1e2
process and @code{"server"} in the child process.
Packit Service cbd1e2
Packit Service cbd1e2
Flags is the bit-wise OR of some (or none) of the following flags:
Packit Service cbd1e2
Packit Service cbd1e2
@table @code
Packit Service cbd1e2
@item ASSUAN_SPAWN_DETACHED
Packit Service cbd1e2
If set and there is a need to start the server it will be started as a
Packit Service cbd1e2
background process.  This flag is useful under W32 systems, so that no
Packit Service cbd1e2
new console is created and pops up a console window when starting the
Packit Service cbd1e2
server.  On W32CE systems this flag is ignored.
Packit Service cbd1e2
@end table
Packit Service cbd1e2
Packit Service cbd1e2
@item pid_t (*waitpid) (assuan_context_t ctx, pid_t pid, int action, int *status, int options)
Packit Service cbd1e2
This is the function called by @sc{Assuan} to wait for the spawned
Packit Service cbd1e2
child process @var{pid} to exit, or, if @var{action} is 1, to just
Packit Service cbd1e2
release all resources associated with @var{pid} (required on Windows
Packit Service cbd1e2
platforms).  If @var{action} is 0, this is equivalent to @code{waitpid}.
Packit Service cbd1e2
Packit Service cbd1e2
@item int (*socketpair) (assuan_context_t ctx, int namespace, int style, int protocol, assuan_fd_t filedes[2])
Packit Service cbd1e2
This is the function called by @sc{Assuan} to create a socketpair.  It
Packit Service cbd1e2
is equivalent to @code{socketpair}.
Packit Service cbd1e2
@end table
Packit Service cbd1e2
@end deftp
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun void assuan_set_system_hooks (@w{assuan_system_hooks_t @var{system_hooks}})
Packit Service cbd1e2
Set the default system hooks to use.  There is currently no way to
Packit Service cbd1e2
reset to the default system hooks.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun void assuan_sock_set_system_hooks (@w{assuan_system_hooks_t @var{system_hooks}})
Packit Service cbd1e2
The socket subsystem uses an internal context which uses the default
Packit Service cbd1e2
system hooks.  This function allows to change these system hooks.  The
Packit Service cbd1e2
function is not thread-safe and only useful if a certain order of
Packit Service cbd1e2
assuan and assuan socket initializations are required.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun void assuan_ctx_set_system_hooks (@w{assuan_context_t @var{ctx}}, @w{assuan_system_hooks_t @var{system_hooks}})
Packit Service cbd1e2
Set the system hooks for context @var{ctx}.  There is currently no way
Packit Service cbd1e2
to reset to the default system hooks, create a new context for that.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
The following system hook collections are defined by the library for
Packit Service cbd1e2
your convenience:
Packit Service cbd1e2
Packit Service cbd1e2
@table @code
Packit Service cbd1e2
@item ASSUAN_SYSTEM_NPTH
Packit Service cbd1e2
System hooks suitable for use with the nPth library.
Packit Service cbd1e2
@item ASSUAN_SYSTEM_NPTH_IMPL
Packit Service cbd1e2
The implementation of system hooks for use with the nPth library.
Packit Service cbd1e2
This must be invoked once somewhere in the application, and defines
Packit Service cbd1e2
the structure that is referenced by @code{ASSUAN_SYSTEM_NPTH}.
Packit Service cbd1e2
@item ASSUAN_SYSTEM_PTH
Packit Service cbd1e2
System hooks suitable for use with the GNU Pth library.
Packit Service cbd1e2
@item ASSUAN_SYSTEM_PTH_IMPL
Packit Service cbd1e2
The implementation of system hooks for use with the GNU Pth library.
Packit Service cbd1e2
This must be invoked once somewhere in the application, and defines
Packit Service cbd1e2
the structure that is referenced by @code{ASSUAN_SYSTEM_PTH}.
Packit Service cbd1e2
@end table
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@node Reading and Writing
Packit Service cbd1e2
@section How to communicate with the peer
Packit Service cbd1e2
Packit Service cbd1e2
What would be an IPC library without the ability to read and write
Packit Service cbd1e2
data?  Not very useful.  Libassuan has high level functions to take
Packit Service cbd1e2
care of of the more boring stuff, but eventually data needs to be
Packit Service cbd1e2
written and read.
Packit Service cbd1e2
Packit Service cbd1e2
@noindent
Packit Service cbd1e2
The basic read and write functions are:
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun gpg_error_t assuan_read_line (@w{assuan_context_t @var{ctx}}, @w{char **@var{line}}, @w{size_t *@var{linelen}})
Packit Service cbd1e2
Packit Service cbd1e2
Read the next line written by the peer to the control channel and store
Packit Service cbd1e2
a pointer to the buffer holding that line at the address @var{line}.
Packit Service cbd1e2
The valid length of the lines is stored at the address of @var{linelen}.
Packit Service cbd1e2
This buffer is valid until the next read operation on the same context
Packit Service cbd1e2
@var{ctx}.  You may modify the context of this buffer.  The buffer is
Packit Service cbd1e2
invalid (i.e. must not be used) if an error is returned.  This function
Packit Service cbd1e2
returns @code{0} on success or an error value.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun gpg_error_t assuan_write_line (@w{assuan_context_t @var{ctx}}, @w{const char *@var{line}})
Packit Service cbd1e2
Packit Service cbd1e2
Write the string @var{line} to the other end on the control channel.
Packit Service cbd1e2
This string needs to be a proper formatted Assuan protocol line and
Packit Service cbd1e2
should not include a linefeed.  Sending linefeed or @code{Nul}
Packit Service cbd1e2
characters is not possible and not allowed by the assuan protocol.  This
Packit Service cbd1e2
function shall not be used for sending data (@code{D}) lines.  This
Packit Service cbd1e2
function returns @code{0} on success or an error value.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@noindent
Packit Service cbd1e2
To actually send bulk data lines a specialized function is available:
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun gpg_error_t assuan_send_data (@w{assuan_context_t @var{ctx}}, @w{const void *@var{buffer}}, @w{size_t @var{length}})
Packit Service cbd1e2
Packit Service cbd1e2
This function is used by a server or a client to send @var{length} bytes
Packit Service cbd1e2
of bulk data in @var{buffer} to the other end on the control channel.
Packit Service cbd1e2
The data will be escaped as required by the Assuan protocol and may get
Packit Service cbd1e2
buffered until a line is full.  To flush any pending data, @var{buffer}
Packit Service cbd1e2
may be passed as @code{NULL} and @var{length} be @code{0}.
Packit Service cbd1e2
Packit Service cbd1e2
@noindent
Packit Service cbd1e2
When used by a client, this flush operation does also send the
Packit Service cbd1e2
@code{END} command to terminate the response on an @command{INQUIRE}
Packit Service cbd1e2
request.  Note that the function @code{assuan_transact} takes care of
Packit Service cbd1e2
sending this @code{END} itself.
Packit Service cbd1e2
Packit Service cbd1e2
@noindent
Packit Service cbd1e2
This function returns @code{0} on success or an error value.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
The input and output of data can be controlled at a higher level using
Packit Service cbd1e2
an I/O monitor.
Packit Service cbd1e2
Packit Service cbd1e2
@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}})}
Packit Service cbd1e2
The monitor function is called right after a line has been received,
Packit Service cbd1e2
if @var{inout} is @code{ASSUAN_IO_FROM_PEER}, or just before it is
Packit Service cbd1e2
send, if @var{inout} is @code{ASSUAN_IO_TO_PEER}.  The
Packit Service cbd1e2
@var{hook_value} is provided by the user when registering the I/O
Packit Service cbd1e2
monitor function with a context using @code{assuan_set_io_monitor}.
Packit Service cbd1e2
The callback function should return the bitwise OR of some (or none) of the
Packit Service cbd1e2
following flags:
Packit Service cbd1e2
Packit Service cbd1e2
@table @code
Packit Service cbd1e2
@item ASSUAN_IO_MONITOR_NOLOG
Packit Service cbd1e2
Active logging of this line is suppressed.  This can reduce debug
Packit Service cbd1e2
output in the case of a frequent message.
Packit Service cbd1e2
@item ASSUAN_IO_MONITOR_IGNORE
Packit Service cbd1e2
The whole output line is discarded.
Packit Service cbd1e2
@end table
Packit Service cbd1e2
@end deftp
Packit Service cbd1e2
Packit Service cbd1e2
@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}})
Packit Service cbd1e2
This function registers an I/O monitor @var{io_monitor} for the
Packit Service cbd1e2
context @var{ctx} with the hook value @var{hook_data}.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@c
Packit Service cbd1e2
@c     C L I E N T   C O D E
Packit Service cbd1e2
@c
Packit Service cbd1e2
@node Client code
Packit Service cbd1e2
@chapter How to develop an Assuan client
Packit Service cbd1e2
Packit Service cbd1e2
Depending on the type of the server you want to connect you need to use
Packit Service cbd1e2
different functions.
Packit Service cbd1e2
Packit Service cbd1e2
If the peer is not a simple pipe server but one using full-duplex
Packit Service cbd1e2
sockets, the full-fledged variant of the above function should be
Packit Service cbd1e2
used:
Packit Service cbd1e2
Packit Service cbd1e2
@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}})
Packit Service cbd1e2
Packit Service cbd1e2
A call to this functions forks the current process and executes the
Packit Service cbd1e2
program @var{name}, passing the arguments given in the NULL-terminated
Packit Service cbd1e2
list @var{argv}.  A list of file descriptors not to be closed may be
Packit Service cbd1e2
given using the @code{ASSUAN_INVALID_FD} terminated array @var{fd_child_list}.
Packit Service cbd1e2
Packit Service cbd1e2
If @var{name} is a null pointer, only a fork but no exec is done.  Thus
Packit Service cbd1e2
the child continues to run.  However all file descriptors are closed and
Packit Service cbd1e2
some special environment variables are set.  To let the caller detect
Packit Service cbd1e2
whether the child or the parent continues, the parent returns with
Packit Service cbd1e2
@code{"client"} returned in @var{argv} and the child returns with
Packit Service cbd1e2
@code{"server"} in @var{argv}.  This feature is only available on POSIX
Packit Service cbd1e2
platforms.
Packit Service cbd1e2
Packit Service cbd1e2
If @var{atfork} is not NULL, this function is called in the child right
Packit Service cbd1e2
after the fork and the value @var{atforkvalue} is passed as the first
Packit Service cbd1e2
argument.  That function should only act if the second argument it
Packit Service cbd1e2
received is @code{0}.  Such a fork callback is useful to release
Packit Service cbd1e2
additional resources not to be used by the child.
Packit Service cbd1e2
Packit Service cbd1e2
@noindent
Packit Service cbd1e2
@var{flags} is a bit vector and controls how the function acts:
Packit Service cbd1e2
Packit Service cbd1e2
@table @code
Packit Service cbd1e2
@item ASSUAN_PIPE_CONNECT_FDPASSING
Packit Service cbd1e2
If cleared a simple pipe based server is expected.  If set a server
Packit Service cbd1e2
based on full-duplex pipes is expected.  Such pipes are usually
Packit Service cbd1e2
created using the @code{socketpair} function.  It also enables
Packit Service cbd1e2
features only available with such servers.
Packit Service cbd1e2
Packit Service cbd1e2
@item ASSUAN_PIPE_CONNECT_DETACHED
Packit Service cbd1e2
If set and there is a need to start the server it will be started as a
Packit Service cbd1e2
background process.  This flag is useful under W32 systems, so that no
Packit Service cbd1e2
new console is created and pops up a console window when starting the
Packit Service cbd1e2
server.  On W32CE systems this flag is ignored.
Packit Service cbd1e2
@end table
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
If you are using a long running server listening either on a TCP or a
Packit Service cbd1e2
Unix domain socket, the following function is used to connect to the server:
Packit Service cbd1e2
Packit Service cbd1e2
@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}})
Packit Service cbd1e2
Packit Service cbd1e2
Make a connection to the Unix domain socket @var{name} using the
Packit Service cbd1e2
already-initialized Assuan context at @var{ctx}.  @var{server_pid} is
Packit Service cbd1e2
currently not used but may become handy in the future; if you don't
Packit Service cbd1e2
know the server's process ID (PID), pass @code{ASSUAN_INVALID_PID}.
Packit Service cbd1e2
With @var{flags} set to @code{ASSUAN_SOCKET_CONNECT_FDPASSING},
Packit Service cbd1e2
@code{sendmsg} and @code{recvmesg} are used for input and output and
Packit Service cbd1e2
thereby enable the use of descriptor passing.
Packit Service cbd1e2
Packit Service cbd1e2
Connecting to a TCP server is not yet implemented.  Standard URL
Packit Service cbd1e2
schemes are reserved for @var{name} specifying a TCP server.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
Now that we have a connection to the server, all work may be
Packit Service cbd1e2
conveniently done using a couple of callbacks and the transact
Packit Service cbd1e2
function:
Packit Service cbd1e2
Packit Service cbd1e2
@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}})
Packit Service cbd1e2
Packit Service cbd1e2
Here @var{ctx} is the Assuan context opened by one of the connect
Packit Service cbd1e2
calls.  @var{command} is the actual Assuan command string.  It
Packit Service cbd1e2
shall not end with a line feed and its length is limited to
Packit Service cbd1e2
@code{ASSUAN_LINELENGTH} (~1000 bytes)
Packit Service cbd1e2
Packit Service cbd1e2
@var{data_cb} is called by Libassuan for data lines; @var{data_cb_arg}
Packit Service cbd1e2
is passed to it along with the data and the length.  [FIXME: needs
Packit Service cbd1e2
more documentation].
Packit Service cbd1e2
Packit Service cbd1e2
@var{inquire_cb} is called by Libassuan when the server requests
Packit Service cbd1e2
additional information from the client while processing the command.
Packit Service cbd1e2
This callback shall check the provided inquiry name and send the data
Packit Service cbd1e2
as requested back using the @code{assuan_send_data}.  The server
Packit Service cbd1e2
passed @var{inquiry_cb_arg} along with the inquiry name to the
Packit Service cbd1e2
callback.
Packit Service cbd1e2
Packit Service cbd1e2
@var{status_cb} is called by Libassuan for each status line it receives
Packit Service cbd1e2
from the server.  @var{status_cb_arg} is passed along with the status
Packit Service cbd1e2
line to the callback.
Packit Service cbd1e2
Packit Service cbd1e2
The function returns @code{0} success or an error value.  The error value
Packit Service cbd1e2
may be the one one returned by the server in error lines or one
Packit Service cbd1e2
generated by the callback functions.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
Libassuan supports descriptor passing on some platforms.  The next two
Packit Service cbd1e2
functions are used with this feature:
Packit Service cbd1e2
Packit Service cbd1e2
@anchor{function assuan_sendfd}
Packit Service cbd1e2
@deftypefun gpg_error_t assuan_sendfd (@w{assuan_context_t @var{ctx}}, @w{assuan_fd_t @var{fd}})
Packit Service cbd1e2
Packit Service cbd1e2
Send the descriptor @var{fd} to the peer using the context @var{ctx}.
Packit Service cbd1e2
The descriptor must be sent before the command is issued that makes
Packit Service cbd1e2
use of the descriptor.
Packit Service cbd1e2
Packit Service cbd1e2
Note that calling this function with a @var{ctx} of @code{NULL} and
Packit Service cbd1e2
@var{fd} of @code{ASSUAN_INVALID_FD} can be used as a runtime test to
Packit Service cbd1e2
check whether descriptor passing is available on the platform:
Packit Service cbd1e2
@code{0} is returned if descriptor passing is available, otherwise an
Packit Service cbd1e2
error with the error code @code{GPG_ERR_NOT_IMPLEMENTED} is returned.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@anchor{function assuan_receivefd}
Packit Service cbd1e2
@deftypefun gpg_error_t assuan_receivefd (@w{assuan_context_t @var{ctx}}, @w{assuan_fd_t *@var{fd}})
Packit Service cbd1e2
Packit Service cbd1e2
Receive a descriptor pending for the context @var{ctx} from the peer.
Packit Service cbd1e2
The descriptor must be pending before this function is called.  To
Packit Service cbd1e2
accomplish this, the peer needs to use @code{assuan_sendfd} before the
Packit Service cbd1e2
trigger is sent (e.g. using @code{assuan_write_line ("INPUT FD")}.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@c
Packit Service cbd1e2
@c     S E R V E R   C O D E
Packit Service cbd1e2
@c
Packit Service cbd1e2
@node Server code
Packit Service cbd1e2
@chapter How to develop an Assuan server
Packit Service cbd1e2
Packit Service cbd1e2
Implementing a server for Assuan is a bit more complex than a client.
Packit Service cbd1e2
However, it is a straightforward task we are going to explain using a
Packit Service cbd1e2
commented example.
Packit Service cbd1e2
Packit Service cbd1e2
@noindent
Packit Service cbd1e2
The list of the implemented server commands is defined by a table like:
Packit Service cbd1e2
Packit Service cbd1e2
@smallexample
Packit Service cbd1e2
  static struct @{
Packit Service cbd1e2
    const char *name;
Packit Service cbd1e2
    int (*handler) (assuan_context_t, char *line);
Packit Service cbd1e2
  @} command_table[] = @{
Packit Service cbd1e2
    @{ "FOO", cmd_foo @},
Packit Service cbd1e2
    @{ "BAR", cmd_bar @},
Packit Service cbd1e2
    @{ "INPUT", NULL @},
Packit Service cbd1e2
    @{ "OUTPUT", NULL @},
Packit Service cbd1e2
    @{ NULL @}@};
Packit Service cbd1e2
@end smallexample
Packit Service cbd1e2
Packit Service cbd1e2
For convenience this table is usually put after the actual command
Packit Service cbd1e2
handlers (@code{cmd_foo}, @code{cmd_bar}) or even put inside
Packit Service cbd1e2
@code{command_handler} (see below).  Note that the commands
Packit Service cbd1e2
@code{INPUT} and @code{OUTPUT} do not require a handler because
Packit Service cbd1e2
Libassuan provides a default handler for them.  It is however possible
Packit Service cbd1e2
to assign a custom handler.
Packit Service cbd1e2
Packit Service cbd1e2
A prerequisite for this example code is that a client has already
Packit Service cbd1e2
connected to the server.  Often there are two modes combined in one
Packit Service cbd1e2
program: A pipe-based server, where a client has forked the server
Packit Service cbd1e2
process, or a Unix domain socket based server that is listening on the
Packit Service cbd1e2
socket.
Packit Service cbd1e2
Packit Service cbd1e2
@example
Packit Service cbd1e2
void
Packit Service cbd1e2
command_handler (int fd)
Packit Service cbd1e2
@{
Packit Service cbd1e2
  gpg_error_t rc;
Packit Service cbd1e2
  int i;
Packit Service cbd1e2
  assuan_context_t ctx;
Packit Service cbd1e2
Packit Service cbd1e2
  rc = assuan_new (&ctx;;
Packit Service cbd1e2
  if (rc)
Packit Service cbd1e2
    @{
Packit Service cbd1e2
      fprintf (stderr, "server context creation failed: %s\n",
Packit Service cbd1e2
               gpg_strerror(rc));
Packit Service cbd1e2
      return;
Packit Service cbd1e2
    @}
Packit Service cbd1e2
Packit Service cbd1e2
  if (fd == -1)
Packit Service cbd1e2
    @{
Packit Service cbd1e2
      assuan_fd_t filedes[2];
Packit Service cbd1e2
Packit Service cbd1e2
      filedes[0] = assuan_fd_from_posix_fd (0);
Packit Service cbd1e2
      filedes[1] = assuan_fd_from_posix_fd (1);
Packit Service cbd1e2
      rc = assuan_init_pipe_server (ctx, filedes);
Packit Service cbd1e2
    @}
Packit Service cbd1e2
  else
Packit Service cbd1e2
    rc = assuan_init_socket_server (ctx, fd, ASSUAN_SOCKET_SERVER_ACCEPTED);
Packit Service cbd1e2
  if (rc)
Packit Service cbd1e2
    @{
Packit Service cbd1e2
      fprintf (stderr, "server init failed: %s\n", gpg_strerror (rc));
Packit Service cbd1e2
      return;
Packit Service cbd1e2
    @}
Packit Service cbd1e2
@end example
Packit Service cbd1e2
Packit Service cbd1e2
@noindent
Packit Service cbd1e2
This is the first part of the command handler.  We start off by
Packit Service cbd1e2
allocating a new Assuan context with @code{assuan_new}.
Packit Service cbd1e2
@xref{function assuan_new}.
Packit Service cbd1e2
Packit Service cbd1e2
In case this is called as a pipe based server, @var{fd} will be based
Packit Service cbd1e2
as @var{fd} and the code assumes that the server's @code{stdin} and
Packit Service cbd1e2
@code{stdout} file handles are connected to a pipe.  The
Packit Service cbd1e2
initialization is thus done using the function:
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun gpg_error_t assuan_init_pipe_server (@w{assuan_context_t @var{ctx}}, @w{assuan_fd_t @var{filedes}[2]})
Packit Service cbd1e2
Packit Service cbd1e2
This function takes the two file descriptors from @var{filedes} and
Packit Service cbd1e2
returns a new Assuan context at @var{r_ctx}.  As usual, a return value
Packit Service cbd1e2
of @code{0} indicates success and a failure is indicated by
Packit Service cbd1e2
returning an error value.  In case of error, @code{NULL} will be stored
Packit Service cbd1e2
at @var{r_ctx}.
Packit Service cbd1e2
Packit Service cbd1e2
In case the server has been called using a bi-directional pipe
Packit Service cbd1e2
(socketpair), @var{filedes} is ignored and the file descriptor is
Packit Service cbd1e2
taken from the environment variable @env{_assuan_connection_fd}.  You
Packit Service cbd1e2
generally don't need to know this, because @code{assuan_pipe_connect},
Packit Service cbd1e2
which is called by the client to connect to such a server,
Packit Service cbd1e2
automagically sets this variable.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@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}})
Packit Service cbd1e2
Packit Service cbd1e2
This function takes the file descriptor @var{fd}, which is expected to
Packit Service cbd1e2
be associated with a socket, and an Assuan context @var{ctx}.  The
Packit Service cbd1e2
following bits are currently defined for @var{flags}:
Packit Service cbd1e2
Packit Service cbd1e2
@table @code
Packit Service cbd1e2
@item ASSUAN_SOCKET_SERVER_FDPASSING
Packit Service cbd1e2
If set, @code{sendmsg} and @code{recvmesg} are used for input and
Packit Service cbd1e2
output, which enables the use of descriptor passing.
Packit Service cbd1e2
@item ASSUAN_SOCKET_SERVER_ACCEPTED
Packit Service cbd1e2
If set, @var{fd} refers to an already accepted socket.  That is,
Packit Service cbd1e2
Libassuan won't call @var{accept} for it.  It is suggested to set this
Packit Service cbd1e2
bit as it allows better control of the connection state.
Packit Service cbd1e2
@end table
Packit Service cbd1e2
Packit Service cbd1e2
As usual, a return value of @code{0} indicates success and a failure
Packit Service cbd1e2
is indicated by returning an error value.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@noindent
Packit Service cbd1e2
On the Windows platform the following function needs to be called after
Packit Service cbd1e2
@code{assuan_init_socket_server}:
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun void assuan_set_sock_nonce ( @
Packit Service cbd1e2
        @w{assuan_context_t @var{ctx}}, @
Packit Service cbd1e2
        @w{assuan_sock_nonce_t *@var{nonce}})
Packit Service cbd1e2
Packit Service cbd1e2
Save a copy of @var{nonce} in context @var{ctx}.  This should be used
Packit Service cbd1e2
to register the server's nonce with a context established by
Packit Service cbd1e2
@code{assuan_init_socket_server}.  It is technically only needed for
Packit Service cbd1e2
Windows, but it does no harm to use it on other systems.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@noindent
Packit Service cbd1e2
After error checking, the implemented assuan commands are registered with
Packit Service cbd1e2
the server.
Packit Service cbd1e2
Packit Service cbd1e2
@example
Packit Service cbd1e2
  for (i = 0; command_table[i].name; i++)
Packit Service cbd1e2
    @{
Packit Service cbd1e2
      rc = assuan_register_command (ctx,
Packit Service cbd1e2
                                    command_table[i].name,
Packit Service cbd1e2
                                    command_table[i].handler, NULL);
Packit Service cbd1e2
      if (rc)
Packit Service cbd1e2
        @{
Packit Service cbd1e2
          fprintf (stderr, "register failed: %s\n", gpg_strerror (rc));
Packit Service cbd1e2
          assuan_release (ctx);
Packit Service cbd1e2
          return;
Packit Service cbd1e2
        @}
Packit Service cbd1e2
    @}
Packit Service cbd1e2
@end example
Packit Service cbd1e2
Packit Service cbd1e2
@deftp {Data type} {gpg_error_t (*assuan_handler_t) (@w{assuan_context_t @var{ctx}}, @w{char *@var{line}})}
Packit Service cbd1e2
This is the function invoked by @sc{Assuan} for various command
Packit Service cbd1e2
related callback functions.  Some of these callback functions have a
Packit Service cbd1e2
different type, but most use @code{assuan_handler_t}.
Packit Service cbd1e2
@end deftp
Packit Service cbd1e2
Packit Service cbd1e2
@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}})
Packit Service cbd1e2
Packit Service cbd1e2
This registers the command named @var{cmd_string} with the Assuan
Packit Service cbd1e2
context @var{ctx}.  @var{handler} is the function called by Libassuan
Packit Service cbd1e2
if this command is received from the client.  @var{NULL} may be used
Packit Service cbd1e2
for @var{handler} to use a default handler (this only works with a few
Packit Service cbd1e2
pre-defined commands).  Note that several default handlers have
Packit Service cbd1e2
already been registered when the context has been created: @code{NOP},
Packit Service cbd1e2
@code{CANCEL}, @code{OPTION}, @code{BYE}, @code{AUTH}, @code{RESET}
Packit Service cbd1e2
and @code{END}.  It is possible, but not recommended, to override
Packit Service cbd1e2
these commands.
Packit Service cbd1e2
Packit Service cbd1e2
@var{help_string} is a help string that is used for automatic
Packit Service cbd1e2
documentation.  It should contain a usage line followed by an empty
Packit Service cbd1e2
line and a complete description.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@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}})
Packit Service cbd1e2
Packit Service cbd1e2
Register a function to be called right after a command has been
Packit Service cbd1e2
processed.  @var{err} is the result code from the last internal assuan
Packit Service cbd1e2
operation and not the one returned by the handler.  It may be used for
Packit Service cbd1e2
command-related cleanup.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun gpg_error_t assuan_register_bye_notify (@w{assuan_context_t @var{ctx}}, @w{assuan_handler_t @var{handler}})
Packit Service cbd1e2
Packit Service cbd1e2
Register function @var{fnc} with context @var{ctx} to be called right
Packit Service cbd1e2
before the standard handler for the @code{BYE} command is being called.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun gpg_error_t assuan_register_reset_notify (@w{assuan_context_t @var{ctx}}, @w{assuan_handler_t @var{handler}})
Packit Service cbd1e2
Packit Service cbd1e2
Register function @var{fnc} with context @var{ctx} to be called right
Packit Service cbd1e2
before the standard handler for the @code{RESET} command is being called.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun gpg_error_t assuan_register_cancel_notify (@w{assuan_context_t @var{ctx}}, @w{assuan_handler_t @var{handler}})
Packit Service cbd1e2
Packit Service cbd1e2
Register function @var{fnc} with context @var{ctx} to be called right
Packit Service cbd1e2
before the standard handler for the @code{RESET} command is being called.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@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*)})
Packit Service cbd1e2
Packit Service cbd1e2
Register function @var{fnc} with context @var{ctx} for processing
Packit Service cbd1e2
options.  That function is being called with the context, the name and
Packit Service cbd1e2
the value of the option.  Leading and trailing spaces are removed from
Packit Service cbd1e2
the name and the value.  The optional leading two dashes of the name
Packit Service cbd1e2
are removed as well.  If no value has been given, an empty string is
Packit Service cbd1e2
passed.  The function needs to return @code{0} on success or an error
Packit Service cbd1e2
code.
Packit Service cbd1e2
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun gpg_error_t assuan_register_input_notify (@w{assuan_context_t @var{ctx}}, @w{assuan_handler_t @var{handler}})
Packit Service cbd1e2
Packit Service cbd1e2
Although the input function may be overridden with a custom handler, it
Packit Service cbd1e2
is often more convenient to use the default handler and to know whether
Packit Service cbd1e2
an @code{INPUT} command has been seen and successfully parsed.  The second
Packit Service cbd1e2
argument passed to that function is the entire line.  Because that line
Packit Service cbd1e2
has already been parsed when the function gets called, a file descriptor
Packit Service cbd1e2
set with the @code{INPUT} command may already be used.  That file
Packit Service cbd1e2
descriptor is available by calling @code{assuan_get_input_fd}.  If the
Packit Service cbd1e2
notification function returns an error, the input fd does not change.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun gpg_error_t assuan_register_output_notify (@w{assuan_context_t @var{ctx}}, @w{assuan_handler_t @var{handler}})
Packit Service cbd1e2
Packit Service cbd1e2
Although the output function may be overridden with a custom handler, it
Packit Service cbd1e2
is often more convenient to use the default handler and to know whether
Packit Service cbd1e2
an @code{OUTPUT} command has been seen and successfully parsed.  The second
Packit Service cbd1e2
argument passed to that function is the entire line.  Because that line
Packit Service cbd1e2
has already been parsed when the function gets called, a file descriptor
Packit Service cbd1e2
set with the @code{OUTPUT} command may already be used.  That file
Packit Service cbd1e2
descriptor is available by calling @code{assuan_get_output_fd}. If the
Packit Service cbd1e2
notification function returns an error, the output fd does not change.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun gpg_error_t assuan_set_hello_line (@w{assuan_context_t @var{ctx}}, @w{const char *@var{line}})
Packit Service cbd1e2
Packit Service cbd1e2
This is not actually a register function but may be called also after
Packit Service cbd1e2
registering commands. It changes the ``Hello'' line, sent by the
Packit Service cbd1e2
server to the client as a first response, from a default string to the
Packit Service cbd1e2
string @var{line}.  For logging purposes, it is often useful to use
Packit Service cbd1e2
such a custom hello line which may tell version numbers and such.
Packit Service cbd1e2
Linefeeds are allowed in this string, however, each line needs to be
Packit Service cbd1e2
shorter than the Assuan line length limit.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@noindent
Packit Service cbd1e2
Now that everything has been setup, we can start to process our
Packit Service cbd1e2
clients requests.
Packit Service cbd1e2
Packit Service cbd1e2
@example
Packit Service cbd1e2
  for (;;)
Packit Service cbd1e2
    @{
Packit Service cbd1e2
      rc = assuan_accept (ctx);
Packit Service cbd1e2
      if (rc == -1)
Packit Service cbd1e2
        break;
Packit Service cbd1e2
      else if (rc)
Packit Service cbd1e2
        @{
Packit Service cbd1e2
          fprintf (stderr, "accept problem: %s\n", gpg_strerror (rc));
Packit Service cbd1e2
          break;
Packit Service cbd1e2
        @}
Packit Service cbd1e2
Packit Service cbd1e2
      rc = assuan_process (ctx);
Packit Service cbd1e2
      if (rc)
Packit Service cbd1e2
        @{
Packit Service cbd1e2
          fprintf (stderr, "processing failed: %s\n", gpg_strerror (rc));
Packit Service cbd1e2
          continue;
Packit Service cbd1e2
        @}
Packit Service cbd1e2
    @}
Packit Service cbd1e2
  assuan_release (ctx);
Packit Service cbd1e2
@}
Packit Service cbd1e2
@end example
Packit Service cbd1e2
Packit Service cbd1e2
@noindent
Packit Service cbd1e2
For future extensibility and to properly detect the end of the
Packit Service cbd1e2
connection the core of the server should loop over the
Packit Service cbd1e2
accept and process calls.
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun gpg_error_t assuan_accept (@w{assuan_context_t @var{ctx}})
Packit Service cbd1e2
Packit Service cbd1e2
A call to this function cancel any existing connection and waits for a
Packit Service cbd1e2
connection from a client (that might be skipped, depending on the type
Packit Service cbd1e2
of the server).  The initial handshake is performed which may include an
Packit Service cbd1e2
initial authentication or encryption negotiation.  On success @code{0}
Packit Service cbd1e2
is returned.  An error value will be returned if the connection could for
Packit Service cbd1e2
some reason not be established.  An error code of @code{GPG_ERR_EOF} indicates
Packit Service cbd1e2
the end of the connection.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun gpg_error_t assuan_process (@w{assuan_context_t @var{ctx}})
Packit Service cbd1e2
Packit Service cbd1e2
This function is used to handle the Assuan protocol after a connection
Packit Service cbd1e2
has been established using @code{assuan_accept}.  It is the main
Packit Service cbd1e2
protocol handler responsible for reading the client commands and calling
Packit Service cbd1e2
the appropriate handlers.  The function returns @code{0} on success or
Packit Service cbd1e2
an error value if something went seriously wrong.  Error values from the
Packit Service cbd1e2
individual command handlers, i.e. operational error, are not seen here.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@noindent
Packit Service cbd1e2
That is all needed for the server code.  You only need to come up with
Packit Service cbd1e2
the code for the individual command handlers.  Take care that the line
Packit Service cbd1e2
passed to the command handlers is allocated statically within the
Packit Service cbd1e2
context and calls to Assuan functions may modify that line.  You are
Packit Service cbd1e2
also allowed to modify that line which makes parsing much easier.
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@c
Packit Service cbd1e2
@c    E x t e r n a l  I / O  L o o p s
Packit Service cbd1e2
@c
Packit Service cbd1e2
@node External I/O Loop
Packit Service cbd1e2
@chapter How to use external I/O event loops
Packit Service cbd1e2
Packit Service cbd1e2
The above implementations of an Assuan client and server are
Packit Service cbd1e2
synchronous, insofar as the main routines block until a request or
Packit Service cbd1e2
client connection is completely processed.  In some programs, for
Packit Service cbd1e2
example GUI applications, this is undesirable.  Instead, Assuan
Packit Service cbd1e2
operations should be non-blocking, and the caller should be able to
Packit Service cbd1e2
poll all involved file descriptors to determine when the next Assuan
Packit Service cbd1e2
function can be invoked without blocking.
Packit Service cbd1e2
Packit Service cbd1e2
To make this possible, client and server have to adhere to some rules:
Packit Service cbd1e2
@itemize @bullet
Packit Service cbd1e2
@item
Packit Service cbd1e2
Either partner should always write full lines.  If partial lines are
Packit Service cbd1e2
written, the remainder of the line should be sent without delay.
Packit Service cbd1e2
@item
Packit Service cbd1e2
Either partner should eagerly receive status messages.  While
Packit Service cbd1e2
receiving and sending bulk data may be delayed, the status
Packit Service cbd1e2
communication channel is different: Both partners may send status
Packit Service cbd1e2
messages in blocking mode at any time the protocol allows them to send
Packit Service cbd1e2
such status messages.  To ensure that these send operations do not
Packit Service cbd1e2
actually block the sender, the recipient must be ready to receive
Packit Service cbd1e2
without undue delay.
Packit Service cbd1e2
@item
Packit Service cbd1e2
If descriptor passing is used over a socket, the descriptor must be
Packit Service cbd1e2
sent after the corresponding command without undue delay.
Packit Service cbd1e2
@end itemize
Packit Service cbd1e2
Packit Service cbd1e2
Together, these restrictions allow to limit the need for asynchronous
Packit Service cbd1e2
I/O operations to bulk data and the inbound status file descriptor.
Packit Service cbd1e2
Packit Service cbd1e2
In addition to the above rules, client and server should adhere to the
Packit Service cbd1e2
following implementation guidelines.
Packit Service cbd1e2
Packit Service cbd1e2
@menu
Packit Service cbd1e2
* External I/O Loop Client::    External I/O event loops in the client.
Packit Service cbd1e2
* External I/O Loop Server::    External I/O event loops in the server.
Packit Service cbd1e2
@end menu
Packit Service cbd1e2
Packit Service cbd1e2
@node External I/O Loop Client
Packit Service cbd1e2
@section External I/O event loops in the client.
Packit Service cbd1e2
Packit Service cbd1e2
The reference implementation for using external I/O event loops in the
Packit Service cbd1e2
client is the GPGME library, which exports its own external I/O event
Packit Service cbd1e2
loop mechanism and utilizes the Assuan library transparently for the
Packit Service cbd1e2
user.  The following steps document how GPGME achieves this.
Packit Service cbd1e2
Packit Service cbd1e2
@enumerate
Packit Service cbd1e2
@item
Packit Service cbd1e2
Before connecting, set up pipes for bulk data transfer (using the
Packit Service cbd1e2
INPUT/OUTPUT commands, for example).  These are passed to the server
Packit Service cbd1e2
either by inheritance (using a pipe server) or by FD passing (using a
Packit Service cbd1e2
socket server).
Packit Service cbd1e2
Packit Service cbd1e2
@item
Packit Service cbd1e2
Then you need to connect to the server.  GPGME uses a pipe server, so
Packit Service cbd1e2
it just spawns a server process, which is a non-blocking operation.
Packit Service cbd1e2
FIXME: Currently, using a client with external event loop over a
Packit Service cbd1e2
socket connection is not supported.  It is easy to support (we just
Packit Service cbd1e2
need a variation of @code{assuan_socket_connect} which takes an
Packit Service cbd1e2
already connected socket FD and turns it into an Assuan context), so
Packit Service cbd1e2
if you need this let us know.
Packit Service cbd1e2
Packit Service cbd1e2
@item
Packit Service cbd1e2
After connecting, get the inbound status FD with
Packit Service cbd1e2
@code{assuan_get_active_fds} (the first one returned is the status
Packit Service cbd1e2
FD).  This FD can be duplicated if it is convenient (GPGME does this
Packit Service cbd1e2
to be able to close this FD and associated callback handlers without
Packit Service cbd1e2
disrupting Assuan's internals).
Packit Service cbd1e2
Packit Service cbd1e2
@item
Packit Service cbd1e2
Then register the Assuan inbound status FD and all bulk data FDs with
Packit Service cbd1e2
the I/O event mechanism.  In general, this requires setting up
Packit Service cbd1e2
callback handlers for these FDs and registering them with the main
Packit Service cbd1e2
event loop.
Packit Service cbd1e2
Packit Service cbd1e2
@item
Packit Service cbd1e2
When bulk data FDs become ready, you can simply perform the
Packit Service cbd1e2
corresponding read or write operations.  When the inbound status FD
Packit Service cbd1e2
becomes ready, you can receive the next server line with
Packit Service cbd1e2
assuan_read_line().
Packit Service cbd1e2
Packit Service cbd1e2
@item
Packit Service cbd1e2
You should close and unregister the bulk data FDs when you wrote all
Packit Service cbd1e2
data (for outbound FDs) or receive an EOF (for inbound FDs).  When you
Packit Service cbd1e2
receive an ERR from the server, or an OK for the final operation, you
Packit Service cbd1e2
can unregister the inbound status FD and call @code{assuan_release}.
Packit Service cbd1e2
Packit Service cbd1e2
@item
Packit Service cbd1e2
As noted above, all send operations on the outbound status FD are done
Packit Service cbd1e2
immediate with blocking.  In GPGME, this has never caused any problems.
Packit Service cbd1e2
Packit Service cbd1e2
@item
Packit Service cbd1e2
The @code{INQUIRE} function can be handled in two ways: If the
Packit Service cbd1e2
requested data is immediately available, the client can just send the
Packit Service cbd1e2
data blockingly.  If the requested data needs to be fetched from a
Packit Service cbd1e2
blocking source, a callback handler can be registered for the FD with
Packit Service cbd1e2
the main event loop.  GPGME does not support the @code{INQUIRE}
Packit Service cbd1e2
function, so we do not have any practical experience with this.
Packit Service cbd1e2
@end enumerate
Packit Service cbd1e2
Packit Service cbd1e2
Currently, the client can not cancel a pending operation gracefully.
Packit Service cbd1e2
It can, however, disconnect from the server at any time.  It is the
Packit Service cbd1e2
responsibility of the server to periodically send status messages to
Packit Service cbd1e2
the client to probe if the connection remains alive.
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@node External I/O Loop Server
Packit Service cbd1e2
@section External I/O event loops in the server.
Packit Service cbd1e2
Packit Service cbd1e2
Currently, no Assuan server exists which uses external I/O event
Packit Service cbd1e2
loops.  However, the following guidelines should lead to a usable
Packit Service cbd1e2
implementation:
Packit Service cbd1e2
Packit Service cbd1e2
@enumerate
Packit Service cbd1e2
@item
Packit Service cbd1e2
For socket servers: You can not use @code{assuan_accept}, so you
Packit Service cbd1e2
should just implement the bind/connect/listen/accept stage yourself.
Packit Service cbd1e2
You can register the listen FD with your main event loop, accept the
Packit Service cbd1e2
connection when it becomes ready, and finally call
Packit Service cbd1e2
@code{assuan_init_socket_server} with the final argument being
Packit Service cbd1e2
@code{ASSUAN_SOCKET_SERVER_ACCEPTED} to create an Assuan context for this
Packit Service cbd1e2
connection.  This way you can also handle multiple connections in
Packit Service cbd1e2
parallel.  The reference implementation for this approach is DirMngr.
Packit Service cbd1e2
Packit Service cbd1e2
For pipe servers: @code{assuan_init_pipe_server} creates an Assuan
Packit Service cbd1e2
context valid for the pipe FDs.
Packit Service cbd1e2
Packit Service cbd1e2
@item
Packit Service cbd1e2
Once you have a context for a single connection, you can get the
Packit Service cbd1e2
inbound status FD with @code{assuan_get_active_fds} (the first one
Packit Service cbd1e2
returned is the status FD).  This FD can be duplicated if it is
Packit Service cbd1e2
convenient.  Every time the inbound status FD is readable, you should
Packit Service cbd1e2
invoke the function @code{assuan_process_next} (see below) to process
Packit Service cbd1e2
the next incoming message.  @code{assuan_process_next} processes as
Packit Service cbd1e2
many status lines as can be received by a single @code{read}
Packit Service cbd1e2
operation.  When it returns, the inbound status FD may still be
Packit Service cbd1e2
readable, but Assuan does not check this.
Packit Service cbd1e2
Packit Service cbd1e2
The function @code{assuan_process_next} returns 0 if it can not make
Packit Service cbd1e2
progress reliably, and it returns true in @code{done} if the client
Packit Service cbd1e2
closed the connection.  See below for more information on this
Packit Service cbd1e2
function.
Packit Service cbd1e2
Packit Service cbd1e2
@item
Packit Service cbd1e2
The command will be dispatched by @code{assuan_process_next} just as
Packit Service cbd1e2
with @code{assuan_process}, however, you will want to implement the
Packit Service cbd1e2
command handlers in such a way that they do not block.  For example,
Packit Service cbd1e2
the command handler may just register the bulk data FDs with the main
Packit Service cbd1e2
event loop and return.
Packit Service cbd1e2
Packit Service cbd1e2
When the command is finished, irregardless if this happens directly in
Packit Service cbd1e2
the command handler or later, you must call @code{assuan_process_done}
Packit Service cbd1e2
with an appropriate error value (or 0 for success) to return an
Packit Service cbd1e2
appropriate status line to the client.  You can do this at the end of
Packit Service cbd1e2
the command handler, for example by ending it with @code{return
Packit Service cbd1e2
assuan_process_done (error_code);}.  Another possibility is to invoke
Packit Service cbd1e2
@code{assuan_process_done} from the place in the code which closes the
Packit Service cbd1e2
last active bulk FD registered with the main event loop for this
Packit Service cbd1e2
operation.
Packit Service cbd1e2
@end enumerate
Packit Service cbd1e2
Packit Service cbd1e2
It is not possible to use @code{assuan_inquire} in a command handler,
Packit Service cbd1e2
as this function blocks on receiving the inquired data from the
Packit Service cbd1e2
client.  Instead, the asynchronous version @code{assuan_inquire_ext}
Packit Service cbd1e2
needs to be used (see below), which invokes a callback when the client
Packit Service cbd1e2
provided the inquired data.  A typical usage would be for the command
Packit Service cbd1e2
handler to register a continuation with @code{assuan_inquire_ext} and
Packit Service cbd1e2
return 0.  Eventually, the continuation would be invoked by
Packit Service cbd1e2
@code{assuan_process_next} when the client data arrived.  The
Packit Service cbd1e2
continuation could complete the command and eventually call
Packit Service cbd1e2
@code{assuan_process_done}.
Packit Service cbd1e2
Packit Service cbd1e2
Cancellation is supported by returning an appropriate error value to
Packit Service cbd1e2
the client with @code{assuan_process_done}.  For long running
Packit Service cbd1e2
operations, the server should send progress status messages to the
Packit Service cbd1e2
client in regular intervals to notice when the client disconnects.
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun gpg_error_t assuan_process_next (@w{assuan_context_t @var{ctx}}, @w{int *@var{done}})
Packit Service cbd1e2
This is the same as @code{assuan_process} but the caller has to
Packit Service cbd1e2
provide the outer loop.  He should loop as long as the return code is
Packit Service cbd1e2
zero and @var{done} is false.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun gpg_error_t assuan_process_done (@w{assuan_context_t @var{ctx}}, @w{gpg_error_t @var{rc}})
Packit Service cbd1e2
Finish a pending command and return the error code @var{rc} to the
Packit Service cbd1e2
client.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@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}})
Packit Service cbd1e2
This is similar to @code{assuan_inquire} but the caller has to provide
Packit Service cbd1e2
the outer loop (using @code{assuan_process_next}).  The caller should
Packit Service cbd1e2
specify a continuation with @var{cb}, which receives @var{cb_data} as
Packit Service cbd1e2
its first argument, and the error value as well as the inquired data as
Packit Service cbd1e2
its remaining arguments.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@c
Packit Service cbd1e2
@c     U T I L I T I E S
Packit Service cbd1e2
@c
Packit Service cbd1e2
@node Utilities
Packit Service cbd1e2
@chapter Utility functions
Packit Service cbd1e2
Packit Service cbd1e2
@noindent
Packit Service cbd1e2
There are a lot of helper functions to make writing Assuan code easier.
Packit Service cbd1e2
Some of these functions provide information not available with the
Packit Service cbd1e2
general functions.
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun gpg_error_t assuan_write_status (@w{assuan_context_t @var{ctx}}, @w{const char *@var{keyword}}, @w{const char *@var{text}})
Packit Service cbd1e2
Packit Service cbd1e2
This is a convenience function for a server to send a status line.  You
Packit Service cbd1e2
need to pass it the @var{keyword} and the content of the status line in
Packit Service cbd1e2
@var{text}.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@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}})
Packit Service cbd1e2
Packit Service cbd1e2
A server may use this function to request specific data from a client.
Packit Service cbd1e2
This function sends an 'INQUIRE' command back to the client and
Packit Service cbd1e2
returns the client's response in a newly allocated buffer.  You need
Packit Service cbd1e2
to pass at least the server's context (@var{ctx}) and a description of
Packit Service cbd1e2
the required data (@var{keyword}).  All other parameters may be
Packit Service cbd1e2
@code{NULL} or @code{0}, but this is rarely useful.
Packit Service cbd1e2
Packit Service cbd1e2
On success the result is stored in a newly allocated buffer stored at
Packit Service cbd1e2
@var{r_buffer}. The length of the data is stored at @var{r_length}.
Packit Service cbd1e2
If @var{maxlen} has not been given as @code{0}, it specifies an upper
Packit Service cbd1e2
size limit of the expected data.  If the client returns too much
Packit Service cbd1e2
data the function fails and an error with the error code
Packit Service cbd1e2
@code{GPG_ERR_ASS_TOO_MUCH_DATA} will be returned.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun FILE* assuan_get_data_fp (@w{assuan_context_t @var{ctx}})
Packit Service cbd1e2
Packit Service cbd1e2
Return a stdio stream for the Assuan context @var{ctx}.  This stream may
Packit Service cbd1e2
then be used for data output (assuan_write_data).  The stream is valid
Packit Service cbd1e2
until the end of the current handler.  Calling @code{fclose} for that stream is
Packit Service cbd1e2
not required.  Assuan does all the buffering needed to insert the status
Packit Service cbd1e2
line as well as the required line wrapping and quoting for data lines.
Packit Service cbd1e2
Packit Service cbd1e2
This function is only available on systems supporting either
Packit Service cbd1e2
@code{funopen} or @code{fopencookie}. If it is not supported @code{NULL}
Packit Service cbd1e2
is returned and @code{errno} is set to @code{ENOSYS}.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun gpg_error_t assuan_set_okay_line (@w{assuan_context_t @var{ctx}}, @w{const char *@var{line}})
Packit Service cbd1e2
Packit Service cbd1e2
Set the text used for the next @code{OK} response to @var{line}.  This is
Packit Service cbd1e2
sometimes useful to send additional human readable information along
Packit Service cbd1e2
with the OK line.  The string is automatically reset at the end of the
Packit Service cbd1e2
current handler.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun gpg_error_t assuan_command_parse_fd (@w{assuan_context_t @var{ctx}}, @w{char *@var{line}}, @w{assuan_fd_t *@var{rfd}})
Packit Service cbd1e2
Packit Service cbd1e2
This is the core of the default @code{INPUT} and @code{OUTPUT}
Packit Service cbd1e2
handler.  It may be used in custom commands as well to negotiate a
Packit Service cbd1e2
file descriptor.  If @var{line} contains @code{FD=@var{n}}, it returns
Packit Service cbd1e2
@var{n} in @var{rfd} assuming a local file descriptor.  If @var{line}
Packit Service cbd1e2
contains just @code{FD} it returns a file descriptor at @var{rfd};
Packit Service cbd1e2
this file descriptor needs to have been sent by the client right
Packit Service cbd1e2
before using @code{assuan_sendfd}.
Packit Service cbd1e2
Packit Service cbd1e2
On W32 systems the returned file descriptor is a system handle and not a
Packit Service cbd1e2
libc low level I/O file descriptor.  Thus applications need to use
Packit Service cbd1e2
@code{_open_osfhandle} before they can pass this descriptor to standard
Packit Service cbd1e2
functions like @code{fdopen} or @code{dup}.
Packit Service cbd1e2
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun @w{const char *} assuan_get_command_name (@w{assuan_context_t @var{ctx}})
Packit Service cbd1e2
Packit Service cbd1e2
Return the name of the command currently processed by a handler.
Packit Service cbd1e2
The returned string is valid until the next call to an Assuan
Packit Service cbd1e2
function on the same context.  Returns @code{NULL} if no handler is
Packit Service cbd1e2
executed or the command is not known.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun assuan_fd_t assuan_get_input_fd (@w{assuan_context_t @var{ctx}})
Packit Service cbd1e2
Packit Service cbd1e2
Return the file descriptor sent by the client using the last @code{INPUT}
Packit Service cbd1e2
command.  Returns @code{ASSUAN_INVALID_FD} if no file descriptor is available.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun assuan_fd_t assuan_get_output_fd (@w{assuan_context_t @var{ctx}})
Packit Service cbd1e2
Packit Service cbd1e2
Return the file descriptor sent by the client using the last
Packit Service cbd1e2
@code{OUTPUT} command.  Returns @code{ASSUAN_INVALID_FD} if no file descriptor is
Packit Service cbd1e2
available.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun gpg_error_t assuan_close_input_fd (@w{assuan_context_t @var{ctx}})
Packit Service cbd1e2
Packit Service cbd1e2
Close the file descriptor set by the last @code{INPUT} command.  This
Packit Service cbd1e2
function has the advantage over a simple @code{close} that it can do
Packit Service cbd1e2
some sanity checks and make sure that a following
Packit Service cbd1e2
@code{assuan_get_input_fd} won't return an already closed descriptor.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun gpg_error_t assuan_close_output_fd (@w{assuan_context_t @var{ctx}})
Packit Service cbd1e2
Packit Service cbd1e2
Close the file descriptor set by the last @code{OUTPUT} command.  This
Packit Service cbd1e2
function has the advantage over a simple @code{close} that it can do
Packit Service cbd1e2
some sanity checks and make sure that a following
Packit Service cbd1e2
@code{assuan_get_input_fd} won't return an already closed descriptor.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun gpg_error_t assuan_set_error (@w{assuan_context_t @var{ctx}}, @w{gpg_error_t @var{err}}, @w{const char *@var{text}})
Packit Service cbd1e2
This is a helper to provide a more descriptive error text with @code{ERR}
Packit Service cbd1e2
lines.  For this to work, the text needs to be stored in the context
Packit Service cbd1e2
@var{ctx} while still being in the command handler.  This function is
Packit Service cbd1e2
commonly called this way
Packit Service cbd1e2
@smallexample
Packit Service cbd1e2
  return assuan_set_error (ctx, err, "commands needs 5 arguments");
Packit Service cbd1e2
@end smallexample
Packit Service cbd1e2
The value @var{err} is passed through and thus the return value of the
Packit Service cbd1e2
command handler in the example.  The provided text further explains
Packit Service cbd1e2
that error to humans.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun pid_t assuan_get_pid (@w{assuan_context_t @var{ctx}})
Packit Service cbd1e2
Packit Service cbd1e2
This function returns the pid of the connected connected peer.  If
Packit Service cbd1e2
that pid is not known @code{ASSUAN_INVALID_PID} is returned.  Note
Packit Service cbd1e2
that it is not always possible to learn the pid of the other
Packit Service cbd1e2
process. For a pipe based server the client knows it instantly and a
Packit Service cbd1e2
mechanism is in place to let the server learn it.  For socket based
Packit Service cbd1e2
servers the pid is only available on systems providing the
Packit Service cbd1e2
@code{SO_PEERCRED} socket option @footnote{to our knowledge only the
Packit Service cbd1e2
Linux kernel has this feature}.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@deftp {Data type} {assuan_peercred_t}
Packit Service cbd1e2
This structure is used to store the peer credentials.  The available
Packit Service cbd1e2
members depend on the operating system.
Packit Service cbd1e2
Packit Service cbd1e2
@table @code
Packit Service cbd1e2
@item pid_t pid
Packit Service cbd1e2
The process ID of the peer.
Packit Service cbd1e2
Packit Service cbd1e2
@item uid_t uid
Packit Service cbd1e2
The user ID of the peer process.
Packit Service cbd1e2
Packit Service cbd1e2
@item gid_t gid
Packit Service cbd1e2
The group ID of the peer process.
Packit Service cbd1e2
@end table
Packit Service cbd1e2
@end deftp
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun gpg_error_t assuan_get_peercred (@w{assuan_context_t @var{ctx}}, @w{assuan_peercred_t *@var{peercred}})
Packit Service cbd1e2
Return user credentials of the peer. This will work only on certain
Packit Service cbd1e2
systems and only when connected over a socket.  On success, a pointer
Packit Service cbd1e2
to the peer credentials is stored in @var{peercred}.  The information
Packit Service cbd1e2
is only valid as long as the state of the connection is unchanged (at
Packit Service cbd1e2
least until the next assuan call to the same context).
Packit Service cbd1e2
Packit Service cbd1e2
As of now only the server is able to retrieve this information.  Note,
Packit Service cbd1e2
that for getting the pid of the peer @code{assuan_get_pid} is usually
Packit Service cbd1e2
better suited.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@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}})
Packit Service cbd1e2
Packit Service cbd1e2
Return all active file descriptors for the context @var{ctx}.  This
Packit Service cbd1e2
function can be used to select on the file descriptors and to call
Packit Service cbd1e2
@code{assuan_process_next} if there is an active one.  The first
Packit Service cbd1e2
descriptor in the array is the one used for the command connection.
Packit Service cbd1e2
Currently @var{what} needs to be @code{0} to return descriptors used for
Packit Service cbd1e2
reading, @code{1} will eventually be used to return descriptors used for
Packit Service cbd1e2
writing.  @var{fdarray} is an array of integers provided by the caller;
Packit Service cbd1e2
@var{fdarraysize} gives the size of that array.
Packit Service cbd1e2
Packit Service cbd1e2
On success the number of active descriptors are returned.  These active
Packit Service cbd1e2
descriptors are then stored in @var{fdarray}.  On error @code{-1} is
Packit Service cbd1e2
returned; the most likely reason for this is a too small @var{fdarray}.
Packit Service cbd1e2
Packit Service cbd1e2
Note that on W32 systems the returned file descriptor is a system handle
Packit Service cbd1e2
and not a libc low level I/O file descriptor.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun int assuan_pending_line (@w{assuan_context_t @var{ctx}})
Packit Service cbd1e2
A call to this function return true if a full line has been buffered and
Packit Service cbd1e2
thus an entire assuan line may be read without triggering any actual
Packit Service cbd1e2
I/O.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@c
Packit Service cbd1e2
@c     S O C K E T   W R A P P E R S
Packit Service cbd1e2
@c
Packit Service cbd1e2
@node Socket wrappers
Packit Service cbd1e2
@chapter Socket wrapper functions
Packit Service cbd1e2
Packit Service cbd1e2
@noindent
Packit Service cbd1e2
It is sometimes useful to support Unix domain sockets on Windows.  To do
Packit Service cbd1e2
this in a portable way, Assuan provides a set of wrapper functions which
Packit Service cbd1e2
may be used on any system but will enhance Windows to support these
Packit Service cbd1e2
socket types.  The actual implementation is based on local TCP sockets
Packit Service cbd1e2
and fully transparent for the client.  Server code needs to utilize two
Packit Service cbd1e2
extra functions to check the permissions.
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun gpg_error_t assuan_sock_init (void)
Packit Service cbd1e2
Initialize the socket wrappers.  Must be called once at startup if any
Packit Service cbd1e2
of the socket wrapper functions are used.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun gpg_error_t assuan_sock_deinit (void)
Packit Service cbd1e2
Deinitialize the socket wrappers.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun int assuan_sock_close (@w{assuan_fd_t @var{fd}})
Packit Service cbd1e2
Wrapper for close which does a closesocket on Windows if needed.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun assuan_fd_t assuan_sock_new (@w{int @var{domain}}, @w{int @var{type}}, @w{int @var{proto}});
Packit Service cbd1e2
Wrapper around socket.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun int assuan_sock_connect (@w{assuan_fd_t @var{sockfd}}, @
Packit Service cbd1e2
         @w{struct sockaddr *@var{addr}}, @
Packit Service cbd1e2
         @w{int @var{addrlen}})
Packit Service cbd1e2
Packit Service cbd1e2
Wrapper around connect.  For Unix domain sockets under Windows this
Packit Service cbd1e2
function also does a write immediately after the the connect to send the
Packit Service cbd1e2
nonce as read from the socket's file.  Under Unix this function check
Packit Service cbd1e2
whether the socket file is a redirection file and connects to the
Packit Service cbd1e2
redirected socket instead; see @code{assuan_sock_set_sockaddr_un} for
Packit Service cbd1e2
details on the redirection file format.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun int assuan_sock_connect_byname (@w{const char * @var{host}}, @
Packit Service cbd1e2
         @w{unsigned short @var{port}}, @
Packit Service cbd1e2
         @w{int @var{reserved}}, @
Packit Service cbd1e2
         @w{const char *@var{credentials}}, @
Packit Service cbd1e2
         @w{unsigned int @var{flags}})
Packit Service cbd1e2
Packit Service cbd1e2
Directly connect to @var{port} on @var{host} given as a name.  The
Packit Service cbd1e2
current implementation requires that @var{flags} has either
Packit Service cbd1e2
@code{ASSUAN_SOCK_SOCKS} or @code{ASSUAN_SOCK_TOR} set.  On success a
Packit Service cbd1e2
new TCP STREAM socket is returned; on error @code{ASSUAN_INVALID_FD}
Packit Service cbd1e2
and ERRNO set.  If @var{credentials} is not @code{NULL}, it is a
Packit Service cbd1e2
string used for password based SOCKS authentication.  Username and
Packit Service cbd1e2
password are separated by a colon. @var{reserved} should be 0.  To
Packit Service cbd1e2
test whether the proxy is available @var{host} and @var{port} may be
Packit Service cbd1e2
given as NULL/0:  If the proxy is available the function returns a
Packit Service cbd1e2
valid socket which is in the state after credentials sub-negotiation.
Packit Service cbd1e2
The caller now knows that the SOCKS proxy is available and has been
Packit Service cbd1e2
authenticated; normally the caller closes the socket then.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun int assuan_sock_bind ( @
Packit Service cbd1e2
        @w{assuan_fd_t @var{sockfd}}, @
Packit Service cbd1e2
        @w{struct sockaddr *@var{addr}}, @
Packit Service cbd1e2
        @w{int @var{addrlen}})
Packit Service cbd1e2
Packit Service cbd1e2
Wrapper around bind.  Under Windows this creates a file and writes the
Packit Service cbd1e2
port number and a random nonce to this file.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun int assuan_sock_set_sockaddr_un ( @
Packit Service cbd1e2
        @w{const char *@var{fname}}, @
Packit Service cbd1e2
        @w{struct sockaddr *@var{addr}}, @
Packit Service cbd1e2
        @w{int *@var{r_redirected}})
Packit Service cbd1e2
Packit Service cbd1e2
This is a helper function to initialize the Unix socket domain address
Packit Service cbd1e2
structure @var{addr} and store the file name @var{fname} there.  If
Packit Service cbd1e2
@var{r_redirected} is not NULL the function checks whether @var{fname}
Packit Service cbd1e2
already exists, is a regular file, and not a socket.  In that case
Packit Service cbd1e2
@var{fname} is read to see whether this is a redirection to a socket
Packit Service cbd1e2
file.  If that is the case 1 is stored at @var{r_redirected}.  If the
Packit Service cbd1e2
file does not look like a redirection file 0 will be stored there and
Packit Service cbd1e2
@var{fname} will be used in the regular way.
Packit Service cbd1e2
Packit Service cbd1e2
The format of a redirection file is
Packit Service cbd1e2
Packit Service cbd1e2
@example
Packit Service cbd1e2
%Assuan%
Packit Service cbd1e2
socket=@var{name}
Packit Service cbd1e2
@end example
Packit Service cbd1e2
Packit Service cbd1e2
With @var{name} being is the actual socket to use.  No white spaces
Packit Service cbd1e2
are allowed, both lines must be terminated by a single linefeed, and
Packit Service cbd1e2
extra lines are not allowed.  Environment variables are interpreted in
Packit Service cbd1e2
@var{name} if given in @code{$@{VAR@}} notation.  No escape characters
Packit Service cbd1e2
are defined; if the string @code{$@{} shall be used in file name, an
Packit Service cbd1e2
environment variable with that content may be used.  The length of the
Packit Service cbd1e2
redirection file is limited to 511 bytes which is more than sufficient
Packit Service cbd1e2
for any known implementation of Unix domain sockets.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun int assuan_sock_get_nonce ( @
Packit Service cbd1e2
        @w{struct sockaddr *@var{addr}}, @
Packit Service cbd1e2
        @w{int @var{addrlen}}, @
Packit Service cbd1e2
        @w{assuan_sock_nonce_t *@var{nonce}})
Packit Service cbd1e2
Packit Service cbd1e2
This is used by the server after a bind to return the random nonce.  To
Packit Service cbd1e2
keep the code readable this may also be used on POSIX system.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun int assuan_sock_check_nonce ( @
Packit Service cbd1e2
        @w{assuan_fd_t @var{fd}}, @
Packit Service cbd1e2
        @w{assuan_sock_nonce_t *@var{nonce}})
Packit Service cbd1e2
Packit Service cbd1e2
If the option @code{ASSUAN_SOCKET_SERVER_ACCEPTED} has been used,
Packit Service cbd1e2
Libassuan has no way to check the nonce of the server.  Thus an explicit
Packit Service cbd1e2
check of the saved nonce using this function is required.  If this
Packit Service cbd1e2
function fails the server should immediately drop the connection.  This
Packit Service cbd1e2
function may not be used if Libassuan does the accept call itself
Packit Service cbd1e2
(i.e. @code{ASSUAN_SOCKET_SERVER_ACCEPTED} has not been used) because
Packit Service cbd1e2
in this case Libassuan calls this function internally.  See also
Packit Service cbd1e2
@code{assuan_set_sock_nonce}.
Packit Service cbd1e2
Packit Service cbd1e2
Actually this mechanism is only required on Windows but for cleanness of
Packit Service cbd1e2
code it may be used on POSIX systems as well, where this function is
Packit Service cbd1e2
a nop.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
To control certain properties of the wrapper two additional functions
Packit Service cbd1e2
are provided:
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun int assuan_sock_set_flag ( @
Packit Service cbd1e2
                @w{assuan_fd_t @var{fd}}, @
Packit Service cbd1e2
                @w{const char *@var{name}}, @
Packit Service cbd1e2
                @w{int @var{value}})
Packit Service cbd1e2
Packit Service cbd1e2
Set the flags @var{name} for socket @var{fd} to @var{value}.  See
Packit Service cbd1e2
below for a list of valid names.  Returns 0 on success; on failure
Packit Service cbd1e2
sets ERRNO and returns -1.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
@deftypefun int assuan_sock_get_flag ( @
Packit Service cbd1e2
                @w{assuan_fd_t @var{fd}}, @
Packit Service cbd1e2
                @w{const char *@var{name}}, @
Packit Service cbd1e2
                @w{int *@var{r_value}})
Packit Service cbd1e2
Packit Service cbd1e2
Store the current value of the flag @var{name} for socket @var{fd} at
Packit Service cbd1e2
@var{r_value}.  See below for a list of valid names.  Returns 0 on
Packit Service cbd1e2
success; on failure sets ERRNO and returns -1.
Packit Service cbd1e2
@end deftypefun
Packit Service cbd1e2
Packit Service cbd1e2
The supported flags are:
Packit Service cbd1e2
Packit Service cbd1e2
@table @code
Packit Service cbd1e2
@item cygwin
Packit Service cbd1e2
This flag has an effect only on Windows.  If the value is 1, the
Packit Service cbd1e2
socket is set into Cygwin mode so that Cygwin clients can connect to
Packit Service cbd1e2
such a socket.  This flag needs to be set before a bind and should not
Packit Service cbd1e2
be changed during the lifetime of the socket.  There is no need to set
Packit Service cbd1e2
this flag for connecting to a Cygwin style socket because no state is
Packit Service cbd1e2
required at the client.  On non-Windows platforms setting this flag is
Packit Service cbd1e2
ignored, reading the flag always returns a value of 0.
Packit Service cbd1e2
Packit Service cbd1e2
@item tor-mode
Packit Service cbd1e2
@itemx socks
Packit Service cbd1e2
If @var{value} is 1 globally enable SOCKS5 mode for new connections
Packit Service cbd1e2
using IPv6 or IPv4. @var{fd} must be set to @code{ASSUAN_INVALID_FD} A
Packit Service cbd1e2
future extension may allow to disable SOCKS5 mode for a specified
Packit Service cbd1e2
socket but globally disabling SOCKS5 mode is not possible.  Using the
Packit Service cbd1e2
flag ``tor-mode'' expects the SOCKS5 proxy to listen on port 9050, the
Packit Service cbd1e2
flag ``socks'' expects the proxy to listen on port 1080.
Packit Service cbd1e2
Packit Service cbd1e2
Connections to the loopback address are not routed though the SOCKS
Packit Service cbd1e2
proxy.  UDP requests are not supported at all.  The proxy will be
Packit Service cbd1e2
connected at address 127.0.0.1; an IPv6 connection to the proxy is not
Packit Service cbd1e2
yet supported.
Packit Service cbd1e2
Packit Service cbd1e2
@end table
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@c ---------------------------------------------------------------------
Packit Service cbd1e2
@c Legal BS
Packit Service cbd1e2
@c ---------------------------------------------------------------------
Packit Service cbd1e2
Packit Service cbd1e2
@include lgpl.texi
Packit Service cbd1e2
@include gpl.texi
Packit Service cbd1e2
Packit Service cbd1e2
Packit Service cbd1e2
@c ---------------------------------------------------------------------
Packit Service cbd1e2
@c Indexes
Packit Service cbd1e2
@c ---------------------------------------------------------------------
Packit Service cbd1e2
Packit Service cbd1e2
@node Index
Packit Service cbd1e2
@unnumbered Index
Packit Service cbd1e2
Packit Service cbd1e2
@printindex cp
Packit Service cbd1e2
Packit Service cbd1e2
@c ---------------------------------------------------------------------
Packit Service cbd1e2
@c Epilogue
Packit Service cbd1e2
@c ---------------------------------------------------------------------
Packit Service cbd1e2
Packit Service cbd1e2
@bye
Packit Service cbd1e2
Packit Service cbd1e2