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