Include libassuan statically.
[pwmd.git] / assuan / src / assuan-socket-connect.c
blob8eb6d828abc6598f5e7aa209c61ab61cbc5081c6
1 /* assuan-socket-connect.c - Assuan socket based client
2 * Copyright (C) 2002, 2003, 2004 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, see <http://www.gnu.org/licenses/>.
20 #include <config.h>
21 #include <stdlib.h>
22 #include <stddef.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #ifndef HAVE_W32_SYSTEM
29 #include <sys/socket.h>
30 #include <sys/un.h>
31 #else
32 #include <windows.h>
33 #endif
35 #include "assuan-defs.h"
37 /* Hacks for Slowaris. */
38 #ifndef PF_LOCAL
39 # ifdef PF_UNIX
40 # define PF_LOCAL PF_UNIX
41 # else
42 # define PF_LOCAL AF_UNIX
43 # endif
44 #endif
45 #ifndef AF_LOCAL
46 # define AF_LOCAL AF_UNIX
47 #endif
49 #ifndef SUN_LEN
50 # define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) \
51 + strlen ((ptr)->sun_path))
52 #endif
55 static int
56 do_finish (assuan_context_t ctx)
58 if (ctx->inbound.fd != ASSUAN_INVALID_FD)
60 _assuan_close (ctx->inbound.fd);
62 ctx->inbound.fd = ASSUAN_INVALID_FD;
63 ctx->outbound.fd = ASSUAN_INVALID_FD;
64 return 0;
67 static void
68 do_deinit (assuan_context_t ctx)
70 do_finish (ctx);
74 /* Make a connection to the Unix domain socket NAME and return a new
75 Assuan context in CTX. SERVER_PID is currently not used but may
76 become handy in the future. */
77 assuan_error_t
78 assuan_socket_connect (assuan_context_t *r_ctx,
79 const char *name, pid_t server_pid)
81 return assuan_socket_connect_ext (r_ctx, name, server_pid, 0);
85 /* Make a connection to the Unix domain socket NAME and return a new
86 Assuan context in CTX. SERVER_PID is currently not used but may
87 become handy in the future. With flags set to 1 sendmsg and
88 recvmsg are used. */
89 assuan_error_t
90 assuan_socket_connect_ext (assuan_context_t *r_ctx,
91 const char *name, pid_t server_pid,
92 unsigned int flags)
94 static struct assuan_io io = { _assuan_simple_read, _assuan_simple_write,
95 NULL, NULL };
96 assuan_error_t err;
97 assuan_context_t ctx;
98 assuan_fd_t fd;
99 struct sockaddr_un srvr_addr;
100 size_t len;
101 const char *s;
103 if (!r_ctx || !name)
104 return _assuan_error (ASSUAN_Invalid_Value);
105 *r_ctx = NULL;
107 /* We require that the name starts with a slash, so that we
108 eventually can reuse this function for other socket types. To
109 make things easier we allow an optional driver prefix. */
110 s = name;
111 if (*s && s[1] == ':')
112 s += 2;
113 if (*s != DIRSEP_C && *s != '/')
114 return _assuan_error (ASSUAN_Invalid_Value);
116 if (strlen (name)+1 >= sizeof srvr_addr.sun_path)
117 return _assuan_error (ASSUAN_Invalid_Value);
119 err = _assuan_new_context (&ctx);
120 if (err)
121 return err;
122 ctx->deinit_handler = ((flags&1))? _assuan_uds_deinit : do_deinit;
123 ctx->finish_handler = do_finish;
125 fd = _assuan_sock_new (PF_LOCAL, SOCK_STREAM, 0);
126 if (fd == ASSUAN_INVALID_FD)
128 _assuan_log_printf ("can't create socket: %s\n", strerror (errno));
129 _assuan_release_context (ctx);
130 return _assuan_error (ASSUAN_General_Error);
133 memset (&srvr_addr, 0, sizeof srvr_addr);
134 srvr_addr.sun_family = AF_LOCAL;
135 strncpy (srvr_addr.sun_path, name, sizeof (srvr_addr.sun_path) - 1);
136 srvr_addr.sun_path[sizeof (srvr_addr.sun_path) - 1] = 0;
137 len = SUN_LEN (&srvr_addr);
139 if ( _assuan_sock_connect (fd, (struct sockaddr *) &srvr_addr, len) == -1 )
141 _assuan_log_printf ("can't connect to `%s': %s\n",
142 name, strerror (errno));
143 _assuan_release_context (ctx);
144 _assuan_close (fd);
145 return _assuan_error (ASSUAN_Connect_Failed);
148 ctx->inbound.fd = fd;
149 ctx->outbound.fd = fd;
150 ctx->io = &io;
151 if ((flags&1))
152 _assuan_init_uds_io (ctx);
154 /* initial handshake */
156 int okay, off;
158 err = _assuan_read_from_server (ctx, &okay, &off);
159 if (err)
160 _assuan_log_printf ("can't connect to server: %s\n",
161 assuan_strerror (err));
162 else if (okay != 1)
164 /*LOG ("can't connect to server: `");*/
165 _assuan_log_sanitized_string (ctx->inbound.line);
166 fprintf (assuan_get_assuan_log_stream (), "'\n");
167 err = _assuan_error (ASSUAN_Connect_Failed);
171 if (err)
173 assuan_disconnect (ctx);
175 else
176 *r_ctx = ctx;
177 return 0;