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/>.
27 #include <sys/types.h>
28 #ifndef HAVE_W32_SYSTEM
29 #include <sys/socket.h>
35 #include "assuan-defs.h"
37 /* Hacks for Slowaris. */
40 # define PF_LOCAL PF_UNIX
42 # define PF_LOCAL AF_UNIX
46 # define AF_LOCAL AF_UNIX
50 # define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) \
51 + strlen ((ptr)->sun_path))
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
;
68 do_deinit (assuan_context_t 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. */
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
90 assuan_socket_connect_ext (assuan_context_t
*r_ctx
,
91 const char *name
, pid_t server_pid
,
94 static struct assuan_io io
= { _assuan_simple_read
, _assuan_simple_write
,
99 struct sockaddr_un srvr_addr
;
104 return _assuan_error (ASSUAN_Invalid_Value
);
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. */
111 if (*s
&& s
[1] == ':')
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
);
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
);
145 return _assuan_error (ASSUAN_Connect_Failed
);
148 ctx
->inbound
.fd
= fd
;
149 ctx
->outbound
.fd
= fd
;
152 _assuan_init_uds_io (ctx
);
154 /* initial handshake */
158 err
= _assuan_read_from_server (ctx
, &okay
, &off
);
160 _assuan_log_printf ("can't connect to server: %s\n",
161 assuan_strerror (err
));
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
);
173 assuan_disconnect (ctx
);
181 assuan_error_t
assuan_socket_connect_fd (assuan_context_t
*r_ctx
,
186 static struct assuan_io io
= { _assuan_simple_read
,
187 _assuan_simple_write
};
190 assuan_context_t ctx
;
192 if (!r_ctx
|| fd
< 0)
193 return _assuan_error (ASSUAN_Invalid_Value
);
195 err
= _assuan_new_context (&ctx
);
198 ctx
->deinit_handler
= ((flags
&1))? _assuan_uds_deinit
: do_deinit
;
199 ctx
->finish_handler
= do_finish
;
200 ctx
->inbound
.fd
= fd
;
201 ctx
->outbound
.fd
= fd
;
204 _assuan_init_uds_io (ctx
);
205 ctx
->user_pointer
= ptr
;
207 /* initial handshake */
211 err
= _assuan_read_from_server (ctx
, &okay
, &off
);
213 _assuan_log_printf ("can't connect to server: %s\n",
214 assuan_strerror (err
));
217 /*LOG ("can't connect to server: `");*/
218 _assuan_log_sanitized_string (ctx
->inbound
.line
);
219 fprintf (assuan_get_assuan_log_stream (), "'\n");
220 err
= _assuan_error (ASSUAN_Connect_Failed
);
226 assuan_disconnect (ctx
);