123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- #include <stdio.h>
- #include <errno.h>
- #include "open62541.h"
- #define MIN_ARGS 3
- #define FAILURE 1
- #define CONNECTION_STRING "opc.tcp://localhost:4840"
- static UA_ByteString loadFile(const char *const path) {
- UA_ByteString fileContents = UA_BYTESTRING_NULL;
- if(path == NULL)
- return fileContents;
-
- FILE *fp = fopen(path, "rb");
- if(!fp) {
- errno = 0;
- return fileContents;
- }
-
- fseek(fp, 0, SEEK_END);
- fileContents.length = (size_t)ftell(fp);
- fileContents.data = (UA_Byte*)UA_malloc(fileContents.length * sizeof(UA_Byte));
- if(fileContents.data) {
- fseek(fp, 0, SEEK_SET);
- size_t read = fread(fileContents.data, sizeof(UA_Byte), fileContents.length, fp);
- if(read != fileContents.length)
- UA_ByteString_deleteMembers(&fileContents);
- } else {
- fileContents.length = 0;
- }
- fclose(fp);
- return fileContents;
- }
- static void cleanupClient(UA_Client* client, UA_ByteString* remoteCertificate) {
- UA_ByteString_delete(remoteCertificate);
- UA_Client_delete(client);
- }
- int main(int argc, char* argv[]) {
- UA_Client* client = NULL;
- UA_ByteString* remoteCertificate = NULL;
- UA_StatusCode retval = UA_STATUSCODE_GOOD;
- UA_ByteString* trustList = NULL;
- size_t trustListSize = 0;
- UA_ByteString* revocationList = NULL;
- size_t revocationListSize = 0;
-
- UA_EndpointDescription* endpointArray = NULL;
- size_t endpointArraySize = 0;
-
- UA_ByteString certificate = loadFile(argv[1]);
- UA_ByteString privateKey = loadFile(argv[2]);
- if(argc < MIN_ARGS) {
- UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
- "The Certificate and key is missing."
- "The required arguments are "
- "<client-certificate.der> <client-private-key.der> "
- "[<trustlist1.crl>, ...]");
- return FAILURE;
- }
-
- client = UA_Client_new(UA_ClientConfig_default);
- remoteCertificate = UA_ByteString_new();
- retval = UA_Client_getEndpoints(client, CONNECTION_STRING,
- &endpointArraySize, &endpointArray);
- if(retval != UA_STATUSCODE_GOOD) {
- UA_Array_delete(endpointArray, endpointArraySize,
- &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
- cleanupClient(client, remoteCertificate);
- return (int)retval;
- }
- printf("%i endpoints found\n", (int)endpointArraySize);
- for(size_t endPointCount = 0; endPointCount < endpointArraySize; endPointCount++) {
- printf("URL of endpoint %i is %.*s\n", (int)endPointCount,
- (int)endpointArray[endPointCount].endpointUrl.length,
- endpointArray[endPointCount].endpointUrl.data);
- if(endpointArray[endPointCount].securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT)
- UA_ByteString_copy(&endpointArray[endPointCount].serverCertificate, remoteCertificate);
- }
- if(UA_ByteString_equal(remoteCertificate, &UA_BYTESTRING_NULL)) {
- UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
- "Server does not support Security Mode of"
- " UA_MESSAGESECURITYMODE_SIGNANDENCRYPT");
- cleanupClient(client, remoteCertificate);
- return FAILURE;
- }
- UA_Array_delete(endpointArray, endpointArraySize,
- &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
-
- if(argc > MIN_ARGS) {
- trustListSize = (size_t)argc-MIN_ARGS;
- retval = UA_ByteString_allocBuffer(trustList, trustListSize);
- if(retval != UA_STATUSCODE_GOOD) {
- cleanupClient(client, remoteCertificate);
- return (int)retval;
- }
- for(size_t trustListCount = 0; trustListCount < trustListSize; trustListCount++) {
- trustList[trustListCount] = loadFile(argv[trustListCount+3]);
- }
- }
-
- client = UA_Client_secure_new(UA_ClientConfig_default,
- certificate, privateKey,
- remoteCertificate,
- trustList, trustListSize,
- revocationList, revocationListSize);
- if(client == NULL) {
- UA_ByteString_delete(remoteCertificate);
- return FAILURE;
- }
- UA_ByteString_deleteMembers(&certificate);
- UA_ByteString_deleteMembers(&privateKey);
- for(size_t deleteCount = 0; deleteCount < trustListSize; deleteCount++) {
- UA_ByteString_deleteMembers(&trustList[deleteCount]);
- }
- if(!client) {
- UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
- "Could not create the server config");
- cleanupClient(client, remoteCertificate);
- return FAILURE;
- }
-
- retval = UA_Client_connect(client, CONNECTION_STRING);
- if(retval != UA_STATUSCODE_GOOD) {
- cleanupClient(client, remoteCertificate);
- return (int)retval;
- }
- UA_Variant value;
- UA_Variant_init(&value);
-
- const UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
- retval = UA_Client_readValueAttribute(client, nodeId, &value);
- if(retval == UA_STATUSCODE_GOOD &&
- UA_Variant_hasScalarType(&value, &UA_TYPES[UA_TYPES_DATETIME])) {
- UA_DateTime raw_date = *(UA_DateTime *) value.data;
- UA_DateTimeStruct dts = UA_DateTime_toStruct(raw_date);
- UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "date is: %u-%u-%u %u:%u:%u.%03u\n",
- dts.day, dts.month, dts.year, dts.hour, dts.min, dts.sec, dts.milliSec);
- }
-
- UA_Variant_deleteMembers(&value);
- cleanupClient(client, remoteCertificate);
- return (int)retval;
- }
|