]> git.ipfire.org Git - people/ms/suricata.git/blob - rust/src/ssh/detect.rs
rust: functions that reference raw pointers are unsafe
[people/ms/suricata.git] / rust / src / ssh / detect.rs
1 /* Copyright (C) 2020 Open Information Security Foundation
2 *
3 * You can copy, redistribute or modify this Program under the terms of
4 * the GNU General Public License version 2 as published by the Free
5 * Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * version 2 along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 */
17
18 use super::ssh::SSHTransaction;
19 use crate::core::{STREAM_TOCLIENT, STREAM_TOSERVER};
20 use std::ptr;
21
22 #[no_mangle]
23 pub unsafe extern "C" fn rs_ssh_tx_get_protocol(
24 tx: *mut std::os::raw::c_void, buffer: *mut *const u8, buffer_len: *mut u32, direction: u8,
25 ) -> u8 {
26 let tx = cast_pointer!(tx, SSHTransaction);
27 match direction {
28 STREAM_TOSERVER => {
29 let m = &tx.cli_hdr.protover;
30 if m.len() > 0 {
31 *buffer = m.as_ptr();
32 *buffer_len = m.len() as u32;
33 return 1;
34 }
35 }
36 STREAM_TOCLIENT => {
37 let m = &tx.srv_hdr.protover;
38 if m.len() > 0 {
39 *buffer = m.as_ptr();
40 *buffer_len = m.len() as u32;
41 return 1;
42 }
43 }
44 _ => {}
45 }
46 *buffer = ptr::null();
47 *buffer_len = 0;
48
49 return 0;
50 }
51
52 #[no_mangle]
53 pub unsafe extern "C" fn rs_ssh_tx_get_software(
54 tx: *mut std::os::raw::c_void, buffer: *mut *const u8, buffer_len: *mut u32, direction: u8,
55 ) -> u8 {
56 let tx = cast_pointer!(tx, SSHTransaction);
57 match direction {
58 STREAM_TOSERVER => {
59 let m = &tx.cli_hdr.swver;
60 if m.len() > 0 {
61 *buffer = m.as_ptr();
62 *buffer_len = m.len() as u32;
63 return 1;
64 }
65 }
66 STREAM_TOCLIENT => {
67 let m = &tx.srv_hdr.swver;
68 if m.len() > 0 {
69 *buffer = m.as_ptr();
70 *buffer_len = m.len() as u32;
71 return 1;
72 }
73 }
74 _ => {}
75 }
76 *buffer = ptr::null();
77 *buffer_len = 0;
78
79 return 0;
80 }
81
82 #[no_mangle]
83 pub unsafe extern "C" fn rs_ssh_tx_get_hassh(
84 tx: *mut std::os::raw::c_void,
85 buffer: *mut *const u8,
86 buffer_len: *mut u32,
87 direction: u8,
88 ) -> u8 {
89 let tx = cast_pointer!(tx, SSHTransaction);
90 match direction {
91 STREAM_TOSERVER => {
92 let m = &tx.cli_hdr.hassh;
93 if m.len() > 0 {
94 *buffer = m.as_ptr();
95 *buffer_len = m.len() as u32;
96 return 1;
97 }
98 }
99 STREAM_TOCLIENT => {
100 let m = &tx.srv_hdr.hassh;
101 if m.len() > 0 {
102 *buffer = m.as_ptr();
103 *buffer_len = m.len() as u32;
104 return 1;
105 }
106 }
107 _ => {}
108 }
109 *buffer = ptr::null();
110 *buffer_len = 0;
111
112 return 0;
113 }
114
115 #[no_mangle]
116 pub unsafe extern "C" fn rs_ssh_tx_get_hassh_string(
117 tx: *mut std::os::raw::c_void,
118 buffer: *mut *const u8,
119 buffer_len: *mut u32,
120 direction: u8,
121 ) -> u8 {
122 let tx = cast_pointer!(tx, SSHTransaction);
123 match direction {
124 STREAM_TOSERVER => {
125 let m = &tx.cli_hdr.hassh_string;
126 if m.len() > 0 {
127 *buffer = m.as_ptr();
128 *buffer_len = m.len() as u32;
129 return 1;
130 }
131 }
132 STREAM_TOCLIENT => {
133 let m = &tx.srv_hdr.hassh_string;
134 if m.len() > 0 {
135 *buffer = m.as_ptr();
136 *buffer_len = m.len() as u32;
137 return 1;
138 }
139 }
140 _ => {}
141 }
142 *buffer = ptr::null();
143 *buffer_len = 0;
144
145 return 0;
146 }