pub struct Cookie { /* private fields */ }
Expand description
Option data for a DNS cookie.
A value of this type carries two parts: A mandatory ClientCookie
and
an optional ServerCookie
. The client cookie is chosen by, yes, the
client and added to a request when contacting a specific server for the
first time. When responding, a server calculates a server cookie from the
client cookie and adds both of them to the response. The client remembers
both and includes them in subsequent requests. The server can now check
that the the server cookie was indeed calculated by it and treat the
repeat customer differently.
While you can create a new cookie using the new
method,
shortcuts are available for the standard workflow. A new initial cookie
can be created via create_initial
. As this will
be a random client cookie, it needs the rand
feature. The server can
check whether a received cookie includes a server cookie created by it
via the check_server_hash
method. It needs
the SipHash-2-4 algorithm and is thus available if the siphasher
feature
is enabled. The same feature also enables the
create_response
method which creates the server
cookie to be included in a response.
Implementations§
source§impl Cookie
impl Cookie
sourcepub fn new(client: ClientCookie, server: Option<ServerCookie>) -> Self
pub fn new(client: ClientCookie, server: Option<ServerCookie>) -> Self
Creates a new cookie from client and optional server cookie.
sourcepub fn client(&self) -> ClientCookie
pub fn client(&self) -> ClientCookie
Returns the client cookie.
sourcepub fn server(&self) -> Option<&ServerCookie>
pub fn server(&self) -> Option<&ServerCookie>
Returns a reference to the server cookie if present.
sourcepub fn parse<Octs: AsRef<[u8]> + ?Sized>(
parser: &mut Parser<'_, Octs>
) -> Result<Self, ParseError>
pub fn parse<Octs: AsRef<[u8]> + ?Sized>( parser: &mut Parser<'_, Octs> ) -> Result<Self, ParseError>
Parses the cookie from its wire format.
sourcepub fn check_server_hash(
&self,
client_ip: IpAddr,
secret: &[u8; 16],
timestamp_ok: impl FnOnce(Serial) -> bool
) -> bool
pub fn check_server_hash( &self, client_ip: IpAddr, secret: &[u8; 16], timestamp_ok: impl FnOnce(Serial) -> bool ) -> bool
Returns whether the standard server cookie’s hash is correct.
The client_ip
is the source IP address of a request. The secret
is the server cookie secret. The timestamp is checked via the
timestamp_ok
closure which is given the timestamp and should return
whether it is acceptable.
Returns false
if the cookie is not a server cookie, if it is but
not a standard server cookie, or if it is but either the timestamp
is not acceptable or the hash differs from what it should be.
Thus, if this method returns false
, there is no valid server cookie
and the server can proceed as if there was no server cookie as
described in section 5.2.3 of RFC 7873.
sourcepub fn create_initial() -> Self
pub fn create_initial() -> Self
Creates a random client cookie for including in an initial request.
sourcepub fn create_response(
&self,
timestamp: Serial,
client_ip: IpAddr,
secret: &[u8; 16]
) -> Self
pub fn create_response( &self, timestamp: Serial, client_ip: IpAddr, secret: &[u8; 16] ) -> Self
Creates a standard format cookie option for sending a response.
This method uses the client cookie and the additional values provided to produce a cookie option that should be included in a response.
Trait Implementations§
source§impl ComposeOptData for Cookie
impl ComposeOptData for Cookie
fn compose_len(&self) -> u16
fn compose_option<Target: OctetsBuilder + ?Sized>( &self, target: &mut Target ) -> Result<(), Target::AppendError>
source§impl<Octs, Name> From<Cookie> for AllOptData<Octs, Name>
impl<Octs, Name> From<Cookie> for AllOptData<Octs, Name>
source§impl OptData for Cookie
impl OptData for Cookie
source§fn code(&self) -> OptionCode
fn code(&self) -> OptionCode
source§impl Ord for Cookie
impl Ord for Cookie
source§impl<'a, Octs: AsRef<[u8]> + ?Sized> ParseOptData<'a, Octs> for Cookie
impl<'a, Octs: AsRef<[u8]> + ?Sized> ParseOptData<'a, Octs> for Cookie
source§fn parse_option(
code: OptionCode,
parser: &mut Parser<'a, Octs>
) -> Result<Option<Self>, ParseError>
fn parse_option( code: OptionCode, parser: &mut Parser<'a, Octs> ) -> Result<Option<Self>, ParseError>
source§impl PartialEq for Cookie
impl PartialEq for Cookie
source§impl PartialOrd for Cookie
impl PartialOrd for Cookie
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more