1 /* 2 * The oRTP library is an RTP (Realtime Transport Protocol - rfc3550) implementation with additional features. 3 * Copyright (C) 2017 Belledonne Communications SARL 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software 17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 18 */ 19 20 #ifndef STR_UTILS_H 21 #define STR_UTILS_H 22 23 24 #include <ortp/port.h> 25 #if defined(ORTP_TIMESTAMP) 26 #include <time.h> 27 #endif 28 29 30 #ifndef MIN 31 #define MIN(a,b) (((a)>(b)) ? (b) : (a)) 32 #endif 33 #ifndef MAX 34 #define MAX(a,b) (((a)>(b)) ? (a) : (b)) 35 #endif 36 37 #define return_if_fail(expr) if (!(expr)) {printf("%s:%i- assertion"#expr "failed\n",__FILE__,__LINE__); return;} 38 #define return_val_if_fail(expr,ret) if (!(expr)) {printf("%s:%i- assertion" #expr "failed\n",__FILE__,__LINE__); return (ret);} 39 40 41 typedef struct ortp_recv_addr { 42 int family; 43 union { 44 struct in_addr ipi_addr; 45 struct in6_addr ipi6_addr; 46 } addr; 47 unsigned short port; 48 } ortp_recv_addr_t; 49 50 typedef struct ortp_recv_addr_map { 51 struct sockaddr_storage ss; 52 ortp_recv_addr_t recv_addr; 53 uint64_t ts; 54 } ortp_recv_addr_map_t; 55 56 typedef struct msgb 57 { 58 struct msgb *b_prev; 59 struct msgb *b_next; 60 struct msgb *b_cont; 61 struct datab *b_datap; 62 unsigned char *b_rptr; 63 unsigned char *b_wptr; 64 uint32_t reserved1; 65 uint32_t reserved2; 66 struct timeval timestamp; 67 ortp_recv_addr_t recv_addr; /*contains the destination address of incoming packets, used for ICE processing*/ 68 struct sockaddr_storage net_addr; /*source address of incoming packet, or dest address of outgoing packet, used only by simulator and modifiers*/ 69 socklen_t net_addrlen; /*source (dest) address of incoming (outgoing) packet length used by simulator and modifiers*/ 70 uint8_t ttl_or_hl; 71 } mblk_t; 72 73 74 75 typedef struct datab dblk_t; 76 77 typedef struct _queue 78 { 79 mblk_t _q_stopper; 80 int q_mcount; /*number of packet in the q */ 81 } queue_t; 82 83 #ifdef __cplusplus 84 extern "C" { 85 #endif 86 87 ORTP_PUBLIC void dblk_ref(dblk_t *d); 88 ORTP_PUBLIC void dblk_unref(dblk_t *d); 89 ORTP_PUBLIC unsigned char * dblk_base(dblk_t *db); 90 ORTP_PUBLIC unsigned char * dblk_lim(dblk_t *db); 91 ORTP_PUBLIC int dblk_ref_value(dblk_t *db); 92 93 ORTP_PUBLIC void qinit(queue_t *q); 94 95 ORTP_PUBLIC void putq(queue_t *q, mblk_t *m); 96 97 ORTP_PUBLIC mblk_t * getq(queue_t *q); 98 99 ORTP_PUBLIC void insq(queue_t *q,mblk_t *emp, mblk_t *mp); 100 101 ORTP_PUBLIC void remq(queue_t *q, mblk_t *mp); 102 103 ORTP_PUBLIC mblk_t * peekq(queue_t *q); 104 105 /* remove and free all messages in the q */ 106 #define FLUSHALL 0 107 ORTP_PUBLIC void flushq(queue_t *q, int how); 108 109 ORTP_PUBLIC void mblk_init(mblk_t *mp); 110 111 ORTP_PUBLIC void mblk_meta_copy(const mblk_t *source, mblk_t *dest); 112 113 /* allocates a mblk_t, that points to a datab_t, that points to a buffer of size size. */ 114 ORTP_PUBLIC mblk_t *allocb(size_t size, int unused); 115 #define BPRI_MED 0 116 117 /* allocates a mblk_t, that points to a datab_t, that points to buf; buf will be freed using freefn */ 118 ORTP_PUBLIC mblk_t *esballoc(uint8_t *buf, size_t size, int pri, void (*freefn)(void*) ); 119 120 /* frees a mblk_t, and if the datab ref_count is 0, frees it and the buffer too */ 121 ORTP_PUBLIC void freeb(mblk_t *m); 122 123 /* frees recursively (follow b_cont) a mblk_t, and if the datab 124 ref_count is 0, frees it and the buffer too */ 125 ORTP_PUBLIC void freemsg(mblk_t *mp); 126 127 /* duplicates a mblk_t , buffer is not duplicated*/ 128 ORTP_PUBLIC mblk_t *dupb(mblk_t *m); 129 130 /* duplicates a complex mblk_t, buffer is not duplicated */ 131 ORTP_PUBLIC mblk_t *dupmsg(mblk_t* m); 132 133 /* returns the size of data of a message */ 134 ORTP_PUBLIC size_t msgdsize(const mblk_t *mp); 135 136 /* concatenates all fragment of a complex message*/ 137 ORTP_PUBLIC void msgpullup(mblk_t *mp,size_t len); 138 139 /* duplicates a single message, but with buffer included */ 140 ORTP_PUBLIC mblk_t *copyb(const mblk_t *mp); 141 142 /* duplicates a complex message with buffer included */ 143 ORTP_PUBLIC mblk_t *copymsg(const mblk_t *mp); 144 145 ORTP_PUBLIC mblk_t * appendb(mblk_t *mp, const char *data, size_t size, bool_t pad); 146 ORTP_PUBLIC void msgappend(mblk_t *mp, const char *data, size_t size, bool_t pad); 147 148 ORTP_PUBLIC mblk_t *concatb(mblk_t *mp, mblk_t *newm); 149 150 #define qempty(q) (&(q)->_q_stopper==(q)->_q_stopper.b_next) 151 #define qfirst(q) ((q)->_q_stopper.b_next!=&(q)->_q_stopper ? (q)->_q_stopper.b_next : NULL) 152 #define qbegin(q) ((q)->_q_stopper.b_next) 153 #define qlast(q) ((q)->_q_stopper.b_prev!=&(q)->_q_stopper ? (q)->_q_stopper.b_prev : NULL) 154 #define qend(q,mp) ((mp)==&(q)->_q_stopper) 155 #define qnext(q,mp) ((mp)->b_next) 156 157 typedef struct _msgb_allocator{ 158 queue_t q; 159 }msgb_allocator_t; 160 161 ORTP_PUBLIC void msgb_allocator_init(msgb_allocator_t *pa); 162 ORTP_PUBLIC mblk_t *msgb_allocator_alloc(msgb_allocator_t *pa, size_t size); 163 ORTP_PUBLIC void msgb_allocator_uninit(msgb_allocator_t *pa); 164 165 ORTP_PUBLIC void ortp_recvaddr_to_sockaddr(ortp_recv_addr_t *recvaddr, struct sockaddr *addr, socklen_t *socklen); 166 167 #ifdef __cplusplus 168 } 169 #endif 170 171 #endif 172