3.3.2
Session API (tinydtls/session.c, tinydtls/session.h)
tinydtls/session.h
defines the structure
session_t
which defines variables to
store socket address, address type, size of the address and interface index. It has been defined
as shown in the following code snippet.
(
NOTE:
The variables that depend on UNIX libraries have been shown in in red)
55
typedef
struct
{
socklen_t size
;
/**< size of addr > defined in sys/socket.h */
union
{
struct
sockaddr sa
;
//Defined in sys/socket.h to hold address family and
address string
struct
sockaddr_storage st
;
// Defined in sys/socket.h to store the complete
socket addresses
struct
sockaddr_in sin
;
// Defined in sys/socket.h to deal with IPv4 socket
addresses
struct
sockaddr_in6 sin6
;
// Defined in sys/socket.h to deal with IPv6 socket
addresses
} addr;
uint8_t ifindex;
} session_t;
Resolving the UNIX dependency:
To port this sessions structure on Arduino-like SODAQ
SARA:
•
The UNIX-specific structures have been deleted. A 32-bit integer to hold the socket
length, a simple string to hold the IP, and a 16-bit integer to hold the port are defined,
to compensate for this removal.
Design Decision
: The reason for adding only three variables is that, the NB-IoT API by
SODAQ can allow UDP messaging as long as remote IP and port have been defined.
•
IPv6 related structures (
sockaddr_in6
) do not need to be compensated for, so it is only
removed.
Design Decision:
SODAQ’s NB-IoT API allows only for IPv4 networking, not IPv6.
After making these changes, the
session_t
structure looks as shown in following code snippet:
typedef
struct
{
// socklen_t size;
uint32_t
size;
//Added for SODAQ to hold length of the socket
union
{
//struct sockaddr sa;
// struct sockaddr_storage st;
//struct sockaddr_in sin;
char
* remoteIpStr;
//Added for SODAQ to hold remote IP
uint16_t
port;
//Added for SODAQ to hold remote port
//struct sockaddr_in6 sin6;
} addr;
uint8_t
ifindex;
} session_t;
56
‘Sessions’ API defines the following functions. The purpose of the functionalities has
been described in the comments.
* Initializes the session_t object to its default values */
void
dtls_session_init(session_t *sess);
/*
Compares the given session objects. /
int
_dtls_address_equals_impl(
const
session_t *a,
const
session_t *b);
Between these functions,
_dtls_address_equals_impl()
depends on structures from
socket programming API.
int
_dtls_address_equals_impl(
const
session_t *a,
const
session_t *b)
{
/*It starts by checking whether the inequality between session_t a and
session_t b can be established by checking comparing ifindex, size,
sa.sa_family variables in each variable */
if
( a->ifindex != b->ifindex ||
a->size != b->size ||
a->addr.
sa.sa_family
!= b->addr.
sa.sa_family
)
return
0
;
/*Then, it proceeds to determine the address family first and later compare
individual variables and contents to determine whether the sessions are equal
or not. Again, it uses the variables of UNIX-dependent structures, as
follows:*/
switch
(a->addr.
sa.sa_family
) {
case
AF_INET:
/* Case IPv4*/
return
a->addr.
sin.sin_port
== b->addr.
sin.sin_port
/* Compare if ports are
equal*/
&&
memcmp(&a->addr.sin.
sin_addr
,
&b->addr.sin.
sin_addr
,
sizeof
(
struct
in_addr)
) ==
0
;
/* Compare if the addresses are lexicographically equal */
case
AF_INET6:
/* Case IPv6 */
return
a->addr.
sin6.sin6_port
== b->addr.
sin6.sin6_port
/* Compare if ports
are equal*/
&&
memcmp(&a->addr.
sin6.sin6_addr
, &b->addr.
sin6.sin6_addr
,
sizeof
(
struct
in6_addr)) ==
0
;
/* Compare if the addresses are
lexicographically equal */
}
return
0
;
}
57
Do'stlaringiz bilan baham: |