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 (). */
111 void (*user_finish_handler
)(assuan_context_t
); /* For assuan_set_finish_handler(). */
118 char line
[LINELENGTH
];
119 int linelen
; /* w/o CR, LF - might not be the same as
120 strlen(line) due to embedded nuls. However a nul
121 is always written at this pos. */
123 char line
[LINELENGTH
];
125 int pending
; /* i.e. at least one line is available in the attic */
133 char line
[LINELENGTH
];
139 int pipe_mode
; /* We are in pipe mode, i.e. we can handle just one
140 connection and must terminate then. */
141 pid_t pid
; /* The pid of the peer. */
142 assuan_fd_t listen_fd
; /* The fd we are listening on (used by
144 assuan_sock_nonce_t listen_nonce
; /* Used with LISTEN_FD. */
145 assuan_fd_t connected_fd
; /* helper */
148 int valid
; /* Whether this structure has valid information. */
149 #ifdef HAVE_SO_PEERCRED
150 pid_t pid
; /* The pid of the peer. */
151 uid_t uid
; /* The uid of the peer. */
152 gid_t gid
; /* The gid of the peer. */
153 #endif /* HAVE_SO_PEERCRED */
156 /* Used for Unix domain sockets. */
157 struct sockaddr_un myaddr
;
158 struct sockaddr_un serveraddr
;
160 /* Structure used for unix domain socket buffering. FIXME: We don't
161 use datagrams anymore thus we could get away with a simpler
162 buffering approach. */
164 void *buffer
; /* Malloced buffer. */
165 int bufferallocated
; /* Memory allocated. */
166 int bufferoffset
; /* Offset of start of buffer. */
167 int buffersize
; /* Bytes buffered. */
169 assuan_fd_t pendingfds
[5]; /* Array to save received descriptors. */
170 int pendingfdscount
; /* Number of received descriptors. */
173 void (*deinit_handler
)(assuan_context_t
);
174 int (*accept_handler
)(assuan_context_t
);
175 int (*finish_handler
)(assuan_context_t
);
177 struct cmdtbl_s
*cmdtbl
;
178 size_t cmdtbl_used
; /* used entries */
179 size_t cmdtbl_size
; /* allocated size of table */
181 void (*bye_notify_fnc
)(assuan_context_t
);
182 void (*reset_notify_fnc
)(assuan_context_t
);
183 void (*cancel_notify_fnc
)(assuan_context_t
);
184 int (*option_handler_fnc
)(assuan_context_t
,const char*, const char*);
185 void (*input_notify_fnc
)(assuan_context_t
, const char *);
186 void (*output_notify_fnc
)(assuan_context_t
, const char *);
188 /* This function is called right after a command has been processed.
189 It may be used to command related cleanup. */
190 void (*post_cmd_notify_fnc
)(assuan_context_t
, int);
192 /* If set, this is called right before logging an I/O line. With
193 DIRECTION set to 1 it is called for an output oeration; 0 means
194 an input operation. If bit 0 is set in the return value, the
195 logging of the line will be suppressed. With bit 1 set, the
196 entire line will be ignored. */
197 unsigned int (*io_monitor
)(assuan_context_t ctx
,
202 assuan_fd_t input_fd
; /* Set by the INPUT command. */
203 assuan_fd_t output_fd
; /* Set by the OUTPUT command. */
206 struct assuan_io
*io
;
209 /*-- assuan-pipe-server.c --*/
210 int _assuan_new_context (assuan_context_t
*r_ctx
);
211 void _assuan_release_context (assuan_context_t ctx
);
213 /*-- assuan-uds.c --*/
214 void _assuan_uds_close_fds (assuan_context_t ctx
);
215 void _assuan_uds_deinit (assuan_context_t ctx
);
216 void _assuan_init_uds_io (assuan_context_t ctx
);
219 /*-- assuan-handler.c --*/
220 int _assuan_register_std_commands (assuan_context_t ctx
);
222 /*-- assuan-buffer.c --*/
223 assuan_error_t
_assuan_read_line (assuan_context_t ctx
);
224 int _assuan_cookie_write_data (void *cookie
, const char *buffer
, size_t size
);
225 int _assuan_cookie_write_flush (void *cookie
);
226 assuan_error_t
_assuan_write_line (assuan_context_t ctx
, const char *prefix
,
227 const char *line
, size_t len
);
229 /*-- assuan-client.c --*/
230 assuan_error_t
_assuan_read_from_server (assuan_context_t ctx
,
231 int *okay
, int *off
);
233 /*-- assuan-error.c --*/
235 /*-- assuan-inquire.c --*/
236 int _assuan_inquire_ext_cb (assuan_context_t ctx
);
237 void _assuan_inquire_release (assuan_context_t ctx
);
239 /* Map error codes as used in this implementation to the libgpg-error
241 assuan_error_t
_assuan_error (int oldcode
);
242 /* Check if ERR means EAGAIN. */
243 int _assuan_error_is_eagain (assuan_error_t err
);
245 /* Extract the error code from A. This works for both the old and the
246 new style error codes. This needs to be used whenever an error
248 #define err_code(a) ((a) & 0x00ffffff)
250 /* Check whether A is the erro code for EOF. We allow for old and new
251 style EOF error codes here. */
252 #define err_is_eof(a) ((a) == (-1) || err_code (a) == 16383)
256 /*-- assuan-util.c --*/
257 void *_assuan_malloc (size_t n
);
258 void *_assuan_calloc (size_t n
, size_t m
);
259 void *_assuan_realloc (void *p
, size_t n
);
260 void _assuan_free (void *p
);
262 #define xtrymalloc(a) _assuan_malloc ((a))
263 #define xtrycalloc(a,b) _assuan_calloc ((a),(b))
264 #define xtryrealloc(a,b) _assuan_realloc((a),(b))
265 #define xfree(a) _assuan_free ((a))
267 #define set_error(c,e,t) \
268 assuan_set_error ((c), _assuan_error (ASSUAN_ ## e), (t))
270 #ifdef HAVE_W32_SYSTEM
271 const char *_assuan_w32_strerror (int ec
);
272 #define w32_strerror(e) _assuan_w32_strerror ((e))
273 int _assuan_gpg_strerror_r (unsigned int err
, char *buf
, size_t buflen
);
274 const char *_assuan_gpg_strsource (unsigned int err
);
275 #endif /*HAVE_W32_SYSTEM*/
278 /*-- assuan-logging.c --*/
279 void _assuan_set_default_log_stream (FILE *fp
);
281 void _assuan_log_printf (const char *format
, ...)
282 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 )
283 __attribute__ ((format (printf
,1,2)))
286 void _assuan_log_print_buffer (FILE *fp
, const void *buffer
, size_t length
);
287 void _assuan_log_sanitized_string (const char *string
);
290 /*-- assuan-io.c --*/
291 pid_t
_assuan_waitpid (pid_t pid
, int *status
, int options
);
293 ssize_t
_assuan_simple_read (assuan_context_t ctx
, void *buffer
, size_t size
);
294 ssize_t
_assuan_simple_write (assuan_context_t ctx
, const void *buffer
,
296 ssize_t
_assuan_io_read (assuan_fd_t fd
, void *buffer
, size_t size
);
297 ssize_t
_assuan_io_write (assuan_fd_t fd
, const void *buffer
, size_t size
);
298 #ifdef HAVE_W32_SYSTEM
299 int _assuan_simple_sendmsg (assuan_context_t ctx
, void *msg
);
300 int _assuan_simple_recvmsg (assuan_context_t ctx
, void *msg
);
302 ssize_t
_assuan_simple_sendmsg (assuan_context_t ctx
, struct msghdr
*msg
);
303 ssize_t
_assuan_simple_recvmsg (assuan_context_t ctx
, struct msghdr
*msg
);
306 void _assuan_usleep (unsigned int usec
);
309 /*-- assuan-socket.c --*/
310 int _assuan_close (assuan_fd_t fd
);
311 assuan_fd_t
_assuan_sock_new (int domain
, int type
, int proto
);
312 int _assuan_sock_connect (assuan_fd_t sockfd
,
313 struct sockaddr
*addr
, int addrlen
);
314 int _assuan_sock_bind (assuan_fd_t sockfd
, struct sockaddr
*addr
, int addrlen
);
315 int _assuan_sock_get_nonce (struct sockaddr
*addr
, int addrlen
,
316 assuan_sock_nonce_t
*nonce
);
317 int _assuan_sock_check_nonce (assuan_fd_t fd
, assuan_sock_nonce_t
*nonce
);
318 #ifdef HAVE_W32_SYSTEM
319 int _assuan_sock_wsa2errno (int err
);
322 #ifdef HAVE_FOPENCOOKIE
323 /* We have to implement funopen in terms of glibc's fopencookie. */
324 FILE *_assuan_funopen(void *cookie
,
325 cookie_read_function_t
*readfn
,
326 cookie_write_function_t
*writefn
,
327 cookie_seek_function_t
*seekfn
,
328 cookie_close_function_t
*closefn
);
329 #define funopen(a,r,w,s,c) _assuan_funopen ((a), (r), (w), (s), (c))
330 #endif /*HAVE_FOPENCOOKIE*/
332 /* Prototypes for replacement functions. */
334 void *memrchr (const void *block
, int c
, size_t size
);
337 char *stpcpy (char *dest
, const char *src
);
340 #define setenv _assuan_setenv
341 #define unsetenv _assuan_unsetenv
342 #define clearenv _assuan_clearenv
343 int setenv (const char *name
, const char *value
, int replace
);
345 #ifndef HAVE_PUTC_UNLOCKED
346 int putc_unlocked (int c
, FILE *stream
);
349 #define DIM(v) (sizeof(v)/sizeof((v)[0]))
350 #define DIMof(type,member) DIM(((type *)0)->member)
354 #define SOCKET2HANDLE(s) ((void *)(s))
355 #define HANDLE2SOCKET(h) ((unsigned int)(h))
357 #define SOCKET2HANDLE(s) (s)
358 #define HANDLE2SOCKET(h) (h)
362 #endif /*ASSUAN_DEFS_H*/