iotivity 0.9.0
[platform/upstream/iotivity.git] / resource / csdk / libcoap-4.1.1 / examples / rd.c
1 /* coap -- simple implementation of the Constrained Application Protocol (CoAP)
2  *         as defined in draft-ietf-core-coap
3  *
4  * Copyright (C) 2010--2013 Olaf Bergmann <bergmann@tzi.org>
5  *
6  * This file is part of the CoAP library libcoap. Please see
7  * README for terms of use. 
8  */
9
10
11 /**
12  * @file rd.c
13  * @brief CoRE resource directory
14  *
15  * @see http://tools.ietf.org/id/draft-shelby-core-resource-directory
16  */
17
18 #include <string.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <stdio.h>
22 #include <ctype.h>
23 #include <sys/select.h>
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include <netdb.h>
29 #include <sys/stat.h>
30 #include <dirent.h>
31 #include <errno.h>
32 #include <signal.h>
33
34 #include "config.h"
35 #include "utlist.h"
36 #include "resource.h"
37 #include "coap.h"
38
39 #define COAP_RESOURCE_CHECK_TIME 2
40
41 #define RD_ROOT_STR   ((unsigned char *)"rd")
42 #define RD_ROOT_SIZE  2
43
44 #ifndef min
45 #define min(a,b) ((a) < (b) ? (a) : (b))
46 #endif
47
48 typedef struct rd_t {
49   UT_hash_handle hh;    /**< hash handle (for internal use only) */
50   coap_key_t key;       /**< the actual key bytes for this resource */
51
52   size_t etag_len;              /**< actual length of @c etag */
53   unsigned char etag[8];        /**< ETag for current description */
54
55   str data;                     /**< points to the resource description  */
56 } rd_t;
57
58 rd_t *resources = NULL;
59
60 inline rd_t *
61 rd_new() {
62   rd_t *rd;
63   rd = (rd_t *)coap_malloc(sizeof(rd_t));
64   if (rd)
65     memset(rd, 0, sizeof(rd_t));
66
67   return rd;
68 }       
69
70 inline void
71 rd_delete(rd_t *rd) {
72   if (rd) {
73     coap_free(rd->data.s);
74     coap_free(rd);
75   }
76 }
77
78 /* temporary storage for dynamic resource representations */
79 static int quit = 0;
80
81 /* SIGINT handler: set quit to 1 for graceful termination */
82 void
83 handle_sigint(int signum) {
84   quit = 1;
85 }
86
87 void 
88 hnd_get_resource(coap_context_t  *ctx, struct coap_resource_t *resource, 
89               coap_address_t *peer, coap_pdu_t *request, str *token,
90               coap_pdu_t *response) {
91   rd_t *rd = NULL;
92   unsigned char buf[3];
93   
94   HASH_FIND(hh, resources, resource->key, sizeof(coap_key_t), rd);
95
96   response->hdr->code = COAP_RESPONSE_CODE(205);
97
98   coap_add_option(response, COAP_OPTION_CONTENT_TYPE,
99           coap_encode_var_bytes(buf, COAP_MEDIATYPE_APPLICATION_LINK_FORMAT), buf);
100
101   if (rd && rd->etag_len)
102     coap_add_option(response, COAP_OPTION_ETAG, rd->etag_len, rd->etag);
103
104   if (rd && rd->data.s)
105     coap_add_data(response, rd->data.length, rd->data.s);
106 }
107
108 void 
109 hnd_put_resource(coap_context_t  *ctx, struct coap_resource_t *resource, 
110                  coap_address_t *peer, coap_pdu_t *request, str *token,
111                  coap_pdu_t *response) {
112 #if 1
113   response->hdr->code = COAP_RESPONSE_CODE(501);
114 #else /* FIXME */
115   coap_opt_iterator_t opt_iter;
116   coap_opt_t *token, *etag;
117   coap_pdu_t *response;
118   size_t size = sizeof(coap_hdr_t);
119   int type = (request->hdr->type == COAP_MESSAGE_CON) 
120     ? COAP_MESSAGE_ACK : COAP_MESSAGE_NON;
121   rd_t *rd = NULL;
122   unsigned char code;           /* result code */
123   unsigned char *data;
124   str tmp;
125
126   HASH_FIND(hh, resources, resource->key, sizeof(coap_key_t), rd);
127   if (rd) {
128     /* found resource object, now check Etag */
129     etag = coap_check_option(request, COAP_OPTION_ETAG, &opt_iter);
130     if (!etag || (COAP_OPT_LENGTH(etag) != rd->etag_len)
131         || memcmp(COAP_OPT_VALUE(etag), rd->etag, rd->etag_len) != 0) {
132       
133       if (coap_get_data(request, &tmp.length, &data)) {
134
135         tmp.s = (unsigned char *)coap_malloc(tmp.length);
136         if (!tmp.s) {
137           debug("hnd_put_rd: cannot allocate storage for new rd\n");
138           code = COAP_RESPONSE_CODE(503);
139           goto finish;
140         }
141
142         coap_free(rd->data.s);
143         rd->data.s = tmp.s;
144         rd->data.length = tmp.length;
145         memcpy(rd->data.s, data, rd->data.length);
146       }
147     }
148
149     if (etag) {
150       rd->etag_len = min(COAP_OPT_LENGTH(etag), sizeof(rd->etag));
151       memcpy(rd->etag, COAP_OPT_VALUE(etag), rd->etag_len);
152     }
153
154     code = COAP_RESPONSE_CODE(204);
155     /* FIXME: update lifetime */
156     
157     } else {
158     
159     code = COAP_RESPONSE_CODE(503);
160   }
161
162   finish:
163   /* FIXME: do not create a new response but use the old one instead */
164   response = coap_pdu_init(type, code, request->hdr->id, size);
165
166   if (!response) {
167     debug("cannot create response for message %d\n", request->hdr->id);
168     return;
169   }
170
171   if (request->hdr->token_length)
172     coap_add_token(response, request->hdr->token_length, request->hdr->token);
173
174   if (coap_send(ctx, peer, response) == COAP_INVALID_TID) {
175     debug("hnd_get_rd: cannot send response for message %d\n", 
176           request->hdr->id);
177   }
178   coap_delete_pdu(response);
179 #endif
180 }
181
182 void 
183 hnd_delete_resource(coap_context_t  *ctx, struct coap_resource_t *resource, 
184                     coap_address_t *peer, coap_pdu_t *request, str *token,
185                     coap_pdu_t *response) {
186   rd_t *rd = NULL;
187
188   HASH_FIND(hh, resources, resource->key, sizeof(coap_key_t), rd);
189   if (rd) {
190     HASH_DELETE(hh, resources, rd);
191     rd_delete(rd);
192   }
193   /* FIXME: link attributes for resource have been created dynamically
194    * using coap_malloc() and must be released. */
195   coap_delete_resource(ctx, resource->key);
196
197   response->hdr->code = COAP_RESPONSE_CODE(202);
198 }
199
200 void 
201 hnd_get_rd(coap_context_t  *ctx, struct coap_resource_t *resource, 
202               coap_address_t *peer, coap_pdu_t *request, str *token,
203               coap_pdu_t *response) {
204   unsigned char buf[3];
205
206   response->hdr->code = COAP_RESPONSE_CODE(205);
207
208   coap_add_option(response, COAP_OPTION_CONTENT_TYPE,
209           coap_encode_var_bytes(buf, COAP_MEDIATYPE_APPLICATION_LINK_FORMAT), buf);
210
211   coap_add_option(response, COAP_OPTION_MAXAGE,
212           coap_encode_var_bytes(buf, 0x2ffff), buf);
213 }
214
215 int
216 parse_param(unsigned char *search, size_t search_len,
217             unsigned char *data, size_t data_len, str *result) {
218
219   if (result)
220     memset(result, 0, sizeof(str));
221
222   if (!search_len) 
223     return 0;
224   
225   while (search_len <= data_len) {
226
227     /* handle parameter if found */
228     if (memcmp(search, data, search_len) == 0) {
229       data += search_len;
230       data_len -= search_len;
231
232       /* key is only valid if we are at end of string or delimiter follows */
233       if (!data_len || *data == '=' || *data == '&') {
234         while (data_len && *data != '=') {
235           ++data; --data_len;
236         }
237       
238         if (data_len > 1 && result) {
239           /* value begins after '=' */
240           
241           result->s = ++data;
242           while (--data_len && *data != '&') {
243             ++data; result->length++;
244           }
245         }
246         
247         return 1;
248       }
249     }
250
251     /* otherwise proceed to next */
252     while (--data_len && *data++ != '&')
253       ;
254   }
255   
256   return 0;
257 }
258
259 void
260 add_source_address(struct coap_resource_t *resource, coap_address_t *peer) {
261 #define BUFSIZE 64
262   char *buf;
263   size_t n = 1;
264   
265   buf = (char *)coap_malloc(BUFSIZE);
266   if (!buf)    
267     return;
268
269   buf[0] = '"';
270
271   switch(peer->addr.sa.sa_family) {
272
273   case AF_INET:
274     /* FIXME */
275     break;
276
277   case AF_INET6:
278     n += snprintf(buf + n, BUFSIZE - n,
279                   "[%02x%02x:%02x%02x:%02x%02x:%02x%02x"        \
280                   ":%02x%02x:%02x%02x:%02x%02x:%02x%02x]",
281                   peer->addr.sin6.sin6_addr.s6_addr[0],
282                   peer->addr.sin6.sin6_addr.s6_addr[1],
283                   peer->addr.sin6.sin6_addr.s6_addr[2],
284                   peer->addr.sin6.sin6_addr.s6_addr[3],
285                   peer->addr.sin6.sin6_addr.s6_addr[4],
286                   peer->addr.sin6.sin6_addr.s6_addr[5],
287                   peer->addr.sin6.sin6_addr.s6_addr[6],
288                   peer->addr.sin6.sin6_addr.s6_addr[7],
289                   peer->addr.sin6.sin6_addr.s6_addr[8],
290                   peer->addr.sin6.sin6_addr.s6_addr[9],
291                   peer->addr.sin6.sin6_addr.s6_addr[10],
292                   peer->addr.sin6.sin6_addr.s6_addr[11],
293                   peer->addr.sin6.sin6_addr.s6_addr[12],
294                   peer->addr.sin6.sin6_addr.s6_addr[13],
295                   peer->addr.sin6.sin6_addr.s6_addr[14],
296                   peer->addr.sin6.sin6_addr.s6_addr[15]);    
297     
298     if (peer->addr.sin6.sin6_port != htons(COAP_DEFAULT_PORT)) {
299       n += 
300         snprintf(buf + n, BUFSIZE - n, ":%d", peer->addr.sin6.sin6_port);
301     }
302     break;
303   default:
304     ;
305   }
306
307   if (n < BUFSIZE)
308     buf[n++] = '"';
309
310   coap_add_attr(resource, (unsigned char *)"A", 1, (unsigned char *)buf, n, COAP_ATTR_FLAGS_RELEASE_VALUE);
311 #undef BUFSIZE
312 }
313
314
315 rd_t *
316 make_rd(coap_address_t *peer, coap_pdu_t *pdu) {    
317   rd_t *rd;
318   unsigned char *data;
319   coap_opt_iterator_t opt_iter;
320   coap_opt_t *etag;
321
322   rd = rd_new();
323   
324   if (!rd) {
325     debug("hnd_get_rd: cannot allocate storage for rd\n");
326     return NULL;
327   }
328
329   if (coap_get_data(pdu, &rd->data.length, &data)) {
330     rd->data.s = (unsigned char *)coap_malloc(rd->data.length);
331     if (!rd->data.s) {
332       debug("hnd_get_rd: cannot allocate storage for rd->data\n");
333       rd_delete(rd);
334       return NULL;
335     }
336     memcpy(rd->data.s, data, rd->data.length);
337   }
338
339   etag = coap_check_option(pdu, COAP_OPTION_ETAG, &opt_iter);
340   if (etag) {
341     rd->etag_len = min(COAP_OPT_LENGTH(etag), sizeof(rd->etag));
342     memcpy(rd->etag, COAP_OPT_VALUE(etag), rd->etag_len);
343   }
344
345   return rd;
346 }
347
348 void 
349 hnd_post_rd(coap_context_t  *ctx, struct coap_resource_t *resource, 
350               coap_address_t *peer, coap_pdu_t *request, str *token,
351               coap_pdu_t *response) {
352   coap_resource_t *r;
353   coap_opt_iterator_t opt_iter;
354   coap_opt_t *query;
355 #define LOCSIZE 68
356   unsigned char *loc;
357   size_t loc_size;
358   str h = {0, NULL}, ins = {0, NULL}, rt = {0, NULL}, lt = {0, NULL};           /* store query parameters */
359   unsigned char *buf;
360
361   loc = (unsigned char *)coap_malloc(LOCSIZE);
362   if (!loc) {
363     response->hdr->code = COAP_RESPONSE_CODE(500);
364     return;
365   }
366   memcpy(loc, RD_ROOT_STR, RD_ROOT_SIZE);
367
368   loc_size = RD_ROOT_SIZE;
369   loc[loc_size++] = '/';
370   
371   /* store query parameters for later use */
372   query = coap_check_option(request, COAP_OPTION_URI_QUERY, &opt_iter);
373   if (query) {
374     parse_param((unsigned char *)"h", 1, 
375                 COAP_OPT_VALUE(query), COAP_OPT_LENGTH(query), &h);
376     parse_param((unsigned char *)"ins", 3, 
377                 COAP_OPT_VALUE(query), COAP_OPT_LENGTH(query), &ins);
378     parse_param((unsigned char *)"lt", 2, 
379                 COAP_OPT_VALUE(query), COAP_OPT_LENGTH(query), &lt);
380     parse_param((unsigned char *)"rt", 2, 
381                 COAP_OPT_VALUE(query), COAP_OPT_LENGTH(query), &rt);
382   } 
383   
384   if (h.length) {               /* client has specified a node name */
385     memcpy(loc + loc_size, h.s, min(h.length, LOCSIZE - loc_size - 1));
386     loc_size += min(h.length, LOCSIZE - loc_size - 1);
387
388     if (ins.length && loc_size > 1) {
389       loc[loc_size++] = '-';
390       memcpy((char *)(loc + loc_size), 
391              ins.s, min(ins.length, LOCSIZE - loc_size - 1));
392       loc_size += min(ins.length, LOCSIZE - loc_size - 1);
393     }
394  
395   } else {                      /* generate node identifier */
396     loc_size += 
397       snprintf((char *)(loc + loc_size), LOCSIZE - loc_size - 1, 
398                "%x", request->hdr->id);
399     
400     if (loc_size > 1) {
401       if (ins.length) {
402         loc[loc_size++] = '-';
403         memcpy((char *)(loc + loc_size), 
404                ins.s, min(ins.length, LOCSIZE - loc_size - 1));
405         loc_size += min(ins.length, LOCSIZE - loc_size - 1);
406       } else {
407         coap_tick_t now;
408         coap_ticks(&now);
409         
410         loc_size += 
411           snprintf((char *)(loc + loc_size), LOCSIZE - loc_size - 1, 
412                    "-%x", now);
413       }
414     }
415   }
416
417   /* TODO:
418    *   - use lt to check expiration
419    */
420   
421   r = coap_resource_init(loc, loc_size, COAP_RESOURCE_FLAGS_RELEASE_URI);
422   coap_register_handler(r, COAP_REQUEST_GET, hnd_get_resource);
423   coap_register_handler(r, COAP_REQUEST_PUT, hnd_put_resource);
424   coap_register_handler(r, COAP_REQUEST_DELETE, hnd_delete_resource);
425
426   if (ins.s) {
427     buf = (unsigned char *)coap_malloc(ins.length + 2);
428     if (buf) {
429       /* add missing quotes */
430       buf[0] = '"';
431       memcpy(buf + 1, ins.s, ins.length);
432       buf[ins.length + 1] = '"';
433       coap_add_attr(r, (unsigned char *)"ins", 3, buf, ins.length + 2, COAP_ATTR_FLAGS_RELEASE_VALUE);
434     }
435   }
436
437   if (rt.s) {
438     buf = (unsigned char *)coap_malloc(rt.length + 2);
439     if (buf) {
440       /* add missing quotes */
441       buf[0] = '"';
442       memcpy(buf + 1, rt.s, rt.length);
443       buf[rt.length + 1] = '"';
444       coap_add_attr(r, (unsigned char *)"rt", 2, buf, rt.length + 2, COAP_ATTR_FLAGS_RELEASE_VALUE);
445     }
446   }
447
448   add_source_address(r, peer);
449
450   {
451     rd_t *rd;
452     rd = make_rd(peer, request);
453     if (rd) {
454       coap_hash_path(loc, loc_size, rd->key);
455       HASH_ADD(hh, resources, key, sizeof(coap_key_t), rd);
456     } else {
457       /* FIXME: send error response and delete r */
458     }
459   }
460
461   coap_add_resource(ctx, r);
462
463
464   /* create response */
465
466   response->hdr->code = COAP_RESPONSE_CODE(201);
467
468   { /* split path into segments and add Location-Path options */
469     unsigned char _b[LOCSIZE];
470     unsigned char *b = _b;
471     size_t buflen = sizeof(_b);
472     int nseg;
473     
474     nseg = coap_split_path(loc, loc_size, b, &buflen);
475     while (nseg--) {
476       coap_add_option(response, COAP_OPTION_LOCATION_PATH,
477                       COAP_OPT_LENGTH(b), COAP_OPT_VALUE(b));
478       b += COAP_OPT_SIZE(b);
479     }
480   }
481 }
482
483 void
484 init_resources(coap_context_t *ctx) {
485   coap_resource_t *r;
486
487   r = coap_resource_init(RD_ROOT_STR, RD_ROOT_SIZE, 0);
488   coap_register_handler(r, COAP_REQUEST_GET, hnd_get_rd);
489   coap_register_handler(r, COAP_REQUEST_POST, hnd_post_rd);
490
491   coap_add_attr(r, (unsigned char *)"ct", 2, (unsigned char *)"40", 2, 0);
492   coap_add_attr(r, (unsigned char *)"rt", 2, (unsigned char *)"\"core-rd\"", 9, 0);
493   coap_add_attr(r, (unsigned char *)"ins", 2, (unsigned char *)"\"default\"", 9, 0);
494
495   coap_add_resource(ctx, r);
496
497 }
498
499 void
500 usage( const char *program, const char *version) {
501   const char *p;
502
503   p = strrchr( program, '/' );
504   if ( p )
505     program = ++p;
506
507   fprintf( stderr, "%s v%s -- CoRE Resource Directory implementation\n"
508            "(c) 2011-2012 Olaf Bergmann <bergmann@tzi.org>\n\n"
509            "usage: %s [-A address] [-p port]\n\n"
510            "\t-A address\tinterface address to bind to\n"
511            "\t-p port\t\tlisten on specified port\n"
512            "\t-v num\t\tverbosity level (default: 3)\n",
513            program, version, program );
514 }
515
516 coap_context_t *
517 get_context(const char *node, const char *port) {
518   coap_context_t *ctx = NULL;  
519   int s;
520   struct addrinfo hints;
521   struct addrinfo *result, *rp;
522
523   memset(&hints, 0, sizeof(struct addrinfo));
524   hints.ai_family = AF_UNSPEC;    /* Allow IPv4 or IPv6 */
525   hints.ai_socktype = SOCK_DGRAM; /* Coap uses UDP */
526   hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
527   
528   s = getaddrinfo(node, port, &hints, &result);
529   if ( s != 0 ) {
530     fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
531     return NULL;
532   } 
533
534   /* iterate through results until success */
535   for (rp = result; rp != NULL; rp = rp->ai_next) {
536     coap_address_t addr;
537
538     if (rp->ai_addrlen <= sizeof(addr.addr)) {
539       coap_address_init(&addr);
540       addr.size = rp->ai_addrlen;
541       memcpy(&addr.addr, rp->ai_addr, rp->ai_addrlen);
542
543       ctx = coap_new_context(&addr);
544       if (ctx) {
545         /* TODO: output address:port for successful binding */
546         goto finish;
547       }
548     }
549   }
550   
551   fprintf(stderr, "no context available for interface '%s'\n", node);
552
553  finish:
554   freeaddrinfo(result);
555   return ctx;
556 }
557
558 int
559 join(coap_context_t *ctx, char *group_name) {
560   struct ipv6_mreq mreq;
561   struct addrinfo   *reslocal = NULL, *resmulti = NULL, hints, *ainfo;
562   int result = -1;
563
564   /* we have to resolve the link-local interface to get the interface id */
565   memset(&hints, 0, sizeof(hints));
566   hints.ai_family = AF_INET6;
567   hints.ai_socktype = SOCK_DGRAM;
568
569   result = getaddrinfo("::", NULL, &hints, &reslocal);
570   if ( result < 0 ) {
571     perror("join: cannot resolve link-local interface");
572     goto finish;
573   }
574
575   /* get the first suitable interface identifier */
576   for (ainfo = reslocal; ainfo != NULL; ainfo = ainfo->ai_next) {
577     if ( ainfo->ai_family == AF_INET6 ) {
578       mreq.ipv6mr_interface =
579               ((struct sockaddr_in6 *)ainfo->ai_addr)->sin6_scope_id;
580       break;
581     }
582   }
583
584   memset(&hints, 0, sizeof(hints));
585   hints.ai_family = AF_INET6;
586   hints.ai_socktype = SOCK_DGRAM;
587
588   /* resolve the multicast group address */
589   result = getaddrinfo(group_name, NULL, &hints, &resmulti);
590
591   if ( result < 0 ) {
592     perror("join: cannot resolve multicast address");
593     goto finish;
594   }
595
596   for (ainfo = resmulti; ainfo != NULL; ainfo = ainfo->ai_next) {
597     if ( ainfo->ai_family == AF_INET6 ) {
598       mreq.ipv6mr_multiaddr =
599         ((struct sockaddr_in6 *)ainfo->ai_addr)->sin6_addr;
600       break;
601     }
602   }
603
604   result = setsockopt( ctx->sockfd, IPPROTO_IPV6, IPV6_JOIN_GROUP,
605                        (char *)&mreq, sizeof(mreq) );
606   if ( result < 0 )
607     perror("join: setsockopt");
608
609  finish:
610   freeaddrinfo(resmulti);
611   freeaddrinfo(reslocal);
612
613   return result;
614 }
615
616 int
617 main(int argc, char **argv) {
618   coap_context_t  *ctx;
619   fd_set readfds;
620   struct timeval tv, *timeout;
621   int result;
622   coap_tick_t now;
623   coap_queue_t *nextpdu;
624   char addr_str[NI_MAXHOST] = "::";
625   char port_str[NI_MAXSERV] = "5683";
626   char *group = NULL;
627   int opt;
628   coap_log_t log_level = LOG_WARNING;
629
630   while ((opt = getopt(argc, argv, "A:g:p:v:")) != -1) {
631     switch (opt) {
632     case 'A' :
633       strncpy(addr_str, optarg, NI_MAXHOST-1);
634       addr_str[NI_MAXHOST - 1] = '\0';
635       break;
636     case 'g' :
637       group = optarg;
638       break;
639     case 'p' :
640       strncpy(port_str, optarg, NI_MAXSERV-1);
641       port_str[NI_MAXSERV - 1] = '\0';
642       break;
643     case 'v' :
644       log_level = strtol(optarg, NULL, 10);
645       break;
646     default:
647       usage( argv[0], PACKAGE_VERSION );
648       exit( 1 );
649     }
650   }
651
652   coap_set_log_level(log_level);
653
654   ctx = get_context(addr_str, port_str);
655   if (!ctx)
656     return -1;
657
658   if (group)
659     join(ctx, group);
660
661   init_resources(ctx);
662
663   signal(SIGINT, handle_sigint);
664
665   while ( !quit ) {
666     FD_ZERO(&readfds);
667     FD_SET( ctx->sockfd, &readfds );
668
669     nextpdu = coap_peek_next( ctx );
670
671     coap_ticks(&now);
672     while ( nextpdu && nextpdu->t <= now ) {
673       coap_retransmit( ctx, coap_pop_next( ctx ) );
674       nextpdu = coap_peek_next( ctx );
675     }
676
677     if ( nextpdu && nextpdu->t <= now + COAP_RESOURCE_CHECK_TIME ) {
678       /* set timeout if there is a pdu to send before our automatic timeout occurs */
679       tv.tv_usec = ((nextpdu->t - now) % COAP_TICKS_PER_SECOND) * 1000000 / COAP_TICKS_PER_SECOND;
680       tv.tv_sec = (nextpdu->t - now) / COAP_TICKS_PER_SECOND;
681       timeout = &tv;
682     } else {
683       tv.tv_usec = 0;
684       tv.tv_sec = COAP_RESOURCE_CHECK_TIME;
685       timeout = &tv;
686     }
687     result = select( FD_SETSIZE, &readfds, 0, 0, timeout );
688
689     if ( result < 0 ) {         /* error */
690       if (errno != EINTR)
691         perror("select");
692     } else if ( result > 0 ) {  /* read from socket */
693       if ( FD_ISSET( ctx->sockfd, &readfds ) ) {
694         coap_read( ctx );       /* read received data */
695         coap_dispatch( ctx );   /* and dispatch PDUs from receivequeue */
696       }
697     } else {                    /* timeout */
698       /* coap_check_resource_list( ctx ); */
699     }
700   }
701
702   coap_free_context( ctx );
703
704   return 0;
705 }