Skip to content

Commit 82a820e

Browse files
authored
Merge pull request #18 from mszeu/devel
Devel to Master: adding NI and some fixes
2 parents 074e11c + e0db7ec commit 82a820e

File tree

2 files changed

+93
-15
lines changed

2 files changed

+93
-15
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ It requires **Python 3**. It was tested on **Python 3.7**, **3.8** and **3.9** u
1818

1919
## Version
2020

21-
**1.1.5**
21+
**1.1.6**
2222

2323
## Usage
2424

2525
pressureTest.py [-h] [--port PORT]
26-
[--key KEY | --nc | --no | --pci | --j2 | --j4 | --j8 | --jk | --b2 | --randgen | --ecc]
26+
[--key KEY | --nc | --no | --ni | --pci | --j2 | --j4 | --j8 | --jk | --b2 | --randgen | --ecc]
2727
[--ecc-curve {0,1,2}] [--key-use {S,X,N}] [--key-exportability {N,E,S}] [--header HEADER]
2828
[--forever] [--decode] [--times TIMES] [--proto {tcp,udp,tls}] [--keyfile KEYFILE]
2929
[--crtfile CRTFILE] [--echo ECHO]
@@ -43,6 +43,8 @@ It requires **Python 3**. It was tested on **Python 3.7**, **3.8** and **3.9** u
4343

4444
**--no** gathers the status of the payShield through the **NO** command, type **00**.
4545

46+
**--ni** gathers the status of the Ethernet Host Port 1 through the **NI** command.
47+
4648
**--j2** gets HSM Loading using **J2** command.
4749

4850
**--j4** gets Host Command Volumes using **J4** command.

pressureTest.py

Lines changed: 89 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from typing import Tuple, Dict
1414
from types import FunctionType
1515

16-
VERSION = "1.1.5"
16+
VERSION = "1.1.6"
1717

1818

1919
def decode_n0(response_to_decode: bytes, head_len: int):
@@ -56,28 +56,24 @@ def decode_no(response_to_decode: bytes, head_len: int):
5656
"""
5757
BUFFER_SIZE: Dict[str, str] = {
5858
'0': '2K bytes', '1': '8K bytes', '2': '16K bytes', '3': '32K bytes'}
59+
NET_PROTO: Dict[str, str] = {'0': 'TCP', '1': 'UDP'}
5960
response_to_decode, msg_len, str_pointer = common_parser(response_to_decode, head_len)
6061
if response_to_decode[str_pointer:str_pointer + 2] == '00': # No errors
6162
if len(response_to_decode) >= (24 + head_len): # Mode 00
6263
# I obtained the value 24 in this way: 2 for the response len, 2 for the error code and the rest is for the
6364
# sum of the field len as indicated by the Core Host Command Manual
6465
str_pointer = str_pointer + 2
65-
print("I/O buffer size:", BUFFER_SIZE.get(response_to_decode[str_pointer:str_pointer + 1], "Unknown"))
66+
print("I/O buffer size: ", BUFFER_SIZE.get(response_to_decode[str_pointer:str_pointer + 1], "Unknown"))
6667
str_pointer = str_pointer + 1
67-
if response_to_decode[str_pointer:str_pointer + 1] == '0':
68-
print("Type of connection: UDP")
69-
elif response_to_decode[str_pointer:str_pointer + 1] == '1':
70-
print("Type of connection: TCP")
71-
else:
72-
print("Unexpected connection type")
68+
print("Type of connection: ", NET_PROTO.get(response_to_decode[str_pointer:str_pointer + 1], "Unknown"))
7369
str_pointer = str_pointer + 1
74-
print("Number of TCP sockets:", response_to_decode[str_pointer:str_pointer + 2])
70+
print("Number of TCP sockets: ", response_to_decode[str_pointer:str_pointer + 2])
7571
str_pointer = str_pointer + 2
76-
print("Firmware number:", response_to_decode[str_pointer:str_pointer + 9])
72+
print("Firmware number: ", response_to_decode[str_pointer:str_pointer + 9])
7773
str_pointer = str_pointer + 9
78-
print("Reserved:", response_to_decode[str_pointer:str_pointer + 1])
74+
print("Reserved: ", response_to_decode[str_pointer:str_pointer + 1])
7975
str_pointer = str_pointer + 1
80-
print("Reserved:", response_to_decode[str_pointer:str_pointer + 4])
76+
print("Reserved: ", response_to_decode[str_pointer:str_pointer + 4])
8177
else: # Mode 01
8278
str_pointer = str_pointer + 2
8379
if response_to_decode[str_pointer:str_pointer + 1] == '0':
@@ -94,6 +90,74 @@ def decode_no(response_to_decode: bytes, head_len: int):
9490
"\"The Enforce key type 002 separation for PCI HSM compliance\" setting is not one of these.")
9591

9692

93+
def decode_ni(response_to_decode: bytes, head_len: int):
94+
"""
95+
It decodes the result of the command NI an prints the meaning of the returned output
96+
97+
Parameters
98+
___________
99+
response_to_decode: bytes
100+
The response returned by the payShield
101+
head_len: int
102+
The length of the header
103+
104+
Returns
105+
___________
106+
nothing
107+
"""
108+
NET_PROTO: Dict[str, str] = {'0': 'TCP', '1': 'UDP'}
109+
SPECIFIC_ERROR: Dict[str, str] = {'01': 'Failed to execute NETSTAT',
110+
'82': 'Invalid Ethernet Statistics value'}
111+
NET_CONNECTION_STATUS: Dict[str, str] = {'0': 'ESTABLISHED', '1': 'CLOSED'}
112+
response_to_decode, msg_len, str_pointer = common_parser(response_to_decode, head_len)
113+
if response_to_decode[str_pointer:str_pointer + 2] == '00': # No errors
114+
str_pointer = str_pointer + 2
115+
print("Records to follow: ", response_to_decode[str_pointer:str_pointer + 4])
116+
records_to_follow = int(response_to_decode[str_pointer:str_pointer + 4])
117+
str_pointer = str_pointer + 4
118+
for record in range(records_to_follow):
119+
print("Protocol: ", NET_PROTO.get(response_to_decode[str_pointer:str_pointer + 1],
120+
"Unknown"))
121+
str_pointer = str_pointer + 1
122+
print("Local port number: ", response_to_decode[str_pointer:str_pointer + 4])
123+
str_pointer = str_pointer + 4
124+
print("IP Address: ", hex2ip(response_to_decode[str_pointer:str_pointer + 8]))
125+
str_pointer = str_pointer + 8
126+
print("Remote port number: ", response_to_decode[str_pointer:str_pointer + 4])
127+
str_pointer = str_pointer + 4
128+
print("Connection Status: ", NET_CONNECTION_STATUS.get(response_to_decode[str_pointer:str_pointer + 1]
129+
, 'Reserved'))
130+
str_pointer = str_pointer + 1
131+
print("Duration: ", response_to_decode[str_pointer:str_pointer + 8])
132+
str_pointer = str_pointer + 8
133+
print("Total Bytes Sent: ", int(response_to_decode[str_pointer:str_pointer + 16], 16))
134+
str_pointer = str_pointer + 16
135+
print("Total Bytes Received: ", int(response_to_decode[str_pointer:str_pointer + 16], 16))
136+
str_pointer = str_pointer + 16
137+
print("Total Unicast Packets Sent: ", int(response_to_decode[str_pointer:str_pointer + 8], 16))
138+
str_pointer = str_pointer + 8
139+
print("Total Unicast Packets Received: ", int(response_to_decode[str_pointer:str_pointer + 8], 16))
140+
str_pointer = str_pointer + 8
141+
print("Total Non-unicast packets Sent: ", int(response_to_decode[str_pointer:str_pointer + 8], 16))
142+
str_pointer = str_pointer + 8
143+
print("Total Non-unicast packets Received: ", int(response_to_decode[str_pointer:str_pointer + 8], 16))
144+
str_pointer = str_pointer + 8
145+
print("Total Packets Discarded During Send: ", int(response_to_decode[str_pointer:str_pointer + 8], 16))
146+
str_pointer = str_pointer + 8
147+
print("Total Packets Discarded During Receive: ", int(response_to_decode[str_pointer:str_pointer + 8], 16))
148+
str_pointer = str_pointer + 8
149+
print("Total Errors During Send: ", int(response_to_decode[str_pointer:str_pointer + 8], 16))
150+
str_pointer = str_pointer + 8
151+
print("Total Errors During Receive: ", int(response_to_decode[str_pointer:str_pointer + 8], 16))
152+
str_pointer = str_pointer + 8
153+
print("Total Unknown Packets: ", int(response_to_decode[str_pointer:str_pointer + 8], 16))
154+
str_pointer = str_pointer + 8
155+
156+
else:
157+
if SPECIFIC_ERROR.get(response_to_decode[str_pointer:str_pointer + 2]) is not None:
158+
print("Command specific error: ", SPECIFIC_ERROR.get(response_to_decode[str_pointer:str_pointer + 2]))
159+
160+
97161
def decode_nc(response_to_decode: bytes, head_len: int):
98162
"""
99163
It decodes the result of the command NC an prints the meaning of the returned output
@@ -628,6 +692,13 @@ def test_printable(input_str):
628692
return all(c in string.printable for c in input_str)
629693

630694

695+
def hex2ip(hex_ip):
696+
addr_long = int(hex_ip, 16)
697+
hex(addr_long)
698+
hex_ip = socket.inet_ntoa(pack(">L", addr_long))
699+
return hex_ip
700+
701+
631702
def run_test(ip_addr: str, port: int, host_command: str, proto: str = "tcp", header_len: int = 4,
632703
decoder_funct: FunctionType = None) -> int:
633704
"""It connects to the specified host and port, using the specified protocol (tcp, udp or tls) and sends the command.
@@ -794,7 +865,8 @@ def common_parser(response_to_decode: bytes, head_len: int) -> Tuple[str, int, i
794865
'J4': decode_j4,
795866
'JK': decode_jk,
796867
'B2': decode_b2,
797-
'FY': decode_ecc
868+
'FY': decode_ecc,
869+
'NI': decode_ni
798870
}
799871

800872
parser = argparse.ArgumentParser(
@@ -809,6 +881,8 @@ def common_parser(response_to_decode: bytes, head_len: int) -> Tuple[str, int, i
809881
action="store_true")
810882
group.add_argument("--no", help="Retrieves HSM status information using NO command. ",
811883
action="store_true")
884+
group.add_argument("--ni", help="return information about the Ethernet Host port 1",
885+
action="store_true")
812886
group.add_argument("--pci", help="Checks if the HSM is set in PCI compliant mode. ",
813887
action="store_true")
814888
group.add_argument("--j2", help="Get HSM Loading using J2 command. ",
@@ -870,6 +944,8 @@ def common_parser(response_to_decode: bytes, head_len: int) -> Tuple[str, int, i
870944
command = args.header + 'NC'
871945
elif args.no:
872946
command = args.header + 'NO00'
947+
elif args.ni:
948+
command = args.header + 'NI11'
873949
elif args.pci:
874950
command = args.header + 'NO01'
875951
elif args.j2:

0 commit comments

Comments
 (0)