Debian Codegraph @

pppd/plugins/pppoatm/text2qos.c

raw
1 /* text2qos.c - Converts textual representation of QOS parameters to binary
2 encoding */
3
4 /* Written 1996-2000 by Werner Almesberger, EPFL-LRC/ICA */
5
6 #if HAVE_CONFIG_H
7 #include <config.h>
8 #endif
9
10 #include <stdlib.h>
11 #include <stdarg.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <limits.h>
15
16 #include "atm.h"
17
18
19 #define fetch __atmlib_fetch
20
21
22 #define RATE_ERROR -2
23
24
25 int __t2q_get_rate(const char **text,int up)
26 {
27 const char mult[] = "kKmMgGg";
28 const char *multiplier;
29 char *end;
30 unsigned int rate,fract;
31 int power;
32
33 if (!strncmp(*text,"max",3)) {
34 *text += 3;
35 return ATM_MAX_PCR;
36 }
37 rate = strtoul(*text,&end,10);
38 power = fract = 0;
39 if (*end == '.')
40 for (end++; *end && isdigit(*end); end++) {
41 fract = fract*10+*end-48;
42 if (--power == -9) break;
43 }
44 multiplier = NULL;
45 if (*end && (multiplier = strchr(mult,*end))) {
46 while (multiplier >= mult) {
47 if (rate > UINT_MAX/1000) return RATE_ERROR;
48 rate *= 1000;
49 power += 3;
50 multiplier -= 2;
51 }
52 end++;
53 }
54 while (power && fract)
55 if (power < 0) {
56 fract /= 10;
57 power++;
58 }
59 else {
60 fract *= 10;
61 power--;
62 }
63 rate += fract;
64 if (strlen(end) < 3) {
65 if (multiplier) return RATE_ERROR;
66 }
67 else if (!strncmp(end,"cps",3)) end += 3;
68 else if (!strncmp(end,"bps",3)) {
69 if (up && rate % (8 * ATM_CELL_PAYLOAD) == 0)
70 up = 0;
71 rate = rate / (8 * ATM_CELL_PAYLOAD) + !!up;
72 end += 3;
73 }
74 else if (multiplier) return RATE_ERROR;
75 if (rate > INT_MAX) return RATE_ERROR;
76 *text = end;
77 return rate;
78 }
79
80
81 static int params(const char **text,struct atm_trafprm *a,
82 struct atm_trafprm *b)
83 {
84 int value;
85 char *end;
86
87 if (*(*text)++ != ':') return -1;
88 while (1) {
89 if (!**text) return -1;
90 switch (fetch(text,"max_pcr=","pcr=","min_pcr=","max_sdu=","sdu=",
91 NULL)) {
92 case 0:
93 if ((value = __t2q_get_rate(text,0)) == RATE_ERROR) return -1;
94 if (a) a->max_pcr = value;
95 if (b) b->max_pcr = value;
96 break;
97 case 1:
98 if ((value = __t2q_get_rate(text,0)) == RATE_ERROR) return -1;
99 if (a) a->pcr = value;
100 if (b) b->pcr = value;
101 break;
102 case 2:
103 if ((value = __t2q_get_rate(text,1)) == RATE_ERROR) return -1;
104 if (value == ATM_MAX_PCR) return -1;
105 if (a) a->min_pcr = value;
106 if (b) b->min_pcr = value;
107 break;
108 case 3:
109 case 4:
110 value = strtol(*text,&end,10);
111 if (value < 0) return -1;
112 *text = end;
113 if (a) a->max_sdu = value;
114 if (b) b->max_sdu = value;
115 break;
116 default:
117 return 0;
118 }
119 if (!**text) break;
120 if (*(*text)++ != ',') return -1;
121 }
122 return 0;
123 }
124
125
126 int text2qos(const char *text,struct atm_qos *qos,int flags)
127 {
128 int traffic_class,aal;
129
130 traffic_class = ATM_NONE;
131 aal = ATM_NO_AAL;
132 do {
133 static const unsigned char aal_number[] = { ATM_AAL0, ATM_AAL5 };
134 int item;
135
136 item = fetch(&text,"!none","ubr","cbr","vbr","abr","aal0","aal5",NULL);
137 switch (item) {
138 case 1:
139 case 2:
140 /* we don't support VBR yet */
141 case 4:
142 traffic_class = item;
143 break;
144 case 5:
145 case 6:
146 aal = aal_number[item-5];
147 break;
148 default:
149 return -1;
150 }
151 }
152 while (*text == ',' ? text++ : 0);
153 if (!traffic_class) return -1;
154 if (qos && !(flags & T2Q_DEFAULTS)) memset(qos,0,sizeof(*qos));
155 if (qos) qos->txtp.traffic_class = qos->rxtp.traffic_class = traffic_class;
156 if (qos && aal) qos->aal = aal;
157 if (!*text) return 0;
158 if (params(&text,qos ? &qos->txtp : NULL,qos ? &qos->rxtp : NULL))
159 return -1;
160 if (!*text) return 0;
161 switch (fetch(&text,"tx","rx",NULL)) {
162 case 0:
163 if (!fetch(&text,":none",NULL)) {
164 if (qos) qos->txtp.traffic_class = ATM_NONE;
165 if (*text == ',') text++;
166 break;
167 }
168 if (params(&text,qos ? &qos->txtp : NULL,NULL)) return -1;
169 break;
170 case 1:
171 text -= 2;
172 break;
173 default:
174 return -1;
175 }
176 if (!*text) return 0;
177 if (fetch(&text,"rx",NULL)) return -1;
178 if (!fetch(&text,":none",NULL) && qos) qos->rxtp.traffic_class = ATM_NONE;
179 else if (params(&text,qos ? &qos->rxtp : NULL,NULL)) return -1;
180 return *text ? -1 : 0;
181 }