plan9.c 1.4 KB

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