File: dnlogin.c

package info (click to toggle)
dnprogs 2.43.2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,936 kB
  • ctags: 3,872
  • sloc: ansic: 24,686; cpp: 10,608; makefile: 769; sh: 551; awk: 13
file content (180 lines) | stat: -rw-r--r-- 3,691 bytes parent folder | download | duplicates (2)
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
/******************************************************************************
    (c) 2002-2005      P.J. Caulfield          patrick@debian.org

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    *******************************************************************************/

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <ctype.h>
#include <termios.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/fcntl.h>
#include <netdnet/dn.h>
#include <netdnet/dnetdb.h>
#include "dn_endian.h"
#include "dnlogin.h"

/* The global state */
int termfd = -1;
int exit_char = 035; /* Default to ^] */
int finished = 0;    /* terminate mainloop */

int debug = 0;
int char_timeout = 0;
int timeout_valid = 0;

static int mainloop(void)
{
	while (!finished)
	{
		char inbuf[1024];
		struct timeval tv;
		int res;
		int sockfd = found_getsockfd();

		tv.tv_usec = 0;
		tv.tv_sec = char_timeout;

		fd_set in_set;
		FD_ZERO(&in_set);
		FD_SET(termfd, &in_set);
		FD_SET(sockfd, &in_set);

		if ( (res=select(FD_SETSIZE, &in_set, NULL, NULL, timeout_valid?&tv:NULL)) < 0)
		{
			break;
		}

		if (res == 0 && timeout_valid && tv.tv_sec == 0 && tv.tv_usec == 0)
		{
			tty_timeout();
		}
		timeout_valid = 0;

		/* Read from keyboard */
		if (FD_ISSET(termfd, &in_set))
		{
			int len;

			if ( (len=read(termfd, inbuf, sizeof(inbuf))) <= 0)
			{
				perror("read tty");
				break;
			}
			tty_process_terminal(inbuf, len);
		}

		if (FD_ISSET(sockfd, &in_set))
			if (found_read() == -1)
				break;
	}
	write(termfd, "\n", 1);
	return 0;
}

static void set_exit_char(char *string)
{
	int newchar = 0;

	if (string[0] == '^')
	{
		newchar = toupper(string[1]) - '@';
	}
	else
		newchar = strtol(string, NULL, 0);	// Just a number

	// Make sure it's resaonable
	if (newchar > 0 && newchar < 256)
		exit_char = newchar;

}

static void usage(char *prog, FILE * f)
{
	fprintf(f, "\nUsage: %s [OPTIONS] node\n\n", prog);

	fprintf(f, "\nOptions:\n");
	fprintf(f, "  -? -h        display this help message\n");
	fprintf(f, "  -V           show version number\n");
	fprintf(f, "  -e <char>    set exit char\n");
	fprintf(f, "  -d <mask>    debug information\n");

	fprintf(f, "\n");
}


int main(int argc, char *argv[])
{
	int opt;

	// Deal with command-line arguments.
	opterr = 0;
	optind = 0;
	while ((opt = getopt(argc, argv, "?Vhd:te:")) != EOF)
	{
		switch (opt)
		{
		case 'h':
			usage(argv[0], stdout);
			exit(0);

		case '?':
			usage(argv[0], stderr);
			exit(0);

		case 'V':
			printf("\ndnlogin from dnprogs version %s\n\n", VERSION);
			exit(1);
			break;

		case 'e':
			set_exit_char(optarg);
			break;

		case 'd':
			debug = atoi(optarg);
			break;
		}
	}

	if (optind >= argc)
	{
		usage(argv[0], stderr);
		exit(2);
	}

	send_input = cterm_send_input;
	send_oob = cterm_send_oob;
	rahead_change = cterm_rahead_change;
	if (found_setup_link(argv[optind], DNOBJECT_CTERM, cterm_process_network) == 0)
	{
		if (tty_setup("/dev/fd/0", 1) == -1)
			if (tty_setup("/proc/self/fd/0", 1)) {
				perror("cannot connect to stdin\n");
			}
		mainloop();
		tty_setup(NULL, 0);
	}
	else
	{
		return 2;
	}

	return 0;
}