| 1 | /* |
|---|
| 2 | PowerDNS Versatile Database Driven Nameserver |
|---|
| 3 | Copyright (C) 2002 PowerDNS.COM BV |
|---|
| 4 | |
|---|
| 5 | This program is free software; you can redistribute it and/or modify |
|---|
| 6 | it under the terms of the GNU General Public License as published by |
|---|
| 7 | the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | (at your option) any later version. |
|---|
| 9 | |
|---|
| 10 | This program is distributed in the hope that it will be useful, |
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | GNU General Public License for more details. |
|---|
| 14 | |
|---|
| 15 | You should have received a copy of the GNU General Public License |
|---|
| 16 | along with this program; if not, write to the Free Software |
|---|
| 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 18 | */ |
|---|
| 19 | #include "misc.hh" |
|---|
| 20 | #include <vector> |
|---|
| 21 | #include <sstream> |
|---|
| 22 | #include <errno.h> |
|---|
| 23 | #include <cstring> |
|---|
| 24 | |
|---|
| 25 | #include <iomanip> |
|---|
| 26 | #include <string.h> |
|---|
| 27 | #include <stdlib.h> |
|---|
| 28 | #include <stdio.h> |
|---|
| 29 | #include <sys/types.h> |
|---|
| 30 | |
|---|
| 31 | #ifndef WIN32 |
|---|
| 32 | # include <sys/param.h> |
|---|
| 33 | # include <netdb.h> |
|---|
| 34 | # include <sys/time.h> |
|---|
| 35 | # include <time.h> |
|---|
| 36 | # include <netinet/in.h> |
|---|
| 37 | # include <unistd.h> |
|---|
| 38 | #endif // WIN32 |
|---|
| 39 | |
|---|
| 40 | #include "utility.hh" |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | int sendData(const char *buffer, int replen, int outsock) |
|---|
| 45 | { |
|---|
| 46 | u_int16_t nlen=htons(replen); |
|---|
| 47 | Utility::iovec iov[2]; |
|---|
| 48 | iov[0].iov_base=(char*)&nlen; |
|---|
| 49 | iov[0].iov_len=2; |
|---|
| 50 | iov[1].iov_base=(char*)buffer; |
|---|
| 51 | iov[1].iov_len=replen; |
|---|
| 52 | int ret=Utility::writev(outsock,iov,2); |
|---|
| 53 | |
|---|
| 54 | if(ret<0) { |
|---|
| 55 | return -1; |
|---|
| 56 | } |
|---|
| 57 | if(ret!=replen+2) { |
|---|
| 58 | return -1; |
|---|
| 59 | } |
|---|
| 60 | return 0; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | void parseService(const string &descr, ServiceTuple &st) |
|---|
| 65 | { |
|---|
| 66 | vector<string>parts; |
|---|
| 67 | stringtok(parts,descr,":"); |
|---|
| 68 | st.host=parts[0]; |
|---|
| 69 | if(parts.size()>1) |
|---|
| 70 | st.port=atoi(parts[1].c_str()); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | int matchNetmask(const char *address, const char *omask) |
|---|
| 74 | { |
|---|
| 75 | struct in_addr a,m; |
|---|
| 76 | int bits=32; |
|---|
| 77 | char *sep; |
|---|
| 78 | |
|---|
| 79 | char *mask=strdup(omask); |
|---|
| 80 | sep=strchr(mask,'/'); |
|---|
| 81 | |
|---|
| 82 | if(sep) { |
|---|
| 83 | bits=atoi(sep+1); |
|---|
| 84 | *sep=0; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | if(!Utility::inet_aton(address, &a) || !Utility::inet_aton(mask, &m)) |
|---|
| 88 | { |
|---|
| 89 | free(mask); |
|---|
| 90 | return -1; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | free(mask); |
|---|
| 94 | |
|---|
| 95 | // bits==32 -> 0xffffffff |
|---|
| 96 | // bits==16 -> 0xffff0000 |
|---|
| 97 | // bits==0 -> 0x00000000 |
|---|
| 98 | unsigned int bmask=~((1<<(32-bits))-1); // 1<<16 0000 0000 0000 0000 0000 0000 0000 0000 |
|---|
| 99 | |
|---|
| 100 | /* |
|---|
| 101 | fprintf(stderr,"%x\n",bmask); |
|---|
| 102 | fprintf(stderr,"%x\n",(htonl((unsigned int)a.s_addr) & bmask)); |
|---|
| 103 | fprintf(stderr,"%x\n",(htonl((unsigned int)m.s_addr) & bmask)); |
|---|
| 104 | */ |
|---|
| 105 | |
|---|
| 106 | return ((htonl((unsigned int)a.s_addr) & bmask) == (htonl((unsigned int)m.s_addr) & bmask)); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | int waitForData(int fd, int seconds) |
|---|
| 110 | { |
|---|
| 111 | struct timeval tv; |
|---|
| 112 | int ret; |
|---|
| 113 | |
|---|
| 114 | tv.tv_sec = seconds; |
|---|
| 115 | tv.tv_usec = 0; |
|---|
| 116 | |
|---|
| 117 | fd_set readfds; |
|---|
| 118 | FD_ZERO( &readfds ); |
|---|
| 119 | FD_SET( fd, &readfds ); |
|---|
| 120 | |
|---|
| 121 | ret = select( fd + 1, &readfds, NULL, NULL, &tv ); |
|---|
| 122 | if ( ret == -1 ) |
|---|
| 123 | { |
|---|
| 124 | ret = -1; |
|---|
| 125 | errno = ETIMEDOUT; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | return ret; |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | string humanDuration(time_t passed) |
|---|
| 133 | { |
|---|
| 134 | ostringstream ret; |
|---|
| 135 | if(passed<60) |
|---|
| 136 | ret<<passed<<" seconds"; |
|---|
| 137 | else if(passed<3600) |
|---|
| 138 | ret<<setprecision(2)<<passed/60.0<<" minutes"; |
|---|
| 139 | else if(passed<86400) |
|---|
| 140 | ret<<setprecision(3)<<passed/3600.0<<" hours"; |
|---|
| 141 | else if(passed<(86400*30.41)) |
|---|
| 142 | ret<<setprecision(3)<<passed/86400.0<<" days"; |
|---|
| 143 | else |
|---|
| 144 | ret<<setprecision(3)<<passed/(86400*30.41)<<" months"; |
|---|
| 145 | |
|---|
| 146 | return ret.str(); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | DTime::DTime() |
|---|
| 150 | { |
|---|
| 151 | // set(); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | DTime::DTime(const DTime &dt) |
|---|
| 155 | { |
|---|
| 156 | d_set=dt.d_set; |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | time_t DTime::time() |
|---|
| 160 | { |
|---|
| 161 | return d_set.tv_sec; |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | // Make s uppercase: |
|---|
| 165 | void upperCase(string& s) { |
|---|
| 166 | for(unsigned int i = 0; i < s.length(); i++) |
|---|
| 167 | s[i] = toupper(s[i]); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | void chomp(string &line, const string &delim) |
|---|
| 172 | { |
|---|
| 173 | string::reverse_iterator i; |
|---|
| 174 | for( i=line.rbegin();i!=line.rend();++i) |
|---|
| 175 | if(delim.find(*i)==string::npos) |
|---|
| 176 | break; |
|---|
| 177 | |
|---|
| 178 | line.resize(line.rend()-i); |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | void stripLine(string &line) |
|---|
| 185 | { |
|---|
| 186 | unsigned int pos=line.find_first_of("\r\n"); |
|---|
| 187 | if(pos!=string::npos) { |
|---|
| 188 | line.resize(pos); |
|---|
| 189 | } |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | string urlEncode(const string &text) |
|---|
| 193 | { |
|---|
| 194 | string ret; |
|---|
| 195 | for(string::const_iterator i=text.begin();i!=text.end();++i) |
|---|
| 196 | if(*i==' ')ret.append("%20"); |
|---|
| 197 | else ret.append(1,*i); |
|---|
| 198 | return ret; |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | string getHostname() |
|---|
| 202 | { |
|---|
| 203 | #ifdef WIN32 |
|---|
| 204 | # define MAXHOSTNAMELEN 1025 |
|---|
| 205 | #endif // WIN32 |
|---|
| 206 | |
|---|
| 207 | char tmp[MAXHOSTNAMELEN]; |
|---|
| 208 | if(gethostname(tmp, MAXHOSTNAMELEN)) |
|---|
| 209 | return "UNKNOWN"; |
|---|
| 210 | |
|---|
| 211 | return tmp; |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | string itoa(int i) |
|---|
| 215 | { |
|---|
| 216 | ostringstream o; |
|---|
| 217 | o<<i; |
|---|
| 218 | return o.str(); |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | string stringerror() |
|---|
| 222 | { |
|---|
| 223 | return strerror(errno); |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | void cleanSlashes(string &str) |
|---|
| 227 | { |
|---|
| 228 | string::const_iterator i; |
|---|
| 229 | string out; |
|---|
| 230 | for(i=str.begin();i!=str.end();++i) { |
|---|
| 231 | if(*i=='/' && i!=str.begin() && *(i-1)=='/') |
|---|
| 232 | continue; |
|---|
| 233 | out.append(1,*i); |
|---|
| 234 | } |
|---|
| 235 | str=out; |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | const string sockAddrToString(struct sockaddr_in *remote, Utility::socklen_t socklen) |
|---|
| 239 | { |
|---|
| 240 | if(socklen==sizeof(struct sockaddr_in)) |
|---|
| 241 | return inet_ntoa(((struct sockaddr_in *)remote)->sin_addr); |
|---|
| 242 | #ifdef HAVE_IPV6 |
|---|
| 243 | else { |
|---|
| 244 | char tmp[128]; |
|---|
| 245 | |
|---|
| 246 | if(!Utility::inet_ntop(AF_INET6, ( const char * ) &((struct sockaddr_in6 *)remote)->sin6_addr, tmp, sizeof(tmp))) |
|---|
| 247 | return "IPv6 untranslateable"; |
|---|
| 248 | |
|---|
| 249 | return tmp; |
|---|
| 250 | } |
|---|
| 251 | #endif |
|---|
| 252 | |
|---|
| 253 | return "untranslateable"; |
|---|
| 254 | } |
|---|