pcap_netif.c 7.2 KB

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