tizen 2.3.1 release
[framework/connectivity/bluez.git] / src / sdpd-request.c
index 6a903c6..1eefdce 100644 (file)
 #include <config.h>
 #endif
 
-#include <stdio.h>
 #include <errno.h>
 #include <stdlib.h>
-#include <string.h>
 #include <limits.h>
-#include <sys/socket.h>
+#include <stdbool.h>
 
-#include <bluetooth/bluetooth.h>
-#include <bluetooth/l2cap.h>
-#include <bluetooth/sdp.h>
-#include <bluetooth/sdp_lib.h>
+#include "lib/bluetooth.h"
+#include "lib/l2cap.h"
+#include "lib/sdp.h"
+#include "lib/sdp_lib.h"
 
-#include <netinet/in.h>
+#include "src/shared/util.h"
 
 #include "sdpd.h"
 #include "log.h"
@@ -139,6 +137,7 @@ static int extract_des(uint8_t *buf, int len, sdp_list_t **svcReqSeq, uint8_t *p
        for (;;) {
                char *pElem = NULL;
                int localSeqLength = 0;
+               uuid_t *puuid;
 
                if (bufsize < sizeof(uint8_t)) {
                        SDPDBG("->Unexpected end of buffer");
@@ -178,11 +177,15 @@ static int extract_des(uint8_t *buf, int len, sdp_list_t **svcReqSeq, uint8_t *p
                                struct attrid *aid;
                                aid = malloc(sizeof(struct attrid));
                                aid->dtd = dataType;
-                               bt_put_unaligned(ntohs(bt_get_unaligned((uint16_t *)p)), (uint16_t *)&aid->uint16);
+                               aid->uint16 = get_be16(p);
                                pElem = (char *) aid;
                        } else {
+                               uint16_t tmp;
+
+                               memcpy(&tmp, p, sizeof(tmp));
+
                                pElem = malloc(sizeof(uint16_t));
-                               bt_put_unaligned(ntohs(bt_get_unaligned((uint16_t *)p)), (uint16_t *)pElem);
+                               put_be16(tmp, pElem);
                        }
                        p += sizeof(uint16_t);
                        seqlen += sizeof(uint16_t);
@@ -201,11 +204,16 @@ static int extract_des(uint8_t *buf, int len, sdp_list_t **svcReqSeq, uint8_t *p
                                struct attrid *aid;
                                aid = malloc(sizeof(struct attrid));
                                aid->dtd = dataType;
-                               bt_put_unaligned(ntohl(bt_get_unaligned((uint32_t *)p)), (uint32_t *)&aid->uint32);
+                               aid->uint32 = get_be32(p);
+
                                pElem = (char *) aid;
                        } else {
+                               uint32_t tmp;
+
+                               memcpy(&tmp, p, sizeof(tmp));
+
                                pElem = malloc(sizeof(uint32_t));
-                               bt_put_unaligned(ntohl(bt_get_unaligned((uint32_t *)p)), (uint32_t *)pElem);
+                               put_be32(tmp, pElem);
                        }
                        p += sizeof(uint32_t);
                        seqlen += sizeof(uint32_t);
@@ -214,12 +222,14 @@ static int extract_des(uint8_t *buf, int len, sdp_list_t **svcReqSeq, uint8_t *p
                case SDP_UUID16:
                case SDP_UUID32:
                case SDP_UUID128:
-                       pElem = malloc(sizeof(uuid_t));
-                       status = sdp_uuid_extract(p, bufsize, (uuid_t *) pElem, &localSeqLength);
+                       puuid = malloc(sizeof(uuid_t));
+                       status = sdp_uuid_extract(p, bufsize, puuid, &localSeqLength);
                        if (status < 0) {
-                               free(pElem);
+                               free(puuid);
                                goto failed;
                        }
+
+                       pElem = (char *) puuid;
                        seqlen += localSeqLength;
                        p += localSeqLength;
                        bufsize -= localSeqLength;
@@ -359,7 +369,7 @@ static int service_search_req(sdp_req_t *req, sdp_buf_t *buf)
        uint8_t *pCacheBuffer = NULL;
        int handleSize = 0;
        uint32_t cStateId = 0;
-       short *pTotalRecordCount, *pCurrentRecordCount;
+       uint8_t *pTotalRecordCount, *pCurrentRecordCount;
        uint8_t *pdata = req->buf + sizeof(sdp_pdu_hdr_t);
        size_t data_left = req->len - sizeof(sdp_pdu_hdr_t);
 
@@ -385,7 +395,7 @@ static int service_search_req(sdp_req_t *req, sdp_buf_t *buf)
                goto done;
        }
 
-       expected = ntohs(bt_get_unaligned((uint16_t *)pdata));
+       expected = get_be16(pdata);
 
        SDPDBG("Expected count: %d", expected);
        SDPDBG("Bytes scanned : %d", scanned);
@@ -409,14 +419,14 @@ static int service_search_req(sdp_req_t *req, sdp_buf_t *buf)
        pdata = buf->data;
 
        /* total service record count = 0 */
-       pTotalRecordCount = (short *)pdata;
-       bt_put_unaligned(0, (uint16_t *)pdata);
+       pTotalRecordCount = pdata;
+       put_be16(0, pdata);
        pdata += sizeof(uint16_t);
        buf->data_size += sizeof(uint16_t);
 
        /* current service record count = 0 */
-       pCurrentRecordCount = (short *)pdata;
-       bt_put_unaligned(0, (uint16_t *)pdata);
+       pCurrentRecordCount = pdata;
+       put_be16(0, pdata);
        pdata += sizeof(uint16_t);
        buf->data_size += sizeof(uint16_t);
 
@@ -433,7 +443,7 @@ static int service_search_req(sdp_req_t *req, sdp_buf_t *buf)
                        if (sdp_match_uuid(pattern, rec->pattern) > 0 &&
                                        sdp_check_access(rec->handle, &req->device)) {
                                rsp_count++;
-                               bt_put_unaligned(htonl(rec->handle), (uint32_t *)pdata);
+                               put_be32(rec->handle, pdata);
                                pdata += sizeof(uint32_t);
                                handleSize += sizeof(uint32_t);
                        }
@@ -442,8 +452,8 @@ static int service_search_req(sdp_req_t *req, sdp_buf_t *buf)
                SDPDBG("Match count: %d", rsp_count);
 
                buf->data_size += handleSize;
-               bt_put_unaligned(htons(rsp_count), (uint16_t *)pTotalRecordCount);
-               bt_put_unaligned(htons(rsp_count), (uint16_t *)pCurrentRecordCount);
+               put_be16(rsp_count, pTotalRecordCount);
+               put_be16(rsp_count, pCurrentRecordCount);
 
                if (rsp_count > actual) {
                        /* cache the rsp and generate a continuation state */
@@ -472,7 +482,7 @@ static int service_search_req(sdp_req_t *req, sdp_buf_t *buf)
                        if (pCache) {
                                pCacheBuffer = pCache->data;
                                /* get the rsp_count from the cached buffer */
-                               rsp_count = ntohs(bt_get_unaligned((uint16_t *)pCacheBuffer));
+                               rsp_count = get_be16(pCacheBuffer);
 
                                /* get index of the last sdp_record_t sent */
                                lastIndex = cstate->cStateValue.lastIndexSent;
@@ -490,7 +500,7 @@ static int service_search_req(sdp_req_t *req, sdp_buf_t *buf)
                 * current record count and increment the cached
                 * buffer pointer to beyond the counters
                 */
-               pdata = (uint8_t *) pCurrentRecordCount + sizeof(uint16_t);
+               pdata = pCurrentRecordCount + sizeof(uint16_t);
 
                /* increment beyond the totalCount and the currentCount */
                pCacheBuffer += 2 * sizeof(uint16_t);
@@ -498,7 +508,7 @@ static int service_search_req(sdp_req_t *req, sdp_buf_t *buf)
                if (cstate) {
                        handleSize = 0;
                        for (i = lastIndex; (i - lastIndex) < actual && i < rsp_count; i++) {
-                               bt_put_unaligned(bt_get_unaligned((uint32_t *)(pCacheBuffer + i * sizeof(uint32_t))), (uint32_t *)pdata);
+                               memcpy(pdata, pCacheBuffer + i * sizeof(uint32_t), sizeof(uint32_t));
                                pdata += sizeof(uint32_t);
                                handleSize += sizeof(uint32_t);
                        }
@@ -508,8 +518,8 @@ static int service_search_req(sdp_req_t *req, sdp_buf_t *buf)
                }
 
                buf->data_size += handleSize;
-               bt_put_unaligned(htons(rsp_count), (uint16_t *)pTotalRecordCount);
-               bt_put_unaligned(htons(i - lastIndex), (uint16_t *)pCurrentRecordCount);
+               put_be16(rsp_count, pTotalRecordCount);
+               put_be16(i - lastIndex, pCurrentRecordCount);
 
                if (i == rsp_count) {
                        /* set "null" continuationState */
@@ -571,12 +581,12 @@ static int extract_attrs(sdp_record_t *rec, sdp_list_t *seq, sdp_buf_t *buf)
                SDPDBG("AttrDataType : %d", aid->dtd);
 
                if (aid->dtd == SDP_UINT16) {
-                       uint16_t attr = bt_get_unaligned((uint16_t *)&aid->uint16);
+                       uint16_t attr = aid->uint16;
                        sdp_data_t *a = sdp_data_get(rec, attr);
                        if (a)
                                sdp_append_to_pdu(buf, a);
                } else if (aid->dtd == SDP_UINT32) {
-                       uint32_t range = bt_get_unaligned((uint32_t *)&aid->uint32);
+                       uint32_t range = aid->uint32;
                        uint16_t attr;
                        uint16_t low = (0xffff0000 & range) >> 16;
                        uint16_t high = 0x0000ffff & range;
@@ -639,7 +649,7 @@ static int service_attr_req(sdp_req_t *req, sdp_buf_t *buf)
                goto done;
        }
 
-       handle = ntohl(bt_get_unaligned((uint32_t *)pdata));
+       handle = get_be32(pdata);
 
        pdata += sizeof(uint32_t);
        data_left -= sizeof(uint32_t);
@@ -649,7 +659,7 @@ static int service_attr_req(sdp_req_t *req, sdp_buf_t *buf)
                goto done;
        }
 
-       max_rsp_size = ntohs(bt_get_unaligned((uint16_t *)pdata));
+       max_rsp_size = get_be16(pdata);
 
        pdata += sizeof(uint16_t);
        data_left -= sizeof(uint16_t);
@@ -765,7 +775,7 @@ done:
                return status;
 
        /* set attribute list byte count */
-       bt_put_unaligned(htons(buf->data_size - cstate_size), (uint16_t *)buf->data);
+       put_be16(buf->data_size - cstate_size, buf->data);
        buf->data_size += sizeof(uint16_t);
        return 0;
 }
@@ -806,7 +816,7 @@ static int service_search_attr_req(sdp_req_t *req, sdp_buf_t *buf)
                goto done;
        }
 
-       max = ntohs(bt_get_unaligned((uint16_t *)pdata));
+       max = get_be16(pdata);
 
        pdata += sizeof(uint16_t);
        data_left -= sizeof(uint16_t);
@@ -936,7 +946,7 @@ static int service_search_attr_req(sdp_req_t *req, sdp_buf_t *buf)
 
        if (!status) {
                /* set attribute list byte count */
-               bt_put_unaligned(htons(buf->data_size - cstate_size), (uint16_t *)buf->data);
+               put_be16(buf->data_size - cstate_size, buf->data);
                buf->data_size += sizeof(uint16_t);
        }
 
@@ -1020,7 +1030,7 @@ static void process_request(sdp_req_t *req)
 send_rsp:
        if (status) {
                rsphdr->pdu_id = SDP_ERROR_RSP;
-               bt_put_unaligned(htons(status), (uint16_t *)rsp.data);
+               put_be16(status, rsp.data);
                rsp.data_size = sizeof(uint16_t);
        }
 
@@ -1043,6 +1053,22 @@ send_rsp:
        free(req->buf);
 }
 
+void handle_internal_request(int sk, int mtu, void *data, int len)
+{
+       sdp_req_t req;
+
+       bacpy(&req.device, BDADDR_ANY);
+       bacpy(&req.bdaddr, BDADDR_LOCAL);
+       req.local = 0;
+       req.sock = sk;
+       req.mtu = mtu;
+       req.flags = 0;
+       req.buf = data;
+       req.len = len;
+
+       process_request(&req);
+}
+
 void handle_request(int sk, uint8_t *data, int len)
 {
        struct sockaddr_l2 sa;