diff options
Diffstat (limited to 'src/mt/lookup.cc')
| -rw-r--r-- | src/mt/lookup.cc | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/src/mt/lookup.cc b/src/mt/lookup.cc new file mode 100644 index 0000000..c0e72c8 --- /dev/null +++ b/src/mt/lookup.cc | |||
| @@ -0,0 +1,88 @@ | |||
| 1 | /************************************************************************* | ||
| 2 | * | ||
| 3 | * HTCd - Copyright (C) 1998-2006 Henrik Rydberg | ||
| 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 18 | */ | ||
| 19 | |||
| 20 | #include <mt/lookup.h> | ||
| 21 | #include <mt/mvec.h> | ||
| 22 | #include <mt/lock.h> | ||
| 23 | #include <arpa/inet.h> | ||
| 24 | #include <netinet/in.h> | ||
| 25 | #include <sys/socket.h> | ||
| 26 | |||
| 27 | const time_t VALIDSEC=120; | ||
| 28 | static msvec<IPnode> cache; | ||
| 29 | static mutex_t mutex; | ||
| 30 | |||
| 31 | int Lookup(IPnode& node,const in_addr& ia) | ||
| 32 | { | ||
| 33 | MLOCK(mutex); | ||
| 34 | IPnode* p=cache.begin(); | ||
| 35 | time_t now=time(0); | ||
| 36 | while(p<cache.end()) { | ||
| 37 | if(now>p->valid) cache.erase(p); | ||
| 38 | else if(memcmp(&p->addr,&ia,sizeof(ia))==0) {p->valid=now+VALIDSEC;node=*p;return 1;} | ||
| 39 | else p++; | ||
| 40 | } | ||
| 41 | hostent hent; | ||
| 42 | sref_t buf; | ||
| 43 | int herr; | ||
| 44 | hostent* hp=0; | ||
| 45 | gethostbyaddr_r((const char*)&ia,sizeof(ia),AF_INET, | ||
| 46 | &hent, | ||
| 47 | buf,sizeof(sref_t), | ||
| 48 | &hp, | ||
| 49 | &herr); | ||
| 50 | if(!hp) return 0; | ||
| 51 | memcpy(&node.addr,hp->h_addr,sizeof(hp->h_length)); | ||
| 52 | node.ip=inet_ntoa(node.addr); | ||
| 53 | node.name=hp->h_name; | ||
| 54 | node.valid=now+VALIDSEC; | ||
| 55 | cache.push_back(node); | ||
| 56 | return 1; | ||
| 57 | } | ||
| 58 | |||
| 59 | int Lookup(IPnode& node,const sref& s) | ||
| 60 | { | ||
| 61 | MLOCK(mutex); | ||
| 62 | IPnode* p=cache.begin(); | ||
| 63 | time_t now=time(0); | ||
| 64 | while(p<cache.end()) { | ||
| 65 | if(now>p->valid) cache.erase(p); | ||
| 66 | else if(p->ip==s||p->name==s) { p->valid=now+VALIDSEC; node=*p; return 1; } | ||
| 67 | else p++; | ||
| 68 | } | ||
| 69 | hostent hent; | ||
| 70 | sref_t sbuf,buf; | ||
| 71 | int herr; | ||
| 72 | hostent* hp=0; | ||
| 73 | for(int i=0;s[i];i++) if(s[i]!='.'&&(s[i]<'0'||s[i]>'9')) { | ||
| 74 | gethostbyname_r(s.copyto(sbuf),&hent,buf,sizeof(sref_t),&hp,&herr); | ||
| 75 | break; | ||
| 76 | } | ||
| 77 | if(!hp) { | ||
| 78 | unsigned long a=inet_addr(s.copyto(buf)); | ||
| 79 | gethostbyaddr_r((char*)&a,sizeof(a),AF_INET,&hent,buf,sizeof(sref_t),&hp,&herr); | ||
| 80 | } | ||
| 81 | if(!hp) return 0; | ||
| 82 | memcpy(&node.addr,hp->h_addr,sizeof(hp->h_length)); | ||
| 83 | node.ip=inet_ntoa(node.addr); | ||
| 84 | node.name=hp->h_name; | ||
| 85 | node.valid=now+VALIDSEC; | ||
| 86 | cache.push_back(node); | ||
| 87 | return 1; | ||
| 88 | } | ||
