1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
use crate::{
    base::{
        EFI_IPv4_ADDRESS, 
        EFI_STATUS, 
        EFI_HANDLE, 
        EFI_EVENT,
        EFI_GUID,
        EFI_SUCCESS,
        UINT8,
        UINT16,
        UINT32,
        UINTN,
        BOOLEAN,
        VOID,
        TRUE,
        FALSE,
    },
    managed_network::EFI_MANAGED_NETWORK_CONFIG_DATA,
    simple_network::EFI_SIMPLE_NETWORK_MODE,
    ip4::EFI_IP4_MODE_DATA,
};

use core::{mem, ptr};

pub const EFI_TCP4_SERVICE_BINDING_PROTOCOL_GUID: EFI_GUID = EFI_GUID(0x00720665, 0x67EB, 0x4a99, [0xBA, 0xF7, 0xD3, 0xC3, 0x3A, 0x1C, 0x7C, 0xC9]);

pub const EFI_TCP4_PROTOCOL_GUID: EFI_GUID = EFI_GUID(0x65530BC7, 0xA359, 0x410f, [0xB0, 0x10, 0x5A, 0xAD, 0xC7, 0xEC, 0x2B, 0x62]);

#[repr(C)]
pub struct EFI_TCP4_PROTOCOL {
    pub GetModeData: EFI_TCP4_GET_MODE_DATA,
    pub Configure: EFI_TCP4_CONFIGURE,
    pub Routes: EFI_TCP4_ROUTES,
    pub Connect: EFI_TCP4_CONNECT,
    pub Accept: EFI_TCP4_ACCEPT,
    pub Transmit: EFI_TCP4_TRANSMIT,
    pub Receive: EFI_TCP4_RECEIVE,
    pub Close: EFI_TCP4_CLOSE,
    pub Cancel: EFI_TCP4_CANCEL,
    pub Poll: EFI_TCP4_POLL,
}


pub type EFI_TCP4_GET_MODE_DATA = extern "win64" fn(
    This: *const EFI_TCP4_PROTOCOL,
    Tcp4State: *mut EFI_TCP4_CONNECTION_STATE,
    Tcp4ConfigData: *mut EFI_TCP4_CONFIG_DATA,
    Ip4ModeData: *mut EFI_IP4_MODE_DATA,
    MnpConfigData: *mut EFI_MANAGED_NETWORK_CONFIG_DATA,
    SnpModeData: *mut EFI_SIMPLE_NETWORK_MODE 
) -> EFI_STATUS;

#[derive(Debug)]
#[repr(C)]
pub struct EFI_TCP4_ACCESS_POINT {
    pub UseDefaultAddress: BOOLEAN,
    pub StationAddress: EFI_IPv4_ADDRESS,
    pub SubnetMask: EFI_IPv4_ADDRESS,
    pub StationPort: UINT16,
    pub RemoteAddress: EFI_IPv4_ADDRESS,
    pub RemotePort: UINT16,
    pub ActiveFlag: BOOLEAN,
}

impl Default for EFI_TCP4_ACCESS_POINT {
    fn default() -> Self {
        Self {
            UseDefaultAddress: FALSE,
            StationAddress: EFI_IPv4_ADDRESS::zero(),
            SubnetMask: EFI_IPv4_ADDRESS::zero(),
            StationPort: 0,
            RemoteAddress: EFI_IPv4_ADDRESS::zero(),
            RemotePort: 0,
            ActiveFlag: TRUE,
        }
    }
}

#[derive(Debug)]
#[repr(C)]
pub struct EFI_TCP4_OPTION {
    pub ReceiveBufferSize: UINT32,
    pub SendBufferSize: UINT32,
    pub MaxSynBackLog: UINT32,
    pub ConnectionTimeout: UINT32,
    pub DataRetries: UINT32,
    pub FinTimeout: UINT32,
    pub TimeWaitTimeout: UINT32,
    pub KeepAliveProbes: UINT32,
    pub KeepAliveTime: UINT32,
    pub KeepAliveInterval: UINT32,
    pub EnableNagle: BOOLEAN,
    pub EnableTimeStamp: BOOLEAN,
    pub EnableWindowScaling: BOOLEAN,
    pub EnableSelectiveAck: BOOLEAN,
    pub EnablePathMtuDiscovery: BOOLEAN,
}

