]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - usr.sbin/i4b/isdnd/dial.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / usr.sbin / i4b / isdnd / dial.c
1 /*
2  * Copyright (c) 1997, 2002 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  * $FreeBSD$
31  *
32  *      last edit-date: [Sat May 13 13:05:52 2006]
33  *
34  *---------------------------------------------------------------------------*/
35
36 #include "isdnd.h"
37
38 /*---------------------------------------------------------------------------*
39  *      select the first remote number to dial according to the
40  *      dial strategy
41  *---------------------------------------------------------------------------*/
42 void
43 select_first_dialno(cfg_entry_t *cep)
44 {
45         int i, j;
46
47         if(cep->keypad[0] != '\0')
48                 return;
49
50         if(cep->remote_numbers_count < 1)
51         {
52                 llog(LL_ERR, "select_first_dialno: remote_numbers_count < 1!");
53                 return;
54         }
55
56         if(cep->remote_numbers_count == 1)
57         {
58                 strcpy(cep->remote_phone_dialout.number, cep->remote_numbers[0].number);
59                 strcpy(cep->remote_phone_dialout.subaddr, cep->remote_numbers[0].subaddr);
60                 DBGL(DL_DIAL, (llog(LL_DBG, "select_first_dialno: only one no, no = %s", cep->remote_phone_dialout.number)));
61                 cep->last_remote_number = 0;
62                 return;
63         }
64
65         if(cep->remote_numbers_handling == RNH_FIRST)
66         {
67                 strcpy(cep->remote_phone_dialout.number, cep->remote_numbers[0].number);
68                 strcpy(cep->remote_phone_dialout.subaddr, cep->remote_numbers[0].subaddr);
69                 DBGL(DL_DIAL, (llog(LL_DBG, "select_first_dialno: use first, no = %s", cep->remote_phone_dialout.number)));
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.number, cep->remote_numbers[i].number);
83                                 strcpy(cep->remote_phone_dialout.subaddr, cep->remote_numbers[i].subaddr);
84                                 DBGL(DL_DIAL, (llog(LL_DBG, "select_first_dialno: use last, no = %s", cep->remote_phone_dialout.number)));
85                                 cep->last_remote_number = i;
86                                 return;
87                         }
88                         else
89                         {
90                                 if(++i >= cep->remote_numbers_count)
91                                         i = 0;
92
93                                 strcpy(cep->remote_phone_dialout.number, cep->remote_numbers[i].number);
94                                 strcpy(cep->remote_phone_dialout.subaddr, cep->remote_numbers[i].subaddr);
95                                 DBGL(DL_DIAL, (llog(LL_DBG, "select_first_dialno: use next, no = %s", cep->remote_phone_dialout.number)));
96                                 cep->last_remote_number = i;
97                                 return;
98                         }
99                 }
100
101                 if(++i >= cep->remote_numbers_count)
102                         i = 0;
103         }
104         strcpy(cep->remote_phone_dialout.number, cep->remote_numbers[0].number);
105         DBGL(DL_DIAL, (llog(LL_DBG, "select_first_dialno: no last found (use 0), no = %s", cep->remote_phone_dialout.number)));
106         cep->last_remote_number = 0;    
107 }                                                                       
108
109 /*---------------------------------------------------------------------------*
110  *      select next remote number to dial (last was unsuccesfull)
111  *---------------------------------------------------------------------------*/
112 void
113 select_next_dialno(cfg_entry_t *cep)
114 {
115         if(cep->remote_numbers_count < 1)
116         {
117                 llog(LL_ERR, "select_next_dialno: remote_numbers_count < 1!");
118                 return;
119         }
120
121         if(cep->remote_numbers_count == 1)
122         {
123                 strcpy(cep->remote_phone_dialout.number, cep->remote_numbers[0].number);
124                 strcpy(cep->remote_phone_dialout.subaddr, cep->remote_numbers[0].subaddr);
125                 DBGL(DL_DIAL, (llog(LL_DBG, "select_next_dialno: only one no, no = %s", cep->remote_phone_dialout.number)));
126                 cep->last_remote_number = 0;
127                 return;
128         }
129
130         /* mark last try as bad */
131
132         cep->remote_numbers[cep->last_remote_number].flag = RNF_IDLE;
133
134         /* next one to try */
135         
136         cep->last_remote_number++;
137
138         if(cep->last_remote_number >= cep->remote_numbers_count)
139                 cep->last_remote_number = 0;
140
141         strcpy(cep->remote_phone_dialout.number, cep->remote_numbers[cep->last_remote_number].number);
142         
143         DBGL(DL_DIAL, (llog(LL_DBG, "select_next_dialno: index=%d, no=%s",
144                 cep->last_remote_number,
145                 cep->remote_numbers[cep->last_remote_number].number)));
146 }                                                                       
147
148 /*---------------------------------------------------------------------------*
149  *      dial succeded, store this number as the last successful
150  *---------------------------------------------------------------------------*/
151 void
152 select_this_dialno(cfg_entry_t *cep)
153 {
154         cep->remote_numbers[cep->last_remote_number].flag = RNF_SUCC;
155         
156         DBGL(DL_DIAL, (llog(LL_DBG, "select_this_dialno: index = %d, no = %s",
157                 cep->last_remote_number,
158                 cep->remote_numbers[cep->last_remote_number].number)));
159 }
160
161 /* EOF */