summaryrefslogtreecommitdiffstats
path: root/usr.sbin/i4b/isdnd/rates.c
blob: 5b7d8f8e536a28d16b2173b8aa57c4d1df0e8d51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*
 *   Copyright (c) 1997 Gary Jennejohn. All rights reserved.
 * 
 *   Copyright (c) 1997, 1999 Hellmuth Michaelis. All rights reserved.
 *
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions
 *   are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *   3. Neither the name of the author nor the names of any co-contributors
 *      may be used to endorse or promote products derived from this software
 *      without specific prior written permission.
 *   4. Altered versions must be plainly marked as such, and must not be
 *      misrepresented as being the original software and/or documentation.
 *   
 *   THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 *   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 *   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 *   ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 *   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 *   OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 *   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 *   OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 *   SUCH DAMAGE.
 *
 *---------------------------------------------------------------------------
 *
 *	i4b daemon - charging rates description file handling
 *	-----------------------------------------------------
 *
 *	$Id: rates.c,v 1.9 1999/02/15 09:55:47 hm Exp $ 
 *
 *      last edit-date: [Sun Feb 14 10:12:01 1999]
 *
 *---------------------------------------------------------------------------*/

static char error[256];

static int getrate(int rate_type);

#ifdef PARSE_DEBUG_MAIN

#include <stdio.h>

#define MAIN

#define ERROR (-1)

extern int got_rate;

int main( int argc, char **argv )
{
	int ret;
	ret = readrates("/etc/isdn/isdnd.rates");
	if(ret == ERROR)
		fprintf(stderr, "readrates returns [%d], [%s]\n", ret, error);
	else
		{
		int type = 0;

		got_rate = 1;

		fprintf(stderr, "readrates returns [%d]\n", ret);

		for( type=0; type<4; type++ )
			{
			int unit = getrate( type );
			fprintf(stderr, "getrate(%d) => %d\n", type, unit );
			}
		}

	return(ret);
}

#endif

#include "isdnd.h"

/*---------------------------------------------------------------------------*
 *	parse rates file
 *---------------------------------------------------------------------------*/
