1
/* assuan-socket-connect.c - Assuan socket based client
2
* Copyright (C) 2002 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
26
#include <sys/socket.h>
30
#include "assuan-defs.h"
32
#ifdef HAVE_JNLIB_LOGGING
33
#include "../jnlib/logging.h"
34
#define LOGERROR(a) log_error ((a))
35
#define LOGERROR1(a,b) log_error ((a), (b))
36
#define LOGERROR2(a,b,c) log_error ((a), (b), (c))
37
#define LOGERRORX(a) log_printf ((a))
39
#define LOGERROR(a) fprintf (stderr, (a))
40
#define LOGERROR1(a,b) fprintf (stderr, (a), (b))
41
#define LOGERROR2(a,b,c) fprintf (stderr, (a), (b), (c))
42
#define LOGERRORX(a) fputs ((a), stderr)
48
do_finish (ASSUAN_CONTEXT ctx)
50
if (ctx->inbound.fd != -1)
52
close (ctx->inbound.fd);
55
ctx->outbound.fd = -1;
60
do_deinit (ASSUAN_CONTEXT ctx)
67
/* Make a connection to the Unix domain socket NAME and return a new
68
Assuan context in CTX. SERVER_PID is currently not used but may
69
becode handy in future. */
71
assuan_socket_connect (ASSUAN_CONTEXT *r_ctx,
72
const char *name, pid_t server_pid)
77
struct sockaddr_un srvr_addr;
81
return ASSUAN_Invalid_Value;
84
/* we require that the name starts with a slash, so that we can
85
alter reuse this function for other socket types */
87
return ASSUAN_Invalid_Value;
88
if (strlen (name)+1 >= sizeof srvr_addr.sun_path)
89
return ASSUAN_Invalid_Value;
91
err = _assuan_new_context (&ctx);
94
ctx->pid = server_pid; /* save it in case we need it later */
95
ctx->deinit_handler = do_deinit;
96
ctx->finish_handler = do_finish;
98
fd = socket (AF_UNIX, SOCK_STREAM, 0);
101
LOGERROR1 ("can't create socket: %s\n", strerror (errno));
102
_assuan_release_context (ctx);
103
return ASSUAN_General_Error;
106
memset (&srvr_addr, 0, sizeof srvr_addr );
107
srvr_addr.sun_family = AF_UNIX;
108
strcpy (srvr_addr.sun_path, name);
109
len = (offsetof (struct sockaddr_un, sun_path)
110
+ strlen (srvr_addr.sun_path) + 1);
112
if (connect (fd, (struct sockaddr*)&srvr_addr, len) == -1)
114
LOGERROR2 ("can't connect to `%s': %s\n", name, strerror (errno));
115
_assuan_release_context (ctx);
117
return ASSUAN_Connect_Failed;
120
ctx->inbound.fd = fd;
121
ctx->outbound.fd = fd;
123
/* initial handshake */
127
err = _assuan_read_from_server (ctx, &okay, &off);
130
LOGERROR1 ("can't connect server: %s\n", assuan_strerror (err));
134
LOGERROR ("can't connect server: `");
135
_assuan_log_sanitized_string (ctx->inbound.line);
137
err = ASSUAN_Connect_Failed;
143
assuan_disconnect (ctx);