summaryrefslogtreecommitdiff
path: root/mobicore/daemon/Common/NetlinkConnection.cpp
blob: f91d4e9ea45c063855761f4a91dc6fc667fff61f (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/** @addtogroup MCD_MCDIMPL_DAEMON_SRV
 * @{
 * @file
 *
 * Connection data.
 *
 * Copyright (c) 2013 TRUSTONIC LIMITED
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the TRUSTONIC LIMITED nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <cstring>
#include <errno.h>
#include <stdio.h>
#include <sys/socket.h>
#include <linux/netlink.h>

#include "NetlinkConnection.h"

#include "log.h"


uint64_t hashConnection(
    pid_t pid,
    uint32_t seq
)
{
    return (((uint64_t)seq) << 32) | (uint64_t)pid;
}


//------------------------------------------------------------------------------
NetlinkConnection::NetlinkConnection(
    void
) : Connection(),
    dataLeft(0),
    manager(NULL)
{
    detached = false;
    dataMsg = NULL;
    dataStart = NULL;
    dataLen = 0;


    selfPid = getpid();
    peerPid = 0;
    sequenceMagic = 0;
    hash = hashConnection(peerPid, sequenceMagic);
}


//------------------------------------------------------------------------------
NetlinkConnection::NetlinkConnection(
    NetlinkConnectionManager    *manager,
    int                         socketDescriptor,
    uint32_t                    pid,
    uint32_t                    seq
): Connection(),
    dataLeft(0),
    manager(manager)
{
    detached = false;
    dataMsg = NULL;
    dataStart = NULL;
    dataLen = 0;

    this->socketDescriptor = socketDescriptor;
    selfPid = getpid();
    peerPid = pid;
    sequenceMagic = seq;
    hash = hashConnection(pid, seq);
}


//------------------------------------------------------------------------------
NetlinkConnection::~NetlinkConnection(
    void
)
{
    LOG_I("%s: destroy connection for PID 0x%X", __FUNCTION__, peerPid);
    socketDescriptor = -1;
    free(dataMsg);

    if (manager) {
        manager->removeConnection(hash);
    }
}


//------------------------------------------------------------------------------
bool NetlinkConnection::connect(
    const char *dest
)
{
    struct sockaddr_nl addr;
    bool ret = false;

    assert(NULL != dest);

    LOG_I("%s: Connecting to SEQ 0x%X", __FUNCTION__, MC_DAEMON_PID);
    do {
        if ((socketDescriptor = socket(PF_NETLINK, SOCK_DGRAM, MC_DAEMON_NETLINK)) < 0) {
            LOG_E("%s: Can't open netlink socket - errno: %d(%s)",
                  __FUNCTION__, errno, strerror(errno));
            break;
        }
        memset(&addr, 0, sizeof(addr));
        addr.nl_family = AF_NETLINK;
        addr.nl_pid = selfPid;  /* self pid */
        addr.nl_groups = 0;  /* not in mcast groups */

        if (bind(socketDescriptor, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
            LOG_E("%s: bind() failed - errno: %d(%s)", __FUNCTION__, errno, strerror(errno));
            close(socketDescriptor);

            // Set invalid socketDescriptor
            socketDescriptor = -1;
            break;
        }
        ret = true;


    } while (false);

    return ret;
}


//------------------------------------------------------------------------------
void NetlinkConnection::handleMessage(
    struct nlmsghdr *nlh
)
{
    dataMutex.lock();
    /* Takeover the buffer */
    dataMsg = nlh;
    dataLen = NLMSG_PAYLOAD(dataMsg, 0);
    dataStart = static_cast<uint8_t *>(NLMSG_DATA(dataMsg));
    dataMutex.unlock();
    dataLeft.signal();
}

//------------------------------------------------------------------------------
size_t NetlinkConnection::readData(
    void *buffer,
    uint32_t len
)
{
    return readData(buffer, len, -1);
}


//------------------------------------------------------------------------------
size_t NetlinkConnection::readData(
    void      *buffer,
    uint32_t  len,
    int32_t   timeout
)
{
    size_t ret = -1;
    assert(NULL != buffer);

    if (!dataLeft.wait(timeout)) {
        return -2;
    }
    dataMutex.lock();
    // No data left?? Could we get this far?
    if (dataLen <= 0) {
        dataMutex.unlock();
        return -2;
    }

    //LOG_I("%s: reading connection data %u, connection data left %u",
    //      __FUNCTION__, len, dataLen);

    assert(dataStart != NULL);

    // trying to read more than the left data
    if (len > dataLen) {
        ret = dataLen;
        memcpy(buffer, dataStart, dataLen);
        dataLen = 0;
    } else {
        ret = len;
        memcpy(buffer, dataStart, len);
        dataLen -= len;
        dataStart += len;
    }

    if (dataLen == 0) {
        dataStart = NULL;
        free(dataMsg);
        dataMsg = NULL;
    } else {
        // Still some data left
        dataLeft.signal();
    }
    dataMutex.unlock();

    //LOG_I("%s: read %u", __FUNCTION__, ret);
    return ret;
}

//------------------------------------------------------------------------------
size_t NetlinkConnection::writeData(
    void *buffer,
    uint32_t len
)
{
    size_t ret;
    struct sockaddr_nl dest_addr;
    struct nlmsghdr *nlh = NULL;
    struct iovec iov;
    struct msghdr msg;

    assert(NULL != buffer);
    assert(-1 != socketDescriptor);

    //LOG_I("%s: send data %u to PID %u", __FUNCTION__, len, sequenceMagic);

    memset(&dest_addr, 0, sizeof(dest_addr));
    memset(&msg, 0, sizeof(msghdr));
    dest_addr.nl_family = AF_NETLINK;
    dest_addr.nl_pid = peerPid;
    dest_addr.nl_groups = 0; /* unicast */

    nlh = (struct nlmsghdr *)malloc(
              NLMSG_SPACE(len));
    if (nlh == NULL) {
        LOG_E("Allocation failure");
        return -1;
    }

    /* Fill the netlink message header */
    nlh->nlmsg_len = NLMSG_SPACE(len);
    nlh->nlmsg_pid = selfPid;
    nlh->nlmsg_flags = NLM_F_REQUEST;
    nlh->nlmsg_seq = sequenceMagic;

    /* Fill in the netlink message payload */
    memcpy(NLMSG_DATA(nlh), buffer, len);

    iov.iov_base = (void *)nlh;
    iov.iov_len = nlh->nlmsg_len;
    msg.msg_name = (void *)&dest_addr;
    msg.msg_namelen = sizeof(dest_addr);
    msg.msg_iov = &iov;
    msg.msg_iovlen = 1;
    msg.msg_control = NULL;
    msg.msg_controllen = 0;

    ret = sendmsg(socketDescriptor, &msg, 0);
    if (ret != NLMSG_SPACE(len)) {
        LOG_E( "%s: could no send all data, ret=%d, errno: %d(%s)",
               __FUNCTION__, ret, errno, strerror(errno));
        ret = -1;
    } else {
        /* The whole message sent also includes the header, so make sure to
         * return only the number of payload data sent, not everything */
        ret = len;
    }

    free(nlh);

    return ret;
}

/** @} */