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 /** Lookup array to hold the information related to known protocol
31  * in /etc/protocols */
32 char *known_proto[256];
33 static int init_once = 0;
34 
35 /**
36  * \brief Function to load the protocol names from the specified protocol
37  * file.
38  */
40 {
41  BUG_ON(init_once);
42  init_once++;
43  memset(known_proto, 0x00, sizeof(known_proto));
44 
45  /* Load the known protocols name from the /etc/protocols file */
46  FILE *fp = fopen(PROTO_FILE,"r");
47  if (fp != NULL) {
48  char line[200];
49  char *ptr = NULL;
50 
51  while(fgets(line, sizeof(line), fp) != NULL) {
52  if (line[0] == '#')
53  continue;
54 
55  char *name = strtok_r(line," \t", &ptr);
56  if (name == NULL)
57  continue;
58 
59  char *proto_ch = strtok_r(NULL," \t", &ptr);
60  if (proto_ch == NULL)
61  continue;
62 
63  int proto = atoi(proto_ch);
64  if (proto >= 255)
65  continue;
66 
67  char *cname = strtok_r(NULL, " \t", &ptr);
68 
69  if (known_proto[proto] != NULL) {
71  }
72 
73  if (cname != NULL) {
74  known_proto[proto] = SCStrdup(cname);
75  } else {
76  known_proto[proto] = SCStrdup(name);
77  }
78  if (unlikely(known_proto[proto] == NULL)) {
79  SCLogError(SC_ERR_MEM_ALLOC, "Failed proto name allocation");
80  continue;
81  }
82  int proto_len = strlen(known_proto[proto]);
83  if (proto_len > 0 && known_proto[proto][proto_len - 1] == '\n')
84  known_proto[proto][proto_len - 1] = '\0';
85  }
86  fclose(fp);
87  }
88 }
89 
90 /**
91  * \brief Function to check if the received protocol number is valid and do
92  * we have corresponding name entry for this number or not.
93  *
94  * \param proto Protocol number to be validated
95  * \retval ret On success returns TRUE otherwise FALSE
96  */
97 uint8_t SCProtoNameValid(uint16_t proto)
98 {
99  uint8_t ret = FALSE;
100 
101  if (proto <= 255 && known_proto[proto] != NULL) {
102  ret = TRUE;
103  }
104 
105  return ret;
106 }
107 
108 /**
109  * \brief Function to clears the memory used in storing the protocol names.
110  */
112 {
113  int cnt;
114  /* clears the memory of loaded protocol names */
115  for (cnt = 0; cnt < 255; cnt++) {
116  if (known_proto[cnt] != NULL)
117  SCFree(known_proto[cnt]);
118  }
119 }
SCProtoNameInit
void SCProtoNameInit()
Function to load the protocol names from the specified protocol file.
Definition: util-proto-name.c:39
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
proto
uint8_t proto
Definition: decode-template.h:0
PROTO_FILE
#define PROTO_FILE
Definition: util-proto-name.h:28
TRUE
#define TRUE
Definition: suricata-common.h:33
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:282
FALSE
#define FALSE
Definition: suricata-common.h:34
util-proto-name.h
SCProtoNameValid
uint8_t SCProtoNameValid(uint16_t proto)
Function to check if the received protocol number is valid and do we have corresponding name entry fo...
Definition: util-proto-name.c:97
suricata-common.h
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
SCStrdup
#define SCStrdup(s)
Definition: util-mem.h:56
SCFree
#define SCFree(p)
Definition: util-mem.h:61
SC_ERR_MEM_ALLOC
@ SC_ERR_MEM_ALLOC
Definition: util-error.h:31
known_proto
char * known_proto[256]
Definition: util-proto-name.c:32
SCProtoNameDeInit
void SCProtoNameDeInit()
Function to clears the memory used in storing the protocol names.
Definition: util-proto-name.c:111