fuzz_tcp_message.cc 2.9 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_ServerConfig_setDefault(config);
  44. if(retval != UA_STATUSCODE_GOOD) {
  45. UA_Server_delete(server);
  46. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
  47. "Could not set the server config");
  48. return 0;
  49. }
  50. retval = UA_Server_run_startup(server);
  51. if(retval != UA_STATUSCODE_GOOD)
  52. return 0;
  53. pthread_t serverThread;
  54. int rc = pthread_create(&serverThread, NULL, serverLoop, (void *)server);
  55. if (rc){
  56. printf("ERROR; return code from pthread_create() is %d\n", rc);
  57. exit(-1);
  58. }
  59. int sockfd = 0;
  60. {
  61. // create a client and write to localhost TCP server
  62. if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  63. {
  64. printf("\n Error : Could not create socket \n");
  65. return 1;
  66. }
  67. struct sockaddr_in serv_addr;
  68. serv_addr.sin_family = AF_INET;
  69. serv_addr.sin_port = htons(4840);
  70. serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  71. if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) >= 0)
  72. {
  73. write(sockfd, data, size);
  74. }
  75. }
  76. running = false;
  77. void *status;
  78. pthread_join(serverThread, &status);
  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. }