Coverage Report

Created: 2023-06-07 06:48

/src/ndpi/src/lib/protocols/hangout.c
Line
Count
Source
1
/*
2
 * hangout.c
3
 *
4
 * Copyright (C) 2012-22 - ntop.org
5
 *
6
 * This module is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This module is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License.
17
 * If not, see <http://www.gnu.org/licenses/>.
18
 *
19
 */
20
21
#include "ndpi_protocol_ids.h"
22
23
189k
#define NDPI_CURRENT_PROTO NDPI_PROTOCOL_HANGOUT_DUO
24
25
/* #define DEBUG_LRU 1 */
26
27
#include "ndpi_api.h"
28
29
/* stun.c */
30
extern u_int32_t get_stun_lru_key(struct ndpi_flow_struct *flow, u_int8_t rev);
31
32
/* https://support.google.com/a/answer/1279090?hl=en */
33
365
#define HANGOUT_UDP_LOW_PORT  19302
34
184
#define HANGOUT_UDP_HIGH_PORT 19309
35
4.86k
#define HANGOUT_TCP_LOW_PORT  19305
36
2.45k
#define HANGOUT_TCP_HIGH_PORT 19309
37
38
/* ***************************************************************** */
39
40
365
static u_int8_t isHangoutUDPPort(u_int16_t port) {
41
365
  if((port >= HANGOUT_UDP_LOW_PORT) && (port <= HANGOUT_UDP_HIGH_PORT))
42
2
    return(1);
43
363
  else
44
363
    return(0);
45
365
}
46
47
/* ***************************************************************** */
48
49
4.86k
static u_int8_t isHangoutTCPPort(u_int16_t port) {
50
4.86k
  if((port >= HANGOUT_TCP_LOW_PORT) && (port <= HANGOUT_TCP_HIGH_PORT))
51
2
    return(1);
52
4.86k
  else
53
4.86k
    return(0);
54
4.86k
}
55
56
/* ***************************************************************** */
57
58
static void ndpi_search_hangout(struct ndpi_detection_module_struct *ndpi_struct,
59
189k
        struct ndpi_flow_struct *flow) {
60
189k
  struct ndpi_packet_struct * packet = &ndpi_struct->packet;
61
62
189k
  NDPI_LOG_DBG(ndpi_struct, "search Hangout\n");
63
64
189k
  if((packet->payload_packet_len > 24) && flow->guessed_protocol_id_by_ip == NDPI_PROTOCOL_GOOGLE) {
65
2.61k
    int matched_src = 0;
66
2.61k
    if(
67
2.61k
       ((packet->udp != NULL) && (matched_src = isHangoutUDPPort(ntohs(packet->udp->source))
68
183
          || isHangoutUDPPort(ntohs(packet->udp->dest))))
69
2.61k
       ||
70
2.61k
       ((packet->tcp != NULL) && (matched_src = isHangoutTCPPort(ntohs(packet->tcp->source))
71
2.43k
          || isHangoutTCPPort(ntohs(packet->tcp->dest))))) {
72
4
      NDPI_LOG_INFO(ndpi_struct, "found Hangout\n");
73
74
      /* Hangout is over STUN hence the LRU cache is shared */
75
76
4
      if(ndpi_struct->stun_cache) {
77
4
  u_int32_t key = get_stun_lru_key(flow, !matched_src);
78
79
#ifdef DEBUG_LRU
80
  printf("[LRU] ADDING %u / %u.%u\n", key, NDPI_PROTOCOL_STUN, NDPI_PROTOCOL_HANGOUT_DUO);
81
#endif
82
83
4
  ndpi_lru_add_to_cache(ndpi_struct->stun_cache, key, NDPI_PROTOCOL_HANGOUT_DUO, ndpi_get_current_time(flow));
84
4
      }
85
      
86
4
      ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_HANGOUT_DUO,
87
4
         NDPI_PROTOCOL_STUN, NDPI_CONFIDENCE_DPI);
88
4
      return;
89
4
    }
90
2.61k
  }
91
  
92
189k
  NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
93
189k
}
94
95
/* ***************************************************************** */
96
97
1
void init_hangout_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id) {
98
1
  ndpi_set_bitmask_protocol_detection("GoogleHangout", ndpi_struct, *id,
99
1
              NDPI_PROTOCOL_HANGOUT_DUO,
100
1
              ndpi_search_hangout,
101
1
              NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_TCP_OR_UDP_WITH_PAYLOAD_WITHOUT_RETRANSMISSION,
102
1
              SAVE_DETECTION_BITMASK_AS_UNKNOWN,
103
1
              ADD_TO_DETECTION_BITMASK);
104
105
1
  *id += 1;
106
1
}
107