📄 server.c
字号:
return FALSE;}static void connect_event(GIOChannel *chan, int err, const bdaddr_t *src, const bdaddr_t *dst, gpointer user_data){ struct timeout *to; if (err < 0) { error("accept(): %s(%d)", strerror(errno), errno); return; } g_io_channel_set_close_on_unref(chan, TRUE); /* * BNEP_SETUP_CONNECTION_REQUEST_MSG shall be received and * user shall authorize the incomming connection before * the time expires. */ to = g_malloc0(sizeof(struct timeout)); to->id = g_timeout_add(SETUP_TIMEOUT, timeout_cb, to); to->watch = g_io_add_watch_full(chan, G_PRIORITY_DEFAULT, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL, bnep_setup, to, setup_destroy); g_io_channel_unref(chan); return;}int server_init(DBusConnection *conn, const char *iface_prefix, gboolean secure){ int lm; lm = secure ? L2CAP_LM_SECURE : 0; security = secure; connection = dbus_connection_ref(conn); prefix = iface_prefix; bnep_io = bt_l2cap_listen(BDADDR_ANY, BNEP_PSM, BNEP_MTU, lm, connect_event, NULL); if (!bnep_io) return -1; g_io_channel_set_close_on_unref(bnep_io, FALSE); if (bridge_create(BNEP_SVC_GN) < 0) error("Can't create GN bridge"); return 0;}void server_exit(){ if (bnep_io != NULL) { g_io_channel_close(bnep_io); g_io_channel_unref(bnep_io); bnep_io = NULL; } if (bridge_remove(BNEP_SVC_GN) < 0) error("Can't remove GN bridge"); dbus_connection_unref(connection); connection = NULL;}static uint32_t register_server_record(struct network_server *ns){ sdp_record_t *record; record = server_record_new(ns->name, ns->id); if (!record) { error("Unable to allocate new service record"); return 0; } if (add_record_to_server(&ns->src, record) < 0) { error("Failed to register service record"); sdp_record_free(record); return 0; } debug("register_server_record: got record id 0x%x", record->handle); return record->handle;}static DBusMessage *get_uuid(DBusConnection *conn, DBusMessage *msg, void *data){ struct network_server *ns = data; DBusMessage *reply; const char *uuid; reply = dbus_message_new_method_return(msg); if (!reply) return NULL; uuid = bnep_uuid(ns->id); dbus_message_append_args(reply, DBUS_TYPE_STRING, &uuid, DBUS_TYPE_INVALID); return reply;}static inline DBusMessage *failed(DBusMessage *msg, const char *description){ return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed", description);}static inline DBusMessage *invalid_arguments(DBusMessage *msg, const char *description){ return g_dbus_create_error(msg, ERROR_INTERFACE ".InvalidArguments", description);}static DBusMessage *enable(DBusConnection *conn, DBusMessage *msg, void *data){ struct network_server *ns = data; DBusMessage *reply; if (ns->enable) return g_dbus_create_error(msg, ERROR_INTERFACE ".AlreadyExist", "Server already enabled"); if (bacmp(&ns->src, BDADDR_ANY) == 0) { int dev_id; dev_id = hci_get_route(&ns->src); if ((dev_id < 0) || (hci_devba(dev_id, &ns->src) < 0)) return failed(msg, "Adapter not available"); /* Store the server info */ server_store(ns->path); } reply = dbus_message_new_method_return(msg); if (!reply) return NULL; /* Add the service record */ ns->record_id = register_server_record(ns); if (!ns->record_id) { dbus_message_unref(reply); return failed(msg, "Service record registration failed"); } ns->enable = TRUE; store_property(&ns->src, ns->id, "enabled", "1"); g_dbus_emit_signal(conn, ns->path, NETWORK_SERVER_INTERFACE, "Enabled", DBUS_TYPE_INVALID); return reply;}static void kill_connection(void *data, void *udata){ const char *address = data; bdaddr_t dst; str2ba(address, &dst); bnep_kill_connection(&dst);}static DBusMessage *disable(DBusConnection *conn, DBusMessage *msg, void *data){ struct network_server *ns = data; DBusMessage *reply; reply = dbus_message_new_method_return(msg); if (!reply) return NULL; if (!ns->enable) return failed(msg, "Not enabled"); /* Remove the service record */ if (ns->record_id) { remove_record_from_server(ns->record_id); ns->record_id = 0; } ns->enable = FALSE; g_slist_foreach(ns->clients, (GFunc) kill_connection, NULL); store_property(&ns->src, ns->id, "enabled", "0"); g_dbus_emit_signal(conn, ns->path, NETWORK_SERVER_INTERFACE, "Disabled", DBUS_TYPE_INVALID); return reply;}static DBusMessage *is_enabled(DBusConnection *conn, DBusMessage *msg, void *data){ struct network_server *ns = data; DBusMessage *reply; reply = dbus_message_new_method_return(msg); if (!reply) return NULL; dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &ns->enable, DBUS_TYPE_INVALID); return reply;}static DBusMessage *set_name(DBusConnection *conn, DBusMessage *msg, void *data){ struct network_server *ns = data; DBusMessage *reply; const char *name; reply = dbus_message_new_method_return(msg); if (!reply) return NULL; if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID)) return NULL; if (!name || (strlen(name) == 0)) return invalid_arguments(msg, "Invalid name"); if (ns->name) g_free(ns->name); ns->name = g_strdup(name); if (ns->enable && ns->record_id) { uint32_t handle = register_server_record(ns); if (!handle) { dbus_message_unref(reply); return failed(msg, "Service record attribute update failed"); } remove_record_from_server(ns->record_id); ns->record_id = handle; } store_property(&ns->src, ns->id, "name", ns->name); return reply;}static DBusMessage *get_name(DBusConnection *conn, DBusMessage *msg, void *data){ struct network_server *ns = data; char name[] = ""; const char *pname = (ns->name ? ns->name : name); DBusMessage *reply; reply = dbus_message_new_method_return(msg); if (!reply) return NULL; dbus_message_append_args(reply, DBUS_TYPE_STRING, &pname, DBUS_TYPE_INVALID); return reply;}static DBusMessage *set_address_range(DBusConnection *conn, DBusMessage *msg, void *data){ return NULL;}static DBusMessage *set_routing(DBusConnection *conn, DBusMessage *msg, void *data){ struct network_server *ns = data; const char *iface; if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &iface, DBUS_TYPE_INVALID)) return NULL; /* FIXME: Check if the interface is valid/UP */ if (!iface || (strlen(iface) == 0)) return invalid_arguments(msg, "Invalid interface"); if (ns->iface) g_free(ns->iface); ns->iface = g_strdup(iface); return dbus_message_new_method_return(msg);}static DBusMessage *get_info(DBusConnection *conn, DBusMessage *msg, void *data){ struct network_server *ns = data; DBusMessage *reply; DBusMessageIter iter; DBusMessageIter dict; const char *uuid; reply = dbus_message_new_method_return(msg); if (!reply) return NULL; dbus_message_iter_init_append(reply, &iter); dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict); dbus_message_iter_append_dict_entry(&dict, "name", DBUS_TYPE_STRING, &ns->name); uuid = bnep_uuid(ns->id); dbus_message_iter_append_dict_entry(&dict, "uuid", DBUS_TYPE_STRING, &uuid); dbus_message_iter_close_container(&iter, &dict); return reply;}static void server_free(struct network_server *ns){ if (!ns) return; /* FIXME: Missing release/free all bnepX interfaces */ if (ns->record_id) remove_record_from_server(ns->record_id); if (ns->iface) g_free(ns->iface); if (ns->name) g_free(ns->name); if (ns->range) g_free(ns->range); if (ns->path) g_free(ns->path); if (ns->clients) { g_slist_foreach(ns->clients, (GFunc) g_free, NULL); g_slist_free(ns->clients); } g_free(ns);}static void server_unregister(void *data){ struct network_server *ns = data; info("Unregistered server path:%s", ns->path); servers = g_slist_remove(servers, ns); server_free(ns);}static GDBusMethodTable server_methods[] = { { "GetUUID", "", "s", get_uuid }, { "Enable", "", "", enable }, { "Disable", "", "", disable }, { "IsEnabled", "", "b", is_enabled }, { "SetName", "s", "", set_name }, { "GetName", "", "s", get_name }, { "SetAddressRange", "ss", "", set_address_range }, { "SetRouting", "s", "", set_routing }, { "GetInfo", "", "a{sv}",get_info }, { }};static GDBusSignalTable server_signals[] = { { "Enabled", "" }, { "Disabled", "" }, { }};int server_register(const char *path, bdaddr_t *src, uint16_t id){ struct network_server *ns; if (!path) return -EINVAL; ns = g_new0(struct network_server, 1); if (!g_dbus_register_interface(connection, path, NETWORK_SERVER_INTERFACE, server_methods, server_signals, NULL, ns, server_unregister)) { error("D-Bus failed to register %s interface", NETWORK_SERVER_INTERFACE); server_free(ns); return -1; } /* Setting a default name */ if (id == BNEP_SVC_NAP) ns->name = g_strdup("BlueZ NAP service"); else if (id == BNEP_SVC_GN) ns->name = g_strdup("BlueZ GN service"); else ns->name = g_strdup("BlueZ PANU service"); ns->path = g_strdup(path); ns->id = id; bacpy(&ns->src, src); servers = g_slist_append(servers, ns); info("Registered server path:%s", path); return 0;}int server_register_from_file(const char *path, const bdaddr_t *src, uint16_t id, const char *filename){ struct network_server *ns; char *str; if (!path) return -EINVAL; ns = g_new0(struct network_server, 1); bacpy(&ns->src, src); ns->path = g_strdup(path); ns->id = id; ns->name = textfile_get(filename, "name"); if (!ns->name) { /* Name is mandatory */ server_free(ns); return -1; } ns->range = textfile_get(filename, "address_range"); ns->iface = textfile_get(filename, "routing"); str = textfile_get(filename, "enabled"); if (str) { if (strcmp("1", str) == 0) { ns->record_id = register_server_record(ns); ns->enable = TRUE; } g_free(str); } if (!g_dbus_register_interface(connection, path, NETWORK_SERVER_INTERFACE, server_methods, server_signals, NULL, ns, server_unregister)) { error("D-Bus failed to register %s interface", NETWORK_SERVER_INTERFACE); server_free(ns); return -1; } servers = g_slist_append(servers, ns); info("Registered server path:%s", path); return 0;}int server_store(const char *path){ struct network_server *ns; char filename[PATH_MAX + 1]; char addr[18]; GSList *l; l = g_slist_find_custom(servers, path, find_server); if (!l) { error("Unable to salve %s on storage", path); return -ENOENT; } ns = l->data; ba2str(&ns->src, addr); if (ns->id == BNEP_SVC_NAP) create_name(filename, PATH_MAX, STORAGEDIR, addr, "nap"); else if (ns->id == BNEP_SVC_GN) create_name(filename, PATH_MAX, STORAGEDIR, addr, "gn"); else create_name(filename, PATH_MAX, STORAGEDIR, addr, "panu"); create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); textfile_put(filename, "name", ns->name); if (ns->iface) textfile_put(filename, "routing", ns->iface); if (ns->range) textfile_put(filename, "range", ns->range); textfile_put(filename, "enabled", ns->enable ? "1": "0"); return 0;}int server_find_data(const char *path, const char *pattern){ struct network_server *ns; const char *uuid; GSList *l; l = g_slist_find_custom(servers, path, find_server); if (!l) return -1; ns = l->data; if (ns->name && strcasecmp(pattern, ns->name) == 0) return 0; if (ns->iface && strcasecmp(pattern, ns->iface) == 0) return 0; uuid = bnep_name(ns->id); if (uuid && strcasecmp(pattern, uuid) == 0) return 0; if (bnep_service_id(pattern) == ns->id) return 0; return -1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -