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 #include "url.h"
00026 #include "libavutil/time.h"
00027
00028 #if HAVE_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 HAVE_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 HAVE_THREADS && GNUTLS_VERSION_NUMBER <= 0x020b00
00061 #include <gcrypt.h>
00062 #include <errno.h>
00063 GCRY_THREAD_OPTION_PTHREAD_IMPL;
00064 #endif
00065 #endif
00066
00067 void ff_tls_init(void)
00068 {
00069 avpriv_lock_avformat();
00070 #if CONFIG_OPENSSL
00071 if (!openssl_init) {
00072 SSL_library_init();
00073 SSL_load_error_strings();
00074 #if HAVE_THREADS
00075 if (!CRYPTO_get_locking_callback()) {
00076 int i;
00077 openssl_mutexes = av_malloc(sizeof(pthread_mutex_t) * CRYPTO_num_locks());
00078 for (i = 0; i < CRYPTO_num_locks(); i++)
00079 pthread_mutex_init(&openssl_mutexes[i], NULL);
00080 CRYPTO_set_locking_callback(openssl_lock);
00081 #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
00082 CRYPTO_set_id_callback(openssl_thread_id);
00083 #endif
00084 }
00085 #endif
00086 }
00087 openssl_init++;
00088 #endif
00089 #if CONFIG_GNUTLS
00090 #if HAVE_THREADS && GNUTLS_VERSION_NUMBER < 0x020b00
00091 if (gcry_control(GCRYCTL_ANY_INITIALIZATION_P) == 0)
00092 gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
00093 #endif
00094 gnutls_global_init();
00095 #endif
00096 avpriv_unlock_avformat();
00097 }
00098
00099 void ff_tls_deinit(void)
00100 {
00101 avpriv_lock_avformat();
00102 #if CONFIG_OPENSSL
00103 openssl_init--;
00104 if (!openssl_init) {
00105 #if HAVE_THREADS
00106 if (CRYPTO_get_locking_callback() == openssl_lock) {
00107 int i;
00108 CRYPTO_set_locking_callback(NULL);
00109 for (i = 0; i < CRYPTO_num_locks(); i++)
00110 pthread_mutex_destroy(&openssl_mutexes[i]);
00111 av_free(openssl_mutexes);
00112 }
00113 #endif
00114 }
00115 #endif
00116 #if CONFIG_GNUTLS
00117 gnutls_global_deinit();
00118 #endif
00119 avpriv_unlock_avformat();
00120 }
00121
00122 int ff_network_inited_globally;
00123
00124 int ff_network_init(void)
00125 {
00126 #if HAVE_WINSOCK2_H
00127 WSADATA wsaData;
00128 #endif
00129
00130 if (!ff_network_inited_globally)
00131 av_log(NULL, AV_LOG_WARNING, "Using network protocols without global "
00132 "network initialization. Please use "
00133 "avformat_network_init(), this will "
00134 "become mandatory later.\n");
00135 #if HAVE_WINSOCK2_H
00136 if (WSAStartup(MAKEWORD(1,1), &wsaData))
00137 return 0;
00138 #endif
00139 return 1;
00140 }
00141
00142 int ff_network_wait_fd(int fd, int write)
00143 {
00144 int ev = write ? POLLOUT : POLLIN;
00145 struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
00146 int ret;
00147 ret = poll(&p, 1, 100);
00148 return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN);
00149 }
00150
00151 int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb)
00152 {
00153 int ret;
00154 int64_t wait_start = 0;
00155
00156 while (1) {
00157 ret = ff_network_wait_fd(fd, write);
00158 if (ret != AVERROR(EAGAIN))
00159 return ret;
00160 if (ff_check_interrupt(int_cb))
00161 return AVERROR_EXIT;
00162 if (timeout) {
00163 if (!wait_start)
00164 wait_start = av_gettime();
00165 else if (av_gettime() - wait_start > timeout)
00166 return AVERROR(ETIMEDOUT);
00167 }
00168 }
00169 }
00170
00171 void ff_network_close(void)
00172 {
00173 #if HAVE_WINSOCK2_H
00174 WSACleanup();
00175 #endif
00176 }
00177
00178 #if HAVE_WINSOCK2_H
00179 int ff_neterrno(void)
00180 {
00181 int err = WSAGetLastError();
00182 switch (err) {
00183 case WSAEWOULDBLOCK:
00184 return AVERROR(EAGAIN);
00185 case WSAEINTR:
00186 return AVERROR(EINTR);
00187 case WSAEPROTONOSUPPORT:
00188 return AVERROR(EPROTONOSUPPORT);
00189 case WSAETIMEDOUT:
00190 return AVERROR(ETIMEDOUT);
00191 case WSAECONNREFUSED:
00192 return AVERROR(ECONNREFUSED);
00193 case WSAEINPROGRESS:
00194 return AVERROR(EINPROGRESS);
00195 }
00196 return -err;
00197 }
00198 #endif
00199
00200 int ff_is_multicast_address(struct sockaddr *addr)
00201 {
00202 if (addr->sa_family == AF_INET) {
00203 return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
00204 }
00205 #if HAVE_STRUCT_SOCKADDR_IN6
00206 if (addr->sa_family == AF_INET6) {
00207 return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
00208 }
00209 #endif
00210
00211 return 0;
00212 }