File: call.c

package info (click to toggle)
ifmail 2.14tx8.10-32
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,056 kB
  • sloc: ansic: 30,328; perl: 4,955; yacc: 839; makefile: 716; sh: 424; cpp: 235; lex: 206; awk: 24
file content (384 lines) | stat: -rw-r--r-- 7,911 bytes parent folder | download | duplicates (3)
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
/* ### Modified by P. Saratxaga on 25 Oct 1995 ###
 * - added aftercall(nlent)  (code from T.Tanaka)
 * - added Txy support from ifmail-os
 */
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "lutil.h"
#include "xutil.h"
#include "ftn.h"
#include "nodelist.h"
#include "config.h"
#include "session.h"
#include "callstat.h"
#include "Txy.h"
#include "setproctitle.h"

extern int forcedcalls;
extern char *forcedphone;
extern char *forcedline;
#if defined(HAS_TCP) || defined(HAS_TERM)
extern char *inetaddr;
#endif

/* ************************* */
extern int aftercall(node *);

extern char *get_modem_string(modem_string*,node*);
extern int openport(char*,int);
#if defined(HAS_TCP) || defined(HAS_TERM)
extern int opentcp(char*);
extern void closetcp(void);
#endif
extern void localport(void);
extern void nolocalport(void);
extern void closeport(void);
extern int dialphone(node*,char*);
extern int hangup(node*);
extern void rdoptions(node*);
extern int nodelock(faddr*);
extern int nodeulock(faddr*);

#ifdef HIDDEN

extern fa_list *Hidden;
extern fa_list *Override;

#define MAXLINES 16

struct s_hidden {
		 char *phone;
		 char *txy;
		};

int parsehidden(char *str,struct s_hidden *hidden)
{
 char *tmp,*p;

 hidden->phone=NULL;
 hidden->txy=NULL;

 debug(6,"Hidden string %s\n",str);

 tmp=strdup(str);
 p=strtok(tmp," ");
  if(p)
   {
     if(p[0]!='-') hidden->phone=strdup(p);
     p=strtok(NULL," ");
     if(p) hidden->txy=strdup(p);
   }
   free(tmp); 
   return 1;
}

int override(faddr *addr,node *nlent,fa_list *FromWhere)
{
 int i;
 static char TXY[4];
 struct s_hidden hidden;

 hidden.phone=NULL;
 hidden.txy=NULL;

 parsehidden(FromWhere->addr->name,&hidden);
 if(hidden.phone) strncpy(nlent->phone,hidden.phone,strlen(nlent->phone));
  if(hidden.txy)
    if(strcasecmp(hidden.txy,"CM")==0) nlent->flags |= CM;
    else
     {
      strncpy(TXY,hidden.txy,3);
      if(nlent->flags & CM)
       {
        i=0;while(nlent->uflags[i] && i<MAXUFLAGS) i++;
        if(i<MAXUFLAGS) nlent->uflags[i]=TXY;
        if(i+1<MAXUFLAGS) nlent->uflags[i+1]=NULL;
        nlent->flags&=~CM;
       }
      else
       {
        i=0;
        while(nlent->uflags[i] && i<MAXUFLAGS)
         {
          if(nlent->uflags[i][0]=='T' ||
	     (nlent->uflags[i][0]=='U' && nlent->uflags[i][1]=='T'))
	   {
	    nlent->uflags[i]=TXY;
	    i=MAXUFLAGS+1;
	   }
	   i++;
	  }
	   if(i<=MAXUFLAGS)
	    {
	     if(i<MAXUFLAGS) nlent->uflags[i]=TXY;
	      else loginf("Can't change call time for %s",ascinode(addr,0x1f));
	      if(i+1<MAXUFLAGS) nlent->uflags[i+1]=NULL;
            }
          }
      } /* Not CM */
 free(hidden.phone);
 free(hidden.txy);
 return 0;
}

int setline(faddr *addr,node *nlent,callstat *st)
{
 fa_list *tmp,*Lines[MAXLINES];
 int i,TryLine,LineCount=1;

 Lines[0]=NULL;	/* First Line - from nodelist. May be override */

 for (tmp=Override;tmp;tmp=tmp->next)
  if ((tmp->addr->net == addr->net) &&
	    (tmp->addr->node == addr->node)) Lines[0]=tmp;

 for (tmp=Hidden;tmp;tmp=tmp->next)
  if ((tmp->addr->net == addr->net) && (tmp->addr->node == addr->node))
    if(LineCount<MAXLINES) { Lines[LineCount]=tmp; LineCount++; }

 TryLine = st->tryno % LineCount;

 debug(6,"--- First TryLine = %d from %d lines --- \n", TryLine,LineCount);

 for(i=TryLine;i<LineCount;i++)
   {
    nlent=getnlent(addr);
    if(Lines[i]) override(addr,nlent,Lines[i]);
    debug(6,"Check Line %d\n",i);
    if(!not_work_time_now(nlent)) return 1;
   }

 for(i=0;i<TryLine;i++)
   {
    nlent=getnlent(addr);
    if(Lines[i]) override(addr,nlent,Lines[i]);
    debug(6,"Check Line %d\n",i);
    if(!not_work_time_now(nlent)) return 1;
   }
 return 0;
}

#endif

int checkretry(st)
callstat *st;
{
	return 0;
	/* check retries and time; rc=1 - not reached, rc=2 - undialable */
}

