Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
L
libzmq
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
libzmq
Commits
abca9f6b
Commit
abca9f6b
authored
May 28, 2018
by
Simon Giesecke
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Problem: Magic numbers in plain_server.cpp
Solution: introduced constants
parent
2da6629e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
17 deletions
+37
-17
plain_server.cpp
src/plain_server.cpp
+37
-17
No files found.
src/plain_server.cpp
View file @
abca9f6b
...
...
@@ -118,17 +118,21 @@ int zmq::plain_server_t::process_hello (msg_t *msg_)
if
(
rc
==
-
1
)
return
-
1
;
const
unsigned
char
*
ptr
=
static_cast
<
unsigned
char
*>
(
msg_
->
data
());
const
char
*
ptr
=
static_cast
<
char
*>
(
msg_
->
data
());
size_t
bytes_left
=
msg_
->
size
();
if
(
bytes_left
<
6
||
memcmp
(
ptr
,
"
\x05
HELLO"
,
6
))
{
const
char
hello_prefix
[]
=
"
\x05
HELLO"
;
const
size_t
hello_prefix_len
=
sizeof
(
hello_prefix
)
-
1
;
if
(
bytes_left
<
hello_prefix_len
||
memcmp
(
ptr
,
hello_prefix
,
hello_prefix_len
))
{
session
->
get_socket
()
->
event_handshake_failed_protocol
(
session
->
get_endpoint
(),
ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND
);
errno
=
EPROTO
;
return
-
1
;
}
ptr
+=
6
;
bytes_left
-=
6
;
ptr
+=
hello_prefix_len
;
bytes_left
-=
hello_prefix_len
;
if
(
bytes_left
<
1
)
{
// PLAIN I: invalid PLAIN client, did not send username
...
...
@@ -149,7 +153,7 @@ int zmq::plain_server_t::process_hello (msg_t *msg_)
errno
=
EPROTO
;
return
-
1
;
}
const
std
::
string
username
=
std
::
string
(
(
char
*
)
ptr
,
username_length
);
const
std
::
string
username
=
std
::
string
(
ptr
,
username_length
);
ptr
+=
username_length
;
bytes_left
-=
username_length
;
if
(
bytes_left
<
1
)
{
...
...
@@ -172,7 +176,7 @@ int zmq::plain_server_t::process_hello (msg_t *msg_)
return
-
1
;
}
const
std
::
string
password
=
std
::
string
(
(
char
*
)
ptr
,
password_length
);
const
std
::
string
password
=
std
::
string
(
ptr
,
password_length
);
ptr
+=
password_length
;
bytes_left
-=
password_length
;
if
(
bytes_left
>
0
)
{
...
...
@@ -204,9 +208,11 @@ int zmq::plain_server_t::process_hello (msg_t *msg_)
int
zmq
::
plain_server_t
::
produce_welcome
(
msg_t
*
msg_
)
const
{
const
int
rc
=
msg_
->
init_size
(
8
);
const
char
welcome_prefix
[]
=
"
\x07
WELCOME"
;
const
size_t
welcome_prefix_len
=
sizeof
(
welcome_prefix
)
-
1
;
const
int
rc
=
msg_
->
init_size
(
welcome_prefix_len
);
errno_assert
(
rc
==
0
);
memcpy
(
msg_
->
data
(),
"
\x07
WELCOME"
,
8
);
memcpy
(
msg_
->
data
(),
welcome_prefix
,
welcome_prefix_len
);
return
0
;
}
...
...
@@ -215,13 +221,17 @@ int zmq::plain_server_t::process_initiate (msg_t *msg_)
const
unsigned
char
*
ptr
=
static_cast
<
unsigned
char
*>
(
msg_
->
data
());
const
size_t
bytes_left
=
msg_
->
size
();
if
(
bytes_left
<
9
||
memcmp
(
ptr
,
"
\x08
INITIATE"
,
9
))
{
const
char
initiate_prefix
[]
=
"
\x08
INITIATE"
;
const
size_t
initiate_prefix_len
=
sizeof
(
initiate_prefix
)
-
1
;
if
(
bytes_left
<
initiate_prefix_len
||
memcmp
(
ptr
,
initiate_prefix
,
initiate_prefix_len
))
{
session
->
get_socket
()
->
event_handshake_failed_protocol
(
session
->
get_endpoint
(),
ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND
);
errno
=
EPROTO
;
return
-
1
;
}
const
int
rc
=
parse_metadata
(
ptr
+
9
,
bytes_left
-
9
);
const
int
rc
=
parse_metadata
(
ptr
+
initiate_prefix_len
,
bytes_left
-
initiate_prefix_len
);
if
(
rc
==
0
)
state
=
sending_ready
;
return
rc
;
...
...
@@ -229,7 +239,9 @@ int zmq::plain_server_t::process_initiate (msg_t *msg_)
int
zmq
::
plain_server_t
::
produce_ready
(
msg_t
*
msg_
)
const
{
make_command_with_basic_properties
(
msg_
,
"
\5
READY"
,
6
);
const
char
ready_prefix
[]
=
"
\5
READY"
;
const
size_t
ready_prefix_len
=
sizeof
(
ready_prefix
)
-
1
;
make_command_with_basic_properties
(
msg_
,
ready_prefix
,
ready_prefix_len
);
return
0
;
}
...
...
@@ -237,12 +249,18 @@ int zmq::plain_server_t::produce_ready (msg_t *msg_) const
int
zmq
::
plain_server_t
::
produce_error
(
msg_t
*
msg_
)
const
{
zmq_assert
(
status_code
.
length
()
==
3
);
const
int
rc
=
msg_
->
init_size
(
6
+
1
+
status_code
.
length
());
const
char
error_prefix
[]
=
"
\5
ERROR"
;
const
size_t
error_prefix_len
=
sizeof
(
error_prefix
)
-
1
;
const
char
status_code_len
=
static_cast
<
char
>
(
status_code
.
length
());
const
size_t
status_code_len_size
=
sizeof
(
status_code_len
);
const
int
rc
=
msg_
->
init_size
(
error_prefix_len
+
status_code_len_size
+
status_code_len
);
zmq_assert
(
rc
==
0
);
char
*
msg_data
=
static_cast
<
char
*>
(
msg_
->
data
());
memcpy
(
msg_data
,
"
\5
ERROR"
,
6
);
msg_data
[
6
]
=
static_cast
<
char
>
(
status_code
.
length
());
memcpy
(
msg_data
+
7
,
status_code
.
c_str
(),
status_code
.
length
());
memcpy
(
msg_data
,
error_prefix
,
error_prefix_len
);
msg_data
[
error_prefix_len
]
=
status_code_len
;
memcpy
(
msg_data
+
error_prefix_len
+
status_code_len_size
,
status_code
.
c_str
(),
status_code
.
length
());
return
0
;
}
...
...
@@ -253,6 +271,8 @@ void zmq::plain_server_t::send_zap_request (const std::string &username_,
reinterpret_cast
<
const
uint8_t
*>
(
username_
.
c_str
()),
reinterpret_cast
<
const
uint8_t
*>
(
password_
.
c_str
())};
size_t
credentials_sizes
[]
=
{
username_
.
size
(),
password_
.
size
()};
zap_client_t
::
send_zap_request
(
"PLAIN"
,
5
,
credentials
,
credentials_sizes
,
2
);
const
char
plain_mechanism_name
[]
=
"PLAIN"
;
zap_client_t
::
send_zap_request
(
plain_mechanism_name
,
sizeof
(
plain_mechanism_name
)
-
1
,
credentials
,
credentials_sizes
,
sizeof
(
credentials
)
/
sizeof
(
credentials
[
0
]));
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment