summaryrefslogtreecommitdiffstats
path: root/gnu/usr.sbin/isdn/itel/iteld.c
blob: 8df099c109a7045e06ff2596a4beb367cdbfaf14 (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
static char     rcsid[] = "@(#)$Id: iteld.c,v 1.2 1995/01/25 13:58:28 jkr Exp jkr $";
/*******************************************************************************
 *  II - Version 0.1 $Revision: 1.2 $   $State: Exp $
 *
 * Copyright 1994 Dietmar Friede
 *******************************************************************************
 * Bug reports, patches, comments, suggestions should be sent to:
 *
 *	jkr@saarlink.de or jkrause@guug.de
 *
 *******************************************************************************
 * $Log: iteld.c,v $
 *
 ******************************************************************************/

/* This is a ISDN-Daemon */
/* It accepts ISDN-Telefon calls */

#include <sys/types.h>
#include <stdio.h>
#include <signal.h>
#include <errno.h>
#include <setjmp.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/ioctl.h>
#include <sys/file.h>
#include "../../../../sys/gnu/isdn/isdn_ioctl.h"
#define min(a,b)	((a)<(b)?(a):(b))

#define NITEL		1
#define NR_RINGS	10
#define ANSWER_NUM	2	/* which number we use */
#define TEL_LOG_FILE	"/var/log/isdn.tel"

listen_t        listen;
isdn_param      ip;

int             dofork = 1;
int             quit = 0;
int             def_subadr = 2;
jmp_buf         context;
char           *logfile = TEL_LOG_FILE;

struct answ
{
	char            occupied;
	u_char          an, cn;
	char            no[15];
}               answ[NITEL];

int             ind, rind;
char            rbuf[2048];

void
catchsig()
{
	quit++;
	longjmp(context, 1);
}

process(int n)
{
	fillparam(0, &ip, &listen);
	if (ioctl(n, ISDN_SET_PARAM, &ip) < 0)
	{
		perror("ioctl: Set Param");
		exit(3);
	}
	if (ioctl(n, ISDN_LISTEN, &listen) < 0)
	{
		perror("ioctl: Listen");
		exit(4);
	}
}

fillparam(int ap, isdn_param * ip, listen_t * t)
{

	bzero(ip, sizeof(isdn_param));
	bzero(t, sizeof(listen_t));
	t->appl = ip->appl = ap;
	t->ctrl = 0;
	t->inf_mask = 3;
	t->subadr_mask = (u_short) 0x3ff;
	t->si_mask = (u_short) 6;
	ip->dlpd.protokoll = 3;
	ip->dlpd.length = 7;
	ip->dlpd.data_length = 1024;
	ip->timeout = 60;
	ip->prot = 0;
	ip->ncpd.protokoll = 4;
}

void
main(int argc, char **argv)
{
	FILE           *f;
	int             a, n, i;

	dofork = 1;

	while ((i = getopt(argc, argv, "c:l:F")) != EOF)
		switch (i)
		{
		default:
			fprintf(stderr, "Usage: itel [ -F ] [ -l logfile ]\n");
			exit(1);
		case 'l':
			logfile = optarg;
			break;
		case 'F':
			dofork = 0;
			break;
		}

	if (dofork)
	{
		if ((i = fork()) < 0)
		{
			fprintf(stderr, "Can't fork, %m");
			exit(1);
		}
		if (i > 0)
			exit(0);
	}			/* running as daemon now */
	if (freopen(logfile, "a", stderr) == NULL)
	{
		perror(logfile);
		exit(1);
	}
	if ((n = open("/dev/isdn2", O_RDWR)) < 0)
	{
		perror("open: /dev/isdn2");
		exit(1);
	}
	process(n);

	(void) signal(SIGHUP, catchsig);
	(void) signal(SIGTERM, catchsig);
	(void) signal(SIGKILL, catchsig);
	(void) signal(SIGINT, catchsig);
	(void) signal(SIGQUIT, catchsig);

	quit = 0;
	fprintf(stderr, "s:iteld started\n");
	fflush(stderr);
	while (1)
	{
		int             l;
		int             an, cn, serv, serv_add, subadr;
		int             typ, nl, dl;
		char           *tn;
		u_char          buf[4];
		u_char          telnum[128];

		(void) setjmp(context);
		if ((l = read(n, rbuf, 1024)) > 0)
		{
			switch (rbuf[0])
			{
			case 'a':
				sscanf(rbuf + 2, "%d %d %d %d %d %d %d %n",
				       &an, &cn, &serv, &serv_add, &subadr, &typ, &nl, &l);
				l += 2;
				buf[0] = cn;
				buf[1] = find_appl();
				buf[2] = buf[3] = 0;
				if (buf[1] == 0xff)
				{
					buf[2] = 0x3e;	/* call reject */
					fprintf(stderr, "iteld: No Application\n");
				}
				if (ioctl(n, ISDN_ACCEPT, &buf) < 0)
				{
					perror("ioctl: Accept");
				}
				if (buf[1] == 0xff)
					fprintf(stderr, "r:%d:%s\n", an, rbuf + l);
				else
					fprintf(stderr, "a:%d:%s\n", an, rbuf + l);
				break;
			case 'd':
				fprintf(stderr, " s:dialing?i\n");
				break;
			case 'i':
				sscanf(rbuf + 2, "%d %d %d %n", &an, &typ, &nl, &l);
				fprintf(stderr, "i:%d:%x:%s\n", an, typ, rbuf + l + 2);
				break;
			case 'C':
				sscanf(rbuf + 2, "%d %d %d", &an, &cn, &dl);
				if (dl)
					break;
				fprintf(stderr, "C:%d:%d\n", an, cn);
				if (subadr == def_subadr)
					/* jkr */
					system("/isdn/lib/answ >/dev/null 2>&1 &");
				break;
			case 'D':
				sscanf(rbuf + 2, "%d %d", &an, &cn);
				answ[0].occupied = 0;
				fprintf(stderr, "D:%d:%d\n", an, cn);
				break;
			default:
				break;
			}
		}
		if (quit)
		{
			fprintf(stderr, "s:Quit\n");
			exit(0);
		}
		fflush(stderr);
	}
}

int
find_appl()
{
	int             i;
	struct answ    *a;

	for (i = 0; i < NITEL; i++)
	{
		a = &answ[i];
		if (a->occupied == 0)
		{
			a->occupied = 1;
			return (i);
		}
	}
	return (-1);
}
OpenPOWER on IntegriCloud