tinydtls  0.8.6
session.c
Go to the documentation of this file.
1 /*******************************************************************************
2  *
3  * Copyright (c) 2011, 2012, 2013, 2014, 2015 Olaf Bergmann (TZI) and others.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
7  *
8  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
9  * and the Eclipse Distribution License is available at
10  * http://www.eclipse.org/org/documents/edl-v10.php.
11  *
12  * Contributors:
13  * Olaf Bergmann - initial API and implementation
14  *
15  *******************************************************************************/
16 
17 #include "session.h"
18 
19 #ifdef HAVE_ASSERT_H
20 #include <assert.h>
21 #else
22 #ifndef assert
23 #warning "assertions are disabled"
24 # define assert(x)
25 #endif
26 #endif
27 
28 #ifdef WITH_CONTIKI
29 #define _dtls_address_equals_impl(A,B) \
30  ((A)->size == (B)->size \
31  && (A)->port == (B)->port \
32  && uip_ipaddr_cmp(&((A)->addr),&((B)->addr)) \
33  && (A)->ifindex == (B)->ifindex)
34 
35 #else /* WITH_CONTIKI */
36 
37 static inline int
39  const session_t *b) {
40  if (a->ifindex != b->ifindex ||
41  a->size != b->size || a->addr.sa.sa_family != b->addr.sa.sa_family)
42  return 0;
43 
44  /* need to compare only relevant parts of sockaddr_in6 */
45  switch (a->addr.sa.sa_family) {
46  case AF_INET:
47  return
48  a->addr.sin.sin_port == b->addr.sin.sin_port &&
49  memcmp(&a->addr.sin.sin_addr, &b->addr.sin.sin_addr,
50  sizeof(struct in_addr)) == 0;
51  case AF_INET6:
52  return a->addr.sin6.sin6_port == b->addr.sin6.sin6_port &&
53  memcmp(&a->addr.sin6.sin6_addr, &b->addr.sin6.sin6_addr,
54  sizeof(struct in6_addr)) == 0;
55  default: /* fall through and signal error */
56  ;
57  }
58  return 0;
59 }
60 #endif /* WITH_CONTIKI */
61 
62 void
64  assert(sess);
65  memset(sess, 0, sizeof(session_t));
66  sess->size = sizeof(sess->addr);
67 }
68 
69 int
71  assert(a); assert(b);
72  return _dtls_address_equals_impl(a, b);
73 }
int dtls_session_equals(const session_t *a, const session_t *b)
Definition: session.c:70
void dtls_session_init(session_t *sess)
Definition: session.c:63
socklen_t size
Definition: session.h:41
#define assert(x)
Definition: hmac.c:25
static int _dtls_address_equals_impl(const session_t *a, const session_t *b)
Definition: session.c:38
struct sockaddr sa
Definition: session.h:43
uint8_t ifindex
Definition: session.h:48
union session_t::@2 addr
struct sockaddr_in6 sin6
Definition: session.h:46
struct sockaddr_in sin
Definition: session.h:45