1
0

pcap_netif.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2012-11-05 Bernard the first version
  9. * 2012-11-13 Bernard merge prife's patch for exclusive
  10. * access pcap driver.
  11. */
  12. #ifdef _TIME_T_DEFINED
  13. #undef _TIME_T_DEFINED
  14. #endif
  15. #ifdef _MSC_VER
  16. /*
  17. * we do not want the warnings about the old deprecated and unsecure CRT functions
  18. * since these examples can be compiled under *nix as well
  19. */
  20. #define _CRT_SECURE_NO_WARNINGS
  21. #endif
  22. #include <pcap.h>
  23. #include <rtthread.h>
  24. #include <netif/ethernetif.h>
  25. #define MAX_ADDR_LEN 6
  26. #define NETIF_DEVICE(netif) ((struct pcap_netif*)(netif))
  27. #define NETIF_PCAP(netif) (NETIF_DEVICE(netif)->tap)
  28. struct pcap_netif
  29. {
  30. /* inherit from ethernet device */
  31. struct eth_device parent;
  32. pcap_t *tap;
  33. /* interface address info. */
  34. rt_uint8_t dev_addr[MAX_ADDR_LEN]; /* hw address */
  35. };
  36. static struct pcap_netif pcap_netif_device;
  37. static struct rt_semaphore sem_lock;
  38. static rt_mailbox_t packet_mb = RT_NULL;
  39. static void pcap_thread_entry(void* parameter)
  40. {
  41. pcap_if_t *netif;
  42. pcap_t *tap;
  43. char errbuf[PCAP_ERRBUF_SIZE];
  44. struct pcap_pkthdr *header;
  45. const u_char *pkt_data;
  46. int res;
  47. netif = (pcap_if_t *) parameter;
  48. /* Open the adapter */
  49. if ((tap = pcap_open_live(netif->name,
  50. 65536, // portion of the packet to capture.
  51. 1, // promiscuous mode (nonzero means promiscuous)
  52. 1, // read timeout, 0 blocked, -1 no timeout
  53. errbuf )) == NULL)
  54. {
  55. rt_kprintf("Unable to open the adapter. %s is not supported by WinPcap\n", netif->name);
  56. return;
  57. }
  58. NETIF_PCAP(&pcap_netif_device) = tap;
  59. /* Read the packets */
  60. while (1)
  61. {
  62. struct eth_device* eth;
  63. struct pbuf *p;
  64. rt_enter_critical();
  65. res = pcap_next_ex(tap, &header, &pkt_data);
  66. rt_exit_critical();
  67. if (res == 0) continue;
  68. eth = (struct eth_device*) &pcap_netif_device;
  69. p = pbuf_alloc(PBUF_LINK, header->len, PBUF_RAM);
  70. pbuf_take(p, pkt_data, header->len);
  71. /* send to packet mailbox */
  72. rt_mb_send_wait(packet_mb, (rt_uint32_t)p, RT_WAITING_FOREVER);
  73. /* notify eth rx thread to receive packet */
  74. eth_device_ready(eth);
  75. }
  76. }
  77. static rt_err_t pcap_netif_init(rt_device_t dev)
  78. {
  79. rt_thread_t tid;
  80. pcap_if_t *alldevs;
  81. pcap_if_t *d;
  82. pcap_t *tap;
  83. int inum, i=0;
  84. char errbuf[PCAP_ERRBUF_SIZE];
  85. /* Retrieve the device list */
  86. if(pcap_findalldevs(&alldevs, errbuf) == -1)
  87. {
  88. rt_kprintf("Error in pcap_findalldevs: %s\n", errbuf);
  89. return -RT_ERROR;
  90. }
  91. /* Print the list */
  92. for(d = alldevs; d; d = d->next)
  93. {
  94. rt_kprintf("%d. %s", ++i, d->name);
  95. if (d->description)
  96. rt_kprintf(" (%s)\n", d->description);
  97. else
  98. rt_kprintf(" (No description available)\n");
  99. }
  100. if(i == 0)
  101. {
  102. rt_kprintf("\nNo interfaces found! Make sure WinPcap is installed.\n");
  103. return -RT_ERROR;
  104. }
  105. inum = 1;
  106. /* Jump to the selected adapter */
  107. for(d = alldevs, i = 0; i < inum-1 ;d = d->next, i++);
  108. {
  109. rt_kprintf("Select (%s) as network interface\n", d->description);
  110. packet_mb = rt_mb_create("pcap", 64, RT_IPC_FLAG_FIFO);
  111. tid = rt_thread_create("pcap", pcap_thread_entry, d,
  112. 2048, RT_THREAD_PRIORITY_MAX - 1, 10);
  113. if (tid != RT_NULL)
  114. {
  115. rt_thread_startup(tid);
  116. }
  117. rt_thread_delay(100);
  118. }
  119. pcap_freealldevs(alldevs);
  120. return RT_EOK;
  121. }
  122. static rt_err_t pcap_netif_open(rt_device_t dev, rt_uint16_t oflag)
  123. {
  124. return RT_EOK;
  125. }
  126. static rt_err_t pcap_netif_close(rt_device_t dev)
  127. {
  128. pcap_t *tap;
  129. tap = NETIF_PCAP(dev);
  130. pcap_close(tap);
  131. return RT_EOK;
  132. }
  133. static rt_size_t pcap_netif_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  134. {
  135. rt_set_errno(-RT_ENOSYS);
  136. return 0;
  137. }
  138. static rt_size_t pcap_netif_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  139. {
  140. rt_set_errno(-RT_ENOSYS);
  141. return 0;
  142. }
  143. static rt_err_t pcap_netif_control(rt_device_t dev, int cmd, void *args)
  144. {
  145. switch (cmd)
  146. {
  147. case NIOCTL_GADDR:
  148. /* get mac address */
  149. if (args) rt_memcpy(args, pcap_netif_device.dev_addr, 6);
  150. else return -RT_ERROR;
  151. break;
  152. default :
  153. break;
  154. }
  155. return RT_EOK;
  156. }
  157. rt_err_t pcap_netif_tx( rt_device_t dev, struct pbuf* p)
  158. {
  159. struct pbuf *q;
  160. rt_uint8_t *ptr;
  161. rt_uint8_t buf[2048];
  162. rt_err_t result = RT_EOK;
  163. pcap_t *tap;
  164. int res;
  165. tap = NETIF_PCAP(dev);
  166. /* lock EMAC device */
  167. rt_sem_take(&sem_lock, RT_WAITING_FOREVER);
  168. /* copy data to tx buffer */
  169. q = p;
  170. ptr = (rt_uint8_t*)buf;
  171. while (q)
  172. {
  173. memcpy(ptr, q->payload, q->len);
  174. ptr += q->len;
  175. q = q->next;
  176. }
  177. rt_enter_critical();
  178. res = pcap_sendpacket(tap, buf, p->tot_len);
  179. rt_exit_critical();
  180. if (res != 0)
  181. {
  182. rt_kprintf("Error sending the packet: \n", pcap_geterr(tap));
  183. result = -RT_ERROR;
  184. }
  185. /* unlock EMAC device */
  186. rt_sem_release(&sem_lock);
  187. return result;
  188. }
  189. struct pbuf *pcap_netif_rx(rt_device_t dev)
  190. {
  191. struct pbuf* p = RT_NULL;
  192. rt_mb_recv(packet_mb, (rt_uint32_t*)&p, 0);
  193. return p;
  194. }
  195. void pcap_netif_hw_init(void)
  196. {
  197. rt_sem_init(&sem_lock, "eth_lock", 1, RT_IPC_FLAG_FIFO);
  198. pcap_netif_device.dev_addr[0] = 0x00;
  199. pcap_netif_device.dev_addr[1] = 0x60;
  200. pcap_netif_device.dev_addr[2] = 0x37;
  201. /* set mac address: (only for test) */
  202. pcap_netif_device.dev_addr[3] = 0x12;
  203. pcap_netif_device.dev_addr[4] = 0x34;
  204. pcap_netif_device.dev_addr[5] = 0x56;
  205. pcap_netif_device.parent.parent.init = pcap_netif_init;
  206. pcap_netif_device.parent.parent.open = pcap_netif_open;
  207. pcap_netif_device.parent.parent.close = pcap_netif_close;
  208. pcap_netif_device.parent.parent.read = pcap_netif_read;
  209. pcap_netif_device.parent.parent.write = pcap_netif_write;
  210. pcap_netif_device.parent.parent.control = pcap_netif_control;
  211. pcap_netif_device.parent.parent.user_data = RT_NULL;
  212. pcap_netif_device.parent.eth_rx = pcap_netif_rx;
  213. pcap_netif_device.parent.eth_tx = pcap_netif_tx;
  214. eth_device_init(&(pcap_netif_device.parent), "e0");
  215. }
  216. #include <finsh.h>
  217. void list_pcap(void)
  218. {
  219. int i=0;
  220. pcap_if_t *alldevs;
  221. pcap_if_t *d;
  222. char errbuf[PCAP_ERRBUF_SIZE];
  223. /* Retrieve the device list */
  224. if(pcap_findalldevs(&alldevs, errbuf) == -1)
  225. {
  226. rt_kprintf("Error in pcap_findalldevs: %s\n", errbuf);
  227. return -RT_ERROR;
  228. }
  229. /* Print the list */
  230. for(d = alldevs; d; d = d->next)
  231. {
  232. rt_kprintf("%d. %s", ++i, d->name);
  233. if (d->description)
  234. rt_kprintf(" (%s)\n", d->description);
  235. else
  236. rt_kprintf(" (No description available)\n");
  237. }
  238. if(i == 0)
  239. {
  240. rt_kprintf("\nNo interfaces found! Make sure WinPcap is installed.\n");
  241. return -RT_ERROR;
  242. }
  243. pcap_freealldevs(alldevs);
  244. return ;
  245. }
  246. FINSH_FUNCTION_EXPORT(list_pcap, show host netif adapter);