~ubuntu-branches/ubuntu/quantal/gnutls26/quantal

« back to all changes in this revision

Viewing changes to lib/gl/sockets.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Langasek
  • Date: 2011-05-20 13:07:18 UTC
  • mfrom: (12.1.11 sid)
  • Revision ID: james.westby@ubuntu.com-20110520130718-db41dybbanzfvlji
Tags: 2.10.5-1ubuntu1
* Merge from Debian unstable, remaining changes:
  - Fix build failure with --no-add-needed.
  - Build for multiarch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* sockets.c --- wrappers for Windows socket functions
2
2
 
3
 
   Copyright (C) 2008-2009 Free Software Foundation, Inc.
 
3
   Copyright (C) 2008-2010 Free Software Foundation, Inc.
4
4
 
5
5
   This program is free software: you can redistribute it and/or modify
6
6
   it under the terms of the GNU Lesser General Public License as published by
25
25
#if WINDOWS_SOCKETS
26
26
 
27
27
/* This includes winsock2.h on MinGW. */
28
 
#include <sys/socket.h>
 
28
# include <sys/socket.h>
29
29
 
30
 
#include "close-hook.h"
 
30
# include "close-hook.h"
31
31
 
32
32
/* Get set_winsock_errno, FD_TO_SOCKET etc. */
33
 
#include "w32sock.h"
 
33
# include "w32sock.h"
34
34
 
35
35
static int
36
36
close_fd_maybe_socket (int fd, const struct close_hook *remaining_list)
46
46
    {
47
47
      /* fd refers to a socket.  */
48
48
      /* FIXME: other applications, like squid, use an undocumented
49
 
         _free_osfhnd free function.  But this is not enough: The 'osfile'
50
 
         flags for fd also needs to be cleared, but it is hard to access it.
51
 
         Instead, here we just close twice the file descriptor.  */
 
49
         _free_osfhnd free function.  But this is not enough: The 'osfile'
 
50
         flags for fd also needs to be cleared, but it is hard to access it.
 
51
         Instead, here we just close twice the file descriptor.  */
52
52
      if (closesocket (sock))
53
 
        {
54
 
          set_winsock_errno ();
55
 
          return -1;
56
 
        }
 
53
        {
 
54
          set_winsock_errno ();
 
55
          return -1;
 
56
        }
57
57
      else
58
 
        {
59
 
          /* This call frees the file descriptor and does a
60
 
             CloseHandle ((HANDLE) _get_osfhandle (fd)), which fails.  */
61
 
          _close (fd);
62
 
          return 0;
63
 
        }
 
58
        {
 
59
          /* This call frees the file descriptor and does a
 
60
             CloseHandle ((HANDLE) _get_osfhandle (fd)), which fails.  */
 
61
          _close (fd);
 
62
          return 0;
 
63
        }
64
64
    }
65
65
  else
66
66
    /* Some other type of file descriptor.  */
69
69
 
70
70
static struct close_hook close_sockets_hook;
71
71
 
72
 
#endif
 
72
static int initialized_sockets_version /* = 0 */;
 
73
 
 
74
#endif /* WINDOWS_SOCKETS */
73
75
 
74
76
int
75
 
gl_sockets_startup (int version)
 
77
gl_sockets_startup (int version _GL_UNUSED)
76
78
{
77
79
#if WINDOWS_SOCKETS
78
 
  WSADATA data;
79
 
  int err;
80
 
 
81
 
  err = WSAStartup (version, &data);
82
 
  if (err != 0)
83
 
    return 1;
84
 
 
85
 
  if (data.wVersion < version)
86
 
    return 2;
87
 
 
88
 
  register_close_hook (close_fd_maybe_socket, &close_sockets_hook);
 
80
  if (version > initialized_sockets_version)
 
81
    {
 
82
      WSADATA data;
 
83
      int err;
 
84
 
 
85
      err = WSAStartup (version, &data);
 
86
      if (err != 0)
 
87
        return 1;
 
88
 
 
89
      if (data.wVersion < version)
 
90
        return 2;
 
91
 
 
92
      if (initialized_sockets_version == 0)
 
93
        register_close_hook (close_fd_maybe_socket, &close_sockets_hook);
 
94
 
 
95
      initialized_sockets_version = version;
 
96
    }
89
97
#endif
90
98
 
91
99
  return 0;
97
105
#if WINDOWS_SOCKETS
98
106
  int err;
99
107
 
 
108
  initialized_sockets_version = 0;
 
109
 
100
110
  unregister_close_hook (&close_sockets_hook);
101
111
 
102
112
  err = WSACleanup ();