smb: Rework constructs to use Self/Default

This commit modifies the constructors to use Self and/or
Default::default() when feasible
pull/6139/head
Jeff Lucovsky 5 years ago committed by Victor Julien
parent f502f21f9e
commit 02dccb1529

@ -61,7 +61,7 @@ impl SMBCommonHdr {
} }
} }
#[derive(Debug)] #[derive(Default, Debug)]
pub struct DCERPCIface { pub struct DCERPCIface {
pub uuid: Vec<u8>, pub uuid: Vec<u8>,
pub ver: u16, pub ver: u16,
@ -72,19 +72,17 @@ pub struct DCERPCIface {
} }
impl DCERPCIface { impl DCERPCIface {
pub fn new(uuid: Vec<u8>, ver: u16, ver_min: u16) -> DCERPCIface { pub fn new(uuid: Vec<u8>, ver: u16, ver_min: u16) -> Self {
DCERPCIface { Self {
uuid: uuid, uuid: uuid,
ver:ver, ver:ver,
ver_min:ver_min, ver_min:ver_min,
ack_result:0, ..Default::default()
ack_reason:0,
acked:false,
} }
} }
} }
#[derive(Debug)] #[derive(Default, Debug)]
pub struct SMBTransactionDCERPC { pub struct SMBTransactionDCERPC {
pub opnum: u16, pub opnum: u16,
pub req_cmd: u8, pub req_cmd: u8,
@ -99,32 +97,19 @@ pub struct SMBTransactionDCERPC {
} }
impl SMBTransactionDCERPC { impl SMBTransactionDCERPC {
fn new_request(req: u8, call_id: u32) -> SMBTransactionDCERPC { fn new_request(req: u8, call_id: u32) -> Self {
return SMBTransactionDCERPC { return Self {
opnum: 0, opnum: 0,
req_cmd: req, req_cmd: req,
req_set: true, req_set: true,
res_cmd: 0,
res_set: false,
call_id: call_id, call_id: call_id,
frag_cnt_ts: 0, ..Default::default()
frag_cnt_tc: 0,
stub_data_ts:Vec::new(),
stub_data_tc:Vec::new(),
} }
} }
fn new_response(call_id: u32) -> SMBTransactionDCERPC { fn new_response(call_id: u32) -> Self {
return SMBTransactionDCERPC { return Self {
opnum: 0,
req_cmd: 0,
req_set: false,
res_cmd: 0,
res_set: false,
call_id: call_id, call_id: call_id,
frag_cnt_ts: 0, ..Default::default()
frag_cnt_tc: 0,
stub_data_ts:Vec::new(),
stub_data_tc:Vec::new(),
} }
} }
pub fn set_result(&mut self, res: u8) { pub fn set_result(&mut self, res: u8) {

@ -22,7 +22,7 @@ use crate::filecontainer::*;
use crate::smb::smb::*; use crate::smb::smb::*;
/// File tracking transaction. Single direction only. /// File tracking transaction. Single direction only.
#[derive(Debug)] #[derive(Default, Debug)]
pub struct SMBTransactionFile { pub struct SMBTransactionFile {
pub direction: u8, pub direction: u8,
pub fuid: Vec<u8>, pub fuid: Vec<u8>,
@ -35,14 +35,10 @@ pub struct SMBTransactionFile {
} }
impl SMBTransactionFile { impl SMBTransactionFile {
pub fn new() -> SMBTransactionFile { pub fn new() -> Self {
return SMBTransactionFile { return Self {
direction: 0,
fuid: Vec::new(),
file_name: Vec::new(),
share_name: Vec::new(),
file_tracker: FileTransferTracker::new(), file_tracker: FileTransferTracker::new(),
post_gap_ts: 0, ..Default::default()
} }
} }
} }

@ -20,7 +20,7 @@ use crate::smb::smb::*;
use crate::smb::smb1_session::*; use crate::smb::smb1_session::*;
use crate::smb::auth::*; use crate::smb::auth::*;
#[derive(Debug)] #[derive(Default, Debug)]
pub struct SMBTransactionSessionSetup { pub struct SMBTransactionSessionSetup {
pub request_host: Option<SessionSetupRequest>, pub request_host: Option<SessionSetupRequest>,
pub response_host: Option<SessionSetupResponse>, pub response_host: Option<SessionSetupResponse>,
@ -29,13 +29,8 @@ pub struct SMBTransactionSessionSetup {
} }
impl SMBTransactionSessionSetup { impl SMBTransactionSessionSetup {
pub fn new() -> SMBTransactionSessionSetup { pub fn new() -> Self {
return SMBTransactionSessionSetup { return Default::default()
request_host: None,
response_host: None,
ntlmssp: None,
krb_ticket: None,
}
} }
} }

@ -192,7 +192,7 @@ pub fn ntlmssp_type_string(c: u32) -> String {
}.to_string() }.to_string()
} }
#[derive(Eq, PartialEq, Debug, Clone)] #[derive(Default, Eq, PartialEq, Debug, Clone)]
pub struct SMBVerCmdStat { pub struct SMBVerCmdStat {
smb_ver: u8, smb_ver: u8,
smb1_cmd: u8, smb1_cmd: u8,
@ -205,60 +205,40 @@ pub struct SMBVerCmdStat {
} }
impl SMBVerCmdStat { impl SMBVerCmdStat {
pub fn new() -> SMBVerCmdStat { pub fn new() -> Self {
return SMBVerCmdStat { Default::default()
smb_ver: 0, }
smb1_cmd: 0, pub fn new1(cmd: u8) -> Self {
smb2_cmd: 0, return Self {
status_set: false,
status_is_dos_error: false,
status_error_class: 0,
status: 0,
}
}
pub fn new1(cmd: u8) -> SMBVerCmdStat {
return SMBVerCmdStat {
smb_ver: 1, smb_ver: 1,
smb1_cmd: cmd, smb1_cmd: cmd,
smb2_cmd: 0, ..Default::default()
status_set: false,
status_is_dos_error: false,
status_error_class: 0,
status: 0,
} }
} }
pub fn new1_with_ntstatus(cmd: u8, status: u32) -> SMBVerCmdStat { pub fn new1_with_ntstatus(cmd: u8, status: u32) -> Self {
return SMBVerCmdStat { return Self {
smb_ver: 1, smb_ver: 1,
smb1_cmd: cmd, smb1_cmd: cmd,
smb2_cmd: 0,
status_set: true, status_set: true,
status_is_dos_error: false,
status_error_class: 0,
status: status, status: status,
..Default::default()
} }
} }
pub fn new2(cmd: u16) -> SMBVerCmdStat { pub fn new2(cmd: u16) -> Self {
return SMBVerCmdStat { return Self {
smb_ver: 2, smb_ver: 2,
smb1_cmd: 0,
smb2_cmd: cmd, smb2_cmd: cmd,
status_set: false, ..Default::default()
status_is_dos_error: false,
status_error_class: 0,
status: 0,
} }
} }
pub fn new2_with_ntstatus(cmd: u16, status: u32) -> SMBVerCmdStat { pub fn new2_with_ntstatus(cmd: u16, status: u32) -> Self {
return SMBVerCmdStat { return Self {
smb_ver: 2, smb_ver: 2,
smb1_cmd: 0,
smb2_cmd: cmd, smb2_cmd: cmd,
status_set: true, status_set: true,
status_is_dos_error: false,
status_error_class: 0,
status: status, status: status,
..Default::default()
} }
} }
@ -338,8 +318,8 @@ pub struct SMBFiletime {
} }
impl SMBFiletime { impl SMBFiletime {
pub fn new(raw: u64) -> SMBFiletime { pub fn new(raw: u64) -> Self {
SMBFiletime { Self {
ts: raw, ts: raw,
} }
} }
@ -380,9 +360,9 @@ pub struct SMBTransactionSetFilePathInfo {
impl SMBTransactionSetFilePathInfo { impl SMBTransactionSetFilePathInfo {
pub fn new(filename: Vec<u8>, fid: Vec<u8>, subcmd: u16, loi: u16, delete_on_close: bool) pub fn new(filename: Vec<u8>, fid: Vec<u8>, subcmd: u16, loi: u16, delete_on_close: bool)
-> SMBTransactionSetFilePathInfo -> Self
{ {
return SMBTransactionSetFilePathInfo { return Self {
filename: filename, fid: fid, filename: filename, fid: fid,
subcmd: subcmd, subcmd: subcmd,
loi: loi, loi: loi,
@ -438,8 +418,8 @@ pub struct SMBTransactionRename {
} }
impl SMBTransactionRename { impl SMBTransactionRename {
pub fn new(fuid: Vec<u8>, oldname: Vec<u8>, newname: Vec<u8>) -> SMBTransactionRename { pub fn new(fuid: Vec<u8>, oldname: Vec<u8>, newname: Vec<u8>) -> Self {
return SMBTransactionRename { return Self {
fuid: fuid, oldname: oldname, newname: newname, fuid: fuid, oldname: oldname, newname: newname,
} }
} }
@ -463,7 +443,7 @@ impl SMBState {
} }
} }
#[derive(Debug)] #[derive(Default, Debug)]
pub struct SMBTransactionCreate { pub struct SMBTransactionCreate {
pub disposition: u32, pub disposition: u32,
pub delete_on_close: bool, pub delete_on_close: bool,
@ -480,23 +460,18 @@ pub struct SMBTransactionCreate {
} }
impl SMBTransactionCreate { impl SMBTransactionCreate {
pub fn new(filename: Vec<u8>, disp: u32, del: bool, dir: bool) -> SMBTransactionCreate { pub fn new(filename: Vec<u8>, disp: u32, del: bool, dir: bool) -> Self {
return SMBTransactionCreate { return Self {
disposition: disp, disposition: disp,
delete_on_close: del, delete_on_close: del,
directory: dir, directory: dir,
filename: filename, filename: filename,
guid: Vec::new(), ..Default::default()
create_ts: 0,
last_access_ts: 0,
last_write_ts: 0,
last_change_ts: 0,
size: 0,
} }
} }
} }
#[derive(Debug)] #[derive(Default, Debug)]
pub struct SMBTransactionNegotiate { pub struct SMBTransactionNegotiate {
pub smb_ver: u8, pub smb_ver: u8,
pub dialects: Vec<Vec<u8>>, pub dialects: Vec<Vec<u8>>,
@ -508,18 +483,16 @@ pub struct SMBTransactionNegotiate {
} }
impl SMBTransactionNegotiate { impl SMBTransactionNegotiate {
pub fn new(smb_ver: u8) -> SMBTransactionNegotiate { pub fn new(smb_ver: u8) -> Self {
return SMBTransactionNegotiate { return Self {
smb_ver: smb_ver, smb_ver: smb_ver,
dialects: Vec::new(),
dialects2: Vec::new(),
client_guid: None,
server_guid: Vec::with_capacity(16), server_guid: Vec::with_capacity(16),
..Default::default()
} }
} }
} }
#[derive(Debug)] #[derive(Default, Debug)]
pub struct SMBTransactionTreeConnect { pub struct SMBTransactionTreeConnect {
pub is_pipe: bool, pub is_pipe: bool,
pub share_type: u8, pub share_type: u8,
@ -532,14 +505,10 @@ pub struct SMBTransactionTreeConnect {
} }
impl SMBTransactionTreeConnect { impl SMBTransactionTreeConnect {
pub fn new(share_name: Vec<u8>) -> SMBTransactionTreeConnect { pub fn new(share_name: Vec<u8>) -> Self {
return SMBTransactionTreeConnect { return Self {
is_pipe:false,
share_type: 0,
tree_id:0,
share_name:share_name, share_name:share_name,
req_service: None, ..Default::default()
res_service: None,
} }
} }
} }
@ -567,17 +536,17 @@ pub struct SMBTransaction {
} }
impl SMBTransaction { impl SMBTransaction {
pub fn new() -> SMBTransaction { pub fn new() -> Self {
return SMBTransaction{ return Self {
id: 0, id: 0,
vercmd: SMBVerCmdStat::new(), vercmd: SMBVerCmdStat::new(),
hdr: SMBCommonHdr::init(), hdr: SMBCommonHdr::init(),
request_done: false, request_done: false,
response_done: false, response_done: false,
type_data: None, type_data: None,
de_state: None, de_state: None,
events: std::ptr::null_mut(), events: std::ptr::null_mut(),
tx_data: AppLayerTxData::new(), tx_data: AppLayerTxData::new(),
} }
} }
@ -616,8 +585,8 @@ pub struct SMBFileGUIDOffset {
} }
impl SMBFileGUIDOffset { impl SMBFileGUIDOffset {
pub fn new(guid: Vec<u8>, offset: u64) -> SMBFileGUIDOffset { pub fn new(guid: Vec<u8>, offset: u64) -> Self {
SMBFileGUIDOffset { Self {
guid:guid, guid:guid,
offset:offset, offset:offset,
} }
@ -637,7 +606,7 @@ pub const SMBHDR_TYPE_TRANS_FRAG: u32 = 8;
pub const SMBHDR_TYPE_TREE: u32 = 9; pub const SMBHDR_TYPE_TREE: u32 = 9;
pub const SMBHDR_TYPE_DCERPCTX: u32 = 10; pub const SMBHDR_TYPE_DCERPCTX: u32 = 10;
#[derive(Hash, Eq, PartialEq, Debug)] #[derive(Default, Hash, Eq, PartialEq, Debug)]
pub struct SMBCommonHdr { pub struct SMBCommonHdr {
pub ssn_id: u64, pub ssn_id: u64,
pub tree_id: u32, pub tree_id: u32,
@ -646,16 +615,11 @@ pub struct SMBCommonHdr {
} }
impl SMBCommonHdr { impl SMBCommonHdr {
pub fn init() -> SMBCommonHdr { pub fn init() -> Self {
SMBCommonHdr { Default::default()
rec_type : 0,
ssn_id : 0,
tree_id : 0,
msg_id : 0,
}
} }
pub fn new(rec_type: u32, ssn_id: u64, tree_id: u32, msg_id: u64) -> SMBCommonHdr { pub fn new(rec_type: u32, ssn_id: u64, tree_id: u32, msg_id: u64) -> Self {
SMBCommonHdr { Self {
rec_type : rec_type, rec_type : rec_type,
ssn_id : ssn_id, ssn_id : ssn_id,
tree_id : tree_id, tree_id : tree_id,
@ -712,8 +676,8 @@ pub struct SMBHashKeyHdrGuid {
} }
impl SMBHashKeyHdrGuid { impl SMBHashKeyHdrGuid {
pub fn new(hdr: SMBCommonHdr, guid: Vec<u8>) -> SMBHashKeyHdrGuid { pub fn new(hdr: SMBCommonHdr, guid: Vec<u8>) -> Self {
SMBHashKeyHdrGuid { Self {
hdr: hdr, guid: guid, hdr: hdr, guid: guid,
} }
} }
@ -726,8 +690,8 @@ pub struct SMBTree {
} }
impl SMBTree { impl SMBTree {
pub fn new(name: Vec<u8>, is_pipe: bool) -> SMBTree { pub fn new(name: Vec<u8>, is_pipe: bool) -> Self {
SMBTree { Self {
name:name, name:name,
is_pipe:is_pipe, is_pipe:is_pipe,
} }
@ -802,8 +766,8 @@ pub struct SMBState<> {
impl SMBState { impl SMBState {
/// Allocation function for a new TLS parser instance /// Allocation function for a new TLS parser instance
pub fn new() -> SMBState { pub fn new() -> Self {
SMBState { Self {
ssn2vec_map:HashMap::new(), ssn2vec_map:HashMap::new(),
guid2name_map:HashMap::new(), guid2name_map:HashMap::new(),
ssn2vecoffset_map:HashMap::new(), ssn2vecoffset_map:HashMap::new(),

@ -28,8 +28,8 @@ pub struct SMBTransactionIoctl {
} }
impl SMBTransactionIoctl { impl SMBTransactionIoctl {
pub fn new(func: u32) -> SMBTransactionIoctl { pub fn new(func: u32) -> Self {
return SMBTransactionIoctl { return Self {
func: func, func: func,
} }
} }

Loading…
Cancel
Save