blob: f71ec6af52593e5b5fb479a2eb7fd34c55d7a244 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001#!/bin/sh /etc/rc.common
2# Copyright (C) 2014 CESNET, z.s.p.o
3# Copyright (C) 2018 OpenWrt.org
4
5START=90
6
7NAME=rtpproxy
8COMMAND="/usr/bin/$NAME"
9
10USE_PROCD=1
11
12#PROCD_DEBUG=1
13
14LOGGER="/usr/bin/logger -t $NAME"
15LOG_ERR="$LOGGER -p user.err -s"
16
17run_instance() {
18 procd_open_instance
19 procd_set_param command $COMMAND
20 procd_append_param command \
21 $1 \
22 -p "/var/run/$NAME-$2.pid" \
23 -f
24 # forward stderr to logd
25 procd_set_param stderr 1
26 procd_close_instance
27
28 $LOGGER instance $2 has started
29}
30
31check_ip() {
32 local tmp_addr
33
34 if [ "$1" = "ipaddr" ]; then
35 network_get_ipaddr tmp_addr "$2" || tmp_addr="$2"
36 else
37 network_get_ipaddr6 tmp_addr "$2" || tmp_addr="$2"
38 fi
39
40 echo "$tmp_addr"
41}
42
43check_ipaddr() {
44 local value="$1"
45 local type="$2"
46 local param="$3"
47 local one two
48
49 [ -z "$value" ] && {
50 $LOG_ERR empty $type entry
51 exit 1
52 }
53
54 # Bail if more than 1 slash.
55 [ $(echo "$value" | awk -F "/" '{print NF-1}') -gt 1 ] && {
56 $LOG_ERR init script does not understand $type entry \""$value"\"
57 exit 1
58 }
59
60 IFS="/" read one two << EOF
61$value
62EOF
63
64 one="$(check_ip "$type" "$one")"
65 if [ -n "$two" ]; then
66 two="$(check_ip "$type" "$two")"
67 rtpproxy_options=$rtpproxy_options" $param $one/$two"
68 else
69 rtpproxy_options=$rtpproxy_options" $param $one"
70 fi
71}
72
73check_param() {
74 local param="$1"
75 local value="$2"
76 local default_value="$3"
77
78 if [ "$value" != "" ]; then
79 rtpproxy_options=$rtpproxy_options" $param $value"
80 else
81 if [ "$default_value" != "" ]; then
82 rtpproxy_options=$rtpproxy_options" $param $default_value"
83 fi
84 fi
85}
86
87check_special_param() {
88 local param="$1"
89
90 if [ "$param" != "" ]; then
91 rtpproxy_options=$rtpproxy_options" $param"
92 fi
93}
94
95handle_instance() {
96 local site="$1"
97 local socket opts ipaddr ip6addr rtpproxy_options log_level
98
99 config_get socket "$site" socket
100 config_get opts "$site" opts
101 config_get ipaddr "$site" ipaddr
102 config_get ip6addr "$site" ip6addr
103 config_get user "$site" user
104 config_get log_level "$site" log_level
105
106 check_param "-s" "$socket"
107 check_param "-u" "$user" "nobody"
108 check_param "-d" "$log_level" "DBUG"
109
110 check_special_param "$opts"
111
112 [ -n "$ipaddr" ] && check_ipaddr "$ipaddr" ipaddr '-l'
113 [ -n "$ip6addr" ] && check_ipaddr "$ip6addr" ip6addr '-6'
114
115 run_instance "$rtpproxy_options" "$site"
116}
117
118start_service() {
119 local enabled
120
121 config_load $NAME
122
123 config_get_bool enabled global enabled 0
124
125 if [ "$enabled" -eq 1 ]; then
126 . /lib/functions/network.sh
127 config_foreach handle_instance instance
128 else
129 $LOG_ERR service not enabled
130 $LOG_ERR edit /etc/config/$NAME
131 fi
132}
133