📄 request.c
字号:
#ifdef SDP_DEBUG if (seq) SDPDBG("Entries in attr seq : %d\n", sdp_list_len(seq)); else SDPDBG("NULL attribute descriptor\n"); SDPDBG("AttrDataType : %d\n", dtd);#endif if (seq == NULL) { SDPDBG("Attribute sequence is NULL\n"); return 0; } if (dtd == SDP_UINT16) for (; seq; seq = seq->next) { uint16_t attr = sdp_get_unaligned((uint16_t *)seq->data); sdp_data_t *a = (sdp_data_t *)sdp_data_get(rec, attr); if (a) sdp_append_to_pdu(buf, a); } else if (dtd == SDP_UINT32) { sdp_buf_t pdu; sdp_gen_record_pdu(rec, &pdu); for (; seq; seq = seq->next) { uint32_t range = sdp_get_unaligned((uint32_t *)seq->data); uint16_t attr; uint16_t low = (0xffff0000 & range) >> 16; uint16_t high = 0x0000ffff & range; SDPDBG("attr range : 0x%x\n", range); SDPDBG("Low id : 0x%x\n", low); SDPDBG("High id : 0x%x\n", high); if (low == 0x0000 && high == 0xffff && pdu.data_size <= buf->buf_size) { /* copy it */ memcpy(buf->data, pdu.data, pdu.data_size); buf->data_size = pdu.data_size; break; } /* (else) sub-range of attributes */ for (attr = low; attr <= high; attr++) { sdp_data_t *a = (sdp_data_t *)sdp_data_get(rec, attr); if (a) sdp_append_to_pdu(buf, a); } } free(pdu.data); } else { SDPERR("Unexpected data type : 0x%x\n", dtd); SDPERR("Expect uint16_t or uint32_t\n"); return SDP_INVALID_SYNTAX; } return 0;}/* * A request for the attributes of a service record. * First check if the service record (specified by * service record handle) exists, then call the attribute * streaming function */static int service_attr_req(sdp_req_t *req, sdp_buf_t *buf){ sdp_cont_state_t *cstate = NULL; char *pResponse = NULL; short cstate_size = 0; sdp_list_t *seq = NULL; uint8_t dtd = 0; int scanned = 0; int max_rsp_size; int status = 0, plen, mlen; char *pdata = req->buf + sizeof(sdp_pdu_hdr_t); uint32_t handle = ntohl(sdp_get_unaligned((uint32_t *)pdata)); SDPDBG(""); pdata += sizeof(uint32_t); max_rsp_size = ntohs(sdp_get_unaligned((uint16_t *)pdata)); pdata += sizeof(uint16_t); /* extract the attribute list */ scanned = extract_des(pdata, &seq, &dtd, SDP_TYPE_ANY); if (scanned == -1) { status = SDP_INVALID_SYNTAX; goto done; } pdata += scanned; plen = ntohs(((sdp_pdu_hdr_t *)(req->buf))->plen); mlen = scanned + sizeof(uint32_t) + sizeof(uint16_t) + 1; // ensure we don't read past buffer if (plen < mlen || plen != mlen + *(uint8_t *)pdata) { status = SDP_INVALID_SYNTAX; goto done; } /* * if continuation state exists, attempt * to get rsp remainder from cache, else send error */ cstate = sdp_cstate_get(pdata); SDPDBG("SvcRecHandle : 0x%x\n", handle); SDPDBG("max_rsp_size : %d\n", max_rsp_size); /* * Calculate Attribute size acording to MTU * We can send only (MTU - sizeof(sdp_pdu_hdr_t) - sizeof(sdp_cont_state_t)) */ max_rsp_size = MIN(max_rsp_size, req->mtu - sizeof(sdp_pdu_hdr_t) - sizeof(uint32_t) - SDP_CONT_STATE_SIZE - sizeof(uint16_t)); /* pull header for AttributeList byte count */ buf->data += sizeof(uint16_t); buf->buf_size -= sizeof(uint16_t); if (cstate) { sdp_buf_t *pCache = sdp_get_cached_rsp(cstate); SDPDBG("Obtained cached rsp : %p\n", pCache); if (pCache) { short sent = MIN(max_rsp_size, pCache->data_size - cstate->cStateValue.maxBytesSent); pResponse = pCache->data; memcpy(buf->data, pResponse + cstate->cStateValue.maxBytesSent, sent); buf->data_size += sent; cstate->cStateValue.maxBytesSent += sent; SDPDBG("Response size : %d sending now : %d bytes sent so far : %d\n", pCache->data_size, sent, cstate->cStateValue.maxBytesSent); if (cstate->cStateValue.maxBytesSent == pCache->data_size) cstate_size = sdp_set_cstate_pdu(buf, NULL); else cstate_size = sdp_set_cstate_pdu(buf, cstate); } else { status = SDP_INVALID_CSTATE; SDPERR("NULL cache buffer and non-NULL continuation state\n"); } } else { sdp_record_t *rec = sdp_record_find(handle); status = extract_attrs(rec, seq, dtd, buf); if (buf->data_size > max_rsp_size) { sdp_cont_state_t newState; memset((char *)&newState, 0, sizeof(sdp_cont_state_t)); newState.timestamp = sdp_cstate_alloc_buf(buf); /* * Reset the buffer size to the maximum expected and * set the sdp_cont_state_t */ SDPDBG("Creating continuation state of size : %d\n", buf->data_size); buf->data_size = max_rsp_size; newState.cStateValue.maxBytesSent = max_rsp_size; cstate_size = sdp_set_cstate_pdu(buf, &newState); } else { if (buf->data_size == 0) sdp_append_to_buf(buf, 0, 0); cstate_size = sdp_set_cstate_pdu(buf, NULL); } } // push header buf->data -= sizeof(uint16_t); buf->buf_size += sizeof(uint16_t);done: if (seq) sdp_list_free(seq, free); if (status) return status; /* set attribute list byte count */ sdp_put_unaligned(htons(buf->data_size - cstate_size), (uint16_t *)buf->data); buf->data_size += sizeof(uint16_t); return 0;}/* * combined service search and attribute extraction */static int service_search_attr_req(sdp_req_t *req, sdp_buf_t *buf){ int status = 0, plen, totscanned; char *pdata, *pResponse = NULL; int scanned, max, rsp_count = 0; sdp_list_t *pattern = NULL, *seq = NULL, *svcList; sdp_cont_state_t *cstate = NULL; short cstate_size = 0; uint8_t dtd = 0; sdp_buf_t tmpbuf; tmpbuf.data = NULL; pdata = req->buf + sizeof(sdp_pdu_hdr_t); scanned = extract_des(pdata, &pattern, &dtd, SDP_TYPE_UUID); if (scanned == -1) { status = SDP_INVALID_SYNTAX; goto done; } totscanned = scanned; SDPDBG("Bytes scanned: %d", scanned); pdata += scanned; max = ntohs(sdp_get_unaligned((uint16_t *)pdata)); pdata += sizeof(uint16_t); SDPDBG("Max Attr expected: %d", max); /* extract the attribute list */ scanned = extract_des(pdata, &seq, &dtd, SDP_TYPE_ANY); if (scanned == -1) { status = SDP_INVALID_SYNTAX; goto done; } pdata += scanned; totscanned += scanned + sizeof(uint16_t) + 1; plen = ntohs(((sdp_pdu_hdr_t *)(req->buf))->plen); if (plen < totscanned || plen != totscanned + *(uint8_t *)pdata) { status = SDP_INVALID_SYNTAX; goto done; } /* * if continuation state exists attempt * to get rsp remainder from cache, else send error */ cstate = sdp_cstate_get(pdata); // continuation information svcList = sdp_get_record_list(); tmpbuf.data = (char *)malloc(USHRT_MAX); tmpbuf.data_size = 0; tmpbuf.buf_size = USHRT_MAX; memset(tmpbuf.data, 0, USHRT_MAX); /* * Calculate Attribute size acording to MTU * We can send only (MTU - sizeof(sdp_pdu_hdr_t) - sizeof(sdp_cont_state_t)) */ max = MIN(max, req->mtu - sizeof(sdp_pdu_hdr_t) - SDP_CONT_STATE_SIZE - sizeof(uint16_t)); /* pull header for AttributeList byte count */ buf->data += sizeof(uint16_t); buf->buf_size -= sizeof(uint16_t); if (cstate == NULL) { /* no continuation state -> create new response */ sdp_list_t *p; for (p = svcList; p; p = p->next) { sdp_record_t *rec = (sdp_record_t *)p->data; if (sdp_match_uuid(pattern, rec->pattern) > 0) { rsp_count++; status = extract_attrs(rec, seq, dtd, &tmpbuf); SDPDBG("Response count : %d\n", rsp_count); SDPDBG("Local PDU size : %d\n", tmpbuf.data_size); if (status) { SDPDBG("Extract attr from record returns err\n"); break; } if (buf->data_size + tmpbuf.data_size < buf->buf_size) { // to be sure no relocations sdp_append_to_buf(buf, tmpbuf.data, tmpbuf.data_size); tmpbuf.data_size = 0; memset(tmpbuf.data, 0, USHRT_MAX); } else { SDPERR("Relocation needed\n"); break; } SDPDBG("Net PDU size : %d\n", buf->data_size); } } if (buf->data_size > max) { sdp_cont_state_t newState; memset((char *)&newState, 0, sizeof(sdp_cont_state_t)); newState.timestamp = sdp_cstate_alloc_buf(buf); /* * Reset the buffer size to the maximum expected and * set the sdp_cont_state_t */ buf->data_size = max; newState.cStateValue.maxBytesSent = max; cstate_size = sdp_set_cstate_pdu(buf, &newState); } else cstate_size = sdp_set_cstate_pdu(buf, NULL); } else { /* continuation State exists -> get from cache */ sdp_buf_t *pCache = sdp_get_cached_rsp(cstate); if (pCache) { uint16_t sent = MIN(max, pCache->data_size - cstate->cStateValue.maxBytesSent); pResponse = pCache->data; memcpy(buf->data, pResponse + cstate->cStateValue.maxBytesSent, sent); buf->data_size += sent; cstate->cStateValue.maxBytesSent += sent; if (cstate->cStateValue.maxBytesSent == pCache->data_size) cstate_size = sdp_set_cstate_pdu(buf, NULL); else cstate_size = sdp_set_cstate_pdu(buf, cstate); } else { status = SDP_INVALID_CSTATE; SDPDBG("Non-null continuation state, but null cache buffer\n"); } } if (!rsp_count && !cstate) { // found nothing buf->data_size = 0; sdp_append_to_buf(buf, tmpbuf.data, tmpbuf.data_size); sdp_set_cstate_pdu(buf, NULL); } // push header buf->data -= sizeof(uint16_t); buf->buf_size += sizeof(uint16_t); if (!status) { /* set attribute list byte count */ sdp_put_unaligned(htons(buf->data_size - cstate_size), (uint16_t *)buf->data); buf->data_size += sizeof(uint16_t); }done: if (tmpbuf.data) free(tmpbuf.data); if (pattern) sdp_list_free(pattern, free); if (seq) sdp_list_free(seq, free); return status;}/* * Top level request processor. Calls the appropriate processing * function based on request type. Handles service registration * client requests also. */void process_request(sdp_req_t *req){ sdp_pdu_hdr_t *reqhdr = (sdp_pdu_hdr_t *)req->buf; sdp_pdu_hdr_t *rsphdr; sdp_buf_t rsp; char *buf = (char *)malloc(USHRT_MAX); int sent = 0; int status = SDP_INVALID_SYNTAX; SDPDBG(""); memset((void *)buf, 0, USHRT_MAX); rsp.data = buf + sizeof(sdp_pdu_hdr_t); rsp.data_size = 0; rsp.buf_size = USHRT_MAX - sizeof(sdp_pdu_hdr_t); rsphdr = (sdp_pdu_hdr_t *)buf; if (ntohs(reqhdr->plen) != req->len - sizeof(sdp_pdu_hdr_t)) { status = SDP_INVALID_PDU_SIZE; goto send_rsp; } switch (reqhdr->pdu_id) { case SDP_SVC_SEARCH_REQ: SDPDBG("Got a svc srch req\n"); status = service_search_req(req, &rsp); rsphdr->pdu_id = SDP_SVC_SEARCH_RSP; break; case SDP_SVC_ATTR_REQ: SDPDBG("Got a svc attr req\n"); status = service_attr_req(req, &rsp); rsphdr->pdu_id = SDP_SVC_ATTR_RSP; break; case SDP_SVC_SEARCH_ATTR_REQ: SDPDBG("Got a svc srch attr req\n"); status = service_search_attr_req(req, &rsp); rsphdr->pdu_id = SDP_SVC_SEARCH_ATTR_RSP; break; /* Following requests are allowed only for local connections */ case SDP_SVC_REGISTER_REQ: SDPDBG("Service register request\n"); if (req->local) { status = service_register_req(req, &rsp); rsphdr->pdu_id = SDP_SVC_REGISTER_RSP; } break; case SDP_SVC_UPDATE_REQ: SDPDBG("Service update request\n"); if (req->local) { status = service_update_req(req, &rsp); rsphdr->pdu_id = SDP_SVC_UPDATE_RSP; } break; case SDP_SVC_REMOVE_REQ: SDPDBG("Service removal request\n"); if (req->local) { status = service_remove_req(req, &rsp); rsphdr->pdu_id = SDP_SVC_REMOVE_RSP; } break; default: SDPERR("Unknown PDU ID : 0x%x received\n", reqhdr->pdu_id); status = SDP_INVALID_SYNTAX; break; }send_rsp: if (status) { rsphdr->pdu_id = SDP_ERROR_RSP; sdp_put_unaligned(htons(status), (uint16_t *)rsp.data); rsp.data_size = sizeof(uint16_t); } SDPDBG("Sending rsp. status %d", status); rsphdr->tid = reqhdr->tid; rsphdr->plen = htons(rsp.data_size); /* point back to the real buffer start and set the real rsp length */ rsp.data_size += sizeof(sdp_pdu_hdr_t); rsp.data = buf; /* stream the rsp PDU */ sent = send(req->sock, rsp.data, rsp.data_size, 0); SDPDBG("Bytes Sent : %d\n", sent); free(rsp.data); free(req->buf);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -