udpecho.c 827 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <ip.h>
  4. void
  5. usage(void)
  6. {
  7. fprint(2, "usage: %s [-x netmtpt]\n", argv0);
  8. exits("usage");
  9. }
  10. void
  11. main(int argc, char **argv)
  12. {
  13. int fd, cfd, n;
  14. char data[128];
  15. char devdir[40];
  16. char buf[4096];
  17. char net[32];
  18. char *p;
  19. setnetmtpt(net, sizeof(net), nil);
  20. ARGBEGIN{
  21. case 'x':
  22. p = ARGF();
  23. if(p == nil)
  24. usage();
  25. setnetmtpt(net, sizeof(net), p);
  26. break;
  27. }ARGEND;
  28. sprint(data, "%s/udp!*!echo", net);
  29. cfd = announce(data, devdir);
  30. if(cfd < 0)
  31. sysfatal("can't announce: %r");
  32. if(fprint(cfd, "headers") < 0)
  33. sysfatal("can't set header mode: %r");
  34. sprint(data, "%s/data", devdir);
  35. fd = open(data, ORDWR);
  36. if(fd < 0)
  37. sysfatal("open udp data");
  38. for(;;){
  39. n = read(fd, buf, sizeof(buf));
  40. if(n < 0)
  41. sysfatal("error reading: %r");
  42. write(fd, buf, n);
  43. }
  44. }