00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "libavutil/avutil.h"
00022 #include "network.h"
00023 #include "libavcodec/internal.h"
00024 #include "libavutil/mem.h"
00025
00026 #define THREADS (HAVE_PTHREADS || (defined(WIN32) && !defined(__MINGW32CE__)))
00027
00028 #if THREADS
00029 #if HAVE_PTHREADS
00030 #include <pthread.h>
00031 #else
00032 #include "libavcodec/w32pthreads.h"
00033 #endif
00034 #endif
00035
00036 #if CONFIG_OPENSSL
00037 #include <openssl/ssl.h>
00038 static int openssl_init;
00039 #if THREADS
00040 #include <openssl/crypto.h>
00041 #include "libavutil/avutil.h"
00042 pthread_mutex_t *openssl_mutexes;
00043 static void openssl_lock(int mode, int type, const char *file, int line)
00044 {
00045 if (mode & CRYPTO_LOCK)
00046 pthread_mutex_lock(&openssl_mutexes[type]);
00047 else
00048 pthread_mutex_unlock(&openssl_mutexes[type]);
00049 }
00050 #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
00051 static unsigned long openssl_thread_id(void)
00052 {
00053 return (intptr_t) pthread_self();
00054 }
00055 #endif
00056 #endif
00057 #endif
00058 #if CONFIG_GNUTLS
00059 #include <gnutls/gnutls.h>
00060 #if THREADS && GNUTLS_VERSION_NUMBER <= 0x020b00
00061 #include <gcrypt.h>
00062 #include <errno.h>
00063 #undef malloc
00064 #undef free
00065 GCRY_THREAD_OPTION_PTHREAD_IMPL;
00066 #endif
00067 #endif
00068
00069 void ff_tls_init(void)
00070 {
00071 avpriv_lock_avformat();
00072 #if CONFIG_OPENSSL
00073 if (!openssl_init) {
00074 SSL_library_init();
00075 SSL_load_error_strings();
00076 #if THREADS
00077 if (!CRYPTO_get_locking_callback()) {
00078 int i;
00079 openssl_mutexes = av_malloc(sizeof(pthread_mutex_t) * CRYPTO_num_locks());
00080 for (i = 0; i < CRYPTO_num_locks(); i++)
00081 pthread_mutex_init(&openssl_mutexes[i], NULL);
00082 CRYPTO_set_locking_callback(openssl_lock);
00083 #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
00084 CRYPTO_set_id_callback(openssl_thread_id);
00085 #endif
00086 }
00087 #endif
00088 }
00089 openssl_init++;
00090 #endif
00091 #if CONFIG_GNUTLS
00092 #if THREADS && GNUTLS_VERSION_NUMBER < 0x020b00
00093 if (gcry_control(GCRYCTL_ANY_INITIALIZATION_P) == 0)
00094 gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
00095 #endif
00096 gnutls_global_init();
00097 #endif
00098 avpriv_unlock_avformat();
00099 }
00100
00101 void ff_tls_deinit(void)
00102 {
00103 avpriv_lock_avformat();
00104 #if CONFIG_OPENSSL
00105 openssl_init--;
00106 if (!openssl_init) {
00107 #if THREADS
00108 if (CRYPTO_get_locking_callback() == openssl_lock) {
00109 int i;
00110 CRYPTO_set_locking_callback(NULL);
00111 for (i = 0; i < CRYPTO_num_locks(); i++)
00112 pthread_mutex_destroy(&openssl_mutexes[i]);
00113 av_free(openssl_mutexes);
00114 }
00115 #endif
00116 }
00117 #endif
00118 #if CONFIG_GNUTLS
00119 gnutls_global_deinit();
00120 #endif
00121 avpriv_unlock_avformat();
00122 }
00123
00124 int ff_network_inited_globally;
00125
00126 int ff_network_init(void)
00127 {
00128 #if HAVE_WINSOCK2_H
00129 WSADATA wsaData;
00130 #endif
00131
00132 if (!ff_network_inited_globally)
00133 av_log(NULL, AV_LOG_WARNING, "Using network protocols without global "
00134 "network initialization. Please use "
00135 "avformat_network_init(), this will "
00136 "become mandatory later.\n");
00137 #if HAVE_WINSOCK2_H
00138 if (WSAStartup(MAKEWORD(1,1), &wsaData))
00139 return 0;
00140 #endif
00141 return 1;
00142 }
00143
00144 int ff_network_wait_fd(int fd, int write)
00145 {
00146 int ev = write ? POLLOUT : POLLIN;
00147 struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
00148 int ret;
00149 ret = poll(&p, 1, 100);
00150 return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN);
00151 }
00152
00153 void ff_network_close(void)
00154 {
00155 #if HAVE_WINSOCK2_H
00156 WSACleanup();
00157 #endif
00158 }
00159
00160 #if HAVE_WINSOCK2_H
00161 int ff_neterrno(void)
00162 {
00163 int err = WSAGetLastError();
00164 switch (err) {
00165 case WSAEWOULDBLOCK:
00166 return AVERROR(EAGAIN);
00167 case WSAEINTR:
00168 return AVERROR(EINTR);
00169 case WSAEPROTONOSUPPORT:
00170 return AVERROR(EPROTONOSUPPORT);
00171 case WSAETIMEDOUT:
00172 return AVERROR(ETIMEDOUT);
00173 case WSAECONNREFUSED:
00174 return AVERROR(ECONNREFUSED);
00175 case WSAEINPROGRESS:
00176 return AVERROR(EINPROGRESS);
00177 }
00178 return -err;
00179 }
00180 #endif
00181
00182 int ff_is_multicast_address(struct sockaddr *addr)
00183 {
00184 if (addr->sa_family == AF_INET) {
00185 return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
00186 }
00187 #if HAVE_STRUCT_SOCKADDR_IN6
00188 if (addr->sa_family == AF_INET6) {
00189 return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
00190 }
00191 #endif
00192
00193 return 0;
00194 }