Contiki 2.5
eid.c
Go to the documentation of this file.
1 /**
2  * \addtogroup agent
3  * @{
4  */
5 
6 /**
7  * \file
8  *
9  * \author Wolf-Bastian Pöttner <poettner@ibr.cs.tu-bs.de>
10  */
11 
12 #include <string.h>
13 #include <stdlib.h>
14 
15 #include "contiki.h"
16 #include "logging.h"
17 
18 #include "agent.h"
19 #include "sdnv.h"
20 
21 #include "eid.h"
22 
23 #define BUFFER_LENGTH 100
24 
25 int eid_parse_host(char * buffer, uint8_t length, uint32_t * node_id)
26 {
27  /* Do we have an ipn scheme? */
28  if( strncmp(buffer, "ipn:", 4) != 0 ) {
29  LOG(LOGD_DTN, LOG_AGENT, LOGL_WRN, "Unknown EID format %s", &buffer[4]);
30  return -1;
31  }
32 
33  /* Extract the node part of the EID */
34  *node_id = atoi(&buffer[4]);
35 
36  return strlen(buffer);
37 }
38 
39 int eid_parse_host_length(uint8_t * buffer, uint8_t length, uint32_t * node_id)
40 {
41  uint32_t eid_length;
42  int sdnv_length = 0;
43  int ret = 0;
44 
45  /* Recover the EID length */
46  sdnv_length = sdnv_decode(buffer, length, &eid_length);
47  if (sdnv_length < 0) {
48  LOG(LOGD_DTN, LOG_AGENT, LOGL_WRN, "Unknown EID format %s", &buffer[4]);
49  return -1;
50  }
51 
52  /* Parse the EID */
53  ret = eid_parse_host((char *) &buffer[sdnv_length], length - sdnv_length, node_id);
54  if( ret < 0 ) {
55  LOG(LOGD_DTN, LOG_AGENT, LOGL_WRN, "Unknown EID format %s", &buffer[4]);
56  return -1;
57  }
58 
59  return sdnv_length + ret;
60 }
61 
62 int eid_parse_full(char * buffer, uint8_t length, uint32_t * node_id, uint32_t * service_id)
63 {
64  char * delimeter = NULL;
65  int result;
66 
67  /* Do we have an ipn scheme? */
68  if( strncmp(buffer, "ipn:", 4) != 0 ) {
69  LOG(LOGD_DTN, LOG_AGENT, LOGL_WRN, "Unknown EID format %s", &buffer[4]);
70  return -1;
71  }
72 
73  /* Go and search for the . delimeter */
74  delimeter = strchr(buffer, '.');
75  if( delimeter == NULL ) {
76  LOG(LOGD_DTN, LOG_AGENT, LOGL_WRN, "Unknown EID format %s", &buffer[4]);
77  return -1;
78  }
79 
80  /* Make this two seperate strings */
81  *delimeter = 0;
82  delimeter ++;
83 
84  /* Now parse host part */
85  *node_id = atoi(&buffer[4]);
86 
87  /* And service part */
88  *service_id = atoi(delimeter);
89 
90  result = strlen(buffer) + strlen(delimeter) + 1;
91 
92  /* Reconstruct the original string */
93  *(delimeter-1) = '.';
94 
95  return result;
96 }
97 
98 
99 int eid_parse_full_length(uint8_t * buffer, uint8_t length, uint32_t * node_id, uint32_t * service_id)
100 {
101  uint32_t eid_length;
102  int sdnv_length = 0;
103  int ret = 0;
104 
105  /* Recover the EID length */
106  sdnv_length = sdnv_decode(buffer, length, &eid_length);
107  if (sdnv_length < 0) {
108  LOG(LOGD_DTN, LOG_AGENT, LOGL_WRN, "Unknown EID format %s", &buffer[4]);
109  return -1;
110  }
111 
112  ret = eid_parse_full((char *) &buffer[sdnv_length], length - sdnv_length, node_id, service_id);
113  if( ret < 0 ) {
114  LOG(LOGD_DTN, LOG_AGENT, LOGL_WRN, "Unknown EID format %s", &buffer[4]);
115  return -1;
116  }
117 
118  return sdnv_length + ret;
119 }
120 
121 int eid_create_host(uint32_t node_id, char * buffer, uint8_t length)
122 {
123  return sprintf(buffer, "ipn:%lu", node_id);
124 }
125 
126 int eid_create_host_length(uint32_t node_id, uint8_t * buffer, uint8_t length)
127 {
128  char string_buffer[BUFFER_LENGTH];
129  int ret = 0;
130  int eid_length = 0;
131 
132  /* Encode EID */
133  eid_length = eid_create_host(node_id, string_buffer, BUFFER_LENGTH);
134  if( eid_length < 0 ) {
135  return eid_length;
136  }
137 
138  /* Encode length of EID */
139  ret = sdnv_encode(eid_length, (uint8_t *) buffer, length);
140  if (ret < 0) {
141  return -1;
142  }
143 
144  /* Check if we have enough buffer left */
145  if( length - ret < eid_length ) {
146  return -1;
147  }
148 
149  /* And then append the EID string */
150  memcpy(buffer + ret, string_buffer, eid_length);
151 
152  return ret + eid_length;
153 }
154 
155 int eid_create_full(uint32_t node_id, uint32_t service_id, char * buffer, uint8_t length)
156 {
157  return sprintf(buffer, "ipn:%lu.%lu", node_id, service_id);
158 }
159 
160 int eid_create_full_length(uint32_t node_id, uint32_t service_id, uint8_t * buffer, uint8_t length)
161 {
162  char string_buffer[BUFFER_LENGTH];
163  int ret = 0;
164  int eid_length = 0;
165 
166  /* Encode EID */
167  eid_length = eid_create_full(node_id, service_id, string_buffer, BUFFER_LENGTH);
168  if( eid_length < 0 ) {
169  return eid_length;
170  }
171 
172  /* Encode length of EID */
173  ret = sdnv_encode(eid_length, (uint8_t *) buffer, length);
174  if (ret < 0) {
175  return -1;
176  }
177 
178  /* Check if we have enough buffer left */
179  if( length - ret < eid_length ) {
180  return -1;
181  }
182 
183  /* And then append the EID string */
184  memcpy(buffer + ret, string_buffer, eid_length);
185 
186  return ret + eid_length;
187 }