Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.sbin / i4b / isdnd / dial.c
1 /*
2  * Copyright (c) 1997, 2001 Hellmuth Michaelis. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  *---------------------------------------------------------------------------
26  *
27  *      i4b daemon - dial handling routines
28  *      -----------------------------------
29  *
30  *      $Id: dial.c,v 1.8 1999/12/13 21:25:24 hm Exp $ 
31  *
32  * $FreeBSD: src/usr.sbin/i4b/isdnd/dial.c,v 1.6.2.2 2001/12/16 15:13:38 hm Exp $
33  *
34  *      last edit-date: [Mon Dec 13 21:45:51 1999]
35  *
36  *---------------------------------------------------------------------------*/
37
38 #include "isdnd.h"
39
40 /*---------------------------------------------------------------------------*
41  *      select the first remote number to dial according to the
42  *      dial strategy
43  *---------------------------------------------------------------------------*/
44 void
45 select_first_dialno(cfg_entry_t *cep)
46 {
47         int i, j;
48
49         if(cep->keypad[0] != '\0')
50                 return;
51
52         if(cep->remote_numbers_count < 1)
53         {
54                 log(LL_ERR, "select_first_dialno: remote_numbers_count < 1!");
55                 return;
56         }
57
58         if(cep->remote_numbers_count == 1)
59         {
60                 strcpy(cep->remote_phone_dialout, cep->remote_numbers[0].number);
61                 DBGL(DL_DIAL, (log(LL_DBG, "select_first_dialno: only one no, no = %s", cep->remote_phone_dialout)));
62                 cep->last_remote_number = 0;
63                 return;
64         }
65
66         if(cep->remote_numbers_handling == RNH_FIRST)
67         {
68                 strcpy(cep->remote_phone_dialout, cep->remote_numbers[0].number);
69                 DBGL(DL_DIAL, (log(LL_DBG, "select_first_dialno: use first, no = %s", cep->remote_phone_dialout)));
70                 cep->last_remote_number = 0;
71                 return;
72         }
73
74         i = cep->last_remote_number;
75            
76         for(j = cep->remote_numbers_count; j > 0; j--)
77         {
78                 if(cep->remote_numbers[i].flag == RNF_SUCC)
79                 {
80                         if(cep->remote_numbers_handling == RNH_LAST)
81                         {
82                                 strcpy(cep->remote_phone_dialout, cep->remote_numbers[i].number);
83                                 DBGL(DL_DIAL, (log(LL_DBG, "select_first_dialno: use last, no = %s", cep->remote_phone_dialout)));
84                                 cep->last_remote_number = i;
85                                 return;
86                         }
87                         else
88                         {
89                                 if(++i >= cep->remote_numbers_count)
90                                         i = 0;
91
92                                 strcpy(cep->remote_phone_dialout, cep->remote_numbers[i].number);
93                                 DBGL(DL_DIAL, (log(LL_DBG, "select_first_dialno: use next, no = %s", cep->remote_phone_dialout)));
94                                 cep->last_remote_number = i;
95                                 return;
96                         }
97                 }
98
99                 if(++i >= cep->remote_numbers_count)
100                         i = 0;
101         }
102         strcpy(cep->remote_phone_dialout, cep->remote_numbers[0].number);
103         DBGL(DL_DIAL, (log(LL_DBG, "select_first_dialno: no last found (use 0), no = %s", cep->remote_phone_dialout)));
104         cep->last_remote_number = 0;    
105 }                                                                       
106
107 /*---------------------------------------------------------------------------*
108  *      select next remote number to dial (last was unsuccesfull)
109  *---------------------------------------------------------------------------*/
110 void
111 select_next_dialno(cfg_entry_t *cep)
112 {
113         if(cep->remote_numbers_count < 1)
114         {
115                 log(LL_ERR, "select_next_dialno: remote_numbers_count < 1!");
116                 return;
117         }
118
119         if(cep->remote_numbers_count == 1)
120         {
121                 strcpy(cep->remote_phone_dialout, cep->remote_numbers[0].number);
122                 DBGL(DL_DIAL, (log(LL_DBG, "select_next_dialno: only one no, no = %s", cep->remote_phone_dialout)));
123                 cep->last_remote_number = 0;
124                 return;
125         }
126
127         /* mark last try as bad */
128
129         cep->remote_numbers[cep->last_remote_number].flag = RNF_IDLE;
130
131         /* next one to try */
132         
133         cep->last_remote_number++;
134
135         if(cep->last_remote_number >= cep->remote_numbers_count)
136                 cep->last_remote_number = 0;
137
138         strcpy(cep->remote_phone_dialout, cep->remote_numbers[cep->last_remote_number].number);
139         
140         DBGL(DL_DIAL, (log(LL_DBG, "select_next_dialno: index=%d, no=%s",
141                 cep->last_remote_number,
142                 cep->remote_numbers[cep->last_remote_number].number)));
143 }                                                                       
144
145 /*---------------------------------------------------------------------------*
146  *      dial succeded, store this number as the last successful
147  *---------------------------------------------------------------------------*/
148 void
149 select_this_dialno(cfg_entry_t *cep)
150 {
151         cep->remote_numbers[cep->last_remote_number].flag = RNF_SUCC;
152         
153         DBGL(DL_DIAL, (log(LL_DBG, "select_this_dialno: index = %d, no = %s",
154                 cep->last_remote_number,
155                 cep->remote_numbers[cep->last_remote_number].number)));
156 }
157
158 /* EOF */