sendpack.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <pcap.h>
  4. #include "misc.h"
  5. void main(int argc, char **argv)
  6. {
  7. pcap_t *fp;
  8. char errbuf[PCAP_ERRBUF_SIZE];
  9. u_char packet[100];
  10. int i;
  11. /* Load Npcap and its functions. */
  12. if (!LoadNpcapDlls())
  13. {
  14. fprintf(stderr, "Couldn't load Npcap\n");
  15. exit(1);
  16. }
  17. /* Check the validity of the command line */
  18. if (argc != 2)
  19. {
  20. printf("usage: %s interface (e.g. 'rpcap://eth0')", argv[0]);
  21. return;
  22. }
  23. /* Open the output device */
  24. if ( (fp= pcap_open(argv[1], // name of the device
  25. 100, // portion of the packet to capture (only the first 100 bytes)
  26. PCAP_OPENFLAG_PROMISCUOUS, // promiscuous mode
  27. 1000, // read timeout
  28. NULL, // authentication on the remote machine
  29. errbuf // error buffer
  30. ) ) == NULL)
  31. {
  32. fprintf(stderr,"\nUnable to open the adapter. %s is not supported by Npcap\n", argv[1]);
  33. return;
  34. }
  35. i = 0;
  36. switch(pcap_datalink(fp))
  37. {
  38. case DLT_NULL:
  39. // Pretend IPv4
  40. packet[i++] = 2;
  41. packet[i++] = 0;
  42. packet[i++] = 0;
  43. packet[i++] = 0;
  44. break;
  45. case DLT_EN10MB:
  46. /* Supposing to be on ethernet, set mac destination to 1:1:1:1:1:1 */
  47. while (i < 6)
  48. packet[i++]=1;
  49. /* set mac source to 2:2:2:2:2:2 */
  50. while (i < 12)
  51. packet[i++]=2;
  52. break;
  53. default:
  54. fprintf(stderr, "\nError, unknown data-link type %u\n", pcap_datalink(fp));
  55. return 4;
  56. }
  57. /* Fill the rest of the packet */
  58. for(;i<100;i++)
  59. {
  60. packet[i]=(u_char)i;
  61. }
  62. /* Send down the packet */
  63. if (pcap_sendpacket(fp, packet, 100 /* size */) != 0)
  64. {
  65. fprintf(stderr,"\nError sending the packet: %s\n", pcap_geterr(fp));
  66. return;
  67. }
  68. return;
  69. }