fuzz_tcp_message.cc 3.0 KB

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