pcap_netif.c 6.8 KB

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