~phablet-team/aethercast/fix-for-microsoft-dongle

« back to all changes in this revision

Viewing changes to src/w11tng/netlinklistener.cpp

  • Committer: Tarmac
  • Author(s): Simon Fels
  • Date: 2016-03-04 14:14:09 UTC
  • mfrom: (119.4.12 ac-networking-fixes)
  • Revision ID: tarmac-20160304141409-xe8khdgb43nha1fn
Multiple changes for Networking support:

* Add a DisconnectAll dbus method to allow the UI to easily disconnect
  any connected device. Just for the purpose of a demo.
* Return proper errors when scanning fails.
* Enable miracast mode for the WiFi driver if available.
* Set GO intent by default to 7. Should be higher if we're a sink.
* Print frequencies consider for GO negotiation
* Several minor fixes
* Enable unit tests again.

Approved by PS Jenkins bot, Thomas Voß.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2015 Canonical, Ltd.
3
 
 *
4
 
 * This program is free software: you can redistribute it and/or modify it
5
 
 * under the terms of the GNU General Public License version 3, as published
6
 
 * by the Free Software Foundation.
7
 
 *
8
 
 * This program is distributed in the hope that it will be useful, but
9
 
 * WITHOUT ANY WARRANTY; without even the implied warranties of
10
 
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11
 
 * PURPOSE.  See the GNU General Public License for more details.
12
 
 *
13
 
 * You should have received a copy of the GNU General Public License along
14
 
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 
 *
16
 
 */
17
 
 
18
 
#include <asm/types.h>
19
 
#include <sys/socket.h>
20
 
#include <unistd.h>
21
 
#include <errno.h>
22
 
#include <stdio.h>
23
 
#include <string.h>
24
 
#include <net/if.h>
25
 
#include <netinet/in.h>
26
 
#include <linux/netlink.h>
27
 
#include <linux/rtnetlink.h>
28
 
#include <stdlib.h>
29
 
#include <sys/time.h>
30
 
#include <sys/types.h>
31
 
#include <arpa/inet.h>
32
 
 
33
 
#include <mcs/logger.h>
34
 
#include <mcs/keep_alive.h>
35
 
#include <mcs/networkutils.h>
36
 
 
37
 
#include "netlinklistener.h"
38
 
 
39
 
namespace w11tng {
40
 
NetlinkListener::Ptr NetlinkListener::Create(const std::weak_ptr<Delegate> &delegate) {
41
 
    return std::shared_ptr<NetlinkListener>(new NetlinkListener(delegate))->FinalizeConstruction();
42
 
}
43
 
 
44
 
NetlinkListener::Ptr NetlinkListener::FinalizeConstruction() {
45
 
    auto sp = shared_from_this();
46
 
 
47
 
    auto fd = ::socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
48
 
    if (fd < 0) {
49
 
        MCS_ERROR("Could not connect with netlink");
50
 
        return sp;
51
 
    }
52
 
 
53
 
    struct sockaddr_nl addr = {};
54
 
    memset(&addr, 0, sizeof(addr));
55
 
    addr.nl_family = AF_NETLINK;
56
 
    addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR;
57
 
 
58
 
    if (::bind(fd, (struct sockaddr*) &addr, sizeof(addr)) < 0) {
59
 
        MCS_ERROR("Failed to bind netlink socket");
60
 
        ::close(fd);
61
 
        return sp;
62
 
    }
63
 
 
64
 
    channel_ = g_io_channel_unix_new(fd);
65
 
    g_io_channel_set_encoding(channel_, nullptr, nullptr);
66
 
    g_io_channel_set_buffered(channel_, FALSE);
67
 
    g_io_channel_set_close_on_unref(channel_, TRUE);
68
 
 
69
 
    g_io_add_watch_full(channel_, 0, GIOCondition(G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP),
70
 
                        NetlinkListener::OnDataAvailable,
71
 
                        new mcs::WeakKeepAlive<NetlinkListener>(sp),
72
 
                        [](gpointer data) { delete static_cast<mcs::WeakKeepAlive<NetlinkListener>*>(data); });
73
 
 
74
 
    return sp;
75
 
}
76
 
 
77
 
NetlinkListener::NetlinkListener(const std::weak_ptr<Delegate> &delegate) :
78
 
    delegate_(delegate),
79
 
    channel_(nullptr),
80
 
