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
00031
00032
00033
00034
00035
00036
00037 #include "str.h"
00038 #include "dprint.h"
00039 #include "mem/mem.h"
00040
00041
00042
00043 struct host_alias{
00044 str alias;
00045 unsigned short port;
00046 unsigned short proto;
00047 struct host_alias* next;
00048 };
00049
00050
00051 extern struct host_alias* aliases;
00052
00053
00054
00055
00056
00057 static inline int grep_aliases(char* name, int len, unsigned short port,
00058 unsigned short proto)
00059 {
00060 struct host_alias* a;
00061
00062 #ifdef USE_IPV6
00063 if ((len>2)&&((*name)=='[')&&(name[len-1]==']')){
00064
00065 name++;
00066 len-=2;
00067 }
00068 #endif
00069 for(a=aliases;a;a=a->next)
00070 if ((a->alias.len==len) && ((a->port==0) || (port==0) ||
00071 (a->port==port)) && ((a->proto==0) || (proto==0) ||
00072 (a->proto==proto)) && (strncasecmp(a->alias.s, name, len)==0))
00073 return 1;
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 LOG(L_ERR, "ERROR: add_alias: memory allocation error\n");
00113 if (a) pkg_free(a);
00114 return -1;
00115 }
00116
00117
00118