Coverage Report

Created: 2025-07-11 06:28

/src/opensips/name_alias.h
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
 *  2003-03-19  replaced all mallocs/frees w/ pkg_malloc/pkg_free (andrei)
24
 *  2003-10-21  support for proto added: proto:host:port (andrei)
25
 *  2009-02-01  added interface to registed additional functions for checking
26
 *              the aliases (bogdan)
27
 */
28
29
#ifndef NAME_ALIAS_H
30
#define NAME_ALIAS_H
31
32
33
#include <strings.h>
34
#include "str.h"
35
#include "dprint.h"
36
#include "ip_addr.h"
37
#include "mem/mem.h"
38
39
#define si_alias_accept_subdomain(_flags) (int) _flags & SI_ACCEPT_SUBDOMAIN_ALIAS
40
41
struct host_alias{
42
  str alias;
43
  unsigned short port;
44
  unsigned short proto;
45
  int accept_subdomain;
46
  struct host_alias* next;
47
};
48
49
50
extern struct host_alias* aliases;
51
52
53
typedef int (is_alias_fct)(char* name, int len, unsigned short port,
54
    unsigned short proto);
55
56
struct alias_function {
57
  is_alias_fct *alias_f;
58
  struct alias_function *next;
59
};
60
61
extern struct alias_function* alias_fcts;
62
63
0
static inline int match_domain(char* alias, int alias_len, char* host, int host_len, int accept_subdomain) {
64
0
  int index_offset;
65
66
  /* Check if the alias is a subdomain alias and if so calculate the index offset to start the comparison
67
   * Given an alias my.domain.com or my.great.domain.com and a subdomain of domain.com the comparison should start at domain.com
68
   * a host of domain.com will also match, if the flag is not set then do a strict comparison
69
   */
70
0
  if (accept_subdomain) {
71
0
    index_offset = host_len - alias_len;
72
    // the host we're checking is a shorter len than the alias so no need to compare
73
0
    if (index_offset < 0) return 0;
74
    // if the offset is greater than 0 we need to ensure the host we're checking has a preceding '.' to ensure it's a subdomain
75
0
    else if (index_offset > 0 && !(*((host + index_offset) - 1) == '.')) return 0;
76
77
0
    if (strncasecmp(alias, host + index_offset, alias_len)==0)
78
0
      return 1;
79
0
  } else if (host_len == alias_len && strncasecmp(alias, host, host_len)==0) {
80
0
    return 1;
81
0
  }
82
83
0
  return 0;
84
0
}
Unexecuted instantiation: forward.c:match_domain
Unexecuted instantiation: socket_info.c:match_domain
Unexecuted instantiation: name_alias.c:match_domain
Unexecuted instantiation: cfg.tab.c:match_domain
85
86
/* returns 1 if  name is in the alias list; if port=0, port no is ignored
87
 * if proto=0, proto is ignored*/
88
static inline int grep_aliases(char* name, int len, unsigned short port,
89
                unsigned short proto)
90
0
{
91
0
  struct  host_alias* a;
92
0
  struct alias_function *af;
93
94
0
  if ((len>2)&&((*name)=='[')&&(name[len-1]==']')){
95
    /* ipv6 reference, skip [] */
96
0
    name++;
97
0
    len-=2;
98
0
  }
99
100
0
  for(a=aliases;a;a=a->next) {
101
0
    if (((a->port==0) || (port==0) || (a->port==port)) &&
102
0
        ((a->proto==0) || (proto==0) || (a->proto==proto))) {
103
0
      if (match_domain(a->alias.s, a->alias.len, name, len, a->accept_subdomain))
104
0
        return 1;
105
0
    }
106
0
  }
107
108
0
  for( af=alias_fcts ; af ; af=af->next ) {
109
0
    if ( af->alias_f(name,len,port,proto)>0 )
110
0
      return 1;
111
0
  }
112
0
  return 0;
113
0
}
Unexecuted instantiation: forward.c:grep_aliases
Unexecuted instantiation: socket_info.c:grep_aliases
Unexecuted instantiation: name_alias.c:grep_aliases
Unexecuted instantiation: cfg.tab.c:grep_aliases
114
115
/* adds an alias to the list (only if it isn't already there) */
116
int add_alias(char* name, int len, unsigned short port, unsigned short proto, int accept_subdomain);
117
118
/* register a new function for detecting aliases */
119
int register_alias_fct( is_alias_fct *fct );
120
121
#endif /* NAME_ALIAS_H */