plan9.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. /* Copyright © Coraid, Inc. 2006. All rights reserved. */
  10. #include <u.h>
  11. #include <libc.h>
  12. #include "cec.h"
  13. int fd = -1;
  14. int cfd = -1;
  15. int efd = -1;
  16. int
  17. netopen0(char *e)
  18. {
  19. char buf[128], ctl[13];
  20. int n;
  21. snprint(buf, sizeof buf, "%s/clone", e);
  22. if((efd = open(buf, ORDWR)) == -1)
  23. return -1;
  24. memset(ctl, 0, sizeof ctl);
  25. if(read(efd, ctl, sizeof ctl) < 0)
  26. return -1;
  27. n = atoi(ctl);
  28. snprint(buf, sizeof buf, "connect %d", Etype);
  29. if(write(efd, buf, strlen(buf)) != strlen(buf))
  30. return -1;
  31. snprint(buf, sizeof buf, "%s/%d/ctl", e, n);
  32. if((cfd = open(buf, ORDWR)) < 0)
  33. return -1;
  34. snprint(buf, sizeof buf, "nonblocking");
  35. if(write(cfd, buf, strlen(buf)) != strlen(buf))
  36. return -1;
  37. snprint(buf, sizeof buf, "%s/%d/data", e, n);
  38. fd = open(buf, ORDWR);
  39. return fd;
  40. }
  41. void
  42. netclose(void)
  43. {
  44. close(efd);
  45. close(cfd);
  46. close(fd);
  47. efd = -1;
  48. cfd = -1;
  49. fd = -1;
  50. }
  51. int
  52. netopen(char *e)
  53. {
  54. int r;
  55. if((r = netopen0(e)) >= 0)
  56. return r;
  57. perror("netopen");
  58. netclose();
  59. return -1;
  60. }
  61. /* what if len < netlen? */
  62. int
  63. netget(void *v, int len)
  64. {
  65. int l;
  66. l = read(fd, v, len);
  67. if(debug && l > 0){
  68. fprint(2, "read %d bytes\n", l);
  69. dump((uint8_t*)v, l);
  70. }
  71. if (l <= 0)
  72. return 0;
  73. return l;
  74. }
  75. int
  76. netsend(void *v, int len)
  77. {
  78. uint8_t *p;
  79. p = v;
  80. if (debug) {
  81. fprint(2, "sending %d bytes\n", len);
  82. dump(p, len);
  83. }
  84. if (len < 60)
  85. len = 60; /* mintu */
  86. return write(fd, p, len);
  87. }