net.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #include <u.h>
  10. #include <libc.h>
  11. #include <thread.h>
  12. #include <sunrpc.h>
  13. typedef struct Arg Arg;
  14. struct Arg
  15. {
  16. int fd;
  17. char adir[40];
  18. SunSrv *srv;
  19. };
  20. static void
  21. sunNetListen(void *v)
  22. {
  23. int fd, lcfd;
  24. char ldir[40];
  25. Arg *a = v;
  26. for(;;){
  27. lcfd = listen(a->adir, ldir);
  28. if(lcfd < 0)
  29. break;
  30. fd = accept(lcfd, ldir);
  31. close(lcfd);
  32. if(fd < 0)
  33. continue;
  34. if(!sunSrvFd(a->srv, fd))
  35. close(fd);
  36. }
  37. free(a);
  38. close(a->fd);
  39. }
  40. int
  41. sunSrvNet(SunSrv *srv, char *addr)
  42. {
  43. Arg *a;
  44. a = emalloc(sizeof(Arg));
  45. if((a->fd = announce(addr, a->adir)) < 0)
  46. return -1;
  47. a->srv = srv;
  48. proccreate(sunNetListen, a, SunStackSize);
  49. return 0;
  50. }
  51. int
  52. sunSrvAnnounce(SunSrv *srv, char *addr)
  53. {
  54. if(strstr(addr, "udp!"))
  55. return sunSrvUdp(srv, addr);
  56. else
  57. return sunSrvNet(srv, addr);
  58. }