FreeCalypso > hg > freecalypso-sw
annotate loadtools/ttypassthru.c @ 1013:6eee1e547778
fc-shell: arbitrary send command implemented in interactive mode
author | Mychaela Falconia <falcon@ivan.Harhan.ORG> |
---|---|
date | Sun, 20 Mar 2016 22:27:07 +0000 |
parents | dfd3110d84e3 |
children |
rev | line source |
---|---|
7
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
1 /* |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
2 * This module implements the pass-thru operation mode, in which |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
3 * the Unix host tty is cross-connected directly to the target |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
4 * running some code we have just loaded. |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
5 */ |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
6 |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
7 #include <sys/types.h> |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
8 #include <sys/ioctl.h> |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
9 #include <sys/errno.h> |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
10 #include <termios.h> |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
11 #include <stdio.h> |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
12 #include <stdlib.h> |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
13 #include <unistd.h> |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
14 #include <strings.h> |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
15 |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
16 extern int errno; |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
17 |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
18 extern int target_fd; |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
19 |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
20 static struct termios saved_termios, my_termios; |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
21 |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
22 static void |
124
dfd3110d84e3
fc-[ix]ram: tty pass-thru mode made binary-safe
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
10
diff
changeset
|
23 safe_output(buf, cc) |
dfd3110d84e3
fc-[ix]ram: tty pass-thru mode made binary-safe
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
10
diff
changeset
|
24 u_char *buf; |
dfd3110d84e3
fc-[ix]ram: tty pass-thru mode made binary-safe
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
10
diff
changeset
|
25 { |
dfd3110d84e3
fc-[ix]ram: tty pass-thru mode made binary-safe
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
10
diff
changeset
|
26 int i, c; |
dfd3110d84e3
fc-[ix]ram: tty pass-thru mode made binary-safe
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
10
diff
changeset
|
27 |
dfd3110d84e3
fc-[ix]ram: tty pass-thru mode made binary-safe
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
10
diff
changeset
|
28 for (i = 0; i < cc; i++) { |
dfd3110d84e3
fc-[ix]ram: tty pass-thru mode made binary-safe
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
10
diff
changeset
|
29 c = buf[i]; |
dfd3110d84e3
fc-[ix]ram: tty pass-thru mode made binary-safe
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
10
diff
changeset
|
30 if (c == '\r' || c == '\n' || c == '\t' || c == '\b') { |
dfd3110d84e3
fc-[ix]ram: tty pass-thru mode made binary-safe
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
10
diff
changeset
|
31 putchar(c); |
dfd3110d84e3
fc-[ix]ram: tty pass-thru mode made binary-safe
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
10
diff
changeset
|
32 continue; |
dfd3110d84e3
fc-[ix]ram: tty pass-thru mode made binary-safe
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
10
diff
changeset
|
33 } |
dfd3110d84e3
fc-[ix]ram: tty pass-thru mode made binary-safe
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
10
diff
changeset
|
34 if (c & 0x80) { |
dfd3110d84e3
fc-[ix]ram: tty pass-thru mode made binary-safe
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
10
diff
changeset
|
35 putchar('M'); |
dfd3110d84e3
fc-[ix]ram: tty pass-thru mode made binary-safe
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
10
diff
changeset
|
36 putchar('-'); |
dfd3110d84e3
fc-[ix]ram: tty pass-thru mode made binary-safe
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
10
diff
changeset
|
37 c &= 0x7F; |
dfd3110d84e3
fc-[ix]ram: tty pass-thru mode made binary-safe
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
10
diff
changeset
|
38 } |
dfd3110d84e3
fc-[ix]ram: tty pass-thru mode made binary-safe
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
10
diff
changeset
|
39 if (c < 0x20) { |
dfd3110d84e3
fc-[ix]ram: tty pass-thru mode made binary-safe
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
10
diff
changeset
|
40 putchar('^'); |
dfd3110d84e3
fc-[ix]ram: tty pass-thru mode made binary-safe
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
10
diff
changeset
|
41 putchar(c + '@'); |
dfd3110d84e3
fc-[ix]ram: tty pass-thru mode made binary-safe
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
10
diff
changeset
|
42 } else if (c == 0x7F) { |
dfd3110d84e3
fc-[ix]ram: tty pass-thru mode made binary-safe
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
10
diff
changeset
|
43 putchar('^'); |
dfd3110d84e3
fc-[ix]ram: tty pass-thru mode made binary-safe
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
10
diff
changeset
|
44 putchar('?'); |
dfd3110d84e3
fc-[ix]ram: tty pass-thru mode made binary-safe
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
10
diff
changeset
|
45 } else |
dfd3110d84e3
fc-[ix]ram: tty pass-thru mode made binary-safe
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
10
diff
changeset
|
46 putchar(c); |
dfd3110d84e3
fc-[ix]ram: tty pass-thru mode made binary-safe
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
10
diff
changeset
|
47 } |
dfd3110d84e3
fc-[ix]ram: tty pass-thru mode made binary-safe
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
10
diff
changeset
|
48 fflush(stdout); |
dfd3110d84e3
fc-[ix]ram: tty pass-thru mode made binary-safe
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
10
diff
changeset
|
49 } |
dfd3110d84e3
fc-[ix]ram: tty pass-thru mode made binary-safe
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
10
diff
changeset
|
50 |
dfd3110d84e3
fc-[ix]ram: tty pass-thru mode made binary-safe
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
10
diff
changeset
|
51 static void |
7
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
52 loop() |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
53 { |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
54 char buf[BUFSIZ]; |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
55 fd_set fds, fds1; |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
56 register int i, cc, max; |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
57 |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
58 FD_ZERO(&fds); |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
59 FD_SET(0, &fds); |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
60 FD_SET(target_fd, &fds); |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
61 max = target_fd + 1; |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
62 for (;;) { |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
63 bcopy(&fds, &fds1, sizeof(fd_set)); |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
64 i = select(max, &fds1, NULL, NULL, NULL); |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
65 if (i < 0) { |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
66 if (errno == EINTR) |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
67 continue; |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
68 tcsetattr(0, TCSAFLUSH, &saved_termios); |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
69 perror("select"); |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
70 exit(1); |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
71 } |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
72 if (FD_ISSET(0, &fds1)) { |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
73 cc = read(0, buf, sizeof buf); |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
74 if (cc <= 0) |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
75 return; |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
76 if (cc == 1 && buf[0] == 0x1C) |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
77 return; |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
78 write(target_fd, buf, cc); |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
79 } |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
80 if (FD_ISSET(target_fd, &fds1)) { |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
81 cc = read(target_fd, buf, sizeof buf); |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
82 if (cc <= 0) { |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
83 tcsetattr(0, TCSAFLUSH, &saved_termios); |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
84 fprintf(stderr, "EOF/error on target tty\n"); |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
85 exit(1); |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
86 } |
124
dfd3110d84e3
fc-[ix]ram: tty pass-thru mode made binary-safe
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
10
diff
changeset
|
87 safe_output(buf, cc); |
7
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
88 } |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
89 } |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
90 } |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
91 |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
92 tty_passthru() |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
93 { |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
94 static int zero = 0; |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
95 |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
96 ioctl(target_fd, FIONBIO, &zero); |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
97 |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
98 tcgetattr(0, &saved_termios); |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
99 bcopy(&saved_termios, &my_termios, sizeof(struct termios)); |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
100 cfmakeraw(&my_termios); |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
101 my_termios.c_cc[VMIN] = 1; |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
102 my_termios.c_cc[VTIME] = 0; |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
103 tcsetattr(0, TCSAFLUSH, &my_termios); |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
104 |
10
e2e80a09338e
fc-sertool actually works; output from loadagent is visible!
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
105 printf("Entering tty pass-thru; type ^\\ to exit\r\n\n"); |
7
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
106 loop(); |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
107 tcsetattr(0, TCSAFLUSH, &saved_termios); |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
108 return 0; |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
109 } |