xmr.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include <u.h>
  2. #include <libc.h>
  3. enum {
  4. Soh= 0x1,
  5. Eot= 0x4,
  6. Ack= 0x6,
  7. Nak= 0x15,
  8. Cancel= 0x18,
  9. };
  10. int notifyf(void*, char*);
  11. int readupto(uchar*, int);
  12. int receive(int, uchar);
  13. void send(int);
  14. int debug, dfd;
  15. void
  16. main(int argc, char **argv)
  17. {
  18. int fd;
  19. uchar seqno;
  20. ulong bytes;
  21. ARGBEGIN {
  22. case 'd':
  23. dfd = 2;
  24. debug = 1;
  25. break;
  26. } ARGEND
  27. if(argc != 1){
  28. fprint(2, "usage: xmr filename\n");
  29. exits("usage");
  30. }
  31. fd = open("/dev/consctl", OWRITE);
  32. if(fd >= 0)
  33. write(fd, "rawon", 5);
  34. fd = create(argv[0], ORDWR, 0666);
  35. if(fd < 0){
  36. perror("xmr: create");
  37. exits("create");
  38. }
  39. atnotify(notifyf, 1);
  40. send(Nak);
  41. /*
  42. * keep receiving till the other side gives up
  43. */
  44. bytes = 0;
  45. for(seqno = 1; ; seqno++){
  46. if(receive(fd, seqno) == -1)
  47. break;
  48. bytes += 128;
  49. }
  50. fprint(2, "xmr: received %ld bytes\n", bytes);
  51. exits(0);
  52. }
  53. void
  54. send(int byte)
  55. {
  56. uchar c;
  57. c = byte;
  58. if(write(1, &c, 1) != 1){
  59. fprint(2, "xmr: hungup\n");
  60. exits("hangup");
  61. }
  62. }
  63. int
  64. readupto(uchar *a, int len)
  65. {
  66. int n;
  67. int sofar;
  68. for(sofar = 0; sofar < len; sofar += n){
  69. n = read(0, a, len-sofar);
  70. if(n <= 0){
  71. send(Nak);
  72. return sofar;
  73. }
  74. if(*a == Eot || *a == Cancel)
  75. return sofar + n;
  76. a += n;
  77. }
  78. return sofar;
  79. }
  80. int
  81. receive(int fd, uchar seqno)
  82. {
  83. uchar buf[128+4];
  84. uchar sum;
  85. uchar *p;
  86. int n;
  87. int tries;
  88. int have;
  89. for(have = 0, tries = 0;; tries++){
  90. if(debug)
  91. fprint(dfd, "have == %d\n", have);
  92. if(tries > 10){
  93. fprint(2, "xmr: timed out\n");
  94. if(debug)
  95. close(dfd);
  96. exits("timeout");
  97. }
  98. /* try to gather up a block */
  99. alarm(15*1000);
  100. n = readupto(&buf[have], 132-have);
  101. alarm(0);
  102. have += n;
  103. if(have){
  104. switch(buf[0]){
  105. case Eot:
  106. send(Ack);
  107. return -1;
  108. case Cancel:
  109. fprint(2, "xmr: transfer aborted by sender\n");
  110. exits("cancel");
  111. }
  112. }
  113. if(have != 132)
  114. continue;
  115. /* checksum */
  116. for(p = buf, sum = 0; p < &buf[128+3]; p++)
  117. sum += *p;
  118. /* If invalid block, resynchronize */
  119. if(buf[0] != Soh || buf[2] != (255-buf[1]) || sum != buf[131]){
  120. if(debug){
  121. fprint(dfd, "resync %2.2ux %d %d %ux %ux\n", buf[0],
  122. buf[1], buf[2], sum, buf[131]);
  123. write(dfd, (char*)buf+3, 128);
  124. fprint(dfd, "\n");
  125. }
  126. p = memchr(buf+1, Soh, 131);
  127. if(p){
  128. have = 132-(p-buf);
  129. memmove(buf, p, have);
  130. } else
  131. have = 0;
  132. continue;
  133. }
  134. /* it's probably a real block, so dump it if there's an error */
  135. have = 0;
  136. /* if this is the last block, ack */
  137. if(buf[1] == seqno-1){
  138. tries = 0;
  139. send(Ack);
  140. }else if(buf[1] == seqno){
  141. if(debug)
  142. fprint(dfd, "Ack\n");
  143. send(Ack);
  144. if(write(fd, buf+3, 128) != 128){
  145. fprint(2, "xmr: abort, error writing file\n");
  146. exits("write");
  147. }
  148. return 0;
  149. } else {
  150. send(Nak);
  151. }
  152. }
  153. }
  154. int
  155. notifyf(void *a, char *msg)
  156. {
  157. USED(a);
  158. if(strcmp(msg, "alarm") == 0)
  159. return 1;
  160. return 0;
  161. }