-
Notifications
You must be signed in to change notification settings - Fork 48
/
utils.c
154 lines (127 loc) · 3.53 KB
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* Copyright (c) 1996-2024, OPC Foundation. All rights reserved.
The source code in this file is covered under a dual-license scenario:
- RCL: for OPC Foundation members in good-standing
- GPL V2: everybody else
RCL license terms accompanied with this source code. See http://opcfoundation.org/License/RCL/1.00/
GNU General Public License as published by the Free Software Foundation;
version 2 of the License are accompanied with this source code. See http://opcfoundation.org/License/GPLv2
This source code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#include "utils.h"
//#include <opcua_statuscodes.h>
//#include <opcua_errorhandling.h>
/* uastack includes */
#include <opcua_timer.h>
#include <opcua_socket.h>
#include <opcua_list.h>
#include <opcua_memory.h>
#include <opcua_endpoint.h>
#include <opcua_core.h>
/* local platform includes */
#include <platform.h>
#include <log.h>
#include <opcua_p_socket.h>
#include "tlds-alpha-by-domain.h"
typedef struct _TLDStruct
{
char tlds[2000][100];
int nrTlds;
} TLDStruct;
static TLDStruct tld_struct;
OpcUa_StatusCode ualds_parse_url(char *szUrl, char **szScheme, char **szHostname, uint16_t *port, char **szPath)
{
char *szTmp = szUrl;
*szScheme = OpcUa_Null;
*szHostname = OpcUa_Null;
*port = 4840;
*szPath = OpcUa_Null;
*szScheme = szTmp;
while (*szTmp != 0 && *szTmp != ':') { szTmp++; }
if (*szTmp == 0) { return OpcUa_BadInvalidArgument; }
*szTmp = 0;
szTmp++;
if (*szTmp != '/') { return OpcUa_BadInvalidArgument; }
szTmp++;
if (*szTmp != '/') { return OpcUa_BadInvalidArgument; }
szTmp++;
*szHostname = szTmp;
if (*szTmp == '[')
{
while (*szTmp != 0 && *szTmp != ']') { szTmp++; }
}
while (*szTmp != 0 && *szTmp != ':' && *szTmp != '/') { szTmp++; }
if (*szTmp == 0) { return OpcUa_Good; }
if (*szTmp == ':')
{
char *szPort = OpcUa_Null;
*szTmp = 0;
szPort = ++szTmp;
*port = (uint16_t)strtol(szPort, OpcUa_Null, 10);
while (*szTmp != 0 && *szTmp != '/') { szTmp++; }
}
if (*szTmp == '/')
{
*szPath = szTmp;
}
// if szHostname is between square brackets, remove them (it is used for IPv6).
int szHostnameLen = strlen(*szHostname);
if (szHostnameLen >= 2 && (*szHostname)[0] == '[' && (*szHostname)[szHostnameLen-1] == ']')
{
(*szHostname)[szHostnameLen-1] = 0;
(*szHostname) += 1;
}
return OpcUa_Good;
}
int is_Host_IP4Address(const char* host)
{
int a1, b1, c1, d1;
if (sscanf(host, "%d.%d.%d.%d", &a1, &b1, &c1, &d1) != 4)
{
return -1;
}
if (a1 < 0 || a1 > 255)
{
return -1;
}
if (b1 < 0 || b1 > 255)
{
return -1;
}
if (c1 < 0 || c1 > 255)
{
return -1;
}
if (d1 < 0 || d1 > 255)
{
return -1;
}
return 0;
}
int isTLD(char* domain)
{
int i = 0;
for (i = 0; i < tld_struct.nrTlds; ++i)
{
if (ualds_platform_strcpy_insensitive(domain, tld_struct.tlds[i]) == 0)
{
return 0;
}
}
return 1;
}
void loadKnownTLD()
{
// load known Top Level Domains (TLD)
tld_struct.nrTlds = 0;
char * pch;
pch = strtok(tld_str, " ");
while (pch != NULL)
{
strcpy(tld_struct.tlds[tld_struct.nrTlds], ".");
strcat(tld_struct.tlds[tld_struct.nrTlds], pch);
tld_struct.nrTlds++;
pch = strtok(NULL, " ");
}
}