#[derive(Debug)]
#[repr(C)]
pub struct EFI_TCP4_CONFIG_DATA {
    pub TypeOfService: UINT8,
    pub TimeToLive: UINT8,
    pub AccessPoint: EFI_TCP4_ACCESS_POINT,
    pub ControlOption: *const EFI_TCP4_OPTION,
}

impl Default for EFI_TCP4_CONFIG_DATA {
    fn default() -> Self {
        Self {
            TypeOfService: 0,
            TimeToLive: 0,
            AccessPoint: EFI_TCP4_ACCESS_POINT::default(),
            ControlOption: ptr::null() as *const EFI_TCP4_OPTION,
        }
    }
}

#[derive(Debug)]
#[repr(C)]
pub enum EFI_TCP4_CONNECTION_STATE{
    Tcp4StateClosed = 0,
    Tcp4StateListen = 1,
    Tcp4StateSynSent = 2,
    Tcp4StateSynReceived = 3,
    Tcp4StateEstablished = 4,
    Tcp4StateFinWait1 = 5,
    Tcp4StateFinWait2 = 6,
    Tcp4StateClosing = 7,
    Tcp4StateTimeWait = 8,
    Tcp4StateCloseWait = 9,
    Tcp4StateLastAck = 10
}

pub type EFI_TCP4_CONFIGURE = extern "win64" fn(
    This: *const EFI_TCP4_PROTOCOL,
    TcpConfigData: *const EFI_TCP4_CONFIG_DATA,
) -> EFI_STATUS;


pub type EFI_TCP4_ROUTES = extern "win64" fn(
    This: *const EFI_TCP4_PROTOCOL,
    DeleteRoute: BOOLEAN,
    SubnetAddress: *const EFI_IPv4_ADDRESS,
    SubnetMask: *const EFI_IPv4_ADDRESS,
    GatewayAddress: *const EFI_IPv4_ADDRESS
) -> EFI_STATUS;

pub type EFI_TCP4_CONNECT = extern "win64" fn(
    This: *const EFI_TCP4_PROTOCOL,
    ConnectionToken: *mut EFI_TCP4_CONNECTION_TOKEN
) -> EFI_STATUS;

#[derive(Debug)]
#[repr(C)]
pub struct EFI_TCP4_COMPLETION_TOKEN {
    pub Event: EFI_EVENT,
    pub Status: EFI_STATUS,
}

impl Default for EFI_TCP4_COMPLETION_TOKEN {
    fn default() -> Self {
        Self {
            Event: ptr::null() as EFI_EVENT,
            Status: EFI_SUCCESS
        }
    }
}

#[derive(Debug)]
#[repr(C)]
pub struct EFI_TCP4_CONNECTION_TOKEN {
    pub CompletionToken: EFI_TCP4_COMPLETION_TOKEN,
}


impl Default for EFI_TCP4_CONNECTION_TOKEN {
    fn default() -> Self {
        Self { CompletionToken: EFI_TCP4_COMPLETION_TOKEN::default() }
    }
}

pub type EFI_TCP4_ACCEPT = extern "win64" fn(
    This: *const EFI_TCP4_PROTOCOL,
    ListenToken: *const EFI_TCP4_LISTEN_TOKEN
) -> EFI_STATUS;

#[derive(Debug)]
#[repr(C)]
pub struct EFI_TCP4_LISTEN_TOKEN {
    pub CompletionToken: EFI_TCP4_COMPLETION_TOKEN,
    pub NewChildHandle: EFI_HANDLE,
}

