/src/rtpproxy/src/rtpp_ttl.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2004-2006 Maxim Sobolev <sobomax@FreeBSD.org> |
3 | | * Copyright (c) 2006-2015 Sippy Software, Inc., http://www.sippysoft.com |
4 | | * All rights reserved. |
5 | | * |
6 | | * Redistribution and use in source and binary forms, with or without |
7 | | * modification, are permitted provided that the following conditions |
8 | | * are met: |
9 | | * 1. Redistributions of source code must retain the above copyright |
10 | | * notice, this list of conditions and the following disclaimer. |
11 | | * 2. Redistributions in binary form must reproduce the above copyright |
12 | | * notice, this list of conditions and the following disclaimer in the |
13 | | * documentation and/or other materials provided with the distribution. |
14 | | * |
15 | | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
16 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
19 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
20 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
21 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
22 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
23 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
24 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
25 | | * SUCH DAMAGE. |
26 | | * |
27 | | */ |
28 | | |
29 | | #include <pthread.h> |
30 | | #include <stddef.h> |
31 | | #include <stdlib.h> |
32 | | |
33 | | #include "rtpp_types.h" |
34 | | #include "rtpp_mallocs.h" |
35 | | #include "rtpp_codeptr.h" |
36 | | #include "rtpp_refcnt.h" |
37 | | #include "rtpp_ttl.h" |
38 | | #include "rtpp_ttl_fin.h" |
39 | | |
40 | | struct rtpp_ttl_priv { |
41 | | struct rtpp_ttl pub; |
42 | | int max_ttl; |
43 | | int ttl; |
44 | | pthread_mutex_t lock; |
45 | | }; |
46 | | |
47 | | static void rtpp_ttl_dtor(struct rtpp_ttl_priv *); |
48 | | static void rtpp_ttl_reset(struct rtpp_ttl *); |
49 | | static void rtpp_ttl_reset_with(struct rtpp_ttl *, int); |
50 | | static int rtpp_ttl_get_remaining(struct rtpp_ttl *); |
51 | | static int rtpp_ttl_decr(struct rtpp_ttl *); |
52 | | |
53 | | DEFINE_SMETHODS(rtpp_ttl, |
54 | | .reset = &rtpp_ttl_reset, |
55 | | .reset_with = &rtpp_ttl_reset_with, |
56 | | .get_remaining = &rtpp_ttl_get_remaining, |
57 | | .decr = &rtpp_ttl_decr, |
58 | | ); |
59 | | |
60 | | struct rtpp_ttl * |
61 | | rtpp_ttl_ctor(int max_ttl) |
62 | 32.0k | { |
63 | 32.0k | struct rtpp_ttl_priv *pvt; |
64 | | |
65 | 32.0k | pvt = rtpp_rzmalloc(sizeof(struct rtpp_ttl_priv), PVT_RCOFFS(pvt)); |
66 | 32.0k | if (pvt == NULL) { |
67 | 0 | goto e0; |
68 | 0 | } |
69 | 32.0k | if (pthread_mutex_init(&pvt->lock, NULL) != 0) { |
70 | 0 | goto e1; |
71 | 0 | } |
72 | 32.0k | pvt->ttl = pvt->max_ttl = max_ttl; |
73 | 32.0k | PUBINST_FININIT(&pvt->pub, pvt, rtpp_ttl_dtor); |
74 | 32.0k | return ((&pvt->pub)); |
75 | | |
76 | 0 | e1: |
77 | 0 | RTPP_OBJ_DECREF(&(pvt->pub)); |
78 | 0 | e0: |
79 | 0 | return (NULL); |
80 | 0 | } |
81 | | |
82 | | static void |
83 | | rtpp_ttl_dtor(struct rtpp_ttl_priv *pvt) |
84 | 32.0k | { |
85 | | |
86 | 32.0k | rtpp_ttl_fin(&(pvt->pub)); |
87 | 32.0k | pthread_mutex_destroy(&pvt->lock); |
88 | 32.0k | } |
89 | | |
90 | | static void |
91 | | rtpp_ttl_reset(struct rtpp_ttl *self) |
92 | 39.8k | { |
93 | 39.8k | struct rtpp_ttl_priv *pvt; |
94 | | |
95 | 39.8k | PUB2PVT(self, pvt); |
96 | 39.8k | pthread_mutex_lock(&pvt->lock); |
97 | 39.8k | pvt->ttl = pvt->max_ttl; |
98 | 39.8k | pthread_mutex_unlock(&pvt->lock); |
99 | 39.8k | } |
100 | | |
101 | | static void |
102 | | rtpp_ttl_reset_with(struct rtpp_ttl *self, int max_ttl) |
103 | 6.48k | { |
104 | 6.48k | struct rtpp_ttl_priv *pvt; |
105 | | |
106 | 6.48k | PUB2PVT(self, pvt); |
107 | 6.48k | pthread_mutex_lock(&pvt->lock); |
108 | 6.48k | pvt->ttl = max_ttl; |
109 | 6.48k | pvt->max_ttl = max_ttl; |
110 | 6.48k | pthread_mutex_unlock(&pvt->lock); |
111 | 6.48k | } |
112 | | |
113 | | static int |
114 | | rtpp_ttl_get_remaining(struct rtpp_ttl *self) |
115 | 6.69k | { |
116 | 6.69k | struct rtpp_ttl_priv *pvt; |
117 | 6.69k | int rval; |
118 | | |
119 | 6.69k | PUB2PVT(self, pvt); |
120 | 6.69k | pthread_mutex_lock(&pvt->lock); |
121 | 6.69k | rval = pvt->ttl; |
122 | 6.69k | pthread_mutex_unlock(&pvt->lock); |
123 | 6.69k | return (rval); |
124 | 6.69k | } |
125 | | |
126 | | static int |
127 | | rtpp_ttl_decr(struct rtpp_ttl *self) |
128 | 3 | { |
129 | 3 | struct rtpp_ttl_priv *pvt; |
130 | 3 | int rval; |
131 | | |
132 | 3 | PUB2PVT(self, pvt); |
133 | 3 | pthread_mutex_lock(&pvt->lock); |
134 | 3 | rval = pvt->ttl; |
135 | 3 | if (pvt->ttl > 0) |
136 | 3 | pvt->ttl--; |
137 | 3 | pthread_mutex_unlock(&pvt->lock); |
138 | 3 | return (rval); |
139 | 3 | } |