From 542fc57346fbb329934152f446f58cc61a5d5d00 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Wed, 31 Mar 2004 16:37:55 +0000 Subject: Add SDP library code --- src/sdp.c | 3076 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 3076 insertions(+) create mode 100644 src/sdp.c (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c new file mode 100644 index 00000000..4ffb0d93 --- /dev/null +++ b/src/sdp.c @@ -0,0 +1,3076 @@ +/* + BlueZ - Bluetooth protocol stack for Linux + Copyright (C) 2000-2001 Qualcomm Incorporated + + Written 2000,2001 by Maxim Krasnyansky + + Based on an original SDP implementation by Nokia Corporation. + Copyright (C) 2001,2002 Nokia Corporation. + Original author Guruprasad Krishnamurthy + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License version 2 as + published by the Free Software Foundation; + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. + IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY + CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + + ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, + COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS + SOFTWARE IS DISCLAIMED. +*/ + +/* + * $Id$ + */ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#define BASE_UUID "00000000-0000-1000-8000-00805F9B34FB" + +static uint128_t *bluetooth_base_uuid = NULL; + +/* Message structure. */ +struct tupla { + int index; + char *str; +}; + +static struct tupla Protocol[] = { + { SDP_UUID, "SDP" }, + { RFCOMM_UUID, "RFCOMM" }, + { TCS_BIN_UUID, "TCS-BIN" }, + { L2CAP_UUID, "L2CAP" }, + { IP_UUID, "IP" }, + { UDP_UUID, "UDP" }, + { TCP_UUID, "TCP" }, + { TCS_AT_UUID, "TCS-AT" }, + { OBEX_UUID, "OBEX" }, + { FTP_UUID, "FTP" }, + { HTTP_UUID, "HTTP" }, + { WSP_UUID, "WSP" }, + { BNEP_UUID, "BNEP" }, + { 0 } +}; + +static struct tupla ServiceClass[] = { + { SDP_SERVER_SVCLASS_ID, "SDP Server" }, + { BROWSE_GRP_DESC_SVCLASS_ID, "Browse Group Descriptor" }, + { PUBLIC_BROWSE_GROUP, "Public Browse Group" }, + { SERIAL_PORT_SVCLASS_ID, "Serial Port" }, + { LAN_ACCESS_SVCLASS_ID, "LAN Access Using PPP" }, + { DIALUP_NET_SVCLASS_ID, "Dialup Networking" }, + { IRMC_SYNC_SVCLASS_ID, "IrMCSync" }, + { OBEX_OBJPUSH_SVCLASS_ID, "OBEX Object Push" }, + { OBEX_FILETRANS_SVCLASS_ID, "OBEX File Transfer" }, + { IRMC_SYNC_CMD_SVCLASS_ID, "IrMCSync Command" }, + { HEADSET_SVCLASS_ID, "Headset" }, + { CORDLESS_TELEPHONY_SVCLASS_ID,"Cordless Telephony" }, + { INTERCOM_SVCLASS_ID, "Intercom" }, + { FAX_SVCLASS_ID, "Fax" }, + { HEADSET_AGW_SVCLASS_ID, "Headset Audio Gateway" }, + { PNP_INFO_SVCLASS_ID, "PnP Information" }, + { GENERIC_NETWORKING_SVCLASS_ID,"Generic Networking" }, + { GENERIC_FILETRANS_SVCLASS_ID, "Generic File Transfer" }, + { GENERIC_AUDIO_SVCLASS_ID, "Generic Audio" }, + { GENERIC_TELEPHONY_SVCLASS_ID, "Generic Telephony" }, + { PANU_SVCLASS_ID, "PAN user" }, + { NAP_SVCLASS_ID, "Network access point" }, + { GN_SVCLASS_ID, "PAN group network" }, + { 0 } +}; + +static struct tupla Profile[] = { + { SERIAL_PORT_PROFILE_ID, "Serial Port" }, + { LAN_ACCESS_PROFILE_ID, "LAN Access Using PPP" }, + { DIALUP_NET_PROFILE_ID, "Dialup Networking" }, + { IRMC_SYNC_PROFILE_ID, "IrMCSync" }, + { OBEX_OBJPUSH_PROFILE_ID, "OBEX Object Push" }, + { OBEX_FILETRANS_PROFILE_ID, "OBEX File Transfer" }, + { IRMC_SYNC_CMD_PROFILE_ID, "IrMCSync Command" }, + { HEADSET_PROFILE_ID, "Headset" }, + { CORDLESS_TELEPHONY_PROFILE_ID, "Cordless Telephony" }, + { INTERCOM_PROFILE_ID, "Intercom" }, + { FAX_PROFILE_ID, "Fax" }, + { HEADSET_AGW_PROFILE_ID, "Headset Audio Gateway" }, + { PANU_PROFILE_ID, "PAN user" }, + { NAP_PROFILE_ID, "PAN access point" }, + { GN_PROFILE_ID, "PAN group network" }, + { 0 } +}; + +static char *string_lookup(struct tupla *pt0, int index) +{ + struct tupla *pt; + for (pt = pt0; pt->index; pt++) + if (pt->index == index) + return pt->str; + return ""; +} + +/* + * Prints into a string the Protocol UUID + * coping a maximum of n characters. + */ +static int uuid2str(struct tupla *message, const uuid_t *uuid, char *str, size_t n) +{ + char *str2; + + if (!uuid) { + snprintf(str, n, "NULL"); + return -2; + } + switch (uuid->type) { + case SDP_UUID16: + str2 = string_lookup(message, uuid->value.uuid16); + snprintf(str, n, str2); + break; + case SDP_UUID32: + snprintf(str, n, "Error: This is uuid32"); + return -3; + case SDP_UUID128: + snprintf(str, n, "Error: This is uuid128"); + return -4; + default: + snprintf(str, n, "Type of UUID (%x) unknown.", uuid->type); + return -1; + } + return 0; +} + +int sdp_proto_uuid2strn(const uuid_t *uuid, char *str, size_t n) +{ + return uuid2str(Protocol, uuid, str, n); +} + +int sdp_svclass_uuid2strn(const uuid_t *uuid, char *str, size_t n) +{ + return uuid2str(ServiceClass, uuid, str, n); +} + +int sdp_profile_uuid2strn(const uuid_t *uuid, char *str, size_t n) +{ + return uuid2str(Profile, uuid, str, n); +} + +/* + * convert the UUID to string, copying a maximum of n characters. + */ +int sdp_uuid2strn(const uuid_t *uuid, char *str, size_t n) +{ + if (!uuid) { + snprintf(str, n, "NULL"); + return -2; + } + switch (uuid->type) { + case SDP_UUID16: + snprintf(str, n, "%.4x", uuid->value.uuid16); + break; + case SDP_UUID32: + snprintf(str, n, "%.8x", uuid->value.uuid32); + break; + case SDP_UUID128:{ + unsigned int data0; + unsigned short data1; + unsigned short data2; + unsigned short data3; + unsigned int data4; + unsigned short data5; + + memcpy(&data0, &uuid->value.uuid128.data[0], 4); + memcpy(&data1, &uuid->value.uuid128.data[4], 2); + memcpy(&data2, &uuid->value.uuid128.data[6], 2); + memcpy(&data3, &uuid->value.uuid128.data[8], 2); + memcpy(&data4, &uuid->value.uuid128.data[10], 4); + memcpy(&data5, &uuid->value.uuid128.data[14], 2); + + snprintf(str, n, "%.8x-%.4x-%.4x-%.4x-%.8x%.4x", + ntohl(data0), ntohs(data1), + ntohs(data2), ntohs(data3), + ntohl(data4), ntohs(data5)); + } + break; + default: + snprintf(str, n, "Type of UUID (%x) unknown.", uuid->type); + return -1; // Enum type of UUID not set + } + return 0; +} + +#ifdef SDP_DEBUG +/* + * Function prints the UUID in hex as per defined syntax - + * + * 4bytes-2bytes-2bytes-2bytes-6bytes + * + * There is some ugly code, including hardcoding, but + * that is just the way it is converting 16 and 32 bit + * UUIDs to 128 bit as defined in the SDP doc + */ +void sdp_uuid_print(const uuid_t *uuid) +{ + if (uuid == NULL) { + SDPERR("Null passed to print UUID\n"); + return; + } + if (uuid->type == SDP_UUID16) { + SDPDBG(" uint16_t : 0x%.4x\n", uuid->value.uuid16); + } else if (uuid->type == SDP_UUID32) { + SDPDBG(" uint32_t : 0x%.8x\n", uuid->value.uuid32); + } else if (uuid->type == SDP_UUID128) { + unsigned int data0; + unsigned short data1; + unsigned short data2; + unsigned short data3; + unsigned int data4; + unsigned short data5; + + memcpy(&data0, &uuid->value.uuid128.data[0], 4); + memcpy(&data1, &uuid->value.uuid128.data[4], 2); + memcpy(&data2, &uuid->value.uuid128.data[6], 2); + memcpy(&data3, &uuid->value.uuid128.data[8], 2); + memcpy(&data4, &uuid->value.uuid128.data[10], 4); + memcpy(&data5, &uuid->value.uuid128.data[14], 2); + + SDPDBG(" uint128_t : 0x%.8x-", ntohl(data0)); + SDPDBG("%.4x-", ntohs(data1)); + SDPDBG("%.4x-", ntohs(data2)); + SDPDBG("%.4x-", ntohs(data3)); + SDPDBG("%.8x", ntohl(data4)); + SDPDBG("%.4x\n", ntohs(data5)); + } else + SDPERR("Enum type of UUID not set\n"); +} +#endif + +sdp_data_t *sdp_data_alloc(uint8_t dtd, const void *value) +{ + sdp_data_t *seq; + int len = 0; + sdp_data_t *d = (sdp_data_t *)malloc(sizeof(sdp_data_t)); + + if (!d) + return NULL; + + memset(d, 0, sizeof(sdp_data_t)); + d->dtd = dtd; + d->unitSize = sizeof(uint8_t); + + switch (dtd) { + case SDP_DATA_NIL: + break; + case SDP_UINT8: + d->val.uint8 = *(uint8_t *)value; + d->unitSize += sizeof(uint8_t); + break; + case SDP_INT8: + case SDP_BOOL: + d->val.int8 = *(int8_t *)value; + d->unitSize += sizeof(int8_t); + break; + case SDP_UINT16: + d->val.uint16 = sdp_get_unaligned((uint16_t *)value); + d->unitSize += sizeof(uint16_t); + break; + case SDP_INT16: + d->val.int16 = sdp_get_unaligned((int16_t *)value); + d->unitSize += sizeof(int16_t); + break; + case SDP_UINT32: + d->val.uint32 = sdp_get_unaligned((uint32_t *)value); + d->unitSize += sizeof(uint32_t); + break; + case SDP_INT32: + d->val.int32 = sdp_get_unaligned((int32_t *)value); + d->unitSize += sizeof(int32_t); + break; + case SDP_INT64: + d->val.int64 = sdp_get_unaligned((int64_t *)value); + d->unitSize += sizeof(int64_t); + break; + case SDP_UINT64: + d->val.uint64 = sdp_get_unaligned((uint64_t *)value); + d->unitSize += sizeof(uint64_t); + break; + case SDP_UINT128: + memcpy(&d->val.uint128.data, value, sizeof(uint128_t)); + d->unitSize += sizeof(uint128_t); + break; + case SDP_INT128: + memcpy(&d->val.int128.data, value, sizeof(uint128_t)); + d->unitSize += sizeof(uint128_t); + break; + case SDP_UUID16: + sdp_uuid16_create(&d->val.uuid, sdp_get_unaligned((uint16_t *)value)); + d->unitSize += sizeof(uint16_t); + break; + case SDP_UUID32: + sdp_uuid32_create(&d->val.uuid, sdp_get_unaligned((uint32_t *)value)); + d->unitSize += sizeof(uint32_t); + break; + case SDP_UUID128: + sdp_uuid128_create(&d->val.uuid, value); + d->unitSize += sizeof(uint128_t); + break; + case SDP_URL_STR8: + case SDP_TEXT_STR8: + case SDP_URL_STR16: + case SDP_TEXT_STR16: + if (!value) + goto out_error; + + len = strlen(value); + d->unitSize += len; + if (len <= USHRT_MAX) { + d->val.str = (char *)malloc(len + 1); + if (!d->val.str) + goto out_error; + + strcpy(d->val.str, value); + if (len <= UCHAR_MAX) { + d->unitSize += sizeof(uint8_t); + if (dtd != SDP_URL_STR8 && dtd != SDP_TEXT_STR8) { + if (dtd == SDP_URL_STR16) + dtd = SDP_URL_STR8; + else + dtd = SDP_TEXT_STR8; + } + } else { + d->unitSize += sizeof(uint16_t); + if (dtd == SDP_TEXT_STR8) + dtd = SDP_TEXT_STR16; + else + dtd = SDP_URL_STR16; + } + } else { + SDPERR("Strings of size > USHRT_MAX not supported\n"); + goto out_error; + } + break; + case SDP_URL_STR32: + case SDP_TEXT_STR32: + SDPERR("Strings of size > USHRT_MAX not supported\n"); + break; + case SDP_ALT8: + case SDP_ALT16: + case SDP_ALT32: + case SDP_SEQ8: + case SDP_SEQ16: + case SDP_SEQ32: + if (dtd == SDP_ALT8 || dtd == SDP_SEQ8) + d->unitSize += sizeof(uint8_t); + else if (dtd == SDP_ALT16 || dtd == SDP_SEQ16) + d->unitSize += sizeof(uint16_t); + else if (dtd == SDP_ALT32 || dtd == SDP_SEQ32) + d->unitSize += sizeof(uint32_t); + seq = (sdp_data_t *)value; + d->val.dataseq = seq; + for (; seq; seq = seq->next) + d->unitSize += seq->unitSize; + break; + default: + goto out_error; + } + return d; + +out_error: + free(d); + return NULL; +} + +sdp_data_t *sdp_seq_append(sdp_data_t *seq, sdp_data_t *d) +{ + if (seq) { + sdp_data_t *p; + for (p = seq; p->next; p = p->next); + p->next = d; + } else + seq = d; + d->next = NULL; + return seq; +} + +sdp_data_t *sdp_seq_alloc(void **dtds, void **values, int len) +{ + sdp_data_t *curr = NULL, *seq = NULL; + int i; + + for (i = 0; i < len; i++) { + sdp_data_t *data; + uint8_t dtd = *(uint8_t *)dtds[i]; + if (dtd >= SDP_SEQ8 && dtd <= SDP_ALT32) + data = (sdp_data_t *)values[i]; + else + data = sdp_data_alloc(dtd, values[i]); + if (!data) + return NULL; + if (curr) + curr->next = data; + else + seq = data; + curr = data; + } + return sdp_data_alloc(SDP_SEQ8, seq); +} + +int sdp_attr_add(sdp_record_t *rec, uint16_t attr, sdp_data_t *d) +{ + sdp_data_t *p = sdp_data_get(rec, attr); + + if (p) + return -1; + d->attrId = attr; + rec->attrlist = sdp_list_insert_sorted(rec->attrlist, d, sdp_attrid_comp_func); + return 0; +} + +void sdp_attr_remove(sdp_record_t *rec, uint16_t attr) +{ + sdp_data_t *d = sdp_data_get(rec, attr); + if (d) + rec->attrlist = sdp_list_remove(rec->attrlist, d); +} + +void sdp_set_seq_len(char *ptr, int length) +{ + uint8_t dtd = *(uint8_t *)ptr++; + + switch (dtd) { + case SDP_SEQ8: + case SDP_ALT8: + case SDP_TEXT_STR8: + case SDP_URL_STR8: + *(uint8_t *)ptr = (uint8_t)length; + break; + case SDP_SEQ16: + case SDP_ALT16: + case SDP_TEXT_STR16: + case SDP_URL_STR16: + sdp_put_unaligned(htons(length), (uint16_t *)ptr); + break; + case SDP_SEQ32: + case SDP_ALT32: + case SDP_TEXT_STR32: + case SDP_URL_STR32: + sdp_put_unaligned(htons(length), (uint32_t *)ptr); + break; + } +} + +int sdp_set_data_type(sdp_buf_t *buf, uint8_t dtd) +{ + int orig = buf->data_size; + uint8_t *p = buf->data + buf->data_size; + + *p++ = dtd; + buf->data_size += sizeof(uint8_t); + switch (dtd) { + case SDP_SEQ8: + case SDP_TEXT_STR8: + case SDP_URL_STR8: + case SDP_ALT8: + buf->data_size += sizeof(uint8_t); + break; + case SDP_SEQ16: + case SDP_TEXT_STR16: + case SDP_URL_STR16: + case SDP_ALT16: + buf->data_size += sizeof(uint16_t); + break; + case SDP_SEQ32: + case SDP_TEXT_STR32: + case SDP_URL_STR32: + case SDP_ALT32: + buf->data_size += sizeof(uint32_t); + break; + } + return buf->data_size - orig; +} + +void sdp_set_attrid(sdp_buf_t *buf, uint16_t attr) +{ + uint8_t *p = buf->data; + + // data type for attr + *p++ = SDP_UINT16; + buf->data_size = sizeof(uint8_t); + sdp_put_unaligned(htons(attr), (uint16_t *)p); + p += sizeof(uint16_t); + buf->data_size += sizeof(uint16_t); +} + +static int get_data_size(sdp_buf_t *buf, sdp_data_t *sdpdata) +{ + sdp_data_t *d; + int n = 0; + + for (d = sdpdata->val.dataseq; d; d = d->next) + n += sdp_gen_pdu(buf, d); + return n; +} + +int sdp_gen_pdu(sdp_buf_t *buf, sdp_data_t *d) +{ + int pdu_size = 0, data_size = 0; + unsigned char *src = NULL, is_seq = 0, is_alt = 0; + uint8_t dtd = d->dtd; + uint16_t u16; + uint32_t u32; + uint64_t u64; + uint128_t u128; + char *seqp = buf->data + buf->data_size; + + pdu_size = sdp_set_data_type(buf, dtd); + switch (dtd) { + case SDP_DATA_NIL: + break; + case SDP_UINT8: + src = &d->val.uint8; + data_size = sizeof(uint8_t); + break; + case SDP_UINT16: + u16 = htons(d->val.uint16); + src = (unsigned char *)&u16; + data_size = sizeof(uint16_t); + break; + case SDP_UINT32: + u32 = htonl(d->val.uint32); + src = (unsigned char *)&u32; + data_size = sizeof(uint32_t); + break; + case SDP_UINT64: + u64 = hton64(d->val.uint64); + src = (unsigned char *)&u64; + data_size = sizeof(uint64_t); + break; + case SDP_UINT128: + hton128(&d->val.uint128, &u128); + src = (unsigned char *)&u128; + data_size = sizeof(uint128_t); + break; + case SDP_INT8: + case SDP_BOOL: + src = (unsigned char *)&d->val.int8; + data_size = sizeof(int8_t); + break; + case SDP_INT16: + u16 = htons(d->val.int16); + src = (unsigned char *)&u16; + data_size = sizeof(int16_t); + break; + case SDP_INT32: + u32 = htonl(d->val.int32); + src = (unsigned char *)&u32; + data_size = sizeof(int32_t); + break; + case SDP_INT64: + u64 = hton64(d->val.int64); + src = (unsigned char *)&u64; + data_size = sizeof(int64_t); + break; + case SDP_INT128: + hton128(&d->val.int128, &u128); + src = (unsigned char *)&u128; + data_size = sizeof(uint128_t); + break; + case SDP_TEXT_STR8: + case SDP_URL_STR8: + case SDP_TEXT_STR16: + case SDP_TEXT_STR32: + case SDP_URL_STR16: + case SDP_URL_STR32: + src = (unsigned char *)d->val.str; + data_size = strlen(d->val.str); + sdp_set_seq_len(seqp, data_size); + break; + case SDP_SEQ8: + case SDP_SEQ16: + case SDP_SEQ32: + is_seq = 1; + data_size = get_data_size(buf, d); + sdp_set_seq_len(seqp, data_size); + break; + case SDP_ALT8: + case SDP_ALT16: + case SDP_ALT32: + is_alt = 1; + data_size = get_data_size(buf, d); + sdp_set_seq_len(seqp, data_size); + break; + case SDP_UUID16: + u16 = htons(d->val.uuid.value.uuid16); + src = (unsigned char *)&u16; + data_size = sizeof(uint16_t); + break; + case SDP_UUID32: + u32 = htonl(d->val.uuid.value.uuid32); + src = (unsigned char *)&u32; + data_size = sizeof(uint32_t); + break; + case SDP_UUID128: + src = (unsigned char *)&d->val.uuid.value.uuid128; + data_size = sizeof(uint128_t); + break; + default: + break; + } + if (!is_seq && !is_alt) { + if (src && buf) { + memcpy(buf->data + buf->data_size, src, data_size); + buf->data_size += data_size; + } else if (dtd != SDP_DATA_NIL) + SDPDBG("Gen PDU : Cant copy from NULL source or dest\n"); + } + pdu_size += data_size; + return pdu_size; +} + +static void sdp_attr_pdu(void *value, void *udata) +{ + sdp_append_to_pdu((sdp_buf_t *)udata, (sdp_data_t *)value); +} + +int sdp_gen_record_pdu(const sdp_record_t *rec, sdp_buf_t *buf) +{ + buf->data = (char *)malloc(SDP_PDU_CHUNK_SIZE); + if (buf->data) { + buf->buf_size = SDP_PDU_CHUNK_SIZE; + buf->data_size = 0; + memset(buf->data, 0, buf->buf_size); + sdp_list_foreach(rec->attrlist, sdp_attr_pdu, buf); + return 0; + } + return -1; +} + +void sdp_attr_replace(sdp_record_t *rec, uint16_t attr, sdp_data_t *d) +{ + sdp_data_t *p = sdp_data_get(rec, attr); + + if (p) { + rec->attrlist = sdp_list_remove(rec->attrlist, p); + sdp_data_free(p); + } + d->attrId = attr; + rec->attrlist = sdp_list_insert_sorted(rec->attrlist, (void *)d, sdp_attrid_comp_func); +} + +int sdp_attrid_comp_func(const void *key1, const void *key2) +{ + const sdp_data_t *d1 = (const sdp_data_t *)key1; + const sdp_data_t *d2 = (const sdp_data_t *)key2; + + if (d1 && d2) + return d1->attrId - d2->attrId; + return 0; +} + +static void data_seq_free(sdp_data_t *seq) +{ + sdp_data_t *d = seq->val.dataseq; + + while (d) { + sdp_data_t *next = d->next; + sdp_data_free(d); + d = next; + } +} + +void sdp_data_free(sdp_data_t *d) +{ + switch (d->dtd) { + case SDP_SEQ8: + case SDP_SEQ16: + case SDP_SEQ32: + data_seq_free(d); + break; + case SDP_URL_STR8: + case SDP_URL_STR16: + case SDP_URL_STR32: + case SDP_TEXT_STR8: + case SDP_TEXT_STR16: + case SDP_TEXT_STR32: + free(d->val.str); + break; + } + free(d); +} + +static sdp_data_t *extract_int(const void *p, int *len) +{ + sdp_data_t *d = (sdp_data_t *)malloc(sizeof(sdp_data_t)); + + SDPDBG("Extracting integer\n"); + memset(d, 0, sizeof(sdp_data_t)); + d->dtd = *(uint8_t *)p; + p += sizeof(uint8_t); + *len += sizeof(uint8_t); + + switch (d->dtd) { + case SDP_DATA_NIL: + break; + case SDP_BOOL: + case SDP_INT8: + case SDP_UINT8: + *len += sizeof(uint8_t); + d->val.uint8 = *(uint8_t *)p; + break; + case SDP_INT16: + case SDP_UINT16: + *len += sizeof(uint16_t); + d->val.uint16 = ntohs(sdp_get_unaligned((uint16_t *)p)); + break; + case SDP_INT32: + case SDP_UINT32: + *len += sizeof(uint32_t); + d->val.uint32 = ntohl(sdp_get_unaligned((uint32_t *)p)); + break; + case SDP_INT64: + case SDP_UINT64: + *len += sizeof(uint64_t); + d->val.uint64 = ntoh64(sdp_get_unaligned((uint64_t *)p)); + break; + case SDP_INT128: + case SDP_UINT128: + *len += sizeof(uint128_t); + ntoh128((uint128_t *)p, &d->val.uint128); + break; + default: + free(d); + d = NULL; + } + return d; +} + +static sdp_data_t *extract_uuid(const void *p, int *len, sdp_record_t *rec) +{ + sdp_data_t *d = (sdp_data_t *)malloc(sizeof(sdp_data_t)); + + SDPDBG("Extracting UUID"); + memset(d, 0, sizeof(sdp_data_t)); + if (0 > sdp_uuid_extract(p, &d->val.uuid, len)) { + free(d); + return NULL; + } + d->dtd = *(uint8_t *)p; + sdp_pattern_add_uuid(rec, &d->val.uuid); + return d; +} + +/* + * Extract strings from the PDU (could be service description and similar info) + */ +static sdp_data_t *extract_str(const void *p, int *len) +{ + char *s; + int n; + sdp_data_t *d = (sdp_data_t *)malloc(sizeof(sdp_data_t)); + + memset(d, 0, sizeof(sdp_data_t)); + d->dtd = *(uint8_t *)p; + p += sizeof(uint8_t); + *len += sizeof(uint8_t); + + switch (d->dtd) { + case SDP_TEXT_STR8: + case SDP_URL_STR8: + n = *(uint8_t *)p; + p += sizeof(uint8_t); + *len += sizeof(uint8_t) + n; + break; + case SDP_TEXT_STR16: + case SDP_URL_STR16: + n = ntohs(sdp_get_unaligned((uint16_t *)p)); + p += sizeof(uint16_t); + *len += sizeof(uint16_t) + n; + break; + default: + SDPERR("Sizeof text string > UINT16_MAX\n"); + free(d); + return 0; + } + + s = (char *)malloc(n + 1); + memset(s, 0, n + 1); + strncpy(s, p, n); + + SDPDBG("Len : %d\n", n); + SDPDBG("Str : %s\n", s); + + d->val.str = s; + return d; +} + +static sdp_data_t *extract_seq(const void *p, int *len, sdp_record_t *rec) +{ + int seqlen, n = 0; + sdp_data_t *curr, *prev; + sdp_data_t *d = (sdp_data_t *)malloc(sizeof(sdp_data_t)); + + SDPDBG("Extracting SEQ"); + memset(d, 0, sizeof(sdp_data_t)); + *len = sdp_extract_seqtype(p, &d->dtd, &seqlen); + SDPDBG("Sequence Type : 0x%x length : 0x%x\n", d->dtd, seqlen); + + if (*len == 0) + return d; + + p += *len; + curr = prev = NULL; + while (n < seqlen) { + int attrlen = 0; + curr = sdp_extract_attr(p, &attrlen, rec); + if (curr == NULL) + break; + + if (prev) + prev->next = curr; + else + d->val.dataseq = curr; + prev = curr; + p += attrlen; + n += attrlen; + + SDPDBG("Extracted: %d SequenceLength: %d", n, seqlen); + } + + *len += n; + return d; +} + +sdp_data_t *sdp_extract_attr(const char *p, int *size, sdp_record_t *rec) +{ + sdp_data_t *elem; + int n = 0; + uint8_t dtd = *(const uint8_t *)p; + + SDPDBG("extract_attr: dtd=0x%x", dtd); + switch (dtd) { + case SDP_DATA_NIL: + case SDP_BOOL: + case SDP_UINT8: + case SDP_UINT16: + case SDP_UINT32: + case SDP_UINT64: + case SDP_UINT128: + case SDP_INT8: + case SDP_INT16: + case SDP_INT32: + case SDP_INT64: + case SDP_INT128: + elem = extract_int(p, &n); + break; + case SDP_UUID16: + case SDP_UUID32: + case SDP_UUID128: + elem = extract_uuid(p, &n, rec); + break; + case SDP_TEXT_STR8: + case SDP_TEXT_STR16: + case SDP_TEXT_STR32: + case SDP_URL_STR8: + case SDP_URL_STR16: + case SDP_URL_STR32: + elem = extract_str(p, &n); + break; + case SDP_SEQ8: + case SDP_SEQ16: + case SDP_SEQ32: + case SDP_ALT8: + case SDP_ALT16: + case SDP_ALT32: + elem = extract_seq(p, &n, rec); + break; + default: + SDPERR("Unknown data descriptor : 0x%x terminating\n", dtd); + return NULL; + } + *size += n; + return elem; +} + +#ifdef SDP_DEBUG +static void attr_print_func(void *value, void *userData) +{ + sdp_data_t *d = (sdp_data_t *)value; + + SDPDBG("=====================================\n"); + SDPDBG("ATTRIBUTE IDENTIFIER : 0x%x\n", d->attrId); + SDPDBG("ATTRIBUTE VALUE PTR : 0x%x\n", (uint32_t)value); + if (d) + sdp_data_print(d); + else + SDPDBG("NULL value\n"); + SDPDBG("=====================================\n"); +} + +void sdp_print_service_attr(sdp_list_t *svcAttrList) +{ + sdp_list_foreach(svcAttrList, attr_print_func, NULL); +} +#endif + +sdp_record_t *sdp_extract_pdu(const char *buf, int *scanned) +{ + int extracted = 0, seqlen = 0; + uint8_t dtd; + uint16_t attr; + sdp_record_t *rec = sdp_record_alloc(); + const char *p = buf; + + *scanned = sdp_extract_seqtype(buf, &dtd, &seqlen); + p += *scanned; + rec->attrlist = NULL; + while (extracted < seqlen) { + int n = sizeof(uint8_t), attrlen = 0; + sdp_data_t *data = NULL; + + SDPDBG("Extract PDU, sequenceLength: %d localExtractedLength: %d", seqlen, extracted); + dtd = *(uint8_t *)p; + attr = ntohs(sdp_get_unaligned((uint16_t *)(p+n))); + n += sizeof(uint16_t); + + SDPDBG("DTD of attrId : %d Attr id : 0x%x \n", dtd, attr); + + data = sdp_extract_attr(p+n, &attrlen, rec); + + SDPDBG("Attr id : 0x%x attrValueLength : %d\n", attr, attrlen); + + n += attrlen; + if (data == NULL) { + SDPDBG("Terminating extraction of attributes"); + break; + } + if (attr == SDP_ATTR_RECORD_HANDLE) + rec->handle = data->val.uint32; + extracted += n; + p += n; + sdp_attr_replace(rec, attr, data); + SDPDBG("Extract PDU, seqLength: %d localExtractedLength: %d", + seqlen, extracted); + } +#ifdef SDP_DEBUG + SDPDBG("Successful extracting of Svc Rec attributes\n"); + sdp_print_service_attr(rec->attrlist); +#endif + *scanned += seqlen; + return rec; +} + +#ifdef SDP_DEBUG +static void print_dataseq(sdp_data_t *p) +{ + sdp_data_t *d; + + for (d = p; d; d = d->next) + sdp_data_print(d); +} +#endif + +void sdp_record_print(const sdp_record_t *rec) +{ + sdp_data_t *d = sdp_data_get(rec, SDP_ATTR_SVCNAME_PRIMARY); + if (d) + printf("Service Name: %s\n", d->val.str); + d = sdp_data_get(rec, SDP_ATTR_SVCDESC_PRIMARY); + if (d) + printf("Service Description: %s\n", d->val.str); + d = sdp_data_get(rec, SDP_ATTR_PROVNAME_PRIMARY); + if (d) + printf("Service Provider: %s\n", d->val.str); +} + +#ifdef SDP_DEBUG +void sdp_data_print(sdp_data_t *d) +{ + switch (d->dtd) { + case SDP_DATA_NIL: + SDPDBG("NIL\n"); + break; + case SDP_BOOL: + case SDP_UINT8: + case SDP_UINT16: + case SDP_UINT32: + case SDP_UINT64: + case SDP_UINT128: + case SDP_INT8: + case SDP_INT16: + case SDP_INT32: + case SDP_INT64: + case SDP_INT128: + SDPDBG("Integer : 0x%x\n", d->val.uint32); + break; + case SDP_UUID16: + case SDP_UUID32: + case SDP_UUID128: + SDPDBG("UUID\n"); + sdp_uuid_print(&d->val.uuid); + break; + case SDP_TEXT_STR8: + case SDP_TEXT_STR16: + case SDP_TEXT_STR32: + SDPDBG("Text : %s\n", d->val.str); + break; + case SDP_URL_STR8: + case SDP_URL_STR16: + case SDP_URL_STR32: + SDPDBG("URL : %s\n", d->val.str); + break; + case SDP_SEQ8: + case SDP_SEQ16: + case SDP_SEQ32: + print_dataseq(d->val.dataseq); + break; + case SDP_ALT8: + case SDP_ALT16: + case SDP_ALT32: + SDPDBG("Data Sequence Alternates\n"); + print_dataseq(d->val.dataseq); + break; + } +} +#endif + +sdp_data_t *sdp_data_get(const sdp_record_t *rec, uint16_t attrId) +{ + if (rec->attrlist) { + sdp_data_t sdpTemplate; + sdp_list_t *p; + + sdpTemplate.attrId = attrId; + p = sdp_list_find(rec->attrlist, &sdpTemplate, sdp_attrid_comp_func); + if (p) + return (sdp_data_t *)p->data; + } + return 0; +} + +/* + * Extract the sequence type and its length, and return offset into buf + * or 0 on failure. + */ +int sdp_extract_seqtype(const char *buf, uint8_t *dtdp, int *size) +{ + uint8_t dtd = *(uint8_t *)buf; + int scanned = sizeof(uint8_t); + + buf += sizeof(uint8_t); + *dtdp = dtd; + switch (dtd) { + case SDP_SEQ8: + case SDP_ALT8: + *size = *(uint8_t *)buf; + scanned += sizeof(uint8_t); + break; + case SDP_SEQ16: + case SDP_ALT16: + *size = ntohs(sdp_get_unaligned((uint16_t *)buf)); + scanned += sizeof(uint16_t); + break; + case SDP_SEQ32: + case SDP_ALT32: + *size = ntohl(sdp_get_unaligned((uint32_t *)buf)); + scanned += sizeof(uint32_t); + break; + default: + SDPERR("Unknown sequence type, aborting\n"); + return 0; + } + return scanned; +} + +int sdp_send_req(sdp_session_t *session, char *buf, int size) +{ + int sent = 0; + + while (sent < size) { + int n = send(session->sock, buf + sent, size - sent, 0); + if (n < 0) + return -1; + sent += n; + } + return 0; +} + +int sdp_read_rsp(sdp_session_t *session, char *buf, int size) +{ + fd_set readFds; + struct timeval timeout = { SDP_RESPONSE_TIMEOUT, 0 }; + + FD_SET(session->sock, &readFds); + SDPDBG("Waiting for response\n"); + if (0 == select(session->sock + 1, &readFds, NULL, NULL, &timeout)) { + SDPERR("Client timed out\n"); + errno = ETIMEDOUT; + return -1; + } + return recv(session->sock, buf, size, 0); +} + +/* + * generic send request, wait for response method. + */ +int sdp_send_req_w4_rsp(sdp_session_t *session, char *reqbuf, char *rspbuf, int reqsize, int *rspsize) +{ + int n; + sdp_pdu_hdr_t *reqhdr = (sdp_pdu_hdr_t *)reqbuf; + sdp_pdu_hdr_t *rsphdr = (sdp_pdu_hdr_t *)rspbuf; + + SDPDBG(""); + if (0 > sdp_send_req(session, reqbuf, reqsize)) { + SDPERR("Error sending data:%s", strerror(errno)); + return -1; + } + n = sdp_read_rsp(session, rspbuf, SDP_RSP_BUFFER_SIZE); + if (0 > n) + return -1; + SDPDBG("Read : %d\n", n); + if (n == 0 || reqhdr->tid != rsphdr->tid) { + errno = EPROTO; + return -1; + } + *rspsize = n; + return 0; +} + +/* + * singly-linked lists (after openobex implementation) + */ +sdp_list_t *sdp_list_append(sdp_list_t *p, void *d) +{ + sdp_list_t *q, *n = (sdp_list_t *)malloc(sizeof(sdp_list_t)); + + if (!n) + return 0; + + n->data = d; + n->next = 0; + + if (!p) + return n; + + for (q = p; q->next; q = q->next); + q->next = n; + + return p; +} + +sdp_list_t *sdp_list_remove(sdp_list_t *list, void *d) +{ + sdp_list_t *p, *q; + + for (q = 0, p = list; p; q = p, p = p->next) + if (p->data == d) { + if (q) + q->next = p->next; + else + list = p->next; + free(p); + break; + } + + return list; +} + +sdp_list_t *sdp_list_insert_sorted(sdp_list_t *list, void *d, sdp_comp_func_t f) +{ + sdp_list_t *q, *p, *n; + + n = (sdp_list_t *)malloc(sizeof(sdp_list_t)); + if (!n) + return 0; + n->data = d; + for (q = 0, p = list; p; q = p, p = p->next) + if (f(p->data, d) >= 0) + break; + // insert between q and p; if !q insert at head + if (q) + q->next = n; + else + list = n; + n->next = p; + return list; +} + +/* + * Every element of the list points to things which need + * to be free()'d. This method frees the list's contents + */ +void sdp_list_free(sdp_list_t *list, sdp_free_func_t f) +{ + sdp_list_t *next; + while (list) { + next = list->next; + if (f) + f(list->data); + free(list); + list = next; + } +} + +static inline int __find_port(sdp_data_t *seq, int proto) +{ + if (!seq || !seq->next) + return 0; + + if (SDP_IS_UUID(seq->dtd) && sdp_uuid_to_proto(&seq->val.uuid) == proto) { + seq = seq->next; + switch (seq->dtd) { + case SDP_UINT8: + return seq->val.uint8; + case SDP_UINT16: + return seq->val.uint16; + } + } + return 0; +} + +int sdp_get_proto_port(const sdp_list_t *list, int proto) +{ + if (proto != L2CAP_UUID && proto != RFCOMM_UUID) { + errno = EINVAL; + return -1; + } + + for (; list; list = list->next) { + sdp_list_t *p; + for (p = list->data; p; p = p->next) { + sdp_data_t *seq = (sdp_data_t *) p->data; + int port = __find_port(seq, proto); + if (port) + return port; + } + } + return 0; +} + +sdp_data_t *sdp_get_proto_desc(sdp_list_t *list, int proto) +{ + for (; list; list = list->next) { + sdp_list_t *p; + for (p = list->data; p; p = p->next) { + sdp_data_t *seq = (sdp_data_t *) p->data; + if (SDP_IS_UUID(seq->dtd) && + sdp_uuid_to_proto(&seq->val.uuid) == proto) + return seq->next; + } + } + return NULL; +} + +int sdp_get_access_protos(const sdp_record_t *rec, sdp_list_t **pap) +{ + sdp_data_t *pdlist, *curr; + sdp_list_t *ap = 0; + + pdlist = sdp_data_get(rec, SDP_ATTR_PROTO_DESC_LIST); + if (pdlist == NULL) { + errno = ENODATA; + return -1; + } + SDPDBG("AP type : 0%x\n", pdlist->dtd); + + for (; pdlist; pdlist = pdlist->next) { + sdp_list_t *pds = 0; + for (curr = pdlist->val.dataseq; curr; curr = curr->next) + pds = sdp_list_append(pds, curr->val.dataseq); + ap = sdp_list_append(ap, pds); + } + *pap = ap; + return 0; +} + +int sdp_get_uuidseq_attr(const sdp_record_t *rec, uint16_t attr, sdp_list_t **seqp) +{ + sdp_data_t *sdpdata = sdp_data_get(rec, attr); + + *seqp = NULL; + if (sdpdata && sdpdata->dtd >= SDP_SEQ8 && sdpdata->dtd <= SDP_SEQ32) { + sdp_data_t *d; + for (d = sdpdata->val.dataseq; d; d = d->next) { + uuid_t *u = (uuid_t *)malloc(sizeof(uuid_t)); + memset((char *)u, 0, sizeof(uuid_t)); + if (d->dtd >= SDP_UUID16 && d->dtd <= SDP_UUID128) { + *u = d->val.uuid; + *seqp = sdp_list_append(*seqp, u); + } else + goto fail; + } + return 0; + } +fail: + sdp_list_free(*seqp, free); + errno = EINVAL; + return -1; +} + +int sdp_set_uuidseq_attr(sdp_record_t *rec, uint16_t aid, sdp_list_t *seq) +{ + int status = 0, i, len; + void **dtds, **values; + uint8_t uuid16 = SDP_UUID16; + uint8_t uuid32 = SDP_UUID32; + uint8_t uuid128 = SDP_UUID128; + sdp_list_t *p; + + len = sdp_list_len(seq); + if (!seq || len == 0) + return -1; + dtds = (void **)malloc(len * sizeof(void *)); + values = (void **)malloc(len * sizeof(void *)); + for (p = seq, i = 0; i < len; i++, p = p->next) { + uuid_t *uuid = (uuid_t *)p->data; + if (uuid) + switch (uuid->type) { + case SDP_UUID16: + dtds[i] = &uuid16; + values[i] = &uuid->value.uuid16; + break; + case SDP_UUID32: + dtds[i] = &uuid32; + values[i] = &uuid->value.uuid32; + break; + case SDP_UUID128: + dtds[i] = &uuid128; + values[i] = &uuid->value.uuid128; + break; + default: + status = -1; + break; + } + else { + status = -1; + break; + } + } + if (status == 0) { + sdp_data_t *data = sdp_seq_alloc(dtds, values, len); + sdp_attr_replace(rec, aid, data); + sdp_pattern_add_uuidseq(rec, seq); + } + free(dtds); + free(values); + return status; +} + +int sdp_get_lang_attr(const sdp_record_t *rec, sdp_list_t **langSeq) +{ + sdp_lang_attr_t *lang; + sdp_data_t *sdpdata, *curr_data; + + *langSeq = NULL; + sdpdata = sdp_data_get(rec, SDP_ATTR_LANG_BASE_ATTR_ID_LIST); + if (sdpdata == NULL) { + errno = ENODATA; + return -1; + } + curr_data = sdpdata->val.dataseq; + while (curr_data) { + sdp_data_t *pCode = curr_data; + sdp_data_t *pEncoding = pCode->next; + sdp_data_t *pOffset = pEncoding->next; + if (pCode && pEncoding && pOffset) { + lang = (sdp_lang_attr_t *)malloc(sizeof(sdp_lang_attr_t)); + lang->code_ISO639 = pCode->val.uint16; + lang->encoding = pEncoding->val.uint16; + lang->base_offset = pOffset->val.uint16; + SDPDBG("code_ISO639 : 0x%02x\n", lang->code_ISO639); + SDPDBG("encoding : 0x%02x\n", lang->encoding); + SDPDBG("base_offfset : 0x%02x\n", lang->base_offset); + *langSeq = sdp_list_append(*langSeq, lang); + } + curr_data = pOffset->next; + } + return 0; +} + +int sdp_get_profile_descs(const sdp_record_t *rec, sdp_list_t **profDescSeq) +{ + sdp_profile_desc_t *profDesc; + sdp_data_t *sdpdata, *seq; + + *profDescSeq = NULL; + sdpdata = sdp_data_get(rec, SDP_ATTR_PFILE_DESC_LIST); + if (!sdpdata || !sdpdata->val.dataseq) { + errno = ENODATA; + return -1; + } + for (seq = sdpdata->val.dataseq; seq && seq->val.dataseq; seq = seq->next) { + sdp_data_t *uuid = seq->val.dataseq; + sdp_data_t *pVnum = seq->val.dataseq->next; + if (uuid && pVnum) { + profDesc = (sdp_profile_desc_t *)malloc(sizeof(sdp_profile_desc_t)); + profDesc->uuid = uuid->val.uuid; + profDesc->version = pVnum->val.uint16; +#ifdef SDP_DEBUG + sdp_uuid_print(&profDesc->uuid); + SDPDBG("Vnum : 0x%04x\n", profDesc->version); +#endif + *profDescSeq = sdp_list_append(*profDescSeq, profDesc); + } + } + return 0; +} + +int sdp_get_server_ver(const sdp_record_t *rec, sdp_list_t **u16) +{ + sdp_data_t *d, *curr; + + *u16 = NULL; + d = sdp_data_get(rec, SDP_ATTR_VERSION_NUM_LIST); + if (d == NULL) { + errno = ENODATA; + return -1; + } + for (curr = d->val.dataseq; curr; curr = curr->next) + *u16 = sdp_list_append(*u16, &curr->val.uint16); + return 0; +} + +/* flexible extraction of basic attributes - Jean II */ +/* How do we expect caller to extract predefined data sequences? */ +int sdp_get_int_attr(const sdp_record_t *rec, uint16_t attrid, int *value) +{ + sdp_data_t *sdpdata = sdp_data_get(rec, attrid); + + if (sdpdata) + /* Verify that it is what the caller expects */ + if (sdpdata->dtd == SDP_BOOL || sdpdata->dtd == SDP_UINT8 || + sdpdata->dtd == SDP_UINT16 || sdpdata->dtd == SDP_UINT32 || + sdpdata->dtd == SDP_INT8 || sdpdata->dtd == SDP_INT16 || + sdpdata->dtd == SDP_INT32) { + *value = sdpdata->val.uint32; + return 0; + } + errno = EINVAL; + return -1; +} + +int sdp_get_string_attr(const sdp_record_t *rec, uint16_t attrid, char *value, int valuelen) +{ + sdp_data_t *sdpdata = sdp_data_get(rec, attrid); + if (sdpdata) + /* Verify that it is what the caller expects */ + if (sdpdata->dtd == SDP_TEXT_STR8 || sdpdata->dtd == SDP_TEXT_STR16 || sdpdata->dtd == SDP_TEXT_STR32) + if (strlen(sdpdata->val.str) < valuelen) { + strcpy(value, sdpdata->val.str); + return 0; + } + errno = EINVAL; + return -1; +} + +#define get_basic_attr(attrID, pAttrValue, fieldName) \ + sdp_data_t *data = sdp_data_get(rec, attrID); \ + if (data) { \ + *pAttrValue = data->val.fieldName; \ + return 0; \ + } \ + errno = EINVAL; \ + return -1; + +int sdp_get_service_id(const sdp_record_t *rec, uuid_t *uuid) +{ + get_basic_attr(SDP_ATTR_SERVICE_ID, uuid, uuid); +} + +int sdp_get_group_id(const sdp_record_t *rec, uuid_t *uuid) +{ + get_basic_attr(SDP_ATTR_GROUP_ID, uuid, uuid); +} + +int sdp_get_record_state(const sdp_record_t *rec, uint32_t *svcRecState) +{ + get_basic_attr(SDP_ATTR_RECORD_STATE, svcRecState, uint32); +} + +int sdp_get_service_avail(const sdp_record_t *rec, uint8_t *svcAvail) +{ + get_basic_attr(SDP_ATTR_SERVICE_AVAILABILITY, svcAvail, uint8); +} + +int sdp_get_service_ttl(const sdp_record_t *rec, uint32_t *svcTTLInfo) +{ + get_basic_attr(SDP_ATTR_SVCINFO_TTL, svcTTLInfo, uint32); +} + +int sdp_get_database_state(const sdp_record_t *rec, uint32_t *svcDBState) +{ + get_basic_attr(SDP_ATTR_SVCDB_STATE, svcDBState, uint32); +} + +/* + * NOTE that none of the setXXX() functions below will + * actually update the SDP server, unless the + * {register, update}sdp_record_t() function is invoked. + */ + +int sdp_attr_add_new(sdp_record_t *rec, uint16_t attr, uint8_t dtd, const void *value) +{ + sdp_data_t *d = sdp_data_alloc(dtd, value); + if (d) { + sdp_attr_replace(rec, attr, d); + return 0; + } + return -1; +} + +/* + * Set the information attributes of the service + * pointed to by rec. The attributes are + * service name, description and provider name + */ +void sdp_set_info_attr(sdp_record_t *rec, const char *name, const char *prov, const char *desc) +{ + if (name) + sdp_attr_add_new(rec, SDP_ATTR_SVCNAME_PRIMARY, SDP_TEXT_STR8, (void *)name); + if (prov) + sdp_attr_add_new(rec, SDP_ATTR_PROVNAME_PRIMARY, SDP_TEXT_STR8, (void *)prov); + if (desc) + sdp_attr_add_new(rec, SDP_ATTR_SVCDESC_PRIMARY, SDP_TEXT_STR8, (void *)desc); +} + +static sdp_data_t *access_proto_to_dataseq(sdp_record_t *rec, sdp_list_t *proto) +{ + sdp_data_t *seq = NULL; + void *dtds[10], *values[10]; + void **seqDTDs, **seqs; + int i, seqlen; + sdp_list_t *p; + + seqlen = sdp_list_len(proto); + seqDTDs = (void **)malloc(seqlen * sizeof(void *)); + seqs = (void **)malloc(seqlen * sizeof(void *)); + for (i = 0, p = proto; p; p = p->next, i++) { + sdp_list_t *elt = (sdp_list_t *)p->data; + sdp_data_t *s; + int pslen = 0; + for (; elt && pslen < sizeof(dtds); elt = elt->next, pslen++) { + sdp_data_t *d = (sdp_data_t *)elt->data; + dtds[pslen] = &d->dtd; + switch (d->dtd) { + case SDP_UUID16: + values[pslen] = &((uuid_t *)d)->value.uuid16; + break; + case SDP_UUID32: + values[pslen] = &((uuid_t *)d)->value.uuid32; + break; + case SDP_UUID128: + values[pslen] = &((uuid_t *)d)->value.uuid128; + break; + case SDP_UINT8: + values[pslen] = &d->val.uint8; + break; + case SDP_UINT16: + values[pslen] = &d->val.uint16; + break; + case SDP_SEQ8: + case SDP_SEQ16: + case SDP_SEQ32: + values[pslen] = d; + break; + // FIXME: more + } + } + s = sdp_seq_alloc(dtds, values, pslen); + if (s) { + seqDTDs[i] = &s->dtd; + seqs[i] = s; + } + } + seq = sdp_seq_alloc(seqDTDs, seqs, seqlen); + free(seqDTDs); + free(seqs); + return seq; +} + +/* + * sets the access protocols of the service specified + * to the value specified in "access_proto" + * + * Note that if there are alternate mechanisms by + * which the service is accessed, then they should + * be specified as sequences + * + * Using a value of NULL for accessProtocols has + * effect of removing this attribute (if previously set) + * + * This function replaces the existing sdp_access_proto_t + * structure (if any) with the new one specified. + * + * returns 0 if successful or -1 if there is a failure. + */ +int sdp_set_access_protos(sdp_record_t *rec, const sdp_list_t *ap) +{ + const sdp_list_t *p; + sdp_data_t *protos = 0; + + for (p = ap; p; p = p->next) { + sdp_data_t *seq = access_proto_to_dataseq(rec, (sdp_list_t *)p->data); + protos = sdp_seq_append(protos, seq); + } + sdp_attr_add(rec, SDP_ATTR_PROTO_DESC_LIST, protos); + return 0; +} + +/* + * set the "LanguageBase" attributes of the service record + * record to the value specified in "langAttrList". + * + * "langAttrList" is a linked list of "sdp_lang_attr_t" + * objects, one for each language in which user visible + * attributes are present in the service record. + * + * Using a value of NULL for langAttrList has + * effect of removing this attribute (if previously set) + * + * This function replaces the exisiting sdp_lang_attr_t + * structure (if any) with the new one specified. + * + * returns 0 if successful or -1 if there is a failure. + */ +int sdp_set_lang_attr(sdp_record_t *rec, const sdp_list_t *seq) +{ + uint8_t uint16 = SDP_UINT16; + int status = 0, i = 0, seqlen = sdp_list_len(seq); + void **dtds = (void **)malloc(3 * seqlen * sizeof(void *)); + void **values = (void **)malloc(3 * seqlen * sizeof(void *)); + const sdp_list_t *p; + + for (p = seq; p; p = p->next) { + sdp_lang_attr_t *lang = (sdp_lang_attr_t *)p->data; + if (!lang) { + status = -1; + break; + } + dtds[i] = &uint16; + values[i] = &lang->code_ISO639; + i++; + dtds[i] = &uint16; + values[i] = &lang->encoding; + i++; + dtds[i] = &uint16; + values[i] = &lang->base_offset; + i++; + } + if (status == 0) { + sdp_data_t *seq = sdp_seq_alloc(dtds, values, 3 * seqlen); + sdp_attr_add(rec, SDP_ATTR_LANG_BASE_ATTR_ID_LIST, seq); + } + free(dtds); + free(values); + return status; +} + +/* + * set the "ServiceID" attribute of the service. + * + * This is the UUID of the service. + * + * returns 0 if successful or -1 if there is a failure. + */ +void sdp_set_service_id(sdp_record_t *rec, uuid_t uuid) +{ + switch (uuid.type) { + case SDP_UUID16: + sdp_attr_add_new(rec, SDP_ATTR_SERVICE_ID, SDP_UUID16, &uuid.value.uuid16); + break; + case SDP_UUID32: + sdp_attr_add_new(rec, SDP_ATTR_SERVICE_ID, SDP_UUID32, &uuid.value.uuid32); + break; + case SDP_UUID128: + sdp_attr_add_new(rec, SDP_ATTR_SERVICE_ID, SDP_UUID128, &uuid.value.uuid128); + break; + } + sdp_pattern_add_uuid(rec, &uuid); +} + +/* + * set the GroupID attribute of the service record defining a group. + * + * This is the UUID of the group. + * + * returns 0 if successful or -1 if there is a failure. + */ +void sdp_set_group_id(sdp_record_t *rec, uuid_t uuid) +{ + switch (uuid.type) { + case SDP_UUID16: + sdp_attr_add_new(rec, SDP_ATTR_GROUP_ID, SDP_UUID16, &uuid.value.uuid16); + break; + case SDP_UUID32: + sdp_attr_add_new(rec, SDP_ATTR_GROUP_ID, SDP_UUID32, &uuid.value.uuid32); + break; + case SDP_UUID128: + sdp_attr_add_new(rec, SDP_ATTR_GROUP_ID, SDP_UUID128, &uuid.value.uuid128); + break; + } + sdp_pattern_add_uuid(rec, &uuid); +} + +/* + * set the ProfileDescriptorList attribute of the service record + * pointed to by record to the value specified in "profileDesc". + * + * Each element in the list is an object of type + * sdp_profile_desc_t which is a definition of the + * Bluetooth profile that this service conforms to. + * + * Using a value of NULL for profileDesc has + * effect of removing this attribute (if previously set) + * + * This function replaces the exisiting ProfileDescriptorList + * structure (if any) with the new one specified. + * + * returns 0 if successful or -1 if there is a failure. + */ +int sdp_set_profile_descs(sdp_record_t *rec, const sdp_list_t *profiles) +{ + int status = 0; + uint8_t uuid16 = SDP_UUID16; + uint8_t uuid32 = SDP_UUID32; + uint8_t uuid128 = SDP_UUID128; + uint8_t uint16 = SDP_UINT16; + int i = 0, seqlen = sdp_list_len(profiles); + void **seqDTDs = (void **)malloc(seqlen * sizeof(void *)); + void **seqs = (void **)malloc(seqlen * sizeof(void *)); + const sdp_list_t *p; + + for (p = profiles; p; p = p->next) { + sdp_data_t *seq; + void *dtds[2], *values[2]; + sdp_profile_desc_t *profile = (sdp_profile_desc_t *)p->data; + if (!profile) { + status = -1; + break; + } + switch (profile->uuid.type) { + case SDP_UUID16: + dtds[0] = &uuid16; + values[0] = &profile->uuid.value.uuid16; + break; + case SDP_UUID32: + dtds[0] = &uuid32; + values[0] = &profile->uuid.value.uuid32; + break; + case SDP_UUID128: + dtds[0] = &uuid128; + values[0] = &profile->uuid.value.uuid128; + break; + default: + status = -1; + break; + } + dtds[1] = &uint16; + values[1] = &profile->version; + seq = sdp_seq_alloc(dtds, values, 2); + if (seq) { + seqDTDs[i] = &seq->dtd; + seqs[i] = seq; + sdp_pattern_add_uuid(rec, &profile->uuid); + } + i++; + } + if (status == 0) { + sdp_data_t *pAPSeq = sdp_seq_alloc(seqDTDs, seqs, seqlen); + sdp_attr_add(rec, SDP_ATTR_PFILE_DESC_LIST, pAPSeq); + } + free(seqDTDs); + free(seqs); + return status; +} + +/* + * sets various URL attributes of the service + * pointed to by record. The URL include + * + * client: a URL to the client's + * platform specific (WinCE, PalmOS) executable + * code that can be used to access this service. + * + * doc: a URL pointing to service documentation + * + * icon: a URL to an icon that can be used to represent + * this service. + * + * Note that you need to pass NULL for any URLs + * that you don't want to set or remove + */ +void sdp_set_url_attr(sdp_record_t *rec, const char *client, const char *doc, const char *icon) +{ + sdp_attr_add_new(rec, SDP_ATTR_CLNT_EXEC_URL, SDP_URL_STR8, client); + sdp_attr_add_new(rec, SDP_ATTR_DOC_URL, SDP_URL_STR8, doc); + sdp_attr_add_new(rec, SDP_ATTR_ICON_URL, SDP_URL_STR8, icon); +} + +/* + * The code in this function is executed only once per + * thread. We compute the actual bit value of the Bluetooth + * base UUID which is a string defined in bt_std_values.h + * and is assumed to be of the standard form with "-" separators. + * + * The algorithm however converts the string to 4 unsigned longs + * using the strtoul() and assigns the values in sequence to + * the 128bit value + */ +uint128_t *sdp_create_base_uuid(void) +{ + char baseStr[128]; + int delim = '-'; + unsigned long dataLongValue; + char *delimPtr; + char *dataPtr; + char temp[10]; + int toBeCopied; + char *data; + + if (bluetooth_base_uuid == NULL) { + strcpy(baseStr, BASE_UUID); + bluetooth_base_uuid = (uint128_t *)malloc(sizeof(uint128_t)); + data = bluetooth_base_uuid->data; + memset(data, '\0', sizeof(uint128_t)); + memset(temp, '\0', 10); + dataPtr = baseStr; + delimPtr = NULL; + delimPtr = strchr(dataPtr, delim); + toBeCopied = delimPtr - dataPtr; + if (toBeCopied != 8) { + SDPDBG("To be copied(1) : %d\n", toBeCopied); + return NULL; + } + strncpy(temp, dataPtr, toBeCopied); + dataLongValue = htonl(strtoul(temp, NULL, 16)); + memcpy(&data[0], &dataLongValue, 4); + + /* + * Get the next 4 bytes (note that there is a "-" + * between them now) + */ + memset(temp, '\0', 10); + dataPtr = delimPtr + 1; + delimPtr = strchr(dataPtr, delim); + toBeCopied = delimPtr - dataPtr; + if (toBeCopied != 4) { + SDPDBG("To be copied(2) : %d\n", toBeCopied); + return NULL; + } + strncpy(temp, dataPtr, toBeCopied); + dataPtr = delimPtr + 1; + delimPtr = strchr(dataPtr, delim); + toBeCopied = delimPtr - dataPtr; + if (toBeCopied != 4) { + SDPDBG("To be copied(3) : %d\n", toBeCopied); + return NULL; + } + strncat(temp, dataPtr, toBeCopied); + dataLongValue = htonl(strtoul(temp, NULL, 16)); + memcpy(&data[4], &dataLongValue, 4); + + /* + * Get the last 4 bytes (note that there are 6 bytes + * after the last separator, which is truncated (2+4) + */ + memset(temp, '\0', 10); + dataPtr = delimPtr + 1; + dataPtr = delimPtr + 1; + delimPtr = strchr(dataPtr, delim); + toBeCopied = delimPtr - dataPtr; + if (toBeCopied != 4) { + SDPDBG("To be copied(4) : %d\n", toBeCopied); + return NULL; + } + strncpy(temp, dataPtr, toBeCopied); + strncat(temp, (delimPtr + 1), 4); + dataLongValue = htonl(strtoul(temp, NULL, 16)); + memcpy(&data[8], &dataLongValue, 4); + dataLongValue = htonl(strtoul(delimPtr + 5, NULL, 16)); + memcpy(&data[12], &dataLongValue, 4); + } + return bluetooth_base_uuid; +} + +uuid_t *sdp_uuid16_create(uuid_t *u, uint16_t val) +{ + memset(u, 0, sizeof(uuid_t)); + u->type = SDP_UUID16; + u->value.uuid16 = val; + return u; +} + +uuid_t *sdp_uuid32_create(uuid_t *u, uint32_t val) +{ + memset(u, 0, sizeof(uuid_t)); + u->type = SDP_UUID32; + u->value.uuid32 = val; + return u; +} + +uuid_t *sdp_uuid128_create(uuid_t *u, const void *val) +{ + memset(u, 0, sizeof(uuid_t)); + u->type = SDP_UUID128; + memcpy(&u->value.uuid128, val, sizeof(uint128_t)); + return u; +} + +/* + * UUID comparison function + * returns 0 if uuidValue1 == uuidValue2 else -1 + */ +int sdp_uuid16_cmp(const void *p1, const void *p2) +{ + const uuid_t *u1 = (const uuid_t *)p1; + const uuid_t *u2 = (const uuid_t *)p2; + return memcmp(&u1->value.uuid16, &u2->value.uuid16, sizeof(uint16_t)); +} + +/* + * UUID comparison function + * returns 0 if uuidValue1 == uuidValue2 else -1 + */ +int sdp_uuid128_cmp(const void *p1, const void *p2) +{ + const uuid_t *u1 = (const uuid_t *)p1; + const uuid_t *u2 = (const uuid_t *)p2; + return memcmp(&u1->value.uuid128, &u2->value.uuid128, sizeof(uint128_t)); +} + +/* + * 128 to 16 bit and 32 to 16 bit UUID conversion functions + * yet to be implemented. Note that the input is in NBO in + * both 32 and 128 bit UUIDs and conversion is needed + */ +void sdp_uuid16_to_uuid128(uuid_t *uuid128, uuid_t *uuid16) +{ + /* + * We have a 16 bit value, which needs to be added to + * bytes 3 and 4 (at indices 2 and 3) of the Bluetooth base + */ + unsigned short data1; + + // allocate a 128bit UUID and init to the Bluetooth base UUID + uint128_t *pBTBase128Bit = sdp_create_base_uuid(); + uuid128->value.uuid128 = *pBTBase128Bit; + uuid128->type = SDP_UUID128; + + // extract bytes 2 and 3 of 128bit BT base UUID + memcpy(&data1, &pBTBase128Bit->data[2], 2); + + // add the given UUID (16 bits) + data1 += htons(uuid16->value.uuid16); + + // set bytes 2 and 3 of the 128 bit value + memcpy(&uuid128->value.uuid128.data[2], &data1, 2); +} + +void sdp_uuid32_to_uuid128(uuid_t *uuid128, uuid_t *uuid32) +{ + /* + * We have a 32 bit value, which needs to be added to + * bytes 1->4 (at indices 0 thru 3) of the Bluetooth base + */ + unsigned int data0; + + // allocate a 128bit UUID and init to the Bluetooth base UUID + uint128_t *pBTBase128Bit = sdp_create_base_uuid(); + uuid128->value.uuid128 = *pBTBase128Bit; + uuid128->type = SDP_UUID128; + + // extract first 4 bytes + memcpy(&data0, &pBTBase128Bit->data[0], 4); + + // add the given UUID (32bits) + data0 += htonl(uuid32->value.uuid32); + + // set the 4 bytes of the 128 bit value + memcpy(&uuid128->value.uuid128.data[0], &data0, 4); +} + +uuid_t *sdp_uuid_to_uuid128(uuid_t *uuid) +{ + uuid_t *uuid128 = (uuid_t *)malloc(sizeof(uuid_t)); + memset(uuid128, 0, sizeof(uuid_t)); + switch (uuid->type) { + case SDP_UUID128: + *uuid128 = *uuid; + break; + case SDP_UUID32: + sdp_uuid32_to_uuid128(uuid128, uuid); + break; + case SDP_UUID16: + sdp_uuid16_to_uuid128(uuid128, uuid); + break; + } + return uuid128; +} + +/* + * converts a 128-bit uuid to a 16/32-bit one if possible + * returns true if uuid contains a 16/32-bit UUID at exit + */ +int sdp_uuid128_to_uuid(uuid_t *uuid) +{ + extern uint128_t *sdp_create_base_uuid(); + int i; + uint128_t *b = sdp_create_base_uuid(); + uint128_t *u = &uuid->value.uuid128; + uint32_t data; + + if (uuid->type != SDP_UUID128) + return 1; + + for (i = 4; i < sizeof(b->data); i++) + if (b->data[i] != u->data[i]) + return 0; + + memcpy(&data, u->data, 4); + data = htonl(data); + if (data <= 0xffff) { + uuid->type = SDP_UUID16; + uuid->value.uuid16 = (uint16_t)data; + } else { + uuid->type = SDP_UUID32; + uuid->value.uuid32 = data; + } + return 1; +} + +/* + * convert a UUID to the 16-bit short-form + */ +int sdp_uuid_to_proto(uuid_t *uuid) +{ + uuid_t u = *uuid; + if (sdp_uuid128_to_uuid(&u) && u.type == SDP_UUID16) + return u.value.uuid16; + return 0; +} + +int sdp_uuid_extract(const char *p, uuid_t *uuid, int *scanned) +{ + uint8_t type = *(const uint8_t *)p; + + if (!SDP_IS_UUID(type)) { + SDPERR("Unknown data type : %d expecting a svc UUID\n", type); + return -1; + } + p += sizeof(uint8_t); + *scanned += sizeof(uint8_t); + if (type == SDP_UUID16) { + sdp_uuid16_create(uuid, ntohs(sdp_get_unaligned((uint16_t *)p))); + *scanned += sizeof(uint16_t); + p += sizeof(uint16_t); + } else if (type == SDP_UUID32) { + sdp_uuid32_create(uuid, ntohl(sdp_get_unaligned((uint32_t *)p))); + *scanned += sizeof(uint32_t); + p += sizeof(uint32_t); + } else { + sdp_uuid128_create(uuid, p); + *scanned += sizeof(uint128_t); + p += sizeof(uint128_t); + } + return 0; +} + +/* + * This function appends data to the PDU buffer "dst" from source "src". + * The data length is also computed and set. + * Should the PDU length exceed 2^8, then sequence type is + * set accordingly and the data is memmove()'d. + */ +void sdp_append_to_buf(sdp_buf_t *dst, char *data, int len) +{ + char *p = dst->data; + uint8_t dtd = *(uint8_t *)p; + + SDPDBG("Append src size: %d\n", len); + SDPDBG("Append dst size: %d\n", dst->data_size); + SDPDBG("Dst buffer size: %d\n", dst->buf_size); + if (dst->data_size + len > dst->buf_size) { + int need = SDP_PDU_CHUNK_SIZE * ((len / SDP_PDU_CHUNK_SIZE) + 1); + dst->data = (char *)realloc(dst->data, dst->buf_size + need); + + SDPDBG("Realloc'ing : %d\n", need); + + if (dst->data == NULL) { + SDPERR("Realloc fails \n"); + } + dst->buf_size += need; + } + if (dst->data_size == 0 && dtd == 0) { + // create initial sequence + *(uint8_t *)p = SDP_SEQ8; + p += sizeof(uint8_t); + dst->data_size += sizeof(uint8_t); + // reserve space for sequence size + p += sizeof(uint8_t); + dst->data_size += sizeof(uint8_t); + } + + memcpy(dst->data + dst->data_size, data, len); + dst->data_size += len; + + dtd = *(uint8_t *)dst->data; + if (dst->data_size > UCHAR_MAX && dtd == SDP_SEQ8) { + short offset = sizeof(uint8_t) + sizeof(uint8_t); + memmove(dst->data + offset + 1, dst->data + offset, dst->data_size - offset); + p = dst->data; + *(uint8_t *)p = SDP_SEQ16; + p += sizeof(uint8_t); + dst->data_size += 1; + } + p = dst->data; + dtd = *(uint8_t *)p; + p += sizeof(uint8_t); + switch (dtd) { + case SDP_SEQ8: + *(uint8_t *)p = dst->data_size - sizeof(uint8_t) - sizeof(uint8_t); + break; + case SDP_SEQ16: + sdp_put_unaligned(htons(dst->data_size - sizeof(uint8_t) - sizeof(uint16_t)), (uint16_t *)p); + break; + case SDP_SEQ32: + sdp_put_unaligned(htonl(dst->data_size - sizeof(uint8_t) - sizeof(uint32_t)), (uint32_t *)p); + break; + } +} + +void sdp_append_to_pdu(sdp_buf_t *pdu, sdp_data_t *d) +{ + char buf[SDP_SEQ_PDUFORM_SIZE]; + sdp_buf_t append; + + append.data = buf; + append.buf_size = sizeof(buf); + append.data_size = 0; + sdp_set_attrid(&append, d->attrId); + sdp_gen_pdu(&append, d); + sdp_append_to_buf(pdu, append.data, append.data_size); +} + +/* + * Registers an sdp record. + * + * It is incorrect to call this method on a record that + * has been already registered with the server. + * + * Returns a non-null value (a pointer) to a service + * record if successful, else -1 setting errno + */ +int sdp_record_register(sdp_session_t *session, sdp_record_t *rec, uint8_t flags) +{ + char *p; + int status = 0; + char *req, *rsp; + int reqsize, rspsize; + sdp_pdu_hdr_t *reqhdr, *rsphdr; + sdp_buf_t pdu; + + SDPDBG(""); + + if (!session->local) { + errno = EREMOTE; + return -1; + } + req = (char *)malloc(SDP_REQ_BUFFER_SIZE); + rsp = (char *)malloc(SDP_RSP_BUFFER_SIZE); + if (req == NULL || rsp == NULL) { + status = -1; + errno = ENOMEM; + goto end; + } + reqhdr = (sdp_pdu_hdr_t *)req; + reqhdr->pdu_id = SDP_SVC_REGISTER_REQ; + reqhdr->tid = htons(sdp_gen_tid(session)); + reqsize = sizeof(sdp_pdu_hdr_t) + 1; + p = req + sizeof(sdp_pdu_hdr_t); + *p++ = flags; + if (0 > sdp_gen_record_pdu(rec, &pdu)) { + status = -1; + errno = ENOMEM; + goto end; + } + memcpy(p, pdu.data, pdu.data_size); + free(pdu.data); + reqsize += pdu.data_size; + reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t)); + + status = sdp_send_req_w4_rsp(session, req, rsp, reqsize, &rspsize); + if (status < 0) + goto end; + rsphdr = (sdp_pdu_hdr_t *)rsp; + p = rsp + sizeof(sdp_pdu_hdr_t); + if (rsphdr->pdu_id == SDP_SVC_REGISTER_RSP) { + uint32_t handle = ntohl(sdp_get_unaligned((uint32_t *)p)); + sdp_data_t *data = sdp_data_alloc(SDP_UINT32, &handle); + rec->handle = handle; + sdp_attr_replace(rec, SDP_ATTR_RECORD_HANDLE, data); + } +end: + if (req) + free(req); + if (rsp) + free(rsp); + return status; +} + +/* + * unregister a service record + */ +int sdp_record_unregister(sdp_session_t *session, sdp_record_t *rec) +{ + char *p; + int status = 0; + char *reqbuf, *rspbuf; + int reqsize = 0, rspsize = 0; + sdp_pdu_hdr_t *reqhdr, *rsphdr; + uint32_t handle = 0; + + SDPDBG(""); + + handle = rec->handle; + if (handle == SDP_SERVER_RECORD_HANDLE) { + errno = EINVAL; + return -1; + } + if (!session->local) { + errno = EREMOTE; + return -1; + } + reqbuf = (char *)malloc(SDP_REQ_BUFFER_SIZE); + rspbuf = (char *)malloc(SDP_RSP_BUFFER_SIZE); + if (!reqbuf || !rspbuf) { + errno = ENOMEM; + status = -1; + goto end; + } + reqhdr = (sdp_pdu_hdr_t *)reqbuf; + reqhdr->pdu_id = SDP_SVC_REMOVE_REQ; + reqhdr->tid = htons(sdp_gen_tid(session)); + + p = reqbuf + sizeof(sdp_pdu_hdr_t); + reqsize = sizeof(sdp_pdu_hdr_t); + sdp_put_unaligned(htonl(handle), (uint32_t *)p); + reqsize += sizeof(uint32_t); + + reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t)); + status = sdp_send_req_w4_rsp(session, reqbuf, rspbuf, reqsize, &rspsize); + if (status == 0) { + rsphdr = (sdp_pdu_hdr_t *)rspbuf; + p = rspbuf + sizeof(sdp_pdu_hdr_t); + status = sdp_get_unaligned((uint16_t *)p); + if (status == 0 && rsphdr->pdu_id == SDP_SVC_REMOVE_RSP) { + SDPDBG("Removing local copy\n"); + sdp_record_free(rec); + } + } +end: + if (reqbuf) + free(reqbuf); + if (rspbuf) + free(rspbuf); + return status; +} + +/* + * modify an existing service record + */ +int sdp_record_update(sdp_session_t *session, const sdp_record_t *rec) +{ + char *p; + int status = 0; + char *reqbuf, *rspbuf; + int reqsize, rspsize; + sdp_pdu_hdr_t *reqhdr, *rsphdr; + uint32_t handle; + sdp_buf_t pdu; + + SDPDBG(""); + handle = rec->handle; + + if (handle == SDP_SERVER_RECORD_HANDLE) { + errno = EINVAL; + return -1; + } + if (!session->local) { + errno = EREMOTE; + return -1; + } + reqbuf = (char *)malloc(SDP_REQ_BUFFER_SIZE); + rspbuf = (char *)malloc(SDP_RSP_BUFFER_SIZE); + if (!reqbuf || !rspbuf) { + errno = ENOMEM; + status = -1; + goto end; + } + reqhdr = (sdp_pdu_hdr_t *)reqbuf; + reqhdr->pdu_id = SDP_SVC_UPDATE_REQ; + reqhdr->tid = htons(sdp_gen_tid(session)); + + p = (char *)(reqbuf + sizeof(sdp_pdu_hdr_t)); + reqsize = sizeof(sdp_pdu_hdr_t); + + sdp_put_unaligned(htonl(handle), (uint32_t *)p); + reqsize += sizeof(uint32_t); + p += sizeof(uint32_t); + + if (0 > sdp_gen_record_pdu(rec, &pdu)) { + errno = ENOMEM; + status = -1; + goto end; + } + memcpy(p, pdu.data, pdu.data_size); + reqsize += pdu.data_size; + + reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t)); + status = sdp_send_req_w4_rsp(session, reqbuf, rspbuf, reqsize, &rspsize); + + SDPDBG("Send req status : %d\n", status); + + if (status == 0) { + rsphdr = (sdp_pdu_hdr_t *)rspbuf; + p = rspbuf + sizeof(sdp_pdu_hdr_t); + status = sdp_get_unaligned((uint16_t *)p); + } +end: + if (reqbuf) + free(reqbuf); + if (rspbuf) + free(rspbuf); + return status; +} + +sdp_record_t *sdp_record_alloc() +{ + sdp_record_t *rec = (sdp_record_t *)malloc(sizeof(sdp_record_t)); + memset((void *)rec, 0, sizeof(sdp_record_t)); + rec->handle = 0xffffffff; + return rec; +} + +/* + * Free the contents of a service record + */ +void sdp_record_free(sdp_record_t *rec) +{ + sdp_list_free(rec->attrlist, (sdp_free_func_t)sdp_data_free); + sdp_list_free(rec->pattern, free); + free(rec); +} + +void sdp_pattern_add_uuid(sdp_record_t *rec, uuid_t *uuid) +{ + uuid_t *uuid128 = sdp_uuid_to_uuid128(uuid); + + SDPDBG("SvcRec : 0x%lx\n", (unsigned long)rec); + SDPDBG("Elements in target pattern : %d\n", sdp_list_len(rec->pattern)); + SDPDBG("Trying to add : 0x%lx\n", (unsigned long)uuid128); + + if (sdp_list_find(rec->pattern, uuid128, sdp_uuid128_cmp) == NULL) + rec->pattern = sdp_list_insert_sorted(rec->pattern, uuid128, sdp_uuid128_cmp); + else + free(uuid128); + + SDPDBG("Elements in target pattern : %d\n", sdp_list_len(rec->pattern)); +} + +void sdp_pattern_add_uuidseq(sdp_record_t *rec, sdp_list_t *seq) +{ + for (; seq; seq = seq->next) { + uuid_t *uuid = (uuid_t *)seq->data; + sdp_pattern_add_uuid(rec, uuid); + } +} + +/* + * Extract a sequence of service record handles from a PDU buffer + * and add the entries to a sdp_list_t. Note that the service record + * handles are not in "data element sequence" form, but just like + * an array of service handles + */ +static void extract_record_handle_seq(char *pdu, sdp_list_t **seq, int count, int *scanned) +{ + sdp_list_t *pSeq = *seq; + char *pdata = pdu; + int n; + + for (n = 0; n < count; n++) { + uint32_t *pSvcRec = (uint32_t *) malloc(sizeof(uint32_t)); + *pSvcRec = ntohl(sdp_get_unaligned((uint32_t *)pdata)); + pSeq = sdp_list_append(pSeq, pSvcRec); + pdata += sizeof(uint32_t); + *scanned += sizeof(uint32_t); + } + *seq = pSeq; +} +/* + * Generate the attribute sequence pdu form + * from sdp_list_t elements. Return length of attr seq + */ +static int gen_dataseq_pdu(char *dst, const sdp_list_t *seq, uint8_t dtd) +{ + sdp_data_t *dataseq; + void **types, **values; + sdp_buf_t buf; + int i, seqlen = sdp_list_len(seq); + + // Fill up the value and the dtd arrays + SDPDBG(""); + + memset(&buf, 0, sizeof(sdp_buf_t)); + buf.data = (char *)malloc(SDP_UUID_SEQ_SIZE); + buf.buf_size = SDP_UUID_SEQ_SIZE; + + SDPDBG("Seq length : %d\n", seqlen); + + types = malloc(seqlen * sizeof(void *)); + values = malloc(seqlen * sizeof(void *)); + for (i = 0; i < seqlen; i++) { + void *data = seq->data; + types[i] = &dtd; + if (SDP_IS_UUID(dtd)) + data = &((uuid_t *)data)->value; + values[i] = data; + seq = seq->next; + } + + dataseq = sdp_seq_alloc(types, values, seqlen); + SDPDBG("Data Seq : 0x%p\n", seq); + seqlen = sdp_gen_pdu(&buf, dataseq); + SDPDBG("Copying : %d\n", buf.data_size); + memcpy(dst, buf.data, buf.data_size); + + sdp_data_free(dataseq); + + free(types); + free(values); + free(buf.data); + return seqlen; +} + +static int gen_searchseq_pdu(char *dst, const sdp_list_t *seq) +{ + uuid_t *uuid = (uuid_t *)seq->data; + return gen_dataseq_pdu(dst, seq, uuid->type); +} + +static int gen_attridseq_pdu(char *dst, const sdp_list_t *seq, uint8_t dataType) +{ + return gen_dataseq_pdu(dst, seq, dataType); +} + +static int copy_cstate(char *pdata, const sdp_cstate_t *cstate) +{ + if (cstate) { + *pdata++ = cstate->length; + memcpy(pdata, cstate->data, cstate->length); + return cstate->length + 1; + } + *pdata = 0; + return 1; +} + +/* + * This is a service search request. + * + * INPUT : + * + * sdp_list_t *search_list + * Singly linked list containing elements of the search + * pattern. Each entry in the list is a UUID (DataTypeSDP_UUID16) + * of the service to be searched + * + * uint16_t max_rec_num + * A 16 bit integer which tells the service, the maximum + * entries that the client can handle in the response. The + * server is obliged not to return > max_rec_num entries + * + * OUTPUT : + * + * int return value + * 0: + * The request completed successfully. This does not + * mean the requested services were found + * -1: + * On any failure and sets errno + * + * sdp_list_t **rsp_list + * This variable is set on a successful return if there are + * non-zero service handles. It is a singly linked list of + * service records (sdp_record_t *) + * + * uint16_t *handleCount + * This is a pointer to a 16 bit integer, which is set to + * indicate the number of service record handles present in + * rec_list + */ +int sdp_service_search_req(sdp_session_t *session, const sdp_list_t *search, + uint16_t max_rec_num, sdp_list_t **rsp) +{ + int status = 0; + int reqsize = 0, _reqsize; + int rspsize = 0, rsplen; + int seqlen = 0; + int scanned, total_rec_count, rec_count; + char *pdata, *_pdata; + char *reqbuf, *rspbuf; + sdp_pdu_hdr_t *reqhdr, *rsphdr; + sdp_cstate_t *cstate = NULL; + + reqbuf = malloc(SDP_REQ_BUFFER_SIZE); + rspbuf = malloc(SDP_RSP_BUFFER_SIZE); + if (!reqbuf || !rspbuf) { + errno = ENOMEM; + status = -1; + goto end; + } + reqhdr = (sdp_pdu_hdr_t *)reqbuf; + reqhdr->pdu_id = SDP_SVC_SEARCH_REQ; + pdata = reqbuf + sizeof(sdp_pdu_hdr_t); + reqsize = sizeof(sdp_pdu_hdr_t); + + // add service class IDs for search + seqlen = gen_searchseq_pdu(pdata, search); + + SDPDBG("Data seq added : %d\n", seqlen); + + // set the length and increment the pointer + reqsize += seqlen; + pdata += seqlen; + + // specify the maximum svc rec count that client expects + sdp_put_unaligned(htons(max_rec_num), (uint16_t *)pdata); + reqsize += sizeof(uint16_t); + pdata += sizeof(uint16_t); + + _reqsize = reqsize; + _pdata = pdata; + *rsp = NULL; + + do { + // Add continuation state or NULL (first time) + reqsize = _reqsize + copy_cstate(_pdata, cstate); + + // Set the request header's param length + reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t)); + + reqhdr->tid = htons(sdp_gen_tid(session)); + /* + * Send the request, wait for response and if + * no error, set the appropriate values and return + */ + status = sdp_send_req_w4_rsp(session, reqbuf, rspbuf, reqsize, &rspsize); + if (status < 0) + goto end; + + rsplen = 0; + rsphdr = (sdp_pdu_hdr_t *)rspbuf; + rsplen = ntohs(rsphdr->plen); + + if (rsphdr->pdu_id == SDP_ERROR_RSP) { + SDPDBG("Status : 0x%x\n", rsphdr->pdu_id); + status = -1; + goto end; + } + scanned = 0; + pdata = rspbuf + sizeof(sdp_pdu_hdr_t); + + // net service record match count + total_rec_count = ntohs(sdp_get_unaligned((uint16_t *)pdata)); + pdata += sizeof(uint16_t); + scanned += sizeof(uint16_t); + rec_count = ntohs(sdp_get_unaligned((uint16_t *)pdata)); + pdata += sizeof(uint16_t); + scanned += sizeof(uint16_t); + + SDPDBG("Total svc count: %d\n", total_rec_count); + SDPDBG("Current svc count: %d\n", rec_count); + SDPDBG("ResponseLength: %d\n", rsplen); + + if (!rec_count) { + status = -1; + goto end; + } + extract_record_handle_seq(pdata, rsp, rec_count, &scanned); + SDPDBG("BytesScanned : %d\n", scanned); + + if (rsplen > scanned) { + uint8_t cstate_len; + + pdata = rspbuf + sizeof(sdp_pdu_hdr_t) + scanned; + cstate_len = *(uint8_t *)pdata; + if (cstate_len > 0) { + cstate = (sdp_cstate_t *)pdata; + SDPDBG("Cont state length: %d\n", cstate_len); + } else + cstate = NULL; + } + } while (cstate); + + end: + if (reqbuf) + free(reqbuf); + if (rspbuf) + free(rspbuf); + + return status; +} + +/* + * This is a service attribute request. + * + * INPUT : + * + * uint32_t handle + * The handle of the service for which the attribute(s) are + * requested + * + * sdp_attrreq_type_t reqtype + * Attribute identifiers are 16 bit unsigned integers specified + * in one of 2 ways described below : + * SDP_ATTR_REQ_INDIVIDUAL - 16bit individual identifiers + * They are the actual attribute identifiers in ascending order + * + * SDP_ATTR_REQ_RANGE - 32bit identifier range + * The high-order 16bits is the start of range + * the low-order 16bits are the end of range + * 0x0000 to 0xFFFF gets all attributes + * + * sdp_list_t *attrid + * Singly linked list containing attribute identifiers desired. + * Every element is either a uint16_t(attrSpec = SDP_ATTR_REQ_INDIVIDUAL) + * or a uint32_t(attrSpec=SDP_ATTR_REQ_RANGE) + * + * OUTPUT : + * return sdp_record_t * + * 0: + * On any error and sets errno + * !0: + * The service record + */ +sdp_record_t *sdp_service_attr_req(sdp_session_t *session, uint32_t handle, + sdp_attrreq_type_t reqtype, const sdp_list_t *attrids) +{ + int status = 0; + int reqsize = 0, _reqsize; + int rspsize = 0, rsp_count; + int attr_list_len = 0; + int seqlen = 0; + char *pdata, *_pdata; + char *reqbuf, *rspbuf; + sdp_pdu_hdr_t *reqhdr, *rsphdr; + sdp_cstate_t *cstate = NULL; + uint8_t cstate_len = 0; + sdp_buf_t rsp_concat_buf; + sdp_record_t *rec = 0; + + if (reqtype != SDP_ATTR_REQ_INDIVIDUAL && reqtype != SDP_ATTR_REQ_RANGE) { + errno = EINVAL; + return 0; + } + + reqbuf = (char *)malloc(SDP_REQ_BUFFER_SIZE); + rspbuf = (char *)malloc(SDP_RSP_BUFFER_SIZE); + if (!reqbuf || !rspbuf) { + errno = ENOMEM; + status = -1; + goto end; + } + memset((char *)&rsp_concat_buf, 0, sizeof(sdp_buf_t)); + reqhdr = (sdp_pdu_hdr_t *)reqbuf; + reqhdr->pdu_id = SDP_SVC_ATTR_REQ; + + pdata = reqbuf + sizeof(sdp_pdu_hdr_t); + reqsize = sizeof(sdp_pdu_hdr_t); + + // add the service record handle + sdp_put_unaligned(htonl(handle), (uint32_t *)pdata); + reqsize += sizeof(uint32_t); + pdata += sizeof(uint32_t); + + // specify the response limit + sdp_put_unaligned(htons(65535), (uint16_t *)pdata); + reqsize += sizeof(uint16_t); + pdata += sizeof(uint16_t); + + // get attr seq PDU form + seqlen = gen_attridseq_pdu(pdata, attrids, + reqtype == SDP_ATTR_REQ_INDIVIDUAL? SDP_UINT16 : SDP_UINT32); + if (seqlen == -1) { + errno = EINVAL; + status = -1; + goto end; + } + pdata += seqlen; + reqsize += seqlen; + SDPDBG("Attr list length : %d\n", seqlen); + + // save before Continuation State + _pdata = pdata; + _reqsize = reqsize; + + do { + // add NULL continuation state + reqsize = _reqsize + copy_cstate(_pdata, cstate); + + // set the request header's param length + reqhdr->tid = htons(sdp_gen_tid(session)); + reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t)); + + status = sdp_send_req_w4_rsp(session, reqbuf, rspbuf, reqsize, &rspsize); + if (status < 0) + goto end; + rsp_count = 0; + rsphdr = (sdp_pdu_hdr_t *)rspbuf; + if (rsphdr->pdu_id == SDP_ERROR_RSP) { + SDPDBG("PDU ID : 0x%x\n", rsphdr->pdu_id); + status = -1; + goto end; + } + pdata = rspbuf + sizeof(sdp_pdu_hdr_t); + rsp_count = ntohs(sdp_get_unaligned((uint16_t *)pdata)); + attr_list_len += rsp_count; + pdata += sizeof(uint16_t); + + // if continuation state set need to re-issue request before parsing + cstate_len = *(uint8_t *)(pdata + rsp_count); + + SDPDBG("Response id : %d\n", rsphdr->pdu_id); + SDPDBG("Attrlist byte count : %d\n", rsp_count); + SDPDBG("sdp_cstate_t length : %d\n", cstate_len); + + /* + * a split response: concatenate intermediate responses + * and the last one (which has cstate_len == 0) + */ + if (cstate_len > 0 || rsp_concat_buf.data_size != 0) { + char *targetPtr = NULL; + + cstate = cstate_len > 0? (sdp_cstate_t *)(pdata + rsp_count): 0; + + // build concatenated response buffer + rsp_concat_buf.data = (char *)realloc(rsp_concat_buf.data, rsp_concat_buf.data_size + rsp_count); + rsp_concat_buf.buf_size = rsp_concat_buf.data_size + rsp_count; + targetPtr = rsp_concat_buf.data + rsp_concat_buf.data_size; + memcpy(targetPtr, pdata, rsp_count); + rsp_concat_buf.data_size += rsp_count; + } + } while (cstate); + + if (attr_list_len > 0) { + int scanned = 0; + if (rsp_concat_buf.data_size != 0) + pdata = rsp_concat_buf.data; + rec = sdp_extract_pdu(pdata, &scanned); + + if (!rec) + status = -1; + } + + end: + if (reqbuf) + free(reqbuf); + if (rsp_concat_buf.data) + free(rsp_concat_buf.data); + if (rspbuf) + free(rspbuf); + return rec; +} + +/* + * This is a service search request combined with the service + * attribute request. First a service class match is done and + * for matching service, requested attributes are extracted + * + * INPUT : + * + * sdp_list_t *search + * Singly linked list containing elements of the search + * pattern. Each entry in the list is a UUID(DataTypeSDP_UUID16) + * of the service to be searched + * + * AttributeSpecification attrSpec + * Attribute identifiers are 16 bit unsigned integers specified + * in one of 2 ways described below : + * SDP_ATTR_REQ_INDIVIDUAL - 16bit individual identifiers + * They are the actual attribute identifiers in ascending order + * + * SDP_ATTR_REQ_RANGE - 32bit identifier range + * The high-order 16bits is the start of range + * the low-order 16bits are the end of range + * 0x0000 to 0xFFFF gets all attributes + * + * sdp_list_t *attrids + * Singly linked list containing attribute identifiers desired. + * Every element is either a uint16_t(attrSpec = SDP_ATTR_REQ_INDIVIDUAL) + * or a uint32_t(attrSpec=SDP_ATTR_REQ_RANGE) + * + * OUTPUT : + * int return value + * 0: + * The request completed successfully. This does not + * mean the requested services were found + * -1: + * On any error and sets errno + * + * sdp_list_t **rsp + * This variable is set on a successful return to point to + * service(s) found. Each element of this list is of type + * sdp_record_t* (of the services which matched the search list) + */ +int sdp_service_search_attr_req(sdp_session_t *session, const sdp_list_t *search, sdp_attrreq_type_t reqtype, const sdp_list_t *attrids, sdp_list_t **rsp) +{ + int status = 0; + int reqsize = 0, _reqsize; + int rspsize = 0; + int seqlen = 0, attr_list_len = 0; + int rsp_count = 0, cstate_len = 0; + char *pdata, *_pdata; + char *reqbuf, *rspbuf; + sdp_pdu_hdr_t *reqhdr, *rsphdr; + uint8_t dataType; + sdp_list_t *rec_list = NULL; + sdp_buf_t rsp_concat_buf; + sdp_cstate_t *cstate = NULL; + + if (reqtype != SDP_ATTR_REQ_INDIVIDUAL && reqtype != SDP_ATTR_REQ_RANGE) { + errno = EINVAL; + return -1; + } + reqbuf = (char *)malloc(SDP_REQ_BUFFER_SIZE); + rspbuf = (char *)malloc(SDP_RSP_BUFFER_SIZE); + if (!reqbuf || !rspbuf) { + errno = ENOMEM; + status = -1; + goto end; + } + + memset((char *)&rsp_concat_buf, 0, sizeof(sdp_buf_t)); + reqhdr = (sdp_pdu_hdr_t *)reqbuf; + reqhdr->pdu_id = SDP_SVC_SEARCH_ATTR_REQ; + + // generate PDU + pdata = reqbuf + sizeof(sdp_pdu_hdr_t); + reqsize = sizeof(sdp_pdu_hdr_t); + + // add service class IDs for search + seqlen = gen_searchseq_pdu(pdata, search); + + SDPDBG("Data seq added : %d\n", seqlen); + + // now set the length and increment the pointer + reqsize += seqlen; + pdata += seqlen; + + sdp_put_unaligned(htons(SDP_MAX_ATTR_LEN), (uint16_t *)pdata); + reqsize += sizeof(uint16_t); + pdata += sizeof(uint16_t); + + SDPDBG("Max attr byte count : %d\n", SDP_MAX_ATTR_LEN); + + // get attr seq PDU form + seqlen = gen_attridseq_pdu(pdata, attrids, + reqtype == SDP_ATTR_REQ_INDIVIDUAL? SDP_UINT16: SDP_UINT32); + if (seqlen == -1) { + status = EINVAL; + goto end; + } + pdata += seqlen; + SDPDBG("Attr list length : %d\n", seqlen); + reqsize += seqlen; + *rsp = 0; + + // save before Continuation State + _pdata = pdata; + _reqsize = reqsize; + + do { + reqhdr->tid = htons(sdp_gen_tid(session)); + + // add continuation state (can be null) + reqsize = _reqsize + copy_cstate(_pdata, cstate); + + // set the request header's param length + reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t)); + rsphdr = (sdp_pdu_hdr_t *)rspbuf; + status = sdp_send_req_w4_rsp(session, reqbuf, rspbuf, reqsize, &rspsize); + if (status < 0) { + SDPDBG("Status : 0x%x\n", rsphdr->pdu_id); + goto end; + } + + if (rsphdr->pdu_id == SDP_ERROR_RSP) { + status = -1; + goto end; + } + + pdata = rspbuf + sizeof(sdp_pdu_hdr_t); + rsp_count = ntohs(sdp_get_unaligned((uint16_t *)pdata)); + attr_list_len += rsp_count; + pdata += sizeof(uint16_t); // pdata points to attribute list + cstate_len = *(uint8_t *)(pdata + rsp_count); + + SDPDBG("Attrlist byte count : %d\n", attr_list_len); + SDPDBG("Response byte count : %d\n", rsp_count); + SDPDBG("Cstate length : %d\n", cstate_len); + /* + * This is a split response, need to concatenate intermediate + * responses and the last one which will have cstate_len == 0 + */ + if (cstate_len > 0 || rsp_concat_buf.data_size != 0) { + char *targetPtr = NULL; + + cstate = cstate_len > 0? (sdp_cstate_t *)(pdata + rsp_count): 0; + + // build concatenated response buffer + rsp_concat_buf.data = (char *)realloc(rsp_concat_buf.data, rsp_concat_buf.data_size + rsp_count); + targetPtr = rsp_concat_buf.data + rsp_concat_buf.data_size; + rsp_concat_buf.buf_size = rsp_concat_buf.data_size + rsp_count; + memcpy(targetPtr, pdata, rsp_count); + rsp_concat_buf.data_size += rsp_count; + } + } while (cstate); + + if (attr_list_len > 0) { + int scanned = 0; + + if (rsp_concat_buf.data_size != 0) + pdata = rsp_concat_buf.data; + + /* + * Response is a sequence of sequence(s) for one or + * more data element sequence(s) representing services + * for which attributes are returned + */ + scanned = sdp_extract_seqtype(pdata, &dataType, &seqlen); + + SDPDBG("Bytes scanned : %d\n", scanned); + SDPDBG("Seq length : %d\n", seqlen); + + if (scanned && seqlen) { + pdata += scanned; + do { + int recsize = 0; + sdp_record_t *rec = sdp_extract_pdu(pdata, &recsize); + if (rec == NULL) { + SDPERR("SVC REC is null\n"); + status = -1; + goto end; + } + scanned += recsize; + pdata += recsize; + + SDPDBG("Loc seq length : %d\n", recsize); + SDPDBG("Svc Rec Handle : 0x%x\n", rec->handle); + SDPDBG("Bytes scanned : %d\n", scanned); + SDPDBG("Attrlist byte count : %d\n", attr_list_len); + rec_list = sdp_list_append(rec_list, rec); + } while (scanned < attr_list_len); + + SDPDBG("Successful scan of service attr lists\n"); + *rsp = rec_list; + } + } + end: + if (rsp_concat_buf.data) + free(rsp_concat_buf.data); + if (reqbuf) + free(reqbuf); + if (rspbuf) + free(rspbuf); + return status; +} + +/* + * Find devices in the piconet. + */ +int sdp_general_inquiry(inquiry_info *ii, int num_dev, int duration, uint8_t *found) +{ + int n = hci_inquiry(-1, 10, num_dev, NULL, &ii, 0); + if (n < 0) { + SDPERR("Inquiry failed:%s", strerror(errno)); + return -1; + } + *found = n; + return 0; +} + +int sdp_close(sdp_session_t *session) +{ + int ret = close(session->sock); + free(session); + return ret; +} + +static inline int sdp_is_local(const bdaddr_t *device) +{ + return memcmp(device, BDADDR_LOCAL, sizeof(bdaddr_t)) == 0; +} + +sdp_session_t *sdp_connect(const bdaddr_t *src, const bdaddr_t *dst, uint32_t flags) +{ + sdp_session_t *session = malloc(sizeof(sdp_session_t)); + if (!session) + return session; + memset(session, 0, sizeof(*session)); + session->flags = flags; + if (sdp_is_local(dst)) { + struct sockaddr_un sa; + + // create local unix connection + session->sock = socket(PF_UNIX, SOCK_STREAM, 0); + session->local = 1; + if (session->sock >= 0) { + sa.sun_family = AF_UNIX; + strcpy(sa.sun_path, SDP_UNIX_PATH); + if (connect(session->sock, (struct sockaddr *)&sa, sizeof(sa)) == 0) + return session; + } + } else { + struct sockaddr_l2 sa; + + // create L2CAP connection + session->sock = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP); + session->local = 0; + if (session->sock >= 0) { + sa.l2_family = AF_BLUETOOTH; + sa.l2_psm = 0; + if (bacmp(src, BDADDR_ANY) != 0) { + sa.l2_bdaddr = *src; + if (0 > bind(session->sock, (struct sockaddr *)&sa, sizeof(sa))) + goto fail; + } + sa.l2_psm = htobs(SDP_PSM); + sa.l2_bdaddr = *dst; + do + if (connect(session->sock, (struct sockaddr *)&sa, sizeof(sa)) == 0) + return session; + while (errno == EBUSY && (flags & SDP_RETRY_IF_BUSY)); + } + } +fail: + if (session->sock >= 0) + close(session->sock); + free(session); + return 0; +} -- cgit From 51e671b116fe121053444c4817e38ad07e9586f8 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Wed, 31 Mar 2004 19:50:46 +0000 Subject: Use $(top_builddir) as include root directory --- src/sdp.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 4ffb0d93..b793f64f 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -38,13 +38,13 @@ #include #include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include #define BASE_UUID "00000000-0000-1000-8000-00805F9B34FB" -- cgit From ff3922238b4258eb2f9f9458589d3cecd177f7bc Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Thu, 1 Apr 2004 10:18:22 +0000 Subject: Store additional unitSize for strings --- src/sdp.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index b793f64f..0fec56f7 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -810,14 +810,15 @@ static sdp_data_t *extract_str(const void *p, int *len) return 0; } - s = (char *)malloc(n + 1); + s = malloc(n + 1); memset(s, 0, n + 1); - strncpy(s, p, n); + memcpy(s, p, n); SDPDBG("Len : %d\n", n); SDPDBG("Str : %s\n", s); d->val.str = s; + d->unitSize = n; return d; } -- cgit From b5705586036d2aefa5b75ebe26ed64445a4d79be Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Thu, 1 Apr 2004 14:02:12 +0000 Subject: Add string representation for HID and CIP --- src/sdp.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 0fec56f7..cbc68193 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -70,6 +70,8 @@ static struct tupla Protocol[] = { { HTTP_UUID, "HTTP" }, { WSP_UUID, "WSP" }, { BNEP_UUID, "BNEP" }, + { HIDP_UUID, "HIDP" }, + { CMTP_UUID, "CMTP" }, { 0 } }; @@ -97,6 +99,8 @@ static struct tupla ServiceClass[] = { { PANU_SVCLASS_ID, "PAN user" }, { NAP_SVCLASS_ID, "Network access point" }, { GN_SVCLASS_ID, "PAN group network" }, + { HID_SVCLASS_ID, "Human Interface Device" }, + { CIP_SVCLASS_ID, "Common ISDN Access" }, { 0 } }; @@ -116,6 +120,8 @@ static struct tupla Profile[] = { { PANU_PROFILE_ID, "PAN user" }, { NAP_PROFILE_ID, "PAN access point" }, { GN_PROFILE_ID, "PAN group network" }, + { HID_SVCLASS_ID, "Human Interface Device" }, + { CIP_SVCLASS_ID, "Common ISDN Access" }, { 0 } }; -- cgit From 764abe23a0d4ede999f1f34ee0e310c0eeaaff79 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sat, 3 Apr 2004 05:11:38 +0000 Subject: Update copyright information --- src/sdp.c | 61 ++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 32 insertions(+), 29 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index cbc68193..6740e9d6 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -1,35 +1,38 @@ -/* - BlueZ - Bluetooth protocol stack for Linux - Copyright (C) 2000-2001 Qualcomm Incorporated - - Written 2000,2001 by Maxim Krasnyansky - - Based on an original SDP implementation by Nokia Corporation. - Copyright (C) 2001,2002 Nokia Corporation. - Original author Guruprasad Krishnamurthy - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation; - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. - IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY - CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - - ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, - COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS - SOFTWARE IS DISCLAIMED. -*/ - /* - * $Id$ + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2001-2002 Nokia Corporation + * Copyright (C) 2002-2003 Maxim Krasnyansky + * Copyright (C) 2002-2004 Marcel Holtmann + * Copyright (C) 2002-2003 Stephen Crane + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. + * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY + * CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, + * COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS + * SOFTWARE IS DISCLAIMED. + * + * + * $Id$ */ +#ifdef HAVE_CONFIG_H +#include +#endif + #include #include #include -- cgit From 233bd39dee08afc5176b203e6348184da56d7bff Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sun, 2 May 2004 21:40:30 +0000 Subject: Add sdp_get_add_access_protos() function --- src/sdp.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 6740e9d6..75c8edcb 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -1310,6 +1310,30 @@ int sdp_get_access_protos(const sdp_record_t *rec, sdp_list_t **pap) return 0; } +int sdp_get_add_access_protos(const sdp_record_t *rec, sdp_list_t **pap) +{ + sdp_data_t *pdlist, *curr; + sdp_list_t *ap = 0; + + pdlist = sdp_data_get(rec, SDP_ATTR_ADD_PROTO_DESC_LIST); + if (pdlist == NULL) { + errno = ENODATA; + return -1; + } + SDPDBG("AP type : 0%x\n", pdlist->dtd); + + pdlist = pdlist->val.dataseq; + + for (; pdlist; pdlist = pdlist->next) { + sdp_list_t *pds = 0; + for (curr = pdlist->val.dataseq; curr; curr = curr->next) + pds = sdp_list_append(pds, curr->val.dataseq); + ap = sdp_list_append(ap, pds); + } + *pap = ap; + return 0; +} + int sdp_get_uuidseq_attr(const sdp_record_t *rec, uint16_t attr, sdp_list_t **seqp) { sdp_data_t *sdpdata = sdp_data_get(rec, attr); -- cgit From a8efcfa5ca81ef29e4d56dac70761d9b9ffc9708 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Thu, 22 Jul 2004 18:08:49 +0000 Subject: UUID32 values can also be used as protocol identifiers --- src/sdp.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 75c8edcb..9cb2230a 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -2069,14 +2069,14 @@ int sdp_uuid128_to_uuid(uuid_t *uuid) uint128_t *b = sdp_create_base_uuid(); uint128_t *u = &uuid->value.uuid128; uint32_t data; - + if (uuid->type != SDP_UUID128) return 1; - + for (i = 4; i < sizeof(b->data); i++) if (b->data[i] != u->data[i]) return 0; - + memcpy(&data, u->data, 4); data = htonl(data); if (data <= 0xffff) { @@ -2095,8 +2095,14 @@ int sdp_uuid128_to_uuid(uuid_t *uuid) int sdp_uuid_to_proto(uuid_t *uuid) { uuid_t u = *uuid; - if (sdp_uuid128_to_uuid(&u) && u.type == SDP_UUID16) - return u.value.uuid16; + if (sdp_uuid128_to_uuid(&u)) { + switch (u.type) { + case SDP_UUID16: + return u.value.uuid16; + case SDP_UUID32: + return u.value.uuid32; + } + } return 0; } -- cgit From 735e59b37803748fea1077de064b285f78de1c10 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Wed, 13 Oct 2004 12:03:16 +0000 Subject: Add SIM Access types --- src/sdp.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 9cb2230a..971644ba 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -104,6 +104,7 @@ static struct tupla ServiceClass[] = { { GN_SVCLASS_ID, "PAN group network" }, { HID_SVCLASS_ID, "Human Interface Device" }, { CIP_SVCLASS_ID, "Common ISDN Access" }, + { SAP_SVCLASS_ID, "SIM Access" }, { 0 } }; @@ -123,8 +124,9 @@ static struct tupla Profile[] = { { PANU_PROFILE_ID, "PAN user" }, { NAP_PROFILE_ID, "PAN access point" }, { GN_PROFILE_ID, "PAN group network" }, - { HID_SVCLASS_ID, "Human Interface Device" }, - { CIP_SVCLASS_ID, "Common ISDN Access" }, + { HID_PROFILE_ID, "Human Interface Device" }, + { CIP_PROFILE_ID, "Common ISDN Access" }, + { SAP_PROFILE_ID, "SIM Access" }, { 0 } }; -- cgit From 11cc772e89c75f5bf58fd887befac4b88a25c91c Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Tue, 26 Oct 2004 02:18:45 +0000 Subject: Add more audio/video profile translations --- src/sdp.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 971644ba..28fffc6f 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -74,6 +74,8 @@ static struct tupla Protocol[] = { { WSP_UUID, "WSP" }, { BNEP_UUID, "BNEP" }, { HIDP_UUID, "HIDP" }, + { AVCTP_UUID, "AVCTP" }, + { AVDTP_UUID, "AVDTP" }, { CMTP_UUID, "CMTP" }, { 0 } }; @@ -91,6 +93,8 @@ static struct tupla ServiceClass[] = { { IRMC_SYNC_CMD_SVCLASS_ID, "IrMCSync Command" }, { HEADSET_SVCLASS_ID, "Headset" }, { CORDLESS_TELEPHONY_SVCLASS_ID,"Cordless Telephony" }, + { AUDIO_SOURCE_SVCLASS_ID, "Audio Source" }, + { AUDIO_SINK_SVCLASS_ID, "Audio Sink" }, { INTERCOM_SVCLASS_ID, "Intercom" }, { FAX_SVCLASS_ID, "Fax" }, { HEADSET_AGW_SVCLASS_ID, "Headset Audio Gateway" }, @@ -118,6 +122,7 @@ static struct tupla Profile[] = { { IRMC_SYNC_CMD_PROFILE_ID, "IrMCSync Command" }, { HEADSET_PROFILE_ID, "Headset" }, { CORDLESS_TELEPHONY_PROFILE_ID, "Cordless Telephony" }, + { ADVANCED_AUDIO_PROFILE_ID, "Advanced Audio" }, { INTERCOM_PROFILE_ID, "Intercom" }, { FAX_PROFILE_ID, "Fax" }, { HEADSET_AGW_PROFILE_ID, "Headset Audio Gateway" }, -- cgit From e32d72e13f1ad2b8fb9d97f99e9c2bd5f94771f9 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Thu, 28 Oct 2004 10:38:41 +0000 Subject: Delete comment for a no long used parameter --- src/sdp.c | 5 ----- 1 file changed, 5 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 28fffc6f..05cb4e97 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -2565,11 +2565,6 @@ static int copy_cstate(char *pdata, const sdp_cstate_t *cstate) * This variable is set on a successful return if there are * non-zero service handles. It is a singly linked list of * service records (sdp_record_t *) - * - * uint16_t *handleCount - * This is a pointer to a 16 bit integer, which is set to - * indicate the number of service record handles present in - * rec_list */ int sdp_service_search_req(sdp_session_t *session, const sdp_list_t *search, uint16_t max_rec_num, sdp_list_t **rsp) -- cgit From 599e9c7e04dd55654ba8664ef2eba467ea0de277 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Tue, 2 Nov 2004 11:04:19 +0000 Subject: Fix bug where htonl() should be used instead of htons() --- src/sdp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 05cb4e97..e6035bf0 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -488,7 +488,7 @@ void sdp_set_seq_len(char *ptr, int length) case SDP_ALT32: case SDP_TEXT_STR32: case SDP_URL_STR32: - sdp_put_unaligned(htons(length), (uint32_t *)ptr); + sdp_put_unaligned(htonl(length), (uint32_t *)ptr); break; } } -- cgit From 0ea7064b34d9f03e9aa7d8abeb7c0e6bcc8a37ac Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Mon, 29 Nov 2004 03:48:21 +0000 Subject: Add service classes and profile ids for WAP --- src/sdp.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index e6035bf0..a830cf8b 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -98,6 +98,8 @@ static struct tupla ServiceClass[] = { { INTERCOM_SVCLASS_ID, "Intercom" }, { FAX_SVCLASS_ID, "Fax" }, { HEADSET_AGW_SVCLASS_ID, "Headset Audio Gateway" }, + { WAP_SVCLASS_ID, "WAP" }, + { WAP_CLIENT_SVCLASS_ID, "WAP Client" }, { PNP_INFO_SVCLASS_ID, "PnP Information" }, { GENERIC_NETWORKING_SVCLASS_ID,"Generic Networking" }, { GENERIC_FILETRANS_SVCLASS_ID, "Generic File Transfer" }, @@ -126,6 +128,8 @@ static struct tupla Profile[] = { { INTERCOM_PROFILE_ID, "Intercom" }, { FAX_PROFILE_ID, "Fax" }, { HEADSET_AGW_PROFILE_ID, "Headset Audio Gateway" }, + { WAP_PROFILE_ID, "WAP" }, + { WAP_CLIENT_PROFILE_ID, "WAP Client" }, { PANU_PROFILE_ID, "PAN user" }, { NAP_PROFILE_ID, "PAN access point" }, { GN_PROFILE_ID, "PAN group network" }, -- cgit From 6864c723272d4790a2fec4a1c23fa5e46ac15e5f Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Tue, 7 Dec 2004 11:29:58 +0000 Subject: Update the UUID constants and its translations --- src/sdp.c | 160 ++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 88 insertions(+), 72 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index a830cf8b..7c3bd7d7 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -60,91 +60,105 @@ struct tupla { }; static struct tupla Protocol[] = { - { SDP_UUID, "SDP" }, - { RFCOMM_UUID, "RFCOMM" }, - { TCS_BIN_UUID, "TCS-BIN" }, - { L2CAP_UUID, "L2CAP" }, - { IP_UUID, "IP" }, - { UDP_UUID, "UDP" }, - { TCP_UUID, "TCP" }, - { TCS_AT_UUID, "TCS-AT" }, - { OBEX_UUID, "OBEX" }, - { FTP_UUID, "FTP" }, - { HTTP_UUID, "HTTP" }, - { WSP_UUID, "WSP" }, - { BNEP_UUID, "BNEP" }, - { HIDP_UUID, "HIDP" }, - { AVCTP_UUID, "AVCTP" }, - { AVDTP_UUID, "AVDTP" }, - { CMTP_UUID, "CMTP" }, + { SDP_UUID, "SDP" }, + { UDP_UUID, "UDP" }, + { RFCOMM_UUID, "RFCOMM" }, + { TCP_UUID, "TCP" }, + { TCS_BIN_UUID, "TCS-BIN" }, + { TCS_AT_UUID, "TCS-AT" }, + { OBEX_UUID, "OBEX" }, + { IP_UUID, "IP" }, + { FTP_UUID, "FTP" }, + { HTTP_UUID, "HTTP" }, + { WSP_UUID, "WSP" }, + { BNEP_UUID, "BNEP" }, + { UPNP_UUID, "UPNP" }, + { HIDP_UUID, "HIDP" }, + { HCRP_CTRL_UUID, "HCRP-Ctrl" }, + { HCRP_DATA_UUID, "HCRP-Data" }, + { HCRP_NOTE_UUID, "HCRP-Notify" }, + { AVCTP_UUID, "AVCTP" }, + { AVDTP_UUID, "AVDTP" }, + { CMTP_UUID, "CMTP" }, + { UDI_UUID, "UDI" }, + { L2CAP_UUID, "L2CAP" }, { 0 } }; static struct tupla ServiceClass[] = { - { SDP_SERVER_SVCLASS_ID, "SDP Server" }, - { BROWSE_GRP_DESC_SVCLASS_ID, "Browse Group Descriptor" }, - { PUBLIC_BROWSE_GROUP, "Public Browse Group" }, - { SERIAL_PORT_SVCLASS_ID, "Serial Port" }, - { LAN_ACCESS_SVCLASS_ID, "LAN Access Using PPP" }, - { DIALUP_NET_SVCLASS_ID, "Dialup Networking" }, - { IRMC_SYNC_SVCLASS_ID, "IrMCSync" }, - { OBEX_OBJPUSH_SVCLASS_ID, "OBEX Object Push" }, - { OBEX_FILETRANS_SVCLASS_ID, "OBEX File Transfer" }, - { IRMC_SYNC_CMD_SVCLASS_ID, "IrMCSync Command" }, - { HEADSET_SVCLASS_ID, "Headset" }, - { CORDLESS_TELEPHONY_SVCLASS_ID,"Cordless Telephony" }, - { AUDIO_SOURCE_SVCLASS_ID, "Audio Source" }, - { AUDIO_SINK_SVCLASS_ID, "Audio Sink" }, - { INTERCOM_SVCLASS_ID, "Intercom" }, - { FAX_SVCLASS_ID, "Fax" }, - { HEADSET_AGW_SVCLASS_ID, "Headset Audio Gateway" }, - { WAP_SVCLASS_ID, "WAP" }, - { WAP_CLIENT_SVCLASS_ID, "WAP Client" }, - { PNP_INFO_SVCLASS_ID, "PnP Information" }, - { GENERIC_NETWORKING_SVCLASS_ID,"Generic Networking" }, - { GENERIC_FILETRANS_SVCLASS_ID, "Generic File Transfer" }, - { GENERIC_AUDIO_SVCLASS_ID, "Generic Audio" }, - { GENERIC_TELEPHONY_SVCLASS_ID, "Generic Telephony" }, - { PANU_SVCLASS_ID, "PAN user" }, - { NAP_SVCLASS_ID, "Network access point" }, - { GN_SVCLASS_ID, "PAN group network" }, - { HID_SVCLASS_ID, "Human Interface Device" }, - { CIP_SVCLASS_ID, "Common ISDN Access" }, - { SAP_SVCLASS_ID, "SIM Access" }, + { SDP_SERVER_SVCLASS_ID, "SDP Server" }, + { BROWSE_GRP_DESC_SVCLASS_ID, "Browse Group Descriptor" }, + { PUBLIC_BROWSE_GROUP, "Public Browse Group" }, + { SERIAL_PORT_SVCLASS_ID, "Serial Port" }, + { LAN_ACCESS_SVCLASS_ID, "LAN Access Using PPP" }, + { DIALUP_NET_SVCLASS_ID, "Dialup Networking" }, + { IRMC_SYNC_SVCLASS_ID, "IrMC Sync" }, + { OBEX_OBJPUSH_SVCLASS_ID, "OBEX Object Push" }, + { OBEX_FILETRANS_SVCLASS_ID, "OBEX File Transfer" }, + { IRMC_SYNC_CMD_SVCLASS_ID, "IrMC Sync Command" }, + { HEADSET_SVCLASS_ID, "Headset" }, + { CORDLESS_TELEPHONY_SVCLASS_ID, "Cordless Telephony" }, + { AUDIO_SOURCE_SVCLASS_ID, "Audio Source" }, + { AUDIO_SINK_SVCLASS_ID, "Audio Sink" }, + { AV_REMOTE_TARGET_SVCLASS_ID, "AV Remote Target" }, + { ADVANCED_AUDIO_SVCLASS_ID, "Advanced Audio" }, + { AV_REMOTE_SVCLASS_ID, "AV Remote" }, + { VIDEO_CONF_SVCLASS_ID, "Video Conferencing" }, + { INTERCOM_SVCLASS_ID, "Intercom" }, + { FAX_SVCLASS_ID, "Fax" }, + { HEADSET_AGW_SVCLASS_ID, "Headset Audio Gateway" }, + { WAP_SVCLASS_ID, "WAP" }, + { WAP_CLIENT_SVCLASS_ID, "WAP Client" }, + { PANU_SVCLASS_ID, "PAN User" }, + { NAP_SVCLASS_ID, "Network Access Point" }, + { GN_SVCLASS_ID, "PAN Group Network" }, + { DIRECT_PRINTING_SVCLASS_ID, "Direct Printing" }, + { REFERENCE_PRINTING_SVCLASS_ID, "Reference Printing" }, + { IMAGING_SVCLASS_ID, "Imaging" }, + { IMAGING_RESPONDER_SVCLASS_ID, "Imaging Responder" }, + { IMAGING_ARCHIVE_SVCLASS_ID, "Imaging Automatic Archive" }, + { IMAGING_REFOBJS_SVCLASS_ID, "Imaging Referenced Objects" }, + { HANDSFREE_SVCLASS_ID, "Handsfree" }, + { HANDSFREE_AGW_SVCLASS_ID, "Handfree Audio Gateway" }, + { DIRECT_PRT_REFOBJS_SVCLASS_ID, "Direct Printing Ref. Objects" }, + { REFLECTED_UI_SVCLASS_ID, "Reflected UI" }, + { BASIC_PRINTING_SVCLASS_ID, "Basic Printing" }, + { PRINTING_STATUS_SVCLASS_ID, "Printing Status" }, + { HID_SVCLASS_ID, "Human Interface Device" }, + { HCR_SVCLASS_ID, "Hardcopy Cable Replacement" }, + { HCR_PRINT_SVCLASS_ID, "HCR Print" }, + { HCR_SCAN_SVCLASS_ID, "HCR Scan" }, + { CIP_SVCLASS_ID, "Common ISDN Access" }, + { VIDEO_CONF_GW_SVCLASS_ID, "Video Conferencing Gateway" }, + { UDI_MT_SVCLASS_ID, "UDI MT" }, + { UDI_TA_SVCLASS_ID, "UDI TA" }, + { AV_SVCLASS_ID, "Audio/Video" }, + { SAP_SVCLASS_ID, "SIM Access" }, + { PNP_INFO_SVCLASS_ID, "PnP Information" }, + { GENERIC_NETWORKING_SVCLASS_ID, "Generic Networking" }, + { GENERIC_FILETRANS_SVCLASS_ID, "Generic File Transfer" }, + { GENERIC_AUDIO_SVCLASS_ID, "Generic Audio" }, + { GENERIC_TELEPHONY_SVCLASS_ID, "Generic Telephony" }, + { UPNP_SVCLASS_ID, "UPnP" }, + { UPNP_IP_SVCLASS_ID, "UPnP IP" }, + { UPNP_PAN_SVCLASS_ID, "UPnP PAN" }, + { UPNP_LAP_SVCLASS_ID, "UPnP LAP" }, + { UPNP_L2CAP_SVCLASS_ID, "UPnP L2CAP" }, + { VIDEO_SOURCE_SVCLASS_ID, "Video Source" }, + { VIDEO_SINK_SVCLASS_ID, "Video Sink" }, { 0 } }; -static struct tupla Profile[] = { - { SERIAL_PORT_PROFILE_ID, "Serial Port" }, - { LAN_ACCESS_PROFILE_ID, "LAN Access Using PPP" }, - { DIALUP_NET_PROFILE_ID, "Dialup Networking" }, - { IRMC_SYNC_PROFILE_ID, "IrMCSync" }, - { OBEX_OBJPUSH_PROFILE_ID, "OBEX Object Push" }, - { OBEX_FILETRANS_PROFILE_ID, "OBEX File Transfer" }, - { IRMC_SYNC_CMD_PROFILE_ID, "IrMCSync Command" }, - { HEADSET_PROFILE_ID, "Headset" }, - { CORDLESS_TELEPHONY_PROFILE_ID, "Cordless Telephony" }, - { ADVANCED_AUDIO_PROFILE_ID, "Advanced Audio" }, - { INTERCOM_PROFILE_ID, "Intercom" }, - { FAX_PROFILE_ID, "Fax" }, - { HEADSET_AGW_PROFILE_ID, "Headset Audio Gateway" }, - { WAP_PROFILE_ID, "WAP" }, - { WAP_CLIENT_PROFILE_ID, "WAP Client" }, - { PANU_PROFILE_ID, "PAN user" }, - { NAP_PROFILE_ID, "PAN access point" }, - { GN_PROFILE_ID, "PAN group network" }, - { HID_PROFILE_ID, "Human Interface Device" }, - { CIP_PROFILE_ID, "Common ISDN Access" }, - { SAP_PROFILE_ID, "SIM Access" }, - { 0 } -}; +#define Profile ServiceClass static char *string_lookup(struct tupla *pt0, int index) { struct tupla *pt; + for (pt = pt0; pt->index; pt++) - if (pt->index == index) + if (pt->index == index) return pt->str; + return ""; } @@ -160,6 +174,7 @@ static int uuid2str(struct tupla *message, const uuid_t *uuid, char *str, size_t snprintf(str, n, "NULL"); return -2; } + switch (uuid->type) { case SDP_UUID16: str2 = string_lookup(message, uuid->value.uuid16); @@ -175,6 +190,7 @@ static int uuid2str(struct tupla *message, const uuid_t *uuid, char *str, size_t snprintf(str, n, "Type of UUID (%x) unknown.", uuid->type); return -1; } + return 0; } -- cgit From 0e6eb45baedd15cc65beb454ec00cefb5fb7962d Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Fri, 10 Dec 2004 09:05:49 +0000 Subject: Fix UUID-32 to string translation --- src/sdp.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 7c3bd7d7..f138f1b3 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -181,10 +181,11 @@ static int uuid2str(struct tupla *message, const uuid_t *uuid, char *str, size_t snprintf(str, n, str2); break; case SDP_UUID32: - snprintf(str, n, "Error: This is uuid32"); - return -3; + str2 = string_lookup(message, uuid->value.uuid32); + snprintf(str, n, str2); + break; case SDP_UUID128: - snprintf(str, n, "Error: This is uuid128"); + snprintf(str, n, "Error: This is UUID-128"); return -4; default: snprintf(str, n, "Type of UUID (%x) unknown.", uuid->type); -- cgit From aae7cfb9807fd7452e6662fc9f6c3ebe0e64dbf5 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sun, 23 Jan 2005 14:33:58 +0000 Subject: Replace sdp_{get|put}_unaligned with bt_{get|put}_unaligned --- src/sdp.c | 180 +++++++++++++++++++++++++++++++------------------------------- 1 file changed, 90 insertions(+), 90 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index f138f1b3..6230edfe 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -304,7 +304,7 @@ sdp_data_t *sdp_data_alloc(uint8_t dtd, const void *value) { sdp_data_t *seq; int len = 0; - sdp_data_t *d = (sdp_data_t *)malloc(sizeof(sdp_data_t)); + sdp_data_t *d = (sdp_data_t *) malloc(sizeof(sdp_data_t)); if (!d) return NULL; @@ -317,36 +317,36 @@ sdp_data_t *sdp_data_alloc(uint8_t dtd, const void *value) case SDP_DATA_NIL: break; case SDP_UINT8: - d->val.uint8 = *(uint8_t *)value; + d->val.uint8 = *(uint8_t *) value; d->unitSize += sizeof(uint8_t); break; case SDP_INT8: case SDP_BOOL: - d->val.int8 = *(int8_t *)value; + d->val.int8 = *(int8_t *) value; d->unitSize += sizeof(int8_t); break; case SDP_UINT16: - d->val.uint16 = sdp_get_unaligned((uint16_t *)value); + d->val.uint16 = bt_get_unaligned((uint16_t *) value); d->unitSize += sizeof(uint16_t); break; case SDP_INT16: - d->val.int16 = sdp_get_unaligned((int16_t *)value); + d->val.int16 = bt_get_unaligned((int16_t *) value); d->unitSize += sizeof(int16_t); break; case SDP_UINT32: - d->val.uint32 = sdp_get_unaligned((uint32_t *)value); + d->val.uint32 = bt_get_unaligned((uint32_t *) value); d->unitSize += sizeof(uint32_t); break; case SDP_INT32: - d->val.int32 = sdp_get_unaligned((int32_t *)value); + d->val.int32 = bt_get_unaligned((int32_t *) value); d->unitSize += sizeof(int32_t); break; case SDP_INT64: - d->val.int64 = sdp_get_unaligned((int64_t *)value); + d->val.int64 = bt_get_unaligned((int64_t *) value); d->unitSize += sizeof(int64_t); break; case SDP_UINT64: - d->val.uint64 = sdp_get_unaligned((uint64_t *)value); + d->val.uint64 = bt_get_unaligned((uint64_t *) value); d->unitSize += sizeof(uint64_t); break; case SDP_UINT128: @@ -358,11 +358,11 @@ sdp_data_t *sdp_data_alloc(uint8_t dtd, const void *value) d->unitSize += sizeof(uint128_t); break; case SDP_UUID16: - sdp_uuid16_create(&d->val.uuid, sdp_get_unaligned((uint16_t *)value)); + sdp_uuid16_create(&d->val.uuid, bt_get_unaligned((uint16_t *) value)); d->unitSize += sizeof(uint16_t); break; case SDP_UUID32: - sdp_uuid32_create(&d->val.uuid, sdp_get_unaligned((uint32_t *)value)); + sdp_uuid32_create(&d->val.uuid, bt_get_unaligned((uint32_t *) value)); d->unitSize += sizeof(uint32_t); break; case SDP_UUID128: @@ -379,7 +379,7 @@ sdp_data_t *sdp_data_alloc(uint8_t dtd, const void *value) len = strlen(value); d->unitSize += len; if (len <= USHRT_MAX) { - d->val.str = (char *)malloc(len + 1); + d->val.str = (char *) malloc(len + 1); if (!d->val.str) goto out_error; @@ -490,26 +490,26 @@ void sdp_attr_remove(sdp_record_t *rec, uint16_t attr) void sdp_set_seq_len(char *ptr, int length) { - uint8_t dtd = *(uint8_t *)ptr++; + uint8_t dtd = *(uint8_t *) ptr++; switch (dtd) { case SDP_SEQ8: case SDP_ALT8: case SDP_TEXT_STR8: case SDP_URL_STR8: - *(uint8_t *)ptr = (uint8_t)length; + *(uint8_t *)ptr = (uint8_t) length; break; case SDP_SEQ16: case SDP_ALT16: case SDP_TEXT_STR16: case SDP_URL_STR16: - sdp_put_unaligned(htons(length), (uint16_t *)ptr); + bt_put_unaligned(htons(length), (uint16_t *) ptr); break; case SDP_SEQ32: case SDP_ALT32: case SDP_TEXT_STR32: case SDP_URL_STR32: - sdp_put_unaligned(htonl(length), (uint32_t *)ptr); + bt_put_unaligned(htonl(length), (uint32_t *) ptr); break; } } @@ -551,7 +551,7 @@ void sdp_set_attrid(sdp_buf_t *buf, uint16_t attr) // data type for attr *p++ = SDP_UINT16; buf->data_size = sizeof(uint8_t); - sdp_put_unaligned(htons(attr), (uint16_t *)p); + bt_put_unaligned(htons(attr), (uint16_t *) p); p += sizeof(uint16_t); buf->data_size += sizeof(uint16_t); } @@ -755,11 +755,11 @@ void sdp_data_free(sdp_data_t *d) static sdp_data_t *extract_int(const void *p, int *len) { - sdp_data_t *d = (sdp_data_t *)malloc(sizeof(sdp_data_t)); + sdp_data_t *d = (sdp_data_t *) malloc(sizeof(sdp_data_t)); SDPDBG("Extracting integer\n"); memset(d, 0, sizeof(sdp_data_t)); - d->dtd = *(uint8_t *)p; + d->dtd = *(uint8_t *) p; p += sizeof(uint8_t); *len += sizeof(uint8_t); @@ -770,27 +770,27 @@ static sdp_data_t *extract_int(const void *p, int *len) case SDP_INT8: case SDP_UINT8: *len += sizeof(uint8_t); - d->val.uint8 = *(uint8_t *)p; + d->val.uint8 = *(uint8_t *) p; break; case SDP_INT16: case SDP_UINT16: *len += sizeof(uint16_t); - d->val.uint16 = ntohs(sdp_get_unaligned((uint16_t *)p)); + d->val.uint16 = ntohs(bt_get_unaligned((uint16_t *) p)); break; case SDP_INT32: case SDP_UINT32: *len += sizeof(uint32_t); - d->val.uint32 = ntohl(sdp_get_unaligned((uint32_t *)p)); + d->val.uint32 = ntohl(bt_get_unaligned((uint32_t *) p)); break; case SDP_INT64: case SDP_UINT64: *len += sizeof(uint64_t); - d->val.uint64 = ntoh64(sdp_get_unaligned((uint64_t *)p)); + d->val.uint64 = ntoh64(bt_get_unaligned((uint64_t *) p)); break; case SDP_INT128: case SDP_UINT128: *len += sizeof(uint128_t); - ntoh128((uint128_t *)p, &d->val.uint128); + ntoh128((uint128_t *) p, &d->val.uint128); break; default: free(d); @@ -801,7 +801,7 @@ static sdp_data_t *extract_int(const void *p, int *len) static sdp_data_t *extract_uuid(const void *p, int *len, sdp_record_t *rec) { - sdp_data_t *d = (sdp_data_t *)malloc(sizeof(sdp_data_t)); + sdp_data_t *d = (sdp_data_t *) malloc(sizeof(sdp_data_t)); SDPDBG("Extracting UUID"); memset(d, 0, sizeof(sdp_data_t)); @@ -809,7 +809,7 @@ static sdp_data_t *extract_uuid(const void *p, int *len, sdp_record_t *rec) free(d); return NULL; } - d->dtd = *(uint8_t *)p; + d->dtd = *(uint8_t *) p; sdp_pattern_add_uuid(rec, &d->val.uuid); return d; } @@ -821,23 +821,23 @@ static sdp_data_t *extract_str(const void *p, int *len) { char *s; int n; - sdp_data_t *d = (sdp_data_t *)malloc(sizeof(sdp_data_t)); + sdp_data_t *d = (sdp_data_t *) malloc(sizeof(sdp_data_t)); memset(d, 0, sizeof(sdp_data_t)); - d->dtd = *(uint8_t *)p; + d->dtd = *(uint8_t *) p; p += sizeof(uint8_t); *len += sizeof(uint8_t); switch (d->dtd) { case SDP_TEXT_STR8: case SDP_URL_STR8: - n = *(uint8_t *)p; + n = *(uint8_t *) p; p += sizeof(uint8_t); *len += sizeof(uint8_t) + n; break; case SDP_TEXT_STR16: case SDP_URL_STR16: - n = ntohs(sdp_get_unaligned((uint16_t *)p)); + n = ntohs(bt_get_unaligned((uint16_t *) p)); p += sizeof(uint16_t); *len += sizeof(uint16_t) + n; break; @@ -984,13 +984,13 @@ sdp_record_t *sdp_extract_pdu(const char *buf, int *scanned) sdp_data_t *data = NULL; SDPDBG("Extract PDU, sequenceLength: %d localExtractedLength: %d", seqlen, extracted); - dtd = *(uint8_t *)p; - attr = ntohs(sdp_get_unaligned((uint16_t *)(p+n))); + dtd = *(uint8_t *) p; + attr = ntohs(bt_get_unaligned((uint16_t *) (p + n))); n += sizeof(uint16_t); SDPDBG("DTD of attrId : %d Attr id : 0x%x \n", dtd, attr); - data = sdp_extract_attr(p+n, &attrlen, rec); + data = sdp_extract_attr(p + n, &attrlen, rec); SDPDBG("Attr id : 0x%x attrValueLength : %d\n", attr, attrlen); @@ -1109,7 +1109,7 @@ sdp_data_t *sdp_data_get(const sdp_record_t *rec, uint16_t attrId) */ int sdp_extract_seqtype(const char *buf, uint8_t *dtdp, int *size) { - uint8_t dtd = *(uint8_t *)buf; + uint8_t dtd = *(uint8_t *) buf; int scanned = sizeof(uint8_t); buf += sizeof(uint8_t); @@ -1117,17 +1117,17 @@ int sdp_extract_seqtype(const char *buf, uint8_t *dtdp, int *size) switch (dtd) { case SDP_SEQ8: case SDP_ALT8: - *size = *(uint8_t *)buf; + *size = *(uint8_t *) buf; scanned += sizeof(uint8_t); break; case SDP_SEQ16: case SDP_ALT16: - *size = ntohs(sdp_get_unaligned((uint16_t *)buf)); + *size = ntohs(bt_get_unaligned((uint16_t *) buf)); scanned += sizeof(uint16_t); break; case SDP_SEQ32: case SDP_ALT32: - *size = ntohl(sdp_get_unaligned((uint32_t *)buf)); + *size = ntohl(bt_get_unaligned((uint32_t *) buf)); scanned += sizeof(uint32_t); break; default: @@ -2136,7 +2136,7 @@ int sdp_uuid_to_proto(uuid_t *uuid) int sdp_uuid_extract(const char *p, uuid_t *uuid, int *scanned) { - uint8_t type = *(const uint8_t *)p; + uint8_t type = *(const uint8_t *) p; if (!SDP_IS_UUID(type)) { SDPERR("Unknown data type : %d expecting a svc UUID\n", type); @@ -2145,11 +2145,11 @@ int sdp_uuid_extract(const char *p, uuid_t *uuid, int *scanned) p += sizeof(uint8_t); *scanned += sizeof(uint8_t); if (type == SDP_UUID16) { - sdp_uuid16_create(uuid, ntohs(sdp_get_unaligned((uint16_t *)p))); + sdp_uuid16_create(uuid, ntohs(bt_get_unaligned((uint16_t *) p))); *scanned += sizeof(uint16_t); p += sizeof(uint16_t); } else if (type == SDP_UUID32) { - sdp_uuid32_create(uuid, ntohl(sdp_get_unaligned((uint32_t *)p))); + sdp_uuid32_create(uuid, ntohl(bt_get_unaligned((uint32_t *) p))); *scanned += sizeof(uint32_t); p += sizeof(uint32_t); } else { @@ -2169,7 +2169,7 @@ int sdp_uuid_extract(const char *p, uuid_t *uuid, int *scanned) void sdp_append_to_buf(sdp_buf_t *dst, char *data, int len) { char *p = dst->data; - uint8_t dtd = *(uint8_t *)p; + uint8_t dtd = *(uint8_t *) p; SDPDBG("Append src size: %d\n", len); SDPDBG("Append dst size: %d\n", dst->data_size); @@ -2203,22 +2203,22 @@ void sdp_append_to_buf(sdp_buf_t *dst, char *data, int len) short offset = sizeof(uint8_t) + sizeof(uint8_t); memmove(dst->data + offset + 1, dst->data + offset, dst->data_size - offset); p = dst->data; - *(uint8_t *)p = SDP_SEQ16; + *(uint8_t *) p = SDP_SEQ16; p += sizeof(uint8_t); dst->data_size += 1; } p = dst->data; - dtd = *(uint8_t *)p; + dtd = *(uint8_t *) p; p += sizeof(uint8_t); switch (dtd) { case SDP_SEQ8: - *(uint8_t *)p = dst->data_size - sizeof(uint8_t) - sizeof(uint8_t); + *(uint8_t *) p = dst->data_size - sizeof(uint8_t) - sizeof(uint8_t); break; case SDP_SEQ16: - sdp_put_unaligned(htons(dst->data_size - sizeof(uint8_t) - sizeof(uint16_t)), (uint16_t *)p); + bt_put_unaligned(htons(dst->data_size - sizeof(uint8_t) - sizeof(uint16_t)), (uint16_t *) p); break; case SDP_SEQ32: - sdp_put_unaligned(htonl(dst->data_size - sizeof(uint8_t) - sizeof(uint32_t)), (uint32_t *)p); + bt_put_unaligned(htonl(dst->data_size - sizeof(uint8_t) - sizeof(uint32_t)), (uint32_t *) p); break; } } @@ -2260,8 +2260,8 @@ int sdp_record_register(sdp_session_t *session, sdp_record_t *rec, uint8_t flags errno = EREMOTE; return -1; } - req = (char *)malloc(SDP_REQ_BUFFER_SIZE); - rsp = (char *)malloc(SDP_RSP_BUFFER_SIZE); + req = (char *) malloc(SDP_REQ_BUFFER_SIZE); + rsp = (char *) malloc(SDP_RSP_BUFFER_SIZE); if (req == NULL || rsp == NULL) { status = -1; errno = ENOMEM; @@ -2286,10 +2286,10 @@ int sdp_record_register(sdp_session_t *session, sdp_record_t *rec, uint8_t flags status = sdp_send_req_w4_rsp(session, req, rsp, reqsize, &rspsize); if (status < 0) goto end; - rsphdr = (sdp_pdu_hdr_t *)rsp; + rsphdr = (sdp_pdu_hdr_t *) rsp; p = rsp + sizeof(sdp_pdu_hdr_t); if (rsphdr->pdu_id == SDP_SVC_REGISTER_RSP) { - uint32_t handle = ntohl(sdp_get_unaligned((uint32_t *)p)); + uint32_t handle = ntohl(bt_get_unaligned((uint32_t *) p)); sdp_data_t *data = sdp_data_alloc(SDP_UINT32, &handle); rec->handle = handle; sdp_attr_replace(rec, SDP_ATTR_RECORD_HANDLE, data); @@ -2325,28 +2325,28 @@ int sdp_record_unregister(sdp_session_t *session, sdp_record_t *rec) errno = EREMOTE; return -1; } - reqbuf = (char *)malloc(SDP_REQ_BUFFER_SIZE); - rspbuf = (char *)malloc(SDP_RSP_BUFFER_SIZE); + reqbuf = (char *) malloc(SDP_REQ_BUFFER_SIZE); + rspbuf = (char *) malloc(SDP_RSP_BUFFER_SIZE); if (!reqbuf || !rspbuf) { errno = ENOMEM; status = -1; goto end; } - reqhdr = (sdp_pdu_hdr_t *)reqbuf; + reqhdr = (sdp_pdu_hdr_t *) reqbuf; reqhdr->pdu_id = SDP_SVC_REMOVE_REQ; reqhdr->tid = htons(sdp_gen_tid(session)); p = reqbuf + sizeof(sdp_pdu_hdr_t); reqsize = sizeof(sdp_pdu_hdr_t); - sdp_put_unaligned(htonl(handle), (uint32_t *)p); + bt_put_unaligned(htonl(handle), (uint32_t *) p); reqsize += sizeof(uint32_t); reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t)); status = sdp_send_req_w4_rsp(session, reqbuf, rspbuf, reqsize, &rspsize); if (status == 0) { - rsphdr = (sdp_pdu_hdr_t *)rspbuf; + rsphdr = (sdp_pdu_hdr_t *) rspbuf; p = rspbuf + sizeof(sdp_pdu_hdr_t); - status = sdp_get_unaligned((uint16_t *)p); + status = bt_get_unaligned((uint16_t *) p); if (status == 0 && rsphdr->pdu_id == SDP_SVC_REMOVE_RSP) { SDPDBG("Removing local copy\n"); sdp_record_free(rec); @@ -2384,21 +2384,21 @@ int sdp_record_update(sdp_session_t *session, const sdp_record_t *rec) errno = EREMOTE; return -1; } - reqbuf = (char *)malloc(SDP_REQ_BUFFER_SIZE); - rspbuf = (char *)malloc(SDP_RSP_BUFFER_SIZE); + reqbuf = (char *) malloc(SDP_REQ_BUFFER_SIZE); + rspbuf = (char *) malloc(SDP_RSP_BUFFER_SIZE); if (!reqbuf || !rspbuf) { errno = ENOMEM; status = -1; goto end; } - reqhdr = (sdp_pdu_hdr_t *)reqbuf; + reqhdr = (sdp_pdu_hdr_t *) reqbuf; reqhdr->pdu_id = SDP_SVC_UPDATE_REQ; reqhdr->tid = htons(sdp_gen_tid(session)); - p = (char *)(reqbuf + sizeof(sdp_pdu_hdr_t)); + p = (char *) (reqbuf + sizeof(sdp_pdu_hdr_t)); reqsize = sizeof(sdp_pdu_hdr_t); - sdp_put_unaligned(htonl(handle), (uint32_t *)p); + bt_put_unaligned(htonl(handle), (uint32_t *) p); reqsize += sizeof(uint32_t); p += sizeof(uint32_t); @@ -2416,9 +2416,9 @@ int sdp_record_update(sdp_session_t *session, const sdp_record_t *rec) SDPDBG("Send req status : %d\n", status); if (status == 0) { - rsphdr = (sdp_pdu_hdr_t *)rspbuf; + rsphdr = (sdp_pdu_hdr_t *) rspbuf; p = rspbuf + sizeof(sdp_pdu_hdr_t); - status = sdp_get_unaligned((uint16_t *)p); + status = bt_get_unaligned((uint16_t *) p); } end: if (reqbuf) @@ -2484,7 +2484,7 @@ static void extract_record_handle_seq(char *pdu, sdp_list_t **seq, int count, in for (n = 0; n < count; n++) { uint32_t *pSvcRec = (uint32_t *) malloc(sizeof(uint32_t)); - *pSvcRec = ntohl(sdp_get_unaligned((uint32_t *)pdata)); + *pSvcRec = ntohl(bt_get_unaligned((uint32_t *) pdata)); pSeq = sdp_list_append(pSeq, pSvcRec); pdata += sizeof(uint32_t); *scanned += sizeof(uint32_t); @@ -2607,7 +2607,7 @@ int sdp_service_search_req(sdp_session_t *session, const sdp_list_t *search, status = -1; goto end; } - reqhdr = (sdp_pdu_hdr_t *)reqbuf; + reqhdr = (sdp_pdu_hdr_t *) reqbuf; reqhdr->pdu_id = SDP_SVC_SEARCH_REQ; pdata = reqbuf + sizeof(sdp_pdu_hdr_t); reqsize = sizeof(sdp_pdu_hdr_t); @@ -2622,7 +2622,7 @@ int sdp_service_search_req(sdp_session_t *session, const sdp_list_t *search, pdata += seqlen; // specify the maximum svc rec count that client expects - sdp_put_unaligned(htons(max_rec_num), (uint16_t *)pdata); + bt_put_unaligned(htons(max_rec_num), (uint16_t *) pdata); reqsize += sizeof(uint16_t); pdata += sizeof(uint16_t); @@ -2647,7 +2647,7 @@ int sdp_service_search_req(sdp_session_t *session, const sdp_list_t *search, goto end; rsplen = 0; - rsphdr = (sdp_pdu_hdr_t *)rspbuf; + rsphdr = (sdp_pdu_hdr_t *) rspbuf; rsplen = ntohs(rsphdr->plen); if (rsphdr->pdu_id == SDP_ERROR_RSP) { @@ -2659,10 +2659,10 @@ int sdp_service_search_req(sdp_session_t *session, const sdp_list_t *search, pdata = rspbuf + sizeof(sdp_pdu_hdr_t); // net service record match count - total_rec_count = ntohs(sdp_get_unaligned((uint16_t *)pdata)); + total_rec_count = ntohs(bt_get_unaligned((uint16_t *) pdata)); pdata += sizeof(uint16_t); scanned += sizeof(uint16_t); - rec_count = ntohs(sdp_get_unaligned((uint16_t *)pdata)); + rec_count = ntohs(bt_get_unaligned((uint16_t *) pdata)); pdata += sizeof(uint16_t); scanned += sizeof(uint16_t); @@ -2681,7 +2681,7 @@ int sdp_service_search_req(sdp_session_t *session, const sdp_list_t *search, uint8_t cstate_len; pdata = rspbuf + sizeof(sdp_pdu_hdr_t) + scanned; - cstate_len = *(uint8_t *)pdata; + cstate_len = *(uint8_t *) pdata; if (cstate_len > 0) { cstate = (sdp_cstate_t *)pdata; SDPDBG("Cont state length: %d\n", cstate_len); @@ -2752,27 +2752,27 @@ sdp_record_t *sdp_service_attr_req(sdp_session_t *session, uint32_t handle, return 0; } - reqbuf = (char *)malloc(SDP_REQ_BUFFER_SIZE); - rspbuf = (char *)malloc(SDP_RSP_BUFFER_SIZE); + reqbuf = (char *) malloc(SDP_REQ_BUFFER_SIZE); + rspbuf = (char *) malloc(SDP_RSP_BUFFER_SIZE); if (!reqbuf || !rspbuf) { errno = ENOMEM; status = -1; goto end; } - memset((char *)&rsp_concat_buf, 0, sizeof(sdp_buf_t)); - reqhdr = (sdp_pdu_hdr_t *)reqbuf; + memset((char *) &rsp_concat_buf, 0, sizeof(sdp_buf_t)); + reqhdr = (sdp_pdu_hdr_t *) reqbuf; reqhdr->pdu_id = SDP_SVC_ATTR_REQ; pdata = reqbuf + sizeof(sdp_pdu_hdr_t); reqsize = sizeof(sdp_pdu_hdr_t); // add the service record handle - sdp_put_unaligned(htonl(handle), (uint32_t *)pdata); + bt_put_unaligned(htonl(handle), (uint32_t *) pdata); reqsize += sizeof(uint32_t); pdata += sizeof(uint32_t); // specify the response limit - sdp_put_unaligned(htons(65535), (uint16_t *)pdata); + bt_put_unaligned(htons(65535), (uint16_t *) pdata); reqsize += sizeof(uint16_t); pdata += sizeof(uint16_t); @@ -2804,19 +2804,19 @@ sdp_record_t *sdp_service_attr_req(sdp_session_t *session, uint32_t handle, if (status < 0) goto end; rsp_count = 0; - rsphdr = (sdp_pdu_hdr_t *)rspbuf; + rsphdr = (sdp_pdu_hdr_t *) rspbuf; if (rsphdr->pdu_id == SDP_ERROR_RSP) { SDPDBG("PDU ID : 0x%x\n", rsphdr->pdu_id); status = -1; goto end; } pdata = rspbuf + sizeof(sdp_pdu_hdr_t); - rsp_count = ntohs(sdp_get_unaligned((uint16_t *)pdata)); + rsp_count = ntohs(bt_get_unaligned((uint16_t *) pdata)); attr_list_len += rsp_count; pdata += sizeof(uint16_t); // if continuation state set need to re-issue request before parsing - cstate_len = *(uint8_t *)(pdata + rsp_count); + cstate_len = *(uint8_t *) (pdata + rsp_count); SDPDBG("Response id : %d\n", rsphdr->pdu_id); SDPDBG("Attrlist byte count : %d\n", rsp_count); @@ -2829,10 +2829,10 @@ sdp_record_t *sdp_service_attr_req(sdp_session_t *session, uint32_t handle, if (cstate_len > 0 || rsp_concat_buf.data_size != 0) { char *targetPtr = NULL; - cstate = cstate_len > 0? (sdp_cstate_t *)(pdata + rsp_count): 0; + cstate = cstate_len > 0 ? (sdp_cstate_t *) (pdata + rsp_count) : 0; // build concatenated response buffer - rsp_concat_buf.data = (char *)realloc(rsp_concat_buf.data, rsp_concat_buf.data_size + rsp_count); + rsp_concat_buf.data = (char *) realloc(rsp_concat_buf.data, rsp_concat_buf.data_size + rsp_count); rsp_concat_buf.buf_size = rsp_concat_buf.data_size + rsp_count; targetPtr = rsp_concat_buf.data + rsp_concat_buf.data_size; memcpy(targetPtr, pdata, rsp_count); @@ -2920,8 +2920,8 @@ int sdp_service_search_attr_req(sdp_session_t *session, const sdp_list_t *search errno = EINVAL; return -1; } - reqbuf = (char *)malloc(SDP_REQ_BUFFER_SIZE); - rspbuf = (char *)malloc(SDP_RSP_BUFFER_SIZE); + reqbuf = (char *) malloc(SDP_REQ_BUFFER_SIZE); + rspbuf = (char *) malloc(SDP_RSP_BUFFER_SIZE); if (!reqbuf || !rspbuf) { errno = ENOMEM; status = -1; @@ -2929,7 +2929,7 @@ int sdp_service_search_attr_req(sdp_session_t *session, const sdp_list_t *search } memset((char *)&rsp_concat_buf, 0, sizeof(sdp_buf_t)); - reqhdr = (sdp_pdu_hdr_t *)reqbuf; + reqhdr = (sdp_pdu_hdr_t *) reqbuf; reqhdr->pdu_id = SDP_SVC_SEARCH_ATTR_REQ; // generate PDU @@ -2945,7 +2945,7 @@ int sdp_service_search_attr_req(sdp_session_t *session, const sdp_list_t *search reqsize += seqlen; pdata += seqlen; - sdp_put_unaligned(htons(SDP_MAX_ATTR_LEN), (uint16_t *)pdata); + bt_put_unaligned(htons(SDP_MAX_ATTR_LEN), (uint16_t *) pdata); reqsize += sizeof(uint16_t); pdata += sizeof(uint16_t); @@ -2953,7 +2953,7 @@ int sdp_service_search_attr_req(sdp_session_t *session, const sdp_list_t *search // get attr seq PDU form seqlen = gen_attridseq_pdu(pdata, attrids, - reqtype == SDP_ATTR_REQ_INDIVIDUAL? SDP_UINT16: SDP_UINT32); + reqtype == SDP_ATTR_REQ_INDIVIDUAL ? SDP_UINT16 : SDP_UINT32); if (seqlen == -1) { status = EINVAL; goto end; @@ -2975,7 +2975,7 @@ int sdp_service_search_attr_req(sdp_session_t *session, const sdp_list_t *search // set the request header's param length reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t)); - rsphdr = (sdp_pdu_hdr_t *)rspbuf; + rsphdr = (sdp_pdu_hdr_t *) rspbuf; status = sdp_send_req_w4_rsp(session, reqbuf, rspbuf, reqsize, &rspsize); if (status < 0) { SDPDBG("Status : 0x%x\n", rsphdr->pdu_id); @@ -2988,10 +2988,10 @@ int sdp_service_search_attr_req(sdp_session_t *session, const sdp_list_t *search } pdata = rspbuf + sizeof(sdp_pdu_hdr_t); - rsp_count = ntohs(sdp_get_unaligned((uint16_t *)pdata)); + rsp_count = ntohs(bt_get_unaligned((uint16_t *) pdata)); attr_list_len += rsp_count; pdata += sizeof(uint16_t); // pdata points to attribute list - cstate_len = *(uint8_t *)(pdata + rsp_count); + cstate_len = *(uint8_t *) (pdata + rsp_count); SDPDBG("Attrlist byte count : %d\n", attr_list_len); SDPDBG("Response byte count : %d\n", rsp_count); @@ -3003,7 +3003,7 @@ int sdp_service_search_attr_req(sdp_session_t *session, const sdp_list_t *search if (cstate_len > 0 || rsp_concat_buf.data_size != 0) { char *targetPtr = NULL; - cstate = cstate_len > 0? (sdp_cstate_t *)(pdata + rsp_count): 0; + cstate = cstate_len > 0 ? (sdp_cstate_t *) (pdata + rsp_count) : 0; // build concatenated response buffer rsp_concat_buf.data = (char *)realloc(rsp_concat_buf.data, rsp_concat_buf.data_size + rsp_count); -- cgit From 654f832d98530b05dc7ffaefff5950a32de76b3c Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sun, 23 Jan 2005 22:49:48 +0000 Subject: Remove sdp_internal.h --- src/sdp.c | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 6230edfe..6f8ce14b 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -38,6 +38,7 @@ #include #include #include +#include #include #include @@ -47,12 +48,55 @@ #include #include #include -#include + +#include + +#define SDPINF(fmt, arg...) syslog(LOG_INFO, fmt "\n", ## arg) +#define SDPERR(fmt, arg...) syslog(LOG_ERR, "%s: " fmt "\n", __func__ , ## arg) + +#ifdef SDP_DEBUG +#define SDPDBG(fmt, arg...) syslog(LOG_DEBUG, "%s: " fmt "\n", __func__ , ## arg) +#else +#define SDPDBG(fmt...) +#endif + +#if __BYTE_ORDER == __BIG_ENDIAN +#define ntoh64(x) (x) +static inline void ntoh128(uint128_t *src, uint128_t *dst) +{ + int i; + for (i = 0; i < 16; i++) + dst->data[i] = src->data[i]; +} +#else +static inline uint64_t ntoh64(uint64_t n) +{ + uint64_t h; + uint64_t tmp = ntohl(n & 0x00000000ffffffff); + h = ntohl(n >> 32); + h |= tmp << 32; + return h; +} +static inline void ntoh128(uint128_t *src, uint128_t *dst) +{ + int i; + for (i = 0; i < 16; i++) + dst->data[15 - i] = src->data[i]; +} +#endif + +#define hton64(x) ntoh64(x) +#define hton128(x, y) ntoh128(x, y) #define BASE_UUID "00000000-0000-1000-8000-00805F9B34FB" static uint128_t *bluetooth_base_uuid = NULL; +#define SDP_BASIC_ATTR_PDUFORM_SIZE 32 +#define SDP_SEQ_PDUFORM_SIZE 128 +#define SDP_UUID_SEQ_SIZE 256 +#define SDP_MAX_ATTR_LEN 65535 + /* Message structure. */ struct tupla { int index; -- cgit From 816b0a911d833aeb2b8af041b55c7b7c66e43588 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Thu, 27 Jan 2005 23:14:46 +0000 Subject: Update the copyright year --- src/sdp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 6f8ce14b..c6494304 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -4,7 +4,7 @@ * * Copyright (C) 2001-2002 Nokia Corporation * Copyright (C) 2002-2003 Maxim Krasnyansky - * Copyright (C) 2002-2004 Marcel Holtmann + * Copyright (C) 2002-2005 Marcel Holtmann * Copyright (C) 2002-2003 Stephen Crane * * -- cgit From 28b4f0aea5b15ed7c0acd17ab79705a38ffe7c2f Mon Sep 17 00:00:00 2001 From: Stephen Crane Date: Mon, 28 Feb 2005 12:19:25 +0000 Subject: fix incorrect doc comment --- src/sdp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index c6494304..82bd996d 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -2629,7 +2629,7 @@ static int copy_cstate(char *pdata, const sdp_cstate_t *cstate) * sdp_list_t **rsp_list * This variable is set on a successful return if there are * non-zero service handles. It is a singly linked list of - * service records (sdp_record_t *) + * service record handles (uint16_t) */ int sdp_service_search_req(sdp_session_t *session, const sdp_list_t *search, uint16_t max_rec_num, sdp_list_t **rsp) -- cgit From 8b11908eac4e0b4e14534ec2d44ae62806fdfd2f Mon Sep 17 00:00:00 2001 From: Stephen Crane Date: Wed, 2 Mar 2005 10:23:16 +0000 Subject: fix comments --- src/sdp.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 82bd996d..d2b40380 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -2286,8 +2286,7 @@ void sdp_append_to_pdu(sdp_buf_t *pdu, sdp_data_t *d) * It is incorrect to call this method on a record that * has been already registered with the server. * - * Returns a non-null value (a pointer) to a service - * record if successful, else -1 setting errno + * Returns zero on success, otherwise -1 (and sets errno). */ int sdp_record_register(sdp_session_t *session, sdp_record_t *rec, uint8_t flags) { -- cgit From 8bd7ef87e49b68c3090448f337454a1704f5e048 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Wed, 9 Mar 2005 17:04:37 +0000 Subject: Add some extra debug information --- src/sdp.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index d2b40380..1e474db4 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -41,6 +41,8 @@ #include #include #include +#include +#include #include #include @@ -1008,7 +1010,9 @@ static void attr_print_func(void *value, void *userData) void sdp_print_service_attr(sdp_list_t *svcAttrList) { + SDPDBG("Printing service attr list %p\n", svcAttrList); sdp_list_foreach(svcAttrList, attr_print_func, NULL); + SDPDBG("Printed service attr list %p\n", svcAttrList); } #endif -- cgit From 0a2a889c91f9b671b7c31bcb6e4e53daf306fb7e Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sun, 8 May 2005 17:18:18 +0000 Subject: Add SDP_WAIT_ON_CLOSE flag for sdp_connect() --- src/sdp.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 1e474db4..93a518cb 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -3167,13 +3167,17 @@ sdp_session_t *sdp_connect(const bdaddr_t *src, const bdaddr_t *dst, uint32_t fl sa.l2_psm = 0; if (bacmp(src, BDADDR_ANY) != 0) { sa.l2_bdaddr = *src; - if (0 > bind(session->sock, (struct sockaddr *)&sa, sizeof(sa))) + if (bind(session->sock, (struct sockaddr *) &sa, sizeof(sa)) < 0) goto fail; } + if (flags & SDP_WAIT_ON_CLOSE) { + struct linger l = { .l_onoff = 1, .l_linger = 1 }; + setsockopt(session->sock, SOL_SOCKET, SO_LINGER, &l, sizeof(l)); + } sa.l2_psm = htobs(SDP_PSM); sa.l2_bdaddr = *dst; do - if (connect(session->sock, (struct sockaddr *)&sa, sizeof(sa)) == 0) + if (connect(session->sock, (struct sockaddr *) &sa, sizeof(sa)) == 0) return session; while (errno == EBUSY && (flags & SDP_RETRY_IF_BUSY)); } -- cgit From cfa3a02e2593bd72ce4a83f521e1e89a5874ea7d Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Wed, 6 Jul 2005 00:12:25 +0000 Subject: Fix more GCC 4.0 warnings --- src/sdp.c | 123 ++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 60 insertions(+), 63 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 93a518cb..84d0a016 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -534,7 +534,7 @@ void sdp_attr_remove(sdp_record_t *rec, uint16_t attr) rec->attrlist = sdp_list_remove(rec->attrlist, d); } -void sdp_set_seq_len(char *ptr, int length) +void sdp_set_seq_len(uint8_t *ptr, uint32_t length) { uint8_t dtd = *(uint8_t *) ptr++; @@ -614,14 +614,14 @@ static int get_data_size(sdp_buf_t *buf, sdp_data_t *sdpdata) int sdp_gen_pdu(sdp_buf_t *buf, sdp_data_t *d) { - int pdu_size = 0, data_size = 0; + uint32_t pdu_size = 0, data_size = 0; unsigned char *src = NULL, is_seq = 0, is_alt = 0; uint8_t dtd = d->dtd; uint16_t u16; uint32_t u32; uint64_t u64; uint128_t u128; - char *seqp = buf->data + buf->data_size; + uint8_t *seqp = buf->data + buf->data_size; pdu_size = sdp_set_data_type(buf, dtd); switch (dtd) { @@ -735,7 +735,7 @@ static void sdp_attr_pdu(void *value, void *udata) int sdp_gen_record_pdu(const sdp_record_t *rec, sdp_buf_t *buf) { - buf->data = (char *)malloc(SDP_PDU_CHUNK_SIZE); + buf->data = malloc(SDP_PDU_CHUNK_SIZE); if (buf->data) { buf->buf_size = SDP_PDU_CHUNK_SIZE; buf->data_size = 0; @@ -845,7 +845,7 @@ static sdp_data_t *extract_int(const void *p, int *len) return d; } -static sdp_data_t *extract_uuid(const void *p, int *len, sdp_record_t *rec) +static sdp_data_t *extract_uuid(const uint8_t *p, int *len, sdp_record_t *rec) { sdp_data_t *d = (sdp_data_t *) malloc(sizeof(sdp_data_t)); @@ -942,7 +942,7 @@ static sdp_data_t *extract_seq(const void *p, int *len, sdp_record_t *rec) return d; } -sdp_data_t *sdp_extract_attr(const char *p, int *size, sdp_record_t *rec) +sdp_data_t *sdp_extract_attr(const uint8_t *p, int *size, sdp_record_t *rec) { sdp_data_t *elem; int n = 0; @@ -1016,13 +1016,13 @@ void sdp_print_service_attr(sdp_list_t *svcAttrList) } #endif -sdp_record_t *sdp_extract_pdu(const char *buf, int *scanned) +sdp_record_t *sdp_extract_pdu(const uint8_t *buf, int *scanned) { int extracted = 0, seqlen = 0; uint8_t dtd; uint16_t attr; sdp_record_t *rec = sdp_record_alloc(); - const char *p = buf; + const uint8_t *p = buf; *scanned = sdp_extract_seqtype(buf, &dtd, &seqlen); p += *scanned; @@ -1155,7 +1155,7 @@ sdp_data_t *sdp_data_get(const sdp_record_t *rec, uint16_t attrId) * Extract the sequence type and its length, and return offset into buf * or 0 on failure. */ -int sdp_extract_seqtype(const char *buf, uint8_t *dtdp, int *size) +int sdp_extract_seqtype(const uint8_t *buf, uint8_t *dtdp, int *size) { uint8_t dtd = *(uint8_t *) buf; int scanned = sizeof(uint8_t); @@ -1185,9 +1185,9 @@ int sdp_extract_seqtype(const char *buf, uint8_t *dtdp, int *size) return scanned; } -int sdp_send_req(sdp_session_t *session, char *buf, int size) +int sdp_send_req(sdp_session_t *session, uint8_t *buf, uint32_t size) { - int sent = 0; + uint32_t sent = 0; while (sent < size) { int n = send(session->sock, buf + sent, size - sent, 0); @@ -1198,7 +1198,7 @@ int sdp_send_req(sdp_session_t *session, char *buf, int size) return 0; } -int sdp_read_rsp(sdp_session_t *session, char *buf, int size) +int sdp_read_rsp(sdp_session_t *session, uint8_t *buf, uint32_t size) { fd_set readFds; struct timeval timeout = { SDP_RESPONSE_TIMEOUT, 0 }; @@ -1216,7 +1216,7 @@ int sdp_read_rsp(sdp_session_t *session, char *buf, int size) /* * generic send request, wait for response method. */ -int sdp_send_req_w4_rsp(sdp_session_t *session, char *reqbuf, char *rspbuf, int reqsize, int *rspsize) +int sdp_send_req_w4_rsp(sdp_session_t *session, uint8_t *reqbuf, uint8_t *rspbuf, uint32_t reqsize, uint32_t *rspsize) { int n; sdp_pdu_hdr_t *reqhdr = (sdp_pdu_hdr_t *)reqbuf; @@ -1952,7 +1952,7 @@ uint128_t *sdp_create_base_uuid(void) char *dataPtr; char temp[10]; int toBeCopied; - char *data; + uint8_t *data; if (bluetooth_base_uuid == NULL) { strcpy(baseStr, BASE_UUID); @@ -2182,7 +2182,7 @@ int sdp_uuid_to_proto(uuid_t *uuid) return 0; } -int sdp_uuid_extract(const char *p, uuid_t *uuid, int *scanned) +int sdp_uuid_extract(const uint8_t *p, uuid_t *uuid, int *scanned) { uint8_t type = *(const uint8_t *) p; @@ -2214,9 +2214,9 @@ int sdp_uuid_extract(const char *p, uuid_t *uuid, int *scanned) * Should the PDU length exceed 2^8, then sequence type is * set accordingly and the data is memmove()'d. */ -void sdp_append_to_buf(sdp_buf_t *dst, char *data, int len) +void sdp_append_to_buf(sdp_buf_t *dst, uint8_t *data, uint32_t len) { - char *p = dst->data; + uint8_t *p = dst->data; uint8_t dtd = *(uint8_t *) p; SDPDBG("Append src size: %d\n", len); @@ -2224,7 +2224,7 @@ void sdp_append_to_buf(sdp_buf_t *dst, char *data, int len) SDPDBG("Dst buffer size: %d\n", dst->buf_size); if (dst->data_size + len > dst->buf_size) { int need = SDP_PDU_CHUNK_SIZE * ((len / SDP_PDU_CHUNK_SIZE) + 1); - dst->data = (char *)realloc(dst->data, dst->buf_size + need); + dst->data = realloc(dst->data, dst->buf_size + need); SDPDBG("Realloc'ing : %d\n", need); @@ -2273,7 +2273,7 @@ void sdp_append_to_buf(sdp_buf_t *dst, char *data, int len) void sdp_append_to_pdu(sdp_buf_t *pdu, sdp_data_t *d) { - char buf[SDP_SEQ_PDUFORM_SIZE]; + uint8_t buf[SDP_SEQ_PDUFORM_SIZE]; sdp_buf_t append; append.data = buf; @@ -2294,10 +2294,9 @@ void sdp_append_to_pdu(sdp_buf_t *pdu, sdp_data_t *d) */ int sdp_record_register(sdp_session_t *session, sdp_record_t *rec, uint8_t flags) { - char *p; int status = 0; - char *req, *rsp; - int reqsize, rspsize; + uint8_t *req, *rsp, *p; + uint32_t reqsize, rspsize; sdp_pdu_hdr_t *reqhdr, *rsphdr; sdp_buf_t pdu; @@ -2307,8 +2306,8 @@ int sdp_record_register(sdp_session_t *session, sdp_record_t *rec, uint8_t flags errno = EREMOTE; return -1; } - req = (char *) malloc(SDP_REQ_BUFFER_SIZE); - rsp = (char *) malloc(SDP_RSP_BUFFER_SIZE); + req = malloc(SDP_REQ_BUFFER_SIZE); + rsp = malloc(SDP_RSP_BUFFER_SIZE); if (req == NULL || rsp == NULL) { status = -1; errno = ENOMEM; @@ -2354,10 +2353,9 @@ end: */ int sdp_record_unregister(sdp_session_t *session, sdp_record_t *rec) { - char *p; int status = 0; - char *reqbuf, *rspbuf; - int reqsize = 0, rspsize = 0; + uint8_t *reqbuf, *rspbuf, *p; + uint32_t reqsize = 0, rspsize = 0; sdp_pdu_hdr_t *reqhdr, *rsphdr; uint32_t handle = 0; @@ -2372,8 +2370,8 @@ int sdp_record_unregister(sdp_session_t *session, sdp_record_t *rec) errno = EREMOTE; return -1; } - reqbuf = (char *) malloc(SDP_REQ_BUFFER_SIZE); - rspbuf = (char *) malloc(SDP_RSP_BUFFER_SIZE); + reqbuf = malloc(SDP_REQ_BUFFER_SIZE); + rspbuf = malloc(SDP_RSP_BUFFER_SIZE); if (!reqbuf || !rspbuf) { errno = ENOMEM; status = -1; @@ -2412,10 +2410,9 @@ end: */ int sdp_record_update(sdp_session_t *session, const sdp_record_t *rec) { - char *p; int status = 0; - char *reqbuf, *rspbuf; - int reqsize, rspsize; + uint8_t *reqbuf, *rspbuf, *p; + uint32_t reqsize, rspsize; sdp_pdu_hdr_t *reqhdr, *rsphdr; uint32_t handle; sdp_buf_t pdu; @@ -2431,8 +2428,8 @@ int sdp_record_update(sdp_session_t *session, const sdp_record_t *rec) errno = EREMOTE; return -1; } - reqbuf = (char *) malloc(SDP_REQ_BUFFER_SIZE); - rspbuf = (char *) malloc(SDP_RSP_BUFFER_SIZE); + reqbuf = malloc(SDP_REQ_BUFFER_SIZE); + rspbuf = malloc(SDP_RSP_BUFFER_SIZE); if (!reqbuf || !rspbuf) { errno = ENOMEM; status = -1; @@ -2442,7 +2439,7 @@ int sdp_record_update(sdp_session_t *session, const sdp_record_t *rec) reqhdr->pdu_id = SDP_SVC_UPDATE_REQ; reqhdr->tid = htons(sdp_gen_tid(session)); - p = (char *) (reqbuf + sizeof(sdp_pdu_hdr_t)); + p = reqbuf + sizeof(sdp_pdu_hdr_t); reqsize = sizeof(sdp_pdu_hdr_t); bt_put_unaligned(htonl(handle), (uint32_t *) p); @@ -2523,10 +2520,10 @@ void sdp_pattern_add_uuidseq(sdp_record_t *rec, sdp_list_t *seq) * handles are not in "data element sequence" form, but just like * an array of service handles */ -static void extract_record_handle_seq(char *pdu, sdp_list_t **seq, int count, int *scanned) +static void extract_record_handle_seq(uint8_t *pdu, sdp_list_t **seq, int count, int *scanned) { sdp_list_t *pSeq = *seq; - char *pdata = pdu; + uint8_t *pdata = pdu; int n; for (n = 0; n < count; n++) { @@ -2542,7 +2539,7 @@ static void extract_record_handle_seq(char *pdu, sdp_list_t **seq, int count, in * Generate the attribute sequence pdu form * from sdp_list_t elements. Return length of attr seq */ -static int gen_dataseq_pdu(char *dst, const sdp_list_t *seq, uint8_t dtd) +static int gen_dataseq_pdu(uint8_t *dst, const sdp_list_t *seq, uint8_t dtd) { sdp_data_t *dataseq; void **types, **values; @@ -2553,7 +2550,7 @@ static int gen_dataseq_pdu(char *dst, const sdp_list_t *seq, uint8_t dtd) SDPDBG(""); memset(&buf, 0, sizeof(sdp_buf_t)); - buf.data = (char *)malloc(SDP_UUID_SEQ_SIZE); + buf.data = malloc(SDP_UUID_SEQ_SIZE); buf.buf_size = SDP_UUID_SEQ_SIZE; SDPDBG("Seq length : %d\n", seqlen); @@ -2583,18 +2580,18 @@ static int gen_dataseq_pdu(char *dst, const sdp_list_t *seq, uint8_t dtd) return seqlen; } -static int gen_searchseq_pdu(char *dst, const sdp_list_t *seq) +static int gen_searchseq_pdu(uint8_t *dst, const sdp_list_t *seq) { - uuid_t *uuid = (uuid_t *)seq->data; + uuid_t *uuid = (uuid_t *) seq->data; return gen_dataseq_pdu(dst, seq, uuid->type); } -static int gen_attridseq_pdu(char *dst, const sdp_list_t *seq, uint8_t dataType) +static int gen_attridseq_pdu(uint8_t *dst, const sdp_list_t *seq, uint8_t dataType) { return gen_dataseq_pdu(dst, seq, dataType); } -static int copy_cstate(char *pdata, const sdp_cstate_t *cstate) +static int copy_cstate(uint8_t *pdata, const sdp_cstate_t *cstate) { if (cstate) { *pdata++ = cstate->length; @@ -2638,12 +2635,12 @@ int sdp_service_search_req(sdp_session_t *session, const sdp_list_t *search, uint16_t max_rec_num, sdp_list_t **rsp) { int status = 0; - int reqsize = 0, _reqsize; - int rspsize = 0, rsplen; + uint32_t reqsize = 0, _reqsize; + uint32_t rspsize = 0, rsplen; int seqlen = 0; int scanned, total_rec_count, rec_count; - char *pdata, *_pdata; - char *reqbuf, *rspbuf; + uint8_t *pdata, *_pdata; + uint8_t *reqbuf, *rspbuf; sdp_pdu_hdr_t *reqhdr, *rsphdr; sdp_cstate_t *cstate = NULL; @@ -2782,12 +2779,12 @@ sdp_record_t *sdp_service_attr_req(sdp_session_t *session, uint32_t handle, sdp_attrreq_type_t reqtype, const sdp_list_t *attrids) { int status = 0; - int reqsize = 0, _reqsize; - int rspsize = 0, rsp_count; + uint32_t reqsize = 0, _reqsize; + uint32_t rspsize = 0, rsp_count; int attr_list_len = 0; int seqlen = 0; - char *pdata, *_pdata; - char *reqbuf, *rspbuf; + uint8_t *pdata, *_pdata; + uint8_t *reqbuf, *rspbuf; sdp_pdu_hdr_t *reqhdr, *rsphdr; sdp_cstate_t *cstate = NULL; uint8_t cstate_len = 0; @@ -2799,8 +2796,8 @@ sdp_record_t *sdp_service_attr_req(sdp_session_t *session, uint32_t handle, return 0; } - reqbuf = (char *) malloc(SDP_REQ_BUFFER_SIZE); - rspbuf = (char *) malloc(SDP_RSP_BUFFER_SIZE); + reqbuf = malloc(SDP_REQ_BUFFER_SIZE); + rspbuf = malloc(SDP_RSP_BUFFER_SIZE); if (!reqbuf || !rspbuf) { errno = ENOMEM; status = -1; @@ -2874,12 +2871,12 @@ sdp_record_t *sdp_service_attr_req(sdp_session_t *session, uint32_t handle, * and the last one (which has cstate_len == 0) */ if (cstate_len > 0 || rsp_concat_buf.data_size != 0) { - char *targetPtr = NULL; + uint8_t *targetPtr = NULL; cstate = cstate_len > 0 ? (sdp_cstate_t *) (pdata + rsp_count) : 0; // build concatenated response buffer - rsp_concat_buf.data = (char *) realloc(rsp_concat_buf.data, rsp_concat_buf.data_size + rsp_count); + rsp_concat_buf.data = realloc(rsp_concat_buf.data, rsp_concat_buf.data_size + rsp_count); rsp_concat_buf.buf_size = rsp_concat_buf.data_size + rsp_count; targetPtr = rsp_concat_buf.data + rsp_concat_buf.data_size; memcpy(targetPtr, pdata, rsp_count); @@ -2951,12 +2948,12 @@ sdp_record_t *sdp_service_attr_req(sdp_session_t *session, uint32_t handle, int sdp_service_search_attr_req(sdp_session_t *session, const sdp_list_t *search, sdp_attrreq_type_t reqtype, const sdp_list_t *attrids, sdp_list_t **rsp) { int status = 0; - int reqsize = 0, _reqsize; - int rspsize = 0; + uint32_t reqsize = 0, _reqsize; + uint32_t rspsize = 0; int seqlen = 0, attr_list_len = 0; int rsp_count = 0, cstate_len = 0; - char *pdata, *_pdata; - char *reqbuf, *rspbuf; + uint8_t *pdata, *_pdata; + uint8_t *reqbuf, *rspbuf; sdp_pdu_hdr_t *reqhdr, *rsphdr; uint8_t dataType; sdp_list_t *rec_list = NULL; @@ -2967,8 +2964,8 @@ int sdp_service_search_attr_req(sdp_session_t *session, const sdp_list_t *search errno = EINVAL; return -1; } - reqbuf = (char *) malloc(SDP_REQ_BUFFER_SIZE); - rspbuf = (char *) malloc(SDP_RSP_BUFFER_SIZE); + reqbuf = malloc(SDP_REQ_BUFFER_SIZE); + rspbuf = malloc(SDP_RSP_BUFFER_SIZE); if (!reqbuf || !rspbuf) { errno = ENOMEM; status = -1; @@ -3048,12 +3045,12 @@ int sdp_service_search_attr_req(sdp_session_t *session, const sdp_list_t *search * responses and the last one which will have cstate_len == 0 */ if (cstate_len > 0 || rsp_concat_buf.data_size != 0) { - char *targetPtr = NULL; + uint8_t *targetPtr = NULL; cstate = cstate_len > 0 ? (sdp_cstate_t *) (pdata + rsp_count) : 0; // build concatenated response buffer - rsp_concat_buf.data = (char *)realloc(rsp_concat_buf.data, rsp_concat_buf.data_size + rsp_count); + rsp_concat_buf.data = realloc(rsp_concat_buf.data, rsp_concat_buf.data_size + rsp_count); targetPtr = rsp_concat_buf.data + rsp_concat_buf.data_size; rsp_concat_buf.buf_size = rsp_concat_buf.data_size + rsp_count; memcpy(targetPtr, pdata, rsp_count); -- cgit From a1454c0f12421da1849ca347f8a30d084778d679 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Wed, 3 Aug 2005 07:18:31 +0000 Subject: Create per device SDP server functions --- src/sdp.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 84d0a016..4c1117e1 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -2292,7 +2292,7 @@ void sdp_append_to_pdu(sdp_buf_t *pdu, sdp_data_t *d) * * Returns zero on success, otherwise -1 (and sets errno). */ -int sdp_record_register(sdp_session_t *session, sdp_record_t *rec, uint8_t flags) +int sdp_device_record_register(sdp_session_t *session, bdaddr_t *device, sdp_record_t *rec, uint8_t flags) { int status = 0; uint8_t *req, *rsp, *p; @@ -2348,10 +2348,15 @@ end: return status; } +int sdp_record_register(sdp_session_t *session, sdp_record_t *rec, uint8_t flags) +{ + return sdp_device_record_register(session, BDADDR_ANY, rec, flags); +} + /* * unregister a service record */ -int sdp_record_unregister(sdp_session_t *session, sdp_record_t *rec) +int sdp_device_record_unregister(sdp_session_t *session, bdaddr_t *device, sdp_record_t *rec) { int status = 0; uint8_t *reqbuf, *rspbuf, *p; @@ -2405,10 +2410,15 @@ end: return status; } +int sdp_record_unregister(sdp_session_t *session, sdp_record_t *rec) +{ + return sdp_device_record_unregister(session, BDADDR_ANY, rec); +} + /* * modify an existing service record */ -int sdp_record_update(sdp_session_t *session, const sdp_record_t *rec) +int sdp_device_record_update(sdp_session_t *session, bdaddr_t *device, const sdp_record_t *rec) { int status = 0; uint8_t *reqbuf, *rspbuf, *p; @@ -2472,6 +2482,11 @@ end: return status; } +int sdp_record_update(sdp_session_t *session, const sdp_record_t *rec) +{ + return sdp_device_record_update(session, BDADDR_ANY, rec); +} + sdp_record_t *sdp_record_alloc() { sdp_record_t *rec = (sdp_record_t *)malloc(sizeof(sdp_record_t)); -- cgit From fb1557c4111ca9993e4927af6f65bdbf9c2fa8ec Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Wed, 3 Aug 2005 07:41:21 +0000 Subject: Add device specific record register --- src/sdp.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 4c1117e1..22fcd362 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -2318,8 +2318,13 @@ int sdp_device_record_register(sdp_session_t *session, bdaddr_t *device, sdp_rec reqhdr->tid = htons(sdp_gen_tid(session)); reqsize = sizeof(sdp_pdu_hdr_t) + 1; p = req + sizeof(sdp_pdu_hdr_t); - *p++ = flags; - if (0 > sdp_gen_record_pdu(rec, &pdu)) { + if (bacmp(device, BDADDR_ANY)) { + *p++ = flags | SDP_DEVICE_RECORD; + bacpy((bdaddr_t *) p, device); + p += sizeof(bdaddr_t); + } else + *p++ = flags; + if (sdp_gen_record_pdu(rec, &pdu) < 0) { status = -1; errno = ENOMEM; goto end; -- cgit From 6db62cfa3a8de69270cc5e5689b633f43c02e041 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Wed, 3 Aug 2005 07:45:45 +0000 Subject: Fix request size calculation for device specific register --- src/sdp.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 22fcd362..d920a753 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -2322,6 +2322,7 @@ int sdp_device_record_register(sdp_session_t *session, bdaddr_t *device, sdp_rec *p++ = flags | SDP_DEVICE_RECORD; bacpy((bdaddr_t *) p, device); p += sizeof(bdaddr_t); + reqsize += sizeof(bdaddr_t); } else *p++ = flags; if (sdp_gen_record_pdu(rec, &pdu) < 0) { -- cgit From 2fa38208982e7c7bb55ae97acd13dbf82dd24e88 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sat, 6 Aug 2005 17:43:48 +0000 Subject: Add UUID for video distribution --- src/sdp.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index d920a753..c48d4ba7 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -192,6 +192,7 @@ static struct tupla ServiceClass[] = { { UPNP_L2CAP_SVCLASS_ID, "UPnP L2CAP" }, { VIDEO_SOURCE_SVCLASS_ID, "Video Source" }, { VIDEO_SINK_SVCLASS_ID, "Video Sink" }, + { VIDEO_DISTRIBUTION_SVCLASS_ID, "Video Distribution" }, { 0 } }; -- cgit From 8ce8108e712e40e1b9530c96f7ff6cbb32b5cb8b Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sun, 7 Aug 2005 11:19:57 +0000 Subject: Add UUIDs for phonebook access profile --- src/sdp.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index c48d4ba7..0198f9ec 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -180,6 +180,8 @@ static struct tupla ServiceClass[] = { { UDI_TA_SVCLASS_ID, "UDI TA" }, { AV_SVCLASS_ID, "Audio/Video" }, { SAP_SVCLASS_ID, "SIM Access" }, + { PBAP_PCE_SVCLASS_ID, "Phonebook Access - PCE" }, + { PBAP_PSE_SVCLASS_ID, "Phonebook Access - PSE" }, { PNP_INFO_SVCLASS_ID, "PnP Information" }, { GENERIC_NETWORKING_SVCLASS_ID, "Generic Networking" }, { GENERIC_FILETRANS_SVCLASS_ID, "Generic File Transfer" }, -- cgit From 12862715c961bf6dabce2ed4e58da58e5dfe8c6b Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Thu, 22 Sep 2005 23:10:48 +0000 Subject: Fix errno overwrite problems --- src/sdp.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 0198f9ec..56c74d49 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -3160,6 +3160,7 @@ static inline int sdp_is_local(const bdaddr_t *device) sdp_session_t *sdp_connect(const bdaddr_t *src, const bdaddr_t *dst, uint32_t flags) { + int err; sdp_session_t *session = malloc(sizeof(sdp_session_t)); if (!session) return session; @@ -3204,8 +3205,10 @@ sdp_session_t *sdp_connect(const bdaddr_t *src, const bdaddr_t *dst, uint32_t fl } } fail: + err = errno; if (session->sock >= 0) close(session->sock); free(session); + errno = err; return 0; } -- cgit From 0f5a5a9580084a3c4e0644ef5cd75689aeb5ff40 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Fri, 30 Sep 2005 14:43:37 +0000 Subject: Fix profile descriptor problem with Samsung phones --- src/sdp.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 56c74d49..e0df7582 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -1529,12 +1529,24 @@ int sdp_get_profile_descs(const sdp_record_t *rec, sdp_list_t **profDescSeq) return -1; } for (seq = sdpdata->val.dataseq; seq && seq->val.dataseq; seq = seq->next) { - sdp_data_t *uuid = seq->val.dataseq; - sdp_data_t *pVnum = seq->val.dataseq->next; - if (uuid && pVnum) { + uuid_t *uuid = NULL; + uint16_t version = 0x100; + + if (SDP_IS_UUID(seq->dtd)) { + uuid = &seq->val.uuid; + } else { + sdp_data_t *puuid = seq->val.dataseq; + sdp_data_t *pVnum = seq->val.dataseq->next; + if (puuid && pVnum) { + uuid = &puuid->val.uuid; + version = pVnum->val.uint16; + } + } + + if (uuid != NULL) { profDesc = (sdp_profile_desc_t *)malloc(sizeof(sdp_profile_desc_t)); - profDesc->uuid = uuid->val.uuid; - profDesc->version = pVnum->val.uint16; + profDesc->uuid = *uuid; + profDesc->version = version; #ifdef SDP_DEBUG sdp_uuid_print(&profDesc->uuid); SDPDBG("Vnum : 0x%04x\n", profDesc->version); -- cgit From c0d524486a50e8366c12c5ebea1a4441e9db46aa Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sat, 29 Oct 2005 19:25:42 +0000 Subject: Big cleanup of CVS relics --- src/sdp.c | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index e0df7582..57da1df9 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -9,24 +9,19 @@ * * * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation; + * 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. * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. - * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY - * CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * 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. * - * ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, - * COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS - * SOFTWARE IS DISCLAIMED. + * 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 St, Fifth Floor, Boston, MA 02110-1301 USA * - * - * $Id$ */ #ifdef HAVE_CONFIG_H -- cgit From 8db4e616c0107df21e3cacd981d073a64414de77 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Thu, 24 Nov 2005 04:19:49 +0000 Subject: Fix service discovery deadlocks with Samsung D600 phones --- src/sdp.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 57da1df9..67a20008 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -3115,6 +3115,10 @@ int sdp_service_search_attr_req(sdp_session_t *session, const sdp_list_t *search status = -1; goto end; } + if (!recsize) { + sdp_record_free(rec); + break; + } scanned += recsize; pdata += recsize; -- cgit From 641d636fb24fb0dea567388a4f0d0f135d4ea48c Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Fri, 9 Dec 2005 09:12:44 +0000 Subject: Add support for allocation of binary text elements --- src/sdp.c | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 89 insertions(+), 21 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 67a20008..9f3f79e1 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -344,10 +344,9 @@ void sdp_uuid_print(const uuid_t *uuid) } #endif -sdp_data_t *sdp_data_alloc(uint8_t dtd, const void *value) +sdp_data_t *sdp_data_alloc_with_length(uint8_t dtd, const void *value, uint32_t length) { sdp_data_t *seq; - int len = 0; sdp_data_t *d = (sdp_data_t *) malloc(sizeof(sdp_data_t)); if (!d) @@ -414,21 +413,25 @@ sdp_data_t *sdp_data_alloc(uint8_t dtd, const void *value) d->unitSize += sizeof(uint128_t); break; case SDP_URL_STR8: - case SDP_TEXT_STR8: case SDP_URL_STR16: + case SDP_TEXT_STR8: case SDP_TEXT_STR16: - if (!value) - goto out_error; + if (!value) { + free(d); + return NULL; + } + + d->unitSize += length; + if (length <= USHRT_MAX) { + d->val.str = malloc(length); + if (!d->val.str) { + free(d); + return NULL; + } - len = strlen(value); - d->unitSize += len; - if (len <= USHRT_MAX) { - d->val.str = (char *) malloc(len + 1); - if (!d->val.str) - goto out_error; + memcpy(d->val.str, value, length); - strcpy(d->val.str, value); - if (len <= UCHAR_MAX) { + if (length <= UCHAR_MAX) { d->unitSize += sizeof(uint8_t); if (dtd != SDP_URL_STR8 && dtd != SDP_TEXT_STR8) { if (dtd == SDP_URL_STR16) @@ -445,7 +448,8 @@ sdp_data_t *sdp_data_alloc(uint8_t dtd, const void *value) } } else { SDPERR("Strings of size > USHRT_MAX not supported\n"); - goto out_error; + free(d); + d = NULL; } break; case SDP_URL_STR32: @@ -470,13 +474,33 @@ sdp_data_t *sdp_data_alloc(uint8_t dtd, const void *value) d->unitSize += seq->unitSize; break; default: - goto out_error; + free(d); + d = NULL; } + return d; +} -out_error: - free(d); - return NULL; +sdp_data_t *sdp_data_alloc(uint8_t dtd, const void *value) +{ + uint32_t length; + + switch (dtd) { + case SDP_URL_STR8: + case SDP_URL_STR16: + case SDP_TEXT_STR8: + case SDP_TEXT_STR16: + if (!value) + return NULL; + + length = strlen((char *) value); + break; + default: + length = 0; + break; + } + + return sdp_data_alloc_with_length(dtd, value, length); } sdp_data_t *sdp_seq_append(sdp_data_t *seq, sdp_data_t *d) @@ -491,6 +515,34 @@ sdp_data_t *sdp_seq_append(sdp_data_t *seq, sdp_data_t *d) return seq; } +sdp_data_t *sdp_seq_alloc_with_length(void **dtds, void **values, int *length, int len) +{ + sdp_data_t *curr = NULL, *seq = NULL; + int i; + + for (i = 0; i < len; i++) { + sdp_data_t *data; + int8_t dtd = *(uint8_t *) dtds[i]; + + if (dtd >= SDP_SEQ8 && dtd <= SDP_ALT32) + data = (sdp_data_t *) values[i]; + else + data = sdp_data_alloc_with_length(dtd, values[i], length[i]); + + if (!data) + return NULL; + + if (curr) + curr->next = data; + else + seq = data; + + curr = data; + } + + return sdp_data_alloc_with_length(SDP_SEQ8, seq, length[i]); +} + sdp_data_t *sdp_seq_alloc(void **dtds, void **values, int len) { sdp_data_t *curr = NULL, *seq = NULL; @@ -498,19 +550,24 @@ sdp_data_t *sdp_seq_alloc(void **dtds, void **values, int len) for (i = 0; i < len; i++) { sdp_data_t *data; - uint8_t dtd = *(uint8_t *)dtds[i]; + uint8_t dtd = *(uint8_t *) dtds[i]; + if (dtd >= SDP_SEQ8 && dtd <= SDP_ALT32) - data = (sdp_data_t *)values[i]; + data = (sdp_data_t *) values[i]; else data = sdp_data_alloc(dtd, values[i]); + if (!data) return NULL; + if (curr) curr->next = data; else seq = data; + curr = data; } + return sdp_data_alloc(SDP_SEQ8, seq); } @@ -565,6 +622,7 @@ int sdp_set_data_type(sdp_buf_t *buf, uint8_t dtd) *p++ = dtd; buf->data_size += sizeof(uint8_t); + switch (dtd) { case SDP_SEQ8: case SDP_TEXT_STR8: @@ -585,6 +643,7 @@ int sdp_set_data_type(sdp_buf_t *buf, uint8_t dtd) buf->data_size += sizeof(uint32_t); break; } + return buf->data_size - orig; } @@ -607,6 +666,7 @@ static int get_data_size(sdp_buf_t *buf, sdp_data_t *sdpdata) for (d = sdpdata->val.dataseq; d; d = d->next) n += sdp_gen_pdu(buf, d); + return n; } @@ -622,6 +682,7 @@ int sdp_gen_pdu(sdp_buf_t *buf, sdp_data_t *d) uint8_t *seqp = buf->data + buf->data_size; pdu_size = sdp_set_data_type(buf, dtd); + switch (dtd) { case SDP_DATA_NIL: break; @@ -675,9 +736,13 @@ int sdp_gen_pdu(sdp_buf_t *buf, sdp_data_t *d) data_size = sizeof(uint128_t); break; case SDP_TEXT_STR8: - case SDP_URL_STR8: case SDP_TEXT_STR16: case SDP_TEXT_STR32: + src = (unsigned char *)d->val.str; + data_size = d->unitSize - sizeof(uint8_t); + sdp_set_seq_len(seqp, data_size); + break; + case SDP_URL_STR8: case SDP_URL_STR16: case SDP_URL_STR32: src = (unsigned char *)d->val.str; @@ -715,6 +780,7 @@ int sdp_gen_pdu(sdp_buf_t *buf, sdp_data_t *d) default: break; } + if (!is_seq && !is_alt) { if (src && buf) { memcpy(buf->data + buf->data_size, src, data_size); @@ -722,7 +788,9 @@ int sdp_gen_pdu(sdp_buf_t *buf, sdp_data_t *d) } else if (dtd != SDP_DATA_NIL) SDPDBG("Gen PDU : Cant copy from NULL source or dest\n"); } + pdu_size += data_size; + return pdu_size; } -- cgit From 73fa16fccbb85592973068a25949d4c331f7f475 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Fri, 9 Dec 2005 09:39:16 +0000 Subject: Add FD_ZERO() before usage of FD_SET() --- src/sdp.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 9f3f79e1..90bc447e 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -1269,9 +1269,10 @@ int sdp_read_rsp(sdp_session_t *session, uint8_t *buf, uint32_t size) fd_set readFds; struct timeval timeout = { SDP_RESPONSE_TIMEOUT, 0 }; + FD_ZERO(&readFds); FD_SET(session->sock, &readFds); SDPDBG("Waiting for response\n"); - if (0 == select(session->sock + 1, &readFds, NULL, NULL, &timeout)) { + if (select(session->sock + 1, &readFds, NULL, NULL, &timeout) == 0) { SDPERR("Client timed out\n"); errno = ETIMEDOUT; return -1; -- cgit From aa8d087a052ab9c54dde1b0ddaf3a47494ef899a Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Wed, 14 Dec 2005 10:11:26 +0000 Subject: Add support for additional access protocols --- src/sdp.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 90bc447e..28c08679 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -1812,13 +1812,31 @@ static sdp_data_t *access_proto_to_dataseq(sdp_record_t *rec, sdp_list_t *proto) int sdp_set_access_protos(sdp_record_t *rec, const sdp_list_t *ap) { const sdp_list_t *p; - sdp_data_t *protos = 0; + sdp_data_t *protos = NULL; for (p = ap; p; p = p->next) { - sdp_data_t *seq = access_proto_to_dataseq(rec, (sdp_list_t *)p->data); + sdp_data_t *seq = access_proto_to_dataseq(rec, (sdp_list_t *) p->data); protos = sdp_seq_append(protos, seq); } + sdp_attr_add(rec, SDP_ATTR_PROTO_DESC_LIST, protos); + + return 0; +} + +int sdp_set_add_access_protos(sdp_record_t *rec, const sdp_list_t *ap) +{ + const sdp_list_t *p; + sdp_data_t *protos = NULL; + + for (p = ap; p; p = p->next) { + sdp_data_t *seq = access_proto_to_dataseq(rec, (sdp_list_t *) p->data); + protos = sdp_seq_append(protos, seq); + } + + sdp_attr_add(rec, SDP_ATTR_ADD_PROTO_DESC_LIST, + protos ? sdp_data_alloc(SDP_SEQ8, protos) : NULL); + return 0; } -- cgit From 197a2aee34d9a1643cd474f8f167552ca6395d01 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Tue, 3 Jan 2006 12:56:09 +0000 Subject: Update copyright information --- src/sdp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 28c08679..da83c23e 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -4,7 +4,7 @@ * * Copyright (C) 2001-2002 Nokia Corporation * Copyright (C) 2002-2003 Maxim Krasnyansky - * Copyright (C) 2002-2005 Marcel Holtmann + * Copyright (C) 2002-2006 Marcel Holtmann * Copyright (C) 2002-2003 Stephen Crane * * -- cgit From 42e0ce5b430ac5e36c0d21bdddcaf1fe83e025f5 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Wed, 4 Jan 2006 00:04:30 +0000 Subject: Add definitions for Apple Agent --- src/sdp.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index da83c23e..0421e699 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -190,6 +190,7 @@ static struct tupla ServiceClass[] = { { VIDEO_SOURCE_SVCLASS_ID, "Video Source" }, { VIDEO_SINK_SVCLASS_ID, "Video Sink" }, { VIDEO_DISTRIBUTION_SVCLASS_ID, "Video Distribution" }, + { APPLE_AGENT_SVCLASS_ID, "Apple Agent" }, { 0 } }; -- cgit From 49fe626b5c153e4bc6a174982a89e9b3b2e4baf2 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Tue, 17 Jan 2006 10:39:37 +0000 Subject: Add support for setting the record handle on service registration --- src/sdp.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 0421e699..2510ccf3 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -2411,6 +2411,11 @@ int sdp_device_record_register(sdp_session_t *session, bdaddr_t *device, sdp_rec errno = ENOMEM; goto end; } + if (rec->handle && rec->handle != 0xffffffff) { + uint32_t handle = rec->handle; + sdp_data_t *data = sdp_data_alloc(SDP_UINT32, &handle); + sdp_attr_replace(rec, SDP_ATTR_RECORD_HANDLE, data); + } reqhdr = (sdp_pdu_hdr_t *)req; reqhdr->pdu_id = SDP_SVC_REGISTER_REQ; reqhdr->tid = htons(sdp_gen_tid(session)); -- cgit From 73f2fe893f6801358ea4fb88dc8d24050c7d0ca5 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Tue, 30 May 2006 10:58:30 +0000 Subject: Add another Phonebook Access identifier --- src/sdp.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 2510ccf3..58f7b269 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -177,6 +177,7 @@ static struct tupla ServiceClass[] = { { SAP_SVCLASS_ID, "SIM Access" }, { PBAP_PCE_SVCLASS_ID, "Phonebook Access - PCE" }, { PBAP_PSE_SVCLASS_ID, "Phonebook Access - PSE" }, + { PBAP_SVCLASS_ID, "Phonebook Access" }, { PNP_INFO_SVCLASS_ID, "PnP Information" }, { GENERIC_NETWORKING_SVCLASS_ID, "Generic Networking" }, { GENERIC_FILETRANS_SVCLASS_ID, "Generic File Transfer" }, -- cgit From 1b9e5873f730b50987c494c7397b40ac614ba62d Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Fri, 9 Jun 2006 15:45:08 +0000 Subject: Fix sdp_gen_pdu data_size computation --- src/sdp.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 58f7b269..abefca98 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -742,6 +742,10 @@ int sdp_gen_pdu(sdp_buf_t *buf, sdp_data_t *d) case SDP_TEXT_STR32: src = (unsigned char *)d->val.str; data_size = d->unitSize - sizeof(uint8_t); + if (data_size - sizeof(uint8_t) <= UCHAR_MAX) + data_size -= sizeof(uint8_t); + else + data_size -= sizeof(uint16_t); sdp_set_seq_len(seqp, data_size); break; case SDP_URL_STR8: -- cgit From 0ed4f8572e1b32b4dd57304db14351f0e0c59119 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Mon, 26 Jun 2006 12:44:31 +0000 Subject: Fix handling of strings --- src/sdp.c | 22 +--------------------- 1 file changed, 1 insertion(+), 21 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index abefca98..fd743b60 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -432,22 +432,6 @@ sdp_data_t *sdp_data_alloc_with_length(uint8_t dtd, const void *value, uint32_t } memcpy(d->val.str, value, length); - - if (length <= UCHAR_MAX) { - d->unitSize += sizeof(uint8_t); - if (dtd != SDP_URL_STR8 && dtd != SDP_TEXT_STR8) { - if (dtd == SDP_URL_STR16) - dtd = SDP_URL_STR8; - else - dtd = SDP_TEXT_STR8; - } - } else { - d->unitSize += sizeof(uint16_t); - if (dtd == SDP_TEXT_STR8) - dtd = SDP_TEXT_STR16; - else - dtd = SDP_URL_STR16; - } } else { SDPERR("Strings of size > USHRT_MAX not supported\n"); free(d); @@ -742,10 +726,6 @@ int sdp_gen_pdu(sdp_buf_t *buf, sdp_data_t *d) case SDP_TEXT_STR32: src = (unsigned char *)d->val.str; data_size = d->unitSize - sizeof(uint8_t); - if (data_size - sizeof(uint8_t) <= UCHAR_MAX) - data_size -= sizeof(uint8_t); - else - data_size -= sizeof(uint16_t); sdp_set_seq_len(seqp, data_size); break; case SDP_URL_STR8: @@ -973,7 +953,7 @@ static sdp_data_t *extract_str(const void *p, int *len) SDPDBG("Str : %s\n", s); d->val.str = s; - d->unitSize = n; + d->unitSize = n + sizeof(uint8_t); return d; } -- cgit From 8f6ea7c3d24dc21f120b6d29fc975936b4d4f3b7 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Thu, 17 Aug 2006 21:59:25 +0000 Subject: Add additional private data field --- src/sdp.c | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index fd743b60..edde4dbf 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -3301,3 +3301,13 @@ fail: errno = err; return 0; } + +int sdp_get_socket(const sdp_session_t *session) +{ + return session->sock; +} + +uint16_t sdp_gen_tid(sdp_session_t *session) +{ + return session->tid++; +} -- cgit From 21f2a5244f0c6195e17ff71868328bc71d77f4d9 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Wed, 23 Aug 2006 15:05:30 +0000 Subject: Fix malloc() versus bt_malloc() usage --- src/sdp.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index edde4dbf..86caa15c 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -349,7 +349,7 @@ void sdp_uuid_print(const uuid_t *uuid) sdp_data_t *sdp_data_alloc_with_length(uint8_t dtd, const void *value, uint32_t length) { sdp_data_t *seq; - sdp_data_t *d = (sdp_data_t *) malloc(sizeof(sdp_data_t)); + sdp_data_t *d = malloc(sizeof(sdp_data_t)); if (!d) return NULL; @@ -853,7 +853,7 @@ void sdp_data_free(sdp_data_t *d) static sdp_data_t *extract_int(const void *p, int *len) { - sdp_data_t *d = (sdp_data_t *) malloc(sizeof(sdp_data_t)); + sdp_data_t *d = malloc(sizeof(sdp_data_t)); SDPDBG("Extracting integer\n"); memset(d, 0, sizeof(sdp_data_t)); @@ -899,11 +899,11 @@ static sdp_data_t *extract_int(const void *p, int *len) static sdp_data_t *extract_uuid(const uint8_t *p, int *len, sdp_record_t *rec) { - sdp_data_t *d = (sdp_data_t *) malloc(sizeof(sdp_data_t)); + sdp_data_t *d = malloc(sizeof(sdp_data_t)); SDPDBG("Extracting UUID"); memset(d, 0, sizeof(sdp_data_t)); - if (0 > sdp_uuid_extract(p, &d->val.uuid, len)) { + if (sdp_uuid_extract(p, &d->val.uuid, len) < 0) { free(d); return NULL; } @@ -919,7 +919,7 @@ static sdp_data_t *extract_str(const void *p, int *len) { char *s; int n; - sdp_data_t *d = (sdp_data_t *) malloc(sizeof(sdp_data_t)); + sdp_data_t *d = malloc(sizeof(sdp_data_t)); memset(d, 0, sizeof(sdp_data_t)); d->dtd = *(uint8_t *) p; @@ -961,7 +961,7 @@ static sdp_data_t *extract_seq(const void *p, int *len, sdp_record_t *rec) { int seqlen, n = 0; sdp_data_t *curr, *prev; - sdp_data_t *d = (sdp_data_t *)malloc(sizeof(sdp_data_t)); + sdp_data_t *d = malloc(sizeof(sdp_data_t)); SDPDBG("Extracting SEQ"); memset(d, 0, sizeof(sdp_data_t)); @@ -1297,7 +1297,7 @@ int sdp_send_req_w4_rsp(sdp_session_t *session, uint8_t *reqbuf, uint8_t *rspbuf */ sdp_list_t *sdp_list_append(sdp_list_t *p, void *d) { - sdp_list_t *q, *n = (sdp_list_t *)malloc(sizeof(sdp_list_t)); + sdp_list_t *q, *n = malloc(sizeof(sdp_list_t)); if (!n) return 0; @@ -1335,7 +1335,7 @@ sdp_list_t *sdp_list_insert_sorted(sdp_list_t *list, void *d, sdp_comp_func_t f) { sdp_list_t *q, *p, *n; - n = (sdp_list_t *)malloc(sizeof(sdp_list_t)); + n = malloc(sizeof(sdp_list_t)); if (!n) return 0; n->data = d; @@ -1471,7 +1471,7 @@ int sdp_get_uuidseq_attr(const sdp_record_t *rec, uint16_t attr, sdp_list_t **se if (sdpdata && sdpdata->dtd >= SDP_SEQ8 && sdpdata->dtd <= SDP_SEQ32) { sdp_data_t *d; for (d = sdpdata->val.dataseq; d; d = d->next) { - uuid_t *u = (uuid_t *)malloc(sizeof(uuid_t)); + uuid_t *u = malloc(sizeof(uuid_t)); memset((char *)u, 0, sizeof(uuid_t)); if (d->dtd >= SDP_UUID16 && d->dtd <= SDP_UUID128) { *u = d->val.uuid; @@ -1553,7 +1553,7 @@ int sdp_get_lang_attr(const sdp_record_t *rec, sdp_list_t **langSeq) sdp_data_t *pEncoding = pCode->next; sdp_data_t *pOffset = pEncoding->next; if (pCode && pEncoding && pOffset) { - lang = (sdp_lang_attr_t *)malloc(sizeof(sdp_lang_attr_t)); + lang = malloc(sizeof(sdp_lang_attr_t)); lang->code_ISO639 = pCode->val.uint16; lang->encoding = pEncoding->val.uint16; lang->base_offset = pOffset->val.uint16; @@ -1594,7 +1594,7 @@ int sdp_get_profile_descs(const sdp_record_t *rec, sdp_list_t **profDescSeq) } if (uuid != NULL) { - profDesc = (sdp_profile_desc_t *)malloc(sizeof(sdp_profile_desc_t)); + profDesc = malloc(sizeof(sdp_profile_desc_t)); profDesc->uuid = *uuid; profDesc->version = version; #ifdef SDP_DEBUG @@ -2039,7 +2039,7 @@ uint128_t *sdp_create_base_uuid(void) if (bluetooth_base_uuid == NULL) { strcpy(baseStr, BASE_UUID); - bluetooth_base_uuid = (uint128_t *)malloc(sizeof(uint128_t)); + bluetooth_base_uuid = malloc(sizeof(uint128_t)); data = bluetooth_base_uuid->data; memset(data, '\0', sizeof(uint128_t)); memset(temp, '\0', 10); @@ -2201,7 +2201,7 @@ void sdp_uuid32_to_uuid128(uuid_t *uuid128, uuid_t *uuid32) uuid_t *sdp_uuid_to_uuid128(uuid_t *uuid) { - uuid_t *uuid128 = (uuid_t *)malloc(sizeof(uuid_t)); + uuid_t *uuid128 = bt_malloc(sizeof(uuid_t)); memset(uuid128, 0, sizeof(uuid_t)); switch (uuid->type) { case SDP_UUID128: @@ -2583,7 +2583,7 @@ int sdp_record_update(sdp_session_t *session, const sdp_record_t *rec) sdp_record_t *sdp_record_alloc() { - sdp_record_t *rec = (sdp_record_t *)malloc(sizeof(sdp_record_t)); + sdp_record_t *rec = malloc(sizeof(sdp_record_t)); memset((void *)rec, 0, sizeof(sdp_record_t)); rec->handle = 0xffffffff; return rec; @@ -2610,7 +2610,7 @@ void sdp_pattern_add_uuid(sdp_record_t *rec, uuid_t *uuid) if (sdp_list_find(rec->pattern, uuid128, sdp_uuid128_cmp) == NULL) rec->pattern = sdp_list_insert_sorted(rec->pattern, uuid128, sdp_uuid128_cmp); else - free(uuid128); + bt_free(uuid128); SDPDBG("Elements in target pattern : %d\n", sdp_list_len(rec->pattern)); } @@ -2636,7 +2636,7 @@ static void extract_record_handle_seq(uint8_t *pdu, sdp_list_t **seq, int count, int n; for (n = 0; n < count; n++) { - uint32_t *pSvcRec = (uint32_t *) malloc(sizeof(uint32_t)); + uint32_t *pSvcRec = malloc(sizeof(uint32_t)); *pSvcRec = ntohl(bt_get_unaligned((uint32_t *) pdata)); pSeq = sdp_list_append(pSeq, pSvcRec); pdata += sizeof(uint32_t); -- cgit From 3df8390f8e7be37d0adfac2192c425219941d844 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Thu, 24 Aug 2006 14:51:05 +0000 Subject: Fix UUID128 string lookup handling --- src/sdp.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 86caa15c..b1ef47ea 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -208,6 +208,24 @@ static char *string_lookup(struct tupla *pt0, int index) return ""; } +static char *string_lookup_uuid(struct tupla *pt0, const uuid_t* uuid) +{ + uuid_t tmp_uuid; + + memcpy(&tmp_uuid, uuid, sizeof(tmp_uuid)); + + if (sdp_uuid128_to_uuid(&tmp_uuid)) { + switch (tmp_uuid.type) { + case SDP_UUID16: + return string_lookup(pt0, tmp_uuid.value.uuid16); + case SDP_UUID32: + return string_lookup(pt0, tmp_uuid.value.uuid32); + } + } + + return ""; +} + /* * Prints into a string the Protocol UUID * coping a maximum of n characters. @@ -231,8 +249,9 @@ static int uuid2str(struct tupla *message, const uuid_t *uuid, char *str, size_t snprintf(str, n, str2); break; case SDP_UUID128: - snprintf(str, n, "Error: This is UUID-128"); - return -4; + str2 = string_lookup_uuid(message, uuid); + snprintf(str, n, str2); + break; default: snprintf(str, n, "Type of UUID (%x) unknown.", uuid->type); return -1; -- cgit From 8be090b298a02108146d3bf48271f8408ac31917 Mon Sep 17 00:00:00 2001 From: Claudio Takahasi Date: Fri, 25 Aug 2006 19:12:38 +0000 Subject: Added sdp_create for async searches --- src/sdp.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index b1ef47ea..f535ad48 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -3032,6 +3032,59 @@ sdp_record_t *sdp_service_attr_req(sdp_session_t *session, uint32_t handle, return rec; } +/* + * SDP transaction structure for asynchronous search + */ +struct sdp_transaction { + sdp_callback_t *cb; + void *udata; + sdp_cstate_t *cstate; + uint8_t *reqbuf; + sdp_buf_t rsp_concat_buf; + uint32_t reqsize; + int cstate_len; + int attr_list_len; +}; + +/* + * Creates a new sdp session for asynchronous search + * INPUT: + * int sk + * non-blocking L2CAP socket + * + * RETURN: + * sdp_session_t * + * NULL - On memory allocation failure + */ +sdp_session_t *sdp_create(int sk, uint32_t flags) +{ + + struct sdp_transaction *t; + sdp_session_t *session = malloc(sizeof(sdp_session_t)); + if (!session) { + errno = ENOMEM; + return NULL; + } + + memset(session, 0, sizeof(*session)); + + session->flags = flags; + session->sock = sk; + + t = malloc(sizeof(struct sdp_transaction)); + if (!t) { + errno = ENOMEM; + free(session); + return NULL; + } + + memset(t, 0, sizeof(*t)); + + session->priv = t; + + return session; +} + /* * This is a service search request combined with the service * attribute request. First a service class match is done and @@ -3256,7 +3309,25 @@ int sdp_general_inquiry(inquiry_info *ii, int num_dev, int duration, uint8_t *fo int sdp_close(sdp_session_t *session) { - int ret = close(session->sock); + struct sdp_transaction *t; + int ret; + + if (!session) + return -1; + + ret = close(session->sock); + + t = session->priv; + + if (t) { + if (t->reqbuf) + free(t->reqbuf); + + if (t->rsp_concat_buf.data) + free(t->rsp_concat_buf.data); + + free(t); + } free(session); return ret; } -- cgit From dd610a12694a42aeb67417c95d87384f2eef8e70 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Fri, 25 Aug 2006 22:22:01 +0000 Subject: Add sdp_set_notify function --- src/sdp.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index f535ad48..5bbc8ec8 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -3085,6 +3085,34 @@ sdp_session_t *sdp_create(int sk, uint32_t flags) return session; } +/* + * Set the callback function to called when the transaction finishes + * + * INPUT: + * sdp_session_t *session + * Current sdp session to be handled + * sdp_callback_t *cb + * callback to be called when the transaction finishes + * void *udata + * user data passed to callback + * RETURN: + * 0 - Success + * -1 - Failure + */ +int sdp_set_notify(sdp_session_t *session, sdp_callback_t *func, void *udata) +{ + struct sdp_transaction *t; + + if (!session || !session->priv) + return -1; + + t = session->priv; + t->cb = func; + t->udata = udata; + + return 0; +} + /* * This is a service search request combined with the service * attribute request. First a service class match is done and -- cgit From f9264e21a9e0c97e6a4d970c0b31cf01fc18781d Mon Sep 17 00:00:00 2001 From: Claudio Takahasi Date: Mon, 28 Aug 2006 19:49:18 +0000 Subject: Added sdp_process and sdp_service_search_async functions --- src/sdp.c | 214 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 214 insertions(+) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 5bbc8ec8..601985d6 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -3113,6 +3113,220 @@ int sdp_set_notify(sdp_session_t *session, sdp_callback_t *func, void *udata) return 0; } +/* + * Set the callback function to called when the transaction finishes and send the + * service search attribute request PDU. + * + * INPUT: + * sdp_session_t *session + * Current sdp session to be handled + * sdp_list_t *search + * UUID pattern to search + * RETURN: + * 0 - if the request has been sent properly + * -1 - On any failure + */ +int sdp_service_search_async(sdp_session_t *session, const sdp_list_t *search) +{ + struct sdp_transaction *t; + sdp_pdu_hdr_t *reqhdr; + sdp_list_t *attrids; + uint8_t *pdata; + int seqlen = 0; + uint32_t range = 0x0000ffff; + + if (!session || !session->priv) { + errno = EINVAL; + return -1; + } + + t = session->priv; + t->reqbuf = malloc(SDP_REQ_BUFFER_SIZE); + if (!t->reqbuf) { + errno = ENOMEM; + goto end; + } + + memset(t->reqbuf, 0, SDP_REQ_BUFFER_SIZE); + memset((char *)&t->rsp_concat_buf, 0, sizeof(sdp_buf_t)); + + reqhdr = (sdp_pdu_hdr_t *) t->reqbuf; + reqhdr->tid = htons(sdp_gen_tid(session)); + reqhdr->pdu_id = SDP_SVC_SEARCH_ATTR_REQ; + + // generate PDU + pdata = t->reqbuf + sizeof(sdp_pdu_hdr_t); + t->reqsize = sizeof(sdp_pdu_hdr_t); + + // add service class IDs for search + seqlen = gen_searchseq_pdu(pdata, search); + + SDPDBG("Data seq added : %d\n", seqlen); + + // now set the length and increment the pointer + t->reqsize += seqlen; + pdata += seqlen; + + bt_put_unaligned(htons(SDP_MAX_ATTR_LEN), (uint16_t *) pdata); + t->reqsize += sizeof(uint16_t); + pdata += sizeof(uint16_t); + + SDPDBG("Max attr byte count : %d\n", SDP_MAX_ATTR_LEN); + + // get attr seq PDU form + attrids = sdp_list_append(0, &range); + seqlen = gen_attridseq_pdu(pdata, attrids, SDP_UINT32); + sdp_list_free(attrids, 0); + + if (seqlen == -1) { + errno = EINVAL; + goto end; + } + + pdata += seqlen; + SDPDBG("Attr list length : %d\n", seqlen); + t->reqsize += seqlen; + + // set the request header's param length + t->cstate_len = copy_cstate(pdata, t->cstate); + + reqhdr->plen = htons((t->reqsize + t->cstate_len) - sizeof(sdp_pdu_hdr_t)); + + if (sdp_send_req(session, t->reqbuf, t->reqsize + t->cstate_len) < 0) { + SDPERR("Error sendind data:%s", strerror(errno)); + goto end; + } + + session->priv = t; + + return 0; +end: + + if (t) { + if (t->reqbuf) + free(t->reqbuf); + free(t); + } + + return -1; +} + +/* + * Receive the incomming SDP PDU. This function must be called when there is data + * available to be read. On continuation state, the original request (with a new + * transaction ID) and the continuation state data will be appended in the initial PDU. + * If an error happens or the transaction finishes the callback function will be called. + * + * INPUT: + * sdp_session_t *session + * Current sdp session to be handled + * RETURN: + * 0 - if the transaction is on continuation state + * -1 - On any failure or the transaction finished + */ +int sdp_process(sdp_session_t *session) +{ + struct sdp_transaction *t = NULL; + sdp_pdu_hdr_t *reqhdr = NULL; + sdp_pdu_hdr_t *rsphdr = NULL; + uint8_t *pdata = NULL, *rspbuf = NULL; + int n, err = 0, rsp_count = 0, status = -1; + + if (!session || !session->priv) { + err= EINVAL; + goto end; + } + + rspbuf = malloc(SDP_RSP_BUFFER_SIZE); + if (!rspbuf) { + err = ENOMEM; + goto end; + } + + t = session->priv; + reqhdr = (sdp_pdu_hdr_t *)t->reqbuf; + rsphdr = (sdp_pdu_hdr_t *)rspbuf; + + n = sdp_read_rsp(session, rspbuf, SDP_RSP_BUFFER_SIZE); + if (n < 0) { + err = errno; + goto end; + } + + if (n == 0 || reqhdr->tid != rsphdr->tid || rsphdr->pdu_id == SDP_ERROR_RSP) { + err = EPROTO; + goto end; + } + + pdata = rspbuf + sizeof(sdp_pdu_hdr_t); + rsp_count = ntohs(bt_get_unaligned((uint16_t *) pdata)); + t->attr_list_len += rsp_count; + pdata += sizeof(uint16_t); // pdata points to attribute list + + t->cstate_len = *(uint8_t *) (pdata + rsp_count); + + SDPDBG("Attrlist byte count : %d\n", t->attr_list_len); + SDPDBG("Response byte count : %d\n", rsp_count); + SDPDBG("Cstate length : %d\n", t->cstate_len); + /* + * This is a split response, need to concatenate intermediate + * responses and the last one which will have cstate_len == 0 + */ + if (t->cstate_len > 0 || t->rsp_concat_buf.data_size != 0) { + uint8_t *targetPtr = NULL; + + t->cstate = t->cstate_len > 0 ? (sdp_cstate_t *) (pdata + rsp_count) : 0; + + // build concatenated response buffer + t->rsp_concat_buf.data = realloc(t->rsp_concat_buf.data, t->rsp_concat_buf.data_size + rsp_count); + targetPtr = t->rsp_concat_buf.data + t->rsp_concat_buf.data_size; + t->rsp_concat_buf.buf_size = t->rsp_concat_buf.data_size + rsp_count; + memcpy(targetPtr, pdata, rsp_count); + t->rsp_concat_buf.data_size += rsp_count; + } + + if (t->cstate) { + int reqsize; + reqhdr->tid = htons(sdp_gen_tid(session)); + + // add continuation state (can be null) + t->cstate_len = copy_cstate(t->reqbuf + t->reqsize, t->cstate); + + reqsize = t->reqsize + t->cstate_len; + + // set the request header's param length + reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t)); + + if (sdp_send_req(session, t->reqbuf, reqsize) < 0) { + err = errno; + goto end; + } + status = 0; + } else { + if (t->attr_list_len == 0) { + err = ENODATA; + goto end; + } + } + +end: + + /* error detected or transaction finished */ + if (err || !t->cstate) { + if (t->rsp_concat_buf.data_size != 0) + pdata = t->rsp_concat_buf.data; + + if (t->cb) + t->cb(rsphdr->pdu_id, err, pdata, + t->attr_list_len, t->udata); + } + + if (rspbuf) + free(rspbuf); + + return status; +} + /* * This is a service search request combined with the service * attribute request. First a service class match is done and -- cgit From 343f72ba28deaf9ddc68043567baad76e0325e5d Mon Sep 17 00:00:00 2001 From: Claudio Takahasi Date: Mon, 28 Aug 2006 21:26:50 +0000 Subject: Added SDP error response parsing --- src/sdp.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 601985d6..53c7196b 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -3253,12 +3253,18 @@ int sdp_process(sdp_session_t *session) goto end; } - if (n == 0 || reqhdr->tid != rsphdr->tid || rsphdr->pdu_id == SDP_ERROR_RSP) { + if (n == 0 || reqhdr->tid != rsphdr->tid) err = EPROTO; goto end; } pdata = rspbuf + sizeof(sdp_pdu_hdr_t); + + if (rsphdr->pdu_id == SDP_ERROR_RSP) { + err = ntohs(bt_get_unaligned((uint16_t *) pdata)); + goto end; + } + rsp_count = ntohs(bt_get_unaligned((uint16_t *) pdata)); t->attr_list_len += rsp_count; pdata += sizeof(uint16_t); // pdata points to attribute list -- cgit From 3f7d61cbeff52ce209a75cec97ba29507ed764bc Mon Sep 17 00:00:00 2001 From: Claudio Takahasi Date: Mon, 28 Aug 2006 21:32:20 +0000 Subject: fixed syntax error --- src/sdp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 53c7196b..63a9ba1c 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -3253,7 +3253,7 @@ int sdp_process(sdp_session_t *session) goto end; } - if (n == 0 || reqhdr->tid != rsphdr->tid) + if (n == 0 || reqhdr->tid != rsphdr->tid) { err = EPROTO; goto end; } -- cgit From c8e5a1464ff8edb068e7361a257ef4ae34674cf8 Mon Sep 17 00:00:00 2001 From: Claudio Takahasi Date: Tue, 29 Aug 2006 16:29:00 +0000 Subject: changed error handling in sdp_process function --- src/sdp.c | 43 ++++++++++++++++--------------------------- 1 file changed, 16 insertions(+), 27 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 63a9ba1c..bc69551a 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -3230,38 +3230,34 @@ int sdp_process(sdp_session_t *session) sdp_pdu_hdr_t *reqhdr = NULL; sdp_pdu_hdr_t *rsphdr = NULL; uint8_t *pdata = NULL, *rspbuf = NULL; - int n, err = 0, rsp_count = 0, status = -1; + int rsp_count = 0, err = -1; + uint16_t status = 0; if (!session || !session->priv) { - err= EINVAL; - goto end; + SDPERR("Invalid session"); + return -1; } rspbuf = malloc(SDP_RSP_BUFFER_SIZE); - if (!rspbuf) { - err = ENOMEM; - goto end; - } + if (!rspbuf) + return -1; + + memset(rspbuf, 0, SDP_RSP_BUFFER_SIZE); t = session->priv; reqhdr = (sdp_pdu_hdr_t *)t->reqbuf; rsphdr = (sdp_pdu_hdr_t *)rspbuf; - n = sdp_read_rsp(session, rspbuf, SDP_RSP_BUFFER_SIZE); - if (n < 0) { - err = errno; + if (sdp_read_rsp(session, rspbuf, SDP_RSP_BUFFER_SIZE) <= 0) goto end; - } - if (n == 0 || reqhdr->tid != rsphdr->tid) { - err = EPROTO; + if (reqhdr->tid != rsphdr->tid) goto end; - } pdata = rspbuf + sizeof(sdp_pdu_hdr_t); if (rsphdr->pdu_id == SDP_ERROR_RSP) { - err = ntohs(bt_get_unaligned((uint16_t *) pdata)); + status = ntohs(bt_get_unaligned((uint16_t *) pdata)); goto end; } @@ -3304,33 +3300,26 @@ int sdp_process(sdp_session_t *session) reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t)); if (sdp_send_req(session, t->reqbuf, reqsize) < 0) { - err = errno; - goto end; - } - status = 0; - } else { - if (t->attr_list_len == 0) { - err = ENODATA; + SDPERR("Error sendind data:%s", strerror(errno)); goto end; } + err = 0; } end: - - /* error detected or transaction finished */ - if (err || !t->cstate) { + if (err) { if (t->rsp_concat_buf.data_size != 0) pdata = t->rsp_concat_buf.data; if (t->cb) - t->cb(rsphdr->pdu_id, err, pdata, + t->cb(rsphdr->pdu_id, status, pdata, t->attr_list_len, t->udata); } if (rspbuf) free(rspbuf); - return status; + return err; } /* -- cgit From 453a0cedb6c909047553ffc7145bcef26a3ae94c Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Tue, 29 Aug 2006 21:39:07 +0000 Subject: Correct prototypes for async functions --- src/sdp.c | 49 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 10 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index bc69551a..1d6d65fe 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -3113,6 +3113,18 @@ int sdp_set_notify(sdp_session_t *session, sdp_callback_t *func, void *udata) return 0; } +int sdp_service_search_async(sdp_session_t *session, const sdp_list_t *search_list, uint16_t max_rec_num) +{ + /* FIXME: implement! */ + return 0; +} + +int sdp_service_attr_async(sdp_session_t *session, uint32_t handle, sdp_attrreq_type_t reqtype, const sdp_list_t *attrid_list) +{ + /* FIXME: implement! */ + return 0; +} + /* * Set the callback function to called when the transaction finishes and send the * service search attribute request PDU. @@ -3120,20 +3132,39 @@ int sdp_set_notify(sdp_session_t *session, sdp_callback_t *func, void *udata) * INPUT: * sdp_session_t *session * Current sdp session to be handled - * sdp_list_t *search - * UUID pattern to search + * + * sdp_list_t *search + * Singly linked list containing elements of the search + * pattern. Each entry in the list is a UUID(DataTypeSDP_UUID16) + * of the service to be searched + * + * AttributeSpecification attrSpec + * Attribute identifiers are 16 bit unsigned integers specified + * in one of 2 ways described below : + * SDP_ATTR_REQ_INDIVIDUAL - 16bit individual identifiers + * They are the actual attribute identifiers in ascending order + * + * SDP_ATTR_REQ_RANGE - 32bit identifier range + * The high-order 16bits is the start of range + * the low-order 16bits are the end of range + * 0x0000 to 0xFFFF gets all attributes + * + * sdp_list_t *attrids + * Singly linked list containing attribute identifiers desired. + * Every element is either a uint16_t(attrSpec = SDP_ATTR_REQ_INDIVIDUAL) + * or a uint32_t(attrSpec=SDP_ATTR_REQ_RANGE) + * + * RETURN: * 0 - if the request has been sent properly * -1 - On any failure */ -int sdp_service_search_async(sdp_session_t *session, const sdp_list_t *search) +int sdp_service_search_attr_async(sdp_session_t *session, const sdp_list_t *search_list, sdp_attrreq_type_t reqtype, const sdp_list_t *attrid_list) { struct sdp_transaction *t; sdp_pdu_hdr_t *reqhdr; - sdp_list_t *attrids; uint8_t *pdata; int seqlen = 0; - uint32_t range = 0x0000ffff; if (!session || !session->priv) { errno = EINVAL; @@ -3159,7 +3190,7 @@ int sdp_service_search_async(sdp_session_t *session, const sdp_list_t *search) t->reqsize = sizeof(sdp_pdu_hdr_t); // add service class IDs for search - seqlen = gen_searchseq_pdu(pdata, search); + seqlen = gen_searchseq_pdu(pdata, search_list); SDPDBG("Data seq added : %d\n", seqlen); @@ -3174,10 +3205,8 @@ int sdp_service_search_async(sdp_session_t *session, const sdp_list_t *search) SDPDBG("Max attr byte count : %d\n", SDP_MAX_ATTR_LEN); // get attr seq PDU form - attrids = sdp_list_append(0, &range); - seqlen = gen_attridseq_pdu(pdata, attrids, SDP_UINT32); - sdp_list_free(attrids, 0); - + seqlen = gen_attridseq_pdu(pdata, attrid_list, + reqtype == SDP_ATTR_REQ_INDIVIDUAL ? SDP_UINT16 : SDP_UINT32); if (seqlen == -1) { errno = EINVAL; goto end; -- cgit From a76dc65489dd8959547a97a391e405b2025bbd79 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Tue, 29 Aug 2006 22:03:13 +0000 Subject: Make sdp_connect() async aware --- src/sdp.c | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 1d6d65fe..d694fd24 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -3059,8 +3059,10 @@ struct sdp_transaction { sdp_session_t *sdp_create(int sk, uint32_t flags) { + sdp_session_t *session; struct sdp_transaction *t; - sdp_session_t *session = malloc(sizeof(sdp_session_t)); + + session = malloc(sizeof(sdp_session_t)); if (!session) { errno = ENOMEM; return NULL; @@ -3605,12 +3607,30 @@ static inline int sdp_is_local(const bdaddr_t *device) sdp_session_t *sdp_connect(const bdaddr_t *src, const bdaddr_t *dst, uint32_t flags) { + sdp_session_t *session; + struct sdp_transaction *t; int err; - sdp_session_t *session = malloc(sizeof(sdp_session_t)); + + session = malloc(sizeof(sdp_session_t)); if (!session) return session; + memset(session, 0, sizeof(*session)); + session->flags = flags; + session->sock = -1; + + t = malloc(sizeof(struct sdp_transaction)); + if (!t) { + errno = ENOMEM; + free(session); + return NULL; + } + + memset(t, 0, sizeof(*t)); + + session->priv = t; + if (sdp_is_local(dst)) { struct sockaddr_un sa; @@ -3649,10 +3669,13 @@ sdp_session_t *sdp_connect(const bdaddr_t *src, const bdaddr_t *dst, uint32_t fl while (errno == EBUSY && (flags & SDP_RETRY_IF_BUSY)); } } + fail: err = errno; if (session->sock >= 0) close(session->sock); + if (session->priv) + free(session->priv); free(session); errno = err; return 0; -- cgit From adff4cad15e4e13bb9847a0ff62b355caddf89e3 Mon Sep 17 00:00:00 2001 From: Claudio Takahasi Date: Wed, 30 Aug 2006 12:52:34 +0000 Subject: added/updated async functions comments --- src/sdp.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 74 insertions(+), 6 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index d694fd24..27a895f4 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -3088,7 +3088,9 @@ sdp_session_t *sdp_create(int sk, uint32_t flags) } /* - * Set the callback function to called when the transaction finishes + * Sets the callback function/user data used to notify the application + * that the asynchronous transaction finished. This function must be + * called before request an asynchronous search. * * INPUT: * sdp_session_t *session @@ -3098,7 +3100,7 @@ sdp_session_t *sdp_create(int sk, uint32_t flags) * void *udata * user data passed to callback * RETURN: - * 0 - Success + * 0 - Success * -1 - Failure */ int sdp_set_notify(sdp_session_t *session, sdp_callback_t *func, void *udata) @@ -3115,12 +3117,75 @@ int sdp_set_notify(sdp_session_t *session, sdp_callback_t *func, void *udata) return 0; } +/* + * This function starts an asynchronous service search request. + * The incomming and outgoing data are stored in the transaction structure + * buffers. When there is incomming data the sdp_process function must be + * called to get the data and handle the continuation state. + * + * INPUT : + * sdp_session_t *session + * Current sdp session to be handled + * + * sdp_list_t *search_list + * Singly linked list containing elements of the search + * pattern. Each entry in the list is a UUID (DataTypeSDP_UUID16) + * of the service to be searched + * + * uint16_t max_rec_num + * A 16 bit integer which tells the service, the maximum + * entries that the client can handle in the response. The + * server is obliged not to return > max_rec_num entries + * + * OUTPUT : + * + * int return value + * 0 - if the request has been sent properly + * -1 - On any failure and sets errno + */ + int sdp_service_search_async(sdp_session_t *session, const sdp_list_t *search_list, uint16_t max_rec_num) { /* FIXME: implement! */ return 0; } +/* + * This function starts an asynchronous service attribute request. + * The incomming and outgoing data are stored in the transaction structure + * buffers. When there is incomming data the sdp_process function must be + * called to get the data and handle the continuation state. + * + * INPUT : + * sdp_session_t *session + * Current sdp session to be handled + * + * uint32_t handle + * The handle of the service for which the attribute(s) are + * requested + * + * sdp_attrreq_type_t reqtype + * Attribute identifiers are 16 bit unsigned integers specified + * in one of 2 ways described below : + * SDP_ATTR_REQ_INDIVIDUAL - 16bit individual identifiers + * They are the actual attribute identifiers in ascending order + * + * SDP_ATTR_REQ_RANGE - 32bit identifier range + * The high-order 16bits is the start of range + * the low-order 16bits are the end of range + * 0x0000 to 0xFFFF gets all attributes + * + * sdp_list_t *attrid_list + * Singly linked list containing attribute identifiers desired. + * Every element is either a uint16_t(attrSpec = SDP_ATTR_REQ_INDIVIDUAL) + * or a uint32_t(attrSpec=SDP_ATTR_REQ_RANGE) + * + * OUTPUT : + * int return value + * 0 - if the request has been sent properly + * -1 - On any failure and sets errno + */ + int sdp_service_attr_async(sdp_session_t *session, uint32_t handle, sdp_attrreq_type_t reqtype, const sdp_list_t *attrid_list) { /* FIXME: implement! */ @@ -3128,8 +3193,11 @@ int sdp_service_attr_async(sdp_session_t *session, uint32_t handle, sdp_attrreq_ } /* - * Set the callback function to called when the transaction finishes and send the - * service search attribute request PDU. + * This function starts an asynchronous service search attributes. + * It is a service search request combined with attribute request. The incomming + * and outgoing data are stored in the transaction structure buffers. When there + * is incomming data the sdp_process function must be called to get the data + * and handle the continuation state. * * INPUT: * sdp_session_t *session @@ -3151,14 +3219,14 @@ int sdp_service_attr_async(sdp_session_t *session, uint32_t handle, sdp_attrreq_ * the low-order 16bits are the end of range * 0x0000 to 0xFFFF gets all attributes * - * sdp_list_t *attrids + * sdp_list_t *attrid_list * Singly linked list containing attribute identifiers desired. * Every element is either a uint16_t(attrSpec = SDP_ATTR_REQ_INDIVIDUAL) * or a uint32_t(attrSpec=SDP_ATTR_REQ_RANGE) * * RETURN: - * 0 - if the request has been sent properly + * 0 - if the request has been sent properly * -1 - On any failure */ int sdp_service_search_attr_async(sdp_session_t *session, const sdp_list_t *search_list, sdp_attrreq_type_t reqtype, const sdp_list_t *attrid_list) -- cgit From ef1fe5feaa0dde16562723bcb36343c7bc4c31be Mon Sep 17 00:00:00 2001 From: Claudio Takahasi Date: Wed, 30 Aug 2006 21:34:20 +0000 Subject: added sdp_service_attr_async function --- src/sdp.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 70 insertions(+), 3 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 27a895f4..014fc4c8 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -3188,8 +3188,77 @@ int sdp_service_search_async(sdp_session_t *session, const sdp_list_t *search_li int sdp_service_attr_async(sdp_session_t *session, uint32_t handle, sdp_attrreq_type_t reqtype, const sdp_list_t *attrid_list) { - /* FIXME: implement! */ + struct sdp_transaction *t; + sdp_pdu_hdr_t *reqhdr; + uint8_t *pdata; + int seqlen = 0; + + if (!session || !session->priv) { + errno = EINVAL; + return -1; + } + + t = session->priv; + t->reqbuf = malloc(SDP_REQ_BUFFER_SIZE); + if (!t->reqbuf) { + errno = ENOMEM; + goto end; + } + + memset(t->reqbuf, 0, SDP_REQ_BUFFER_SIZE); + memset((char *)&t->rsp_concat_buf, 0, sizeof(sdp_buf_t)); + + reqhdr = (sdp_pdu_hdr_t *) t->reqbuf; + reqhdr->tid = htons(sdp_gen_tid(session)); + reqhdr->pdu_id = SDP_SVC_ATTR_REQ; + + // generate PDU + pdata = t->reqbuf + sizeof(sdp_pdu_hdr_t); + t->reqsize = sizeof(sdp_pdu_hdr_t); + + // add the service record handle + bt_put_unaligned(htonl(handle), (uint32_t *) pdata); + t->reqsize += sizeof(uint32_t); + pdata += sizeof(uint32_t); + + // specify the response limit + bt_put_unaligned(htons(65535), (uint16_t *) pdata); + t->reqsize += sizeof(uint16_t); + pdata += sizeof(uint16_t); + + // get attr seq PDU form + seqlen = gen_attridseq_pdu(pdata, attrid_list, + reqtype == SDP_ATTR_REQ_INDIVIDUAL? SDP_UINT16 : SDP_UINT32); + if (seqlen == -1) { + errno = EINVAL; + goto end; + } + + // now set the length and increment the pointer + t->reqsize += seqlen; + pdata += seqlen; + SDPDBG("Attr list length : %d\n", seqlen); + + // set the request header's param length + t->cstate_len = copy_cstate(pdata, t->cstate); + + reqhdr->plen = htons((t->reqsize + t->cstate_len) - sizeof(sdp_pdu_hdr_t)); + + if (sdp_send_req(session, t->reqbuf, t->reqsize + t->cstate_len) < 0) { + SDPERR("Error sendind data:%s", strerror(errno)); + goto end; + } + return 0; +end: + + if (t) { + if (t->reqbuf) + free(t->reqbuf); + free(t); + } + + return -1; } /* @@ -3296,8 +3365,6 @@ int sdp_service_search_attr_async(sdp_session_t *session, const sdp_list_t *sear goto end; } - session->priv = t; - return 0; end: -- cgit From e9527af7533e35a59580cda37ff58152cb356103 Mon Sep 17 00:00:00 2001 From: Claudio Takahasi Date: Wed, 30 Aug 2006 22:02:14 +0000 Subject: added sdp_service_search_async function --- src/sdp.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 014fc4c8..c10737a5 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -3146,8 +3146,67 @@ int sdp_set_notify(sdp_session_t *session, sdp_callback_t *func, void *udata) int sdp_service_search_async(sdp_session_t *session, const sdp_list_t *search_list, uint16_t max_rec_num) { - /* FIXME: implement! */ + struct sdp_transaction *t; + sdp_pdu_hdr_t *reqhdr; + uint8_t *pdata; + int seqlen = 0; + + if (!session || !session->priv) { + errno = EINVAL; + return -1; + } + + t = session->priv; + t->reqbuf = malloc(SDP_REQ_BUFFER_SIZE); + if (!t->reqbuf) { + errno = ENOMEM; + goto end; + } + + memset(t->reqbuf, 0, SDP_REQ_BUFFER_SIZE); + memset((char *)&t->rsp_concat_buf, 0, sizeof(sdp_buf_t)); + + reqhdr = (sdp_pdu_hdr_t *) t->reqbuf; + reqhdr->tid = htons(sdp_gen_tid(session)); + reqhdr->pdu_id = SDP_SVC_SEARCH_REQ; + + // generate PDU + pdata = t->reqbuf + sizeof(sdp_pdu_hdr_t); + t->reqsize = sizeof(sdp_pdu_hdr_t); + + // add service class IDs for search + seqlen = gen_searchseq_pdu(pdata, search_list); + + SDPDBG("Data seq added : %d\n", seqlen); + + // now set the length and increment the pointer + t->reqsize += seqlen; + pdata += seqlen; + + bt_put_unaligned(htons(max_rec_num), (uint16_t *) pdata); + t->reqsize += sizeof(uint16_t); + pdata += sizeof(uint16_t); + + // set the request header's param length + t->cstate_len = copy_cstate(pdata, t->cstate); + + reqhdr->plen = htons((t->reqsize + t->cstate_len) - sizeof(sdp_pdu_hdr_t)); + + if (sdp_send_req(session, t->reqbuf, t->reqsize + t->cstate_len) < 0) { + SDPERR("Error sendind data:%s", strerror(errno)); + goto end; + } + return 0; +end: + + if (t) { + if (t->reqbuf) + free(t->reqbuf); + free(t); + } + + return -1; } /* -- cgit From 41064d605ba2f88f967c8388c9b20b92a7868a87 Mon Sep 17 00:00:00 2001 From: Ulisses Furquim Date: Wed, 30 Aug 2006 22:44:49 +0000 Subject: fix prototypes for sdp sync/async functions --- src/sdp.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index c10737a5..362dcfb1 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -2735,7 +2735,7 @@ static int copy_cstate(uint8_t *pdata, const sdp_cstate_t *cstate) * * INPUT : * - * sdp_list_t *search_list + * sdp_list_t *search * Singly linked list containing elements of the search * pattern. Each entry in the list is a UUID (DataTypeSDP_UUID16) * of the service to be searched @@ -3127,7 +3127,7 @@ int sdp_set_notify(sdp_session_t *session, sdp_callback_t *func, void *udata) * sdp_session_t *session * Current sdp session to be handled * - * sdp_list_t *search_list + * sdp_list_t *search * Singly linked list containing elements of the search * pattern. Each entry in the list is a UUID (DataTypeSDP_UUID16) * of the service to be searched @@ -3144,7 +3144,7 @@ int sdp_set_notify(sdp_session_t *session, sdp_callback_t *func, void *udata) * -1 - On any failure and sets errno */ -int sdp_service_search_async(sdp_session_t *session, const sdp_list_t *search_list, uint16_t max_rec_num) +int sdp_service_search_async(sdp_session_t *session, const sdp_list_t *search, uint16_t max_rec_num) { struct sdp_transaction *t; sdp_pdu_hdr_t *reqhdr; @@ -3175,7 +3175,7 @@ int sdp_service_search_async(sdp_session_t *session, const sdp_list_t *search_li t->reqsize = sizeof(sdp_pdu_hdr_t); // add service class IDs for search - seqlen = gen_searchseq_pdu(pdata, search_list); + seqlen = gen_searchseq_pdu(pdata, search); SDPDBG("Data seq added : %d\n", seqlen); @@ -3357,7 +3357,7 @@ end: * 0 - if the request has been sent properly * -1 - On any failure */ -int sdp_service_search_attr_async(sdp_session_t *session, const sdp_list_t *search_list, sdp_attrreq_type_t reqtype, const sdp_list_t *attrid_list) +int sdp_service_search_attr_async(sdp_session_t *session, const sdp_list_t *search, sdp_attrreq_type_t reqtype, const sdp_list_t *attrid_list) { struct sdp_transaction *t; sdp_pdu_hdr_t *reqhdr; @@ -3388,7 +3388,7 @@ int sdp_service_search_attr_async(sdp_session_t *session, const sdp_list_t *sear t->reqsize = sizeof(sdp_pdu_hdr_t); // add service class IDs for search - seqlen = gen_searchseq_pdu(pdata, search_list); + seqlen = gen_searchseq_pdu(pdata, search); SDPDBG("Data seq added : %d\n", seqlen); -- cgit From 58eb769e7620e03c8b0bf2d2bcf38c06ab58065f Mon Sep 17 00:00:00 2001 From: Claudio Takahasi Date: Thu, 31 Aug 2006 21:22:06 +0000 Subject: fixed sdp_process to handle cstate --- src/sdp.c | 78 +++++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 54 insertions(+), 24 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 362dcfb1..11896cb0 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -3043,7 +3043,7 @@ struct sdp_transaction { sdp_buf_t rsp_concat_buf; uint32_t reqsize; int cstate_len; - int attr_list_len; + int size; }; /* @@ -3454,7 +3454,8 @@ int sdp_process(sdp_session_t *session) struct sdp_transaction *t = NULL; sdp_pdu_hdr_t *reqhdr = NULL; sdp_pdu_hdr_t *rsphdr = NULL; - uint8_t *pdata = NULL, *rspbuf = NULL; + sdp_cstate_t *pcstate = NULL; + uint8_t *pdata = NULL, *rspbuf = NULL, *targetPtr = NULL; int rsp_count = 0, err = -1; uint16_t status = 0; @@ -3486,40 +3487,69 @@ int sdp_process(sdp_session_t *session) goto end; } - rsp_count = ntohs(bt_get_unaligned((uint16_t *) pdata)); - t->attr_list_len += rsp_count; - pdata += sizeof(uint16_t); // pdata points to attribute list + switch (rsphdr->pdu_id) { + uint8_t *ssr_pdata; + uint16_t tsrc, csrc; + case SDP_SVC_SEARCH_RSP: + /* + * TSRC: Total Service Record Count + * CSRC: Current Service Record Count + */ + ssr_pdata = pdata; + ssr_pdata += sizeof(tsrc); + tsrc = ntohs(bt_get_unaligned((uint16_t *) ssr_pdata)); + csrc = ntohs(bt_get_unaligned((uint16_t *) ssr_pdata)); + + if (t->size == 0) { + /* first fragment */ + rsp_count = sizeof(tsrc) + sizeof(csrc) + csrc * 4; + SDPDBG("Total svc count: %d\n", tsrc); + SDPDBG("Current svc count: %d\n", csrc); + } else { + pdata += 2 * sizeof(uint16_t); /* Ignore TSRC and CSRC */ + rsp_count = csrc * 4; + } + t->size += rsp_count; + break; + case SDP_SVC_ATTR_RSP: + case SDP_SVC_SEARCH_ATTR_RSP: + rsp_count = ntohs(bt_get_unaligned((uint16_t *) pdata)); + SDPDBG("Attrlist byte count : %d\n", rsp_count); - t->cstate_len = *(uint8_t *) (pdata + rsp_count); + pdata += sizeof(uint16_t); // points to attribute list + t->size += rsp_count; + break; + default: + /* FIXME: how handle this situation? */ + SDPDBG("Illegal PDU ID!"); + goto end; + } - SDPDBG("Attrlist byte count : %d\n", t->attr_list_len); - SDPDBG("Response byte count : %d\n", rsp_count); - SDPDBG("Cstate length : %d\n", t->cstate_len); + pcstate = (sdp_cstate_t *) (pdata + rsp_count); + + SDPDBG("Cstate length : %d\n", pcstate->length); /* * This is a split response, need to concatenate intermediate * responses and the last one which will have cstate_len == 0 */ - if (t->cstate_len > 0 || t->rsp_concat_buf.data_size != 0) { - uint8_t *targetPtr = NULL; - t->cstate = t->cstate_len > 0 ? (sdp_cstate_t *) (pdata + rsp_count) : 0; - // build concatenated response buffer - t->rsp_concat_buf.data = realloc(t->rsp_concat_buf.data, t->rsp_concat_buf.data_size + rsp_count); - targetPtr = t->rsp_concat_buf.data + t->rsp_concat_buf.data_size; - t->rsp_concat_buf.buf_size = t->rsp_concat_buf.data_size + rsp_count; - memcpy(targetPtr, pdata, rsp_count); - t->rsp_concat_buf.data_size += rsp_count; - } + // build concatenated response buffer + t->rsp_concat_buf.data = realloc(t->rsp_concat_buf.data, t->rsp_concat_buf.data_size + rsp_count); + targetPtr = t->rsp_concat_buf.data + t->rsp_concat_buf.data_size; + t->rsp_concat_buf.buf_size = t->rsp_concat_buf.data_size + rsp_count; + memcpy(targetPtr, pdata, rsp_count); + t->rsp_concat_buf.data_size += rsp_count; + + if (pcstate->length > 0) { + int reqsize, cstate_len; - if (t->cstate) { - int reqsize; reqhdr->tid = htons(sdp_gen_tid(session)); // add continuation state (can be null) - t->cstate_len = copy_cstate(t->reqbuf + t->reqsize, t->cstate); + cstate_len = copy_cstate(t->reqbuf + t->reqsize, pcstate); - reqsize = t->reqsize + t->cstate_len; + reqsize = t->reqsize + cstate_len; // set the request header's param length reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t)); @@ -3538,7 +3568,7 @@ end: if (t->cb) t->cb(rsphdr->pdu_id, status, pdata, - t->attr_list_len, t->udata); + t->size, t->udata); } if (rspbuf) -- cgit From c01b6039f44d9e78acc26cd8482b5e0c7b23fe14 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sun, 3 Sep 2006 19:51:41 +0000 Subject: Remove cstate and cstate_len from transaction structure --- src/sdp.c | 53 +++++++++++++++++++++++++---------------------------- 1 file changed, 25 insertions(+), 28 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 11896cb0..1ce5e81b 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -3036,14 +3036,11 @@ sdp_record_t *sdp_service_attr_req(sdp_session_t *session, uint32_t handle, * SDP transaction structure for asynchronous search */ struct sdp_transaction { - sdp_callback_t *cb; - void *udata; - sdp_cstate_t *cstate; - uint8_t *reqbuf; + sdp_callback_t *cb; /* called when the transaction finishes */ + void *udata; /* client user data */ + uint8_t *reqbuf; /* pointer to request PDU */ sdp_buf_t rsp_concat_buf; - uint32_t reqsize; - int cstate_len; - int size; + uint32_t reqsize; /* without cstate */ }; /* @@ -3149,7 +3146,7 @@ int sdp_service_search_async(sdp_session_t *session, const sdp_list_t *search, u struct sdp_transaction *t; sdp_pdu_hdr_t *reqhdr; uint8_t *pdata; - int seqlen = 0; + int cstate_len, seqlen = 0; if (!session || !session->priv) { errno = EINVAL; @@ -3187,12 +3184,12 @@ int sdp_service_search_async(sdp_session_t *session, const sdp_list_t *search, u t->reqsize += sizeof(uint16_t); pdata += sizeof(uint16_t); + // set the request header's param length - t->cstate_len = copy_cstate(pdata, t->cstate); - - reqhdr->plen = htons((t->reqsize + t->cstate_len) - sizeof(sdp_pdu_hdr_t)); + cstate_len = copy_cstate(pdata, NULL); + reqhdr->plen = htons((t->reqsize + cstate_len) - sizeof(sdp_pdu_hdr_t)); - if (sdp_send_req(session, t->reqbuf, t->reqsize + t->cstate_len) < 0) { + if (sdp_send_req(session, t->reqbuf, t->reqsize + cstate_len) < 0) { SDPERR("Error sendind data:%s", strerror(errno)); goto end; } @@ -3250,7 +3247,7 @@ int sdp_service_attr_async(sdp_session_t *session, uint32_t handle, sdp_attrreq_ struct sdp_transaction *t; sdp_pdu_hdr_t *reqhdr; uint8_t *pdata; - int seqlen = 0; + int cstate_len, seqlen = 0; if (!session || !session->priv) { errno = EINVAL; @@ -3299,11 +3296,10 @@ int sdp_service_attr_async(sdp_session_t *session, uint32_t handle, sdp_attrreq_ SDPDBG("Attr list length : %d\n", seqlen); // set the request header's param length - t->cstate_len = copy_cstate(pdata, t->cstate); - - reqhdr->plen = htons((t->reqsize + t->cstate_len) - sizeof(sdp_pdu_hdr_t)); + cstate_len = copy_cstate(pdata, NULL); + reqhdr->plen = htons((t->reqsize + cstate_len) - sizeof(sdp_pdu_hdr_t)); - if (sdp_send_req(session, t->reqbuf, t->reqsize + t->cstate_len) < 0) { + if (sdp_send_req(session, t->reqbuf, t->reqsize + cstate_len) < 0) { SDPERR("Error sendind data:%s", strerror(errno)); goto end; } @@ -3362,7 +3358,7 @@ int sdp_service_search_attr_async(sdp_session_t *session, const sdp_list_t *sear struct sdp_transaction *t; sdp_pdu_hdr_t *reqhdr; uint8_t *pdata; - int seqlen = 0; + int cstate_len, seqlen = 0; if (!session || !session->priv) { errno = EINVAL; @@ -3415,11 +3411,10 @@ int sdp_service_search_attr_async(sdp_session_t *session, const sdp_list_t *sear t->reqsize += seqlen; // set the request header's param length - t->cstate_len = copy_cstate(pdata, t->cstate); - - reqhdr->plen = htons((t->reqsize + t->cstate_len) - sizeof(sdp_pdu_hdr_t)); + cstate_len = copy_cstate(pdata, NULL); + reqhdr->plen = htons((t->reqsize + cstate_len) - sizeof(sdp_pdu_hdr_t)); - if (sdp_send_req(session, t->reqbuf, t->reqsize + t->cstate_len) < 0) { + if (sdp_send_req(session, t->reqbuf, t->reqsize + cstate_len) < 0) { SDPERR("Error sendind data:%s", strerror(errno)); goto end; } @@ -3457,6 +3452,7 @@ int sdp_process(sdp_session_t *session) sdp_cstate_t *pcstate = NULL; uint8_t *pdata = NULL, *rspbuf = NULL, *targetPtr = NULL; int rsp_count = 0, err = -1; + size_t size = 0; uint16_t status = 0; if (!session || !session->priv) { @@ -3484,6 +3480,7 @@ int sdp_process(sdp_session_t *session) if (rsphdr->pdu_id == SDP_ERROR_RSP) { status = ntohs(bt_get_unaligned((uint16_t *) pdata)); + size = rsphdr->plen; goto end; } @@ -3500,7 +3497,7 @@ int sdp_process(sdp_session_t *session) tsrc = ntohs(bt_get_unaligned((uint16_t *) ssr_pdata)); csrc = ntohs(bt_get_unaligned((uint16_t *) ssr_pdata)); - if (t->size == 0) { + if (t->rsp_concat_buf.data_size == 0) { /* first fragment */ rsp_count = sizeof(tsrc) + sizeof(csrc) + csrc * 4; SDPDBG("Total svc count: %d\n", tsrc); @@ -3509,7 +3506,6 @@ int sdp_process(sdp_session_t *session) pdata += 2 * sizeof(uint16_t); /* Ignore TSRC and CSRC */ rsp_count = csrc * 4; } - t->size += rsp_count; break; case SDP_SVC_ATTR_RSP: case SDP_SVC_SEARCH_ATTR_RSP: @@ -3517,7 +3513,6 @@ int sdp_process(sdp_session_t *session) SDPDBG("Attrlist byte count : %d\n", rsp_count); pdata += sizeof(uint16_t); // points to attribute list - t->size += rsp_count; break; default: /* FIXME: how handle this situation? */ @@ -3533,7 +3528,6 @@ int sdp_process(sdp_session_t *session) * responses and the last one which will have cstate_len == 0 */ - // build concatenated response buffer t->rsp_concat_buf.data = realloc(t->rsp_concat_buf.data, t->rsp_concat_buf.data_size + rsp_count); targetPtr = t->rsp_concat_buf.data + t->rsp_concat_buf.data_size; @@ -3556,6 +3550,7 @@ int sdp_process(sdp_session_t *session) if (sdp_send_req(session, t->reqbuf, reqsize) < 0) { SDPERR("Error sendind data:%s", strerror(errno)); + /* FIXME: how handle this error ? */ goto end; } err = 0; @@ -3563,12 +3558,14 @@ int sdp_process(sdp_session_t *session) end: if (err) { - if (t->rsp_concat_buf.data_size != 0) + if (t->rsp_concat_buf.data_size != 0) { pdata = t->rsp_concat_buf.data; + size = t->rsp_concat_buf.data_size; + } if (t->cb) t->cb(rsphdr->pdu_id, status, pdata, - t->size, t->udata); + size, t->udata); } if (rspbuf) -- cgit From 1be1f79648ba7aafde6349cfe51f9c929e20d018 Mon Sep 17 00:00:00 2001 From: Ulisses Furquim Date: Mon, 4 Sep 2006 17:13:33 +0000 Subject: Make sdp_connect() fully async aware --- src/sdp.c | 158 +++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 100 insertions(+), 58 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 1ce5e81b..2a054094 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -3041,21 +3042,33 @@ struct sdp_transaction { uint8_t *reqbuf; /* pointer to request PDU */ sdp_buf_t rsp_concat_buf; uint32_t reqsize; /* without cstate */ + uint8_t connected; }; +inline int sdp_is_connected(sdp_session_t *session) +{ + struct sdp_transaction *t = session->priv; + return t->connected; +} + +static inline void sdp_set_connected(sdp_session_t *session) +{ + struct sdp_transaction *t = session->priv; + t->connected = 1; +} + /* * Creates a new sdp session for asynchronous search * INPUT: * int sk - * non-blocking L2CAP socket - * + * non-blocking L2CAP socket + * * RETURN: * sdp_session_t * * NULL - On memory allocation failure */ sdp_session_t *sdp_create(int sk, uint32_t flags) { - sdp_session_t *session; struct sdp_transaction *t; @@ -3064,7 +3077,6 @@ sdp_session_t *sdp_create(int sk, uint32_t flags) errno = ENOMEM; return NULL; } - memset(session, 0, sizeof(*session)); session->flags = flags; @@ -3076,7 +3088,6 @@ sdp_session_t *sdp_create(int sk, uint32_t flags) free(session); return NULL; } - memset(t, 0, sizeof(*t)); session->priv = t; @@ -3826,71 +3837,101 @@ static inline int sdp_is_local(const bdaddr_t *device) return memcmp(device, BDADDR_LOCAL, sizeof(bdaddr_t)) == 0; } -sdp_session_t *sdp_connect(const bdaddr_t *src, const bdaddr_t *dst, uint32_t flags) +static int sdp_connect_local(sdp_session_t *session) { - sdp_session_t *session; - struct sdp_transaction *t; - int err; + struct sockaddr_un sa; + int ret; - session = malloc(sizeof(sdp_session_t)); - if (!session) - return session; + session->sock = socket(PF_UNIX, SOCK_STREAM, 0); + if (session->sock < 0) + return -1; + session->local = 1; - memset(session, 0, sizeof(*session)); + sa.sun_family = AF_UNIX; + strcpy(sa.sun_path, SDP_UNIX_PATH); - session->flags = flags; - session->sock = -1; + ret = connect(session->sock, (struct sockaddr *)&sa, sizeof(sa)); + if (!ret) + sdp_set_connected(session); - t = malloc(sizeof(struct sdp_transaction)); - if (!t) { - errno = ENOMEM; - free(session); - return NULL; + return ret; +} + +static int sdp_connect_l2cap(const bdaddr_t *src, + const bdaddr_t *dst, sdp_session_t *session) +{ + uint32_t flags = session->flags; + struct sockaddr_l2 sa; + int sk; + + session->sock = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP); + if (session->sock < 0) + return -1; + session->local = 0; + + sk = session->sock; + + if (flags & SDP_NON_BLOCKING) { + long arg = fcntl(sk, F_GETFL, 0); + fcntl(sk, F_SETFL, arg | O_NONBLOCK); } - memset(t, 0, sizeof(*t)); + sa.l2_family = AF_BLUETOOTH; + sa.l2_psm = 0; - session->priv = t; + if (bacmp(src, BDADDR_ANY)) { + sa.l2_bdaddr = *src; + if (bind(sk, (struct sockaddr *) &sa, sizeof(sa)) < 0) + return -1; + } - if (sdp_is_local(dst)) { - struct sockaddr_un sa; - - // create local unix connection - session->sock = socket(PF_UNIX, SOCK_STREAM, 0); - session->local = 1; - if (session->sock >= 0) { - sa.sun_family = AF_UNIX; - strcpy(sa.sun_path, SDP_UNIX_PATH); - if (connect(session->sock, (struct sockaddr *)&sa, sizeof(sa)) == 0) - return session; + if (flags & SDP_WAIT_ON_CLOSE) { + struct linger l = { .l_onoff = 1, .l_linger = 1 }; + setsockopt(sk, SOL_SOCKET, SO_LINGER, &l, sizeof(l)); + } + + sa.l2_psm = htobs(SDP_PSM); + sa.l2_bdaddr = *dst; + + do { + int ret = connect(sk, (struct sockaddr *) &sa, sizeof(sa)); + if (!ret) { + sdp_set_connected(session); + return 0; } + if (ret < 0 && (flags & SDP_NON_BLOCKING) && + (errno == EAGAIN || errno == EINPROGRESS)) + return 0; + } while (errno == EBUSY && (flags & SDP_RETRY_IF_BUSY)); + + return -1; +} + +sdp_session_t *sdp_connect(const bdaddr_t *src, + const bdaddr_t *dst, uint32_t flags) +{ + sdp_session_t *session; + int err; + + if ((flags & SDP_RETRY_IF_BUSY) && (flags & SDP_NON_BLOCKING)) { + errno = EINVAL; + return NULL; + } + + session = sdp_create(-1, flags); + if (!session) + return NULL; + + if (sdp_is_local(dst)) { + if (sdp_connect_local(session) < 0) + goto fail; } else { - struct sockaddr_l2 sa; - - // create L2CAP connection - session->sock = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP); - session->local = 0; - if (session->sock >= 0) { - sa.l2_family = AF_BLUETOOTH; - sa.l2_psm = 0; - if (bacmp(src, BDADDR_ANY) != 0) { - sa.l2_bdaddr = *src; - if (bind(session->sock, (struct sockaddr *) &sa, sizeof(sa)) < 0) - goto fail; - } - if (flags & SDP_WAIT_ON_CLOSE) { - struct linger l = { .l_onoff = 1, .l_linger = 1 }; - setsockopt(session->sock, SOL_SOCKET, SO_LINGER, &l, sizeof(l)); - } - sa.l2_psm = htobs(SDP_PSM); - sa.l2_bdaddr = *dst; - do - if (connect(session->sock, (struct sockaddr *) &sa, sizeof(sa)) == 0) - return session; - while (errno == EBUSY && (flags & SDP_RETRY_IF_BUSY)); - } + if (sdp_connect_l2cap(src, dst, session) < 0) + goto fail; } + return session; + fail: err = errno; if (session->sock >= 0) @@ -3899,7 +3940,8 @@ fail: free(session->priv); free(session); errno = err; - return 0; + + return NULL; } int sdp_get_socket(const sdp_session_t *session) -- cgit From aa7577018bb1faac4b67b52dc8b003663cdd03ad Mon Sep 17 00:00:00 2001 From: Ulisses Furquim Date: Mon, 4 Sep 2006 21:10:23 +0000 Subject: Remove connected flag from SDP transaction structure --- src/sdp.c | 24 ++---------------------- 1 file changed, 2 insertions(+), 22 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 2a054094..26f01eac 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -3042,21 +3042,8 @@ struct sdp_transaction { uint8_t *reqbuf; /* pointer to request PDU */ sdp_buf_t rsp_concat_buf; uint32_t reqsize; /* without cstate */ - uint8_t connected; }; -inline int sdp_is_connected(sdp_session_t *session) -{ - struct sdp_transaction *t = session->priv; - return t->connected; -} - -static inline void sdp_set_connected(sdp_session_t *session) -{ - struct sdp_transaction *t = session->priv; - t->connected = 1; -} - /* * Creates a new sdp session for asynchronous search * INPUT: @@ -3840,7 +3827,6 @@ static inline int sdp_is_local(const bdaddr_t *device) static int sdp_connect_local(sdp_session_t *session) { struct sockaddr_un sa; - int ret; session->sock = socket(PF_UNIX, SOCK_STREAM, 0); if (session->sock < 0) @@ -3850,11 +3836,7 @@ static int sdp_connect_local(sdp_session_t *session) sa.sun_family = AF_UNIX; strcpy(sa.sun_path, SDP_UNIX_PATH); - ret = connect(session->sock, (struct sockaddr *)&sa, sizeof(sa)); - if (!ret) - sdp_set_connected(session); - - return ret; + return connect(session->sock, (struct sockaddr *)&sa, sizeof(sa)); } static int sdp_connect_l2cap(const bdaddr_t *src, @@ -3895,10 +3877,8 @@ static int sdp_connect_l2cap(const bdaddr_t *src, do { int ret = connect(sk, (struct sockaddr *) &sa, sizeof(sa)); - if (!ret) { - sdp_set_connected(session); + if (!ret) return 0; - } if (ret < 0 && (flags & SDP_NON_BLOCKING) && (errno == EAGAIN || errno == EINPROGRESS)) return 0; -- cgit From 113a53acaec2519108f0cab944687b7abc537e74 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Tue, 5 Sep 2006 09:41:50 +0000 Subject: Remove sdp_cstate_t from the header file --- src/sdp.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 26f01eac..f961c7d3 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -2720,6 +2720,11 @@ static int gen_attridseq_pdu(uint8_t *dst, const sdp_list_t *seq, uint8_t dataTy return gen_dataseq_pdu(dst, seq, dataType); } +typedef struct { + uint8_t length; + unsigned char data[16]; +} __attribute__ ((packed)) sdp_cstate_t; + static int copy_cstate(uint8_t *pdata, const sdp_cstate_t *cstate) { if (cstate) { -- cgit From 6d83e9274971235c05b40349dbbaf3f826853ff5 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Tue, 5 Sep 2006 09:52:42 +0000 Subject: Print only unitSize length of the strings --- src/sdp.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index f961c7d3..c59a6157 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -1149,13 +1149,13 @@ void sdp_record_print(const sdp_record_t *rec) { sdp_data_t *d = sdp_data_get(rec, SDP_ATTR_SVCNAME_PRIMARY); if (d) - printf("Service Name: %s\n", d->val.str); + printf("Service Name: %.*s\n", d->unitSize, d->val.str); d = sdp_data_get(rec, SDP_ATTR_SVCDESC_PRIMARY); if (d) - printf("Service Description: %s\n", d->val.str); + printf("Service Description: %.*s\n", d->unitSize, d->val.str); d = sdp_data_get(rec, SDP_ATTR_PROVNAME_PRIMARY); if (d) - printf("Service Provider: %s\n", d->val.str); + printf("Service Provider: %.*s\n", d->unitSize, d->val.str); } #ifdef SDP_DEBUG -- cgit From 997f54c70b25706839544018ad15a718911eb45f Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Tue, 5 Sep 2006 10:17:46 +0000 Subject: Cleanup of some memory usage --- src/sdp.c | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index c59a6157..ccbc1657 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -90,9 +90,6 @@ static inline void ntoh128(uint128_t *src, uint128_t *dst) static uint128_t *bluetooth_base_uuid = NULL; -#define SDP_BASIC_ATTR_PDUFORM_SIZE 32 -#define SDP_SEQ_PDUFORM_SIZE 128 -#define SDP_UUID_SEQ_SIZE 256 #define SDP_MAX_ATTR_LEN 65535 /* Message structure. */ @@ -808,14 +805,15 @@ static void sdp_attr_pdu(void *value, void *udata) int sdp_gen_record_pdu(const sdp_record_t *rec, sdp_buf_t *buf) { buf->data = malloc(SDP_PDU_CHUNK_SIZE); - if (buf->data) { - buf->buf_size = SDP_PDU_CHUNK_SIZE; - buf->data_size = 0; - memset(buf->data, 0, buf->buf_size); - sdp_list_foreach(rec->attrlist, sdp_attr_pdu, buf); - return 0; - } - return -1; + if (!buf->data) + return -ENOMEM; + + buf->buf_size = SDP_PDU_CHUNK_SIZE; + buf->data_size = 0; + memset(buf->data, 0, buf->buf_size); + sdp_list_foreach(rec->attrlist, sdp_attr_pdu, buf); + + return 0; } void sdp_attr_replace(sdp_record_t *rec, uint16_t attr, sdp_data_t *d) @@ -2376,12 +2374,14 @@ void sdp_append_to_buf(sdp_buf_t *dst, uint8_t *data, uint32_t len) void sdp_append_to_pdu(sdp_buf_t *pdu, sdp_data_t *d) { - uint8_t buf[SDP_SEQ_PDUFORM_SIZE]; + uint8_t buf[256]; sdp_buf_t append; + memset(&append, 0, sizeof(sdp_buf_t)); append.data = buf; append.buf_size = sizeof(buf); append.data_size = 0; + sdp_set_attrid(&append, d->attrId); sdp_gen_pdu(&append, d); sdp_append_to_buf(pdu, append.data, append.data_size); @@ -2679,8 +2679,11 @@ static int gen_dataseq_pdu(uint8_t *dst, const sdp_list_t *seq, uint8_t dtd) SDPDBG(""); memset(&buf, 0, sizeof(sdp_buf_t)); - buf.data = malloc(SDP_UUID_SEQ_SIZE); - buf.buf_size = SDP_UUID_SEQ_SIZE; + buf.data = malloc(256); + buf.buf_size = 256; + + if (!buf.data) + return -ENOMEM; SDPDBG("Seq length : %d\n", seqlen); -- cgit From 5ec20cab698055029198b10afb4db20bee06ce72 Mon Sep 17 00:00:00 2001 From: Claudio Takahasi Date: Mon, 11 Sep 2006 20:55:05 +0000 Subject: fixed error handling in sdp_process( ) --- src/sdp.c | 89 +++++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 64 insertions(+), 25 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index ccbc1657..e311f014 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -3050,6 +3050,7 @@ struct sdp_transaction { uint8_t *reqbuf; /* pointer to request PDU */ sdp_buf_t rsp_concat_buf; uint32_t reqsize; /* without cstate */ + int err; /* ZERO if success or the errno if failed */ }; /* @@ -3437,6 +3438,35 @@ end: return -1; } +/* + * Function used to get the error reason after sdp_callback_t function has been called + * and the status is 0xffff. It indicates that an error NOT related to SDP_ErrorResponse + * happened. Get errno directly is not safe because multiple transactions can be triggered. + * This function must be used with asynchronous sdp functions only. + * + * INPUT: + * sdp_session_t *session + * Current sdp session to be handled + * RETURN: + * 0 = No error in the current transaction + * -1 - if the session is invalid + * positive value - the errno value + * + */ +int sdp_get_error(sdp_session_t *session) +{ + struct sdp_transaction *t; + + if (!session || !session->priv) { + SDPERR("Invalid session"); + return -1; + } + + t = session->priv; + + return t->err; +} + /* * Receive the incomming SDP PDU. This function must be called when there is data * available to be read. On continuation state, the original request (with a new @@ -3452,14 +3482,14 @@ end: */ int sdp_process(sdp_session_t *session) { - struct sdp_transaction *t = NULL; - sdp_pdu_hdr_t *reqhdr = NULL; - sdp_pdu_hdr_t *rsphdr = NULL; - sdp_cstate_t *pcstate = NULL; - uint8_t *pdata = NULL, *rspbuf = NULL, *targetPtr = NULL; - int rsp_count = 0, err = -1; + struct sdp_transaction *t; + sdp_pdu_hdr_t *reqhdr, *rsphdr; + sdp_cstate_t *pcstate; + uint8_t *pdata, *rspbuf, *targetPtr; + int rsp_count, err = -1; size_t size = 0; - uint16_t status = 0; + uint16_t status = 0xffff; + uint8_t pdu_id = 0x00; if (!session || !session->priv) { SDPERR("Invalid session"); @@ -3467,8 +3497,11 @@ int sdp_process(sdp_session_t *session) } rspbuf = malloc(SDP_RSP_BUFFER_SIZE); - if (!rspbuf) + if (!rspbuf) { + SDPERR("Response buffer alloc failure:%s (%d)", + strerror(errno), errno); return -1; + } memset(rspbuf, 0, SDP_RSP_BUFFER_SIZE); @@ -3476,20 +3509,21 @@ int sdp_process(sdp_session_t *session) reqhdr = (sdp_pdu_hdr_t *)t->reqbuf; rsphdr = (sdp_pdu_hdr_t *)rspbuf; - if (sdp_read_rsp(session, rspbuf, SDP_RSP_BUFFER_SIZE) <= 0) - goto end; + pdata = rspbuf + sizeof(sdp_pdu_hdr_t); - if (reqhdr->tid != rsphdr->tid) + if (sdp_read_rsp(session, rspbuf, SDP_RSP_BUFFER_SIZE) <= 0) { + SDPERR("Read response:%s (%d)", strerror(errno), errno); + t->err = errno; goto end; + } - pdata = rspbuf + sizeof(sdp_pdu_hdr_t); - - if (rsphdr->pdu_id == SDP_ERROR_RSP) { - status = ntohs(bt_get_unaligned((uint16_t *) pdata)); - size = rsphdr->plen; + if (reqhdr->tid != rsphdr->tid) { + t->err = EPROTO; + SDPERR("Wrong transaction ID."); goto end; } + pdu_id = rsphdr->pdu_id; switch (rsphdr->pdu_id) { uint8_t *ssr_pdata; uint16_t tsrc, csrc; @@ -3512,6 +3546,7 @@ int sdp_process(sdp_session_t *session) pdata += 2 * sizeof(uint16_t); /* Ignore TSRC and CSRC */ rsp_count = csrc * 4; } + status = 0x0000; break; case SDP_SVC_ATTR_RSP: case SDP_SVC_SEARCH_ATTR_RSP: @@ -3519,10 +3554,15 @@ int sdp_process(sdp_session_t *session) SDPDBG("Attrlist byte count : %d\n", rsp_count); pdata += sizeof(uint16_t); // points to attribute list + status = 0x0000; break; + case SDP_ERROR_RSP: + status = ntohs(bt_get_unaligned((uint16_t *) pdata)); + size = rsphdr->plen; + goto end; default: - /* FIXME: how handle this situation? */ - SDPDBG("Illegal PDU ID!"); + t->err = EPROTO; + SDPERR("Illegal PDU ID: 0x%x", rsphdr->pdu_id); goto end; } @@ -3531,7 +3571,7 @@ int sdp_process(sdp_session_t *session) SDPDBG("Cstate length : %d\n", pcstate->length); /* * This is a split response, need to concatenate intermediate - * responses and the last one which will have cstate_len == 0 + * responses and the last one which will have cstate length == 0 */ // build concatenated response buffer @@ -3546,7 +3586,7 @@ int sdp_process(sdp_session_t *session) reqhdr->tid = htons(sdp_gen_tid(session)); - // add continuation state (can be null) + // add continuation state cstate_len = copy_cstate(t->reqbuf + t->reqsize, pcstate); reqsize = t->reqsize + cstate_len; @@ -3555,8 +3595,9 @@ int sdp_process(sdp_session_t *session) reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t)); if (sdp_send_req(session, t->reqbuf, reqsize) < 0) { - SDPERR("Error sendind data:%s", strerror(errno)); - /* FIXME: how handle this error ? */ + SDPERR("Error sendind data:%s(%d)", strerror(errno), errno); + status = 0xffff; + t->err = errno; goto end; } err = 0; @@ -3568,10 +3609,8 @@ end: pdata = t->rsp_concat_buf.data; size = t->rsp_concat_buf.data_size; } - if (t->cb) - t->cb(rsphdr->pdu_id, status, pdata, - size, t->udata); + t->cb(pdu_id, status, pdata, size, t->udata); } if (rspbuf) -- cgit From f207a4f0f6d5d38a3e357f62d4976bedaed2cf4f Mon Sep 17 00:00:00 2001 From: Claudio Takahasi Date: Wed, 13 Sep 2006 18:21:48 +0000 Subject: fixed search transaction reqbuf double free --- src/sdp.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index e311f014..d82a23d7 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -3204,10 +3204,9 @@ int sdp_service_search_async(sdp_session_t *session, const sdp_list_t *search, u return 0; end: - if (t) { - if (t->reqbuf) - free(t->reqbuf); - free(t); + if (t->reqbuf) { + free(t->reqbuf); + t->reqbuf = NULL; } return -1; @@ -3314,10 +3313,9 @@ int sdp_service_attr_async(sdp_session_t *session, uint32_t handle, sdp_attrreq_ return 0; end: - if (t) { - if (t->reqbuf) - free(t->reqbuf); - free(t); + if (t->reqbuf) { + free(t->reqbuf); + t->reqbuf = NULL; } return -1; @@ -3429,10 +3427,9 @@ int sdp_service_search_attr_async(sdp_session_t *session, const sdp_list_t *sear return 0; end: - if (t) { - if (t->reqbuf) - free(t->reqbuf); - free(t); + if (t->reqbuf) { + free(t->reqbuf); + t->reqbuf = NULL; } return -1; -- cgit From 12f41aa6ff9529a80a182754d0855dccb11978ac Mon Sep 17 00:00:00 2001 From: Claudio Takahasi Date: Wed, 13 Sep 2006 21:33:19 +0000 Subject: Use transaction err instead of errno for *_async funcs --- src/sdp.c | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index d82a23d7..3e56f490 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -3155,15 +3155,13 @@ int sdp_service_search_async(sdp_session_t *session, const sdp_list_t *search, u uint8_t *pdata; int cstate_len, seqlen = 0; - if (!session || !session->priv) { - errno = EINVAL; + if (!session || !session->priv) return -1; - } t = session->priv; t->reqbuf = malloc(SDP_REQ_BUFFER_SIZE); if (!t->reqbuf) { - errno = ENOMEM; + t->err = ENOMEM; goto end; } @@ -3191,13 +3189,13 @@ int sdp_service_search_async(sdp_session_t *session, const sdp_list_t *search, u t->reqsize += sizeof(uint16_t); pdata += sizeof(uint16_t); - // set the request header's param length cstate_len = copy_cstate(pdata, NULL); reqhdr->plen = htons((t->reqsize + cstate_len) - sizeof(sdp_pdu_hdr_t)); if (sdp_send_req(session, t->reqbuf, t->reqsize + cstate_len) < 0) { SDPERR("Error sendind data:%s", strerror(errno)); + t->err = errno; goto end; } @@ -3255,15 +3253,13 @@ int sdp_service_attr_async(sdp_session_t *session, uint32_t handle, sdp_attrreq_ uint8_t *pdata; int cstate_len, seqlen = 0; - if (!session || !session->priv) { - errno = EINVAL; + if (!session || !session->priv) return -1; - } t = session->priv; t->reqbuf = malloc(SDP_REQ_BUFFER_SIZE); if (!t->reqbuf) { - errno = ENOMEM; + t->err = ENOMEM; goto end; } @@ -3292,7 +3288,7 @@ int sdp_service_attr_async(sdp_session_t *session, uint32_t handle, sdp_attrreq_ seqlen = gen_attridseq_pdu(pdata, attrid_list, reqtype == SDP_ATTR_REQ_INDIVIDUAL? SDP_UINT16 : SDP_UINT32); if (seqlen == -1) { - errno = EINVAL; + t->err = EINVAL; goto end; } @@ -3307,6 +3303,7 @@ int sdp_service_attr_async(sdp_session_t *session, uint32_t handle, sdp_attrreq_ if (sdp_send_req(session, t->reqbuf, t->reqsize + cstate_len) < 0) { SDPERR("Error sendind data:%s", strerror(errno)); + t->err = errno; goto end; } @@ -3365,15 +3362,13 @@ int sdp_service_search_attr_async(sdp_session_t *session, const sdp_list_t *sear uint8_t *pdata; int cstate_len, seqlen = 0; - if (!session || !session->priv) { - errno = EINVAL; + if (!session || !session->priv) return -1; - } t = session->priv; t->reqbuf = malloc(SDP_REQ_BUFFER_SIZE); if (!t->reqbuf) { - errno = ENOMEM; + t->err = ENOMEM; goto end; } @@ -3407,7 +3402,7 @@ int sdp_service_search_attr_async(sdp_session_t *session, const sdp_list_t *sear seqlen = gen_attridseq_pdu(pdata, attrid_list, reqtype == SDP_ATTR_REQ_INDIVIDUAL ? SDP_UINT16 : SDP_UINT32); if (seqlen == -1) { - errno = EINVAL; + t->err = EINVAL; goto end; } @@ -3421,6 +3416,7 @@ int sdp_service_search_attr_async(sdp_session_t *session, const sdp_list_t *sear if (sdp_send_req(session, t->reqbuf, t->reqsize + cstate_len) < 0) { SDPERR("Error sendind data:%s", strerror(errno)); + t->err = errno; goto end; } @@ -3437,8 +3433,9 @@ end: /* * Function used to get the error reason after sdp_callback_t function has been called - * and the status is 0xffff. It indicates that an error NOT related to SDP_ErrorResponse - * happened. Get errno directly is not safe because multiple transactions can be triggered. + * and the status is 0xffff or if sdp_service_{search, attr, search_attr}_async returns -1. + * It indicates that an error NOT related to SDP_ErrorResponse happened. Get errno directly + * is not safe because multiple transactions can be triggered. * This function must be used with asynchronous sdp functions only. * * INPUT: -- cgit From 18a82a6fce76be0883a431380495510010515fff Mon Sep 17 00:00:00 2001 From: Claudio Takahasi Date: Wed, 13 Sep 2006 21:45:21 +0000 Subject: avoid multiple allocs/leak in *_async calls --- src/sdp.c | 48 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 15 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 3e56f490..a9187865 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -3159,14 +3159,20 @@ int sdp_service_search_async(sdp_session_t *session, const sdp_list_t *search, u return -1; t = session->priv; - t->reqbuf = malloc(SDP_REQ_BUFFER_SIZE); + + /* check if the buffer is already allocated */ + if (t->rsp_concat_buf.data) + free(t->rsp_concat_buf.data); + memset(&t->rsp_concat_buf, 0, sizeof(sdp_buf_t)); + if (!t->reqbuf) { - t->err = ENOMEM; - goto end; + t->reqbuf = malloc(SDP_REQ_BUFFER_SIZE); + if (!t->reqbuf) { + t->err = ENOMEM; + goto end; + } } - memset(t->reqbuf, 0, SDP_REQ_BUFFER_SIZE); - memset((char *)&t->rsp_concat_buf, 0, sizeof(sdp_buf_t)); reqhdr = (sdp_pdu_hdr_t *) t->reqbuf; reqhdr->tid = htons(sdp_gen_tid(session)); @@ -3257,14 +3263,20 @@ int sdp_service_attr_async(sdp_session_t *session, uint32_t handle, sdp_attrreq_ return -1; t = session->priv; - t->reqbuf = malloc(SDP_REQ_BUFFER_SIZE); + + /* check if the buffer is already allocated */ + if (t->rsp_concat_buf.data) + free(t->rsp_concat_buf.data); + memset(&t->rsp_concat_buf, 0, sizeof(sdp_buf_t)); + if (!t->reqbuf) { - t->err = ENOMEM; - goto end; + t->reqbuf = malloc(SDP_REQ_BUFFER_SIZE); + if (!t->reqbuf) { + t->err = ENOMEM; + goto end; + } } - memset(t->reqbuf, 0, SDP_REQ_BUFFER_SIZE); - memset((char *)&t->rsp_concat_buf, 0, sizeof(sdp_buf_t)); reqhdr = (sdp_pdu_hdr_t *) t->reqbuf; reqhdr->tid = htons(sdp_gen_tid(session)); @@ -3366,14 +3378,20 @@ int sdp_service_search_attr_async(sdp_session_t *session, const sdp_list_t *sear return -1; t = session->priv; - t->reqbuf = malloc(SDP_REQ_BUFFER_SIZE); + + /* check if the buffer is already allocated */ + if (t->rsp_concat_buf.data) + free(t->rsp_concat_buf.data); + memset(&t->rsp_concat_buf, 0, sizeof(sdp_buf_t)); + if (!t->reqbuf) { - t->err = ENOMEM; - goto end; + t->reqbuf = malloc(SDP_REQ_BUFFER_SIZE); + if (!t->reqbuf) { + t->err = ENOMEM; + goto end; + } } - memset(t->reqbuf, 0, SDP_REQ_BUFFER_SIZE); - memset((char *)&t->rsp_concat_buf, 0, sizeof(sdp_buf_t)); reqhdr = (sdp_pdu_hdr_t *) t->reqbuf; reqhdr->tid = htons(sdp_gen_tid(session)); -- cgit From 9203084c0b92805006c133f82fbc0cbd6b203904 Mon Sep 17 00:00:00 2001 From: Claudio Takahasi Date: Thu, 28 Sep 2006 19:30:01 +0000 Subject: sdp_process:check if the PDU length match with the received data --- src/sdp.c | 55 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 11 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index a9187865..7e7698b2 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -3500,6 +3500,7 @@ int sdp_process(sdp_session_t *session) uint8_t *pdata, *rspbuf, *targetPtr; int rsp_count, err = -1; size_t size = 0; + int n, plen; uint16_t status = 0xffff; uint8_t pdu_id = 0x00; @@ -3523,15 +3524,17 @@ int sdp_process(sdp_session_t *session) pdata = rspbuf + sizeof(sdp_pdu_hdr_t); - if (sdp_read_rsp(session, rspbuf, SDP_RSP_BUFFER_SIZE) <= 0) { + n = sdp_read_rsp(session, rspbuf, SDP_RSP_BUFFER_SIZE); + if (n < 0) { SDPERR("Read response:%s (%d)", strerror(errno), errno); t->err = errno; goto end; } - if (reqhdr->tid != rsphdr->tid) { + if (n == 0 || reqhdr->tid != rsphdr->tid || + (n != (ntohs(rsphdr->plen) + sizeof(sdp_pdu_hdr_t)))) { t->err = EPROTO; - SDPERR("Wrong transaction ID."); + SDPERR("Protocol error."); goto end; } @@ -3541,19 +3544,30 @@ int sdp_process(sdp_session_t *session) uint16_t tsrc, csrc; case SDP_SVC_SEARCH_RSP: /* - * TSRC: Total Service Record Count - * CSRC: Current Service Record Count + * TSRC: Total Service Record Count (2 bytes) + * CSRC: Current Service Record Count (2 bytes) */ ssr_pdata = pdata; - ssr_pdata += sizeof(tsrc); tsrc = ntohs(bt_get_unaligned((uint16_t *) ssr_pdata)); + ssr_pdata += sizeof(uint16_t); csrc = ntohs(bt_get_unaligned((uint16_t *) ssr_pdata)); + /* csrc should never be larger than tsrc */ + if (csrc > tsrc) { + t->err = EPROTO; + SDPERR("Protocol error: wrong current service record count value."); + goto end; + } + + SDPDBG("Total svc count: %d\n", tsrc); + SDPDBG("Current svc count: %d\n", csrc); + + /* parameter length without continuation state */ + plen = sizeof(tsrc) + sizeof(csrc) + csrc * 4; + if (t->rsp_concat_buf.data_size == 0) { /* first fragment */ rsp_count = sizeof(tsrc) + sizeof(csrc) + csrc * 4; - SDPDBG("Total svc count: %d\n", tsrc); - SDPDBG("Current svc count: %d\n", csrc); } else { pdata += 2 * sizeof(uint16_t); /* Ignore TSRC and CSRC */ rsp_count = csrc * 4; @@ -3564,13 +3578,22 @@ int sdp_process(sdp_session_t *session) case SDP_SVC_SEARCH_ATTR_RSP: rsp_count = ntohs(bt_get_unaligned((uint16_t *) pdata)); SDPDBG("Attrlist byte count : %d\n", rsp_count); + + /* + * Number of bytes in the AttributeLists parameter(without + * continuation state) + AttributeListsByteCount field size. + */ + plen = sizeof(uint16_t) + rsp_count; pdata += sizeof(uint16_t); // points to attribute list status = 0x0000; break; case SDP_ERROR_RSP: status = ntohs(bt_get_unaligned((uint16_t *) pdata)); - size = rsphdr->plen; + size = ntohs(rsphdr->plen); + + /* error code + error info */ + plen = size; goto end; default: t->err = EPROTO; @@ -3581,12 +3604,22 @@ int sdp_process(sdp_session_t *session) pcstate = (sdp_cstate_t *) (pdata + rsp_count); SDPDBG("Cstate length : %d\n", pcstate->length); + + /* + * Check out of bound. Continuation state must have at least + * 1 byte: ZERO to indicate that it is not a partial response. + */ + if ((n - sizeof(sdp_pdu_hdr_t)) != (plen + pcstate->length + 1)) { + t->err = EPROTO; + SDPERR("Protocol error: wrong PDU size."); + status = 0xffff; + goto end; + } + /* * This is a split response, need to concatenate intermediate * responses and the last one which will have cstate length == 0 */ - - // build concatenated response buffer t->rsp_concat_buf.data = realloc(t->rsp_concat_buf.data, t->rsp_concat_buf.data_size + rsp_count); targetPtr = t->rsp_concat_buf.data + t->rsp_concat_buf.data_size; t->rsp_concat_buf.buf_size = t->rsp_concat_buf.data_size + rsp_count; -- cgit From e3dbc939bd096c8604d1480e0994f73f65a39f16 Mon Sep 17 00:00:00 2001 From: Claudio Takahasi Date: Thu, 28 Sep 2006 21:36:48 +0000 Subject: fixed csrc for partial responses --- src/sdp.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 7e7698b2..0eeb5a09 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -3569,7 +3569,17 @@ int sdp_process(sdp_session_t *session) /* first fragment */ rsp_count = sizeof(tsrc) + sizeof(csrc) + csrc * 4; } else { - pdata += 2 * sizeof(uint16_t); /* Ignore TSRC and CSRC */ + /* point to the first csrc */ + uint16_t *pcsrc = (uint16_t *) (t->rsp_concat_buf.data + 2); + + /* FIXME: update the interface later. csrc doesn't need be passed to clients */ + + pdata += sizeof(uint16_t); /* point to csrc */ + + /* the first csrc contains the sum of partial csrc responses */ + *pcsrc += bt_get_unaligned((uint16_t *) pdata); + + pdata += sizeof(uint16_t); /* point to the first handle */ rsp_count = csrc * 4; } status = 0x0000; -- cgit From 82817924a25fb0eaaeb9824372106870c45c9db9 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Tue, 7 Nov 2006 18:30:16 +0000 Subject: Add functions for registering binary records --- src/sdp.c | 103 ++++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 74 insertions(+), 29 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 0eeb5a09..d83dea76 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -2395,13 +2395,12 @@ void sdp_append_to_pdu(sdp_buf_t *pdu, sdp_data_t *d) * * Returns zero on success, otherwise -1 (and sets errno). */ -int sdp_device_record_register(sdp_session_t *session, bdaddr_t *device, sdp_record_t *rec, uint8_t flags) +int sdp_device_record_register_binary(sdp_session_t *session, bdaddr_t *device, uint8_t *data, uint32_t size, uint8_t flags, uint32_t *handle) { - int status = 0; uint8_t *req, *rsp, *p; uint32_t reqsize, rspsize; sdp_pdu_hdr_t *reqhdr, *rsphdr; - sdp_buf_t pdu; + int status; SDPDBG(""); @@ -2416,16 +2415,13 @@ int sdp_device_record_register(sdp_session_t *session, bdaddr_t *device, sdp_rec errno = ENOMEM; goto end; } - if (rec->handle && rec->handle != 0xffffffff) { - uint32_t handle = rec->handle; - sdp_data_t *data = sdp_data_alloc(SDP_UINT32, &handle); - sdp_attr_replace(rec, SDP_ATTR_RECORD_HANDLE, data); - } + reqhdr = (sdp_pdu_hdr_t *)req; reqhdr->pdu_id = SDP_SVC_REGISTER_REQ; reqhdr->tid = htons(sdp_gen_tid(session)); reqsize = sizeof(sdp_pdu_hdr_t) + 1; p = req + sizeof(sdp_pdu_hdr_t); + if (bacmp(device, BDADDR_ANY)) { *p++ = flags | SDP_DEVICE_RECORD; bacpy((bdaddr_t *) p, device); @@ -2433,35 +2429,66 @@ int sdp_device_record_register(sdp_session_t *session, bdaddr_t *device, sdp_rec reqsize += sizeof(bdaddr_t); } else *p++ = flags; - if (sdp_gen_record_pdu(rec, &pdu) < 0) { - status = -1; - errno = ENOMEM; - goto end; - } - memcpy(p, pdu.data, pdu.data_size); - free(pdu.data); - reqsize += pdu.data_size; + + memcpy(p, data, size); + reqsize += size; reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t)); status = sdp_send_req_w4_rsp(session, req, rsp, reqsize, &rspsize); if (status < 0) goto end; + rsphdr = (sdp_pdu_hdr_t *) rsp; p = rsp + sizeof(sdp_pdu_hdr_t); + if (rsphdr->pdu_id == SDP_SVC_REGISTER_RSP) { - uint32_t handle = ntohl(bt_get_unaligned((uint32_t *) p)); - sdp_data_t *data = sdp_data_alloc(SDP_UINT32, &handle); - rec->handle = handle; - sdp_attr_replace(rec, SDP_ATTR_RECORD_HANDLE, data); + if (handle) + *handle = ntohl(bt_get_unaligned((uint32_t *) p)); } + end: if (req) free(req); + if (rsp) free(rsp); + return status; } +int sdp_device_record_register(sdp_session_t *session, bdaddr_t *device, sdp_record_t *rec, uint8_t flags) +{ + sdp_buf_t pdu; + uint32_t handle; + int err; + + SDPDBG(""); + + if (rec->handle && rec->handle != 0xffffffff) { + uint32_t handle = rec->handle; + sdp_data_t *data = sdp_data_alloc(SDP_UINT32, &handle); + sdp_attr_replace(rec, SDP_ATTR_RECORD_HANDLE, data); + } + + if (sdp_gen_record_pdu(rec, &pdu) < 0) { + errno = ENOMEM; + return -1; + } + + err = sdp_device_record_register_binary(session, device, + pdu.data, pdu.data_size, flags, &handle); + + free(pdu.data); + + if (err == 0) { + sdp_data_t *data = sdp_data_alloc(SDP_UINT32, &handle); + rec->handle = handle; + sdp_attr_replace(rec, SDP_ATTR_RECORD_HANDLE, data); + } + + return err; +} + int sdp_record_register(sdp_session_t *session, sdp_record_t *rec, uint8_t flags) { return sdp_device_record_register(session, BDADDR_ANY, rec, flags); @@ -2470,25 +2497,25 @@ int sdp_record_register(sdp_session_t *session, sdp_record_t *rec, uint8_t flags /* * unregister a service record */ -int sdp_device_record_unregister(sdp_session_t *session, bdaddr_t *device, sdp_record_t *rec) +int sdp_device_record_unregister_binary(sdp_session_t *session, bdaddr_t *device, uint32_t handle) { - int status = 0; uint8_t *reqbuf, *rspbuf, *p; uint32_t reqsize = 0, rspsize = 0; sdp_pdu_hdr_t *reqhdr, *rsphdr; - uint32_t handle = 0; + int status; SDPDBG(""); - handle = rec->handle; if (handle == SDP_SERVER_RECORD_HANDLE) { errno = EINVAL; return -1; } + if (!session->local) { errno = EREMOTE; return -1; } + reqbuf = malloc(SDP_REQ_BUFFER_SIZE); rspbuf = malloc(SDP_RSP_BUFFER_SIZE); if (!reqbuf || !rspbuf) { @@ -2511,19 +2538,31 @@ int sdp_device_record_unregister(sdp_session_t *session, bdaddr_t *device, sdp_r rsphdr = (sdp_pdu_hdr_t *) rspbuf; p = rspbuf + sizeof(sdp_pdu_hdr_t); status = bt_get_unaligned((uint16_t *) p); - if (status == 0 && rsphdr->pdu_id == SDP_SVC_REMOVE_RSP) { - SDPDBG("Removing local copy\n"); - sdp_record_free(rec); - } + if (status != 0 || rsphdr->pdu_id != SDP_SVC_REMOVE_RSP) + status = -1; } + end: if (reqbuf) free(reqbuf); + if (rspbuf) free(rspbuf); + return status; } +int sdp_device_record_unregister(sdp_session_t *session, bdaddr_t *device, sdp_record_t *rec) +{ + int err; + + err = sdp_device_record_unregister_binary(session, device, rec->handle); + if (err == 0) + sdp_record_free(rec); + + return err; +} + int sdp_record_unregister(sdp_session_t *session, sdp_record_t *rec) { return sdp_device_record_unregister(session, BDADDR_ANY, rec); @@ -2532,16 +2571,22 @@ int sdp_record_unregister(sdp_session_t *session, sdp_record_t *rec) /* * modify an existing service record */ +int sdp_device_record_update_binary(sdp_session_t *session, bdaddr_t *device, uint32_t handle, uint8_t *data, uint32_t size) +{ + return -1; +} + int sdp_device_record_update(sdp_session_t *session, bdaddr_t *device, const sdp_record_t *rec) { - int status = 0; uint8_t *reqbuf, *rspbuf, *p; uint32_t reqsize, rspsize; sdp_pdu_hdr_t *reqhdr, *rsphdr; uint32_t handle; sdp_buf_t pdu; + int status; SDPDBG(""); + handle = rec->handle; if (handle == SDP_SERVER_RECORD_HANDLE) { -- cgit From a7c82c586998da5b64e680a9027a6f5aeba915f5 Mon Sep 17 00:00:00 2001 From: Claudio Takahasi Date: Thu, 9 Nov 2006 21:25:30 +0000 Subject: handling invalid sdp record sintax for registration/unregistration/update --- src/sdp.c | 47 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 12 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index d83dea76..d55f3159 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -2441,7 +2441,14 @@ int sdp_device_record_register_binary(sdp_session_t *session, bdaddr_t *device, rsphdr = (sdp_pdu_hdr_t *) rsp; p = rsp + sizeof(sdp_pdu_hdr_t); - if (rsphdr->pdu_id == SDP_SVC_REGISTER_RSP) { + if (rsphdr->pdu_id == SDP_ERROR_RSP) { + /* Invalid service record */ + errno = EINVAL; + status = -1; + } else if (rsphdr->pdu_id != SDP_SVC_REGISTER_RSP) { + errno = EPROTO; + status = -1; + } else { if (handle) *handle = ntohl(bt_get_unaligned((uint32_t *) p)); } @@ -2534,14 +2541,21 @@ int sdp_device_record_unregister_binary(sdp_session_t *session, bdaddr_t *device reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t)); status = sdp_send_req_w4_rsp(session, reqbuf, rspbuf, reqsize, &rspsize); - if (status == 0) { - rsphdr = (sdp_pdu_hdr_t *) rspbuf; - p = rspbuf + sizeof(sdp_pdu_hdr_t); - status = bt_get_unaligned((uint16_t *) p); - if (status != 0 || rsphdr->pdu_id != SDP_SVC_REMOVE_RSP) - status = -1; - } + if (status < 0) + goto end; + + rsphdr = (sdp_pdu_hdr_t *) rspbuf; + p = rspbuf + sizeof(sdp_pdu_hdr_t); + status = bt_get_unaligned((uint16_t *) p); + if (rsphdr->pdu_id == SDP_ERROR_RSP) { + /* For this case the status always is invalid record handle */ + errno = EINVAL; + status = -1; + } else if (rsphdr->pdu_id != SDP_SVC_REMOVE_RSP) { + errno = EPROTO; + status = -1; + } end: if (reqbuf) free(reqbuf); @@ -2625,13 +2639,22 @@ int sdp_device_record_update(sdp_session_t *session, bdaddr_t *device, const sdp reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t)); status = sdp_send_req_w4_rsp(session, reqbuf, rspbuf, reqsize, &rspsize); + if (status < 0) + goto end; SDPDBG("Send req status : %d\n", status); - if (status == 0) { - rsphdr = (sdp_pdu_hdr_t *) rspbuf; - p = rspbuf + sizeof(sdp_pdu_hdr_t); - status = bt_get_unaligned((uint16_t *) p); + rsphdr = (sdp_pdu_hdr_t *) rspbuf; + p = rspbuf + sizeof(sdp_pdu_hdr_t); + status = bt_get_unaligned((uint16_t *) p); + + if (rsphdr->pdu_id == SDP_ERROR_RSP) { + /* The status can be invalid sintax or invalid record handle */ + errno = EINVAL; + status = -1; + } else if (rsphdr->pdu_id != SDP_SVC_UPDATE_RSP) { + errno = EPROTO; + status = -1; } end: if (reqbuf) -- cgit From 25effaf3a661c4de960ebae7125ba56a990ad628 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sat, 13 Jan 2007 17:50:06 +0000 Subject: Update copyright information --- src/sdp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index d55f3159..4270ac83 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -4,7 +4,7 @@ * * Copyright (C) 2001-2002 Nokia Corporation * Copyright (C) 2002-2003 Maxim Krasnyansky - * Copyright (C) 2002-2006 Marcel Holtmann + * Copyright (C) 2002-2007 Marcel Holtmann * Copyright (C) 2002-2003 Stephen Crane * * -- cgit From c5621bfbf9071ed359d904bdd91308f6599dc5e5 Mon Sep 17 00:00:00 2001 From: Claudio Takahasi Date: Thu, 26 Apr 2007 19:47:36 +0000 Subject: Fixed memory leak --- src/sdp.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 4270ac83..af2cf162 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -2629,13 +2629,14 @@ int sdp_device_record_update(sdp_session_t *session, bdaddr_t *device, const sdp reqsize += sizeof(uint32_t); p += sizeof(uint32_t); - if (0 > sdp_gen_record_pdu(rec, &pdu)) { + if (sdp_gen_record_pdu(rec, &pdu) < 0) { errno = ENOMEM; status = -1; goto end; } memcpy(p, pdu.data, pdu.data_size); reqsize += pdu.data_size; + free(pdu.data); reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t)); status = sdp_send_req_w4_rsp(session, reqbuf, rspbuf, reqsize, &rspsize); -- cgit From cb08fcdb50544c554e1275ede85605e4542b5b1b Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Tue, 22 May 2007 08:13:54 +0000 Subject: Fix URL data size handling --- src/sdp.c | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index af2cf162..0414d013 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -695,61 +695,57 @@ int sdp_gen_pdu(sdp_buf_t *buf, sdp_data_t *d) break; case SDP_UINT16: u16 = htons(d->val.uint16); - src = (unsigned char *)&u16; + src = (unsigned char *) &u16; data_size = sizeof(uint16_t); break; case SDP_UINT32: u32 = htonl(d->val.uint32); - src = (unsigned char *)&u32; + src = (unsigned char *) &u32; data_size = sizeof(uint32_t); break; case SDP_UINT64: u64 = hton64(d->val.uint64); - src = (unsigned char *)&u64; + src = (unsigned char *) &u64; data_size = sizeof(uint64_t); break; case SDP_UINT128: hton128(&d->val.uint128, &u128); - src = (unsigned char *)&u128; + src = (unsigned char *) &u128; data_size = sizeof(uint128_t); break; case SDP_INT8: case SDP_BOOL: - src = (unsigned char *)&d->val.int8; + src = (unsigned char *) &d->val.int8; data_size = sizeof(int8_t); break; case SDP_INT16: u16 = htons(d->val.int16); - src = (unsigned char *)&u16; + src = (unsigned char *) &u16; data_size = sizeof(int16_t); break; case SDP_INT32: u32 = htonl(d->val.int32); - src = (unsigned char *)&u32; + src = (unsigned char *) &u32; data_size = sizeof(int32_t); break; case SDP_INT64: u64 = hton64(d->val.int64); - src = (unsigned char *)&u64; + src = (unsigned char *) &u64; data_size = sizeof(int64_t); break; case SDP_INT128: hton128(&d->val.int128, &u128); - src = (unsigned char *)&u128; + src = (unsigned char *) &u128; data_size = sizeof(uint128_t); break; case SDP_TEXT_STR8: case SDP_TEXT_STR16: case SDP_TEXT_STR32: - src = (unsigned char *)d->val.str; - data_size = d->unitSize - sizeof(uint8_t); - sdp_set_seq_len(seqp, data_size); - break; case SDP_URL_STR8: case SDP_URL_STR16: case SDP_URL_STR32: - src = (unsigned char *)d->val.str; - data_size = strlen(d->val.str); + src = (unsigned char *) d->val.str; + data_size = d->unitSize - sizeof(uint8_t); sdp_set_seq_len(seqp, data_size); break; case SDP_SEQ8: @@ -768,16 +764,16 @@ int sdp_gen_pdu(sdp_buf_t *buf, sdp_data_t *d) break; case SDP_UUID16: u16 = htons(d->val.uuid.value.uuid16); - src = (unsigned char *)&u16; + src = (unsigned char *) &u16; data_size = sizeof(uint16_t); break; case SDP_UUID32: u32 = htonl(d->val.uuid.value.uuid32); - src = (unsigned char *)&u32; + src = (unsigned char *) &u32; data_size = sizeof(uint32_t); break; case SDP_UUID128: - src = (unsigned char *)&d->val.uuid.value.uuid128; + src = (unsigned char *) &d->val.uuid.value.uuid128; data_size = sizeof(uint128_t); break; default: -- cgit From 9d9a7646b189cc2471b925cc448862e14c10c0c4 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Fri, 15 Jun 2007 11:00:46 +0000 Subject: Check record struct pointer before adding an UUID --- src/sdp.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 0414d013..920bc880 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -922,7 +922,8 @@ static sdp_data_t *extract_uuid(const uint8_t *p, int *len, sdp_record_t *rec) return NULL; } d->dtd = *(uint8_t *) p; - sdp_pattern_add_uuid(rec, &d->val.uuid); + if (rec) + sdp_pattern_add_uuid(rec, &d->val.uuid); return d; } -- cgit From 4c6bbf0a08426efd008c1b9e439c693a826ee708 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Wed, 22 Aug 2007 00:31:24 +0000 Subject: Extract main service class for later use --- src/sdp.c | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 920bc880..c39ad64d 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -574,22 +574,48 @@ sdp_data_t *sdp_seq_alloc(void **dtds, void **values, int len) return sdp_data_alloc(SDP_SEQ8, seq); } +static void extract_svclass_uuid(sdp_data_t *data, uuid_t *uuid) +{ + sdp_data_t *d; + + if (!data || data->dtd < SDP_SEQ8 || data->dtd > SDP_SEQ32) + return; + + d = data->val.dataseq; + if (!d) + return; + + if (d->dtd < SDP_UUID16 || d->dtd > SDP_UUID128) + return; + + *uuid = d->val.uuid; +} + int sdp_attr_add(sdp_record_t *rec, uint16_t attr, sdp_data_t *d) { sdp_data_t *p = sdp_data_get(rec, attr); if (p) return -1; + d->attrId = attr; rec->attrlist = sdp_list_insert_sorted(rec->attrlist, d, sdp_attrid_comp_func); + + if (attr == SDP_ATTR_SVCLASS_ID_LIST) + extract_svclass_uuid(d, &rec->svclass); + return 0; } void sdp_attr_remove(sdp_record_t *rec, uint16_t attr) { sdp_data_t *d = sdp_data_get(rec, attr); + if (d) rec->attrlist = sdp_list_remove(rec->attrlist, d); + + if (attr == SDP_ATTR_SVCLASS_ID_LIST) + memset(&rec->svclass, 0, sizeof(rec->svclass)); } void sdp_set_seq_len(uint8_t *ptr, uint32_t length) @@ -820,8 +846,12 @@ void sdp_attr_replace(sdp_record_t *rec, uint16_t attr, sdp_data_t *d) rec->attrlist = sdp_list_remove(rec->attrlist, p); sdp_data_free(p); } + d->attrId = attr; - rec->attrlist = sdp_list_insert_sorted(rec->attrlist, (void *)d, sdp_attrid_comp_func); + rec->attrlist = sdp_list_insert_sorted(rec->attrlist, d, sdp_attrid_comp_func); + + if (attr == SDP_ATTR_SVCLASS_ID_LIST) + extract_svclass_uuid(d, &rec->svclass); } int sdp_attrid_comp_func(const void *key1, const void *key2) @@ -1114,8 +1144,13 @@ sdp_record_t *sdp_extract_pdu(const uint8_t *buf, int *scanned) SDPDBG("Terminating extraction of attributes"); break; } + if (attr == SDP_ATTR_RECORD_HANDLE) rec->handle = data->val.uint32; + + if (attr == SDP_ATTR_SVCLASS_ID_LIST) + extract_svclass_uuid(data, &rec->svclass); + extracted += n; p += n; sdp_attr_replace(rec, attr, data); @@ -1215,7 +1250,7 @@ sdp_data_t *sdp_data_get(const sdp_record_t *rec, uint16_t attrId) if (p) return (sdp_data_t *)p->data; } - return 0; + return NULL; } /* -- cgit From 3f8700636d21bb868c2dbb8d2403e550a7750ceb Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Fri, 31 Aug 2007 18:43:40 +0000 Subject: Don't allocate memory for the Bluetooth base UUID --- src/sdp.c | 149 ++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 78 insertions(+), 71 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index c39ad64d..fcf5c897 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -88,7 +88,10 @@ static inline void ntoh128(uint128_t *src, uint128_t *dst) #define BASE_UUID "00000000-0000-1000-8000-00805F9B34FB" -static uint128_t *bluetooth_base_uuid = NULL; +static uint128_t bluetooth_base_uuid = { + .data = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, + 0x80, 0x00, 0x00, 0x80, 0x5F, 0xF9, 0xB3, 0xFB } +}; #define SDP_MAX_ATTR_LEN 65535 @@ -2078,6 +2081,7 @@ void sdp_set_url_attr(sdp_record_t *rec, const char *client, const char *doc, co */ uint128_t *sdp_create_base_uuid(void) { + uint128_t *base_uuid; char baseStr[128]; int delim = '-'; unsigned long dataLongValue; @@ -2087,69 +2091,75 @@ uint128_t *sdp_create_base_uuid(void) int toBeCopied; uint8_t *data; - if (bluetooth_base_uuid == NULL) { - strcpy(baseStr, BASE_UUID); - bluetooth_base_uuid = malloc(sizeof(uint128_t)); - data = bluetooth_base_uuid->data; - memset(data, '\0', sizeof(uint128_t)); - memset(temp, '\0', 10); - dataPtr = baseStr; - delimPtr = NULL; - delimPtr = strchr(dataPtr, delim); - toBeCopied = delimPtr - dataPtr; - if (toBeCopied != 8) { - SDPDBG("To be copied(1) : %d\n", toBeCopied); - return NULL; - } - strncpy(temp, dataPtr, toBeCopied); - dataLongValue = htonl(strtoul(temp, NULL, 16)); - memcpy(&data[0], &dataLongValue, 4); + strcpy(baseStr, BASE_UUID); + base_uuid = malloc(sizeof(uint128_t)); + if (!base_uuid) + return NULL; - /* - * Get the next 4 bytes (note that there is a "-" - * between them now) - */ - memset(temp, '\0', 10); - dataPtr = delimPtr + 1; - delimPtr = strchr(dataPtr, delim); - toBeCopied = delimPtr - dataPtr; - if (toBeCopied != 4) { - SDPDBG("To be copied(2) : %d\n", toBeCopied); - return NULL; - } - strncpy(temp, dataPtr, toBeCopied); - dataPtr = delimPtr + 1; - delimPtr = strchr(dataPtr, delim); - toBeCopied = delimPtr - dataPtr; - if (toBeCopied != 4) { - SDPDBG("To be copied(3) : %d\n", toBeCopied); - return NULL; - } - strncat(temp, dataPtr, toBeCopied); - dataLongValue = htonl(strtoul(temp, NULL, 16)); - memcpy(&data[4], &dataLongValue, 4); + data = base_uuid->data; + memset(data, '\0', sizeof(uint128_t)); + memset(temp, '\0', 10); + dataPtr = baseStr; + delimPtr = NULL; + delimPtr = strchr(dataPtr, delim); + toBeCopied = delimPtr - dataPtr; + if (toBeCopied != 8) { + SDPDBG("To be copied(1) : %d\n", toBeCopied); + free(base_uuid); + return NULL; + } + strncpy(temp, dataPtr, toBeCopied); + dataLongValue = htonl(strtoul(temp, NULL, 16)); + memcpy(&data[0], &dataLongValue, 4); - /* - * Get the last 4 bytes (note that there are 6 bytes - * after the last separator, which is truncated (2+4) - */ - memset(temp, '\0', 10); - dataPtr = delimPtr + 1; - dataPtr = delimPtr + 1; - delimPtr = strchr(dataPtr, delim); - toBeCopied = delimPtr - dataPtr; - if (toBeCopied != 4) { - SDPDBG("To be copied(4) : %d\n", toBeCopied); - return NULL; - } - strncpy(temp, dataPtr, toBeCopied); - strncat(temp, (delimPtr + 1), 4); - dataLongValue = htonl(strtoul(temp, NULL, 16)); - memcpy(&data[8], &dataLongValue, 4); - dataLongValue = htonl(strtoul(delimPtr + 5, NULL, 16)); - memcpy(&data[12], &dataLongValue, 4); + /* + * Get the next 4 bytes (note that there is a "-" + * between them now) + */ + memset(temp, '\0', 10); + dataPtr = delimPtr + 1; + delimPtr = strchr(dataPtr, delim); + toBeCopied = delimPtr - dataPtr; + if (toBeCopied != 4) { + SDPDBG("To be copied(2) : %d\n", toBeCopied); + free(base_uuid); + return NULL; + } + strncpy(temp, dataPtr, toBeCopied); + dataPtr = delimPtr + 1; + delimPtr = strchr(dataPtr, delim); + toBeCopied = delimPtr - dataPtr; + if (toBeCopied != 4) { + SDPDBG("To be copied(3) : %d\n", toBeCopied); + free(base_uuid); + return NULL; + } + strncat(temp, dataPtr, toBeCopied); + dataLongValue = htonl(strtoul(temp, NULL, 16)); + memcpy(&data[4], &dataLongValue, 4); + + /* + * Get the last 4 bytes (note that there are 6 bytes + * after the last separator, which is truncated (2+4) + */ + memset(temp, '\0', 10); + dataPtr = delimPtr + 1; + dataPtr = delimPtr + 1; + delimPtr = strchr(dataPtr, delim); + toBeCopied = delimPtr - dataPtr; + if (toBeCopied != 4) { + SDPDBG("To be copied(4) : %d\n", toBeCopied); + free(base_uuid); + return NULL; } - return bluetooth_base_uuid; + strncpy(temp, dataPtr, toBeCopied); + strncat(temp, (delimPtr + 1), 4); + dataLongValue = htonl(strtoul(temp, NULL, 16)); + memcpy(&data[8], &dataLongValue, 4); + dataLongValue = htonl(strtoul(delimPtr + 5, NULL, 16)); + memcpy(&data[12], &dataLongValue, 4); + + return base_uuid; } uuid_t *sdp_uuid16_create(uuid_t *u, uint16_t val) @@ -2212,12 +2222,11 @@ void sdp_uuid16_to_uuid128(uuid_t *uuid128, uuid_t *uuid16) unsigned short data1; // allocate a 128bit UUID and init to the Bluetooth base UUID - uint128_t *pBTBase128Bit = sdp_create_base_uuid(); - uuid128->value.uuid128 = *pBTBase128Bit; + uuid128->value.uuid128 = bluetooth_base_uuid; uuid128->type = SDP_UUID128; // extract bytes 2 and 3 of 128bit BT base UUID - memcpy(&data1, &pBTBase128Bit->data[2], 2); + memcpy(&data1, &bluetooth_base_uuid.data[2], 2); // add the given UUID (16 bits) data1 += htons(uuid16->value.uuid16); @@ -2235,12 +2244,11 @@ void sdp_uuid32_to_uuid128(uuid_t *uuid128, uuid_t *uuid32) unsigned int data0; // allocate a 128bit UUID and init to the Bluetooth base UUID - uint128_t *pBTBase128Bit = sdp_create_base_uuid(); - uuid128->value.uuid128 = *pBTBase128Bit; + uuid128->value.uuid128 = bluetooth_base_uuid; uuid128->type = SDP_UUID128; // extract first 4 bytes - memcpy(&data0, &pBTBase128Bit->data[0], 4); + memcpy(&data0, &bluetooth_base_uuid.data[0], 4); // add the given UUID (32bits) data0 += htonl(uuid32->value.uuid32); @@ -2273,11 +2281,10 @@ uuid_t *sdp_uuid_to_uuid128(uuid_t *uuid) */ int sdp_uuid128_to_uuid(uuid_t *uuid) { - extern uint128_t *sdp_create_base_uuid(); - int i; - uint128_t *b = sdp_create_base_uuid(); + uint128_t *b = &bluetooth_base_uuid; uint128_t *u = &uuid->value.uuid128; uint32_t data; + int i; if (uuid->type != SDP_UUID128) return 1; @@ -2290,7 +2297,7 @@ int sdp_uuid128_to_uuid(uuid_t *uuid) data = htonl(data); if (data <= 0xffff) { uuid->type = SDP_UUID16; - uuid->value.uuid16 = (uint16_t)data; + uuid->value.uuid16 = (uint16_t) data; } else { uuid->type = SDP_UUID32; uuid->value.uuid32 = data; -- cgit From c30706178221e3df5dfe82eb5c91f39fe118eb78 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Fri, 31 Aug 2007 20:08:52 +0000 Subject: Fix Bluetooth base UUID typo --- src/sdp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index fcf5c897..cf1a9895 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -90,7 +90,7 @@ static inline void ntoh128(uint128_t *src, uint128_t *dst) static uint128_t bluetooth_base_uuid = { .data = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, - 0x80, 0x00, 0x00, 0x80, 0x5F, 0xF9, 0xB3, 0xFB } + 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB } }; #define SDP_MAX_ATTR_LEN 65535 -- cgit From 1622daf2f8fa0abeb0a11be14558ed30a17fc044 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Tue, 25 Dec 2007 20:35:41 +0000 Subject: Add definitions for MDP --- src/sdp.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index cf1a9895..16dd7774 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -192,6 +192,9 @@ static struct tupla ServiceClass[] = { { VIDEO_SOURCE_SVCLASS_ID, "Video Source" }, { VIDEO_SINK_SVCLASS_ID, "Video Sink" }, { VIDEO_DISTRIBUTION_SVCLASS_ID, "Video Distribution" }, + { MDP_SVCLASS_ID, "MDP" }, + { MDP_SOURCE_SVCLASS_ID, "MDP Source" }, + { MDP_SINK_SVCLASS_ID, "MDP Sink" }, { APPLE_AGENT_SVCLASS_ID, "Apple Agent" }, { 0 } }; -- cgit From 7208028266fc19d380ac77c97c46b6f2fdc80e1d Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sat, 2 Feb 2008 03:11:09 +0000 Subject: Update copyright information --- src/sdp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 16dd7774..40d694de 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -4,7 +4,7 @@ * * Copyright (C) 2001-2002 Nokia Corporation * Copyright (C) 2002-2003 Maxim Krasnyansky - * Copyright (C) 2002-2007 Marcel Holtmann + * Copyright (C) 2002-2008 Marcel Holtmann * Copyright (C) 2002-2003 Stephen Crane * * -- cgit From b393cdff611943a05ec598048b5c4be8130151dd Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sat, 2 Feb 2008 13:17:16 +0000 Subject: Fix compilation problem with USHRT_MAX and UCHAR_MAX --- src/sdp.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 40d694de..3b1a5d93 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -30,9 +30,10 @@ #include #include -#include #include +#include #include +#include #include #include #include -- cgit From f767416e71484be2463caa6482dc978e165cabae Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Wed, 5 Mar 2008 19:30:32 +0000 Subject: Add support for MCAP UUIDs --- src/sdp.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 3b1a5d93..44ba17e4 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -124,6 +124,8 @@ static struct tupla Protocol[] = { { AVDTP_UUID, "AVDTP" }, { CMTP_UUID, "CMTP" }, { UDI_UUID, "UDI" }, + { MCAP_CTRL_UUID, "MCAP-Ctrl" }, + { MCAP_DATA_UUID, "MCAP-Data" }, { L2CAP_UUID, "L2CAP" }, { 0 } }; -- cgit From 1f9ec47e02cc789ab45c85e855c4f9e377df7e3c Mon Sep 17 00:00:00 2001 From: Claudio Takahasi Date: Mon, 10 Mar 2008 21:57:46 +0000 Subject: sdp_get_uuidseq_attr: fixed memory leak --- src/sdp.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 44ba17e4..28e24c0a 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -1530,13 +1530,14 @@ int sdp_get_uuidseq_attr(const sdp_record_t *rec, uint16_t attr, sdp_list_t **se if (sdpdata && sdpdata->dtd >= SDP_SEQ8 && sdpdata->dtd <= SDP_SEQ32) { sdp_data_t *d; for (d = sdpdata->val.dataseq; d; d = d->next) { - uuid_t *u = malloc(sizeof(uuid_t)); - memset((char *)u, 0, sizeof(uuid_t)); - if (d->dtd >= SDP_UUID16 && d->dtd <= SDP_UUID128) { - *u = d->val.uuid; - *seqp = sdp_list_append(*seqp, u); - } else + uuid_t *u; + if (d->dtd < SDP_UUID16 || d->dtd > SDP_UUID128) goto fail; + + u = malloc(sizeof(uuid_t)); + memset(u, 0, sizeof(uuid_t)); + *u = d->val.uuid; + *seqp = sdp_list_append(*seqp, u); } return 0; } -- cgit From bb45b882917f26aaa26b2b903b70d622b39cce84 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Wed, 11 Jun 2008 13:12:21 +0000 Subject: It is better to include string.h instead of malloc.h --- src/sdp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 28e24c0a..3ec877a1 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -34,7 +34,7 @@ #include #include #include -#include +#include #include #include #include -- cgit From a3648e0df0ff53fef74e5437ff089aef96209db2 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Fri, 20 Jun 2008 03:30:53 +0000 Subject: Add more safe version of three low-level extraction functions --- src/sdp.c | 299 +++++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 220 insertions(+), 79 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 3ec877a1..977f6660 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -904,15 +904,76 @@ void sdp_data_free(sdp_data_t *d) free(d); } -static sdp_data_t *extract_int(const void *p, int *len) +int sdp_uuid_extract_safe(const uint8_t *p, int bufsize, uuid_t *uuid, int *scanned) { - sdp_data_t *d = malloc(sizeof(sdp_data_t)); + uint8_t type; + + if (bufsize < sizeof(uint8_t)) { + SDPERR("Unexpected end of packet"); + return -1; + } + + type = *(const uint8_t *) p; + + if (!SDP_IS_UUID(type)) { + SDPERR("Unknown data type : %d expecting a svc UUID\n", type); + return -1; + } + p += sizeof(uint8_t); + *scanned += sizeof(uint8_t); + bufsize -= sizeof(uint8_t); + if (type == SDP_UUID16) { + if (bufsize < sizeof(uint16_t)) { + SDPERR("Not enough room for 16-bit UUID"); + return -1; + } + sdp_uuid16_create(uuid, ntohs(bt_get_unaligned((uint16_t *) p))); + *scanned += sizeof(uint16_t); + p += sizeof(uint16_t); + } else if (type == SDP_UUID32) { + if (bufsize < sizeof(uint32_t)) { + SDPERR("Not enough room for 32-bit UUID"); + return -1; + } + sdp_uuid32_create(uuid, ntohl(bt_get_unaligned((uint32_t *) p))); + *scanned += sizeof(uint32_t); + p += sizeof(uint32_t); + } else { + if (bufsize < sizeof(uint128_t)) { + SDPERR("Not enough room for 128-bit UUID"); + return -1; + } + sdp_uuid128_create(uuid, p); + *scanned += sizeof(uint128_t); + p += sizeof(uint128_t); + } + return 0; +} + +int sdp_uuid_extract(const uint8_t *p, uuid_t *uuid, int *scanned) +{ + /* Assume p points to a buffer of size at least SDP_MAX_ATTR_LEN, + because we don't have any better information */ + return sdp_uuid_extract_safe(p, SDP_MAX_ATTR_LEN, uuid, scanned); +} + +static sdp_data_t *extract_int(const void *p, int bufsize, int *len) +{ + sdp_data_t *d; + + if (bufsize < sizeof(uint8_t)) { + SDPERR("Unexpected end of packet"); + return NULL; + } + + d = malloc(sizeof(sdp_data_t)); SDPDBG("Extracting integer\n"); memset(d, 0, sizeof(sdp_data_t)); d->dtd = *(uint8_t *) p; p += sizeof(uint8_t); *len += sizeof(uint8_t); + bufsize -= sizeof(uint8_t); switch (d->dtd) { case SDP_DATA_NIL: @@ -920,26 +981,51 @@ static sdp_data_t *extract_int(const void *p, int *len) case SDP_BOOL: case SDP_INT8: case SDP_UINT8: + if (bufsize < sizeof(uint8_t)) { + SDPERR("Unexpected end of packet"); + free(d); + return NULL; + } *len += sizeof(uint8_t); d->val.uint8 = *(uint8_t *) p; break; case SDP_INT16: case SDP_UINT16: + if (bufsize < sizeof(uint16_t)) { + SDPERR("Unexpected end of packet"); + free(d); + return NULL; + } *len += sizeof(uint16_t); d->val.uint16 = ntohs(bt_get_unaligned((uint16_t *) p)); break; case SDP_INT32: case SDP_UINT32: + if (bufsize < sizeof(uint32_t)) { + SDPERR("Unexpected end of packet"); + free(d); + return NULL; + } *len += sizeof(uint32_t); d->val.uint32 = ntohl(bt_get_unaligned((uint32_t *) p)); break; case SDP_INT64: case SDP_UINT64: + if (bufsize < sizeof(uint64_t)) { + SDPERR("Unexpected end of packet"); + free(d); + return NULL; + } *len += sizeof(uint64_t); d->val.uint64 = ntoh64(bt_get_unaligned((uint64_t *) p)); break; case SDP_INT128: case SDP_UINT128: + if (bufsize < sizeof(uint128_t)) { + SDPERR("Unexpected end of packet"); + free(d); + return NULL; + } *len += sizeof(uint128_t); ntoh128((uint128_t *) p, &d->val.uint128); break; @@ -950,13 +1036,13 @@ static sdp_data_t *extract_int(const void *p, int *len) return d; } -static sdp_data_t *extract_uuid(const uint8_t *p, int *len, sdp_record_t *rec) +static sdp_data_t *extract_uuid(const uint8_t *p, int bufsize, int *len, sdp_record_t *rec) { sdp_data_t *d = malloc(sizeof(sdp_data_t)); SDPDBG("Extracting UUID"); memset(d, 0, sizeof(sdp_data_t)); - if (sdp_uuid_extract(p, &d->val.uuid, len) < 0) { + if (sdp_uuid_extract_safe(p, bufsize, &d->val.uuid, len) < 0) { free(d); return NULL; } @@ -969,29 +1055,49 @@ static sdp_data_t *extract_uuid(const uint8_t *p, int *len, sdp_record_t *rec) /* * Extract strings from the PDU (could be service description and similar info) */ -static sdp_data_t *extract_str(const void *p, int *len) +static sdp_data_t *extract_str(const void *p, int bufsize, int *len) { char *s; int n; - sdp_data_t *d = malloc(sizeof(sdp_data_t)); + sdp_data_t *d; + + if (bufsize < sizeof(uint8_t)) { + SDPERR("Unexpected end of packet"); + return NULL; + } + + d = malloc(sizeof(sdp_data_t)); memset(d, 0, sizeof(sdp_data_t)); d->dtd = *(uint8_t *) p; p += sizeof(uint8_t); *len += sizeof(uint8_t); + bufsize -= sizeof(uint8_t); switch (d->dtd) { case SDP_TEXT_STR8: case SDP_URL_STR8: + if (bufsize < sizeof(uint8_t)) { + SDPERR("Unexpected end of packet"); + free(d); + return NULL; + } n = *(uint8_t *) p; p += sizeof(uint8_t); - *len += sizeof(uint8_t) + n; + *len += sizeof(uint8_t); + bufsize -= sizeof(uint8_t); break; case SDP_TEXT_STR16: case SDP_URL_STR16: + if (bufsize < sizeof(uint16_t)) { + SDPERR("Unexpected end of packet"); + free(d); + return NULL; + } n = ntohs(bt_get_unaligned((uint16_t *) p)); p += sizeof(uint16_t); *len += sizeof(uint16_t) + n; + bufsize -= sizeof(uint16_t); break; default: SDPERR("Sizeof text string > UINT16_MAX\n"); @@ -999,10 +1105,23 @@ static sdp_data_t *extract_str(const void *p, int *len) return 0; } + if (bufsize < n) { + SDPERR("String too long to fit in packet"); + free(d); + return NULL; + } + s = malloc(n + 1); + if (!s) { + SDPERR("Not enough memory for incoming string"); + free(d); + return NULL; + } memset(s, 0, n + 1); memcpy(s, p, n); + *len += n; + SDPDBG("Len : %d\n", n); SDPDBG("Str : %s\n", s); @@ -1011,7 +1130,67 @@ static sdp_data_t *extract_str(const void *p, int *len) return d; } -static sdp_data_t *extract_seq(const void *p, int *len, sdp_record_t *rec) +/* + * Extract the sequence type and its length, and return offset into buf + * or 0 on failure. + */ +int sdp_extract_seqtype_safe(const uint8_t *buf, int bufsize, uint8_t *dtdp, int *size) +{ + uint8_t dtd; + int scanned = sizeof(uint8_t); + + if (bufsize < sizeof(uint8_t)) { + SDPERR("Unexpected end of packet"); + return 0; + } + + dtd = *(uint8_t *) buf; + buf += sizeof(uint8_t); + bufsize -= sizeof(uint8_t); + *dtdp = dtd; + switch (dtd) { + case SDP_SEQ8: + case SDP_ALT8: + if (bufsize < sizeof(uint8_t)) { + SDPERR("Unexpected end of packet"); + return 0; + } + *size = *(uint8_t *) buf; + scanned += sizeof(uint8_t); + break; + case SDP_SEQ16: + case SDP_ALT16: + if (bufsize < sizeof(uint16_t)) { + SDPERR("Unexpected end of packet"); + return 0; + } + *size = ntohs(bt_get_unaligned((uint16_t *) buf)); + scanned += sizeof(uint16_t); + break; + case SDP_SEQ32: + case SDP_ALT32: + if (bufsize < sizeof(uint32_t)) { + SDPERR("Unexpected end of packet"); + return 0; + } + *size = ntohl(bt_get_unaligned((uint32_t *) buf)); + scanned += sizeof(uint32_t); + break; + default: + SDPERR("Unknown sequence type, aborting\n"); + return 0; + } + return scanned; +} + +int sdp_extract_seqtype(const uint8_t *buf, uint8_t *dtdp, int *size) +{ + /* Assume buf points to a buffer of size at least SDP_MAX_ATTR_LEN, + because we don't have any better information */ + return sdp_extract_seqtype_safe(buf, SDP_MAX_ATTR_LEN, dtdp, size); +} + +static sdp_data_t *extract_seq(const void *p, int bufsize, int *len, sdp_record_t *rec) { int seqlen, n = 0; sdp_data_t *curr, *prev; @@ -1019,17 +1198,24 @@ static sdp_data_t *extract_seq(const void *p, int *len, sdp_record_t *rec) SDPDBG("Extracting SEQ"); memset(d, 0, sizeof(sdp_data_t)); - *len = sdp_extract_seqtype(p, &d->dtd, &seqlen); + *len = sdp_extract_seqtype_safe(p, bufsize, &d->dtd, &seqlen); SDPDBG("Sequence Type : 0x%x length : 0x%x\n", d->dtd, seqlen); if (*len == 0) return d; + if (*len > bufsize) { + SDPERR("Packet not big enough to hold sequence."); + free(d); + return NULL; + } + p += *len; + bufsize -= *len; curr = prev = NULL; while (n < seqlen) { int attrlen = 0; - curr = sdp_extract_attr(p, &attrlen, rec); + curr = sdp_extract_attr_safe(p, bufsize, &attrlen, rec); if (curr == NULL) break; @@ -1040,6 +1226,7 @@ static sdp_data_t *extract_seq(const void *p, int *len, sdp_record_t *rec) prev = curr; p += attrlen; n += attrlen; + bufsize -= attrlen; SDPDBG("Extracted: %d SequenceLength: %d", n, seqlen); } @@ -1048,11 +1235,18 @@ static sdp_data_t *extract_seq(const void *p, int *len, sdp_record_t *rec) return d; } -sdp_data_t *sdp_extract_attr(const uint8_t *p, int *size, sdp_record_t *rec) +sdp_data_t *sdp_extract_attr_safe(const uint8_t *p, int bufsize, int *size, sdp_record_t *rec) { sdp_data_t *elem; int n = 0; - uint8_t dtd = *(const uint8_t *)p; + uint8_t dtd; + + if (bufsize < sizeof(uint8_t)) { + SDPERR("Unexpected end of packet"); + return NULL; + } + + dtd = *(const uint8_t *)p; SDPDBG("extract_attr: dtd=0x%x", dtd); switch (dtd) { @@ -1068,12 +1262,12 @@ sdp_data_t *sdp_extract_attr(const uint8_t *p, int *size, sdp_record_t *rec) case SDP_INT32: case SDP_INT64: case SDP_INT128: - elem = extract_int(p, &n); + elem = extract_int(p, bufsize, &n); break; case SDP_UUID16: case SDP_UUID32: case SDP_UUID128: - elem = extract_uuid(p, &n, rec); + elem = extract_uuid(p, bufsize, &n, rec); break; case SDP_TEXT_STR8: case SDP_TEXT_STR16: @@ -1081,7 +1275,7 @@ sdp_data_t *sdp_extract_attr(const uint8_t *p, int *size, sdp_record_t *rec) case SDP_URL_STR8: case SDP_URL_STR16: case SDP_URL_STR32: - elem = extract_str(p, &n); + elem = extract_str(p, bufsize, &n); break; case SDP_SEQ8: case SDP_SEQ16: @@ -1089,7 +1283,7 @@ sdp_data_t *sdp_extract_attr(const uint8_t *p, int *size, sdp_record_t *rec) case SDP_ALT8: case SDP_ALT16: case SDP_ALT32: - elem = extract_seq(p, &n, rec); + elem = extract_seq(p, bufsize, &n, rec); break; default: SDPERR("Unknown data descriptor : 0x%x terminating\n", dtd); @@ -1099,6 +1293,13 @@ sdp_data_t *sdp_extract_attr(const uint8_t *p, int *size, sdp_record_t *rec) return elem; } +sdp_data_t *sdp_extract_attr(const uint8_t *p, int *size, sdp_record_t *rec) +{ + /* Assume p points to a buffer of size at least SDP_MAX_ATTR_LEN, + because we don't have any better information */ + return sdp_extract_attr_safe(p, SDP_MAX_ATTR_LEN, size, rec); +} + #ifdef SDP_DEBUG static void attr_print_func(void *value, void *userData) { @@ -1262,40 +1463,6 @@ sdp_data_t *sdp_data_get(const sdp_record_t *rec, uint16_t attrId) return NULL; } -/* - * Extract the sequence type and its length, and return offset into buf - * or 0 on failure. - */ -int sdp_extract_seqtype(const uint8_t *buf, uint8_t *dtdp, int *size) -{ - uint8_t dtd = *(uint8_t *) buf; - int scanned = sizeof(uint8_t); - - buf += sizeof(uint8_t); - *dtdp = dtd; - switch (dtd) { - case SDP_SEQ8: - case SDP_ALT8: - *size = *(uint8_t *) buf; - scanned += sizeof(uint8_t); - break; - case SDP_SEQ16: - case SDP_ALT16: - *size = ntohs(bt_get_unaligned((uint16_t *) buf)); - scanned += sizeof(uint16_t); - break; - case SDP_SEQ32: - case SDP_ALT32: - *size = ntohl(bt_get_unaligned((uint32_t *) buf)); - scanned += sizeof(uint32_t); - break; - default: - SDPERR("Unknown sequence type, aborting\n"); - return 0; - } - return scanned; -} - int sdp_send_req(sdp_session_t *session, uint8_t *buf, uint32_t size) { uint32_t sent = 0; @@ -2329,32 +2496,6 @@ int sdp_uuid_to_proto(uuid_t *uuid) return 0; } -int sdp_uuid_extract(const uint8_t *p, uuid_t *uuid, int *scanned) -{ - uint8_t type = *(const uint8_t *) p; - - if (!SDP_IS_UUID(type)) { - SDPERR("Unknown data type : %d expecting a svc UUID\n", type); - return -1; - } - p += sizeof(uint8_t); - *scanned += sizeof(uint8_t); - if (type == SDP_UUID16) { - sdp_uuid16_create(uuid, ntohs(bt_get_unaligned((uint16_t *) p))); - *scanned += sizeof(uint16_t); - p += sizeof(uint16_t); - } else if (type == SDP_UUID32) { - sdp_uuid32_create(uuid, ntohl(bt_get_unaligned((uint32_t *) p))); - *scanned += sizeof(uint32_t); - p += sizeof(uint32_t); - } else { - sdp_uuid128_create(uuid, p); - *scanned += sizeof(uint128_t); - p += sizeof(uint128_t); - } - return 0; -} - /* * This function appends data to the PDU buffer "dst" from source "src". * The data length is also computed and set. @@ -2792,7 +2933,7 @@ static int gen_dataseq_pdu(uint8_t *dst, const sdp_list_t *seq, uint8_t dtd) // Fill up the value and the dtd arrays SDPDBG(""); - + memset(&buf, 0, sizeof(sdp_buf_t)); buf.data = malloc(256); buf.buf_size = 256; @@ -2986,7 +3127,7 @@ int sdp_service_search_req(sdp_session_t *session, const sdp_list_t *search, } } while (cstate); - end: +end: if (reqbuf) free(reqbuf); if (rspbuf) @@ -3146,7 +3287,7 @@ sdp_record_t *sdp_service_attr_req(sdp_session_t *session, uint32_t handle, status = -1; } - end: +end: if (reqbuf) free(reqbuf); if (rsp_concat_buf.data) -- cgit From 0e1789b3f1897512eb30d63c8e9803f1461446c1 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sun, 22 Jun 2008 21:59:42 +0000 Subject: Add safe version of sdp_extract_pdu function --- src/sdp.c | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 977f6660..b9b0e4f6 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -1323,7 +1323,7 @@ void sdp_print_service_attr(sdp_list_t *svcAttrList) } #endif -sdp_record_t *sdp_extract_pdu(const uint8_t *buf, int *scanned) +sdp_record_t *sdp_extract_pdu_safe(const uint8_t *buf, int bufsize, int *scanned) { int extracted = 0, seqlen = 0; uint8_t dtd; @@ -1331,21 +1331,30 @@ sdp_record_t *sdp_extract_pdu(const uint8_t *buf, int *scanned) sdp_record_t *rec = sdp_record_alloc(); const uint8_t *p = buf; - *scanned = sdp_extract_seqtype(buf, &dtd, &seqlen); + *scanned = sdp_extract_seqtype_safe(buf, bufsize, &dtd, &seqlen); p += *scanned; + bufsize -= *scanned; rec->attrlist = NULL; - while (extracted < seqlen) { + + while (extracted < seqlen && bufsize > 0) { int n = sizeof(uint8_t), attrlen = 0; sdp_data_t *data = NULL; - SDPDBG("Extract PDU, sequenceLength: %d localExtractedLength: %d", seqlen, extracted); + SDPDBG("Extract PDU, sequenceLength: %d localExtractedLength: %d", + seqlen, extracted); + + if (bufsize < n + sizeof(uint16_t)) { + SDPERR("Unexpected end of packet"); + break; + } + dtd = *(uint8_t *) p; attr = ntohs(bt_get_unaligned((uint16_t *) (p + n))); n += sizeof(uint16_t); SDPDBG("DTD of attrId : %d Attr id : 0x%x \n", dtd, attr); - data = sdp_extract_attr(p + n, &attrlen, rec); + data = sdp_extract_attr_safe(p + n, bufsize - n, &attrlen, rec); SDPDBG("Attr id : 0x%x attrValueLength : %d\n", attr, attrlen); @@ -1363,9 +1372,11 @@ sdp_record_t *sdp_extract_pdu(const uint8_t *buf, int *scanned) extracted += n; p += n; + bufsize -= n; sdp_attr_replace(rec, attr, data); + SDPDBG("Extract PDU, seqLength: %d localExtractedLength: %d", - seqlen, extracted); + seqlen, extracted); } #ifdef SDP_DEBUG SDPDBG("Successful extracting of Svc Rec attributes\n"); @@ -1375,6 +1386,13 @@ sdp_record_t *sdp_extract_pdu(const uint8_t *buf, int *scanned) return rec; } +sdp_record_t *sdp_extract_pdu(const uint8_t *buf, int *scanned) +{ + /* Assume buf points to a buffer of size at least SDP_MAX_ATTR_LEN, + because we don't have any better information */ + return sdp_extract_pdu_safe(buf, SDP_MAX_ATTR_LEN, scanned); +} + #ifdef SDP_DEBUG static void print_dataseq(sdp_data_t *p) { -- cgit From 0a8f7c9206e17ab118966bf7da8a053ef34dce84 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Mon, 23 Jun 2008 02:49:12 +0000 Subject: Add safety check for continuation state function --- src/sdp.c | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index b9b0e4f6..59686388 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -3002,12 +3002,17 @@ typedef struct { unsigned char data[16]; } __attribute__ ((packed)) sdp_cstate_t; -static int copy_cstate(uint8_t *pdata, const sdp_cstate_t *cstate) +static int copy_cstate(uint8_t *pdata, int pdata_len, const sdp_cstate_t *cstate) { if (cstate) { - *pdata++ = cstate->length; - memcpy(pdata, cstate->data, cstate->length); - return cstate->length + 1; + uint8_t len = cstate->length; + if (len >= pdata_len) { + SDPERR("Continuation state size exceeds internal buffer"); + len = pdata_len - 1; + } + *pdata++ = len; + memcpy(pdata, cstate->data, len); + return len + 1; } *pdata = 0; return 1; @@ -3087,7 +3092,8 @@ int sdp_service_search_req(sdp_session_t *session, const sdp_list_t *search, do { // Add continuation state or NULL (first time) - reqsize = _reqsize + copy_cstate(_pdata, cstate); + reqsize = _reqsize + copy_cstate(_pdata, + SDP_REQ_BUFFER_SIZE - _reqsize, cstate); // Set the request header's param length reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t)); @@ -3249,7 +3255,8 @@ sdp_record_t *sdp_service_attr_req(sdp_session_t *session, uint32_t handle, do { // add NULL continuation state - reqsize = _reqsize + copy_cstate(_pdata, cstate); + reqsize = _reqsize + copy_cstate(_pdata, + SDP_REQ_BUFFER_SIZE - _reqsize, cstate); // set the request header's param length reqhdr->tid = htons(sdp_gen_tid(session)); @@ -3470,7 +3477,7 @@ int sdp_service_search_async(sdp_session_t *session, const sdp_list_t *search, u pdata += sizeof(uint16_t); // set the request header's param length - cstate_len = copy_cstate(pdata, NULL); + cstate_len = copy_cstate(pdata, SDP_REQ_BUFFER_SIZE - t->reqsize, NULL); reqhdr->plen = htons((t->reqsize + cstate_len) - sizeof(sdp_pdu_hdr_t)); if (sdp_send_req(session, t->reqbuf, t->reqsize + cstate_len) < 0) { @@ -3584,7 +3591,7 @@ int sdp_service_attr_async(sdp_session_t *session, uint32_t handle, sdp_attrreq_ SDPDBG("Attr list length : %d\n", seqlen); // set the request header's param length - cstate_len = copy_cstate(pdata, NULL); + cstate_len = copy_cstate(pdata, SDP_REQ_BUFFER_SIZE - t->reqsize, NULL); reqhdr->plen = htons((t->reqsize + cstate_len) - sizeof(sdp_pdu_hdr_t)); if (sdp_send_req(session, t->reqbuf, t->reqsize + cstate_len) < 0) { @@ -3703,7 +3710,7 @@ int sdp_service_search_attr_async(sdp_session_t *session, const sdp_list_t *sear t->reqsize += seqlen; // set the request header's param length - cstate_len = copy_cstate(pdata, NULL); + cstate_len = copy_cstate(pdata, SDP_REQ_BUFFER_SIZE - t->reqsize, NULL); reqhdr->plen = htons((t->reqsize + cstate_len) - sizeof(sdp_pdu_hdr_t)); if (sdp_send_req(session, t->reqbuf, t->reqsize + cstate_len) < 0) { @@ -3916,7 +3923,8 @@ int sdp_process(sdp_session_t *session) reqhdr->tid = htons(sdp_gen_tid(session)); // add continuation state - cstate_len = copy_cstate(t->reqbuf + t->reqsize, pcstate); + cstate_len = copy_cstate(t->reqbuf + t->reqsize, + SDP_REQ_BUFFER_SIZE - t->reqsize, pcstate); reqsize = t->reqsize + cstate_len; @@ -4059,7 +4067,8 @@ int sdp_service_search_attr_req(sdp_session_t *session, const sdp_list_t *search reqhdr->tid = htons(sdp_gen_tid(session)); // add continuation state (can be null) - reqsize = _reqsize + copy_cstate(_pdata, cstate); + reqsize = _reqsize + copy_cstate(_pdata, + SDP_REQ_BUFFER_SIZE - _reqsize, cstate); // set the request header's param length reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t)); -- cgit From 121c632f01e3f49a69a9d637e0da4d25c2ee090d Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Mon, 23 Jun 2008 06:18:33 +0000 Subject: Have the service routines use the new safe functions --- src/sdp.c | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 86 insertions(+), 12 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 59686388..4217aedd 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -2923,18 +2923,23 @@ void sdp_pattern_add_uuidseq(sdp_record_t *rec, sdp_list_t *seq) * handles are not in "data element sequence" form, but just like * an array of service handles */ -static void extract_record_handle_seq(uint8_t *pdu, sdp_list_t **seq, int count, int *scanned) +static void extract_record_handle_seq(uint8_t *pdu, int bufsize, sdp_list_t **seq, int count, int *scanned) { sdp_list_t *pSeq = *seq; uint8_t *pdata = pdu; int n; for (n = 0; n < count; n++) { + if (bufsize < sizeof(uint32_t)) { + SDPERR("Unexpected end of packet"); + break; + } uint32_t *pSvcRec = malloc(sizeof(uint32_t)); *pSvcRec = ntohl(bt_get_unaligned((uint32_t *) pdata)); pSeq = sdp_list_append(pSeq, pSvcRec); pdata += sizeof(uint32_t); *scanned += sizeof(uint32_t); + bufsize -= sizeof(uint32_t); } *seq = pSeq; } @@ -3054,7 +3059,7 @@ int sdp_service_search_req(sdp_session_t *session, const sdp_list_t *search, uint32_t reqsize = 0, _reqsize; uint32_t rspsize = 0, rsplen; int seqlen = 0; - int scanned, total_rec_count, rec_count; + int scanned, total_rec_count, rec_count, pdata_len; uint8_t *pdata, *_pdata; uint8_t *reqbuf, *rspbuf; sdp_pdu_hdr_t *reqhdr, *rsphdr; @@ -3107,7 +3112,12 @@ int sdp_service_search_req(sdp_session_t *session, const sdp_list_t *search, if (status < 0) goto end; - rsplen = 0; + if (rspsize < sizeof(sdp_pdu_hdr_t)) { + SDPERR("Unexpected end of packet"); + status = -1; + goto end; + } + rsphdr = (sdp_pdu_hdr_t *) rspbuf; rsplen = ntohs(rsphdr->plen); @@ -3118,14 +3128,23 @@ int sdp_service_search_req(sdp_session_t *session, const sdp_list_t *search, } scanned = 0; pdata = rspbuf + sizeof(sdp_pdu_hdr_t); + pdata_len = rspsize - sizeof(sdp_pdu_hdr_t); + + if (pdata_len < sizeof(uint16_t) + sizeof(uint16_t)) { + SDPERR("Unexpected end of packet"); + status = -1; + goto end; + } // net service record match count total_rec_count = ntohs(bt_get_unaligned((uint16_t *) pdata)); pdata += sizeof(uint16_t); scanned += sizeof(uint16_t); + pdata_len -= sizeof(uint16_t); rec_count = ntohs(bt_get_unaligned((uint16_t *) pdata)); pdata += sizeof(uint16_t); scanned += sizeof(uint16_t); + pdata_len -= sizeof(uint16_t); SDPDBG("Total svc count: %d\n", total_rec_count); SDPDBG("Current svc count: %d\n", rec_count); @@ -3135,12 +3154,18 @@ int sdp_service_search_req(sdp_session_t *session, const sdp_list_t *search, status = -1; goto end; } - extract_record_handle_seq(pdata, rsp, rec_count, &scanned); + extract_record_handle_seq(pdata, pdata_len, rsp, rec_count, &scanned); SDPDBG("BytesScanned : %d\n", scanned); if (rsplen > scanned) { uint8_t cstate_len; + if (rspsize < sizeof(sdp_pdu_hdr_t) + scanned + sizeof(uint8_t)) { + SDPERR("Unexpected end of packet: continuation state data missing"); + status = -1; + goto end; + } + pdata = rspbuf + sizeof(sdp_pdu_hdr_t) + scanned; cstate_len = *(uint8_t *) pdata; if (cstate_len > 0) { @@ -3199,7 +3224,7 @@ sdp_record_t *sdp_service_attr_req(sdp_session_t *session, uint32_t handle, uint32_t reqsize = 0, _reqsize; uint32_t rspsize = 0, rsp_count; int attr_list_len = 0; - int seqlen = 0; + int seqlen = 0, pdata_len; uint8_t *pdata, *_pdata; uint8_t *reqbuf, *rspbuf; sdp_pdu_hdr_t *reqhdr, *rsphdr; @@ -3265,6 +3290,13 @@ sdp_record_t *sdp_service_attr_req(sdp_session_t *session, uint32_t handle, status = sdp_send_req_w4_rsp(session, reqbuf, rspbuf, reqsize, &rspsize); if (status < 0) goto end; + + if (rspsize < sizeof(sdp_pdu_hdr_t)) { + SDPERR("Unexpected end of packet"); + status = -1; + goto end; + } + rsp_count = 0; rsphdr = (sdp_pdu_hdr_t *) rspbuf; if (rsphdr->pdu_id == SDP_ERROR_RSP) { @@ -3273,11 +3305,25 @@ sdp_record_t *sdp_service_attr_req(sdp_session_t *session, uint32_t handle, goto end; } pdata = rspbuf + sizeof(sdp_pdu_hdr_t); + pdata_len = rspsize - sizeof(sdp_pdu_hdr_t); + + if (pdata_len < sizeof(uint16_t)) { + SDPERR("Unexpected end of packet"); + status = -1; + goto end; + } + rsp_count = ntohs(bt_get_unaligned((uint16_t *) pdata)); attr_list_len += rsp_count; pdata += sizeof(uint16_t); + pdata_len -= sizeof(uint16_t); // if continuation state set need to re-issue request before parsing + if (pdata_len < rsp_count + sizeof(uint8_t)) { + SDPERR("Unexpected end of packet: continuation state data missing"); + status = -1; + goto end; + } cstate_len = *(uint8_t *) (pdata + rsp_count); SDPDBG("Response id : %d\n", rsphdr->pdu_id); @@ -3304,9 +3350,11 @@ sdp_record_t *sdp_service_attr_req(sdp_session_t *session, uint32_t handle, if (attr_list_len > 0) { int scanned = 0; - if (rsp_concat_buf.data_size != 0) + if (rsp_concat_buf.data_size != 0) { pdata = rsp_concat_buf.data; - rec = sdp_extract_pdu(pdata, &scanned); + pdata_len = rsp_concat_buf.data_size; + } + rec = sdp_extract_pdu_safe(pdata, pdata_len, &scanned); if (!rec) status = -1; @@ -4003,7 +4051,7 @@ int sdp_service_search_attr_req(sdp_session_t *session, const sdp_list_t *search uint32_t reqsize = 0, _reqsize; uint32_t rspsize = 0; int seqlen = 0, attr_list_len = 0; - int rsp_count = 0, cstate_len = 0; + int rsp_count = 0, cstate_len = 0, pdata_len; uint8_t *pdata, *_pdata; uint8_t *reqbuf, *rspbuf; sdp_pdu_hdr_t *reqhdr, *rsphdr; @@ -4074,6 +4122,12 @@ int sdp_service_search_attr_req(sdp_session_t *session, const sdp_list_t *search reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t)); rsphdr = (sdp_pdu_hdr_t *) rspbuf; status = sdp_send_req_w4_rsp(session, reqbuf, rspbuf, reqsize, &rspsize); + if (rspsize < sizeof(sdp_pdu_hdr_t)) { + SDPERR("Unexpected end of packet"); + status = -1; + goto end; + } + if (status < 0) { SDPDBG("Status : 0x%x\n", rsphdr->pdu_id); goto end; @@ -4085,9 +4139,25 @@ int sdp_service_search_attr_req(sdp_session_t *session, const sdp_list_t *search } pdata = rspbuf + sizeof(sdp_pdu_hdr_t); + pdata_len = rspsize - sizeof(sdp_pdu_hdr_t); + + if (pdata_len < sizeof(uint16_t)) { + SDPERR("Unexpected end of packet"); + status = -1; + goto end; + } + rsp_count = ntohs(bt_get_unaligned((uint16_t *) pdata)); attr_list_len += rsp_count; pdata += sizeof(uint16_t); // pdata points to attribute list + pdata_len -= sizeof(uint16_t); + + if (pdata_len < rsp_count + sizeof(uint8_t)) { + SDPERR("Unexpected end of packet: continuation state data missing"); + status = -1; + goto end; + } + cstate_len = *(uint8_t *) (pdata + rsp_count); SDPDBG("Attrlist byte count : %d\n", attr_list_len); @@ -4114,24 +4184,27 @@ int sdp_service_search_attr_req(sdp_session_t *session, const sdp_list_t *search if (attr_list_len > 0) { int scanned = 0; - if (rsp_concat_buf.data_size != 0) + if (rsp_concat_buf.data_size != 0) { pdata = rsp_concat_buf.data; + pdata_len = rsp_concat_buf.data_size; + } /* * Response is a sequence of sequence(s) for one or * more data element sequence(s) representing services * for which attributes are returned */ - scanned = sdp_extract_seqtype(pdata, &dataType, &seqlen); + scanned = sdp_extract_seqtype_safe(pdata, pdata_len, &dataType, &seqlen); SDPDBG("Bytes scanned : %d\n", scanned); SDPDBG("Seq length : %d\n", seqlen); if (scanned && seqlen) { pdata += scanned; + pdata_len -= scanned; do { int recsize = 0; - sdp_record_t *rec = sdp_extract_pdu(pdata, &recsize); + sdp_record_t *rec = sdp_extract_pdu_safe(pdata, pdata_len, &recsize); if (rec == NULL) { SDPERR("SVC REC is null\n"); status = -1; @@ -4143,13 +4216,14 @@ int sdp_service_search_attr_req(sdp_session_t *session, const sdp_list_t *search } scanned += recsize; pdata += recsize; + pdata_len -= recsize; SDPDBG("Loc seq length : %d\n", recsize); SDPDBG("Svc Rec Handle : 0x%x\n", rec->handle); SDPDBG("Bytes scanned : %d\n", scanned); SDPDBG("Attrlist byte count : %d\n", attr_list_len); rec_list = sdp_list_append(rec_list, rec); - } while (scanned < attr_list_len); + } while (scanned < attr_list_len && pdata_len > 0); SDPDBG("Successful scan of service attr lists\n"); *rsp = rec_list; -- cgit From ca0d3dc0bb5a573b44496847919cc02c9da2016f Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Mon, 23 Jun 2008 07:29:13 +0000 Subject: More validation of incoming packet lengths --- src/sdp.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index 4217aedd..f8953c42 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -2643,6 +2643,13 @@ int sdp_device_record_register_binary(sdp_session_t *session, bdaddr_t *device, if (status < 0) goto end; + if (rspsize < sizeof(sdp_pdu_hdr_t)) { + SDPERR("Unexpected end of packet"); + errno = EPROTO; + status = -1; + goto end; + } + rsphdr = (sdp_pdu_hdr_t *) rsp; p = rsp + sizeof(sdp_pdu_hdr_t); @@ -2654,6 +2661,12 @@ int sdp_device_record_register_binary(sdp_session_t *session, bdaddr_t *device, errno = EPROTO; status = -1; } else { + if (rspsize < sizeof(sdp_pdu_hdr_t) + sizeof(uint32_t)) { + SDPERR("Unexpected end of packet"); + errno = EPROTO; + status = -1; + goto end; + } if (handle) *handle = ntohl(bt_get_unaligned((uint32_t *) p)); } @@ -2749,6 +2762,13 @@ int sdp_device_record_unregister_binary(sdp_session_t *session, bdaddr_t *device if (status < 0) goto end; + if (rspsize < sizeof(sdp_pdu_hdr_t) + sizeof(uint16_t)) { + SDPERR("Unexpected end of packet"); + errno = EPROTO; + status = -1; + goto end; + } + rsphdr = (sdp_pdu_hdr_t *) rspbuf; p = rspbuf + sizeof(sdp_pdu_hdr_t); status = bt_get_unaligned((uint16_t *) p); @@ -2848,6 +2868,13 @@ int sdp_device_record_update(sdp_session_t *session, bdaddr_t *device, const sdp if (status < 0) goto end; + if (rspsize < sizeof(sdp_pdu_hdr_t) + sizeof(uint16_t)) { + SDPERR("Unexpected end of packet"); + errno = EPROTO; + status = -1; + goto end; + } + SDPDBG("Send req status : %d\n", status); rsphdr = (sdp_pdu_hdr_t *) rspbuf; -- cgit From bd32ca373cd507d9c9746bde40b0e7bb4034adbe Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Thu, 10 Jul 2008 01:43:09 +0000 Subject: Fix spelling --- src/sdp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index f8953c42..e365370c 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -164,7 +164,7 @@ static struct tupla ServiceClass[] = { { IMAGING_ARCHIVE_SVCLASS_ID, "Imaging Automatic Archive" }, { IMAGING_REFOBJS_SVCLASS_ID, "Imaging Referenced Objects" }, { HANDSFREE_SVCLASS_ID, "Handsfree" }, - { HANDSFREE_AGW_SVCLASS_ID, "Handfree Audio Gateway" }, + { HANDSFREE_AGW_SVCLASS_ID, "Handsfree Audio Gateway" }, { DIRECT_PRT_REFOBJS_SVCLASS_ID, "Direct Printing Ref. Objects" }, { REFLECTED_UI_SVCLASS_ID, "Reflected UI" }, { BASIC_PRINTING_SVCLASS_ID, "Basic Printing" }, -- cgit From 924fa35c97929ff95590a1f33cc2bd8cca7cf1f2 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Thu, 10 Jul 2008 01:44:16 +0000 Subject: Change order of includes --- src/sdp.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/sdp.c') diff --git a/src/sdp.c b/src/sdp.c index e365370c..3149c0f1 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -36,10 +36,10 @@ #include #include #include -#include -#include #include #include +#include +#include #include #include -- cgit