2 * Routines for Audiocodes TrunkPack Network Control Protocol (TPNCP) dissection
4 * Copyright (c) 2007 by Valery Sigalov <valery.sigalov@audiocodes.com>
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.com>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 /*-------------------------------------------------------------------------------------------------------------------------------------------*/
35 #include <wsutil/file_util.h>
37 #include <epan/exceptions.h>
38 #include <epan/packet.h>
39 #include <epan/prefs.h>
40 #include <epan/emem.h>
41 #include <wsutil/filesystem.h>
42 #include <epan/dissectors/packet-tcp.h>
43 #include <epan/strutil.h>
44 #include <epan/to_str.h>
46 /*-------------------------------------------------------------------------------------------------------------------------------------------*/
48 #define BASE_TPNCP_PORT 2424
49 #define TCP_PORT_TPNCP_TRUNKPACK BASE_TPNCP_PORT
50 #define UDP_PORT_TPNCP_TRUNKPACK BASE_TPNCP_PORT
51 #define TCP_PORT_TPNCP_HOST BASE_TPNCP_PORT
52 #define UDP_PORT_TPNCP_HOST BASE_TPNCP_PORT
54 #define BASE_TPNCP_DATA_LEN 256
55 #define MAX_TPNCP_DB_ENTRY_LEN BASE_TPNCP_DATA_LEN
57 #define MAX_TPNCP_DB_SIZE 3000
58 #define MAX_ENUMS_NUM 500
59 #define MAX_ENUM_ENTRIES 500
61 /*-------------------------------------------------------------------------------------------------------------------------------------------*/
63 void proto_register_tpncp(void);
64 void proto_reg_handoff_tpncp(void);
66 /* The linked list for storing information about specific data fields. */
67 typedef struct tpncp_data_field_info
69 gchar *tpncp_data_field_name;
70 gint tpncp_data_field_descr;
71 gint tpncp_data_field_sign;
72 gint tpncp_data_field_size;
73 gint tpncp_data_field_array_dim;
74 gint tpncp_data_field_is_ip_addr;
75 struct tpncp_data_field_info *p_next;
76 } tpncp_data_field_info;
78 /*-------------------------------------------------------------------------------------------------------------------------------------------*/
80 /* Desegmentation of TPNCP over TCP */
81 static gboolean tpncp_desegment = TRUE;
83 /* Database for storing information about all TPNCP events. */
84 /* XXX: ToDo: allocate at runtime as needed */
85 static tpncp_data_field_info tpncp_events_info_db[MAX_TPNCP_DB_SIZE];
87 /* Database for storing information about all TPNCP commands. */
88 /* XXX: ToDo: allocate at runtime as needed */
89 static tpncp_data_field_info tpncp_commands_info_db[MAX_TPNCP_DB_SIZE];
91 /* Global variables for bitfields representation. */
92 static gint bits[] = {0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80};
93 static gint bitindex = 0;
95 /* TPNCP packet header fields. */
96 static gint proto_tpncp = -1;
97 static gint hf_tpncp_version = -1;
98 static gint hf_tpncp_length = -1;
99 static gint hf_tpncp_seq_number = -1;
100 /* static gint hf_tpncp_old_event_seq_number = -1; */
101 static gint hf_tpncp_reserved = -1;
102 static gint hf_tpncp_command_id = -1;
103 /* static gint hf_tpncp_old_command_id = -1; */
104 static gint hf_tpncp_event_id = -1;
105 static gint hf_tpncp_cid = -1;
107 /* TPNCP fields defining a subtree. */
108 static gint ett_tpncp = -1;
109 static gint ett_tpncp_body = -1;
111 static guint global_tpncp_trunkpack_tcp_port = TCP_PORT_TPNCP_TRUNKPACK;
112 static guint global_tpncp_trunkpack_udp_port = UDP_PORT_TPNCP_TRUNKPACK;
113 static guint global_tpncp_host_tcp_port = TCP_PORT_TPNCP_HOST;
114 static guint global_tpncp_host_udp_port = UDP_PORT_TPNCP_HOST;
116 static dissector_handle_t tpncp_handle;
118 static guint trunkpack_tcp_port = 0;
119 static guint trunkpack_udp_port = 0;
120 static guint host_tcp_port = 0;
121 static guint host_udp_port = 0;
123 /* XXX: ToDo: allocate at runtime as needed */
124 /* The following allocates something on the order of 2M of static memory ! */
125 /* Also: Runtime value_string_ext arrays should be used */
126 static value_string tpncp_commands_id_vals[MAX_TPNCP_DB_SIZE];
127 static value_string tpncp_events_id_vals[MAX_TPNCP_DB_SIZE];
128 static value_string tpncp_enums_id_vals[MAX_ENUMS_NUM][MAX_ENUM_ENTRIES];
129 static gchar *tpncp_enums_name_vals[MAX_ENUMS_NUM];
131 static gint hf_size = 1;
132 static gint hf_allocated = 0;
133 static hf_register_info *hf = NULL;
135 /*-------------------------------------------------------------------------------------------------------------------------------------------*/
137 static void dissect_tpncp_data(gint data_id, tvbuff_t *tvb, proto_item *item,
138 gint *offset, tpncp_data_field_info *data_fields_info) {
139 proto_tree *ltree = NULL;
140 proto_item *pi = NULL;
146 gint g_str_len, counter, bitshift, bitmask;
147 tpncp_data_field_info *current_tpncp_data_field_info = NULL;
149 ltree = proto_item_add_subtree(item, ett_tpncp_body);
150 current_tpncp_data_field_info = &data_fields_info[data_id];
152 while (current_tpncp_data_field_info) {
153 switch(current_tpncp_data_field_info->tpncp_data_field_size) {
154 case 1: case 2: case 3: case 4:
155 case 5: case 6: case 7: case 8:
156 if ((g_str_len = current_tpncp_data_field_info->tpncp_data_field_array_dim)) { /* add char array */
157 g_str_len = MIN(g_str_len, tvb_length_remaining(tvb, *offset));
158 proto_tree_add_item(ltree, current_tpncp_data_field_info->tpncp_data_field_descr,
159 tvb, *offset, g_str_len, ENC_NA|ENC_ASCII);
160 (*offset) += g_str_len;
162 else { /* add single char */
163 g_uchar = tvb_get_guint8(tvb, *offset);
165 if (current_tpncp_data_field_info->tpncp_data_field_size != 8) {
166 for (counter = 0, bitmask = 0x0, bitshift = bitindex;
167 counter < current_tpncp_data_field_info->tpncp_data_field_size;
169 bitmask |= bits[bitindex++]; /* Bitmask of interesting bits. */
171 g_uchar >>= bitshift;
173 if (current_tpncp_data_field_info->tpncp_data_field_sign || current_tpncp_data_field_info->tpncp_data_field_size != 8) {
174 proto_tree_add_uint(ltree, current_tpncp_data_field_info->tpncp_data_field_descr,
175 tvb, *offset, 1, g_uchar);
179 g_char = (gint8)g_uchar;
180 proto_tree_add_int(ltree, current_tpncp_data_field_info->tpncp_data_field_descr,
181 tvb, *offset, 1, g_char);
183 if ((bitindex == 0) || (bitindex == 8)) {
190 if (current_tpncp_data_field_info->tpncp_data_field_sign) {
191 g_ushort = tvb_get_ntohs(tvb, *offset);
192 proto_tree_add_uint(ltree, current_tpncp_data_field_info->tpncp_data_field_descr,
193 tvb, *offset, 2, g_ushort);
196 g_short = tvb_get_ntohs(tvb, *offset);
197 proto_tree_add_int(ltree, current_tpncp_data_field_info->tpncp_data_field_descr,
198 tvb, *offset, 2, g_short);
203 g_int = tvb_get_ntohl(tvb, *offset);
204 if (current_tpncp_data_field_info->tpncp_data_field_sign) {
205 pi = proto_tree_add_uint(ltree, current_tpncp_data_field_info->tpncp_data_field_descr,
206 tvb, *offset, 4, g_int);
209 pi = proto_tree_add_int(ltree, current_tpncp_data_field_info->tpncp_data_field_descr,
210 tvb, *offset, 4, g_int);
212 /* Add string representation for ip_address's field (if needed). */
213 if (current_tpncp_data_field_info->tpncp_data_field_is_ip_addr) {
214 proto_item_append_text(pi, " (%s)", tvb_ip_to_str(tvb, *offset));
221 current_tpncp_data_field_info = current_tpncp_data_field_info->p_next;
222 if (tvb_length_remaining(tvb, *offset) <= 0) {
228 /*-------------------------------------------------------------------------------------------------------------------------------------------*/
230 static void dissect_tpncp_event(gint event_id, tvbuff_t *tvb,
231 proto_item *item, gint *offset) {
233 /* Place non-standard events here. */
235 dissect_tpncp_data(event_id, tvb, item, offset, tpncp_events_info_db);
240 /*-------------------------------------------------------------------------------------------------------------------------------------------*/
242 static void dissect_tpncp_command(gint command_id, tvbuff_t *tvb,
243 proto_item *item, gint *offset) {
244 switch (command_id) {
245 /* Place non-standard commands here. */
247 dissect_tpncp_data(command_id, tvb, item, offset, tpncp_commands_info_db);
252 /*-------------------------------------------------------------------------------------------------------------------------------------------*/
254 static int dissect_tpncp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_) {
255 proto_item *item = NULL, *tpncp_item = NULL;
256 proto_tree *tpncp_tree = NULL;
259 guint16 seq_number, len, ver, reserved;
262 ver = tvb_get_ntohs(tvb, 0);
263 len = tvb_get_ntohs(tvb, 2);
264 seq_number = tvb_get_ntohs(tvb, 4);
265 reserved = tvb_get_ntohs(tvb, 6);
266 id = tvb_get_ntohl(tvb, 8);
268 if (pinfo->srcport == UDP_PORT_TPNCP_TRUNKPACK) /* Event */
269 cid = tvb_get_ntohl(tvb, 12 );
271 col_set_str(pinfo->cinfo, COL_PROTOCOL, "TPNCP");
273 if (pinfo->srcport == UDP_PORT_TPNCP_TRUNKPACK) {
274 col_add_fstr(pinfo->cinfo, COL_INFO,
275 "EvID=%s(%d), SeqNo=%d, ChID=%d, Len=%d, Ver=%d",
276 val_to_str_const(id, tpncp_events_id_vals, "Unknown"),
277 id, seq_number, cid, len, ver);
279 col_add_fstr(pinfo->cinfo, COL_INFO,
280 "CmdID=%s(%d), SeqNo=%d, Len=%d, Ver=%d",
281 val_to_str_const(id, tpncp_commands_id_vals, "Unknown"),
282 id, seq_number, len, ver);
286 item = proto_tree_add_item(tree, proto_tpncp, tvb, 0, -1, ENC_NA);
287 tpncp_tree = proto_item_add_subtree(item, ett_tpncp);
289 proto_tree_add_uint(tpncp_tree, hf_tpncp_version, tvb, 0, 2, ver);
290 proto_tree_add_uint(tpncp_tree, hf_tpncp_length, tvb, 2, 2, len);
291 proto_tree_add_uint(tpncp_tree, hf_tpncp_seq_number, tvb, 4, 2, seq_number);
292 proto_tree_add_uint(tpncp_tree, hf_tpncp_reserved, tvb, 6, 2, reserved);
294 if (pinfo->srcport == UDP_PORT_TPNCP_TRUNKPACK) {
295 if (try_val_to_str(id, tpncp_events_id_vals)) {
296 proto_tree_add_uint(tpncp_tree, hf_tpncp_event_id, tvb, 8, 4, id);
297 proto_tree_add_int(tpncp_tree, hf_tpncp_cid, tvb, 12, 4, cid);
299 if (tpncp_events_info_db[id].tpncp_data_field_size) {
300 tpncp_header = wmem_strdup_printf(wmem_packet_scope(), "TPNCP Event: %s (%d)",
301 val_to_str_const(id, tpncp_events_id_vals, "Unknown"), id);
302 tpncp_item = proto_tree_add_text(tree, tvb, offset, -1, "%s", tpncp_header);
303 dissect_tpncp_event(id, tvb, tpncp_item, &offset);
308 if (try_val_to_str(id, tpncp_commands_id_vals)) {
309 proto_tree_add_uint(tpncp_tree, hf_tpncp_command_id, tvb, 8, 4, id);
311 if (tpncp_commands_info_db[id].tpncp_data_field_size) {
312 tpncp_header = wmem_strdup_printf(wmem_packet_scope(), "TPNCP Command: %s (%d)",
313 val_to_str_const(id, tpncp_commands_id_vals, "Unknown"), id);
314 tpncp_item = proto_tree_add_text(tree, tvb, offset, -1, "%s", tpncp_header);
315 dissect_tpncp_command(id, tvb, tpncp_item, &offset);
321 return tvb_length(tvb);
324 /*-------------------------------------------------------------------------------------------------------------------------------------------*/
326 static guint get_tpncp_pdu_len(packet_info *pinfo _U_, tvbuff_t *tvb, gint offset) {
329 /* Get the length of the DNS packet. */
330 plen = tvb_get_ntohs(tvb, offset + 2);
331 /* Length does not include the version+length field. */
337 /*-------------------------------------------------------------------------------------------------------------------------------------------*/
339 static int dissect_tpncp_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data) {
340 if (pinfo->can_desegment)
341 /* If desegmentation is enabled (TCP preferences) use the desegmentation API. */
342 tcp_dissect_pdus(tvb, pinfo, tree, tpncp_desegment, 4, get_tpncp_pdu_len, dissect_tpncp, data);
344 /* Otherwise use the regular dissector (might not give correct dissection). */
345 dissect_tpncp(tvb, pinfo, tree, data);
347 return tvb_length(tvb);
350 /*-------------------------------------------------------------------------------------------------------------------------------------------*/
352 static gint fill_tpncp_id_vals(value_string string[], FILE *file) {
353 gint i = 0, tpncp_id = 0;
354 gchar *tpncp_name, *line_in_file;
356 line_in_file = (gchar *)g_malloc(MAX_TPNCP_DB_ENTRY_LEN);
358 tpncp_name = (gchar *)g_malloc(MAX_TPNCP_DB_ENTRY_LEN);
361 while (fgets(line_in_file, MAX_TPNCP_DB_ENTRY_LEN, file) != NULL) {
362 if (!strncmp(line_in_file, "#####", 5)) {
365 if (sscanf(line_in_file, "%255s %d", tpncp_name, &tpncp_id) == 2) {
366 string[i].strptr = g_strdup(tpncp_name);
367 string[i].value = tpncp_id;
368 if (i < (MAX_TPNCP_DB_SIZE-1)) {
377 g_free(line_in_file);
383 /*-------------------------------------------------------------------------------------------------------------------------------------------*/
385 static gint fill_enums_id_vals(FILE *file) {
386 gint i = 0, enum_id = 0, enum_val = 0;
387 gboolean first_entry = TRUE;
388 gchar *line_in_file = NULL, *enum_name = NULL,
389 *enum_type = NULL, *enum_str = NULL;
391 line_in_file = (gchar *)g_malloc(MAX_TPNCP_DB_ENTRY_LEN);
393 enum_name = (gchar *)g_malloc(MAX_TPNCP_DB_ENTRY_LEN);
395 enum_type = (gchar *)g_malloc(MAX_TPNCP_DB_ENTRY_LEN);
397 enum_str = (gchar *)g_malloc(MAX_TPNCP_DB_ENTRY_LEN);
400 while (fgets(line_in_file, MAX_TPNCP_DB_ENTRY_LEN, file) != NULL) {
401 if (!strncmp(line_in_file, "#####", 5)) {
404 if (sscanf(line_in_file, "%255s %255s %d", enum_name, enum_str, &enum_id) == 3) {
405 if (strcmp(enum_type, enum_name)) {
407 tpncp_enums_id_vals[enum_val][i].strptr = NULL;
408 tpncp_enums_id_vals[enum_val][i].value = 0;
409 if (enum_val < (MAX_ENUMS_NUM-1)) {
418 tpncp_enums_name_vals[enum_val] = g_strdup(enum_name);
419 g_strlcpy(enum_type, enum_name, MAX_TPNCP_DB_ENTRY_LEN);
421 tpncp_enums_id_vals[enum_val][i].strptr = g_strdup(enum_str);
422 tpncp_enums_id_vals[enum_val][i].value = enum_id;
423 if (i < (MAX_ENUM_ENTRIES-1)) {
431 /* make sure the last entry in the array is null but
432 * don't overflow if we've filled the entire thing (in which case
433 * we have to drop an entry) */
434 if (enum_val + 1 >= MAX_ENUMS_NUM) {
435 g_free(tpncp_enums_name_vals[enum_val]);
436 tpncp_enums_name_vals[enum_val] = NULL;
439 tpncp_enums_name_vals[enum_val+1] = NULL;
442 g_free(line_in_file);
450 /*-------------------------------------------------------------------------------------------------------------------------------------------*/
452 static gint get_enum_name_val(gchar *enum_name) {
455 while (tpncp_enums_name_vals[enum_val]) {
456 if (!strcmp(enum_name, tpncp_enums_name_vals[enum_val]))
464 /*-------------------------------------------------------------------------------------------------------------------------------------------*/
466 static gint init_tpncp_data_fields_info(tpncp_data_field_info *data_fields_info, FILE *file) {
467 static gboolean was_registered = FALSE;
468 gchar *tpncp_db_entry = NULL, *tpncp_data_field_name = NULL, *tmp = NULL;
469 gint enum_val, data_id, current_data_id = -1,
470 tpncp_data_field_sign, tpncp_data_field_size,
471 tpncp_data_field_array_dim, tpncp_data_field_is_ip_addr;
473 tpncp_data_field_info *current_tpncp_data_field_info = NULL;
474 hf_register_info hf_entr;
476 static hf_register_info hf_tpncp[] = {
502 &hf_tpncp_seq_number,
515 &hf_tpncp_old_event_seq_number,
518 "tpncp.old_event_seq_number",
540 &hf_tpncp_command_id,
546 VALS(tpncp_commands_id_vals),
553 &hf_tpncp_old_command_id,
556 "tpncp.old_command_id",
559 VALS(tpncp_commands_id_vals),
572 VALS(tpncp_events_id_vals),
591 /* Register common fields of hf_register_info struture. */
592 hf_entr.hfinfo.type = FT_NONE;
593 hf_entr.hfinfo.strings = NULL;
594 hf_entr.hfinfo.bitmask = 0x0;
595 hf_entr.hfinfo.blurb = NULL;
596 hf_entr.hfinfo.id = 0;
597 hf_entr.hfinfo.parent = 0;
598 hf_entr.hfinfo.ref_type = HF_REF_TYPE_NONE;
599 hf_entr.hfinfo.same_name_next = NULL;
600 hf_entr.hfinfo.same_name_prev_id = -1;
602 if (!was_registered) {
603 /* Register non-standard data should be done only once. */
604 hf_allocated = hf_size+(int)array_length(hf_tpncp)-1;
605 if ((hf = (hf_register_info *)g_realloc(hf, hf_allocated * sizeof(hf_register_info))) == NULL) {
606 /* XXX realloc returning NULL does not free the original memory,
610 for (idx = 0; idx < array_length(hf_tpncp); idx++) {
611 memcpy(hf + (hf_size - 1), hf_tpncp + idx, sizeof(hf_register_info));
614 was_registered = TRUE;
619 /* Register standard data. */
620 tpncp_db_entry = (gchar *)g_malloc(MAX_TPNCP_DB_ENTRY_LEN);
621 tpncp_db_entry[0] = 0;
623 while (fgets(tpncp_db_entry, MAX_TPNCP_DB_ENTRY_LEN, file) != NULL) {
624 if (!strncmp(tpncp_db_entry, "#####", 5)) {
629 /* Default to decimal display type */
630 hf_entr.hfinfo.display = BASE_DEC;
632 if ((tmp = strtok(tpncp_db_entry, " ")) == NULL)
633 continue; /* Badly formed data base entry - skip corresponding field's registration. */
635 if ((tpncp_data_field_name = strtok(NULL, " ")) == NULL)
636 continue; /* Badly formed data base entry - skip corresponding field's registration. */
637 if ((tmp = strtok(NULL, " ")) == NULL)
638 continue; /* Badly formed data base entry - skip corresponding field's registration. */
639 tpncp_data_field_sign = atoi(tmp);
640 if ((tmp = strtok(NULL, " ")) == NULL)
641 continue; /* Badly formed data base entry - skip corresponding field's registration. */
642 tpncp_data_field_size = atoi(tmp);
643 if ((tmp = strtok(NULL, " ")) == NULL)
644 continue; /* Badly formed data base entry - skip corresponding field's registration. */
645 tpncp_data_field_array_dim = atoi(tmp);
646 if ((tmp = strtok(NULL, " ")) == NULL)
647 continue; /* Badly formed data base entry - skip corresponding field's registration. */
648 tpncp_data_field_is_ip_addr = atoi(tmp);
649 if ((tmp = strtok(NULL, "\n")) == NULL)
650 continue; /* Badly formed data base entry - skip corresponding field's registration. */
652 if (current_data_id != data_id) { /* new data */
653 current_tpncp_data_field_info = &data_fields_info[data_id];
654 current_data_id = data_id;
657 if ((current_tpncp_data_field_info->p_next =
658 (tpncp_data_field_info *)g_malloc0(sizeof(tpncp_data_field_info)))
660 g_free(tpncp_db_entry);
663 current_tpncp_data_field_info = current_tpncp_data_field_info->p_next;
665 /* Register specific fields of hf_register_info struture. */
666 if (strcmp(tmp, "primitive")) {
667 enum_val = get_enum_name_val(tmp);
668 if (enum_val == -1) {
669 hf_entr.hfinfo.strings = NULL;
672 hf_entr.hfinfo.strings = VALS(tpncp_enums_id_vals[enum_val]);
676 hf_entr.hfinfo.strings = NULL;
678 current_tpncp_data_field_info->tpncp_data_field_descr = -1;
679 hf_entr.p_id = ¤t_tpncp_data_field_info->tpncp_data_field_descr;
680 current_tpncp_data_field_info->tpncp_data_field_name = g_strdup_printf("tpncp.%s", tpncp_data_field_name);
681 hf_entr.hfinfo.name = current_tpncp_data_field_info->tpncp_data_field_name;
682 hf_entr.hfinfo.abbrev = current_tpncp_data_field_info->tpncp_data_field_name;
683 switch (tpncp_data_field_size) {
684 case 1: case 2: case 3: case 4:
685 case 5: case 6: case 7: case 8:
686 if (tpncp_data_field_array_dim) {
687 hf_entr.hfinfo.type = FT_STRING;
688 hf_entr.hfinfo.display = BASE_NONE;
691 hf_entr.hfinfo.type = (tpncp_data_field_sign)?FT_UINT8:FT_INT8;
694 hf_entr.hfinfo.type = (tpncp_data_field_sign)?FT_UINT16:FT_INT16;
697 hf_entr.hfinfo.type = (tpncp_data_field_sign)?FT_UINT32:FT_INT32;
702 /* Register initialized hf_register_info in global database. */
703 if (hf_size > hf_allocated) {
704 hf_allocated += 1024;
705 if ((hf = (hf_register_info *)g_realloc(hf, hf_allocated * sizeof(hf_register_info))) == NULL) {
706 /* XXX realloc returning NULL does not free the original memory,
708 g_free(tpncp_db_entry);
712 memcpy(hf + hf_size - 1, &hf_entr, sizeof(hf_register_info));
714 current_tpncp_data_field_info->tpncp_data_field_sign = tpncp_data_field_sign;
715 current_tpncp_data_field_info->tpncp_data_field_size = tpncp_data_field_size;
716 current_tpncp_data_field_info->tpncp_data_field_array_dim = tpncp_data_field_array_dim;
717 current_tpncp_data_field_info->tpncp_data_field_is_ip_addr = tpncp_data_field_is_ip_addr;
720 g_free(tpncp_db_entry);
725 /*-------------------------------------------------------------------------------------------------------------------------------------------*/
727 static gint init_tpncp_db(void) {
728 gchar *tpncp_dat_file_path;
732 tpncp_dat_file_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S"tpncp" G_DIR_SEPARATOR_S "tpncp.dat", get_datafile_dir());
734 /* Open file with TPNCP data. */
735 if ((file = ws_fopen(tpncp_dat_file_path, "r")) == NULL) {
736 g_free(tpncp_dat_file_path);
740 g_free(tpncp_dat_file_path);
742 ret = fill_tpncp_id_vals(tpncp_events_id_vals, file);
746 ret = fill_tpncp_id_vals(tpncp_commands_id_vals, file);
750 ret = fill_enums_id_vals(file);
754 ret = init_tpncp_data_fields_info(tpncp_events_info_db, file);
758 ret = init_tpncp_data_fields_info(tpncp_commands_info_db, file);
766 /*-------------------------------------------------------------------------------------------------------------------------------------------*/
768 void proto_reg_handoff_tpncp(void) {
769 static gint tpncp_prefs_initialized = FALSE;
770 static dissector_handle_t tpncp_tcp_handle;
772 /* If we weren't able to load the database (and thus the hf_ entries)
773 * do not attach to any ports (if we did then we'd get a "dissector bug"
774 * assertions every time a packet is handed to us and we tried to use the
777 if (proto_tpncp == -1)
780 if (!tpncp_prefs_initialized) {
781 tpncp_tcp_handle = new_create_dissector_handle(dissect_tpncp_tcp, proto_tpncp);
783 tpncp_prefs_initialized = TRUE;
786 dissector_delete_uint("tcp.port", trunkpack_tcp_port, tpncp_tcp_handle);
787 dissector_delete_uint("udp.port", trunkpack_udp_port, tpncp_handle);
788 dissector_delete_uint("tcp.port", host_tcp_port, tpncp_tcp_handle);
789 dissector_delete_uint("udp.port", host_udp_port, tpncp_handle);
792 trunkpack_tcp_port = global_tpncp_trunkpack_tcp_port;
793 trunkpack_udp_port = global_tpncp_trunkpack_udp_port;
795 host_tcp_port = global_tpncp_host_tcp_port;
796 host_udp_port = global_tpncp_host_udp_port;
798 dissector_add_uint("tcp.port", global_tpncp_trunkpack_tcp_port, tpncp_tcp_handle);
799 dissector_add_uint("udp.port", global_tpncp_trunkpack_udp_port, tpncp_handle);
802 /*-------------------------------------------------------------------------------------------------------------------------------------------*/
804 void proto_register_tpncp(void) {
806 module_t *tpncp_module;
807 static gint *ett[] = {
812 if (init_tpncp_db() == -1) {
813 g_warning("Could not load tpncp.dat file, tpncp dissector will not work");
817 proto_tpncp = proto_register_protocol("AudioCodes TPNCP (TrunkPack Network Control Protocol)",
820 /* Rather than duplicating large quantities of code from
821 * proto_register_field_array() and friends to sanitize the tpncp.dat file
822 * when we read it, just catch any exceptions we get while registering and
823 * take them as a hint that the file is corrupt. Then move on, so that at
824 * least the rest of the protocol dissectors will still work.
827 /* The function proto_register_field_array does not work with dynamic
828 * arrays, so pass dynamic array elements one-by-one in the loop.
830 for(idx = 0; idx < hf_size; idx++) {
831 proto_register_field_array(proto_tpncp, &hf[idx], 1);
836 g_warning("Corrupt tpncp.dat file, tpncp dissector will not work.");
841 proto_register_subtree_array(ett, array_length(ett));
843 tpncp_handle = new_register_dissector("tpncp", dissect_tpncp, proto_tpncp);
845 tpncp_module = prefs_register_protocol(proto_tpncp, proto_reg_handoff_tpncp);
847 prefs_register_uint_preference(tpncp_module, "tcp.trunkpack_port",
848 "TPNCP \"well-known\" TrunkPack TCP Port",
849 "", 10, &global_tpncp_trunkpack_tcp_port);
851 prefs_register_uint_preference(tpncp_module, "udp.trunkpack_port",
852 "TPNCP \"well-known\" TrunkPack UDP Port",
853 "", 10, &global_tpncp_trunkpack_udp_port);