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 2.1 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
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
26 #include <gnutls_int.h>
27 #include <gnutls_errors.h>
35 #ifdef HAVE_PTHREAD_LOCKS
40 /* We need to disable gnulib's replacement wrappers to get native
41 Windows interfaces. */
45 /* System specific function wrappers.
48 /* wrappers for write() and writev()
53 system_errno (gnutls_transport_ptr p
)
55 int tmperr
= WSAGetLastError ();
69 WSASetLastError (tmperr
);
75 system_write (gnutls_transport_ptr ptr
, const void *data
, size_t data_size
)
77 return send (GNUTLS_POINTER_TO_INT (ptr
), data
, data_size
, 0);
81 system_errno (gnutls_transport_ptr ptr
)
87 system_writev (gnutls_transport_ptr ptr
, const giovec_t
* iovec
,
90 return writev (GNUTLS_POINTER_TO_INT (ptr
), (struct iovec
*) iovec
,
97 system_read (gnutls_transport_ptr ptr
, void *data
, size_t data_size
)
99 return recv (GNUTLS_POINTER_TO_INT (ptr
), data
, data_size
, 0);
103 system_read_peek (gnutls_transport_ptr ptr
, void *data
, size_t data_size
)
105 return recv (GNUTLS_POINTER_TO_INT (ptr
), data
, data_size
, MSG_PEEK
);
110 #ifdef HAVE_WIN32_LOCKS
113 /* FIXME: win32 locks are untested */
115 gnutls_system_mutex_init (void **priv
)
117 CRITICAL_SECTION
*lock
= malloc (sizeof (CRITICAL_SECTION
));
120 return GNUTLS_E_MEMORY_ERROR
;
122 InitializeCriticalSection (lock
);
130 gnutls_system_mutex_deinit (void **priv
)
132 DeleteCriticalSection ((CRITICAL_SECTION
*) * priv
);
139 gnutls_system_mutex_lock (void **priv
)
141 EnterCriticalSection ((CRITICAL_SECTION
*) * priv
);
146 gnutls_system_mutex_unlock (void **priv
)
148 LeaveCriticalSection ((CRITICAL_SECTION
*) * priv
);
153 _gnutls_atfork (void (*prepare
) (void), void (*parent
) (void),
154 void (*child
) (void))
160 #endif /* WIN32_LOCKS */
162 #ifdef HAVE_PTHREAD_LOCKS
165 gnutls_system_mutex_init (void **priv
)
167 pthread_mutex_t
*lock
= malloc (sizeof (pthread_mutex_t
));
171 return GNUTLS_E_MEMORY_ERROR
;
173 ret
= pthread_mutex_init (lock
, NULL
);
178 return GNUTLS_E_LOCKING_ERROR
;
187 gnutls_system_mutex_deinit (void **priv
)
189 pthread_mutex_destroy ((pthread_mutex_t
*) * priv
);
195 gnutls_system_mutex_lock (void **priv
)
197 if (pthread_mutex_lock ((pthread_mutex_t
*) * priv
))
200 return GNUTLS_E_LOCKING_ERROR
;
207 gnutls_system_mutex_unlock (void **priv
)
209 if (pthread_mutex_unlock ((pthread_mutex_t
*) * priv
))
212 return GNUTLS_E_LOCKING_ERROR
;
219 _gnutls_atfork (void (*prepare
) (void), void (*parent
) (void),
220 void (*child
) (void))
222 return pthread_atfork (prepare
, parent
, child
);
225 #endif /* PTHREAD_LOCKS */
230 gnutls_system_mutex_init (void **priv
)
236 gnutls_system_mutex_deinit (void **priv
)
242 gnutls_system_mutex_lock (void **priv
)
248 gnutls_system_mutex_unlock (void **priv
)
254 _gnutls_atfork (void (*prepare
) (void), void (*parent
) (void),
255 void (*child
) (void))
260 #endif /* NO_LOCKS */
262 gnutls_time_func gnutls_time
= time
;
263 mutex_init_func gnutls_mutex_init
= gnutls_system_mutex_init
;
264 mutex_deinit_func gnutls_mutex_deinit
= gnutls_system_mutex_deinit
;
265 mutex_lock_func gnutls_mutex_lock
= gnutls_system_mutex_lock
;
266 mutex_unlock_func gnutls_mutex_unlock
= gnutls_system_mutex_unlock
;