📄 utils.c
字号:
/* Service Discovery Protocol (SDP) Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com>, Stephen Crane <steve.crane@rococosoft.com> Based on original SDP implementation by Nokia Corporation. Copyright (C) 2001,2002 Nokia Corporation. Original author Guruprasad Krishnamurthy <guruprasad.krishnamurthy@nokia.com> 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.*//* Miscellaneous functions used by SDP clients and server Fixes: Guruprasad Krishnamurthy <guruprasad.krishnamurthy@nokia.com>*//* * $Id: utils.c,v 1.20 2003/02/12 13:18:06 jscrane Exp $ */#include <stdlib.h>#include <errno.h>#include <sys/time.h>#include "sdp.h"#include "sdp_lib.h"#include "sdp_internal.h"/* * 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; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -