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 (!(flags & NI_NUMERICSERV))
00240 ent = getservbyport(sin->sin_port, flags & NI_DGRAM ? "udp" : "tcp");
00241
00242 if (ent)
00243 snprintf(serv, servlen, "%s", ent->s_name);
00244 else
00245 snprintf(serv, servlen, "%d", ntohs(sin->sin_port));
00246 }
00247
00248 return 0;
00249 }
00250 #endif
00251
00252 #if !HAVE_GETADDRINFO || HAVE_WINSOCK2_H
00253 const char *ff_gai_strerror(int ecode)
00254 {
00255 switch (ecode) {
00256 case EAI_AGAIN:
00257 return "Temporary failure in name resolution";
00258 case EAI_BADFLAGS:
00259 return "Invalid flags for ai_flags";
00260 case EAI_FAIL:
00261 return "A non-recoverable error occurred";
00262 case EAI_FAMILY:
00263 return "The address family was not recognized or the address "
00264 "length was invalid for the specified family";
00265 case EAI_MEMORY:
00266 return "Memory allocation failure";
00267 #if EAI_NODATA != EAI_NONAME
00268 case EAI_NODATA:
00269 return "No address associated with hostname";
00270 #endif
00271 case EAI_NONAME:
00272 return "The name does not resolve for the supplied parameters";
00273 case EAI_SERVICE:
00274 return "servname not supported for ai_socktype";
00275 case EAI_SOCKTYPE:
00276 return "ai_socktype not supported";
00277 }
00278
00279 return "Unknown error";
00280 }
00281 #endif
00282
00283 int ff_socket_nonblock(int socket, int enable)
00284 {
00285 #if HAVE_WINSOCK2_H
00286 u_long param = enable;
00287 return ioctlsocket(socket, FIONBIO, ¶m);
00288 #else
00289 if (enable)
00290 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
00291 else
00292 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
00293 #endif
00294 }
00295
00296 #if !HAVE_POLL_H
00297 int ff_poll(struct pollfd *fds, nfds_t numfds, int timeout)
00298 {
00299 fd_set read_set;
00300 fd_set write_set;
00301 fd_set exception_set;
00302 nfds_t i;
00303 int n;
00304 int rc;
00305
00306 #if HAVE_WINSOCK2_H
00307 if (numfds >= FD_SETSIZE) {
00308 errno = EINVAL;
00309 return -1;
00310 }
00311 #endif
00312
00313 FD_ZERO(&read_set);
00314 FD_ZERO(&write_set);
00315 FD_ZERO(&exception_set);
00316
00317 n = 0;
00318 for (i = 0; i < numfds; i++) {
00319 if (fds[i].fd < 0)
00320 continue;
00321 #if !HAVE_WINSOCK2_H
00322 if (fds[i].fd >= FD_SETSIZE) {
00323 errno = EINVAL;
00324 return -1;
00325 }
00326 #endif
00327
00328 if (fds[i].events & POLLIN)
00329 FD_SET(fds[i].fd, &read_set);
00330 if (fds[i].events & POLLOUT)
00331 FD_SET(fds[i].fd, &write_set);
00332 if (fds[i].events & POLLERR)
00333 FD_SET(fds[i].fd, &exception_set);
00334
00335 if (fds[i].fd >= n)
00336 n = fds[i].fd + 1;
00337 }
00338
00339 if (n == 0)
00340
00341 return 0;
00342
00343 if (timeout < 0) {
00344 rc = select(n, &read_set, &write_set, &exception_set, NULL);
00345 } else {
00346 struct timeval tv;
00347 tv.tv_sec = timeout / 1000;
00348 tv.tv_usec = 1000 * (timeout % 1000);
00349 rc = select(n, &read_set, &write_set, &exception_set, &tv);
00350 }
00351
00352 if (rc < 0)
00353 return rc;
00354
00355 for (i = 0; i < numfds; i++) {
00356 fds[i].revents = 0;
00357
00358 if (FD_ISSET(fds[i].fd, &read_set))
00359 fds[i].revents |= POLLIN;
00360 if (FD_ISSET(fds[i].fd, &write_set))
00361 fds[i].revents |= POLLOUT;
00362 if (FD_ISSET(fds[i].fd, &exception_set))
00363 fds[i].revents |= POLLERR;
00364 }
00365
00366 return rc;
00367 }
00368 #endif
00369 #endif