suricata
util-proto-name.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2010 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Gurvinder Singh <gurvindersinghdahiya@gmail.com>
22  *
23  * File to provide the protocol names based on protocol numbers defined in the
24  * specified protocol file.
25  */
26 
27 #include "suricata-common.h"
28 #include "util-proto-name.h"
29 
30 static int init_once = 0;
31 
32 /**
33  * \brief Function to load the protocol names from the specified protocol
34  * file.
35  */
37 {
38  BUG_ON(init_once);
39  init_once++;
40  memset(known_proto, 0x00, sizeof(known_proto));
41 
42  /* Load the known protocols name from the /etc/protocols file */
43  FILE *fp = fopen(PROTO_FILE,"r");
44  if (fp != NULL) {
45  char line[200];
46  char *ptr = NULL;
47 
48  while(fgets(line, sizeof(line), fp) != NULL) {
49  if (line[0] == '#')
50  continue;
51 
52  char *name = strtok_r(line," \t", &ptr);
53  if (name == NULL)
54  continue;
55 
56  char *proto_ch = strtok_r(NULL," \t", &ptr);
57  if (proto_ch == NULL)
58  continue;
59 
60  int proto = atoi(proto_ch);
61  if (proto >= 255)
62  continue;
63 
64  char *cname = strtok_r(NULL, " \t", &ptr);
65 
66  if (known_proto[proto] != NULL) {
67  SCFree(known_proto[proto]);
68  }
69 
70  if (cname != NULL) {
71  known_proto[proto] = SCStrdup(cname);
72  } else {
73  known_proto[proto] = SCStrdup(name);
74  }
75  if (unlikely(known_proto[proto] == NULL)) {
76  SCLogError(SC_ERR_MEM_ALLOC, "Failed proto name allocation");
77  continue;
78  }
79  int proto_len = strlen(known_proto[proto]);
80  if (proto_len > 0 && known_proto[proto][proto_len - 1] == '\n')
81  known_proto[proto][proto_len - 1] = '\0';
82  }
83  fclose(fp);
84  }
85 }
86 
87 /**
88  * \brief Function to check if the received protocol number is valid and do
89  * we have corresponding name entry for this number or not.
90  *
91  * \param proto Protocol number to be validated
92  * \retval ret On success returns TRUE otherwise FALSE
93  */
94 uint8_t SCProtoNameValid(uint16_t proto)
95 {
96  uint8_t ret = FALSE;
97 
98  if (proto <= 255 && known_proto[proto] != NULL) {
99  ret = TRUE;
100  }
101 
102  return ret;
103 }
104 
105 /**
106  * \brief Function to clears the memory used in storing the protocol names.
107  */
109 {
110  int cnt;
111  /* clears the memory of loaded protocol names */
112  for (cnt = 0; cnt < 255; cnt++) {
113  if (known_proto[cnt] != NULL)
114  SCFree(known_proto[cnt]);
115  }
116 }
char * known_proto[256]
uint8_t SCProtoNameValid(uint16_t proto)
Function to check if the received protocol number is valid and do we have corresponding name entry fo...
#define BUG_ON(x)
#define FALSE
#define unlikely(expr)
Definition: util-optimize.h:35
void SCProtoNameInit()
Function to load the protocol names from the specified protocol file.
#define TRUE
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
#define PROTO_FILE
uint8_t proto
#define SCFree(a)
Definition: util-mem.h:236
#define SCStrdup(a)
Definition: util-mem.h:220
void SCProtoNameDeInit()
Function to clears the memory used in storing the protocol names.