File: socket.c

package info (click to toggle)
fuse-emulator 1.1.1%2Bdfsg1-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,120 kB
  • ctags: 8,968
  • sloc: ansic: 78,960; sh: 11,228; perl: 3,742; makefile: 1,104; yacc: 236; lex: 140
file content (241 lines) | stat: -rw-r--r-- 6,367 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* socket.c: Socket-related compatibility routines
   Copyright (c) 2011 Sergio BaldovĂ­, Philip Kendall

   $Id: socket.c 4775 2012-11-26 23:03:36Z sbaldovi $

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with this program; if not, write to the Free Software Foundation, Inc.,
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

   Author contact information:

   E-mail: philip-fuse@shadowmagic.org.uk

*/

#include <config.h>

#include <winsock2.h>
#include <ws2tcpip.h>

#include "compat.h"
#include "fuse.h"
#include "ui/ui.h"

const compat_socket_t compat_socket_invalid = INVALID_SOCKET;
const int compat_socket_EBADF = WSAENOTSOCK;

struct compat_socket_selfpipe_t {
  SOCKET self_socket;
  libspectrum_word port;
};

int
compat_socket_close( compat_socket_t fd )
{
  return closesocket( fd );
}

int compat_socket_get_error( void )
{
  return WSAGetLastError();
}

const char *
compat_socket_get_strerror( void )
{
  static TCHAR buffer[256];
  TCHAR *ptr;
  DWORD msg_size;

  /* get description of last winsock error */
  msg_size = FormatMessage( 
               FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
               NULL, WSAGetLastError(),
               MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
               buffer, sizeof( buffer ) / sizeof( TCHAR ), NULL );

  if( !msg_size ) return NULL;

  /* skip 'new line' like chars */
  for( ptr = buffer; *ptr; ptr++ ) {
    if( ( *ptr == '\r' ) || ( *ptr == '\n' ) ) {
      *ptr = '\0';
      break;
    }
  }

  return (const char *)buffer;
}

static int
selfpipe_test( compat_socket_selfpipe_t *self )
{
  fd_set readfds;
  int active;
  struct timeval tv = { 1, 0 };

  /* Send testing packet */
  compat_socket_selfpipe_wake( self );

  /* Safe reading from control socket */
  FD_ZERO( &readfds );
  FD_SET( self->self_socket, &readfds );
  active = select( 0, &readfds, NULL, NULL, &tv );
  if( active == 0 || active == compat_socket_invalid ) {
    return -1;
  }

  /* Discard testing packet */
  if( FD_ISSET( self->self_socket, &readfds ) ) {
    compat_socket_selfpipe_discard_data( self );
  }

  return 0;
}

compat_socket_selfpipe_t *
compat_socket_selfpipe_alloc( void )
{
  unsigned long mode = 1;
  struct sockaddr_in sa;
  socklen_t sa_len = sizeof(sa);

  compat_socket_selfpipe_t *self = malloc( sizeof( *self ) );
  if( !self ) {
    ui_error( UI_ERROR_ERROR, "%s: %d: out of memory", __FILE__, __LINE__ );
    fuse_abort();
  }
  
  self->self_socket = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
  if( self->self_socket == compat_socket_invalid ) {
    ui_error( UI_ERROR_ERROR,
              "%s: %d: failed to open socket; errno %d: %s\n",
              __FILE__, __LINE__, compat_socket_get_error(),
              compat_socket_get_strerror() );
    fuse_abort();
  }

  /* Set nonblocking mode */
  if( ioctlsocket( self->self_socket, FIONBIO, &mode ) == -1 ) {
    ui_error( UI_ERROR_ERROR, 
              "%s: %d: failed to set socket nonblocking; errno %d: %s\n",
              __FILE__, __LINE__, compat_socket_get_error(),
              compat_socket_get_strerror() );
    fuse_abort();
  }

  memset( &sa, 0, sizeof(sa) );
  sa.sin_family = AF_INET;
  sa.sin_addr.s_addr = htonl( INADDR_LOOPBACK );

  if( bind( self->self_socket, (struct sockaddr*)&sa, sizeof(sa) ) == -1 ) {
    ui_error( UI_ERROR_ERROR,
              "%s: %d: failed to bind socket; errno %d: %s\n",
              __FILE__, __LINE__, compat_socket_get_error(),
              compat_socket_get_strerror() );
    fuse_abort();
  }

  /* Get ephemeral port number */
  if( getsockname( self->self_socket, (struct sockaddr *)&sa, &sa_len ) == -1 ) {
    ui_error( UI_ERROR_ERROR,
              "%s: %d: failed to get socket name; errno %d: %s\n",
              __FILE__, __LINE__, compat_socket_get_error(),
              compat_socket_get_strerror() );
    fuse_abort();
  }

  self->port = ntohs( sa.sin_port );
 
  /* Test communications in order to detect blocking firewalls */
  if( selfpipe_test( self ) == -1 ) {
    ui_error( UI_ERROR_ERROR,
              "Networking: failed to test internal communications" );
    fuse_abort();
  }

  return self;
}

void
compat_socket_selfpipe_free( compat_socket_selfpipe_t *self )
{
  compat_socket_close( self->self_socket );
  free( self );
}

compat_socket_t
compat_socket_selfpipe_get_read_fd( compat_socket_selfpipe_t *self )
{
  return self->self_socket;
}

void
compat_socket_selfpipe_wake( compat_socket_selfpipe_t *self )
{
  struct sockaddr_in sa;

  memset( &sa, 0, sizeof(sa) );
  sa.sin_family = AF_INET;
  sa.sin_addr.s_addr = htonl( INADDR_LOOPBACK );
  sa.sin_port = htons( self->port );

  sendto( self->self_socket, NULL, 0, 0, (struct sockaddr*)&sa, sizeof(sa) );
}

void
compat_socket_selfpipe_discard_data( compat_socket_selfpipe_t *self )
{
  ssize_t bytes_read;
  struct sockaddr_in sa;
  socklen_t sa_length = sizeof(sa);
  static char bitbucket[0x100];

  do {
    /* Socket is non blocking, so we can do this safely */
    bytes_read = recvfrom( self->self_socket, bitbucket, sizeof( bitbucket ),
                           0, (struct sockaddr*)&sa, &sa_length );
  } while( bytes_read != -1 );
}


void
compat_socket_networking_init( void )
{
  WORD wVersionRequested;
  WSADATA wsaData;
  int error;

  wVersionRequested = MAKEWORD( 2, 2 );
  error = WSAStartup( wVersionRequested, &wsaData );
  if( error ) {
    ui_error( UI_ERROR_ERROR, "%s:%d: error %d from WSAStartup()", __FILE__,
              __LINE__, error );
    fuse_abort();
  }

  if( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 ) {
    ui_error( UI_ERROR_ERROR,
              "%s:%d: unexpected version 0x%02x from WSAStartup()",
              __FILE__, __LINE__, wsaData.wVersion );
    fuse_abort();
  }
}

void
compat_socket_networking_end( void )
{
  WSACleanup();
}