00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "str.h"
00031 #include "dprint.h"
00032 #include "mem/mem.h"
00033
00034
00035
00036 struct host_alias{
00037 str alias;
00038 unsigned short port;
00039 unsigned short proto;
00040 struct host_alias* next;
00041 };
00042
00043
00044 extern struct host_alias* aliases;
00045
00046
00047
00048
00049
00050 static inline int grep_aliases(char* name, int len, unsigned short port,
00051 unsigned short proto)
00052 {
00053 struct host_alias* a;
00054
00055 #ifdef USE_IPV6
00056 if ((len>2)&&((*name)=='[')&&(name[len-1]==']')){
00057
00058 name++;
00059 len-=2;
00060 }
00061 #endif
00062 for(a=aliases;a;a=a->next) {
00063 LM_DBG("comparing host [%d:%.*s:%d] with us [%d:%.*s:%d]\n",
00064 proto, len, name, port,
00065 a->proto, a->alias.len, a->alias.s, a->port);
00066 if ((a->alias.len==len) && ((a->port==0) || (port==0) ||
00067 (a->port==port)) && ((a->proto==0) || (proto==0) ||
00068 (a->proto==proto)) && (strncasecmp(a->alias.s, name, len)==0)) {
00069 LM_DBG("match found for: [%d:%.*s:%d]\n", proto, len, name, port);
00070 return 1;
00071 }
00072 }
00073 LM_DBG("no match for: [%d:%.*s:%d]\n", proto, len, name, port);
00074 return 0;
00075 }
00076
00077
00078
00079
00080
00081
00082
00083
00084 static inline int add_alias(char* name, int len, unsigned short port,
00085 unsigned short proto)
00086 {
00087 struct host_alias* a;
00088
00089 if ((port) && (proto)){
00090
00091 if (grep_aliases(name,len, port, proto)) return 0;
00092 }else{
00093
00094 for(a=aliases;a;a=a->next)
00095 if ((a->alias.len==len) && (a->port==port) && (a->proto==proto) &&
00096 (strncasecmp(a->alias.s, name, len)==0))
00097 return 0;
00098 }
00099 a=(struct host_alias*)pkg_malloc(sizeof(struct host_alias));
00100 if(a==0) goto error;
00101 a->alias.s=(char*)pkg_malloc(len+1);
00102 if (a->alias.s==0) goto error;
00103 a->alias.len=len;
00104 memcpy(a->alias.s, name, len);
00105 a->alias.s[len]=0;
00106 a->port=port;
00107 a->proto=proto;
00108 a->next=aliases;
00109 aliases=a;
00110 return 1;
00111 error:
00112 LM_ERR("pkg memory allocation error\n");
00113 if (a) pkg_free(a);
00114 return -1;
00115 }
00116
00117
00118