impl Default for EFI_TCP4_LISTEN_TOKEN {
    fn default() -> Self {
        Self { 
            CompletionToken: EFI_TCP4_COMPLETION_TOKEN::default(),
            NewChildHandle: ptr::null() as EFI_HANDLE
         }
    }
}

#[derive(Debug)]
#[repr(C)]
pub struct EFI_TCP4_TRANSMIT_DATA {
    pub Push: BOOLEAN,
    pub Urgent: BOOLEAN,
    pub DataLength: UINT32,
    pub FragmentCount: UINT32,
    // TODO: FragmentTable field can contains more than 1 element but this is how
    // it is declared in C. Is there a better way to declare it in Rust?
    pub FragmentTable: [EFI_TCP4_FRAGMENT_DATA; 1], 
}

pub type EFI_TCP4_TRANSMIT = extern "win64" fn(
    This: *const EFI_TCP4_PROTOCOL,
    Token: *const EFI_TCP4_IO_TOKEN
) -> EFI_STATUS;

#[repr(C)]
pub union PacketUnion {
    pub RxData: *const EFI_TCP4_RECEIVE_DATA,
    pub TxData: *const EFI_TCP4_TRANSMIT_DATA,
}

#[repr(C)]
pub struct EFI_TCP4_IO_TOKEN {
    pub CompletionToken: EFI_TCP4_COMPLETION_TOKEN,
    pub Packet: PacketUnion
}

impl Default for EFI_TCP4_IO_TOKEN  {
    fn default() -> Self {
        Self { 
            CompletionToken: EFI_TCP4_COMPLETION_TOKEN::default(),
            Packet: PacketUnion { TxData: ptr::null() as *const EFI_TCP4_TRANSMIT_DATA }
         }
    }
}

pub const EFI_CONNECTION_FIN: UINTN = with_high_bit_set!(104);
pub const EFI_CONNECTION_RESET: UINTN = with_high_bit_set!(105);
pub const EFI_CONNECTION_REFUSED: UINTN =  with_high_bit_set!(106);

#[repr(C)]
pub struct EFI_TCP4_RECEIVE_DATA {
    pub UrgentFlag: BOOLEAN,
    pub DataLength: UINT32,
    pub FragmentCount: UINT32,
    // TODO: FragmentTable field can contains more than 1 element but this is how
    // it is declared in C. Is there a better way to declare it in Rust?
    pub FragmentTable: [EFI_TCP4_FRAGMENT_DATA; 1], 
}

#[derive(Debug)]
#[repr(C)]
pub struct EFI_TCP4_FRAGMENT_DATA {
    pub FragmentLength: UINT32,
    pub FragmentBuffer: *const VOID,
}

pub type EFI_TCP4_RECEIVE = extern "win64" fn(
    This: *const EFI_TCP4_PROTOCOL,
    Token: *const EFI_TCP4_IO_TOKEN
) -> EFI_STATUS;

pub type EFI_TCP4_CLOSE = extern "win64" fn(
    This: *const EFI_TCP4_PROTOCOL,
    CloseToken: *const EFI_TCP4_CLOSE_TOKEN
) -> EFI_STATUS;

#[derive(Debug)]
#[repr(C)]
pub struct EFI_TCP4_CLOSE_TOKEN {
    pub CompletionToken: EFI_TCP4_COMPLETION_TOKEN,
    pub AbortOnClose: BOOLEAN,
}

impl Default for EFI_TCP4_CLOSE_TOKEN {
    fn default() -> Self {
        Self { 
            CompletionToken: EFI_TCP4_COMPLETION_TOKEN::default(),
            AbortOnClose: FALSE,
        }
    }
}

pub type EFI_TCP4_CANCEL = extern "win64" fn(
    This: *const EFI_TCP4_PROTOCOL,
    Token: *const EFI_TCP4_COMPLETION_TOKEN
) -> EFI_STATUS;

pub type EFI_TCP4_POLL = extern "win64" fn(
    This: *const EFI_TCP4_PROTOCOL
) -> EFI_STATUS;