2 * Copyright (C) 2010 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GnuTLS.
8 * The GnuTLS is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 3 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>
24 #include <gnutls_int.h>
25 #include <gnutls_errors.h>
33 #ifdef HAVE_PTHREAD_LOCKS
38 /* We need to disable gnulib's replacement wrappers to get native
39 Windows interfaces. */
43 /* System specific function wrappers.
46 /* wrappers for write() and writev()
51 system_errno (gnutls_transport_ptr p
)
53 int tmperr
= WSAGetLastError ();
70 WSASetLastError (tmperr
);
76 system_write (gnutls_transport_ptr ptr
, const void *data
, size_t data_size
)
78 return send (GNUTLS_POINTER_TO_INT (ptr
), data
, data_size
, 0);
82 system_errno (gnutls_transport_ptr_t ptr
)
88 system_writev (gnutls_transport_ptr_t ptr
, const giovec_t
* iovec
,
91 return writev (GNUTLS_POINTER_TO_INT (ptr
), (struct iovec
*) iovec
,
98 system_read (gnutls_transport_ptr_t ptr
, void *data
, size_t data_size
)
100 return recv (GNUTLS_POINTER_TO_INT (ptr
), data
, data_size
, 0);
104 system_read_peek (gnutls_transport_ptr_t ptr
, void *data
, size_t data_size
)
106 return recv (GNUTLS_POINTER_TO_INT (ptr
), data
, data_size
, MSG_PEEK
);
109 /* Wait for data to be received within a timeout period in milliseconds.
110 * If data_size > 0 it will return the specified amount of data in
113 * Returns -1 on error, 0 on timeout.
115 int system_recv_timeout(gnutls_transport_ptr_t ptr
, unsigned int ms
)
122 FD_SET(GNUTLS_POINTER_TO_INT(ptr
), &rfds
);
125 tv
.tv_usec
= ms
* 1000;
127 ret
= select(GNUTLS_POINTER_TO_INT(ptr
)+1, &rfds
, NULL
, NULL
, &tv
);
132 ret2
= recv(GNUTLS_POINTER_TO_INT(ptr
), NULL
, 0, MSG_PEEK
);
141 #ifdef HAVE_WIN32_LOCKS
144 /* FIXME: win32 locks are untested */
146 gnutls_system_mutex_init (void **priv
)
148 CRITICAL_SECTION
*lock
= malloc (sizeof (CRITICAL_SECTION
));
151 return GNUTLS_E_MEMORY_ERROR
;
153 InitializeCriticalSection (lock
);
161 gnutls_system_mutex_deinit (void **priv
)
163 DeleteCriticalSection ((CRITICAL_SECTION
*) * priv
);
170 gnutls_system_mutex_lock (void **priv
)
172 EnterCriticalSection ((CRITICAL_SECTION
*) * priv
);
177 gnutls_system_mutex_unlock (void **priv
)
179 LeaveCriticalSection ((CRITICAL_SECTION
*) * priv
);
184 _gnutls_atfork (void (*prepare
) (void), void (*parent
) (void),
185 void (*child
) (void))
191 #endif /* WIN32_LOCKS */
193 #ifdef HAVE_PTHREAD_LOCKS
196 gnutls_system_mutex_init (void **priv
)
198 pthread_mutex_t
*lock
= malloc (sizeof (pthread_mutex_t
));
202 return GNUTLS_E_MEMORY_ERROR
;
204 ret
= pthread_mutex_init (lock
, NULL
);
209 return GNUTLS_E_LOCKING_ERROR
;
218 gnutls_system_mutex_deinit (void **priv
)
220 pthread_mutex_destroy ((pthread_mutex_t
*) * priv
);
226 gnutls_system_mutex_lock (void **priv
)
228 if (pthread_mutex_lock ((pthread_mutex_t
*) * priv
))
231 return GNUTLS_E_LOCKING_ERROR
;
238 gnutls_system_mutex_unlock (void **priv
)
240 if (pthread_mutex_unlock ((pthread_mutex_t
*) * priv
))
243 return GNUTLS_E_LOCKING_ERROR
;
250 _gnutls_atfork (void (*prepare
) (void), void (*parent
) (void),
251 void (*child
) (void))
253 return pthread_atfork (prepare
, parent
, child
);
256 #endif /* PTHREAD_LOCKS */
261 gnutls_system_mutex_init (void **priv
)
267 gnutls_system_mutex_deinit (void **priv
)
273 gnutls_system_mutex_lock (void **priv
)
279 gnutls_system_mutex_unlock (void **priv
)
285 _gnutls_atfork (void (*prepare
) (void), void (*parent
) (void),
286 void (*child
) (void))
291 #endif /* NO_LOCKS */
293 gnutls_time_func gnutls_time
= time
;
294 mutex_init_func gnutls_mutex_init
= gnutls_system_mutex_init
;
295 mutex_deinit_func gnutls_mutex_deinit
= gnutls_system_mutex_deinit
;
296 mutex_lock_func gnutls_mutex_lock
= gnutls_system_mutex_lock
;
297 mutex_unlock_func gnutls_mutex_unlock
= gnutls_system_mutex_unlock
;