File: cyclades-ser-cli.c

package info (click to toggle)
cyclades-serial-client 0.92
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, jessie, jessie-kfreebsd, lenny, sarge, squeeze, wheezy
  • size: 668 kB
  • ctags: 833
  • sloc: ansic: 9,508; sh: 2,739; makefile: 112
file content (288 lines) | stat: -rw-r--r-- 5,625 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
/*
 *
 * Copyright (C) Cyclades Corporation, 1999-1999. All rights reserved.
 *
 *
 * tsrsock.c
 * cyclades-ser-cli main code
 *
 * History
 * 08/17/1999 V.1.0.0 Initial revision
 *
 * 17/12/1999 V.1.0.1 Version was changed to 1.0.1
 *
 * Oct-27-2001 V.1.0.2
 *	Program version has changed to 1.2.0
 *	Retry message now is of Warning type 
 */

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>

# define _TSR_TSRSOCK_

#include "inc/cyclades-ser-cli.h"
#include "inc/system.h"
#include "inc/tsrio.h"
#include "inc/sock.h"
#include "inc/dev.h"
#include "inc/misc.h"
#include "inc/telnet.h"
#include <sys/socket.h>
#include <sys/un.h>
#include <signal.h>

int P_contr[MAX_CONTROL_SOCKS], P_contr_listen;

/*
 * Internal Variables
 */

static const char *const Version="cyclades-ser-cli " TSRDEV_VERSION " " TSRDEV_DATE;

/*
 * Internal Functions
 */

#ifndef HAVE_DAEMON
#include "inc/daemon.h"
#endif

static void		helpmsg (void);
static void		mkidmsg(char *pgname, char *device);

