Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <arpa/inet.h>
  5. #include <errno.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <string.h>
  10.  
  11. int sz = 0;
  12.  
  13. char* makeq(char* host, int id) {
  14. static char query[2048];
  15. memset(query,(char) 0, sizeof(query));
  16. query[0] = id;
  17. query[1] = id;
  18. query[2] = 1;
  19. query[5] = 1;
  20. strcat(host, ".");
  21. int j = 0, qptr = 12;
  22. for (int i = 0; i < strlen(host); i++) {
  23. if (host[i] == '.') {
  24. query[qptr++] = i - j;
  25. for (int q = j; q < i; q++){
  26. query[qptr++] = host[q];
  27. }
  28. j = i + 1;
  29. }
  30. }
  31. query[qptr + 2] = 1;
  32. query[qptr + 4] = 1;
  33. sz = qptr + 5;
  34. return query;
  35. }
  36.  
  37. int main(int argc, char* argv[]) {
  38.  
  39. unsigned char buff[2048];
  40.  
  41. int sock = socket(AF_INET, SOCK_DGRAM, 0);
  42. struct sockaddr_in addr = {
  43. .sin_family = AF_INET,
  44. .sin_addr = inet_addr("8.8.8.8"),
  45. .sin_port = htons(53)
  46. };
  47. int id = 96;
  48. while(scanf("%s", buff) > 0) {
  49. id++;
  50. unsigned char* message = makeq(buff, id);
  51.  
  52. int x = sendto(sock,
  53. message, sz,
  54. 0,
  55. (const struct sockaddr*) &addr,
  56. sizeof(addr));
  57.  
  58. x = recvfrom(sock,
  59. message, 2048,
  60. 0, NULL, NULL);
  61. printf("%d.%d.%d.%d\n", message[sz + 12], message[sz + 13], message[sz + 14], message[sz + 15]);
  62. }
  63. close(sock);
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement