l2_packet_none.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * WPA Supplicant - Layer2 packet handling example with dummy functions
  3. * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. *
  8. * This file can be used as a starting point for layer2 packet implementation.
  9. */
  10. #include "includes.h"
  11. #include "common.h"
  12. #include "eloop.h"
  13. #include "l2_packet.h"
  14. struct l2_packet_data {
  15. char ifname[17];
  16. u8 own_addr[ETH_ALEN];
  17. void (*rx_callback)(void *ctx, const u8 *src_addr,
  18. const u8 *buf, size_t len);
  19. void *rx_callback_ctx;
  20. int l2_hdr; /* whether to include layer 2 (Ethernet) header data
  21. * buffers */
  22. int fd;
  23. };
  24. int l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr)
  25. {
  26. os_memcpy(addr, l2->own_addr, ETH_ALEN);
  27. return 0;
  28. }
  29. int l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto,
  30. const u8 *buf, size_t len)
  31. {
  32. if (l2 == NULL)
  33. return -1;
  34. /*
  35. * TODO: Send frame (may need different implementation depending on
  36. * whether l2->l2_hdr is set).
  37. */
  38. return 0;
  39. }
  40. static void l2_packet_receive(int sock, void *eloop_ctx, void *sock_ctx)
  41. {
  42. struct l2_packet_data *l2 = eloop_ctx;
  43. u8 buf[2300];
  44. int res;
  45. /* TODO: receive frame (e.g., recv() using sock */
  46. buf[0] = 0;
  47. res = 0;
  48. l2->rx_callback(l2->rx_callback_ctx, NULL /* TODO: src addr */,
  49. buf, res);
  50. }
  51. struct l2_packet_data * l2_packet_init(
  52. const char *ifname, const u8 *own_addr, unsigned short protocol,
  53. void (*rx_callback)(void *ctx, const u8 *src_addr,
  54. const u8 *buf, size_t len),
  55. void *rx_callback_ctx, int l2_hdr)
  56. {
  57. struct l2_packet_data *l2;
  58. l2 = os_zalloc(sizeof(struct l2_packet_data));
  59. if (l2 == NULL)
  60. return NULL;
  61. os_strlcpy(l2->ifname, ifname, sizeof(l2->ifname));
  62. l2->rx_callback = rx_callback;
  63. l2->rx_callback_ctx = rx_callback_ctx;
  64. l2->l2_hdr = l2_hdr;
  65. /*
  66. * TODO: open connection for receiving frames
  67. */
  68. l2->fd = -1;
  69. if (l2->fd >= 0)
  70. eloop_register_read_sock(l2->fd, l2_packet_receive, l2, NULL);
  71. return l2;
  72. }
  73. void l2_packet_deinit(struct l2_packet_data *l2)
  74. {
  75. if (l2 == NULL)
  76. return;
  77. if (l2->fd >= 0) {
  78. eloop_unregister_read_sock(l2->fd);
  79. /* TODO: close connection */
  80. }
  81. os_free(l2);
  82. }
  83. int l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len)
  84. {
  85. /* TODO: get interface IP address */
  86. return -1;
  87. }
  88. void l2_packet_notify_auth_start(struct l2_packet_data *l2)
  89. {
  90. /* This function can be left empty */
  91. }
  92. int l2_packet_set_packet_filter(struct l2_packet_data *l2,
  93. enum l2_packet_filter_type type)
  94. {
  95. return -1;
  96. }