1
1
/*
2
- Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
2
+ Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
3
3
4
4
This program is free software; you can redistribute it and/or modify
5
5
it under the terms of the GNU General Public License as published by
21
21
#include " ProcessInfo.hpp"
22
22
#include " OwnProcessInfo.hpp"
23
23
#include " signaldata/ProcessInfoRep.hpp"
24
- #include " EventLogger .hpp"
24
+ #include " BaseString .hpp"
25
25
#include " ndb_net.h"
26
26
#include " ndb_socket.h"
27
27
#include " NdbTCP.h"
28
28
29
- extern EventLogger * g_eventLogger;
30
-
31
29
/* Utility Functions */
32
30
31
+ static inline bool isValidUriSchemeChar (char c) {
32
+ return (c >= ' a' && c <= ' z' ) ||
33
+ (c >= ' 0' && c <= ' 9' ) ||
34
+ (c == ' +' ) || (c == ' .' ) || (c == ' -' );
35
+ }
36
+
37
+ static bool valid_URI_scheme (const char * s) {
38
+ while (*s) {
39
+ if (! isValidUriSchemeChar (*s)) return false ;
40
+ s++;
41
+ }
42
+ return true ;
43
+ }
44
+
33
45
static inline bool isUtf8CharMultibyte (char c) { // is any part of multi-byte char
34
46
return (c & 0x80 );
35
47
}
@@ -66,9 +78,10 @@ ProcessInfo::ProcessInfo()
66
78
67
79
void ProcessInfo::invalidate ()
68
80
{
69
- memset (connection_name , 0 , ConnectionNameLength );
81
+ memset (uri_path , 0 , UriPathLength );
70
82
memset (host_address, 0 , AddressStringLength);
71
83
memset (process_name, 0 , ProcessNameLength);
84
+ strcpy (uri_scheme, " ndb" );
72
85
node_id = 0 ;
73
86
process_id = 0 ;
74
87
angel_process_id = 0 ;
@@ -88,12 +101,11 @@ ProcessInfo * ProcessInfo::forNodeId(Uint16 nodeId)
88
101
return process;
89
102
/* Make a copy */
90
103
ProcessInfo * self = new ProcessInfo ();
91
- strncpy ( self->host_address , process-> host_address , AddressStringLength);
104
+ self->node_id = nodeId; // do not copy node id
92
105
strncpy (self->process_name , process->process_name , ProcessNameLength);
93
- self->node_id = nodeId; // do not copy node id or connection name
94
106
self->process_id = process->process_id ;
95
107
self->angel_process_id = process->angel_process_id ;
96
- self-> application_port = process-> application_port ;
108
+ /* Do not copy any of the fields that will be set from set_service_uri() */
97
109
return self;
98
110
}
99
111
@@ -105,6 +117,16 @@ void ProcessInfo::release(ProcessInfo *self)
105
117
delete self;
106
118
}
107
119
120
+ /* Check URI components for syntactic validity
121
+ */
122
+ bool ProcessInfo::isValidUri (const char *scheme, const char *path)
123
+ {
124
+ if (path && path[0 ] == ' /' && path[1 ] == ' /' )
125
+ return false ;
126
+ return valid_URI_scheme (scheme);
127
+ }
128
+
129
+
108
130
void ProcessInfo::setProcessName (const char * name) {
109
131
size_t len = truncateUtf8 (name, ProcessNameLength);
110
132
strncpy (process_name, name, len);
@@ -120,20 +142,27 @@ int ProcessInfo::getPid() const {
120
142
return process_id;
121
143
}
122
144
123
- void ProcessInfo::setConnectionName (const char * name) {
124
- size_t len = truncateUtf8 (name, ConnectionNameLength);
125
- strncpy (connection_name, name, len);
126
- connection_name[len] = 0 ;
127
- g_eventLogger->info (" ProcessInfo set connection name: %s" , connection_name);
145
+ void ProcessInfo::setUriPath (const char * path) {
146
+ size_t len = truncateUtf8 (path, UriPathLength);
147
+ strncpy (uri_path, path, len);
148
+ uri_path[len] = 0 ;
128
149
}
129
150
130
- void ProcessInfo::setConnectionName (Uint32 * signal_data) {
131
- memcpy (connection_name, signal_data, ConnectionNameLength);
151
+ void ProcessInfo::setUriPath (Uint32 * signal_data) {
152
+ memcpy (uri_path, signal_data, UriPathLength);
153
+ }
154
+
155
+ void ProcessInfo::setUriScheme (const char * scheme) {
156
+ if (scheme && scheme[0 ] && valid_URI_scheme (scheme)) {
157
+ strncpy (uri_scheme, scheme, UriSchemeLength);
158
+ uri_scheme[UriSchemeLength - 1 ] = ' \0 ' ;
159
+ }
132
160
}
133
161
134
162
void ProcessInfo::setHostAddress (const char * address_string) {
135
163
if (address_string) {
136
164
strncpy (host_address, address_string, AddressStringLength);
165
+ host_address[AddressStringLength-1 ] = ' \0 ' ;
137
166
}
138
167
}
139
168
@@ -146,6 +175,8 @@ void ProcessInfo::setHostAddress(const struct sockaddr * addr, socklen_t len) {
146
175
}
147
176
148
177
void ProcessInfo::setHostAddress (const struct in_addr * addr) {
178
+ /* If address passed in is a wildcard address, do not use it. */
179
+ if (addr->s_addr != htonl (INADDR_ANY))
149
180
Ndb_inet_ntop (AF_INET, addr, host_address, AddressStringLength);
150
181
}
151
182
@@ -155,20 +186,16 @@ void ProcessInfo::setAngelPid(Uint32 pid) {
155
186
156
187
void ProcessInfo::setPort (Uint16 port) {
157
188
application_port = port;
158
- g_eventLogger->info (" ProcessInfo set port: %d" , application_port);
159
189
}
160
190
161
191
void ProcessInfo::setNodeId (Uint16 nodeId) {
162
192
node_id = nodeId;
163
193
}
164
194
165
195
void ProcessInfo::initializeFromProcessInfoRep (ProcessInfoRep * signal) {
166
- g_eventLogger->info (" Received ProcessInfoRep. "
167
- " Node: %d, Port: %d, Name: %s, Pid: %d" ,
168
- signal->node_id , signal->application_port ,
169
- signal->process_name , signal->process_id );
170
196
if (isValid ()) invalidate ();
171
197
setProcessName ( (char *) signal->process_name );
198
+ setUriScheme ( (char *) signal->uri_scheme );
172
199
process_id = signal->process_id ;
173
200
angel_process_id = signal->angel_process_id ;
174
201
application_port = signal->application_port ;
@@ -177,13 +204,29 @@ void ProcessInfo::initializeFromProcessInfoRep(ProcessInfoRep * signal) {
177
204
178
205
void ProcessInfo::buildProcessInfoReport (ProcessInfoRep *signal) {
179
206
memcpy (signal->process_name , process_name, ProcessNameLength);
207
+ memcpy (signal->uri_scheme , uri_scheme, UriSchemeLength);
180
208
signal->node_id = node_id;
181
209
signal->process_id = process_id;
182
210
signal->angel_process_id = angel_process_id;
183
211
signal->application_port = application_port;
212
+ }
213
+
214
+ int ProcessInfo::getServiceUri (char * buffer, size_t buf_len) const {
215
+ int len;
184
216
185
- g_eventLogger->info (" Created ProcessInfoRep. "
186
- " Node: %d, Port: %d, Name: %s, Pid: %d" ,
187
- signal->node_id , signal->application_port ,
188
- signal->process_name , signal->process_id );
217
+ /* Path must begin with a single slash if authority was present. */
218
+ const char * path_prefix = " " ;
219
+ if (uri_path[0 ] != ' \0 ' && uri_path[0 ] != ' /' ) {
220
+ path_prefix = " /" ;
221
+ }
222
+
223
+ if (application_port > 0 ) {
224
+ len = BaseString::snprintf (buffer, buf_len, " %s://%s:%d%s%s" , uri_scheme,
225
+ host_address, application_port, path_prefix, uri_path);
226
+ } else {
227
+ len = BaseString::snprintf (buffer, buf_len, " %s://%s%s%s" , uri_scheme,
228
+ host_address, path_prefix, uri_path);
229
+ }
230
+ return len;
189
231
}
232
+
0 commit comments