fuzz_server.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright 2007 - 2022, Alan Antonuk and the rabbitmq-c contributors.
  2. // SPDX-License-Identifier: mit
  3. #include <arpa/inet.h>
  4. #include <errno.h>
  5. #include <netinet/in.h>
  6. #include <pthread.h>
  7. #include <stdint.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <sys/socket.h>
  12. #include <unistd.h>
  13. #include <rabbitmq-c/amqp.h>
  14. #include <rabbitmq-c/tcp_socket.h>
  15. struct Fuzzer {
  16. int socket;
  17. uint16_t port;
  18. pthread_t thread;
  19. uint64_t size;
  20. uint8_t *buffer;
  21. };
  22. typedef struct Fuzzer Fuzzer;
  23. #define PORT 5672
  24. #define kMinInputLength 9
  25. #define kMaxInputLength 1024
  26. void client(Fuzzer *fuzzer);
  27. void fuzzinit(Fuzzer *fuzzer) {
  28. struct sockaddr_in server_addr;
  29. int res;
  30. fuzzer->socket = socket(AF_INET, SOCK_STREAM, 0);
  31. if (fuzzer->socket == -1) {
  32. fprintf(stderr, "socket failed %s\n", strerror(errno));
  33. exit(1);
  34. }
  35. memset(&server_addr, 0, sizeof(server_addr));
  36. server_addr.sin_family = AF_INET;
  37. server_addr.sin_port = htons(fuzzer->port);
  38. server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  39. res = setsockopt(fuzzer->socket, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int));
  40. if (res) {
  41. fprintf(stderr, "setsockopt failed: %s\n", strerror(errno));
  42. exit(1);
  43. }
  44. res = bind(fuzzer->socket, (struct sockaddr *)&server_addr, sizeof(server_addr));
  45. if (res) {
  46. fprintf(stderr, "bind failed: %s\n", strerror(errno));
  47. exit(1);
  48. }
  49. res = listen(fuzzer->socket, 1);
  50. if (res) {
  51. fprintf(stderr, "listen failed: %s\n", strerror(errno));
  52. exit(1);
  53. }
  54. }
  55. void *Server(void *args) {
  56. Fuzzer *fuzzer = (Fuzzer *)args;
  57. int client;
  58. int res;
  59. char clientData[10240];
  60. client = accept(fuzzer->socket, NULL, NULL);
  61. if (client == -1) {
  62. fprintf(stderr, "accept failed: %s\n", strerror(errno));
  63. exit(1);
  64. }
  65. res = recv(client, clientData, sizeof(clientData), 0);
  66. if (res == -1) {
  67. fprintf(stderr, "recv failed: %s\n", strerror(errno));
  68. exit(1);
  69. }
  70. res = send(client, fuzzer->buffer, fuzzer->size, 0);
  71. if (res == -1) {
  72. fprintf(stderr, "send failed: %s\n", strerror(errno));
  73. exit(1);
  74. }
  75. res = shutdown(client, SHUT_RDWR);
  76. close(client);
  77. return NULL;
  78. }
  79. void clean(Fuzzer *fuzzer) {
  80. shutdown(fuzzer->socket, SHUT_RDWR);
  81. close(fuzzer->socket);
  82. free(fuzzer);
  83. }
  84. extern int LLVMFuzzerTestOneInput(const char *data, size_t size) {
  85. if (size < kMinInputLength || size > kMaxInputLength) {
  86. return 0;
  87. }
  88. Fuzzer *fuzzer = (Fuzzer *)malloc(sizeof(Fuzzer));
  89. fuzzer->port = PORT;
  90. fuzzinit(fuzzer);
  91. pthread_create(&fuzzer->thread, NULL, Server, fuzzer);
  92. client(fuzzer);
  93. pthread_join(fuzzer->thread, NULL);
  94. clean(fuzzer);
  95. return 0;
  96. }
  97. void client(Fuzzer *fuzzer) {
  98. char const *hostname;
  99. int status;
  100. amqp_socket_t *socket = NULL;
  101. amqp_connection_state_t conn;
  102. hostname = "127.0.0.1";
  103. conn = amqp_new_connection();
  104. socket = amqp_tcp_socket_new(conn);
  105. if (!socket) {
  106. exit(1);
  107. }
  108. status = amqp_socket_open(socket, hostname, fuzzer->port);
  109. if (status != AMQP_STATUS_OK) {
  110. int sav_errno = errno;
  111. fprintf(stderr, "amqp_socket_open failed: %s\n", amqp_error_string2(status));
  112. fprintf(stderr, "amqp_socket_open errno: %d: %s\n", sav_errno, strerror(sav_errno));
  113. exit(1);
  114. }
  115. amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest");
  116. amqp_destroy_connection(conn);
  117. }