pcap_netif.c 7.5 KB

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