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