Annotation of src/usr.bin/ssh/ttymodes.c, Revision 1.4
1.1 deraadt 1: /*
1.4 ! deraadt 2: * Author: Tatu Ylonen <ylo@cs.hut.fi>
! 3: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
! 4: * All rights reserved
! 5: * Created: Tue Mar 21 15:59:15 1995 ylo
! 6: * Encoding and decoding of terminal modes in a portable way.
! 7: * Much of the format is defined in ttymodes.h; it is included multiple times
! 8: * into this file with the appropriate macro definitions to generate the
! 9: * suitable code.
! 10: */
1.1 deraadt 11:
12: #include "includes.h"
1.4 ! deraadt 13: RCSID("$Id: ttymodes.c,v 1.3 1999/11/23 22:25:56 markus Exp $");
1.1 deraadt 14:
15: #include "packet.h"
16: #include "ssh.h"
17:
18: #define TTY_OP_END 0
1.3 markus 19: #define TTY_OP_ISPEED 192 /* int follows */
20: #define TTY_OP_OSPEED 193 /* int follows */
1.1 deraadt 21:
1.4 ! deraadt 22: /*
! 23: * Converts POSIX speed_t to a baud rate. The values of the
! 24: * constants for speed_t are not themselves portable.
! 25: */
1.3 markus 26: static int
27: speed_to_baud(speed_t speed)
1.1 deraadt 28: {
1.3 markus 29: switch (speed) {
30: case B0:
31: return 0;
32: case B50:
33: return 50;
34: case B75:
35: return 75;
36: case B110:
37: return 110;
38: case B134:
39: return 134;
40: case B150:
41: return 150;
42: case B200:
43: return 200;
44: case B300:
45: return 300;
46: case B600:
47: return 600;
48: case B1200:
49: return 1200;
50: case B1800:
51: return 1800;
52: case B2400:
53: return 2400;
54: case B4800:
55: return 4800;
56: case B9600:
57: return 9600;
1.1 deraadt 58:
59: #ifdef B19200
1.3 markus 60: case B19200:
61: return 19200;
1.1 deraadt 62: #else /* B19200 */
63: #ifdef EXTA
1.3 markus 64: case EXTA:
65: return 19200;
1.1 deraadt 66: #endif /* EXTA */
67: #endif /* B19200 */
68:
69: #ifdef B38400
1.3 markus 70: case B38400:
71: return 38400;
1.1 deraadt 72: #else /* B38400 */
73: #ifdef EXTB
1.3 markus 74: case EXTB:
75: return 38400;
1.1 deraadt 76: #endif /* EXTB */
77: #endif /* B38400 */
78:
79: #ifdef B7200
1.3 markus 80: case B7200:
81: return 7200;
1.1 deraadt 82: #endif /* B7200 */
83: #ifdef B14400
1.3 markus 84: case B14400:
85: return 14400;
1.1 deraadt 86: #endif /* B14400 */
87: #ifdef B28800
1.3 markus 88: case B28800:
89: return 28800;
1.1 deraadt 90: #endif /* B28800 */
91: #ifdef B57600
1.3 markus 92: case B57600:
93: return 57600;
1.1 deraadt 94: #endif /* B57600 */
95: #ifdef B76800
1.3 markus 96: case B76800:
97: return 76800;
1.1 deraadt 98: #endif /* B76800 */
99: #ifdef B115200
1.3 markus 100: case B115200:
101: return 115200;
1.1 deraadt 102: #endif /* B115200 */
103: #ifdef B230400
1.3 markus 104: case B230400:
105: return 230400;
1.1 deraadt 106: #endif /* B230400 */
1.3 markus 107: default:
108: return 9600;
109: }
1.1 deraadt 110: }
111:
1.4 ! deraadt 112: /*
! 113: * Converts a numeric baud rate to a POSIX speed_t.
! 114: */
1.3 markus 115: static speed_t
116: baud_to_speed(int baud)
1.1 deraadt 117: {
1.3 markus 118: switch (baud) {
119: case 0:
120: return B0;
121: case 50:
122: return B50;
123: case 75:
124: return B75;
125: case 110:
126: return B110;
127: case 134:
128: return B134;
129: case 150:
130: return B150;
131: case 200:
132: return B200;
133: case 300:
134: return B300;
135: case 600:
136: return B600;
137: case 1200:
138: return B1200;
139: case 1800:
140: return B1800;
141: case 2400:
142: return B2400;
143: case 4800:
144: return B4800;
145: case 9600:
146: return B9600;
1.1 deraadt 147:
148: #ifdef B19200
1.3 markus 149: case 19200:
150: return B19200;
1.1 deraadt 151: #else /* B19200 */
152: #ifdef EXTA
1.3 markus 153: case 19200:
154: return EXTA;
1.1 deraadt 155: #endif /* EXTA */
156: #endif /* B19200 */
157:
158: #ifdef B38400
1.3 markus 159: case 38400:
160: return B38400;
1.1 deraadt 161: #else /* B38400 */
162: #ifdef EXTB
1.3 markus 163: case 38400:
164: return EXTB;
1.1 deraadt 165: #endif /* EXTB */
166: #endif /* B38400 */
167:
168: #ifdef B7200
1.3 markus 169: case 7200:
170: return B7200;
1.1 deraadt 171: #endif /* B7200 */
172: #ifdef B14400
1.3 markus 173: case 14400:
174: return B14400;
1.1 deraadt 175: #endif /* B14400 */
176: #ifdef B28800
1.3 markus 177: case 28800:
178: return B28800;
1.1 deraadt 179: #endif /* B28800 */
180: #ifdef B57600
1.3 markus 181: case 57600:
182: return B57600;
1.1 deraadt 183: #endif /* B57600 */
184: #ifdef B76800
1.3 markus 185: case 76800:
186: return B76800;
1.1 deraadt 187: #endif /* B76800 */
188: #ifdef B115200
1.3 markus 189: case 115200:
190: return B115200;
1.1 deraadt 191: #endif /* B115200 */
192: #ifdef B230400
1.3 markus 193: case 230400:
194: return B230400;
1.1 deraadt 195: #endif /* B230400 */
1.3 markus 196: default:
197: return B9600;
198: }
1.1 deraadt 199: }
200:
1.4 ! deraadt 201: /*
! 202: * Encodes terminal modes for the terminal referenced by fd
! 203: * in a portable manner, and appends the modes to a packet
! 204: * being constructed.
! 205: */
1.3 markus 206: void
207: tty_make_modes(int fd)
1.1 deraadt 208: {
1.3 markus 209: struct termios tio;
210: int baud;
1.1 deraadt 211:
1.3 markus 212: /* Get the modes. */
213: if (tcgetattr(fd, &tio) < 0) {
214: packet_put_char(TTY_OP_END);
215: log("tcgetattr: %.100s", strerror(errno));
216: return;
217: }
218: /* Store input and output baud rates. */
219: baud = speed_to_baud(cfgetospeed(&tio));
220: packet_put_char(TTY_OP_OSPEED);
221: packet_put_int(baud);
222: baud = speed_to_baud(cfgetispeed(&tio));
223: packet_put_char(TTY_OP_ISPEED);
224: packet_put_int(baud);
1.1 deraadt 225:
1.3 markus 226: /* Store values of mode flags. */
1.1 deraadt 227: #define TTYCHAR(NAME, OP) \
228: packet_put_char(OP); packet_put_char(tio.c_cc[NAME]);
229: #define TTYMODE(NAME, FIELD, OP) \
230: packet_put_char(OP); packet_put_char((tio.FIELD & NAME) != 0);
231: #define SGTTYCHAR(NAME, OP)
232: #define SGTTYMODE(NAME, FIELD, OP)
233: #define SGTTYMODEN(NAME, FIELD, OP)
234:
235: #include "ttymodes.h"
236:
237: #undef TTYCHAR
238: #undef TTYMODE
239: #undef SGTTYCHAR
240: #undef SGTTYMODE
241: #undef SGTTYMODEN
242:
1.3 markus 243: /* Mark end of mode data. */
244: packet_put_char(TTY_OP_END);
1.1 deraadt 245: }
246:
1.4 ! deraadt 247: /*
! 248: * Decodes terminal modes for the terminal referenced by fd in a portable
! 249: * manner from a packet being read.
! 250: */
1.3 markus 251: void
252: tty_parse_modes(int fd, int *n_bytes_ptr)
1.1 deraadt 253: {
1.3 markus 254: struct termios tio;
255: int opcode, baud;
256: int n_bytes = 0;
257: int failure = 0;
258:
1.4 ! deraadt 259: /*
! 260: * Get old attributes for the terminal. We will modify these
! 261: * flags. I am hoping that if there are any machine-specific
! 262: * modes, they will initially have reasonable values.
! 263: */
1.3 markus 264: if (tcgetattr(fd, &tio) < 0)
265: failure = -1;
266:
267: for (;;) {
268: n_bytes += 1;
269: opcode = packet_get_char();
270: switch (opcode) {
271: case TTY_OP_END:
272: goto set;
273:
274: case TTY_OP_ISPEED:
275: n_bytes += 4;
276: baud = packet_get_int();
277: if (failure != -1 && cfsetispeed(&tio, baud_to_speed(baud)) < 0)
278: error("cfsetispeed failed for %d", baud);
279: break;
280:
281: case TTY_OP_OSPEED:
282: n_bytes += 4;
283: baud = packet_get_int();
284: if (failure != -1 && cfsetospeed(&tio, baud_to_speed(baud)) < 0)
285: error("cfsetospeed failed for %d", baud);
286: break;
1.1 deraadt 287:
288: #define TTYCHAR(NAME, OP) \
289: case OP: \
290: n_bytes += 1; \
291: tio.c_cc[NAME] = packet_get_char(); \
292: break;
293: #define TTYMODE(NAME, FIELD, OP) \
294: case OP: \
295: n_bytes += 1; \
296: if (packet_get_char()) \
297: tio.FIELD |= NAME; \
298: else \
299: tio.FIELD &= ~NAME; \
300: break;
301: #define SGTTYCHAR(NAME, OP)
302: #define SGTTYMODE(NAME, FIELD, OP)
303: #define SGTTYMODEN(NAME, FIELD, OP)
304:
305: #include "ttymodes.h"
306:
307: #undef TTYCHAR
308: #undef TTYMODE
309: #undef SGTTYCHAR
310: #undef SGTTYMODE
311: #undef SGTTYMODEN
312:
1.3 markus 313: default:
314: debug("Ignoring unsupported tty mode opcode %d (0x%x)",
315: opcode, opcode);
1.4 ! deraadt 316: /*
! 317: * Opcodes 0 to 127 are defined to have
! 318: * a one-byte argument.
! 319: */
1.3 markus 320: if (opcode >= 0 && opcode < 128) {
321: n_bytes += 1;
322: (void) packet_get_char();
323: break;
324: } else {
1.4 ! deraadt 325: /*
! 326: * Opcodes 128 to 159 are defined to have
! 327: * an integer argument.
! 328: */
1.3 markus 329: if (opcode >= 128 && opcode < 160) {
330: n_bytes += 4;
331: (void) packet_get_int();
332: break;
333: }
334: }
1.4 ! deraadt 335: /*
! 336: * It is a truly undefined opcode (160 to 255).
! 337: * We have no idea about its arguments. So we
! 338: * must stop parsing. Note that some data may be
! 339: * left in the packet; hopefully there is nothing
! 340: * more coming after the mode data.
! 341: */
1.3 markus 342: log("parse_tty_modes: unknown opcode %d", opcode);
343: packet_integrity_check(0, 1, SSH_CMSG_REQUEST_PTY);
344: goto set;
1.1 deraadt 345: }
346: }
347:
1.3 markus 348: set:
349: if (*n_bytes_ptr != n_bytes) {
350: *n_bytes_ptr = n_bytes;
351: return; /* Don't process bytes passed */
352: }
353: if (failure == -1)
354: return; /* Packet parsed ok but tty stuff failed */
355:
356: /* Set the new modes for the terminal. */
357: if (tcsetattr(fd, TCSANOW, &tio) < 0)
358: log("Setting tty modes failed: %.100s", strerror(errno));
359: return;
1.1 deraadt 360: }