    interface_index_filter_(0) {
81
 
}
82
 
 
83
 
NetlinkListener::~NetlinkListener() {
84
 
    if (channel_)
85
 
        g_io_channel_unref(channel_);
86
 
}
87
 
 
88
 
void NetlinkListener::SetInterfaceFilter(const std::string &interface_name) {
89
 
    interface_index_filter_ = mcs::NetworkUtils::RetrieveInterfaceIndex(interface_name.c_str());
90
 
}
91
 
 
92
 
std::string TypeToString(uint16_t type) {
93
 
    switch (type) {
94
 
    case NLMSG_NOOP:
95
 
        return "NOOP";
96
 
    case NLMSG_ERROR:
97
 
        return "ERROR";
98
 
    case NLMSG_DONE:
99
 
        return "DONE";
100
 
    case NLMSG_OVERRUN:
101
 
        return "OVERRUN";
102
 
    case RTM_GETLINK:
103
 
        return "GETLINK";
104
 
    case RTM_NEWLINK:
105
 
        return "NEWLINK";
106
 
    case RTM_DELLINK:
107
 
        return "DELLINK";
108
 
    case RTM_GETADDR:
109
 
        return "GETADDR";
110
 
    case RTM_NEWADDR:
111
 
        return "NEWADDR";
112
 
    case RTM_DELADDR:
113
 
        return "DELADDR";
114
 
    case RTM_GETROUTE:
115
 
        return "GETROUTE";
116
 
    case RTM_NEWROUTE:
117
 
        return "NEWROUTE";
118
 
    case RTM_DELROUTE:
119
 
        return "DELROUTE";
120
 
    case RTM_NEWNDUSEROPT:
121
 
        return "NEWNDUSEROPT";
122
 
    default:
123
 
        break;
124
 
    }
125
 
    return "unknown";
126
 
}
127
 
 
128
 
void NetlinkListener::ProcessNewAddress(struct nlmsghdr *hdr) {
129
 
    auto msg = (struct ifaddrmsg*) NLMSG_DATA(hdr);
130
 
    auto bytes = IFA_PAYLOAD(hdr);
131
 
 
132
 
    if (interface_index_filter_ > 0 && msg->ifa_index != interface_index_filter_)
133
 
        return;
134
 
 
135
 
    MCS_DEBUG("family %d index %d", (unsigned int) msg->ifa_family, msg->ifa_index);
136
 
 
137
 
    for (auto attr = IFA_RTA(msg); RTA_OK(attr, bytes); attr = RTA_NEXT(attr, bytes)) {
138
 
        switch (attr->rta_type) {
139
 
        case IFA_ADDRESS:
140
 
            auto len = static_cast<int>(RTA_PAYLOAD(attr));
141
 
 
142
 
            if (msg->ifa_family == AF_INET && len == sizeof(struct in_addr)) {
143
 
                struct in_addr addr;
144
 
                addr = *((struct in_addr*) RTA_DATA(attr));
145
 
                MCS_DEBUG("  attr address (len %d) %s", len, inet_ntoa(addr));
146
 
 
147
 
                if (auto sp = delegate_.lock())
148
 
                    sp->OnInterfaceAddressChanged(mcs::NetworkUtils::RetrieveInterfaceName(msg->ifa_index),
149
 
                                                  std::string(inet_ntoa(addr)));
150
 
            }
151
 
            break;
152
 
        }
153
 
    }
154
 
}
155
 
 
156
 
gboolean NetlinkListener::OnDataAvailable(GIOChannel *channel, GIOCondition condition, gpointer user_data) {
157
 
    auto thiz = static_cast<mcs::WeakKeepAlive<NetlinkListener>*>(user_data)->GetInstance().lock();
158
 
    if (not thiz)
159
 
        return FALSE;
160
 
 
161
 
    MCS_DEBUG("");
162
 
 
163
 
    auto fd = g_io_channel_unix_get_fd(thiz->channel_);
164
 
 
165
 
    char buf[4096];
166
 
    struct iovec iov = { buf, sizeof buf };
167
 
    struct sockaddr_nl snl;
168
 
    struct msghdr msg = { static_cast<void*>(&snl), sizeof(snl), &iov, 1, nullptr, 0, 0 };
169
 
 
170
 
    auto ret = ::recvmsg(fd, &msg, 0);
171
 
    if (ret < 0)
172
 
        return TRUE;
173
 
 
174
 
    auto len = ret;
175
 
    void *ptr = &buf[0];
176
 
    while (len > 0) {
177
 
        auto hdr = (struct nlmsghdr*) ptr;
178
 
 
179
 
        if (!NLMSG_OK(hdr, len))
180
 
            break;
181
 
 
182
 
        MCS_DEBUG("%s len %d type %d flags %d seq %d pid %d",
183
 
                  TypeToString(hdr->nlmsg_type),
184
 
                  hdr->nlmsg_len,
185
 
                  hdr->nlmsg_type,
186
 
                  hdr->nlmsg_flags,
187
 
                  hdr->nlmsg_seq,
188
 
                  hdr->nlmsg_pid);
189
 
 
190
 
        switch (hdr->nlmsg_type) {
191
 
        case RTM_NEWADDR:
192
 
            thiz->ProcessNewAddress(hdr);
193
 
            break;
194
 
        default:
195
 
            break;
196
 
        }
197
 
 
198
 
        len -= hdr->nlmsg_len;
199
 
        ptr += hdr->nlmsg_len;
200
 
    }
201
 
 
202
 
    return TRUE;
203
 
}
204
 
 
205
 
} // namespace w11tng