00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #define _SVID_SOURCE
00025
00026 #include "config.h"
00027 #include "avformat.h"
00028 #include "os_support.h"
00029
00030 #if defined(_WIN32) && !defined(__MINGW32CE__)
00031 #undef open
00032 #include <fcntl.h>
00033 #include <io.h>
00034 #include <windows.h>
00035 #include <share.h>
00036
00037 int ff_win32_open(const char *filename_utf8, int oflag, int pmode)
00038 {
00039 int fd;
00040 int num_chars;
00041 wchar_t *filename_w;
00042
00043
00044 num_chars = MultiByteToWideChar(CP_UTF8, 0, filename_utf8, -1, NULL, 0);
00045 if (num_chars <= 0)
00046 return -1;
00047 filename_w = av_mallocz(sizeof(wchar_t) * num_chars);
00048 MultiByteToWideChar(CP_UTF8, 0, filename_utf8, -1, filename_w, num_chars);
00049
00050 fd = _wsopen(filename_w, oflag, SH_DENYNO, pmode);
00051 av_freep(&filename_w);
00052
00053
00054 if (fd == -1 && !(oflag & O_CREAT))
00055 return _sopen(filename_utf8, oflag, SH_DENYNO, pmode);
00056
00057 return fd;
00058 }
00059 #endif
00060
00061 #if CONFIG_NETWORK
00062 #include <fcntl.h>
00063 #if !HAVE_POLL_H
00064 #if HAVE_SYS_TIME_H
00065 #include <sys/time.h>
00066 #endif
00067 #if HAVE_WINSOCK2_H
00068 #include <winsock2.h>
00069 #elif HAVE_SYS_SELECT_H
00070 #include <sys/select.h>
00071 #endif
00072 #endif
00073
00074 #include "network.h"
00075
00076 #if !HAVE_INET_ATON
00077 #include <stdlib.h>
00078
00079 int ff_inet_aton(const char *str, struct in_addr *add)
00080 {
00081 unsigned int add1 = 0, add2 = 0, add3 = 0, add4 = 0;
00082
00083 if (sscanf(str, "%d.%d.%d.%d", &add1, &add2, &add3, &add4) != 4)
00084 return 0;
00085
00086 if (!add1 || (add1 | add2 | add3 | add4) > 255)
00087 return 0;
00088
00089 add->s_addr = htonl((add1 << 24) + (add2 << 16) + (add3 << 8) + add4);
00090
00091 return 1;
00092 }
00093 #else
00094 int ff_inet_aton(const char *str, struct in_addr *add)
00095 {
00096 return inet_aton(str, add);
00097 }
00098 #endif
00099
00100 #if !HAVE_GETADDRINFO
00101 int ff_getaddrinfo(const char *node, const char *service,
00102 const struct addrinfo *hints, struct addrinfo **res)
00103 {
00104 struct hostent *h = NULL;
00105 struct addrinfo *ai;
00106 struct sockaddr_in *sin;
00107
00108 #if HAVE_WINSOCK2_H
00109 int (WSAAPI *win_getaddrinfo)(const char *node, const char *service,
00110 const struct addrinfo *hints,
00111 struct addrinfo **res);
00112 HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
00113 win_getaddrinfo = GetProcAddress(ws2mod, "getaddrinfo");
00114 if (win_getaddrinfo)
00115 return win_getaddrinfo(node, service, hints, res);
00116 #endif
00117
00118 *res = NULL;
00119 sin = av_mallocz(sizeof(struct sockaddr_in));
00120 if (!sin)
00121 return EAI_FAIL;
00122 sin->sin_family = AF_INET;
00123
00124 if (node) {
00125 if (!ff_inet_aton(node, &sin->sin_addr)) {
00126 if (hints && (hints->ai_flags & AI_NUMERICHOST)) {
00127 av_free(sin);
00128 return EAI_FAIL;
00129 }
00130 h = gethostbyname(node);
00131 if (!h) {
00132 av_free(sin);
00133 return EAI_FAIL;
00134 }
00135 memcpy(&sin->sin_addr, h->h_addr_list[0], sizeof(struct in_addr));
00136 }
00137 } else {
00138 if (hints && (hints->ai_flags & AI_PASSIVE))
00139 sin->sin_addr.s_addr = INADDR_ANY;
00140 else
00141 sin->sin_addr.s_addr = INADDR_LOOPBACK;
00142 }
00143
00144
00145
00146 if (service)
00147 sin->sin_port = htons(atoi(service));
00148
00149 ai = av_mallocz(sizeof(struct addrinfo));
00150 if (!ai) {
00151 av_free(sin);
00152 return EAI_FAIL;
00153 }
00154
00155 *res = ai;
00156 ai->ai_family = AF_INET;
00157 ai->ai_socktype = hints ? hints->ai_socktype : 0;
00158 switch (ai->ai_socktype) {
00159 case SOCK_STREAM:
00160 ai->ai_protocol = IPPROTO_TCP;
00161 break;
00162 case SOCK_DGRAM:
00163 ai->ai_protocol = IPPROTO_UDP;
00164 break;
00165 default:
00166 ai->ai_protocol = 0;
00167 break;
00168 }
00169
00170 ai->ai_addr = (struct sockaddr *)sin;
00171 ai->ai_addrlen = sizeof(struct sockaddr_in);
00172 if (hints && (hints->ai_flags & AI_CANONNAME))
00173 ai->ai_canonname = h ? av_strdup(h->h_name) : NULL;
00174
00175 ai->ai_next = NULL;
00176 return 0;
00177 }
00178
00179 void ff_freeaddrinfo(struct addrinfo *res)
00180 {
00181 #if HAVE_WINSOCK2_H
00182 void (WSAAPI *win_freeaddrinfo)(struct addrinfo *res);
00183 HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
00184 win_freeaddrinfo = (void (WSAAPI *)(struct addrinfo *res))
00185 GetProcAddress(ws2mod, "freeaddrinfo");
00186 if (win_freeaddrinfo) {
00187 win_freeaddrinfo(res);
00188 return;
00189 }
00190 #endif
00191
00192 av_free(res->ai_canonname);
00193 av_free(res->ai_addr);
00194 av_free(res);
00195 }
00196
00197 int ff_getnameinfo(const struct sockaddr *sa, int salen,
00198 char *host, int hostlen,
00199 char *serv, int servlen, int flags)
00200 {
00201 const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
00202
00203 #if HAVE_WINSOCK2_H
00204 int (WSAAPI *win_getnameinfo)(const struct sockaddr *sa, socklen_t salen,
00205 char *host, DWORD hostlen,
00206 char *serv, DWORD servlen, int flags);
00207 HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
00208 win_getnameinfo = GetProcAddress(ws2mod, "getnameinfo");
00209 if (win_getnameinfo)
00210 return win_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
00211 #endif
00212
00213 if (sa->sa_family != AF_INET)
00214 return EAI_FAMILY;
00215 if (!host && !serv)
00216 return EAI_NONAME;
00217
00218 if (host && hostlen > 0) {
00219 struct hostent *ent = NULL;
00220 uint32_t a;
00221 if (!(flags & NI_NUMERICHOST))
00222 ent = gethostbyaddr((const char *)&sin->sin_addr,
00223 sizeof(sin->sin_addr), AF_INET);
00224
00225 if (ent) {
00226 snprintf(host, hostlen, "%s", ent->h_name);
00227 } else if (flags & NI_NAMERQD) {
00228 return EAI_NONAME;
00229 } else {
00230 a = ntohl(sin->sin_addr.s_addr);
00231 snprintf(host, hostlen, "%d.%d.%d.%d",
00232 ((a >> 24) & 0xff), ((a >> 16) & 0xff),
00233 ((a >> 8) & 0xff), (a & 0xff));
00234 }
00235 }
00236
00237 if (serv && servlen > 0) {
00238 struct servent *ent = NULL;
00239 #if HAVE_GETSERVBYPORT
00240 if (!(flags & NI_NUMERICSERV))
00241 ent = getservbyport(sin->sin_port, flags & NI_DGRAM ? "udp" : "tcp");
00242 #endif
00243
00244 if (ent)
00245 snprintf(serv, servlen, "%s", ent->s_name);
00246 else
00247 snprintf(serv, servlen, "%d", ntohs(sin->sin_port));
00248 }
00249
00250 return 0;
00251 }
00252 #endif
00253
00254 #if !HAVE_GETADDRINFO || HAVE_WINSOCK2_H
00255 const char *ff_gai_strerror(int ecode)
00256 {
00257 switch (ecode) {
00258 case EAI_AGAIN:
00259 return "Temporary failure in name resolution";
00260 case EAI_BADFLAGS:
00261 return "Invalid flags for ai_flags";
00262 case EAI_FAIL:
00263 return "A non-recoverable error occurred";
00264 case EAI_FAMILY:
00265 return "The address family was not recognized or the address "
00266 "length was invalid for the specified family";
00267 case EAI_MEMORY:
00268 return "Memory allocation failure";
00269 #if EAI_NODATA != EAI_NONAME
00270 case EAI_NODATA:
00271 return "No address associated with hostname";
00272 #endif
00273 case EAI_NONAME:
00274 return "The name does not resolve for the supplied parameters";
00275 case EAI_SERVICE:
00276 return "servname not supported for ai_socktype";
00277 case EAI_SOCKTYPE:
00278 return "ai_socktype not supported";
00279 }
00280
00281 return "Unknown error";
00282 }
00283 #endif
00284
00285 int ff_socket_nonblock(int socket, int enable)
00286 {
00287 #if HAVE_WINSOCK2_H
00288 u_long param = enable;
00289 return ioctlsocket(socket, FIONBIO, ¶m);
00290 #else
00291 if (enable)
00292 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
00293 else
00294 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
00295 #endif
00296 }
00297
00298 #if !HAVE_POLL_H
00299 int ff_poll(struct pollfd *fds, nfds_t numfds, int timeout)
00300 {
00301 fd_set read_set;
00302 fd_set write_set;
00303 fd_set exception_set;
00304 nfds_t i;
00305 int n;
00306 int rc;
00307
00308 #if HAVE_WINSOCK2_H
00309 if (numfds >= FD_SETSIZE) {
00310 errno = EINVAL;
00311 return -1;
00312 }
00313 #endif
00314
00315 FD_ZERO(&read_set);
00316 FD_ZERO(&write_set);
00317 FD_ZERO(&exception_set);
00318
00319 n = 0;
00320 for (i = 0; i < numfds; i++) {
00321 if (fds[i].fd < 0)
00322 continue;
00323 #if !HAVE_WINSOCK2_H
00324 if (fds[i].fd >= FD_SETSIZE) {
00325 errno = EINVAL;
00326 return -1;
00327 }
00328 #endif
00329
00330 if (fds[i].events & POLLIN)
00331 FD_SET(fds[i].fd, &read_set);
00332 if (fds[i].events & POLLOUT)
00333 FD_SET(fds[i].fd, &write_set);
00334 if (fds[i].events & POLLERR)
00335 FD_SET(fds[i].fd, &exception_set);
00336
00337 if (fds[i].fd >= n)
00338 n = fds[i].fd + 1;
00339 }
00340
00341 if (n == 0)
00342
00343 return 0;
00344
00345 if (timeout < 0) {
00346 rc = select(n, &read_set, &write_set, &exception_set, NULL);
00347 } else {
00348 struct timeval tv;
00349 tv.tv_sec = timeout / 1000;
00350 tv.tv_usec = 1000 * (timeout % 1000);
00351 rc = select(n, &read_set, &write_set, &exception_set, &tv);
00352 }
00353
00354 if (rc < 0)
00355 return rc;
00356
00357 for (i = 0; i < numfds; i++) {
00358 fds[i].revents = 0;
00359
00360 if (FD_ISSET(fds[i].fd, &read_set))
00361 fds[i].revents |= POLLIN;
00362 if (FD_ISSET(fds[i].fd, &write_set))
00363 fds[i].revents |= POLLOUT;
00364 if (FD_ISSET(fds[i].fd, &exception_set))
00365 fds[i].revents |= POLLERR;
00366 }
00367
00368 return rc;
00369 }
00370 #endif
00371 #endif