You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /* source: xio-interface.c */
  2. /* Copyright Gerhard Rieger and contributors (see file CHANGES) */
  3. /* Published under the GNU General Public License V.2, see file COPYING */
  4. /* this file contains the source for opening addresses of raw socket type */
  5. #include "xiosysincludes.h"
  6. #if WITH_INTERFACE
  7. #include "xioopen.h"
  8. #include "xio-socket.h"
  9. #include "xio-interface.h"
  10. static
  11. int xioopen_interface(int argc, const char *argv[], struct opt *opts,
  12. int xioflags, xiofile_t *xfd, unsigned groups, int pf,
  13. int dummy2, int dummy3);
  14. const struct addrdesc xioaddr_interface= { "interface", 3, xioopen_interface, GROUP_FD|GROUP_SOCKET, PF_PACKET, 0, 0 HELP(":<interface>") };
  15. static
  16. int _xioopen_interface(const char *ifname,
  17. struct opt *opts, int xioflags, xiofile_t *xxfd,
  18. unsigned groups, int pf) {
  19. xiosingle_t *xfd = &xxfd->stream;
  20. union sockaddr_union us = {{0}};
  21. socklen_t uslen;
  22. int socktype = SOCK_RAW;
  23. unsigned int ifidx;
  24. bool needbind = false;
  25. char *bindstring = NULL;
  26. struct sockaddr_ll sall = { 0 };
  27. if (ifindex(ifname, &ifidx, -1) < 0) {
  28. Error1("unknown interface \"%s\"", ifname);
  29. ifidx = 0; /* desparate attempt to continue */
  30. }
  31. xfd->howtoend = END_SHUTDOWN;
  32. retropt_int(opts, OPT_SO_TYPE, &socktype);
  33. retropt_socket_pf(opts, &pf);
  34. /* ...res_opts[] */
  35. if (applyopts_single(xfd, opts, PH_INIT) < 0) return -1;
  36. applyopts(-1, opts, PH_INIT);
  37. xfd->salen = sizeof(xfd->peersa);
  38. if (pf == PF_UNSPEC) {
  39. pf = xfd->peersa.soa.sa_family;
  40. }
  41. xfd->dtype = XIODATA_RECVFROM_SKIPIP;
  42. if (retropt_string(opts, OPT_BIND, &bindstring)) {
  43. needbind = true;
  44. }
  45. /*!!! parse by ':' */
  46. us.ll.sll_family = pf;
  47. us.ll.sll_protocol = htons(ETH_P_ALL);
  48. us.ll.sll_ifindex = ifidx;
  49. uslen = sizeof(sall);
  50. needbind = true;
  51. xfd->peersa = (union sockaddr_union)us;
  52. return
  53. _xioopen_dgram_sendto(needbind?&us:NULL, uslen,
  54. opts, xioflags, xfd, groups, pf, socktype, 0);
  55. }
  56. static
  57. int xioopen_interface(int argc, const char *argv[], struct opt *opts,
  58. int xioflags, xiofile_t *xxfd, unsigned groups,
  59. int pf, int dummy2, int dummy3) {
  60. xiosingle_t *xfd = &xxfd->stream;
  61. int result;
  62. if (argc != 2) {
  63. Error2("%s: wrong number of parameters (%d instead of 1)",
  64. argv[0], argc-1);
  65. return STAT_NORETRY;
  66. }
  67. if ((result =
  68. _xioopen_interface(argv[1], opts, xioflags, xxfd, groups, pf))
  69. != STAT_OK) {
  70. return result;
  71. }
  72. xfd->dtype = XIOREAD_RECV|XIOWRITE_SENDTO;
  73. if (pf == PF_INET) {
  74. xfd->dtype |= XIOREAD_RECV_SKIPIP;
  75. }
  76. xfd->para.socket.la.soa.sa_family = xfd->peersa.soa.sa_family;
  77. _xio_openlate(xfd, opts);
  78. return STAT_OK;
  79. }
  80. #endif /* WITH_INTERFACE */