00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "config.h"
00023 #include "avformat.h"
00024 #include <unistd.h>
00025 #include <fcntl.h>
00026 #include "os_support.h"
00027
00028 #ifdef CONFIG_NETWORK
00029 #ifndef HAVE_SYS_POLL_H
00030 #ifdef HAVE_WINSOCK2_H
00031 #include <winsock2.h>
00032 #elif defined (HAVE_SYS_SELECT_H)
00033 #include <sys/select.h>
00034 #endif
00035 #endif
00036
00037 #include "network.h"
00038
00039 #if !defined(HAVE_INET_ATON)
00040 #include <stdlib.h>
00041 #include <strings.h>
00042
00043 int inet_aton (const char * str, struct in_addr * add)
00044 {
00045 unsigned int add1 = 0, add2 = 0, add3 = 0, add4 = 0;
00046
00047 if (sscanf(str, "%d.%d.%d.%d", &add1, &add2, &add3, &add4) != 4)
00048 return 0;
00049
00050 if (!add1 || (add1|add2|add3|add4) > 255) return 0;
00051
00052 add->s_addr=(add4<<24)+(add3<<16)+(add2<<8)+add1;
00053
00054 return 1;
00055 }
00056 #endif
00057
00058
00059 int resolve_host(struct in_addr *sin_addr, const char *hostname)
00060 {
00061 struct hostent *hp;
00062
00063 if (!inet_aton(hostname, sin_addr)) {
00064 hp = gethostbyname(hostname);
00065 if (!hp)
00066 return -1;
00067 memcpy(sin_addr, hp->h_addr, sizeof(struct in_addr));
00068 }
00069 return 0;
00070 }
00071
00072 int ff_socket_nonblock(int socket, int enable)
00073 {
00074 #ifdef HAVE_WINSOCK2_H
00075 return ioctlsocket(socket, FIONBIO, &enable);
00076 #else
00077 if (enable)
00078 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
00079 else
00080 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
00081 #endif
00082 }
00083 #endif
00084
00085 #ifdef CONFIG_FFSERVER
00086 #ifndef HAVE_SYS_POLL_H
00087 int poll(struct pollfd *fds, nfds_t numfds, int timeout)
00088 {
00089 fd_set read_set;
00090 fd_set write_set;
00091 fd_set exception_set;
00092 nfds_t i;
00093 int n;
00094 int rc;
00095
00096 #ifdef HAVE_WINSOCK2_H
00097 if (numfds >= FD_SETSIZE) {
00098 errno = EINVAL;
00099 return -1;
00100 }
00101 #endif
00102
00103 FD_ZERO(&read_set);
00104 FD_ZERO(&write_set);
00105 FD_ZERO(&exception_set);
00106
00107 n = -1;
00108 for(i = 0; i < numfds; i++) {
00109 if (fds[i].fd < 0)
00110 continue;
00111 #ifndef HAVE_WINSOCK2_H
00112 if (fds[i].fd >= FD_SETSIZE) {
00113 errno = EINVAL;
00114 return -1;
00115 }
00116 #endif
00117
00118 if (fds[i].events & POLLIN) FD_SET(fds[i].fd, &read_set);
00119 if (fds[i].events & POLLOUT) FD_SET(fds[i].fd, &write_set);
00120 if (fds[i].events & POLLERR) FD_SET(fds[i].fd, &exception_set);
00121
00122 if (fds[i].fd > n)
00123 n = fds[i].fd;
00124 };
00125
00126 if (n == -1)
00127
00128 return 0;
00129
00130 if (timeout < 0)
00131 rc = select(n+1, &read_set, &write_set, &exception_set, NULL);
00132 else {
00133 struct timeval tv;
00134
00135 tv.tv_sec = timeout / 1000;
00136 tv.tv_usec = 1000 * (timeout % 1000);
00137 rc = select(n+1, &read_set, &write_set, &exception_set, &tv);
00138 };
00139
00140 if (rc < 0)
00141 return rc;
00142
00143 for(i = 0; i < (nfds_t) n; i++) {
00144 fds[i].revents = 0;
00145
00146 if (FD_ISSET(fds[i].fd, &read_set)) fds[i].revents |= POLLIN;
00147 if (FD_ISSET(fds[i].fd, &write_set)) fds[i].revents |= POLLOUT;
00148 if (FD_ISSET(fds[i].fd, &exception_set)) fds[i].revents |= POLLERR;
00149 };
00150
00151 return rc;
00152 }
00153 #endif
00154 #endif
00155