net.c 793 B

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