• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • Directories
  • File List
  • Globals

name_alias.h

00001 /*
00002  * $Id$
00003  *
00004  *
00005  * Copyright (C) 2001-2003 FhG Fokus
00006  *
00007  * This file is part of ser, a free SIP server.
00008  *
00009  * ser is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version
00013  *
00014  * For a license to use the ser software under conditions
00015  * other than those described here, or to purchase support for this
00016  * software, please contact iptel.org by e-mail at the following addresses:
00017  *    info@iptel.org
00018  *
00019  * ser is distributed in the hope that it will be useful,
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022  * GNU General Public License for more details.
00023  *
00024  * You should have received a copy of the GNU General Public License 
00025  * along with this program; if not, write to the Free Software 
00026  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00027  */
00028 /*
00029  * History:
00030  * --------
00031  *  2003-03-19  replaced all mallocs/frees w/ pkg_malloc/pkg_free (andrei)
00032  *  2003-10-21  support for proto added: proto:host:port (andrei)
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 /* returns 1 if  name is in the alias list; if port=0, port no is ignored
00056  * if proto=0, proto is ignored*/
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                 /* ipv6 reference, skip [] */
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 /* adds an alias to the list (only if it isn't already there)
00080  * if port==0, the alias will match all the ports
00081  * if proto==0, the alias will match all the protocols
00082  * returns 1 if a new alias was added, 0 if a matching alias was already on
00083  * the list and  -1 on error */
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                 /* don't add if there is already an alias matching it */
00091                 if (grep_aliases(name,len, port, proto)) return 0;
00092         }else{
00093                 /* don't add if already in the list with port or proto ==0*/
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; /* null terminate for easier printing*/
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 

Generated on Tue May 22 2012 13:10:11 for SIP Router by  doxygen 1.7.1