int call(addr)
faddr *addr;
{
	node *nlent;
	int rc=1;
	int speed;
	char *portlist,*p,*q;
	callstat *st;

	if ((nlent=getnlent(addr)) == NULL)
	{
		logerr("cannot call %s: fatal in nodelist lookup",
			ascfnode(addr,0x1f));
		putstatus(addr,0,6);
		return 6;
	}

	st=getstatus(addr);
#ifdef HIDDEN
        setline(addr,nlent,st);
#endif	
	if (!forcedcalls) {
		if ((rc=checkretry(st))) {
			loginf("cannot call %s: %s",
				ascfnode(addr,0x1f),
				(rc==1)?"retry time not reached"
				:"node undialable");
			return 5;
		}
		if (dialdelay) sleep(dialdelay);
	}

	if (nodelock(addr))
	{
		loginf("system %s locked",ascinode(addr,0x1f));
		putstatus(addr,0,4);
		return 4;
	}

	rdoptions(nlent);

	inbound=protinbound; /* master sessions are secure */

	setproctitle("ifcico calling %s (%s %s)",
		ascfnode(&(nlent->addr),0x1f),
#if defined(HAS_TCP) || defined(HAS_TERM)
		inetaddr?"addr":"phone",
		inetaddr?inetaddr:
#else
		"phone",
#endif
			forcedphone?forcedphone:
				nlent->phone?nlent->phone:"<none>");

	if ((nlent->phone || forcedphone
#if defined(HAS_TCP) || defined(HAS_TERM)
					 || inetaddr
#endif
							) &&
	    (forcedcalls || 
	     (((nlent->pflag & (NL_DUMMY|NL_DOWN|NL_HOLD|NL_PVT)) == 0) && 
	      ((localoptions & NOCALL) == 0) && !not_work_time_now(nlent) ||
		(localoptions & NOTXY))))
	{
#if defined(HAS_TCP) || defined(HAS_TERM)
		loginf("calling %s (%s, %s %s)",
			ascfnode(addr,0x1f),nlent->name,
			inetaddr?"addr":"phone",
			inetaddr?inetaddr:
			forcedphone?forcedphone:nlent->phone);
#else
		loginf("calling %s (%s, phone %s)",
			ascfnode(addr,0x1f),nlent->name,
			forcedphone?forcedphone:nlent->phone);
#endif
#if defined(HAS_TCP) || defined(HAS_TERM)
		if (inetaddr)
		{
			rc=opentcp(inetaddr);
			if (rc)
			{
				loginf("cannot connect %s",inetaddr);
				nodeulock(addr);
				putstatus(addr,1,2);
				return 2;
			}
		}
		else
#endif
		     if (forcedline)
		{
			p=forcedline;
			if ((q=strchr(p,':')))
			{
				*q++='\0';
				if ((*q == 'l') || (*q == 'L'))
					speed=atoi(++q);
				else
				{
					speed=atoi(q);
					if (nlent->speed < speed)
						speed=nlent->speed;
				}
			}
			else speed=0;
			setnamesfx(p);
			rc=openport(p,speed);
			if (rc)
			{
				loginf("cannot open port %s",p);
				nodeulock(addr);
				putstatus(addr,0,1);
				return 1;
			}
		}
		else
		{
			if ((portlist=xstrcpy(get_modem_string(modemport,nlent))) == NULL)
			{
				logerr("no matching ports defined");
				nodeulock(addr);
				putstatus(addr,0,9);
				return 9;
			}
			for (rc=1,p=strtok(portlist," \t,");
				rc && p;
				p=strtok(NULL," \t,"))
			{
				if ((q=strrchr(p,':')))
				{
					*q++='\0';
					if ((*q == 'l') || (*q == 'L'))
					{
						speed=atoi(++q);
					}
					else
					{
						speed=atoi(q);
						if (nlent->speed < speed)
							speed=nlent->speed;
					}
				}
				else speed=0;
				setnamesfx(p);
				rc=openport(p,speed);
			}
			if (rc)
			{
				loginf("no free matching ports");
				free(portlist);
				nodeulock(addr);
				putstatus(addr,0,1);
				return 1;
			}
			free(portlist);
		}
#if defined(HAS_TCP) || defined(HAS_TERM)
		if (!inetaddr)
#endif
		if ((rc=dialphone(nlent,forcedphone?forcedphone:nlent->phone)))
		{
			loginf("dial failed");
			nodeulock(addr);
			rc+=1; /* rc=2 - dial fail, rc=3 - could not reset */
		}

		if (rc == 0)
		{
#if defined(HAS_TCP) || defined(HAS_TERM)
			if (!inetaddr)
#endif
				nolocalport();
			rc=session(addr,nlent,SESSION_MASTER,
				SESSION_UNKNOWN,NULL);
			if (rc) rc=abs(rc)+10;
		}
#if defined(HAS_TCP) || defined(HAS_TERM)
		if (inetaddr)
		{
			closetcp();
		}
		else
#endif
		{
			localport();
#ifndef NO_IO_AFTER_HANGUP
			hangup(nlent);
			aftercall(nlent);
#endif
			closeport();
		}
	}
	else
	{
		loginf("cannot call %s (%s, phone %s)",
			ascfnode(addr,0x1f),S(nlent->name),
			S(nlent->phone));
		if ((nlent->phone || forcedphone
#if defined(HAS_TCP) || defined(HAS_TERM)
						 || inetaddr
#endif
								))
			rc=8;
		else rc=7;
		nodeulock(addr);
	}

	if ((rc == 2) || (rc == 30))
		putstatus(addr,1,rc);
	else
		putstatus(addr,0,rc);
	return rc;
}