telnet.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. typedef struct Opt Opt;
  10. int debug;
  11. #define DPRINT if(debug)fprint
  12. enum
  13. {
  14. /* control characters */
  15. Se= 240, /* end subnegotiation */
  16. NOP= 241,
  17. Mark= 242, /* data mark */
  18. Break= 243,
  19. Interrupt= 244,
  20. Abort= 245, /* TENEX ^O */
  21. AreYouThere= 246,
  22. Erasechar= 247, /* erase last character */
  23. Eraseline= 248, /* erase line */
  24. GoAhead= 249, /* half duplex clear to send */
  25. Sb= 250, /* start subnegotiation */
  26. Will= 251,
  27. Wont= 252,
  28. Do= 253,
  29. Dont= 254,
  30. Iac= 255,
  31. /* options */
  32. Binary= 0,
  33. Echo,
  34. SGA,
  35. Stat,
  36. Timing,
  37. Det,
  38. Term,
  39. EOR,
  40. Uid,
  41. Outmark,
  42. Ttyloc,
  43. M3270,
  44. Padx3,
  45. Window,
  46. Speed,
  47. Flow,
  48. Line,
  49. Xloc,
  50. Extend,
  51. };
  52. struct Opt
  53. {
  54. char *name;
  55. int code;
  56. char noway;
  57. int (*change)(Biobuf*, int); /* routine for status change */
  58. int (*sub)(Biobuf*, uint8_t*, int n); /* routine for subnegotiation */
  59. char remote; /* remote value */
  60. char local; /* local value */
  61. };
  62. Opt opt[] =
  63. {
  64. [Binary] = { "binary", 0, 0, },
  65. [Echo] = { "echo", 1, 0, },
  66. [SGA] = { "suppress Go Ahead", 3, 0, },
  67. [Stat] = { "status", 5, 1, },
  68. [Timing] = { "timing", 6, 1, },
  69. [Det] = { "det", 20, 1, },
  70. [Term] = { "terminal", 24, 0, },
  71. [EOR] = { "end of record", 25, 1, },
  72. [Uid] = { "uid", 26, 1, },
  73. [Outmark] = { "outmark", 27, 1, },
  74. [Ttyloc] = { "ttyloc", 28, 1, },
  75. [M3270] = { "3270 mode", 29, 1, },
  76. [Padx3] = { "pad x.3", 30, 1, },
  77. [Window] = { "window size", 31, 1, },
  78. [Speed] = { "speed", 32, 1, },
  79. [Flow] = { "flow control", 33, 1, },
  80. [Line] = { "line mode", 34, 1, },
  81. [Xloc] = { "X display loc", 35, 0, },
  82. [Extend] = { "Extended", 255, 1, },
  83. };
  84. int control(Biobuf*, int);
  85. Opt* findopt(int);
  86. int will(Biobuf*);
  87. int wont(Biobuf*);
  88. int doit(Biobuf*);
  89. int dont(Biobuf*);
  90. int sub(Biobuf*);
  91. int send2(int, int, int);
  92. int send3(int, int, int, int);
  93. int sendnote(int, char*);
  94. void fatal(char*, void*, void*);
  95. char* syserr(void);
  96. int wasintr(void);
  97. long iread(int, void*, int);
  98. long iwrite(int, void*, int);
  99. void binit(Biobuf*, int);
  100. void berase(Biobuf*);
  101. void bkill(Biobuf*);
  102. /*
  103. * parse telnet control messages
  104. */
  105. int
  106. control(Biobuf *bp, int c)
  107. {
  108. if(c < 0)
  109. return -1;
  110. switch(c){
  111. case AreYouThere:
  112. fprint(Bfildes(bp), "Plan 9 telnet, version 1\r\n");
  113. break;
  114. case Sb:
  115. return sub(bp);
  116. case Will:
  117. return will(bp);
  118. case Wont:
  119. return wont(bp);
  120. case Do:
  121. return doit(bp);
  122. case Dont:
  123. return dont(bp);
  124. case Se:
  125. fprint(2, "telnet: SE without an SB\n");
  126. break;
  127. default:
  128. break;
  129. }
  130. return 0;
  131. }
  132. Opt*
  133. findopt(int c)
  134. {
  135. Opt *o;
  136. for(o = opt; o <= &opt[Extend]; o++)
  137. if(o->code == c)
  138. return o;
  139. return 0;
  140. }
  141. int
  142. will(Biobuf *bp)
  143. {
  144. Opt *o;
  145. int c;
  146. int rv = 0;
  147. c = Bgetc(bp);
  148. if(c < 0)
  149. return -1;
  150. DPRINT(2, "will %d\n", c);
  151. o = findopt(c);
  152. if(o == 0){
  153. send3(Bfildes(bp), Iac, Dont, c);
  154. return 0;
  155. }
  156. if(o->noway)
  157. send3(Bfildes(bp), Iac, Dont, c);
  158. else if(o->remote == 0)
  159. rv |= send3(Bfildes(bp), Iac, Do, c);
  160. if(o->remote == 0){
  161. if(o->change)
  162. rv |= (*o->change)(bp, Will);
  163. }
  164. o->remote = 1;
  165. return rv;
  166. }
  167. int
  168. wont(Biobuf *bp)
  169. {
  170. Opt *o;
  171. int c;
  172. int rv = 0;
  173. c = Bgetc(bp);
  174. if(c < 0)
  175. return -1;
  176. DPRINT(2, "wont %d\n", c);
  177. o = findopt(c);
  178. if(o == 0)
  179. return 0;
  180. if(o->remote){
  181. if(o->change)
  182. rv |= (*o->change)(bp, Wont);
  183. rv |= send3(Bfildes(bp), Iac, Dont, c);
  184. }
  185. o->remote = 0;
  186. return rv;
  187. }
  188. int
  189. doit(Biobuf *bp)
  190. {
  191. Opt *o;
  192. int c;
  193. int rv = 0;
  194. c = Bgetc(bp);
  195. if(c < 0)
  196. return -1;
  197. DPRINT(2, "do %d\n", c);
  198. o = findopt(c);
  199. if(o == 0 || o->noway){
  200. send3(Bfildes(bp), Iac, Wont, c);
  201. return 0;
  202. }
  203. if(o->noway)
  204. return 0;
  205. if(o->local == 0){
  206. if(o->change)
  207. rv |= (*o->change)(bp, Do);
  208. rv |= send3(Bfildes(bp), Iac, Will, c);
  209. }
  210. o->local = 1;
  211. return rv;
  212. }
  213. int
  214. dont(Biobuf *bp)
  215. {
  216. Opt *o;
  217. int c;
  218. int rv = 0;
  219. c = Bgetc(bp);
  220. if(c < 0)
  221. return -1;
  222. DPRINT(2, "dont %d\n", c);
  223. o = findopt(c);
  224. if(o == 0)
  225. return 0;
  226. if(o->noway)
  227. return 0;
  228. if(o->local){
  229. o->local = 0;
  230. if(o->change)
  231. rv |= (*o->change)(bp, Dont);
  232. rv |= send3(Bfildes(bp), Iac, Wont, c);
  233. }
  234. o->local = 0;
  235. return rv;
  236. }
  237. /* read in a subnegotiation message and pass it to a routine for that option */
  238. int
  239. sub(Biobuf *bp)
  240. {
  241. uint8_t subneg[128];
  242. uint8_t *p;
  243. Opt *o;
  244. int c;
  245. p = subneg;
  246. for(;;){
  247. c = Bgetc(bp);
  248. if(c == Iac){
  249. c = Bgetc(bp);
  250. if(c == Se)
  251. break;
  252. if(p < &subneg[sizeof(subneg)])
  253. *p++ = Iac;
  254. }
  255. if(c < 0)
  256. return -1;
  257. if(p < &subneg[sizeof(subneg)])
  258. *p++ = c;
  259. }
  260. if(p == subneg)
  261. return 0;
  262. DPRINT(2, "sub %d %d n = %d\n", subneg[0], subneg[1], (int)(p - subneg - 1));
  263. o = findopt(subneg[0]);
  264. if(o == 0 || o->sub == 0)
  265. return 0;
  266. return (*o->sub)(bp, subneg+1, p - subneg - 1);
  267. }
  268. void
  269. sendd(int c0, int c1)
  270. {
  271. char *t = 0;
  272. switch(c0){
  273. case Will:
  274. t = "Will";
  275. break;
  276. case Wont:
  277. t = "Wont";
  278. break;
  279. case Do:
  280. t = "Do";
  281. break;
  282. case Dont:
  283. t = "Dont";
  284. break;
  285. }
  286. if(t)
  287. DPRINT(2, "r %s %d\n", t, c1);
  288. }
  289. int
  290. send2(int f, int c0, int c1)
  291. {
  292. uint8_t buf[2];
  293. buf[0] = c0;
  294. buf[1] = c1;
  295. return iwrite(f, buf, 2) == 2 ? 0 : -1;
  296. }
  297. int
  298. send3(int f, int c0, int c1, int c2)
  299. {
  300. uint8_t buf[3];
  301. buf[0] = c0;
  302. buf[1] = c1;
  303. buf[2] = c2;
  304. sendd(c1, c2);
  305. return iwrite(f, buf, 3) == 3 ? 0 : -1;
  306. }
  307. int
  308. sendnote(int pid, char *msg)
  309. {
  310. int fd;
  311. char name[128];
  312. sprint(name, "/proc/%d/note", pid);
  313. fd = open(name, OWRITE);
  314. if(fd < 0)
  315. return -1;
  316. if(write(fd, msg, strlen(msg))!=strlen(msg))
  317. return -1;
  318. return close(fd);
  319. }
  320. void
  321. fatal(char *fmt, void *a0, void *a1)
  322. {
  323. char buf[128];
  324. sprint(buf, fmt, a0, a1);
  325. fprint(2, "%s: %s\n", argv0, buf);
  326. exits(buf);
  327. }
  328. char*
  329. syserr(void)
  330. {
  331. static char err[ERRMAX];
  332. errstr(err, sizeof err);
  333. return err;
  334. }
  335. int
  336. wasintr(void)
  337. {
  338. return strcmp(syserr(), "interrupted") == 0;
  339. }
  340. long
  341. iread(int f, void *a, int n)
  342. {
  343. long m;
  344. for(;;){
  345. m = read(f, a, n);
  346. if(m >= 0 || !wasintr())
  347. break;
  348. }
  349. return m;
  350. }
  351. long
  352. iwrite(int f, void *a, int n)
  353. {
  354. long m;
  355. m = write(f, a, n);
  356. if(m < 0 && wasintr())
  357. return n;
  358. return m;
  359. }