2 * Copyright (C) 2010-2012 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>
29 #include <sys/types.h>
35 # ifdef HAVE_PTHREAD_LOCKS
39 # if defined(HAVE_GETPWUID_R)
44 /* We need to disable gnulib's replacement wrappers to get native
45 Windows interfaces. */
50 /* System specific function wrappers.
53 /* wrappers for write() and writev()
58 system_errno (gnutls_transport_ptr p
)
60 int tmperr
= WSAGetLastError ();
77 WSASetLastError (tmperr
);
83 system_write (gnutls_transport_ptr ptr
, const void *data
, size_t data_size
)
85 return send (GNUTLS_POINTER_TO_INT (ptr
), data
, data_size
, 0);
89 system_errno (gnutls_transport_ptr_t ptr
)
91 #if defined(_AIX) || defined(AIX)
92 if (errno
== 0) errno
= EAGAIN
;
99 system_writev (gnutls_transport_ptr_t ptr
, const giovec_t
* iovec
,
102 return writev (GNUTLS_POINTER_TO_INT (ptr
), (struct iovec
*) iovec
,
109 system_read (gnutls_transport_ptr_t ptr
, void *data
, size_t data_size
)
111 return recv (GNUTLS_POINTER_TO_INT (ptr
), data
, data_size
, 0);
114 /* Wait for data to be received within a timeout period in milliseconds.
115 * To catch a termination it will also try to receive 0 bytes from the
116 * socket if select reports to proceed.
118 * Returns -1 on error, 0 on timeout, positive value if data are available for reading.
120 int system_recv_timeout(gnutls_transport_ptr_t ptr
, unsigned int ms
)
125 int fd
= GNUTLS_POINTER_TO_INT(ptr
);
131 tv
.tv_usec
= ms
* 1000;
133 while(tv
.tv_usec
>= 1000000)
135 tv
.tv_usec
-= 1000000;
139 ret
= select(fd
+1, &rfds
, NULL
, NULL
, &tv
);
143 ret2
= recv(fd
, NULL
, 0, MSG_PEEK
);
152 #ifdef HAVE_WIN32_LOCKS
155 /* FIXME: win32 locks are untested */
157 gnutls_system_mutex_init (void **priv
)
159 CRITICAL_SECTION
*lock
= malloc (sizeof (CRITICAL_SECTION
));
162 return GNUTLS_E_MEMORY_ERROR
;
164 InitializeCriticalSection (lock
);
172 gnutls_system_mutex_deinit (void **priv
)
174 DeleteCriticalSection ((CRITICAL_SECTION
*) * priv
);
181 gnutls_system_mutex_lock (void **priv
)
183 EnterCriticalSection ((CRITICAL_SECTION
*) * priv
);
188 gnutls_system_mutex_unlock (void **priv
)
190 LeaveCriticalSection ((CRITICAL_SECTION
*) * priv
);
194 #endif /* WIN32_LOCKS */
196 #ifdef HAVE_PTHREAD_LOCKS
199 gnutls_system_mutex_init (void **priv
)
201 pthread_mutex_t
*lock
= malloc (sizeof (pthread_mutex_t
));
205 return GNUTLS_E_MEMORY_ERROR
;
207 ret
= pthread_mutex_init (lock
, NULL
);
212 return GNUTLS_E_LOCKING_ERROR
;
221 gnutls_system_mutex_deinit (void **priv
)
223 pthread_mutex_destroy ((pthread_mutex_t
*) * priv
);
229 gnutls_system_mutex_lock (void **priv
)
231 if (pthread_mutex_lock ((pthread_mutex_t
*) * priv
))
234 return GNUTLS_E_LOCKING_ERROR
;
241 gnutls_system_mutex_unlock (void **priv
)
243 if (pthread_mutex_unlock ((pthread_mutex_t
*) * priv
))
246 return GNUTLS_E_LOCKING_ERROR
;
252 #endif /* PTHREAD_LOCKS */
257 gnutls_system_mutex_init (void **priv
)
263 gnutls_system_mutex_deinit (void **priv
)
269 gnutls_system_mutex_lock (void **priv
)
275 gnutls_system_mutex_unlock (void **priv
)
280 #endif /* NO_LOCKS */
282 gnutls_time_func gnutls_time
= time
;
283 mutex_init_func gnutls_mutex_init
= gnutls_system_mutex_init
;
284 mutex_deinit_func gnutls_mutex_deinit
= gnutls_system_mutex_deinit
;
285 mutex_lock_func gnutls_mutex_lock
= gnutls_system_mutex_lock
;
286 mutex_unlock_func gnutls_mutex_unlock
= gnutls_system_mutex_unlock
;
288 #define CONFIG_PATH ".gnutls"
290 /* Returns a path to store user-specific configuration
293 int _gnutls_find_config_path(char* path
, size_t max_size
)
295 char tmp_home_dir
[1024];
296 const char *home_dir
= getenv ("HOME");
299 if (home_dir
== NULL
|| home_dir
[0] == '\0')
301 const char *home_drive
= getenv ("HOMEDRIVE");
302 const char *home_path
= getenv ("HOMEPATH");
304 if (home_drive
!= NULL
&& home_path
!= NULL
)
306 snprintf(tmp_home_dir
, sizeof(tmp_home_dir
), "%s%s", home_drive
, home_path
);
313 home_dir
= tmp_home_dir
;
315 #elif defined(HAVE_GETPWUID_R)
316 if (home_dir
== NULL
|| home_dir
[0] == '\0')
322 getpwuid_r(getuid(), &_pwd
, buf
, sizeof(buf
), &pwd
);
325 snprintf(tmp_home_dir
, sizeof(tmp_home_dir
), "%s", pwd
->pw_dir
);
332 home_dir
= tmp_home_dir
;
335 if (home_dir
== NULL
|| home_dir
[0] == '\0')
338 home_dir
= tmp_home_dir
;
342 if (home_dir
== NULL
|| home_dir
[0] == 0)
345 snprintf(path
, max_size
, "%s/"CONFIG_PATH
, home_dir
);