1 /* assuan-defs.c - Internal definitions to Assuan
2 * Copyright (C) 2001, 2002, 2004, 2005, 2007, 2008 Free Software Foundation, Inc.
4 * This file is part of Assuan.
6 * Assuan is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * Assuan is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
25 #include <sys/types.h>
26 #ifndef HAVE_W32_SYSTEM
27 #include <sys/socket.h>
36 #ifndef HAVE_W32_SYSTEM
42 #ifdef HAVE_W32_SYSTEM
43 /* Not needed anymore because the current mingw32 defines this in
45 /* typedef int ssize_t; */
47 /* Missing W32 functions */
48 int putc_unlocked (int c
, FILE *stream
);
49 void * memrchr (const void *block
, int c
, size_t size
);
50 char * stpcpy (char *dest
, const char *src
);
53 #define LINELENGTH ASSUAN_LINELENGTH
59 int (*handler
)(assuan_context_t
, char *line
);
63 /* A structure to dispatch I/O functions. All these functions need to
64 return 0 on success and set ERRNO on failure. */
67 /* Routine to read from input_fd. */
68 ssize_t (*readfnc
) (assuan_context_t
, void *, size_t);
69 /* Routine to write to output_fd. */
70 ssize_t (*writefnc
) (assuan_context_t
, const void *, size_t);
71 /* Send a file descriptor. */
72 assuan_error_t (*sendfd
) (assuan_context_t
, assuan_fd_t
);
73 /* Receive a file descriptor. */
74 assuan_error_t (*receivefd
) (assuan_context_t
, assuan_fd_t
*);
78 /* The global variable with the optional hook fucntions. */
79 extern struct assuan_io_hooks _assuan_io_hooks
;
82 /* The context we use with most functions. */
83 struct assuan_context_s
85 assuan_error_t err_no
;
87 int os_errno
; /* Last system error number used with certain
90 /* Context specific flags (cf. assuan_flag_t). */
93 unsigned int no_waitpid
:1; /* See ASSUAN_NO_WAITPID. */
97 int is_server
; /* Set if this is context belongs to a server */
102 /* The following members are used by assuan_inquire_ext. */
103 int (*inquire_cb
) (void *cb_data
, int rc
, unsigned char *buf
, size_t len
);
104 void *inquire_cb_data
;
105 void *inquire_membuf
;
108 char *okay_line
; /* See assuan_set_okay_line() */
110 void *user_pointer
; /* For assuan_get_pointer and assuan_set_pointer (). */
117 char line
[LINELENGTH
];
118 int linelen
; /* w/o CR, LF - might not be the same as
119 strlen(line) due to embedded nuls. However a nul
120 is always written at this pos. */
122 char line
[LINELENGTH
];
124 int pending
; /* i.e. at least one line is available in the attic */
132 char line
[LINELENGTH
];
138 int pipe_mode
; /* We are in pipe mode, i.e. we can handle just one
139 connection and must terminate then. */
140 pid_t pid
; /* The pid of the peer. */
141 assuan_fd_t listen_fd
; /* The fd we are listening on (used by
143 assuan_sock_nonce_t listen_nonce
; /* Used with LISTEN_FD. */
144 assuan_fd_t connected_fd
; /* helper */
147 int valid
; /* Whether this structure has valid information. */
148 #ifdef HAVE_SO_PEERCRED
149 pid_t pid
; /* The pid of the peer. */
150 uid_t uid
; /* The uid of the peer. */
151 gid_t gid
; /* The gid of the peer. */
152 #endif /* HAVE_SO_PEERCRED */
155 /* Used for Unix domain sockets. */
156 struct sockaddr_un myaddr
;
157 struct sockaddr_un serveraddr
;
159 /* Structure used for unix domain socket buffering. FIXME: We don't
160 use datagrams anymore thus we could get away with a simpler
161 buffering approach. */
163 void *buffer
; /* Malloced buffer. */
164 int bufferallocated
; /* Memory allocated. */
165 int bufferoffset
; /* Offset of start of buffer. */
166 int buffersize
; /* Bytes buffered. */
168 assuan_fd_t pendingfds
[5]; /* Array to save received descriptors. */
169 int pendingfdscount
; /* Number of received descriptors. */
172 void (*deinit_handler
)(assuan_context_t
);
173 int (*accept_handler
)(assuan_context_t
);
174 int (*finish_handler
)(assuan_context_t
);
176 struct cmdtbl_s
*cmdtbl
;
177 size_t cmdtbl_used
; /* used entries */
178 size_t cmdtbl_size
; /* allocated size of table */
180 void (*bye_notify_fnc
)(assuan_context_t
);
181 void (*reset_notify_fnc
)(assuan_context_t
);
182 void (*cancel_notify_fnc
)(assuan_context_t
);
183 int (*option_handler_fnc
)(assuan_context_t
,const char*, const char*);
184 void (*input_notify_fnc
)(assuan_context_t
, const char *);
185 void (*output_notify_fnc
)(assuan_context_t
, const char *);
187 /* This function is called right after a command has been processed.
188 It may be used to command related cleanup. */
189 void (*post_cmd_notify_fnc
)(assuan_context_t
, int);
191 /* Called right before the actual command. Should return 0 on success or an
192 * error code which will abort the command. */
193 int (*pre_cmd_notify_fnc
)(assuan_context_t
, const char *);
195 /* If set, this is called right before logging an I/O line. With
196 DIRECTION set to 1 it is called for an output oeration; 0 means
197 an input operation. If bit 0 is set in the return value, the
198 logging of the line will be suppressed. With bit 1 set, the
199 entire line will be ignored. */
200 unsigned int (*io_monitor
)(assuan_context_t ctx
,
205 assuan_fd_t input_fd
; /* Set by the INPUT command. */
206 assuan_fd_t output_fd
; /* Set by the OUTPUT command. */
209 struct assuan_io
*io
;
212 /*-- assuan-pipe-server.c --*/
213 int _assuan_new_context (assuan_context_t
*r_ctx
);
214 void _assuan_release_context (assuan_context_t ctx
);
216 /*-- assuan-uds.c --*/
217 void _assuan_uds_close_fds (assuan_context_t ctx
);
218 void _assuan_uds_deinit (assuan_context_t ctx
);
219 void _assuan_init_uds_io (assuan_context_t ctx
);
222 /*-- assuan-handler.c --*/
223 int _assuan_register_std_commands (assuan_context_t ctx
);
225 /*-- assuan-buffer.c --*/
226 assuan_error_t
_assuan_read_line (assuan_context_t ctx
);
227 int _assuan_cookie_write_data (void *cookie
, const char *buffer
, size_t size
);
228 int _assuan_cookie_write_flush (void *cookie
);
229 assuan_error_t
_assuan_write_line (assuan_context_t ctx
, const char *prefix
,
230 const char *line
, size_t len
);
232 /*-- assuan-client.c --*/
233 assuan_error_t
_assuan_read_from_server (assuan_context_t ctx
,
234 int *okay
, int *off
);
236 /*-- assuan-error.c --*/
238 /*-- assuan-inquire.c --*/
239 int _assuan_inquire_ext_cb (assuan_context_t ctx
);
240 void _assuan_inquire_release (assuan_context_t ctx
);
242 /* Map error codes as used in this implementation to the libgpg-error
244 assuan_error_t
_assuan_error (int oldcode
);
245 /* Check if ERR means EAGAIN. */
246 int _assuan_error_is_eagain (assuan_error_t err
);
248 /* Extract the error code from A. This works for both the old and the
249 new style error codes. This needs to be used whenever an error
251 #define err_code(a) ((a) & 0x00ffffff)
253 /* Check whether A is the erro code for EOF. We allow for old and new
254 style EOF error codes here. */
255 #define err_is_eof(a) ((a) == (-1) || err_code (a) == 16383)
259 /*-- assuan-util.c --*/
260 void *_assuan_malloc (size_t n
);
261 void *_assuan_calloc (size_t n
, size_t m
);
262 void *_assuan_realloc (void *p
, size_t n
);
263 void _assuan_free (void *p
);
265 #define xtrymalloc(a) _assuan_malloc ((a))
266 #define xtrycalloc(a,b) _assuan_calloc ((a),(b))
267 #define xtryrealloc(a,b) _assuan_realloc((a),(b))
268 #define xfree(a) _assuan_free ((a))
270 #define set_error(c,e,t) \
271 assuan_set_error ((c), _assuan_error (ASSUAN_ ## e), (t))
273 #ifdef HAVE_W32_SYSTEM
274 const char *_assuan_w32_strerror (int ec
);
275 #define w32_strerror(e) _assuan_w32_strerror ((e))
276 int _assuan_gpg_strerror_r (unsigned int err
, char *buf
, size_t buflen
);
277 const char *_assuan_gpg_strsource (unsigned int err
);
278 #endif /*HAVE_W32_SYSTEM*/
281 /*-- assuan-logging.c --*/
282 void _assuan_set_default_log_stream (FILE *fp
);
284 void _assuan_log_printf (const char *format
, ...)
285 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 )
286 __attribute__ ((format (printf
,1,2)))
289 void _assuan_log_print_buffer (FILE *fp
, const void *buffer
, size_t length
);
290 void _assuan_log_sanitized_string (const char *string
);
293 /*-- assuan-io.c --*/
294 pid_t
_assuan_waitpid (pid_t pid
, int *status
, int options
);
296 ssize_t
_assuan_simple_read (assuan_context_t ctx
, void *buffer
, size_t size
);
297 ssize_t
_assuan_simple_write (assuan_context_t ctx
, const void *buffer
,
299 ssize_t
_assuan_io_read (assuan_fd_t fd
, void *buffer
, size_t size
);
300 ssize_t
_assuan_io_write (assuan_fd_t fd
, const void *buffer
, size_t size
);
301 #ifdef HAVE_W32_SYSTEM
302 int _assuan_simple_sendmsg (assuan_context_t ctx
, void *msg
);
303 int _assuan_simple_recvmsg (assuan_context_t ctx
, void *msg
);
305 ssize_t
_assuan_simple_sendmsg (assuan_context_t ctx
, struct msghdr
*msg
);
306 ssize_t
_assuan_simple_recvmsg (assuan_context_t ctx
, struct msghdr
*msg
);
309 void _assuan_usleep (unsigned int usec
);
312 /*-- assuan-socket.c --*/
313 int _assuan_close (assuan_fd_t fd
);
314 assuan_fd_t
_assuan_sock_new (int domain
, int type
, int proto
);
315 int _assuan_sock_connect (assuan_fd_t sockfd
,
316 struct sockaddr
*addr
, int addrlen
);
317 int _assuan_sock_bind (assuan_fd_t sockfd
, struct sockaddr
*addr
, int addrlen
);
318 int _assuan_sock_get_nonce (struct sockaddr
*addr
, int addrlen
,
319 assuan_sock_nonce_t
*nonce
);
320 int _assuan_sock_check_nonce (assuan_fd_t fd
, assuan_sock_nonce_t
*nonce
);
321 #ifdef HAVE_W32_SYSTEM
322 int _assuan_sock_wsa2errno (int err
);
325 #ifdef HAVE_FOPENCOOKIE
326 /* We have to implement funopen in terms of glibc's fopencookie. */
327 FILE *_assuan_funopen(void *cookie
,
328 cookie_read_function_t
*readfn
,
329 cookie_write_function_t
*writefn
,
330 cookie_seek_function_t
*seekfn
,
331 cookie_close_function_t
*closefn
);
332 #define funopen(a,r,w,s,c) _assuan_funopen ((a), (r), (w), (s), (c))
333 #endif /*HAVE_FOPENCOOKIE*/
335 /* Prototypes for replacement functions. */
337 void *memrchr (const void *block
, int c
, size_t size
);
340 char *stpcpy (char *dest
, const char *src
);
343 #define setenv _assuan_setenv
344 #define unsetenv _assuan_unsetenv
345 #define clearenv _assuan_clearenv
346 int setenv (const char *name
, const char *value
, int replace
);
348 #ifndef HAVE_PUTC_UNLOCKED
349 int putc_unlocked (int c
, FILE *stream
);
352 #define DIM(v) (sizeof(v)/sizeof((v)[0]))
353 #define DIMof(type,member) DIM(((type *)0)->member)
357 #define SOCKET2HANDLE(s) ((void *)(s))
358 #define HANDLE2SOCKET(h) ((unsigned int)(h))
360 #define SOCKET2HANDLE(s) (s)
361 #define HANDLE2SOCKET(h) (h)
365 #endif /*ASSUAN_DEFS_H*/