|
@@ -19,7 +19,7 @@
|
|
#include "testing_networklayers.h"
|
|
#include "testing_networklayers.h"
|
|
|
|
|
|
#define RECEIVE_BUFFER_SIZE 65535
|
|
#define RECEIVE_BUFFER_SIZE 65535
|
|
-
|
|
|
|
|
|
+#define SERVER_PORT 4840
|
|
|
|
|
|
volatile bool running = true;
|
|
volatile bool running = true;
|
|
|
|
|
|
@@ -27,7 +27,7 @@ static void *serverLoop(void *server_ptr) {
|
|
UA_Server *server = (UA_Server*) server_ptr;
|
|
UA_Server *server = (UA_Server*) server_ptr;
|
|
|
|
|
|
while (running) {
|
|
while (running) {
|
|
- UA_Server_run_iterate(server, true);
|
|
|
|
|
|
+ UA_Server_run_iterate(server, false);
|
|
}
|
|
}
|
|
return NULL;
|
|
return NULL;
|
|
}
|
|
}
|
|
@@ -39,21 +39,20 @@ static void *serverLoop(void *server_ptr) {
|
|
extern "C" int
|
|
extern "C" int
|
|
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
|
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
|
|
|
|
|
- if (!UA_memoryManager_setLimitFromLast4Bytes(data, size))
|
|
|
|
- return 0;
|
|
|
|
- size -= 4;
|
|
|
|
-
|
|
|
|
UA_Server *server = UA_Server_new();
|
|
UA_Server *server = UA_Server_new();
|
|
if(!server) {
|
|
if(!server) {
|
|
- UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
|
|
|
|
|
|
+ UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
|
|
"Could not create server instance using UA_Server_new");
|
|
"Could not create server instance using UA_Server_new");
|
|
- return 0;
|
|
|
|
|
|
+ return EXIT_FAILURE;
|
|
}
|
|
}
|
|
|
|
|
|
UA_ServerConfig *config = UA_Server_getConfig(server);
|
|
UA_ServerConfig *config = UA_Server_getConfig(server);
|
|
- if (UA_ServerConfig_setMinimal(config, 4840, NULL) != UA_STATUSCODE_GOOD) {
|
|
|
|
|
|
+ UA_StatusCode retval = UA_ServerConfig_setMinimal(config, SERVER_PORT, NULL);
|
|
|
|
+ if (retval != UA_STATUSCODE_GOOD) {
|
|
|
|
+ UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
|
|
|
|
+ "Could not create server instance using UA_Server_new. %s", UA_StatusCode_name(retval));
|
|
UA_Server_delete(server);
|
|
UA_Server_delete(server);
|
|
- return 0;
|
|
|
|
|
|
+ return EXIT_FAILURE;
|
|
}
|
|
}
|
|
|
|
|
|
// Enable the mDNS announce and response functionality
|
|
// Enable the mDNS announce and response functionality
|
|
@@ -61,9 +60,20 @@ LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
|
|
|
|
|
config->discovery.mdns.mdnsServerName = UA_String_fromChars("Sample Multicast Server");
|
|
config->discovery.mdns.mdnsServerName = UA_String_fromChars("Sample Multicast Server");
|
|
|
|
|
|
- UA_StatusCode retval = UA_Server_run_startup(server);
|
|
|
|
- if(retval != UA_STATUSCODE_GOOD)
|
|
|
|
- return 0;
|
|
|
|
|
|
+ retval = UA_Server_run_startup(server);
|
|
|
|
+ if(retval != UA_STATUSCODE_GOOD) {
|
|
|
|
+ UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
|
|
|
|
+ "Could not run UA_Server_run_startup. %s", UA_StatusCode_name(retval));
|
|
|
|
+ UA_Server_delete(server);
|
|
|
|
+ return EXIT_FAILURE;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (!UA_memoryManager_setLimitFromLast4Bytes(data, size)) {
|
|
|
|
+ UA_Server_run_shutdown(server);
|
|
|
|
+ UA_Server_delete(server);
|
|
|
|
+ return EXIT_SUCCESS;
|
|
|
|
+ }
|
|
|
|
+ size -= 4;
|
|
|
|
|
|
// Iterate once to initialize the TCP connection. Otherwise the connect below may come before the server is up.
|
|
// Iterate once to initialize the TCP connection. Otherwise the connect below may come before the server is up.
|
|
UA_Server_run_iterate(server, true);
|
|
UA_Server_run_iterate(server, true);
|
|
@@ -71,46 +81,61 @@ LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
|
pthread_t serverThread;
|
|
pthread_t serverThread;
|
|
int rc = pthread_create(&serverThread, NULL, serverLoop, (void *)server);
|
|
int rc = pthread_create(&serverThread, NULL, serverLoop, (void *)server);
|
|
if (rc){
|
|
if (rc){
|
|
- printf("ERROR; return code from pthread_create() is %d\n", rc);
|
|
|
|
- exit(-1);
|
|
|
|
|
|
+
|
|
|
|
+ UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
|
|
|
|
+ "return code from pthread_create() is %d", rc);
|
|
|
|
+
|
|
|
|
+ UA_Server_run_shutdown(server);
|
|
|
|
+ UA_Server_delete(server);
|
|
|
|
+ return -1;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ int retCode = EXIT_SUCCESS;
|
|
|
|
+
|
|
int sockfd = 0;
|
|
int sockfd = 0;
|
|
{
|
|
{
|
|
// create a client and write to localhost TCP server
|
|
// create a client and write to localhost TCP server
|
|
|
|
|
|
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
|
|
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
|
|
{
|
|
{
|
|
- printf("\n Error : Could not create socket \n");
|
|
|
|
- return 1;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- struct sockaddr_in serv_addr;
|
|
|
|
- serv_addr.sin_family = AF_INET;
|
|
|
|
- serv_addr.sin_port = htons(4840);
|
|
|
|
- serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
|
|
|
|
-
|
|
|
|
- int status = connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
|
|
|
|
- if( status >= 0)
|
|
|
|
- {
|
|
|
|
- write(sockfd, data, size);
|
|
|
|
|
|
+ UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_CLIENT,
|
|
|
|
+ "Could not create socket");
|
|
|
|
+ retCode = EXIT_FAILURE;
|
|
} else {
|
|
} else {
|
|
- printf("Could not connect to server");
|
|
|
|
- return 1;
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
+ struct sockaddr_in serv_addr;
|
|
|
|
+ serv_addr.sin_family = AF_INET;
|
|
|
|
+ serv_addr.sin_port = htons(SERVER_PORT);
|
|
|
|
+ serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
|
|
|
|
+
|
|
|
|
+ int status = connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
|
|
|
|
+ if (status >= 0) {
|
|
|
|
+ if (write(sockfd, data, size) != size) {
|
|
|
|
+ UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_CLIENT,
|
|
|
|
+ "Did not write %d bytes", size);
|
|
|
|
+ retCode = EXIT_FAILURE;
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_CLIENT,
|
|
|
|
+ "Could not connect to server: %s", strerror(errno));
|
|
|
|
+ retCode = EXIT_FAILURE;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
}
|
|
}
|
|
running = false;
|
|
running = false;
|
|
void *status;
|
|
void *status;
|
|
pthread_join(serverThread, &status);
|
|
pthread_join(serverThread, &status);
|
|
|
|
|
|
- // Process any remaining data
|
|
|
|
- UA_Server_run_iterate(server, true);
|
|
|
|
|
|
+ // Process any remaining data. Just repeat a few times to empty all the buffered bytes
|
|
|
|
+ for (size_t i=0; i<5; i++) {
|
|
|
|
+ UA_Server_run_iterate(server, false);
|
|
|
|
+ }
|
|
close(sockfd);
|
|
close(sockfd);
|
|
|
|
|
|
|
|
|
|
UA_Server_run_shutdown(server);
|
|
UA_Server_run_shutdown(server);
|
|
UA_Server_delete(server);
|
|
UA_Server_delete(server);
|
|
- return 0;
|
|
|
|
|
|
+
|
|
|
|
+ return retCode;
|
|
}
|
|
}
|