/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 <stddef.h> |
30 | | #include <stdlib.h> |
31 | | #include <stdatomic.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 | | _Atomic(int) max_ttl; |
43 | | _Atomic(int) ttl; |
44 | | }; |
45 | | |
46 | | static void rtpp_ttl_dtor(struct rtpp_ttl_priv *); |
47 | | static void rtpp_ttl_reset(struct rtpp_ttl *); |
48 | | static void rtpp_ttl_reset_with(struct rtpp_ttl *, int); |
49 | | static int rtpp_ttl_get_remaining(struct rtpp_ttl *); |
50 | | static int rtpp_ttl_decr(struct rtpp_ttl *); |
51 | | |
52 | | DEFINE_SMETHODS(rtpp_ttl, |
53 | | .reset = &rtpp_ttl_reset, |
54 | | .reset_with = &rtpp_ttl_reset_with, |
55 | | .get_remaining = &rtpp_ttl_get_remaining, |
56 | | .decr = &rtpp_ttl_decr, |
57 | | ); |
58 | | |
59 | | struct rtpp_ttl * |
60 | | rtpp_ttl_ctor(int max_ttl) |
61 | 22.8k | { |
62 | 22.8k | struct rtpp_ttl_priv *pvt; |
63 | | |
64 | 22.8k | pvt = rtpp_rzmalloc(sizeof(struct rtpp_ttl_priv), PVT_RCOFFS(pvt)); |
65 | 22.8k | if (pvt == NULL) { |
66 | 0 | goto e0; |
67 | 0 | } |
68 | 22.8k | atomic_init(&pvt->max_ttl, max_ttl); |
69 | 22.8k | atomic_init(&pvt->ttl, max_ttl); |
70 | 22.8k | PUBINST_FININIT(&pvt->pub, pvt, rtpp_ttl_dtor); |
71 | 22.8k | return ((&pvt->pub)); |
72 | | |
73 | 0 | e0: |
74 | 0 | return (NULL); |
75 | 22.8k | } |
76 | | |
77 | | static void |
78 | | rtpp_ttl_dtor(struct rtpp_ttl_priv *pvt) |
79 | 22.8k | { |
80 | | |
81 | 22.8k | rtpp_ttl_fin(&(pvt->pub)); |
82 | 22.8k | } |
83 | | |
84 | | static void |
85 | | rtpp_ttl_reset(struct rtpp_ttl *self) |
86 | 122k | { |
87 | 122k | struct rtpp_ttl_priv *pvt; |
88 | 122k | int max_ttl; |
89 | | |
90 | 122k | PUB2PVT(self, pvt); |
91 | 122k | max_ttl = atomic_load_explicit(&pvt->max_ttl, memory_order_relaxed); |
92 | 122k | atomic_store_explicit(&pvt->ttl, max_ttl, memory_order_relaxed); |
93 | 122k | } |
94 | | |
95 | | static void |
96 | | rtpp_ttl_reset_with(struct rtpp_ttl *self, int max_ttl) |
97 | 36.5k | { |
98 | 36.5k | struct rtpp_ttl_priv *pvt; |
99 | | |
100 | 36.5k | PUB2PVT(self, pvt); |
101 | 36.5k | atomic_store_explicit(&pvt->max_ttl, max_ttl, memory_order_relaxed); |
102 | 36.5k | atomic_store_explicit(&pvt->ttl, max_ttl, memory_order_relaxed); |
103 | 36.5k | } |
104 | | |
105 | | static int |
106 | | rtpp_ttl_get_remaining(struct rtpp_ttl *self) |
107 | 50 | { |
108 | 50 | struct rtpp_ttl_priv *pvt; |
109 | 50 | int rval; |
110 | | |
111 | 50 | PUB2PVT(self, pvt); |
112 | 50 | rval = atomic_load_explicit(&pvt->ttl, memory_order_relaxed); |
113 | 50 | if (rval < 0) |
114 | 0 | rval = 0; |
115 | 50 | return (rval); |
116 | 50 | } |
117 | | |
118 | | static int |
119 | | rtpp_ttl_decr(struct rtpp_ttl *self) |
120 | 25 | { |
121 | 25 | struct rtpp_ttl_priv *pvt; |
122 | 25 | int rval; |
123 | | |
124 | 25 | PUB2PVT(self, pvt); |
125 | 25 | rval = atomic_fetch_sub_explicit(&pvt->ttl, 1, memory_order_relaxed); |
126 | 25 | if (rval < 0) |
127 | 0 | rval = 0; |
128 | 25 | return (rval); |
129 | 25 | } |