Coverage Report

Created: 2025-07-18 06:32

/src/opensips/name_alias.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2001-2003 FhG Fokus
3
 * Copyright (C) 2009 Voice Sistem SRL
4
 *
5
 * This file is part of opensips, a free SIP server.
6
 *
7
 * opensips is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 2 of the License, or
10
 * (at your option) any later version
11
 *
12
 * opensips is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
20
 *
21
 * History:
22
 * --------
23
 *  2009-02-02  splitted from name_alias.h (bogdan)
24
 *  2009-02-01  added interface to registed additional functions for checking
25
 *              the aliases (bogdan)
26
 */
27
28
#include <string.h>
29
#include "name_alias.h"
30
31
struct host_alias* aliases=0; /* name aliases list */
32
33
struct alias_function* alias_fcts = NULL;
34
35
36
37
/* adds an alias to the list (only if it isn't already there)
38
 * if port==0, the alias will match all the ports
39
 * if proto==0, the alias will match all the protocols
40
 * returns 1 if a new alias was added, 0 if a matching alias was already on
41
 * the list and  -1 on error */
42
int add_alias(char* name, int len, unsigned short port, unsigned short proto, int accept_subdomain)
43
0
{
44
0
  struct host_alias* a;
45
46
0
  if ((port) && (proto)){
47
    /* don't add if there is already an alias matching it */
48
0
    if (grep_aliases(name,len, port, proto)) return 0;
49
0
  }else{
50
    /* don't add if already in the list with port or proto ==0*/
51
0
    for(a=aliases;a;a=a->next)
52
0
      if ((a->alias.len==len) && (a->port==port) && (a->proto==proto) &&
53
0
          (strncasecmp(a->alias.s, name, len)==0))
54
0
        return 0;
55
0
  }
56
0
  a=(struct host_alias*)pkg_malloc(sizeof(struct host_alias));
57
0
  if(a==0) goto error;
58
0
  a->alias.s=(char*)pkg_malloc(len+1);
59
0
  if (a->alias.s==0) goto error;
60
0
  a->alias.len=len;
61
0
  memcpy(a->alias.s, name, len);
62
0
  a->alias.s[len]=0; /* null terminate for easier printing*/
63
0
  a->port=port;
64
0
  a->proto=proto;
65
0
  a->accept_subdomain=accept_subdomain;
66
0
  a->next=aliases;
67
0
  aliases=a;
68
0
  return 1;
69
0
error:
70
0
  LM_ERR("pkg memory allocation error\n");
71
0
  if (a) pkg_free(a);
72
0
  return -1;
73
0
}
74
75
76
int register_alias_fct( is_alias_fct *fct )
77
0
{
78
0
  struct alias_function *af;
79
80
0
  af = (struct alias_function *)pkg_malloc(sizeof(struct alias_function));
81
0
  if (af==NULL) {
82
0
    LM_ERR("no more pkg mem\n");
83
0
    return -1;
84
0
  }
85
86
0
  af->alias_f = fct;
87
0
  af->next = alias_fcts;
88
0
  alias_fcts = af;
89
90
0
  return 0;
91
0
}
92
93