int main (int argc, char **argv)
{
	int			i;
	char *			device;
	char *			rasname;
	int			physport;
	int			ptyiosize;
	int			netiosize;
	int			retrydelay;
	int			retry, nretries;
	int			opt;
	int			retst;
	int			devmodem;
	int			closemode;
	int			baseport;
	struct sockaddr_un	control_addr;
	struct sigaction	act;
	struct stat		stat_buf;

	act.sa_handler = SIG_IGN;
	if(sigaction(SIGPIPE, &act, NULL))
		sysmessage(MSG_ERR, "Can't block SIGPIPE.\n");

	ptyiosize = DEV_DEFIOSZ;
	netiosize = SOCK_DEFIOSZ;
	retrydelay = RETRY_DELAY ;
	nretries = NUM_RETRIES;
	Nvt.servertype = SRV_RTELNET;
	devmodem = DEV_MODEM;
	closemode = CLOSE_HANG;
	baseport = 0;

	Console = FALSE;
	Foreground = FALSE;

	Pgname = argv[0];
	Debug = 0;

	while ((opt = getopt(argc,argv, "u:n:r:fi:st:m:c:p:d:xvhH")) != EOF) {
		switch (opt) {
		case 'u' :
			ptyiosize = atoi(optarg);
			if (ptyiosize > DEV_MAXIOSZ) {
				ptyiosize = DEV_MAXIOSZ;
			}
			break;
		case 'n' :
			netiosize = atoi(optarg);
			if (netiosize > SOCK_MAXIOSZ) {
				netiosize = SOCK_MAXIOSZ;
			}
			break;
		case 'r' :
			nretries = atoi(optarg);
			break;
		case 'f' :
			Foreground = TRUE;
			break;
		case 'i' :
			retrydelay = atoi(optarg) * 1000;
			break;
		case 's' :
			Nvt.servertype = SRV_SOCKET;
			if(!baseport)
				baseport = SOCKET_BASE;
			break;
		case 'm' :
			devmodem = atoi (optarg);
			break;
		case 'c' :
			closemode = atoi (optarg);
			break;
		case 'p' :
			baseport = atoi (optarg);
			break;
		case 'd' :
			Debug = atoi (optarg);
			break;
		case 'x' :
			Console = TRUE;
			Foreground = TRUE;
			break;
		case 'v' :
			printf("%s\n", Version);
			exit (E_NORMAL);
		case 'h':
		case 'H':
		default :
			helpmsg();
			exit(E_PARMINVAL);
		}
	}
	if(!baseport)
		baseport = RTELNET_BASE;

	argc -= optind;
	argv += optind;

	if (argc != 3 ) {
		helpmsg();
		exit (E_PARMINVAL);
	}

	device = argv[0];

	mkidmsg(Pgname, device);

	if(strncmp(device, "/dev/", 5) != 0)
		fprintf(stderr, "%s: Warning, device does not have a \"/dev\" prefix\n", Idmsg); 

	rasname = argv[1];

	physport = atoi (argv[2]);
		
	if (physport == 0) {
		if (Nvt.servertype == SRV_RTELNET) {
			baseport = RTELNET_STD;
		} else {
			fprintf(stderr,
		"%s: Physical port must be > 0 for socket service\n", Idmsg);
			exit (E_PARMINVAL);
		}
	}

	init_system();

/* Get socket and device addresses */

	if ((retst = dev_getaddr(device)) != E_NORMAL) {
		exit (retst);
	}

	if(Nvt.servertype == SRV_RTELNET)
	{
		P_contr_listen = socket(PF_UNIX, SOCK_STREAM, 0);
		if(P_contr_listen == -1)
		{
			sysmessage(MSG_ERR, "Can't create Unix socket.\n");
			exit(1);
		}
		control_addr.sun_family = AF_UNIX;
		snprintf(P_contrname, sizeof(P_contrname), "%s.control", device);
		P_contrname[sizeof(P_contrname) - 1] = '\0';
		if(!stat(P_contrname, &stat_buf))
		{
			sysmessage(MSG_WARNING, "Removing old control socket \"%s\".\n", P_contrname);
			unlink(P_contrname);
		}
		strcpy(control_addr.sun_path, P_contrname);
		if(bind(P_contr_listen, (struct sockaddr *)&control_addr
	  	, sizeof(control_addr)) || listen(P_contr_listen, 8))
		{
			sysmessage(MSG_ERR, "Can't bind Unix socket.\n");
			exit(1);
		}
		for(i = 0; i < MAX_CONTROL_SOCKS; i++)
			P_contr[i] = -1;
	}

	if ((retst = sock_getaddr(rasname, baseport, physport)) != E_NORMAL)
		exit (retst);


	retry = 0;

	if(!Foreground)
		daemon(0, 0);

	while (retry < nretries) {

		if (retry) {
			if (retrydelay) {
				sysdelay(retrydelay);
			}
			sysmessage(MSG_WARNING, "Trying again ... \n");
		}


		if ((retst = sock_link(netiosize)) != E_NORMAL) {
			if (retst != E_CONNECT) {
				doexit(retst);
			}
			retry++;
			continue;
		}

		retry = 0;

		tsr_init(netiosize, ptyiosize, devmodem, closemode);

/* Main scheduler */

		tsr_io();

		retry++;
	} 

	sysmessage (MSG_ERR, "Exiting ...\n");

	doexit (E_RETRYEND);

	/* Not Reached */
	return 0; /* gcc gives a warning otherwise */
}

static void
helpmsg (void)
{
	fprintf(stderr, "Usage: cyclades-ser-cli [options] devname rasname physport\n");
	fprintf(stderr, "\toptions:\n");
	fprintf(stderr, "\t\t[-h] [-v] [-x]\n");
	fprintf(stderr, "\t\t[-u ptyiosize]  [-n netiosize] [-i retrydelay]\n");
	fprintf(stderr, "\t\t[-r numretries] [-t devtype]   [-s servertype]\n");
	fprintf(stderr, "\t\t[-m devmodem]   [-c closemode] [-p startport]\n");
	fprintf(stderr, "\t\t[-d deblevel]\n");
}


static void
mkidmsg(char *pgname, char *device)
{	
	char *cp;
	if ((cp = strrchr (pgname, '/')) != (char *) NULL) {
		cp++;
	} else {
		cp = pgname;
	}
	Pgname = cp;
	sprintf(Idmsg, "%7s %s", cp, device);
}