l2_packet_none.c 2.6 KB

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