summaryrefslogtreecommitdiff
path: root/bus/driver-afbus.c
blob: ab162cd774c50194db6d8cbc976da9b26b07a796 (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
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* driver.c  Bus client, AF_BUS bits (driver)
 *
 * Copyright (C) 2012 Collabora Ltd
 *
 * Licensed under the Academic Free License version 2.1
 *
 * 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 <config.h>

#include "driver-afbus.h"

#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <asm/types.h>
#include <errno.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/connector.h>
#include <dbus/dbus-transport-afbus.h>

/* FIXME: CN_IDX_NFDBUS should be allocated and moved in include/linux/connector.h */
#define CN_IDX_NFDBUS              (CN_NETLINK_USERS + 3)
#define CN_VAL_NFDBUS              0x01

#define NFDBUS_CMD_ADDMATCH     0x01
#define NFDBUS_CMD_REMOVEMATCH  0x02

struct nfdbus_nl_cfg_req {
        __u32 cmd;
        __u32 len;
        struct sockaddr_bus addr;
        __u64 pad;
        unsigned char data[0];
};

struct nfdbus_nl_cfg_reply {
	__u32 ret_code;
};

static int
ensure_nl_sock(DBusError *error)
{
  static int nlsock = 0;

  struct sockaddr_nl l_local;
  int fd;
  int ret;

  if (nlsock > 0)
    return nlsock;

  fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
  if (fd == -1) {
    dbus_set_error (error, DBUS_ERROR_NETLINK,
                    "Couldn't use the netlink socket: %s",
                    strerror(errno));
    return -1;
  }

  if (!_dbus_set_fd_nonblocking (fd, error))
    {
      _dbus_close_socket (fd, NULL);
      return -1;
    }

  l_local.nl_family = AF_NETLINK;
  l_local.nl_groups = 0;
  l_local.nl_pid = 0;
  ret = bind(fd, (struct sockaddr *)&l_local, sizeof(struct sockaddr_nl));
  if (ret == -1) {
    close(fd);
    dbus_set_error (error, DBUS_ERROR_NETLINK,
                    "Couldn't bind the netlink socket: %s",
                    strerror(errno));
    return -1;
  }

  nlsock = fd;
  return nlsock;
}

static int netlink_send(int nlsock, struct cn_msg *msg, int seq)
{
  struct nlmsghdr *nlh;
  unsigned int size;
  char buf[4096];
  struct cn_msg *m;

  size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len);

  nlh = (struct nlmsghdr *)buf;
  nlh->nlmsg_seq = seq;
  nlh->nlmsg_pid = getpid();
  nlh->nlmsg_type = NLMSG_DONE;
  nlh->nlmsg_len = NLMSG_LENGTH(size - sizeof(*nlh));
  nlh->nlmsg_flags = 0;

  m = NLMSG_DATA(nlh);
  memcpy(m, msg, sizeof(*m) + msg->len);

  return send(nlsock, nlh, size, 0);
}

dbus_bool_t
bus_driver_afbus_upload_match_rule (DBusConnection      *connection,
                                    const char          *rule,
                                    DBusError           *error)
{
  static int seq;

  int nlsock;
  int ret;

  char buf[sizeof(struct cn_msg) + sizeof(struct nfdbus_nl_cfg_req) + 1024];

  struct cn_msg *data;
  struct nlmsghdr *reply;
  struct nfdbus_nl_cfg_req *req;
  //struct nfdbus_nl_cfg_reply *req_reply;

  struct sockaddr_bus address;
  long len = sizeof(address);

  memset (&address, 0, sizeof (address));
  if (!dbus_connection_get_peer_address(connection, &address, &len))
    {
      /* Don't return an error if it is not a AF_BUS socket */
      return TRUE;
    }

  nlsock = ensure_nl_sock (error);
  if (nlsock == -1)
    return FALSE;

  memset(buf, 0, sizeof(buf));

  data = (struct cn_msg *)buf;

  data->id.idx = CN_IDX_NFDBUS;
  data->id.val = CN_VAL_NFDBUS;
  data->seq = seq++;
  data->ack = 0;
  data->len = sizeof(struct nfdbus_nl_cfg_req) + strlen(rule) + 1;
  req = (struct nfdbus_nl_cfg_req *) data->data;

  req->cmd = NFDBUS_CMD_ADDMATCH;
  req->len = strlen(rule) + 1;
  req->addr = address;
  strcpy((char *)req->data, rule);

  ret = netlink_send(nlsock, data, seq++);
  if (ret <= 0)
    {
    }

  memset(buf, 0, sizeof(buf));
  ret = recv(nlsock, buf, sizeof(buf), 0);
  if (ret <= 0)
    {
    }

  reply = (struct nlmsghdr *)buf;
  if (reply->nlmsg_type != NLMSG_DONE)
    {
    }

  return TRUE;
}

dbus_bool_t
bus_driver_afbus_append_unique_name (DBusConnection *connection,
                                     DBusString     *str)
{
  struct sockaddr_bus address;
  long len = sizeof(address);

  memset (&address, 0, sizeof (address));
  if (!dbus_connection_get_peer_address(connection, &address, &len) ||
      address.sbus_family != AF_BUS)
    {
      /* Don't return an error if it is not a AF_BUS socket */
      return TRUE;
    }

  if (!_dbus_string_append (str, "AF-BUS."))
    return FALSE;

  if (!_dbus_string_append_uint (str, address.sbus_addr.s_addr))
    return FALSE;

  if (!_dbus_string_append (str, "."))
    return FALSE;

  return TRUE;
}

dbus_bool_t
bus_driver_afbus_emit_forwarded (DBusConnection *connection,
                                 DBusConnection *addressed_recipient,
                                 const char     *service_name)
{
  struct sockaddr_bus address;
  long len = sizeof (address);
  DBusMessage *message;
  BusContext *context;
  BusTransaction *transaction = NULL;
  dbus_bool_t result = FALSE;

  memset (&address, 0, sizeof (address));
  if (!dbus_connection_get_peer_address (addressed_recipient, &address, &len))
    {
      /* Don't return an error if it is not a AF_BUS socket */
      return TRUE;
    }

  /* Prepare the message to be sent */
  message = dbus_message_new_signal (DBUS_PATH_AFBUS,
                                     DBUS_INTERFACE_AFBUS,
                                     "Forwarded");
  if (message == NULL)
    {
      _dbus_verbose ("Could not allocate AF_BUS.Forwarded signal message\n");
      return FALSE;
    }

  if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
    {
      _dbus_verbose ("Could not set sender for AF_BUS.Forwarded signal message\n");
      goto out;
    }

  if (!dbus_message_append_args (message,
                                 DBUS_TYPE_STRING, &service_name,
                                 DBUS_TYPE_UINT64, &address.sbus_addr.s_addr,
                                 DBUS_TYPE_INVALID))
    {
      _dbus_verbose ("Could not append arguments for AF_BUS.Forwarded signal message\n");
      goto out;
    }

  /* Prepare transaction */
  context = bus_connection_get_context (connection);
  transaction = bus_transaction_new (context);
  if (transaction == NULL)
    {
      _dbus_verbose ("Could not create transaction\n");
      goto out;
    }

  if (bus_transaction_send (transaction, addressed_recipient, message))
    result = TRUE;
  else
      _dbus_verbose ("Could not send AF_BUS.Forwarded signal message\n");

 out:
  if (transaction != NULL)
    bus_transaction_execute_and_free (transaction);

  dbus_message_unref (message);

  return result;
}