Guest User

Untitled

a guest
Oct 18th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <signal.h>
  6. #include <sys/socket.h>
  7. #include <net/if.h>
  8. #include <linux/if_ether.h>
  9. #include <linux/if_packet.h>
  10. #include <arpa/inet.h>
  11.  
  12. static int s = -1;
  13. static int s1 = -1;
  14.  
  15. void onexit(int signum)
  16. {
  17. (void)signum;
  18. printf("Exiting");
  19. close(s);
  20. close(s1);
  21. }
  22.  
  23. int main()
  24. {
  25. char buf[1600];
  26. ssize_t recv_size = -1;
  27. ssize_t send_size = 42;
  28.  
  29. int i = 0;
  30.  
  31. struct sockaddr_ll socket_address;
  32.  
  33. s = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  34.  
  35. if ((s == -1))
  36. {
  37. perror("Socket creation failed");
  38. exit (0);
  39. }
  40.  
  41. signal(SIGINT, onexit);
  42.  
  43. memset(&socket_address, 0, sizeof (socket_address));
  44. socket_address.sll_family = PF_PACKET;
  45. socket_address.sll_ifindex = if_nametoindex("veth0");
  46. socket_address.sll_protocol = htons(ETH_P_ALL);
  47.  
  48. i = bind(s, (struct sockaddr*)&socket_address,
  49. sizeof(socket_address));
  50. if (i == -1)
  51. {
  52. perror("Bind");
  53. exit (0);
  54. }
  55.  
  56.  
  57.  
  58. memset(&buf, 0, sizeof(buf));
  59. buf[0] = 1;
  60. buf[1] = 1;
  61. buf[2] = 1;
  62. buf[3] = 1;
  63. buf[4] = 1;
  64. buf[5] = 1;
  65. buf[6] = 4;
  66. buf[7] = 1;
  67. buf[8] = 3;
  68. buf[9] = 2;
  69. buf[10] = 18;
  70. buf[11] = 93;
  71. buf[12] = 8;
  72. buf[13] = 6;
  73. buf[14] = 0;
  74. buf[15] = 1;
  75. buf[16] = 8;
  76. buf[17] = 0;
  77. buf[18] = 6;
  78. buf[19] = 4;
  79. buf[20] = 0;
  80. buf[21] = 1;
  81. buf[22] = 54;
  82. buf[23] = 21;
  83. buf[24] = -3;
  84. buf[25] = 42;
  85. buf[26] = -18;
  86. buf[27] = -93;
  87. buf[28] = -64;
  88. buf[29] = -88;
  89. buf[30] = 8;
  90. buf[31] = 100;
  91. buf[32] = 0;
  92. buf[33] = 0;
  93. buf[34] = 0;
  94. buf[35] = 0;
  95. buf[36] = 0;
  96. buf[37] = 0;
  97. buf[38] = -40;
  98. buf[39] = 58;
  99. buf[40] = -44;
  100. buf[41] = 100;
  101.  
  102. send_size = send(s, &buf, send_size, 0);
  103. printf("send size = %ld\n", send_size);
  104. if (send_size == -1)
  105. {
  106. perror("Socket send");
  107. exit (0);
  108. }
  109.  
  110. recv_size = recv(s, &buf, sizeof(buf), 0);
  111. printf("recv size %ld\n", recv_size);
  112. if (recv_size == -1)
  113. {
  114. perror("Socket receive");
  115. exit (0);
  116. }
  117.  
  118. printf("\n");
  119. for(i=0; i < recv_size; i++)
  120. {
  121. printf("%02x ", buf[i]);
  122. }
  123. printf("\n");
  124.  
  125.  
  126.  
  127. return 0;
  128. }
Add Comment
Please, Sign In to add comment