~ubuntu-branches/ubuntu/hardy/gnupg/hardy-updates

« back to all changes in this revision

Viewing changes to util/assuan-socket.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2005-12-16 16:57:39 UTC
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20051216165739-v0m2d1you6hd8jho
Tags: upstream-1.4.2
ImportĀ upstreamĀ versionĀ 1.4.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* assuan-socket.c
 
2
 * Copyright (C) 2004 Free Software Foundation, Inc.
 
3
 *
 
4
 * This file is part of GnuPG.
 
5
 *
 
6
 * GnuPG is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * GnuPG is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
 
19
 * USA.
 
20
 */
 
21
 
 
22
/* Please note that this is a stripped down and modified version of
 
23
   the orginal Assuan code from libassuan. */
 
24
 
 
25
#include <config.h>
 
26
#include <stdio.h>
 
27
#ifdef HAVE_W32_SYSTEM
 
28
#include <windows.h>
 
29
#include <io.h>
 
30
#else
 
31
#include <sys/types.h>
 
32
#include <sys/socket.h>
 
33
#endif
 
34
#include "assuan-defs.h"
 
35
 
 
36
int
 
37
_assuan_close (int fd)
 
38
{
 
39
#ifndef HAVE_W32_SYSTEM
 
40
  return close (fd);
 
41
#else
 
42
  int rc = closesocket (fd);
 
43
  if (rc && WSAGetLastError () == WSAENOTSOCK)
 
44
      rc = close (fd);
 
45
  return rc;
 
46
#endif
 
47
}
 
48
 
 
49
 
 
50
int
 
51
_assuan_sock_new (int domain, int type, int proto)
 
52
{
 
53
#ifndef HAVE_W32_SYSTEM
 
54
  return socket (domain, type, proto);
 
55
#else
 
56
  if (domain == AF_UNIX || domain == AF_LOCAL)
 
57
    domain = AF_INET;
 
58
  return socket (domain, type, proto);
 
59
#endif
 
60
}
 
61
 
 
62
 
 
63
int
 
64
_assuan_sock_connect (int sockfd, struct sockaddr * addr, int addrlen)
 
65
{
 
66
#ifndef HAVE_W32_SYSTEM
 
67
  return connect (sockfd, addr, addrlen);
 
68
#else
 
69
  struct sockaddr_in myaddr;
 
70
  struct sockaddr_un * unaddr;
 
71
  FILE * fp;
 
72
  int port = 0;
 
73
  
 
74
  unaddr = (struct sockaddr_un *)addr;
 
75
  fp = fopen (unaddr->sun_path, "rb");
 
76
  if (!fp)
 
77
      return -1;
 
78
  fscanf (fp, "%d", &port);
 
79
  fclose (fp);
 
80
  /* XXX: set errno in this case */
 
81
  if (port < 0 || port > 65535)
 
82
      return -1;
 
83
  
 
84
  myaddr.sin_family = AF_INET;
 
85
  myaddr.sin_port = port; 
 
86
  myaddr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
 
87
 
 
88
  /* we need this later. */
 
89
  unaddr->sun_family = myaddr.sin_family;
 
90
  unaddr->sun_port = myaddr.sin_port;
 
91
  unaddr->sun_addr.s_addr = myaddr.sin_addr.s_addr;
 
92
  
 
93
  return connect (sockfd, (struct sockaddr *)&myaddr, sizeof myaddr);
 
94
#endif
 
95
}
 
96
 
 
97