summaryrefslogtreecommitdiff
path: root/cleopatre/application/igmp_snoopd/src/igmp_v1_v2.c
blob: 703856be87708a590eb9be8a88f39985badc0a8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * igmp_snoopd: an IGMP Snooping Daemon.
 * Copyright (C) 2011 SPiDCOM Technologies
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "internal.h"

#include <linux/igmp.h>
#include <math.h>

#include "utils.h"

/**
 * Extract the Max Response Time value from a query.
 * \param  query  The query.
 * \return  The value of Max Response Time specified in the query.
 */
static duration_sec_t
extract_max_resp_time (const struct igmphdr *query)
{
    uint8_t max_response_time_10th_sec = query->code;

    /* Max Resp Time in the query is represented in units of 1/10 second, */
    /* so we convert to seconds */
    duration_sec_t max_response_time_sec =
        (duration_sec_t) (ceil ((double)max_response_time_10th_sec / 10.0));

    return max_response_time_sec;
}

void
process_v1_query ()
{
    log_debug ("v1 Query");
    /* TODO: implement */
}

void
process_v1_report ()
{
    log_debug ("v1 report");
    /* TODO: implement */
}

void
process_v2_query (struct context *ctx,
                  struct igmphdr *igmp_header)
{
    log_debug ("v2 Query");

    ctx->metrics.last_query_time = time (NULL);

    if (igmp_header->group == 0)
    {
        log_debug ("General Query");

        /* The metrics are only updated if it's a general query,
         * because a v2 Group-Specific query by its own is not enough to
         * indicate that the querier is now a v2 querier. It may be that a v3
         * querier sent a v2 Group-Specific query because the concerned group
         * is in v2 Compatibility mode. In that case, the metrics should not be
         * reset to the defaults values. */

        ctx->metrics.robustness_variable = DEFAULT_ROBUSTNESS_VARIABLE;
        ctx->metrics.query_interval = DEFAULT_QUERY_INTERVAL;
        ctx->metrics.query_response_interval = extract_max_resp_time (igmp_header);
        update_computed_metrics (&ctx->metrics);
    }
    else
    {
        log_debug ("Group-Specific Query");
    }

    update_timer_query_tx (ctx);
}

void
process_v2_report (struct context *ctx,
                   const unsigned char host_eth_addr[ETH_ALEN],
                   struct igmphdr *igmp_header)
{
    log_debug ("v2 Join Report");

    if (ignore_multicast_addr (igmp_header->group))
    {
        log_debug ("Ignored");
        return;
    }

    struct group *grp;
    struct group_member *member;
    find_or_add (&ctx->groups_head, &grp, igmp_header->group, &member, host_eth_addr);

    update_timers (grp, member, &ctx->metrics);

    time_t now = time (NULL);
    grp->v2_host_present_timer_end =
        now + ctx->metrics.older_host_present_interval;

    grp->last_v2_report_time = now;

    member->filter_mode = FILTER_MODE_EXCLUDE;
    group_member_set_sources (member, NULL, 0);
}

void
process_v2_leave (struct context *ctx,
                  const unsigned char host_eth_addr[ETH_ALEN],
                  struct igmphdr *igmp_header)
{
    log_debug ("v2 Leave Report");

    if (ignore_multicast_addr (igmp_header->group))
    {
        log_debug ("Ignored");
        return;
    }

    struct group *grp = find_group (&ctx->groups_head, igmp_header->group);
    if (grp == NULL)
    {
        /* It's a leave for a group we didn't know about.
         * Nothing to do then. */
        return;
    }

    struct group_member *member = find_member (grp, host_eth_addr);
    if (member == NULL)
    {
        /* It's a leave from a member we didn't know about.
         * Nothing to do then. */
        return;
    }

    member->filter_mode = FILTER_MODE_INCLUDE;
    group_member_set_sources(member, NULL, 0);

    trim(grp, member);
}