telnet.h 5.8 KB

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