summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarcel Holtmann <marcel@holtmann.org>2007-01-13 18:02:10 +0000
committerMarcel Holtmann <marcel@holtmann.org>2007-01-13 18:02:10 +0000
commit3f5e2295dd6b747609ad3a893a76af5ba2146c93 (patch)
treecd46adc980b419fe057c0909cd3ccce19fcf1bf5
parent607695ed109340f4b7a5628420e0a8e8aee34f4e (diff)
Better integration into a SDP server library
-rw-r--r--sdpd/main.c203
-rw-r--r--sdpd/request.c40
-rw-r--r--sdpd/sdpd.h5
-rw-r--r--sdpd/service.c166
4 files changed, 209 insertions, 205 deletions
diff --git a/sdpd/main.c b/sdpd/main.c
index 56370dd8..60d477ec 100644
--- a/sdpd/main.c
+++ b/sdpd/main.c
@@ -54,171 +54,6 @@ static GMainLoop *event_loop;
static int l2cap_sock, unix_sock;
-static sdp_record_t *server;
-
-/*
- * List of version numbers supported by the SDP server.
- * Add to this list when newer versions are supported.
- */
-static sdp_version_t sdpVnumArray[1] = {
- { 1, 0 }
-};
-static const int sdpServerVnumEntries = 1;
-
-/*
- * The service database state is an attribute of the service record
- * of the SDP server itself. This attribute is guaranteed to
- * change if any of the contents of the service repository
- * changes. This function updates the timestamp of value of
- * the svcDBState attribute
- * Set the SDP server DB. Simply a timestamp which is the marker
- * when the DB was modified.
- */
-void update_db_timestamp(void)
-{
- uint32_t dbts = sdp_get_time();
- sdp_data_t *d = sdp_data_alloc(SDP_UINT32, &dbts);
- sdp_attr_replace(server, SDP_ATTR_SVCDB_STATE, d);
-}
-
-static void add_lang_attr(sdp_record_t *r)
-{
- sdp_lang_attr_t base_lang;
- sdp_list_t *langs = 0;
-
- base_lang.code_ISO639 = (0x65 << 8) | 0x6e;
- // UTF-8 MIBenum (http://www.iana.org/assignments/character-sets)
- base_lang.encoding = 106;
- base_lang.base_offset = SDP_PRIMARY_LANG_BASE;
- langs = sdp_list_append(0, &base_lang);
- sdp_set_lang_attr(r, langs);
- sdp_list_free(langs, 0);
-}
-
-static void register_public_browse_group(int public)
-{
- sdp_list_t *browselist;
- uuid_t bgscid, pbgid;
- sdp_data_t *sdpdata;
- sdp_record_t *browse = sdp_record_alloc();
-
- if (public) {
- browse->handle = sdp_next_handle();
- if (browse->handle < 0x10000)
- return;
- } else
- browse->handle = SDP_SERVER_RECORD_HANDLE + 1;
-
- sdp_record_add(BDADDR_ANY, browse);
- sdpdata = sdp_data_alloc(SDP_UINT32, &browse->handle);
- sdp_attr_add(browse, SDP_ATTR_RECORD_HANDLE, sdpdata);
-
- add_lang_attr(browse);
- sdp_set_info_attr(browse, "Public Browse Group Root", "BlueZ", "Root of public browse hierarchy");
-
- sdp_uuid16_create(&bgscid, BROWSE_GRP_DESC_SVCLASS_ID);
- browselist = sdp_list_append(0, &bgscid);
- sdp_set_service_classes(browse, browselist);
- sdp_list_free(browselist, 0);
-
- if (public) {
- sdp_uuid16_create(&pbgid, PUBLIC_BROWSE_GROUP);
- sdp_set_group_id(browse, pbgid);
- }
-}
-
-/*
- * The SDP server must present its own service record to
- * the service repository. This can be accessed by service
- * discovery clients. This method constructs a service record
- * and stores it in the repository
- */
-static void register_server_service(int public)
-{
- int i;
- sdp_list_t *classIDList, *browseList;
- sdp_list_t *access_proto = 0;
- uuid_t l2cap, classID, browseGroupId, sdpSrvUUID;
- void **versions, **versionDTDs;
- uint8_t dtd;
- uint16_t version, port;
- sdp_data_t *pData, *port_data, *version_data;
- sdp_list_t *pd, *seq;
-
- server = sdp_record_alloc();
- server->pattern = NULL;
-
- /* Force the record to be SDP_SERVER_RECORD_HANDLE */
- server->handle = SDP_SERVER_RECORD_HANDLE;
-
- sdp_record_add(BDADDR_ANY, server);
- sdp_attr_add(server, SDP_ATTR_RECORD_HANDLE, sdp_data_alloc(SDP_UINT32, &server->handle));
-
- /*
- * Add all attributes to service record. (No need to commit since we
- * are the server and this record is already in the database.)
- */
- add_lang_attr(server);
- sdp_set_info_attr(server, "SDP Server", "BlueZ", "Bluetooth service discovery server");
-
- sdp_uuid16_create(&classID, SDP_SERVER_SVCLASS_ID);
- classIDList = sdp_list_append(0, &classID);
- sdp_set_service_classes(server, classIDList);
- sdp_list_free(classIDList, 0);
-
- /*
- * Set the version numbers supported, these are passed as arguments
- * to the server on command line. Now defaults to 1.0
- * Build the version number sequence first
- */
- versions = (void **)malloc(sdpServerVnumEntries * sizeof(void *));
- versionDTDs = (void **)malloc(sdpServerVnumEntries * sizeof(void *));
- dtd = SDP_UINT16;
- for (i = 0; i < sdpServerVnumEntries; i++) {
- uint16_t *version = malloc(sizeof(uint16_t));
- *version = sdpVnumArray[i].major;
- *version = (*version << 8);
- *version |= sdpVnumArray[i].minor;
- versions[i] = version;
- versionDTDs[i] = &dtd;
- }
- pData = sdp_seq_alloc(versionDTDs, versions, sdpServerVnumEntries);
- for (i = 0; i < sdpServerVnumEntries; i++)
- free(versions[i]);
- free(versions);
- free(versionDTDs);
- sdp_attr_add(server, SDP_ATTR_VERSION_NUM_LIST, pData);
-
- sdp_uuid16_create(&sdpSrvUUID, SDP_UUID);
- sdp_set_service_id(server, sdpSrvUUID);
-
- sdp_uuid16_create(&l2cap, L2CAP_UUID);
- pd = sdp_list_append(0, &l2cap);
- port = SDP_PSM;
- port_data = sdp_data_alloc(SDP_UINT16, &port);
- pd = sdp_list_append(pd, port_data);
- version = 1;
- version_data = sdp_data_alloc(SDP_UINT16, &version);
- pd = sdp_list_append(pd, version_data);
- seq = sdp_list_append(0, pd);
-
- access_proto = sdp_list_append(0, seq);
- sdp_set_access_protos(server, access_proto);
- sdp_list_free(access_proto, free);
- sdp_data_free(port_data);
- sdp_data_free(version_data);
- sdp_list_free(pd, 0);
-
- if (public) {
- sdp_uuid16_create(&browseGroupId, PUBLIC_BROWSE_GROUP);
- browseList = sdp_list_append(0, &browseGroupId);
- sdp_set_browse_groups(server, browseList);
- sdp_list_free(browseList, 0);
- }
-
- update_db_timestamp();
-}
-
/*
* SDP server initialization on startup includes creating the
* l2cap and unix sockets over which discovery and registration clients
@@ -306,42 +141,6 @@ static int init_server(uint16_t mtu, int master, int public)
return 0;
}
-static inline void handle_request(int sk, uint8_t *data, int len)
-{
- struct sockaddr_l2 sa;
- socklen_t size;
- sdp_req_t req;
-
- size = sizeof(sa);
- if (getpeername(sk, (struct sockaddr *) &sa, &size) < 0)
- return;
-
- if (sa.l2_family == AF_BLUETOOTH) {
- struct l2cap_options lo;
- memset(&lo, 0, sizeof(lo));
- size = sizeof(lo);
- getsockopt(sk, SOL_L2CAP, L2CAP_OPTIONS, &lo, &size);
- bacpy(&req.bdaddr, &sa.l2_bdaddr);
- req.mtu = lo.omtu;
- req.local = 0;
- memset(&sa, 0, sizeof(sa));
- size = sizeof(sa);
- getsockname(sk, (struct sockaddr *) &sa, &size);
- bacpy(&req.device, &sa.l2_bdaddr);
- } else {
- bacpy(&req.device, BDADDR_ANY);
- bacpy(&req.bdaddr, BDADDR_LOCAL);
- req.mtu = 2048;
- req.local = 1;
- }
-
- req.sock = sk;
- req.buf = data;
- req.len = len;
-
- process_request(&req);
-}
-
static gboolean io_session_event(GIOChannel *chan, GIOCondition cond, gpointer data)
{
sdp_pdu_hdr_t hdr;
@@ -523,7 +322,7 @@ int main(int argc, char *argv[])
g_io_channel_unref(l2cap_io);
- info("Exit");
+ info("Exit");
stop_logging();
diff --git a/sdpd/request.c b/sdpd/request.c
index 2fc4b8b6..2b052863 100644
--- a/sdpd/request.c
+++ b/sdpd/request.c
@@ -30,10 +30,12 @@
#include <stdio.h>
#include <errno.h>
+#include <stdlib.h>
#include <malloc.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
+#include <bluetooth/l2cap.h>
#include <bluetooth/sdp.h>
#include <bluetooth/sdp_lib.h>
@@ -765,7 +767,7 @@ done:
* function based on request type. Handles service registration
* client requests also.
*/
-void process_request(sdp_req_t *req)
+static void process_request(sdp_req_t *req)
{
sdp_pdu_hdr_t *reqhdr = (sdp_pdu_hdr_t *)req->buf;
sdp_pdu_hdr_t *rsphdr;
@@ -852,3 +854,39 @@ send_rsp:
free(rsp.data);
free(req->buf);
}
+
+void handle_request(int sk, uint8_t *data, int len)
+{
+ struct sockaddr_l2 sa;
+ socklen_t size;
+ sdp_req_t req;
+
+ size = sizeof(sa);
+ if (getpeername(sk, (struct sockaddr *) &sa, &size) < 0)
+ return;
+
+ if (sa.l2_family == AF_BLUETOOTH) {
+ struct l2cap_options lo;
+ memset(&lo, 0, sizeof(lo));
+ size = sizeof(lo);
+ getsockopt(sk, SOL_L2CAP, L2CAP_OPTIONS, &lo, &size);
+ bacpy(&req.bdaddr, &sa.l2_bdaddr);
+ req.mtu = lo.omtu;
+ req.local = 0;
+ memset(&sa, 0, sizeof(sa));
+ size = sizeof(sa);
+ getsockname(sk, (struct sockaddr *) &sa, &size);
+ bacpy(&req.device, &sa.l2_bdaddr);
+ } else {
+ bacpy(&req.device, BDADDR_ANY);
+ bacpy(&req.bdaddr, BDADDR_LOCAL);
+ req.mtu = 2048;
+ req.local = 1;
+ }
+
+ req.sock = sk;
+ req.buf = data;
+ req.len = len;
+
+ process_request(&req);
+}
diff --git a/sdpd/sdpd.h b/sdpd/sdpd.h
index 97637125..a22096d6 100644
--- a/sdpd/sdpd.h
+++ b/sdpd/sdpd.h
@@ -37,12 +37,15 @@ typedef struct request {
int len;
} sdp_req_t;
-void process_request(sdp_req_t *req);
+void handle_request(int sk, uint8_t *data, int len);
int service_register_req(sdp_req_t *req, sdp_buf_t *rsp);
int service_update_req(sdp_req_t *req, sdp_buf_t *rsp);
int service_remove_req(sdp_req_t *req, sdp_buf_t *rsp);
+void register_public_browse_group(int public);
+void register_server_service(int public);
+
typedef struct {
uint32_t timestamp;
union {
diff --git a/sdpd/service.c b/sdpd/service.c
index bea82930..ae4a7c10 100644
--- a/sdpd/service.c
+++ b/sdpd/service.c
@@ -30,6 +30,7 @@
#include <stdio.h>
#include <errno.h>
+#include <stdlib.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
@@ -41,7 +42,170 @@
#include "sdpd.h"
#include "logging.h"
-extern void update_db_timestamp(void);
+static sdp_record_t *server;
+
+/*
+ * List of version numbers supported by the SDP server.
+ * Add to this list when newer versions are supported.
+ */
+static sdp_version_t sdpVnumArray[1] = {
+ { 1, 0 }
+};
+static const int sdpServerVnumEntries = 1;
+
+/*
+ * The service database state is an attribute of the service record
+ * of the SDP server itself. This attribute is guaranteed to
+ * change if any of the contents of the service repository
+ * changes. This function updates the timestamp of value of
+ * the svcDBState attribute
+ * Set the SDP server DB. Simply a timestamp which is the marker
+ * when the DB was modified.
+ */
+static void update_db_timestamp(void)
+{
+ uint32_t dbts = sdp_get_time();
+ sdp_data_t *d = sdp_data_alloc(SDP_UINT32, &dbts);
+ sdp_attr_replace(server, SDP_ATTR_SVCDB_STATE, d);
+}
+
+static void add_lang_attr(sdp_record_t *r)
+{
+ sdp_lang_attr_t base_lang;
+ sdp_list_t *langs = 0;
+
+ base_lang.code_ISO639 = (0x65 << 8) | 0x6e;
+ // UTF-8 MIBenum (http://www.iana.org/assignments/character-sets)
+ base_lang.encoding = 106;
+ base_lang.base_offset = SDP_PRIMARY_LANG_BASE;
+ langs = sdp_list_append(0, &base_lang);
+ sdp_set_lang_attr(r, langs);
+ sdp_list_free(langs, 0);
+}
+
+void register_public_browse_group(int public)
+{
+ sdp_list_t *browselist;
+ uuid_t bgscid, pbgid;
+ sdp_data_t *sdpdata;
+ sdp_record_t *browse = sdp_record_alloc();
+
+ if (public) {
+ browse->handle = sdp_next_handle();
+ if (browse->handle < 0x10000)
+ return;
+ } else
+ browse->handle = SDP_SERVER_RECORD_HANDLE + 1;
+
+ sdp_record_add(BDADDR_ANY, browse);
+ sdpdata = sdp_data_alloc(SDP_UINT32, &browse->handle);
+ sdp_attr_add(browse, SDP_ATTR_RECORD_HANDLE, sdpdata);
+
+ add_lang_attr(browse);
+ sdp_set_info_attr(browse, "Public Browse Group Root", "BlueZ", "Root of public browse hierarchy");
+
+ sdp_uuid16_create(&bgscid, BROWSE_GRP_DESC_SVCLASS_ID);
+ browselist = sdp_list_append(0, &bgscid);
+ sdp_set_service_classes(browse, browselist);
+ sdp_list_free(browselist, 0);
+
+ if (public) {
+ sdp_uuid16_create(&pbgid, PUBLIC_BROWSE_GROUP);
+ sdp_set_group_id(browse, pbgid);
+ }
+}
+
+/*
+ * The SDP server must present its own service record to
+ * the service repository. This can be accessed by service
+ * discovery clients. This method constructs a service record
+ * and stores it in the repository
+ */
+void register_server_service(int public)
+{
+ int i;
+ sdp_list_t *classIDList, *browseList;
+ sdp_list_t *access_proto = 0;
+ uuid_t l2cap, classID, browseGroupId, sdpSrvUUID;
+ void **versions, **versionDTDs;
+ uint8_t dtd;
+ uint16_t version, port;
+ sdp_data_t *pData, *port_data, *version_data;
+ sdp_list_t *pd, *seq;
+
+ server = sdp_record_alloc();
+ server->pattern = NULL;
+
+ /* Force the record to be SDP_SERVER_RECORD_HANDLE */
+ server->handle = SDP_SERVER_RECORD_HANDLE;
+
+ sdp_record_add(BDADDR_ANY, server);
+ sdp_attr_add(server, SDP_ATTR_RECORD_HANDLE, sdp_data_alloc(SDP_UINT32, &server->handle));
+
+ /*
+ * Add all attributes to service record. (No need to commit since we
+ * are the server and this record is already in the database.)
+ */
+ add_lang_attr(server);
+ sdp_set_info_attr(server, "SDP Server", "BlueZ", "Bluetooth service discovery server");
+
+ sdp_uuid16_create(&classID, SDP_SERVER_SVCLASS_ID);
+ classIDList = sdp_list_append(0, &classID);
+ sdp_set_service_classes(server, classIDList);
+ sdp_list_free(classIDList, 0);
+
+ /*
+ * Set the version numbers supported, these are passed as arguments
+ * to the server on command line. Now defaults to 1.0
+ * Build the version number sequence first
+ */
+ versions = (void **)malloc(sdpServerVnumEntries * sizeof(void *));
+ versionDTDs = (void **)malloc(sdpServerVnumEntries * sizeof(void *));
+ dtd = SDP_UINT16;
+ for (i = 0; i < sdpServerVnumEntries; i++) {
+ uint16_t *version = malloc(sizeof(uint16_t));
+ *version = sdpVnumArray[i].major;
+ *version = (*version << 8);
+ *version |= sdpVnumArray[i].minor;
+ versions[i] = version;
+ versionDTDs[i] = &dtd;
+ }
+ pData = sdp_seq_alloc(versionDTDs, versions, sdpServerVnumEntries);
+ for (i = 0; i < sdpServerVnumEntries; i++)
+ free(versions[i]);
+ free(versions);
+ free(versionDTDs);
+ sdp_attr_add(server, SDP_ATTR_VERSION_NUM_LIST, pData);
+
+ sdp_uuid16_create(&sdpSrvUUID, SDP_UUID);
+ sdp_set_service_id(server, sdpSrvUUID);
+
+ sdp_uuid16_create(&l2cap, L2CAP_UUID);
+ pd = sdp_list_append(0, &l2cap);
+ port = SDP_PSM;
+ port_data = sdp_data_alloc(SDP_UINT16, &port);
+ pd = sdp_list_append(pd, port_data);
+ version = 1;
+ version_data = sdp_data_alloc(SDP_UINT16, &version);
+ pd = sdp_list_append(pd, version_data);
+ seq = sdp_list_append(0, pd);
+
+ access_proto = sdp_list_append(0, seq);
+ sdp_set_access_protos(server, access_proto);
+ sdp_list_free(access_proto, free);
+ sdp_data_free(port_data);
+ sdp_data_free(version_data);
+ sdp_list_free(pd, 0);
+
+ if (public) {
+ sdp_uuid16_create(&browseGroupId, PUBLIC_BROWSE_GROUP);
+ browseList = sdp_list_append(0, &browseGroupId);
+ sdp_set_browse_groups(server, browseList);
+ sdp_list_free(browseList, 0);
+ }
+
+ update_db_timestamp();
+}
// FIXME: refactor for server-side
static sdp_record_t *extract_pdu_server(bdaddr_t *device, uint8_t *p, uint32_t handleExpected, int *scanned)