udpecho.c 1.2 KB

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