fuzz_tcp_message.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. UA_ServerConfig_setMinimal(config, 4840, NULL);
  37. // Enable the mDNS announce and response functionality
  38. config->discovery.mdnsEnable = true;
  39. config->discovery.mdns.mdnsServerName = UA_String_fromChars("Sample Multicast Server");
  40. UA_StatusCode retval = UA_ServerConfig_setDefault(config);
  41. if(retval != UA_STATUSCODE_GOOD) {
  42. UA_Server_delete(server);
  43. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
  44. "Could not set the server config");
  45. return 0;
  46. }
  47. retval = UA_Server_run_startup(server);
  48. if(retval != UA_STATUSCODE_GOOD)
  49. return 0;
  50. pthread_t serverThread;
  51. int rc = pthread_create(&serverThread, NULL, serverLoop, (void *)server);
  52. if (rc){
  53. printf("ERROR; return code from pthread_create() is %d\n", rc);
  54. exit(-1);
  55. }
  56. int sockfd = 0;
  57. {
  58. // create a client and write to localhost TCP server
  59. if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  60. {
  61. printf("\n Error : Could not create socket \n");
  62. return 1;
  63. }
  64. struct sockaddr_in serv_addr;
  65. serv_addr.sin_family = AF_INET;
  66. serv_addr.sin_port = htons(4840);
  67. serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  68. if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
  69. {
  70. printf("\n Error : Connect Failed \n");
  71. return 1;
  72. }
  73. write(sockfd, data, size);
  74. }
  75. running = false;
  76. void *status;
  77. pthread_join(serverThread, &status);
  78. UA_Server_run_iterate(server, true);
  79. close(sockfd);
  80. UA_Server_run_shutdown(server);
  81. UA_Server_delete(server);
  82. return 0;
  83. }