int
readrates(char *filename)
{
	char buffer[1024];
	register char *bp;
	struct rates *rt, *ort;
	int rateindx;
	int indx;
	int line = 0;
	FILE *fp;
	int first;
#if DEBUG
	int i, j;
#endif
	
	indx = 0;
	rt = ort = NULL;

	if((fp = fopen(filename, "r")) == NULL)
	{
		sprintf(error, "error open %s: %s", filename, sys_errlist[errno]);
		rate_error = error;
		return(WARNING);
	}

	while((fgets(buffer, MAXPATHLEN, fp)) != NULL)
	{
		line++;

/* comments */
		if(buffer[0] == '#'  || buffer[0] == ' ' ||
		   buffer[0] == '\t' || buffer[0] == '\n')
		{
			continue;
		}

		bp = &buffer[0];

		/* rate type */

		if (*bp == 'r' && *(bp+1) == 'a' && isdigit(*(bp+2)))
		{
				rateindx = *(bp+2) - '0';
				bp += 3;

				/* eat space delimiter */

				while(isspace(*bp))
					bp++;
		}
		else
		{
			sprintf(error, "rates: invalid rate type %c%c%c in line %d", *bp, *(bp+1), *(bp+2), line);
			goto rate_error;
		}
		if (rateindx >= NRATES)
		{
			sprintf(error, "rates: invalid rate index %d in line %d", rateindx, line);
			goto rate_error;
		}

		/* day */
		
		if(isdigit(*bp) && *bp >= '0' && *bp <= '6')
		{
			indx = *bp - '0';

			DBGL(DL_RATES, (log(LL_DBG, "rates: index = %d", indx)));
		}
		else
		{
			sprintf(error, "rates: invalid day digit %c in line %d", *bp, line);
			goto rate_error;
		}

		if(rates[rateindx][indx] == NULL)
		{
			rt = (struct rates *)malloc(sizeof (struct rates));
			if (rt == NULL)
			{
				sprintf(error, "rates: cannot malloc space for rate structure");
				goto rate_error;
		  	}
			rt->next = NULL;
		  	rates[rateindx][indx] = rt;
		}

		bp++;
		
		/* eat space delimiter */

		while(isspace(*bp))
			bp++;

		/* now loop to get the rates entries */

		first = 1;
		
		while(*bp && isdigit(*bp))
		{
			int hour = 0;
			int min = 0;

			if(first)
			{
				first = 0;
			}
			else
			{
				ort = rt;
	
				rt = (struct rates *)malloc(sizeof (struct rates));
				if (rt == NULL)
				{
					sprintf(error, "rates: cannot malloc space2 for rate structure");
					goto rate_error;
			  	}
				ort->next = rt;
				rt->next = NULL;
			}
			
			/* start hour */
			
			if(isdigit(*bp) && isdigit(*(bp+1)))
			{
				hour = atoi(bp);
				bp += 2;
			}
		  	else
			{
				sprintf(error, "rates: start_hr error in line %d", line);
				goto rate_error;
		  	}

			/* point */
			
		  	if(*bp == '.')
		  	{
		  		bp++;
		  	}
		  	else
		  	{
				sprintf(error, "rates: no '.' after start_hr in line %d", line);
				goto rate_error;
			}
		  	
			/* start minute */
			
			if(isdigit(*bp) && isdigit(*(bp+1)))
			{
				min = atoi(bp);
				bp += 2;
			}
		  	else
			{
				sprintf(error, "rates: start_min error in line %d", line);
				goto rate_error;
		  	}

			rt->start_time = hour*60 + min;

			/* minus */
			
		  	if(*bp == '-')
		  	{
		  		bp++;
		  	}
		  	else
		  	{
				sprintf(error, "rates: no '-' after start_min in line %d", line);
				goto rate_error;
			}

			/* end hour */
			
			if(isdigit(*bp) && isdigit(*(bp+1)))
			{
				hour = atoi(bp);
				bp += 2;
			}
		  	else
			{
				sprintf(error, "rates: end_hr error in line %d", line);
				goto rate_error;
		  	}

			/* point */
			
		  	if(*bp == '.')
		  	{
		  		bp++;
		  	}
		  	else
		  	{
				sprintf(error, "rates: no '.' after end_hr in line %d", line);
				goto rate_error;
			}
		  	
			/* end minute */
			
			if(isdigit(*bp) && isdigit(*(bp+1)))
			{
				min = atoi(bp);
				bp += 2;
			}
		  	else
			{
				sprintf(error, "rates: end_min error in line %d", line);
				goto rate_error;
		  	}

			/* if hour is 0 assume it means midnight */
			if( hour == 0 )
				hour = 24;
			rt->end_time = hour * 60 + min;

			if( rt->end_time <= rt->start_time )
				{
				sprintf(error, "rates: end_time must be greater then start_time %d", line);
				goto rate_error;
				}

			/* colon */
			
		  	if(*bp == ':')
		  	{
		  		bp++;
		  	}
		  	else
		  	{
				sprintf(error, "rates: no ':' after end_min in line %d", line);
				goto rate_error;
			}

			/* time */
			
			if(isdigit(*bp))
			{
				rt->rate = atoi(bp);
				while(!isspace(*bp))
					bp++;
			}
		  	else
			{
				sprintf(error, "rates: first rate digit error in line %d", line);
				goto rate_error;
		  	}

			/* eat space delimiter */

			while(isspace(*bp))
				bp++;
		}
	}

#if DEBUG
	if(debug_flags & DL_RATES)
	{
		for (j = 0; j < NRATES; j++)
		{
			for (i = 0; i < NDAYS; i++)
			{
				if (rates [j][i] != NULL)
				{
					rt = rates [j][i];
					for (; rt; rt = rt->next)
					{
						log(LL_DBG, "rates: index %d day %d = %d.%2.2d-%d.%2.2d:%d",
							j, i, rt->start_time/60, rt->start_time%60,
							rt->end_time/60,rt->end_time%60,rt->rate);
					}
				}
				else
				{
					log(LL_DBG, "rates: NO entry for day %d !!\n", i);
				}
			}
		}
	}
#endif
	fclose(fp);
	return(GOOD);

rate_error:
	fclose(fp);
	rate_error = error;
	return(ERROR);
}

