aboutsummaryrefslogtreecommitdiff
path: root/src/mm-port-net.c
blob: c2c0f78e97bbce1ed65c8c786a2e03291ce59c79 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * 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:
 *
 * Copyright (C) 2021 - Aleksander Morgado <aleksander@aleksander.es>
 */

#include <net/if.h>

#include <ModemManager.h>
#include <mm-errors-types.h>

#include "mm-port-net.h"
#include "mm-log-object.h"
#include "mm-netlink.h"

G_DEFINE_TYPE (MMPortNet, mm_port_net, MM_TYPE_PORT)

struct _MMPortNetPrivate {
    guint ifindex;
};

static void
ensure_ifindex (MMPortNet *self)
{
    if (!self->priv->ifindex) {
        self->priv->ifindex = if_nametoindex (mm_port_get_device (MM_PORT (self)));
        if (!self->priv->ifindex)
            mm_obj_warn (self, "couldn't get interface index");
        else
            mm_obj_dbg (self, "interface index: %u", self->priv->ifindex);
    }
}

/*****************************************************************************/

gboolean
mm_port_net_link_setup_finish (MMPortNet     *self,
                               GAsyncResult  *res,
                               GError       **error)
{
    return g_task_propagate_boolean (G_TASK (res), error);
}

static void
netlink_setlink_ready (MMNetlink    *netlink,
                       GAsyncResult *res,
                       GTask        *task)
{
    GError *error = NULL;

    if (!mm_netlink_setlink_finish (netlink, res, &error)) {
        g_prefix_error (&error, "netlink operation failed: ");
        g_task_return_error (task, error);
    } else
        g_task_return_boolean (task, TRUE);
    g_object_unref (task);
}

void
mm_port_net_link_setup (MMPortNet            *self,
                        gboolean              up,
                        guint                 mtu,
                        GCancellable         *cancellable,
                        GAsyncReadyCallback   callback,
                        gpointer              user_data)
{
    GTask *task;

    task = g_task_new (self, cancellable, callback, user_data);

    ensure_ifindex (self);
    if (!self->priv->ifindex) {
        g_task_return_new_error (task, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
                                 "no valid interface index found for %s",
                                 mm_port_get_device (MM_PORT (self)));
        g_object_unref (task);
        return;
    }

    mm_netlink_setlink (mm_netlink_get (), /* singleton */
                        self->priv->ifindex,
                        up,
                        mtu,
                        cancellable,
                        (GAsyncReadyCallback) netlink_setlink_ready,
                        task);
}

/*****************************************************************************/

GByteArray *
mm_port_net_get_hwaddress_finish (MMPortNet     *self,
                                  GAsyncResult  *res,
                                  GError       **error)
{
    return g_task_propagate_pointer (G_TASK (res), error);
}

static void
netlink_get_hwaddress_ready (MMNetlink    *netlink,
                             GAsyncResult *res,
                             GTask        *task)
{
    GError     *error = NULL;
    GByteArray *hwaddr;

    hwaddr = mm_netlink_get_hwaddr_finish (netlink, res, &error);
    if (!hwaddr) {
        g_prefix_error (&error, "netlink operation failed: ");
        g_task_return_error (task, error);
    } else
        g_task_return_pointer (task, hwaddr, (GDestroyNotify) g_byte_array_unref);
    g_object_unref (task);
}

void
mm_port_net_get_hwaddress (MMPortNet            *self,
                           GCancellable         *cancellable,
                           GAsyncReadyCallback   callback,
                           gpointer              user_data)
{
    GTask *task;

    task = g_task_new (self, cancellable, callback, user_data);

    ensure_ifindex (self);
    if (!self->priv->ifindex) {
        g_task_return_new_error (task, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
                                 "no valid interface index found for %s",
                                 mm_port_get_device (MM_PORT (self)));
        g_object_unref (task);
        return;
    }

    mm_netlink_get_hwaddr (mm_netlink_get (), /* singleton */
                           self->priv->ifindex,
                           cancellable,
                           (GAsyncReadyCallback) netlink_get_hwaddress_ready,
                           task);
}

/*****************************************************************************/

MMPortNet *
mm_port_net_new (const gchar *name)
{
    return MM_PORT_NET (g_object_new (MM_TYPE_PORT_NET,
                                      MM_PORT_DEVICE, name,
                                      MM_PORT_SUBSYS, MM_PORT_SUBSYS_NET,
                                      MM_PORT_GROUP,  MM_PORT_GROUP_USED,
                                      MM_PORT_TYPE,   MM_PORT_TYPE_NET,
                                      NULL));
}

static void
mm_port_net_init (MMPortNet *self)
{
    self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, MM_TYPE_PORT_NET, MMPortNetPrivate);
}

static void
mm_port_net_class_init (MMPortNetClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

    g_type_class_add_private (object_class, sizeof (MMPortNetPrivate));
}