00001 /* 00002 * Various utilities for ffmpeg system 00003 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard 00004 * copyright (c) 2002 Francois Revol 00005 * 00006 * This file is part of FFmpeg. 00007 * 00008 * FFmpeg is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * FFmpeg is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with FFmpeg; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00023 /* needed by inet_aton() */ 00024 #define _SVID_SOURCE 00025 00026 #include "config.h" 00027 #include "avformat.h" 00028 #include <unistd.h> 00029 #include <fcntl.h> 00030 #include <sys/time.h> 00031 #include "os_support.h" 00032 00033 #if CONFIG_NETWORK 00034 #if !HAVE_POLL_H 00035 #if HAVE_WINSOCK2_H 00036 #include <winsock2.h> 00037 #elif HAVE_SYS_SELECT_H 00038 #include <sys/select.h> 00039 #endif 00040 #endif 00041 00042 #include "network.h" 00043 00044 #if !HAVE_INET_ATON 00045 #include <stdlib.h> 00046 #include <strings.h> 00047 00048 int inet_aton (const char * str, struct in_addr * add) 00049 { 00050 unsigned int add1 = 0, add2 = 0, add3 = 0, add4 = 0; 00051 00052 if (sscanf(str, "%d.%d.%d.%d", &add1, &add2, &add3, &add4) != 4) 00053 return 0; 00054 00055 if (!add1 || (add1|add2|add3|add4) > 255) return 0; 00056 00057 add->s_addr=(add4<<24)+(add3<<16)+(add2<<8)+add1; 00058 00059 return 1; 00060 } 00061 #endif /* !HAVE_INET_ATON */ 00062 00063 /* resolve host with also IP address parsing */ 00064 int resolve_host(struct in_addr *sin_addr, const char *hostname) 00065 { 00066 struct hostent *hp; 00067 00068 if (!inet_aton(hostname, sin_addr)) { 00069 hp = gethostbyname(hostname); 00070 if (!hp) 00071 return -1; 00072 memcpy(sin_addr, hp->h_addr_list[0], sizeof(struct in_addr)); 00073 } 00074 return 0; 00075 } 00076 00077 int ff_socket_nonblock(int socket, int enable) 00078 { 00079 #if HAVE_WINSOCK2_H 00080 return ioctlsocket(socket, FIONBIO, &enable); 00081 #else 00082 if (enable) 00083 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK); 00084 else 00085 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK); 00086 #endif 00087 } 00088 #endif /* CONFIG_NETWORK */ 00089 00090 #if CONFIG_FFSERVER 00091 #if !HAVE_POLL_H 00092 int poll(struct pollfd *fds, nfds_t numfds, int timeout) 00093 { 00094 fd_set read_set; 00095 fd_set write_set; 00096 fd_set exception_set; 00097 nfds_t i; 00098 int n; 00099 int rc; 00100 00101 #if HAVE_WINSOCK2_H 00102 if (numfds >= FD_SETSIZE) { 00103 errno = EINVAL; 00104 return -1; 00105 } 00106 #endif 00107 00108 FD_ZERO(&read_set); 00109 FD_ZERO(&write_set); 00110 FD_ZERO(&exception_set); 00111 00112 n = -1; 00113 for(i = 0; i < numfds; i++) { 00114 if (fds[i].fd < 0) 00115 continue; 00116 #if !HAVE_WINSOCK2_H 00117 if (fds[i].fd >= FD_SETSIZE) { 00118 errno = EINVAL; 00119 return -1; 00120 } 00121 #endif 00122 00123 if (fds[i].events & POLLIN) FD_SET(fds[i].fd, &read_set); 00124 if (fds[i].events & POLLOUT) FD_SET(fds[i].fd, &write_set); 00125 if (fds[i].events & POLLERR) FD_SET(fds[i].fd, &exception_set); 00126 00127 if (fds[i].fd > n) 00128 n = fds[i].fd; 00129 }; 00130 00131 if (n == -1) 00132 /* Hey!? Nothing to poll, in fact!!! */ 00133 return 0; 00134 00135 if (timeout < 0) 00136 rc = select(n+1, &read_set, &write_set, &exception_set, NULL); 00137 else { 00138 struct timeval tv; 00139 00140 tv.tv_sec = timeout / 1000; 00141 tv.tv_usec = 1000 * (timeout % 1000); 00142 rc = select(n+1, &read_set, &write_set, &exception_set, &tv); 00143 }; 00144 00145 if (rc < 0) 00146 return rc; 00147 00148 for(i = 0; i < (nfds_t) n; i++) { 00149 fds[i].revents = 0; 00150 00151 if (FD_ISSET(fds[i].fd, &read_set)) fds[i].revents |= POLLIN; 00152 if (FD_ISSET(fds[i].fd, &write_set)) fds[i].revents |= POLLOUT; 00153 if (FD_ISSET(fds[i].fd, &exception_set)) fds[i].revents |= POLLERR; 00154 }; 00155 00156 return rc; 00157 } 00158 #endif /* HAVE_POLL_H */ 00159 #endif /* CONFIG_FFSERVER */ 00160