#ifndef PARSE_DEBUG_MAIN

/*---------------------------------------------------------------------------*
 *	get unit length time from configured source
 *---------------------------------------------------------------------------*/
int
get_current_rate(cfg_entry_t *cep, int logit)
{
	int rt;
	
	switch(cep->unitlengthsrc)
	{
		case ULSRC_CMDL:	/* specified on commandline     */
			if(logit)
				log(LL_CHD, "%05d %s rate %d sec/unit (cmdl)",
					cep->cdid, cep->name, unit_length);
			return(unit_length);
			break;

		case ULSRC_CONF:	/* get it from config file      */
			if(logit)
				log(LL_CHD, "%05d %s rate %d sec/unit (conf)",
					cep->cdid, cep->name, cep->unitlength);
			return(cep->unitlength);

		case ULSRC_RATE:	/* get it dynamic from ratesfile*/
			if(!got_rate)	/* got valid rates struct ?? */
			{
				if(logit)
					log(LL_CHD, "%05d %s rate %d sec/unit (no ratefile)",
						cep->cdid, cep->name, UNITLENGTH_DEFAULT);
				return(UNITLENGTH_DEFAULT);
			}
			if((cep->ratetype >= NRATES) ||
			   (cep->ratetype == INVALID_RATE))
			{
				if(logit)
					log(LL_CHD, "%05d %s rate %d sec/unit (rate out of range)",
						cep->cdid, cep->name, UNITLENGTH_DEFAULT);
				return(UNITLENGTH_DEFAULT);
			}
			
			if((rt = getrate(cep->ratetype)) != -1)
			{
				if(logit)
					log(LL_CHD, "%05d %s rate %d sec/unit (rate)",
						cep->cdid, cep->name, rt);
				return(rt);
			}

			if(logit)			
				log(LL_CHD, "%05d %s rate %d sec/unit (ratescan fail)",
					cep->cdid, cep->name, UNITLENGTH_DEFAULT);

			return(UNITLENGTH_DEFAULT);
			break;

		case ULSRC_DYN:	/* dynamically calculated from AOC */
			if((rt = getrate(cep->ratetype)) != -1)
			{
				if(logit)
					log(LL_CHD, "%05d %s rate %d sec/unit (aocd, rate)",
						cep->cdid, cep->name, rt);
				return(rt);
			}
			if(logit)
				log(LL_CHD, "%05d %s rate %d sec/unit (aocd, default)",
					cep->cdid, cep->name, UNITLENGTH_DEFAULT);

			return(UNITLENGTH_DEFAULT);
			break;

		default:
			if(logit)
				log(LL_CHD, "%05d %s rate %d sec/unit (unitlen unknown)",
					cep->cdid, cep->name, UNITLENGTH_DEFAULT);

			return(UNITLENGTH_DEFAULT);
			break;
	}
}
#endif /* PARSE_DEBUG_MAIN */


/*---------------------------------------------------------------------------*
 *	get the currently active rate
 *---------------------------------------------------------------------------*/
static int
getrate(int rate_type )
{
	struct tm *ptr;
	time_t now;
	register struct rates *hd;
	int time_now;

	if((!got_rate) ||
	   (rate_type >= NRATES) ||
	   (rate_type == INVALID_RATE))
	{
		return -1;
	}

	time(&now);			/* get current time */

	ptr = localtime(&now);

	time_now = ptr->tm_hour*60 + ptr->tm_min;

	/* walk thru the rates for weekday until rate for current time found */

	for (hd = rates[rate_type][ptr->tm_wday]; hd; hd = hd->next)
	{
		/* current time within window ? */
		if((time_now >= hd->start_time ) &&
		   (time_now < hd->end_time ))
		{
			DBGL(DL_RATES, (log(LL_DBG, "rate=%d sec/unit (day=%d, beg=%d:%2.2d, end=%d:2.2d, current=%d:%2.2d)",
				hd->rate,
				ptr->tm_wday,
				hd->start_time/60, hd->start_time%60,
				hd->end_time/60, hd->end_time%60,
				time_now/60, time_now%60)));
				
			return hd->rate;
		}
	}
	return -1;
}

/* EOF */
OpenPOWER on IntegriCloud