fuzz_tcp_message.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #include "custom_memory_manager.h"
  5. #include <pthread.h>
  6. #include <open62541/plugin/log_stdout.h>
  7. #include <open62541/server_config_default.h>
  8. #include <open62541/types.h>
  9. #include "ua_server_internal.h"
  10. #include "testing_networklayers.h"
  11. #define RECEIVE_BUFFER_SIZE 65535
  12. volatile bool running = true;
  13. static void *serverLoop(void *server_ptr) {
  14. UA_Server *server = (UA_Server*) server_ptr;
  15. while (running) {
  16. UA_Server_run_iterate(server, true);
  17. }
  18. return NULL;
  19. }
  20. /*
  21. ** Main entry point. The fuzzer invokes this function with each
  22. ** fuzzed input.
  23. */
  24. extern "C" int
  25. LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  26. if (!UA_memoryManager_setLimitFromLast4Bytes(data, size))
  27. return 0;
  28. size -= 4;
  29. UA_Server *server = UA_Server_new();
  30. if(!server) {
  31. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
  32. "Could not create server instance using UA_Server_new");
  33. return 0;
  34. }
  35. UA_ServerConfig *config = UA_Server_getConfig(server);
  36. if (UA_ServerConfig_setMinimal(config, 4840, NULL) != UA_STATUSCODE_GOOD) {
  37. UA_Server_delete(server);
  38. return 0;
  39. }
  40. // Enable the mDNS announce and response functionality
  41. config->discovery.mdnsEnable = true;
  42. config->discovery.mdns.mdnsServerName = UA_String_fromChars("Sample Multicast Server");
  43. UA_StatusCode retval = UA_Server_run_startup(server);
  44. if(retval != UA_STATUSCODE_GOOD)
  45. return 0;
  46. // Iterate once to initialize the TCP connection. Otherwise the connect below may come before the server is up.
  47. UA_Server_run_iterate(server, true);
  48. pthread_t serverThread;
  49. int rc = pthread_create(&serverThread, NULL, serverLoop, (void *)server);
  50. if (rc){
  51. printf("ERROR; return code from pthread_create() is %d\n", rc);
  52. exit(-1);
  53. }
  54. int sockfd = 0;
  55. {
  56. // create a client and write to localhost TCP server
  57. if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  58. {
  59. printf("\n Error : Could not create socket \n");
  60. return 1;
  61. }
  62. struct sockaddr_in serv_addr;
  63. serv_addr.sin_family = AF_INET;
  64. serv_addr.sin_port = htons(4840);
  65. serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  66. int status = connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
  67. if( status >= 0)
  68. {
  69. write(sockfd, data, size);
  70. } else {
  71. printf("Could not connect to server");
  72. return 1;
  73. }
  74. }
  75. running = false;
  76. void *status;
  77. pthread_join(serverThread, &status);
  78. // Process any remaining data
  79. UA_Server_run_iterate(server, true);
  80. close(sockfd);
  81. UA_Server_run_shutdown(server);
  82. UA_Server_delete(server);
  83. return 0;
  84. }