Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @todo Asenkron ve blocking mod için linda ve client tarafı eş zamanlı dinlenecek
- * @todo PoA optionları bind ve parse edilecek
- * @todo Doxy etiketleri yazılacak
- * @todo Gerekli commentler girilecek
- * @todo Fonksiyon dönüşlerinde gerekli return checkleri konacak
- * @todo Eski loglar silinecek ve yeni loglar (DEBUG, Info ve ERR ) eklenecek
- * @todo Lindaya atılıtken kullanılan Linda_put processi, linda_proc.c altındaki fonsiyon kullanılarak yapılacak
- * @todo Filte criteria içindeki cur_node1 her optiondan sonra aramak yerine, while döngüsüne bağlanacak
- * @todo State saklama yapılacak
- * @todo Signal yedikten sonra yapılacak işlemler tekrar düzenlenecek
- * @todo Protocol check yapılarak error code dönülecek client tarafına
- * @todo Request exp_time kontrol edilecek ve paket işlenmeden düşürme işlemi gerekirse response tarafı ayarlanacak
- * @todo 12 numalaralı Content type option ı kontrol edilerek Json veya XML ise ona göre fonksiyolar yazılacak ve cağırılacak
- * @todo Xmltoflat yerine Branching modülüne gönderim yapılacak
- * @todo Değişken tanımları ve yerleri gözden geçirilecek
- * @todo Mollac ve free işlemleri gözden geçirilecek
- * @todo Request function operation switch-case yapılacak, default deger olarak ERR donulecek
- * @todo Print_coap_msge fonksiyoni içindeki printfler sonunda "\n" olmayan log makroları ile değiştirilecek
- * @todo xml ve json mesaj işleme bölümü ayrı bir dosyaya alınacak
- * @todo xml ve json mesaj build işlemleri ayrı bir dosyaya alınacak ve include olarak eklenecek
- *
- * @brief Source file for the FreeCoAP server library
- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdint.h>
- #include <time.h>
- #include <errno.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <dirent.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <arpa/inet.h>
- #include <sys/select.h>
- #include <sys/timerfd.h>
- #include <linux/types.h>
- #include <stdbool.h>
- #include "coap_server.h"
- #include "coap_log.h"
- #include <glib.h>
- #include <libxml/parser.h>
- #include <libxml/tree.h>
- #include <net/if.h>
- #include <signal.h>
- #include "../linda/m2m_client.h"
- #include "resources.h"
- #include "linda_proc.h"
- #include "flatten.h"
- #include "cell_data.h"
- #include "xml_flat.h"
- #include <glib-object.h>
- #include <json-glib/json-glib.h>
- #include "json_flat.h"
- #include "struct_send.h"
- #include <time.h>
- #include "msg_parser.h"
- #include "isValid.h"
- #define COAP_SERVER_ACK_TIMEOUT_SEC 2 /**< Minimum delay to wait before retransmitting a confirmable message */
- #define COAP_SERVER_MAX_RETRANSMIT 4 /**< Maximum number of times a confirmable message can be retransmitted */
- #define DEBUG DBG
- #ifdef COAP_DTLS_EN
- #define COAP_SERVER_DTLS_MTU COAP_MSG_MAX_BUF_LEN /**< Maximum transmission unit excluding the UDP and IPv6 headers */
- #define COAP_SERVER_DTLS_RETRANS_TIMEOUT 1000 /**< Retransmission timeout (msec) for the DTLS handshake */
- #define COAP_SERVER_DTLS_TOTAL_TIMEOUT 60000 /**< Total timeout (msec) for the DTLS handshake */
- #define COAP_SERVER_DTLS_NUM_DH_BITS 1024 /**< DTLS Diffie-Hellman key size */
- #define COAP_SERVER_DTLS_PRIORITIES "PERFORMANCE:-VERS-TLS-ALL:+VERS-DTLS1.0:SERVER_PRECEDENCE"
- /**< DTLS priorities */
- #endif
- #ifdef MALLOC_TRACE
- #include <mcheck.h>
- #endif
- #define MAX_A(A, B) ((A) > (B) ? (A) : (B) )
- int linda_fd;
- extern char *linda_ip;
- extern char *linda_port;
- GHashTable* hash;
- GHashTable* hash_pri_to_coap_code;
- GHashTable* hash_pri_to_coap_detail;
- GHashTable* hash_table_struct;
- heap_table_t *heap = NULL;
- GHashTable* hash_coap_uri_query;
- GHashTable* hash_coap_conf;
- char *rqi_hash_key;
- extern void *linda;
- extern int log_level;
- struct ifreq ifr;
- int ret = 0;
- struct {
- char *poa_port[5];
- char *poa_host;
- char *poa_uri[20];
- char json_poa[50];
- char *dest_uri[20];
- int *xmlBuffSize;
- }coap_server_glb;
- int group1[]={BRANCHING_GROUP, -1};
- char *config_name[5]={"XML_NAME_SPACE","XML_NAME_SPACE_URL","LINDA_IP","LINDA_PORT", NULL};
- char *config_value[4]={ NULL, NULL,NULL,NULL};
- void last_wish(){
- LOG(3, "Process kapanıyor. Bellekler free edilecek ve socketler kapatılacak.");
- if(heap){
- heap_free((void**)&heap);
- LOG(3,"Heap free yapıldı.");
- }
- else {
- LOG(3,"Heap önceden free edilmiş");
- }
- if(linda){
- free(linda);
- LOG(3, "Linda bağlantısı kapatıldı.");
- }
- else {
- LOG(3,"Linda bağlantısı önceden kapatılmış");
- }
- }
- /*
- @brief remove the key value from the string
- @param String pointer
- @param Key value pointer
- */
- int strRemove(char *src,char *key)
- {
- while( *src )
- {
- char *k=key,*s=src;
- while( *k && *k==*s ) ++k,++s;
- if( !*k )
- {
- while( *s ) *src++=*s++;
- *src=0;
- return 1;
- }
- ++src;
- }
- return 0;
- }
- static int print_coap_msge(const char *str, coap_msg_t *msg)
- {
- coap_msg_op_t *op = NULL;
- unsigned num = 0;
- unsigned len = 0;
- unsigned i = 0;
- unsigned j = 0;
- char *token = NULL;
- char *val = NULL;
- coap_log_level_t log_level = 0;
- log_level = coap_log_get_level();
- if (log_level < COAP_LOG_INFO)
- {
- return (0);
- }
- printf("%s\n", str);
- printf("ver: 0x%02x\n", coap_msg_get_ver(msg));
- printf("type: 0x%02x\n", coap_msg_get_type(msg));
- printf("token_len: %d\n", coap_msg_get_token_len(msg));
- printf("code_class: %d\n", coap_msg_get_code_class(msg));
- printf("code_detail: %d\n", coap_msg_get_code_detail(msg));
- printf("msg_id: 0x%04x\n", coap_msg_get_msg_id(msg));
- printf("token: ");
- token = coap_msg_get_token(msg);
- for (i = 0; i < coap_msg_get_token_len(msg); i++)
- {
- printf(" 0x%02x", (unsigned char)token[i]);
- }
- printf("\n");
- op = coap_msg_get_first_op(msg);
- while (op != NULL)
- {
- num = coap_msg_op_get_num(op);
- len = coap_msg_op_get_len(op);
- val = coap_msg_op_get_val(op);
- printf("op[%u].num: %u\n", j, num);
- printf("op[%u].len: %u\n", j, len);
- printf("op[%u].val: ", j);
- for (i = 0; i < len; i++)
- {
- printf(" 0x%02x", (unsigned char)val[i]);
- }
- printf("\n");
- op = coap_msg_op_get_next(op);
- j++;
- }
- printf("payload: ");
- /* primitive.content = coap_msg_get_payload(msg);
- * for (i = 0; i < coap_msg_get_payload_len(msg); i++)
- * {
- * printf("%c", primitive.content[i]);
- * } */
- printf("\n");
- printf("payload_len: %u\n", (unsigned int)coap_msg_get_payload_len(msg));
- fflush(stdout);
- return 0;
- }
- /****************************************************************************************************
- * coap_server_path *
- ****************************************************************************************************/
- /*
- @brief Allocate a URI path structure
- @param[in] str String representation of a URI path
- @returns New URI path structure
- @retval NULL Out-of-memory
- */
- static coap_server_path_t *coap_server_path_new(const char *str)
- {
- coap_server_path_t *path = NULL;
- path = (coap_server_path_t *)malloc(sizeof(coap_server_path_t));
- if (path == NULL)
- {
- return NULL;
- }
- path->str = strdup(str);
- if (path->str == NULL)
- {
- free(path);
- return NULL;
- }
- path->next = NULL;
- return path;
- }
- /*
- @brief Free a URI path structure
- @param[in,out] path Pointer to a URI path structure
- */
- static void coap_server_path_delete(coap_server_path_t *path)
- {
- free(path->str);
- free(path);
- }
- /*
- @brief Initialise a URI path list structure
- @param[out] list Pointer to a URI path list structure
- */
- static void coap_server_path_list_create(coap_server_path_list_t *list)
- {
- memset(list, 0, sizeof(coap_server_path_list_t));
- }
- /*
- @brief Deinitialise a URI path list structure
- @param[in,out] list Pointer to a URI path list structure
- */
- static void coap_server_path_list_destroy(coap_server_path_list_t *list)
- {
- coap_server_path_t *prev = NULL;
- coap_server_path_t *path = NULL;
- path = list->first;
- while (path != NULL)
- {
- prev = path;
- path = path->next;
- coap_server_path_delete(prev);
- }
- memset(list, 0, sizeof(coap_server_path_list_t));
- }
- /*
- @brief Add a URI path to a URI path list structure
- @param[in,out] list Pointer to a URI path list structure
- @param[in] str String representation of a URI path
- @returns Operation status
- @retval 0 Success
- @retval <0 Error
- */
- static int coap_server_path_list_add(coap_server_path_list_t *list, const char *str)
- {
- coap_server_path_t *path = NULL;
- path = coap_server_path_new(str);
- if (path == NULL)
- {
- return -ENOMEM;
- }
- if (list->first == NULL)
- {
- list->first = path;
- list->last = path;
- }
- else
- {
- list->last->next = path;
- list->last = path;
- }
- return 0;
- }
- /*
- @brief Search a URI path list structure for a URI path
- @param[in] list Pointer to a URI path list structure
- @param[in] str String representation of a URI path
- @returns Comparison value
- @retval 0 The URI path list structure does not contain the URI path
- @retval 1 The URI path list structure does contain the URI path
- */
- static int coap_server_path_list_match(coap_server_path_list_t *list, const char *str)
- {
- coap_server_path_t *path = NULL;
- path = list->first;
- while (path != NULL)
- {
- coap_log_debug("Comparing URI path: '%s' with list URI path: '%s'", str, path->str);
- if (strcmp(path->str, str) == 0)
- {
- coap_log_debug("Matched URI path: '%s' with list URI path: '%s'", str, path->str);
- return 1;
- }
- path = path->next;
- }
- return 0;
- }
- #ifdef COAP_DTLS_EN
- /****************************************************************************************************
- * coap_server_trans_dtls *
- ****************************************************************************************************/
- /*
- @brief Listen for a packet from the client with a timeout
- @param[in] trans Pointer to a transaction structure
- @param[in] ms Timeout value in msec
- @returns Operation status
- @retval 1 Success
- @retval 0 Timeout
- @retval <0 Error
- */
- static int coap_server_trans_dtls_listen_timeout(coap_server_trans_t *trans, unsigned ms)
- {
- coap_server_t *server = NULL;
- struct timeval tv = {0};
- fd_set read_fds = {{0}};
- int ret = 0;
- server = trans->server;
- tv.tv_sec = ms / 1000;
- tv.tv_usec = (ms % 1000) * 1000;
- while (1)
- {
- FD_ZERO(&read_fds);
- FD_SET(server->sd, &read_fds);
- ret = select(server->sd + 1, &read_fds, NULL, NULL, &tv);
- if (ret == -1)
- {
- return -errno;
- }
- if (ret == 0)
- {
- return 0; /* timeout */
- }
- if (FD_ISSET(server->sd, &read_fds))
- {
- return 1; /* success */
- }
- }
- }
- /*
- @brief Receive data from the client
- This is a call-back function that the
- GnuTLS library uses to receive data.
- @param[in,out] data Pointer to a transaction structure
- @param[out] buf Pointer to a buffer
- @param[in] len Length of the buffer
- @returns Number of bytes received or error
- @retval 0 Number of bytes received
- @retval -1 Error
- */
- static ssize_t coap_server_trans_dtls_pull_func(gnutls_transport_ptr_t data, void *buf, size_t len)
- {
- coap_server_trans_t *trans = NULL;
- struct sockaddr_in6 client_sin = {0};
- coap_server_t *server = NULL;
- socklen_t client_sin_len = 0;
- ssize_t num = 0;
- trans = (coap_server_trans_t *)data;
- server = trans->server;
- client_sin_len = sizeof(client_sin);
- errno = 0;
- num = recvfrom(server->sd, buf, len, MSG_PEEK, (struct sockaddr *)&client_sin, &client_sin_len); /* peek data */
- if (num == -1)
- {
- return -1;
- }
- if ((client_sin_len != trans->client_sin_len)
- || (memcmp(&client_sin, &trans->client_sin, trans->client_sin_len) != 0))
- {
- gnutls_transport_set_errno(trans->session, EAGAIN);
- return -1;
- }
- return recvfrom(server->sd, buf, num, 0, (struct sockaddr *)&client_sin, &client_sin_len); /* consume data */
- }
- /*
- @brief Wait for receive data from the client
- This is a call-back function that the GnuTLS
- library uses to wait for receive data.
- @param[in,out] data Pointer to a transaction structure
- @param[in] ms Timeout in msec
- @returns Number of bytes received or error
- @retval >0 Number of bytes received
- @retval 0 Timeout
- @retval -1 Error
- */
- static int coap_server_trans_dtls_pull_timeout_func(gnutls_transport_ptr_t data, unsigned ms)
- {
- coap_server_trans_t *trans = NULL;
- struct sockaddr_in6 client_sin = {0};
- coap_server_t *server = NULL;
- socklen_t client_sin_len = 0;
- ssize_t num = 0;
- char buf[COAP_SERVER_DTLS_MTU] = {0};
- int ret = 0;
- trans = (coap_server_trans_t *)data;
- server = trans->server;
- ret = coap_server_trans_dtls_listen_timeout(trans, ms);
- if (ret == 0)
- {
- return 0; /* timeout */
- }
- if (ret < 0)
- {
- return -1;
- }
- client_sin_len = sizeof(client_sin);
- num = recvfrom(server->sd, buf, sizeof(buf), MSG_PEEK, (struct sockaddr *)&client_sin, &client_sin_len); /* peek data */
- if (num == -1)
- {
- return -1;
- }
- if ((client_sin_len != trans->client_sin_len)
- || (memcmp(&client_sin, &trans->client_sin, trans->client_sin_len) != 0))
- {
- gnutls_transport_set_errno(trans->session, EAGAIN);
- return -1;
- }
- return num; /* success */
- }
- /*
- @brief Send data to the client
- This is a call-back function that the
- GnuTLS library uses to send data.
- @param[in] data Pointer to a transaction structure
- @param[in] buf Pointer to a buffer
- @param[in] len Length of the buffer
- @returns Number of bytes sent or error
- @retval >0 Number of bytes sent
- @retval -1 Error
- */
- static ssize_t coap_server_trans_dtls_push_func(gnutls_transport_ptr_t data, const void *buf, size_t len)
- {
- coap_server_trans_t *trans = NULL;
- coap_server_t *server = NULL;
- trans = (coap_server_trans_t *)data;
- server = trans->server;
- return sendto(server->sd, buf, len, 0, (struct sockaddr *)&trans->client_sin, trans->client_sin_len);
- }
- /*
- @brief Perform a DTLS handshake with the client
- @param[in,out] trans Pointer to a transaction structure
- @returns Operation success
- @retval 0 Success
- @retval <0 Error
- */
- static int coap_server_trans_dtls_handshake(coap_server_trans_t *trans)
- {
- int ret = 0;
- int i = 0;
- for (i = 0; i < COAP_SERVER_DTLS_TOTAL_TIMEOUT / COAP_SERVER_DTLS_RETRANS_TIMEOUT; i++)
- {
- ret = gnutls_handshake(trans->session);
- if (ret == GNUTLS_E_SUCCESS)
- {
- return 0; /* success */
- }
- if (ret != GNUTLS_E_AGAIN)
- {
- return -1;
- }
- ret = coap_server_trans_dtls_listen_timeout(trans, COAP_SERVER_DTLS_RETRANS_TIMEOUT);
- if (ret < 0)
- {
- return ret;
- }
- }
- return -1;
- }
- /*
- @brief Initialise the DTLS members of a transaction structure
- Perform a DTLS handshake with the client.
- @param[out] trans Pointer to a transaction structure
- @returns Operation status
- @retval 0 Success
- @retval -1 Error
- */
- static int coap_server_trans_dtls_create(coap_server_trans_t *trans)
- {
- coap_server_t *server = NULL;
- int ret = 0;
- server = trans->server;
- ret = gnutls_init(&trans->session, GNUTLS_SERVER | GNUTLS_DATAGRAM | GNUTLS_NONBLOCK);
- if (ret != GNUTLS_E_SUCCESS)
- {
- return -1;
- }
- ret = gnutls_credentials_set(trans->session, GNUTLS_CRD_CERTIFICATE, server->cred);
- if (ret != GNUTLS_E_SUCCESS)
- {
- gnutls_deinit(trans->session);
- return -1;
- }
- ret = gnutls_priority_set(trans->session, server->priority);
- if (ret != GNUTLS_E_SUCCESS)
- {
- gnutls_deinit(trans->session);
- return -1;
- }
- gnutls_transport_set_ptr(trans->session, trans);
- gnutls_transport_set_pull_function(trans->session, coap_server_trans_dtls_pull_func);
- gnutls_transport_set_pull_timeout_function(trans->session, coap_server_trans_dtls_pull_timeout_func);
- gnutls_transport_set_push_function(trans->session, coap_server_trans_dtls_push_func);
- gnutls_dtls_set_mtu(trans->session, COAP_SERVER_DTLS_MTU);
- gnutls_dtls_set_timeouts(trans->session, COAP_SERVER_DTLS_RETRANS_TIMEOUT, COAP_SERVER_DTLS_TOTAL_TIMEOUT);
- ret = coap_server_trans_dtls_handshake(trans);
- if (ret < 0)
- {
- gnutls_deinit(trans->session);
- return -1;
- }
- return 0;
- }
- /*
- @brief Deinitialise the DTLS members of a transaction structure
- @param[in,out] trans Pointer to a transaction structure
- */
- static void coap_server_trans_dtls_destroy(coap_server_trans_t *trans)
- {
- gnutls_deinit(trans->session);
- }
- #endif /* COAP_DTLS_EN */
- /**************************************************************************************************
- * coap_server_trans *
- **************************************************************************************************/
- /*
- @brief Deinitialise a transaction structure
- @param[in,out] trans Pointer to a transaction structure
- */
- static void coap_server_trans_destroy(coap_server_trans_t *trans)
- {
- coap_log_debug("Destroyed transaction for address %s and port %u", trans->client_addr, ntohs(trans->client_sin.sin6_port));
- #ifdef COAP_DTLS_EN
- coap_server_trans_dtls_destroy(trans);
- #endif
- coap_msg_destroy(&trans->resp);
- coap_msg_destroy(&trans->req);
- close(trans->timer_fd);
- memset(trans, 0, sizeof(coap_server_trans_t));
- }
- /*
- @brief Mark the last time the transaction structure was used
- @param[out] trans Pointer to a transaction structure
- */
- static void coap_server_trans_touch(coap_server_trans_t *trans)
- {
- trans->last_use = time(NULL);
- }
- /*
- @brief Compare a received message with the request part of a transaction structure
- @param[in] trans Pointer to a trasaction structure
- @param[in] msg Pointer to a message structure
- @returns Comparison value
- @retval 0 The message does not match the transaction
- @retval 1 The message matches the transaction
- */
- static int coap_server_trans_match_req(coap_server_trans_t *trans, coap_msg_t *msg)
- {
- return ((coap_msg_get_ver(&trans->req) != 0)
- && (coap_msg_get_msg_id(&trans->req) == coap_msg_get_msg_id(msg)));
- }
- /*
- @brief Compare a recevied message with the response part of a transaction structure
- @param[in] trans Pointer to a trasaction structure
- @param[in] msg Pointer to a message structure
- @returns Comparison value
- @retval 0 The message does not match the transaction
- @retval 1 The message matches the transaction
- */
- static int coap_server_trans_match_resp(coap_server_trans_t *trans, coap_msg_t *msg)
- {
- return ((coap_msg_get_ver(&trans->resp) != 0)
- && (coap_msg_get_msg_id(&trans->resp) == coap_msg_get_msg_id(msg)));
- }
- /*
- @brief Clear the request message in a transaction structure
- @param[in,out] trans Pointer to a transaction structure
- */
- static void coap_server_trans_clear_req(coap_server_trans_t *trans)
- {
- coap_msg_destroy(&trans->req);
- }
- /*
- @brief Clear the response message in a transaction structure
- @param[in,out] trans Pointer to a transaction structure
- */
- static void coap_server_trans_clear_resp(coap_server_trans_t *trans)
- {
- coap_msg_destroy(&trans->resp);
- }
- /*
- @brief Aşağıda bulunan fonksiyonlar kullanılmadığı için şimdilik çıkarılmıştır
- */
- #if 0
- /*
- @brief Set the request message in a transaction structure
- @param[in,out] trans Pointer to a transaction structure
- @param[in] msg Pointer to a message structure
- @returns Operation status
- @retval 0 Success
- @retval <0 Error
- */
- static int coap_server_trans_set_req(coap_server_trans_t *trans, coap_msg_t *msg)
- {
- coap_msg_reset(&trans->req);
- return coap_msg_copy(&trans->req, msg);
- }
- /*
- @brief Set the response message in a transaction structure
- @param[in,out] trans Pointer to a transaction structure
- @param[in] msg Pointer to a message structure
- @returns Operation status
- @retval 0 Success
- @retval <0 Error
- */
- static int coap_server_trans_set_resp(coap_server_trans_t *trans, coap_msg_t *msg)
- {
- coap_msg_reset(&trans->resp);
- return coap_msg_copy(&trans->resp, msg);
- }
- /*
- @brief Initialise the acknowledgement timer in a transaction structure
- The timer is initialised to a random duration between:
- ACK_TIMEOUT and (ACK_TIMEOUT * ACK_RANDOM_FACTOR)
- where:
- ACK_TIMEOUT = 2
- ACK_RANDOM_FACTOR = 1.5
- @param[out] trans Pointer to a transaction structure
- */
- static void coap_server_trans_init_ack_timeout(coap_server_trans_t *trans)
- {
- if (!rand_init)
- {
- srand(time(NULL));
- rand_init = 1;
- }
- trans->timeout.tv_sec = COAP_SERVER_ACK_TIMEOUT_SEC;
- trans->timeout.tv_nsec = (rand() % 1000) * 1000000;
- coap_log_debug("Acknowledgement timeout initialised to: %lu sec, %lu nsec", trans->timeout.tv_sec, trans->timeout.tv_nsec);
- }
- #endif
- /*
- @brief Double the value of the timer in a transaction structure
- @param[in,out] trans Pointer to a trans structure
- */
- static void coap_server_trans_double_timeout(coap_server_trans_t *trans)
- {
- unsigned msec = 2 * ((trans->timeout.tv_sec * 1000)
- + (trans->timeout.tv_nsec / 1000000));
- trans->timeout.tv_sec = msec / 1000;
- trans->timeout.tv_nsec = (msec % 1000) * 1000000;
- coap_log_debug("Timeout doubled to: %lu sec, %lu nsec", trans->timeout.tv_sec, trans->timeout.tv_nsec);
- }
- /*
- @brief Start the timer in a transaction structure
- @param[in,out] trans Pointer to a transaction structure
- @returns Operation status
- @retval 0 Success
- @retval <0 Error
- */
- static int coap_server_trans_start_timer(coap_server_trans_t *trans)
- {
- struct itimerspec its = {{0}};
- int ret = 0;
- its.it_value = trans->timeout;
- ret = timerfd_settime(trans->timer_fd, 0, &its, NULL);
- if (ret == -1)
- {
- return -errno;
- }
- return 0;
- }
- /*
- @brief Stop the timer in a transaction structure
- @param[out] trans Pointer to a transaction structure
- @returns Operation status
- @retval 0 Success
- @retval <0 Error
- */
- static int coap_server_trans_stop_timer(coap_server_trans_t *trans)
- {
- struct itimerspec its = {{0}};
- int ret = 0;
- ret = timerfd_settime(trans->timer_fd, 0, &its, NULL);
- if (ret == -1)
- {
- return -errno;
- }
- return 0;
- }
- /*
- @brief Aşağıda bulunan fonksiyonlar kullanılmadığı için şimdilik çıkarılmıştır
- */
- #if 0
- /*
- @brief Initialise and start the acknowledgement timer in a transaction structure
- @param[out] trans Pointer to a trans structure
- @returns Operation status
- @retval 0 Success
- @retval <0 Error
- */
- static int coap_server_trans_start_ack_timer(coap_server_trans_t *trans)
- {
- trans->num_retrans = 0;
- coap_server_trans_init_ack_timeout(trans);
- return coap_server_trans_start_timer(trans);
- }
- #endif
- /*
- @brief Stop the acknowledgement timer in a transaction structure
- @param[out] trans Pointer to a transaction structure
- @returns Operation status
- @retval 0 Success
- @retval <0 Error
- */
- static int coap_server_trans_stop_ack_timer(coap_server_trans_t *trans)
- {
- trans->num_retrans = 0;
- return coap_server_trans_stop_timer(trans);
- }
- /*
- @brief Update the acknowledgement timer in a transaction structure
- Increase and restart the acknowledgement timer in a transaction structure
- and indicate if the maximum number of retransmits has been reached.
- @param[in,out] trans Pointer to a trans structure
- @returns Operation status
- @retval 0 Success
- @retval <0 Error
- */
- static int coap_server_trans_update_ack_timer(coap_server_trans_t *trans)
- {
- int ret = 0;
- if (trans->num_retrans >= COAP_SERVER_MAX_RETRANSMIT)
- {
- return -ETIMEDOUT;
- }
- coap_server_trans_double_timeout(trans);
- ret = coap_server_trans_start_timer(trans);
- if (ret < 0)
- {
- return ret;
- }
- trans->num_retrans++;
- return 0;
- }
- /*
- @brief Send a message to the client
- @param[in,out] trans Pointer to a transaction structure
- @param[in] msg Pointer to a message structure
- @returns Number of bytes sent or error code
- @retval >0 Number of bytes sent
- @retval <0 Error
- */
- static ssize_t coap_server_trans_send(coap_server_trans_t *trans, coap_msg_t *msg)
- {
- if(trans == NULL)
- ERR("Response mesajının Transaction structı bulunamadı, response gönderilemiyor");
- if(msg == NULL)
- ERR("Response mesajı içi NULL");
- #ifndef COAP_DTLS_EN
- coap_server_t *server = NULL;
- #endif
- ssize_t num = 0;
- char buf[COAP_MSG_MAX_BUF_LEN] = {0};
- num = coap_msg_format(msg, buf, sizeof(buf));
- if (num < 0)
- {
- return num;
- }
- #ifdef COAP_DTLS_EN
- num = gnutls_record_send(trans->session, buf, num);
- if (num < 0)
- {
- if (num == GNUTLS_E_AGAIN)
- {
- return -EAGAIN;
- }
- return -1;
- }
- #else
- server = trans->server;
- num = sendto(server->sd, buf, num, 0, (struct sockaddr *)&trans->client_sin, trans->client_sin_len);
- if (num == -1)
- {
- return -errno;
- }
- #endif
- coap_server_trans_touch(trans);
- coap_log_debug("Sent to address %s and port %u", trans->client_addr, ntohs(trans->client_sin.sin6_port));
- return num;
- }
- /*
- @brief Handle a format error in a received message
- Special handling for the case where a received
- message could not be parsed due to a format error.
- Extract enough information from the received message
- to form a reset message.
- @param[in,out] trans Pointer to a transaction structure
- @param[in] buf Buffer containing the message
- @param[in] len length of the buffer
- */
- static void coap_server_trans_handle_format_error(coap_server_trans_t *trans, char *buf, unsigned len)
- {
- coap_msg_t msg = {0};
- unsigned msg_id = 0;
- unsigned type = 0;
- int ret = 0;
- /* extract enough information to form a reset message */
- ret = coap_msg_parse_type_msg_id(buf, len, &type, &msg_id);
- if ((ret == 0) && (type == COAP_MSG_CON))
- {
- coap_msg_create(&msg);
- ret = coap_msg_set_type(&msg, COAP_MSG_RST);
- if (ret < 0)
- {
- coap_msg_destroy(&msg);
- return;
- }
- ret = coap_msg_set_msg_id(&msg, msg_id);
- if (ret < 0)
- {
- coap_msg_destroy(&msg);
- return;
- }
- coap_server_trans_send(trans, &msg);
- coap_msg_destroy(&msg);
- }
- }
- /*
- @brief Receive a message from the client
- @param[in,out] trans Pointer to a transaction structure
- @param[in] msg Pointer to a message structure
- @returns Number of bytes received or error code
- @retval >0 Number of bytes received
- @retval <0 Error
- */
- static ssize_t coap_server_trans_recv(coap_server_trans_t *trans, coap_msg_t *msg)
- {
- #ifndef COAP_DTLS_EN
- struct sockaddr_in6 client_sin = {0};
- coap_server_t *server = NULL;
- socklen_t client_sin_len = 0;
- #endif
- ssize_t num = 0;
- ssize_t ret = 0;
- char buf[COAP_MSG_MAX_BUF_LEN] = {0};
- #ifdef COAP_DTLS_EN
- num = gnutls_record_recv(trans->session, buf, sizeof(buf));
- if (num < 0)
- {
- if (num == GNUTLS_E_AGAIN)
- {
- return -EAGAIN;
- }
- return -1;
- }
- #else
- server = trans->server;
- client_sin_len = sizeof(client_sin);
- num = recvfrom(server->sd, buf, sizeof(buf), MSG_PEEK, (struct sockaddr *)&client_sin, &client_sin_len);
- if (num == -1)
- {
- return -errno;
- }
- if ((client_sin_len != trans->client_sin_len)
- || (memcmp(&client_sin, &trans->client_sin, client_sin_len) != 0))
- {
- return -EAGAIN;
- }
- num = recvfrom(server->sd, buf, num, 0, (struct sockaddr *)&client_sin, &client_sin_len);
- if (num == -1)
- {
- return -errno;
- }
- #endif
- ret = coap_msg_parse(msg, buf, num);
- if (ret < 0)
- {
- if (ret == -EBADMSG)
- {
- coap_server_trans_handle_format_error(trans, buf, num);
- }
- return ret;
- }
- coap_server_trans_touch(trans);
- coap_log_debug("Received from address %s and port %u", trans->client_addr, ntohs(trans->client_sin.sin6_port));
- return num;
- }
- /*
- @brief Reject a received confirmable message
- Send a reset message to the client.
- @param[in,out] trans Pointer to a transaction structure
- @param[in] msg Pointer to a message structure
- @returns Operation status
- @retval 0 Success
- @retval <0 Error
- */
- static int coap_server_trans_reject_con(coap_server_trans_t *trans, coap_msg_t *msg)
- {
- coap_msg_t rej = {0};
- ssize_t num = 0;
- int ret = 0;
- coap_log_info("Rejecting confirmable request from address %s and port %u", trans->client_addr, ntohs(trans->client_sin.sin6_port));
- coap_msg_create(&rej);
- ret = coap_msg_set_type(&rej, COAP_MSG_RST);
- if (ret < 0)
- {
- coap_msg_destroy(&rej);
- return ret;
- }
- ret = coap_msg_set_msg_id(&rej, coap_msg_get_msg_id(msg));
- if (ret < 0)
- {
- coap_msg_destroy(&rej);
- return ret;
- }
- num = coap_server_trans_send(trans, &rej);
- coap_msg_destroy(&rej);
- if (num < 0)
- {
- return num;
- }
- return 0;
- }
- /*
- @brief Reject a received non-confirmable message
- @param[in] trans Pointer to a transaction structure
- @param[in] msg Pointer to a message structure
- @returns Operation status
- @retval 0 Success
- */
- static int coap_server_trans_reject_non(coap_server_trans_t *trans, coap_msg_t *msg)
- {
- coap_log_info("Rejecting non-confirmable message from address %s and port %u", trans->client_addr, ntohs(trans->client_sin.sin6_port));
- return 0;
- }
- /*
- @brief Reject a received message
- @param[in,out] trans Pointer to a transaction structure
- @param[in] msg Pointer to a message structure
- @returns Operation status
- @retval 0 Success
- @retval <0 Error
- */
- static int coap_server_trans_reject(coap_server_trans_t *trans, coap_msg_t *msg)
- {
- if (coap_msg_get_type(msg) == COAP_MSG_CON)
- {
- return coap_server_trans_reject_con(trans, msg);
- }
- return coap_server_trans_reject_non(trans, msg);
- }
- /*
- @brief Reject a request message containing a bad option
- @param[in,out] trans Pointer to a transaction structure
- @param[in] msg Pointer to a message structure
- @param[in] op_num Option number of the bad option
- @returns Operation status
- @retval 0 Success
- @retval <0 Error
- */
- int coap_server_trans_reject_bad_option(coap_server_trans_t *trans, coap_msg_t *msg, unsigned op_num)
- {
- coap_msg_t rej = {0};
- char payload[COAP_SERVER_DIAG_PAYLOAD_LEN] = {0};
- ssize_t num = 0;
- int ret = 0;
- if (coap_msg_get_type(msg) == COAP_MSG_NON)
- {
- return coap_server_trans_reject_non(trans, msg);
- }
- /* confirmable request containing a bad option */
- /* send a piggy-backed response containing 4.02 Bad Option */
- coap_msg_create(&rej);
- ret = coap_msg_set_type(&rej, COAP_MSG_ACK);
- if (ret != 0)
- {
- coap_msg_destroy(&rej);
- return ret;
- }
- ret = coap_msg_set_code(&rej, COAP_MSG_CLIENT_ERR, COAP_MSG_BAD_OPTION);
- if (ret != 0)
- {
- coap_msg_destroy(&rej);
- return ret;
- }
- ret = coap_msg_set_msg_id(&rej, coap_msg_get_msg_id(msg));
- if (ret != 0)
- {
- coap_msg_destroy(&rej);
- return ret;
- }
- ret = coap_msg_set_token(&rej, coap_msg_get_token(msg), coap_msg_get_token_len(msg));
- if (ret != 0)
- {
- coap_msg_destroy(&rej);
- return ret;
- }
- snprintf(payload, sizeof(payload), "Bad option number: %u", op_num);
- ret = coap_msg_set_payload(&rej, payload, strlen(payload));
- if (ret != 0)
- {
- coap_msg_destroy(&rej);
- return ret;
- }
- num = coap_server_trans_send(trans, &rej);
- if (num < 0)
- {
- coap_msg_destroy(&rej);
- return num;
- }
- coap_msg_destroy(&rej);
- return 0;
- }
- /*
- @brief Send an acknowledgement message to the client
- @param[in,out] trans Pointer to a transaction structure
- @param[in] msg Pointer to a message structure
- @returns Operation status
- @retval 0 Success
- @retval <0 Error
- */
- static int coap_server_trans_send_ack(coap_server_trans_t *trans, coap_msg_t *msg)
- {
- coap_msg_t ack = {0};
- ssize_t num = 0;
- int ret = 0;
- coap_log_info("Acknowledging confirmable message from address %s and port %u", trans->client_addr, ntohs(trans->client_sin.sin6_port));
- coap_msg_create(&ack);
- ret = coap_msg_set_type(&ack, COAP_MSG_ACK);
- if (ret < 0)
- {
- coap_msg_destroy(&ack);
- return ret;
- }
- ret = coap_msg_set_msg_id(&ack, coap_msg_get_msg_id(msg));
- if (ret < 0)
- {
- coap_msg_destroy(&ack);
- return ret;
- }
- num = coap_server_trans_send(trans, &ack);
- coap_msg_destroy(&ack);
- if (num < 0)
- {
- return num;
- }
- return 0;
- }
- /*
- @brief Handle an acknowledgement timeout
- Update the acknowledgement timer in the transaction structure
- and if the maximum number of retransmits has not been reached
- then retransmit the last response to the client.
- @param[in,out] trans Pointer to a transaction structure
- @returns Operation status
- @retval 0 Success
- @retval <0 Error
- */
- static int coap_server_trans_handle_ack_timeout(coap_server_trans_t *trans)
- {
- ssize_t num = 0;
- int ret = 0;
- coap_log_debug("Transaction expired for address %s and port %u", trans->client_addr, ntohs(trans->client_sin.sin6_port));
- ret = coap_server_trans_update_ack_timer(trans);
- if (ret == 0)
- {
- coap_log_debug("Retransmitting to address %s and port %u", trans->client_addr, ntohs(trans->client_sin.sin6_port));
- num = coap_server_trans_send(trans, &trans->resp);
- if (num < 0)
- {
- return num;
- }
- }
- else if (ret == -ETIMEDOUT)
- {
- coap_log_debug("Stopped retransmitting to address %s and port %u", trans->client_addr, ntohs(trans->client_sin.sin6_port));
- coap_log_info("No acknowledgement received from address %s and port %u", trans->client_addr, ntohs(trans->client_sin.sin6_port));
- coap_server_trans_destroy(trans);
- ret = 0;
- }
- return ret;
- }
- /*
- @brief Initialise a transaction structure
- @param[out] trans Pointer to a transaction structure
- @param[in] server Pointer to a server structure
- @param[in] client_sin Pointer to a struct sockaddr_in6
- @param[in] client_sin_len Length of the struct sockaddr_in6
- @returns Operation status
- @retval 0 Success
- @retval <0 Error
- */
- static int coap_server_trans_create(coap_server_trans_t *trans, coap_server_t *server, struct sockaddr_in6 *client_sin, socklen_t client_sin_len)
- {
- const char *p = NULL;
- #ifdef COAP_DTLS_EN
- int ret = 0;
- #endif
- memset(trans, 0, sizeof(coap_server_trans_t));
- trans->active = 1;
- coap_server_trans_touch(trans);
- trans->timer_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
- if (trans->timer_fd == -1)
- {
- memset(trans, 0, sizeof(coap_server_trans_t));
- return -errno;
- }
- memcpy(&trans->client_sin, client_sin, client_sin_len);
- trans->client_sin_len = client_sin_len;
- p = inet_ntop(AF_INET6, &client_sin->sin6_addr, trans->client_addr, sizeof(trans->client_addr));
- if (p == NULL)
- {
- close(trans->timer_fd);
- memset(trans, 0, sizeof(coap_server_trans_t));
- return -errno;
- }
- coap_msg_create(&trans->req);
- coap_msg_create(&trans->resp);
- trans->server = server;
- #ifdef COAP_DTLS_EN
- ret = coap_server_trans_dtls_create(trans);
- if (ret < 0)
- {
- coap_msg_destroy(&trans->resp);
- coap_msg_destroy(&trans->req);
- close(trans->timer_fd);
- memset(trans, 0, sizeof(coap_server_trans_t));
- return ret;
- }
- #endif
- coap_log_debug("Created transaction for address %s and port %u", trans->client_addr, ntohs(trans->client_sin.sin6_port));
- return 0;
- }
- #ifdef COAP_DTLS_EN
- /**********************************************************************************************
- * coap_server_dtls *
- **********************************************************************************************/
- /*
- @brief Initialise the DTLS members of a server structure
- @param[out] server Pointer to a server structure
- @param[in] key_file_name String containing the DTLS key file name
- @param[in] cert_file_name String containing the DTLS certificate file name
- @param[in] trust_file_name String containing the DTLS trust file name
- @param[in] crl_file_name String containing the DTLS certificate revocation list file name
- @returns Operation status
- @retval 0 Success
- @retval -1 Error
- */
- static int coap_server_dtls_create(coap_server_t *server,
- const char *key_file_name,
- const char *cert_file_name,
- const char *trust_file_name,
- const char *crl_file_name)
- {
- int ret = 0;
- ret = gnutls_global_init();
- if (ret != GNUTLS_E_SUCCESS)
- {
- return -1;
- }
- ret = gnutls_certificate_allocate_credentials(&server->cred);
- if (ret != GNUTLS_E_SUCCESS)
- {
- gnutls_global_deinit();
- return -1;
- }
- if ((trust_file_name != NULL) && (strlen(trust_file_name) != 0))
- {
- ret = gnutls_certificate_set_x509_trust_file(server->cred, trust_file_name, GNUTLS_X509_FMT_PEM);
- if (ret == 0)
- {
- gnutls_certificate_free_credentials(server->cred);
- gnutls_global_deinit();
- return -1;
- }
- }
- if ((crl_file_name != NULL) && (strlen(crl_file_name) != 0))
- {
- ret = gnutls_certificate_set_x509_crl_file(server->cred, crl_file_name, GNUTLS_X509_FMT_PEM);
- if (ret < 0)
- {
- gnutls_certificate_free_credentials(server->cred);
- gnutls_global_deinit();
- return -1;
- }
- }
- ret = gnutls_certificate_set_x509_key_file(server->cred, cert_file_name, key_file_name, GNUTLS_X509_FMT_PEM);
- if (ret != GNUTLS_E_SUCCESS)
- {
- gnutls_certificate_free_credentials(server->cred);
- gnutls_global_deinit();
- return -1;
- }
- ret = gnutls_dh_params_init(&server->dh_params);
- if (ret != GNUTLS_E_SUCCESS)
- {
- gnutls_certificate_free_credentials(server->cred);
- gnutls_global_deinit();
- return -1;
- }
- ret = gnutls_dh_params_generate2(server->dh_params, COAP_SERVER_DTLS_NUM_DH_BITS);
- if (ret != GNUTLS_E_SUCCESS)
- {
- gnutls_dh_params_deinit(server->dh_params);
- gnutls_certificate_free_credentials(server->cred);
- gnutls_global_deinit();
- return -1;
- }
- gnutls_certificate_set_dh_params(server->cred, server->dh_params);
- ret = gnutls_priority_init(&server->priority, COAP_SERVER_DTLS_PRIORITIES, NULL);
- if (ret != GNUTLS_E_SUCCESS)
- {
- gnutls_dh_params_deinit(server->dh_params);
- gnutls_certificate_free_credentials(server->cred);
- gnutls_global_deinit();
- return -1;
- }
- return 0;
- }
- /*
- @brief Deinitialise the DTLS members of a server structure
- @param[in,out] trans Pointer to a server structure
- */
- static void coap_server_dtls_destroy(coap_server_t *server)
- {
- gnutls_priority_deinit(server->priority);
- gnutls_certificate_free_credentials(server->cred);
- gnutls_dh_params_deinit(server->dh_params);
- gnutls_global_deinit();
- }
- #endif /* COAP_DTLS_EN */
- /*********************************************************************************************
- * coap_server *
- *********************************************************************************************/
- #ifdef COAP_DTLS_EN
- int coap_server_create(coap_server_t *server,
- int (* handle)(coap_server_t *, coap_msg_t *, coap_msg_t *),
- const char *host,
- unsigned port,
- const char *key_file_name,
- const char *cert_file_name,
- const char *trust_file_name,
- const char *crl_file_name)
- #else
- int coap_server_create(coap_server_t *server,
- int (* handle)(coap_server_t *, coap_msg_t *, coap_msg_t *),
- const char *host,
- unsigned port)
- #endif
- {
- struct sockaddr_in6 server_sin = {0};
- unsigned char msg_id[2] = {0};
- const char *p = NULL;
- socklen_t server_sin_len = 0;
- char server_addr[COAP_SERVER_ADDR_BUF_LEN] = {0};
- int opt_val = 0;
- int flags = 0;
- int ret = 0;
- if ((server == NULL) || (host == NULL))
- {
- return -EINVAL;
- }
- memset(server, 0, sizeof(coap_server_t));
- server->sd = socket(PF_INET6, SOCK_DGRAM, 0);
- if (server->sd == -1)
- {
- memset(server, 0, sizeof(coap_server_t));
- return -errno;
- }
- flags = fcntl(server->sd, F_GETFL, 0);
- if (flags == -1)
- {
- close(server->sd);
- memset(server, 0, sizeof(coap_server_t));
- return -errno;
- }
- ret = fcntl(server->sd, F_SETFL, flags | O_NONBLOCK);
- if (ret == -1)
- {
- close(server->sd);
- memset(server, 0, sizeof(coap_server_t));
- return -errno;
- }
- opt_val = 1;
- ret = setsockopt(server->sd, SOL_SOCKET, SO_REUSEADDR, &opt_val, (socklen_t)sizeof(opt_val));
- if (ret == -1)
- {
- close(server->sd);
- memset(server, 0, sizeof(coap_server_t));
- return -errno;
- }
- server_sin.sin6_family = AF_INET6;
- server_sin.sin6_port = htons(port);
- ret = inet_pton(AF_INET6, host, &server_sin.sin6_addr);
- if (ret == 0)
- {
- close(server->sd);
- memset(server, 0, sizeof(coap_server_t));
- return -EINVAL;
- }
- else if (ret == -1)
- {
- close(server->sd);
- memset(server, 0, sizeof(coap_server_t));
- return -errno;
- }
- /*
- * @brief accept connections from any ipv6 address
- * */
- server_sin.sin6_addr = in6addr_any;
- server_sin_len = sizeof(server_sin);
- ret = bind(server->sd, (struct sockaddr *)&server_sin, server_sin_len);
- if (ret == -1)
- {
- close(server->sd);
- memset(server, 0, sizeof(coap_server_t));
- return -errno;
- }
- coap_msg_gen_rand_str((char *)msg_id, sizeof(msg_id));
- server->msg_id = (((unsigned)msg_id[1]) << 8) | (unsigned)msg_id[0];
- coap_server_path_list_create(&server->sep_list);
- server->handle = handle;
- p = inet_ntop(AF_INET6, &server_sin.sin6_addr, server_addr, sizeof(server_addr));
- if (p == NULL)
- {
- coap_server_path_list_destroy(&server->sep_list);
- close(server->sd);
- memset(server, 0, sizeof(coap_server_t));
- return -errno;
- }
- #ifdef COAP_DTLS_EN
- ret = coap_server_dtls_create(server, key_file_name, cert_file_name, trust_file_name, crl_file_name);
- if (ret < 0)
- {
- coap_server_path_list_destroy(&server->sep_list);
- close(server->sd);
- memset(server, 0, sizeof(coap_server_t));
- return ret;
- }
- #endif
- coap_log_notice("Listening on address %s and port %d", server_addr, ntohs(server_sin.sin6_port));
- return 0;
- }
- /*
- @brief Server destroy and close the connection
- */
- void coap_server_destroy(coap_server_t *server)
- {
- coap_server_trans_t *trans = NULL;
- unsigned i = 0;
- for (i = 0; i < COAP_SERVER_NUM_TRANS; i++)
- {
- trans = &server->trans[i];
- if (trans->active)
- {
- coap_server_trans_destroy(trans);
- }
- }
- #ifdef COAP_DTLS_EN
- coap_server_dtls_destroy(server);
- #endif
- coap_server_path_list_destroy(&server->sep_list);
- close(server->sd);
- memset(server, 0, sizeof(coap_server_t));
- }
- /*
- @brief Take random message ID
- @param[in] server Pointer to a server structure
- @returns Random massage ID
- */
- unsigned coap_server_get_next_msg_id(coap_server_t *server)
- {
- unsigned char msg_id[2] = {0};
- server->msg_id++;
- while (server->msg_id > COAP_MSG_MAX_MSG_ID)
- {
- coap_msg_gen_rand_str((char *)msg_id, sizeof(msg_id));
- server->msg_id = (((unsigned)msg_id[1]) << 8) | (unsigned)msg_id[0];
- }
- return server->msg_id;
- }
- /*
- @brief Check that all of the options in a message are acceptable
- For a proxy, options are acceptable if they are safe to forward or recognized or both.
- For a server, options are acceptable if they are elective or recognized or both.
- @param[in] msg Pointer to message structure
- @returns Operation status or bad option number
- @retval 0 Success
- @retval >0 Bad option number
- */
- static unsigned coap_server_check_options(coap_msg_t *msg)
- {
- #ifdef COAP_PROXY
- return coap_msg_check_unsafe_ops(msg);
- #else /* !COAP_PROXY */
- return coap_msg_check_critical_ops(msg);
- #endif /* COAP_PROXY */
- }
- /*
- @brief Search for a transaction structure in a server structure that matches an endpoint
- @param[in] server Pointer to a server structure
- @param[in] client_sin Pointer to a struct sockaddr_in6
- @param[in] client_sin_len Length of the struct sockaddr_in6
- @returns Pointer to a transaction structure
- @retval NULL No matching transaction structure found
- */
- static coap_server_trans_t *coap_server_find_trans(coap_server_t *server, struct sockaddr_in6 *client_sin, socklen_t client_sin_len)
- {
- coap_server_trans_t *trans = NULL;
- unsigned i = 0;
- for (i = 0; i < COAP_SERVER_NUM_TRANS; i++)
- {
- trans = &server->trans[i];
- if ((trans->active)
- && (trans->client_sin_len == client_sin_len)
- && (memcmp(&trans->client_sin, client_sin, client_sin_len) == 0))
- {
- coap_log_debug("Found existing transaction at index %u", i);
- return trans;
- }
- }
- return NULL;
- }
- /*
- @brief Search for an empty transaction structure in a server structure
- @param[in] server Pointer to a server structure
- @returns Pointer to a transaction structure
- @retval NULL No empty transaction structures available
- */
- static coap_server_trans_t *coap_server_find_empty_trans(coap_server_t *server)
- {
- coap_server_trans_t *trans = NULL;
- unsigned i = 0;
- for (i = 0; i < COAP_SERVER_NUM_TRANS; i++)
- {
- trans = &server->trans[i];
- if (!trans->active)
- {
- coap_log_debug("Found empty transaction at index %u", i);
- return trans;
- }
- }
- return NULL;
- }
- /*
- @brief Search for the oldest transaction structure in a server structure
- Search for the transaction structure in a server structure that was
- used least recently.
- @param[in] server Pointer to a server structure
- @returns Pointer to a transaction structure
- */
- static coap_server_trans_t *coap_server_find_oldest_trans(coap_server_t *server)
- {
- coap_server_trans_t *oldest = NULL;
- coap_server_trans_t *trans = NULL;
- unsigned i = 0;
- unsigned j = 0;
- time_t min_last_use = 0;
- for (i = 0; i < COAP_SERVER_NUM_TRANS; i++)
- {
- trans = &server->trans[i];
- if (trans->active)
- {
- if ((min_last_use == 0) || (trans->last_use < min_last_use))
- {
- oldest = trans;
- min_last_use = trans->last_use;
- j = i;
- }
- }
- }
- coap_log_debug("Found oldest transaction at index %u", j);
- return oldest != NULL ? oldest : &server->trans[0];
- }
- int response_func(coap_msg_t *response_msg);
- int coap_server_exchange(coap_server_t *server);
- /*
- @brief Wait for a message to arrive or an acknowledgement
- timer in any of the active transactions to expire
- @param[in,out] server Pointer to a server structure
- @returns Operation status
- @retval 0 Success
- @retval <0 Error
- */
- static int coap_server_listen(coap_server_t *server)
- {
- coap_server_trans_t *trans = NULL;
- unsigned i = 0;
- fd_set read_fds = {{0}};
- int max_fd = 0;
- int ret = 0;
- int ret_select;
- unsigned msg_id1 = 0;
- clock_t start, end;
- double cpu_time_used;
- LOG(3, "Coap Server Listening...");
- while (1)
- {
- start = 0;
- end = 0;
- start = clock();
- FD_ZERO(&read_fds);
- FD_SET(server->sd, &read_fds);
- FD_SET(linda_fd, &read_fds);
- max_fd = MAX_A(linda_fd, server->sd);
- for (i = 0; i < COAP_SERVER_NUM_TRANS; i++)
- {
- trans = &server->trans[i];
- if (trans->active)
- {
- FD_SET(trans->timer_fd, &read_fds);
- if (trans->timer_fd > max_fd)
- {
- max_fd = trans->timer_fd;
- }
- }
- }
- ret_select = select(max_fd + 1, &read_fds, NULL, NULL, NULL);
- if (ret_select == -1)
- {
- return -errno;
- }
- if (FD_ISSET(server->sd, &read_fds)){
- LOG(3,"Server, dış socket baglantısı aldı");
- ret = coap_server_exchange(server);
- if (ret < 0)
- {
- ERR("coap_server_exchange baslatılamadı");
- }
- ret_select--;
- }
- if (FD_ISSET(linda_fd, &read_fds)){
- LOG(3,"Lindadan mesaj aldı");
- coap_msg_t send_msg = {0};
- coap_msg_create(&send_msg);
- msg_id1 = coap_server_get_next_msg_id(server);
- ret = coap_msg_set_msg_id(&send_msg, msg_id1);
- if (ret < 0){
- ERR("Msg ID set edilemedi!");
- return ret;
- }
- ret=response_func(&send_msg);
- if (ret<0){
- ERR("Error Rosponse Message %s",strerror(ret));
- }
- ret_select--;
- linda_rdy(linda);
- }
- if (ret_select > 0){
- ERR("Okunamayan FD var");
- BOCEK;
- }
- for (i = 0; i < COAP_SERVER_NUM_TRANS; i++){
- trans = &server->trans[i];
- if ((trans->active) && (FD_ISSET(trans->timer_fd, &read_fds))){
- ret = coap_server_trans_handle_ack_timeout(trans);
- if (ret < 0){
- return ret;
- }
- }
- }
- end = clock();
- cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
- INFO("Cpu time used-->%f<--", cpu_time_used);
- }
- return 0;
- }
- /*
- @brief Accept an incoming connection
- @param[in] server Pointer to a server structure
- @param[out] client_sin Pointer to an IPv6 socket structure
- @param[out] client_sin_len Length of the IPv6 socket structure
- Get the address and port number of the client.
- Do not read the received data.
- @returns Number of bytes received or error code
- @retval 0 Success
- @retval <0 Error
- */
- static int coap_server_accept(coap_server_t *server, struct sockaddr_in6 *client_sin, socklen_t *client_sin_len)
- {
- ssize_t num = 0;
- char buf[COAP_MSG_MAX_BUF_LEN] = {0};
- *client_sin_len = sizeof(struct sockaddr_in6);
- num = recvfrom(server->sd, buf, sizeof(buf), MSG_PEEK, (struct sockaddr *)client_sin, client_sin_len);
- if (num == -1)
- {
- return -errno;
- }
- return 0;
- }
- int coap_server_add_sep_resp_uri_path(coap_server_t *server, const char *str)
- {
- return coap_server_path_list_add(&server->sep_list, str);
- }
- int request_func(coap_msg_t *request_msg, char **rqi_hash_key){
- coap_msg_op_t *op = NULL;
- unsigned num = 0;
- unsigned len = 0;
- int index = 0;
- char *val = NULL;
- char *temp = NULL;
- coap_hash_uri_query_t query_num;
- m2m_requestPrimitive_t *preq_primitive =NULL;
- preq_primitive = m2m_requestPrimitive_init();
- if (!preq_primitive){
- ERR("Request Primitive struct hatalı init");
- }
- if (coap_msg_get_code_class(request_msg) == COAP_MSG_REQ){
- LOG(3, "CoAP Rqp mesajı Alındı!");
- /*operation*/
- index = coap_msg_get_code_detail(request_msg);
- switch (index)
- {
- case COAP_MSG_POST :
- preq_primitive->op = 1;
- LOG(3, "CoAP Rqp mesajı operation numarası -->%d<-- type-->POST",preq_primitive->op);
- break;
- case COAP_MSG_GET :
- preq_primitive->op = 2;
- LOG(3, "CoAP Rqp mesajı operation numarası -->%d<-- type-->GET",preq_primitive->op);
- break;
- case COAP_MSG_PUT :
- preq_primitive->op = 3;
- LOG(3, "CoAP Rqp mesajı operation numarası -->%d<-- type-->PUT",preq_primitive->op);
- break;
- case COAP_MSG_DELETE :
- preq_primitive->op = 4;
- LOG(3, "CoAP Rqp mesajı operation numarası -->%d<-- type-->DELETE",preq_primitive->op);
- break;
- default :
- ERR("Undefined operation number -->%d<--",index);
- break;
- }
- index = 0;
- /*Parse all taken request option*/
- op = coap_msg_get_first_op(request_msg);
- while (op != NULL)
- {
- num = coap_msg_op_get_num(op);
- len = coap_msg_op_get_len(op);
- val = coap_msg_op_get_val(op);
- switch (num){
- /*Request Uri-Host parameter parse*/
- case COAP_MSG_URI_HOST :
- preq_primitive->uri_host = strndup(val, len);
- LOG(3, "CoAP Rqp mesajı Uri_Host -->%s<--",preq_primitive->uri_host);
- break;
- /*Request Uri-Port parameter parse*/
- case COAP_MSG_URI_PORT :
- preq_primitive->uri_port = strndup(val, len);
- LOG(3, "CoAP Rqp mesajı Uri_Port -->%s<--",preq_primitive->uri_port);
- if( preq_primitive->uri_host != NULL && preq_primitive->uri_port != NULL){
- sprintf((char *)coap_server_glb.json_poa,"coap://[%s]:%s",preq_primitive->uri_host,preq_primitive->uri_port);
- preq_primitive->poa = strdup(coap_server_glb.json_poa);
- LOG(3, "Coap Rqp mesajı Poa M2M struct-->%s<--", preq_primitive->poa);
- }
- break;
- /*Request To parameter parse*/
- case COAP_MSG_URI_PATH :
- preq_primitive->to = strndup(val, len);
- LOG(3, "CoAP Rqp mesajı TO -->%s<--",preq_primitive->to);
- break;
- /*Content Format parameter parse*/
- case COAP_MSG_CONTENT_FORMAT :
- temp = strndup(val, len);
- preq_primitive->conf = g_hash_table_lookup(hash_coap_conf,temp);
- LOG(3,"Request msg CONF -->%s<--",preq_primitive->conf);
- temp = NULL;
- preq_primitive->pc = m2m_primitiveContent_init();
- break;
- /*Query option search and parse*/
- case COAP_MSG_URI_QUERY :
- for (index = 0; index <= len; index++){
- if(val[index] == '=')
- break;
- }
- temp = strndup(val, (index));
- query_num = atoi(g_hash_table_lookup(hash_coap_uri_query,temp));
- temp = NULL;
- switch(query_num){
- case msg_fc_ty :
- strRemove(val, "ty=");
- if (preq_primitive->op == 1){
- temp = strndup(val, (len-3));
- if (validationCheck(ty,temp) == 1){
- preq_primitive->ty = atoi(temp);
- LOG(3, "CoAP Rqp mesajı ResourceType -->%d<--",preq_primitive->ty);
- }else{
- INFO("Resource Type -->%s<-- is not valid", temp);
- }
- }else {
- if (preq_primitive->fc == NULL){
- preq_primitive->fc = m2m_filterCriteria_init();
- }
- temp = strndup(val, (len-3));
- if (validationCheck(ty,temp) == 1){
- preq_primitive->fc->ty = atoi(temp);
- LOG(3, "CoAP Rqp mesajı Filte Criteria ResourceType -->%d<--",preq_primitive->fc->ty);
- }else{
- INFO("Filter Criteria Resource Type -->%s<-- is not valid", temp);
- }
- }
- temp = NULL;
- break;
- case msg_rt :
- preq_primitive->rti = m2m_responseTypeInfo_init();
- if (!preq_primitive->rti){
- ERR("Request Primitive, Response type info struct init hatalı");
- }
- strRemove(val, "rt=");
- temp = strndup(val,(len-3));
- if (validationCheck(rt,temp) == 1){
- preq_primitive->rti->rt = atoi(temp);
- LOG(3, "CoAP Rqp mesajı ResponseType -->%d<--",preq_primitive->rti->rt);
- }else{
- INFO("Response Type -->%s<-- not valid", temp);
- }
- temp = NULL;
- break;
- case msg_rp :
- strRemove(val, "rp=");
- temp = strndup(val,(len-3));
- preq_primitive->rp = atoi(temp);
- LOG(3, "CoAP Rqp mesajı ResultPersistence -->%d<--",preq_primitive->rp);
- temp = NULL;
- break;
- case msg_rcn :
- strRemove(val, "rcn=");
- temp = strndup(val,(len-4));
- if (validationCheck(rcn,temp) == 1){
- preq_primitive->rcn = atoi(temp);
- LOG(3, "CoAP Rqp mesajı ResultContent -->%d<--",preq_primitive->rcn);
- }else{
- INFO("Result Content -->%s<-- not valid", temp);
- }
- temp = NULL;
- break;
- case msg_da :
- strRemove(val, "da=");
- temp = strndup(val,(len-3));
- if (validationCheck(da,temp) == 1){
- if(strcmp(temp, "true") == 0){
- preq_primitive->da = 1;
- }else if (strcmp(temp, "false") == 0){
- preq_primitive->da = 2;
- }
- LOG(3, "CoAP Rqp mesajı DeliveryAggregation -->%d<--",preq_primitive->da);
- }else{
- INFO("Delivery Aggregation -->%s<-- not valid", temp);
- }
- temp = NULL;
- break;
- case fc_crb :
- if (preq_primitive->fc == NULL)
- preq_primitive->fc = m2m_filterCriteria_init();
- strRemove(val, "crb=");
- temp = strndup(val, (len-4));
- if (validationCheck(crb,temp) == 1){
- preq_primitive->fc->crb = strndup(val,(len-4));
- LOG(3, "CoAP Rqp mesajı FilterCriteria CreatedBefore -->%s<--",preq_primitive->fc->crb);
- }else{
- INFO("Created Before -->%s<-- not valid", temp);
- }
- temp = NULL;
- break;
- case fc_cra :
- if (preq_primitive->fc == NULL)
- preq_primitive->fc = m2m_filterCriteria_init();
- strRemove(val, "cra=");
- temp = strndup(val, (len-4));
- if (validationCheck(cra,temp) == 1){
- preq_primitive->fc->cra = strndup(val,(len-4));
- LOG(3, "CoAP Rqp mesajı FilterCriteria CreatedAfter -->%s<--",preq_primitive->fc->cra);
- }else{
- INFO("Created After -->%s<-- not valid", temp);
- }
- temp = NULL;
- break;
- case fc_ms :
- if (preq_primitive->fc == NULL)
- preq_primitive->fc = m2m_filterCriteria_init();
- strRemove(val, "ms=");
- temp = strndup(val, (len-4));
- if (validationCheck(ms,temp) == 1){
- preq_primitive->fc->ms = strndup(val,(len-3));
- LOG(3, "CoAP Rqp mesajı FilterCriteria ModifiedSince -->%s<--",preq_primitive->fc->ms);
- }else{
- INFO("Modified Since -->%s<-- not valid", temp);
- }
- temp = NULL;
- break;
- case fc_us :
- if (preq_primitive->fc == NULL)
- preq_primitive->fc = m2m_filterCriteria_init();
- strRemove(val, "us=");
- temp = strndup(val, (len-4));
- if (validationCheck(us,temp) == 1){
- preq_primitive->fc->us = strndup(val,(len-3));
- LOG(3, "CoAP Rqp mesajı FilterCriteria UnmodifiedSince -->%s<--",preq_primitive->fc->us);
- }else{
- INFO("Unmodified Since -->%s<-- not valid", temp);
- }
- temp = NULL;
- break;
- case fc_sts :
- if (preq_primitive->fc == NULL)
- preq_primitive->fc = m2m_filterCriteria_init();
- strRemove(val, "sts=");
- temp = strndup(val,(len-4));
- if (validationCheck(sts,temp) == 1){
- preq_primitive->fc->sts = atoi(temp);
- LOG(3, "CoAP Rqp mesajı FilterCriteria StateTagSmaller -->%d<--",preq_primitive->fc->sts);
- }else{
- INFO("State Tag Smaller -->%s<-- not valid", temp);
- }
- temp = NULL;
- break;
- case fc_stb :
- if (preq_primitive->fc == NULL)
- preq_primitive->fc = m2m_filterCriteria_init();
- strRemove(val, "stb=");
- temp = strndup(val,(len-4));
- if (validationCheck(stb,temp) == 1){
- preq_primitive->fc->stb = atoi(temp);
- LOG(3, "CoAP Rqp mesajı FilterCriteria StateTagBigger -->%d<--",preq_primitive->fc->stb);
- }else{
- INFO("State Tag Bigger -->%s<-- not valid", temp);
- }
- temp = NULL;
- break;
- case fc_exb :
- if (preq_primitive->fc == NULL)
- preq_primitive->fc = m2m_filterCriteria_init();
- strRemove(val, "exb=");
- temp = strndup(val,(len-4));
- if (validationCheck(exb,temp) == 1){
- preq_primitive->fc->exb = strndup(val,(len-4));
- LOG(3, "CoAP Rqp mesajı FilterCriteria ExpireBefore -->%s<--",preq_primitive->fc->exb);
- }else{
- INFO("Expire Before -->%s<-- not valid", temp);
- }
- temp = NULL;
- break;
- case fc_exa :
- if (preq_primitive->fc == NULL)
- preq_primitive->fc = m2m_filterCriteria_init();
- strRemove(val, "exa=");
- temp = strndup(val,(len-4));
- if (validationCheck(exa,temp) == 1){
- preq_primitive->fc->exa = strndup(val,(len-4));
- LOG(3, "CoAP Rqp mesajı FilterCriteria ExpireAfter -->%s<--",preq_primitive->fc->exa);
- }else{
- INFO("Expire After -->%s<-- not valid", temp);
- }
- temp = NULL;
- break;
- case fc_lbl :
- if (preq_primitive->fc == NULL)
- preq_primitive->fc = m2m_filterCriteria_init();
- strRemove(val, "lbl=");
- preq_primitive->fc->lbl = strndup(val,(len-4));
- LOG(3, "CoAP Rqp mesajı FilterCriteria Labels -->%s<--",preq_primitive->fc->lbl);
- break;
- case fc_szb :
- if (preq_primitive->fc == NULL)
- preq_primitive->fc = m2m_filterCriteria_init();
- strRemove(val, "szb=");
- temp = strndup(val,(len-4));
- if (validationCheck(szb,temp) == 1){
- preq_primitive->fc->szb = atoi(temp);
- LOG(3, "CoAP Rqp mesajı FilterCriteria SizeBelow -->%d<--",preq_primitive->fc->sza);
- }else{
- INFO("Size Below -->%s<-- not valid", temp);
- }
- temp = NULL;
- break;
- case fc_sza :
- if (preq_primitive->fc == NULL)
- preq_primitive->fc = m2m_filterCriteria_init();
- strRemove(val, "sza=");
- temp = strndup(val,(len-4));
- if (validationCheck(sza,temp) == 1){
- preq_primitive->fc->sza= atoi(temp);
- LOG(3, "CoAP Rqp mesajı FilterCriteria SizeAbove -->%d<--",preq_primitive->fc->szb);
- }else{
- INFO("Size Above -->%s<-- not valid", temp);
- }
- temp = NULL;
- break;
- case fc_cty :
- if (preq_primitive->fc == NULL)
- preq_primitive->fc = m2m_filterCriteria_init();
- strRemove(val,"cty=");
- preq_primitive->fc->cty = strndup(val,(len-4));
- LOG(3, "CoAP Rqp mesajı FilterCriteria ContentType -->%s<--",preq_primitive->fc->cty);
- break;
- case fc_fu :
- if (preq_primitive->fc == NULL)
- preq_primitive->fc = m2m_filterCriteria_init();
- strRemove(val, "fu=");
- temp = strndup(val,(len-3));
- preq_primitive->fc->fu = atoi(temp);
- LOG(3, "CoAP Rqp mesajı FilterCriteria FilterUsage -->%d<--",preq_primitive->fc->fu);
- temp = NULL;
- break;
- case fc_lim :
- if (preq_primitive->fc == NULL)
- preq_primitive->fc = m2m_filterCriteria_init();
- strRemove(val, "lim=");
- temp = strndup(val,(len-4));
- if (validationCheck(lim,temp) == 1){
- preq_primitive->fc->lim = atoi(temp);
- LOG(3, "CoAP Rqp mesajı FilterCriteria Limit -->%d<--",preq_primitive->fc->lim);
- }else{
- INFO("Limit -->%s<-- not valid", temp);
- }
- temp = NULL;
- break;
- case msg_drt :
- strRemove(val, "drt=");
- temp = strndup(val,(len-4));
- if (validationCheck(drt,temp) == 1){
- preq_primitive->drt = atoi(temp);
- LOG(3, "CoAP Rqp mesajı Discovery Result Type -->%d<--",preq_primitive->drt);
- }else{
- INFO("Discovery Result Type -->%s<-- not valid", temp);
- }
- temp = NULL;
- break;
- case fc_atr_nm_cr :
- if (preq_primitive->fc == NULL)
- preq_primitive->fc = m2m_filterCriteria_init();
- if (preq_primitive->fc->atr == NULL)
- preq_primitive->fc->atr = m2m_attribute_init();
- strRemove(val,"cr=");
- preq_primitive->fc->atr->nm = strdup("cr");
- preq_primitive->fc->atr->val = strndup(val,(len-3));
- LOG(3, "CoAP Rqp mesajı FilterCriteria Attribute Name -->%s<--",preq_primitive->fc->atr->nm);
- LOG(3, "CoAP Rqp mesajı FilterCriteria Attribute Value -->%s<--",preq_primitive->fc->atr->val);
- break;
- case fc_atr_nm_mid :
- if (preq_primitive->fc == NULL)
- preq_primitive->fc = m2m_filterCriteria_init();
- if (preq_primitive->fc->atr == NULL)
- preq_primitive->fc->atr = m2m_attribute_init();
- strRemove(val,"mid=");
- preq_primitive->fc->atr->nm = strdup("mid");
- preq_primitive->fc->atr->val = strndup(val,(len-4));
- LOG(3, "CoAP Rqp mesajı FilterCriteria Attribute Name -->%s<--",preq_primitive->fc->atr->nm);
- LOG(3, "CoAP Rqp mesajı FilterCriteria Attribute Value -->%s<--",preq_primitive->fc->atr->val);
- break;
- default :
- ERR("Undefined Query option");
- break;
- }
- temp = NULL;
- index = 0;
- break;
- /*Request massage OneM2M From parameter parse*/
- case oneM2M_FR :
- preq_primitive->fr = strndup(val,len);
- LOG(3, "CoAP Rqp mesajı From -->%s<--",preq_primitive->fr);
- break;
- /*Request massage OneM2M RequestIdentifier parameter parse*/
- case oneM2M_RQI :
- *rqi_hash_key = strndup(val,len);
- preq_primitive->rqi = strndup(val, len);
- LOG(3, "CoAP Rqp mesajı RequestIdentifier -->%s<--",preq_primitive->rqi);
- break;
- /*Request massage OneM2M Name parameter parse*/
- case oneM2M_NM :
- preq_primitive->nm = strndup(val, len);
- LOG(3, "CoAP Rqp mesajı Name -->%s<--",preq_primitive->nm);
- break;
- /*Request massage OneM2M OriginatingTimestamp parameter parse*/
- case oneM2M_OT :
- temp = strndup(val, len);
- if (validationCheck(ot,temp) == 1){
- preq_primitive->ot = strndup(val,len);
- LOG(3, "CoAP Rqp mesajı OriginatingTimestamp -->%s<---",preq_primitive->ot);
- }else{
- INFO("Originating Timestamp -->%s<-- not valid", temp);
- }
- temp = NULL;
- break;
- /*Request massage OneM2M RequestExpirationTimestamp parameter parse*/
- case oneM2M_RQET :
- temp = strndup(val, len);
- if (validationCheck(rqet,temp) == 1){
- preq_primitive->rqet = strndup(val,len);
- LOG(3, "CoAP Rqp mesajı RequestExpirationTimestamp -->%s<--",preq_primitive->rqet);
- }else{
- INFO("Rquest Expiration Timestamp -->%s<-- not valid", temp);
- }
- temp = NULL;
- break;
- /*Request massage OneM2M ResultExpirationTimestamp parameter parse*/
- case oneM2M_RSET :
- temp = strndup(val, len);
- if (validationCheck(rset,temp) == 1){
- preq_primitive->rset = strndup (val,len);
- LOG(3, "CoAP Rqp mesajı ResultExpirationTimestamp -->%s<--",preq_primitive->rset);
- }else{
- INFO("Result Expiration Timestamp -->%s<-- not valid", temp);
- }
- temp = NULL;
- break;
- /*Request massage OneM2M OperationExecutionTime parameter parse*/
- case oneM2M_OET :
- temp = strndup(val, len);
- if (validationCheck(oet,temp) == 1){
- preq_primitive->oet = strndup(val,len);
- LOG(3, "CoAP Rqp mesajı OperationExecutionTime -->%s<--",preq_primitive->oet);
- }else{
- INFO("Operation Execution Timestamp -->%s<-- not valid", temp);
- }
- temp = NULL;
- break;
- /*Request massage OneM2M NotificationURI parameter parse*/
- case oneM2M_RTURI :
- preq_primitive->rti->nu = strndup(val,len);
- LOG(3, "CoAP Rqp mesajı NotificationURI -->%s<--",preq_primitive->rti->nu);
- break;
- /*Request massage OneM2M EventCategory parameter parse*/
- case oneM2M_EC :
- temp = strndup(val,len);
- if (validationCheck(ec,temp) == 1){
- preq_primitive->ec = atoi(temp);
- LOG(3, "CoAP Rqp mesajı EventCategory -->%d<--",preq_primitive->ec);
- }else{
- INFO("Event Category -->%s<-- not valid", temp);
- }
- temp = NULL;
- break;
- /*Request massage OneM2M GroupRequestIdentifier parameter parse*/
- case oneM2M_GID :
- preq_primitive->gid = strndup(val,len);
- LOG(3, "CoAP Rqp mesajı GroupRequestIdentifier -->%s<--",preq_primitive->gid);
- break;
- default :
- ERR("Undefiened COAP option -->%d<--",num);
- break;
- }
- op = coap_msg_op_get_next(op);
- }
- if (strcmp(preq_primitive->conf, "vnd.onem2m-preq+xml") == 0){
- preq_primitive->pc->content = strndup(coap_msg_get_payload(request_msg), coap_msg_get_payload_len(request_msg));
- preq_primitive->pc->ty = xmlCnt;
- LOG(3, "Content Xml -->%s<--", (char *)preq_primitive->pc->content);
- if(sendRequest((void *)preq_primitive, linda_ip, atoi(linda_port), group1, COAP_SERVER_GROUP) == -1){
- ERR("Flat-Xml branching e gönderilemedi");
- }
- }else if (strcmp(preq_primitive->conf, "vnd.onem2m-preq+json") == 0){
- preq_primitive->pc->content = strndup(coap_msg_get_payload(request_msg), coap_msg_get_payload_len(request_msg));
- preq_primitive->pc->ty = jsonCnt;
- LOG(3, "Content Json -->%s<--", (char *)preq_primitive->pc->content);
- if(sendRequest((void *)preq_primitive, linda_ip, atoi(linda_port), group1, COAP_SERVER_GROUP) == -1){
- ERR("Flat-Json branching e gönderilemedi");
- }
- }else {
- ERR("Undefined Content Format");
- }
- }
- return 0;
- }
- int response_func(coap_msg_t *response_msg){
- char *linda_buff;
- xmlBuffer *xml_buffer = xmlBufferCreate();
- int ret = 0;
- int countt,flat_index;
- linda_buff = NULL;
- /* @brief global timeout değişkenleri */
- int linda_timeout_sec = 5;
- int linda_timeout_usec = 0;
- coap_server_trans_t *trans_struct = NULL;
- ssize_t num = 0;
- int res;
- Tag tag;
- int len;
- int search_check;
- char *value=NULL;
- int from_linda;
- int counter;
- bool table_check;
- char *conf_;
- char *json_buffer = NULL;
- char *rsp_uri_host = NULL;
- char *rsp_uri_port = NULL;
- char *temp = NULL;
- if(heap == NULL) {
- /* flat veri değişkenlerini tanımla ve yer al */
- heap = (heap_table_t *)heap_init(4096);
- if(heap == NULL) {
- LOG(3,"Bellek Bitmiş!");
- BOCEK;
- }
- }
- else {
- heap_reset((void *)heap);
- }
- usleep(500000);
- res= linda_read(linda, linda_timeout_sec, linda_timeout_usec, &from_linda, &tag, &len, &value);
- if ( res < 0 ){
- ERR( "Bağlantı Hatası.");
- return -1;
- }
- if ( res == 0 ){
- LOG(3,"süreaşımı");
- }
- /* Veri gelmiş.
- len== 0 ise linda kapanmış olabilir.
- */
- if ( len <= 0 ){
- ERR( "Bağlantı Hatası. Mailbox Kapalı Olabilir..");
- return -1;
- }
- if (len >= 4 && strncmp(value, "LL=", 3) == 0){
- int x;
- x= value[3]-'0';
- if (x >= 0 && x <= 3){
- log_level = x;
- /* Log seviyesi değiştiğinde her durumda log seviyesi görülebilmeli. O yüzden seviye 1*/
- LOG(1, "Log seviyesi %d yapıldı", log_level);
- }else{
- ERR("Log seviyesi %d range dışı. Log seviyesi default değer ile devam edecek", x);
- }
- }
- free(linda_buff);
- if (!value)
- ERR("Gelen veri okunamadı");
- linda_buff = malloc(len + 1);
- memcpy( linda_buff, value, len);
- linda_buff[len] = '\0';
- LOG(3, "Buf: %s",linda_buff );
- linda_set_heap_params((void *)heap, value, len);
- flat_index = heap->root_index;
- temp = getNodeValFromHeap(heap, flat_index, poa, 0);
- if(temp != NULL){
- len = strlen(temp);
- for (counter = 0; counter <= len; counter++){
- if(temp[counter] == ']')
- break;
- }
- rsp_uri_host = strndup((temp+8),(counter-8));
- rsp_uri_port = strndup((temp + (counter+2)), 4);
- ret = coap_msg_add_op(response_msg,COAP_MSG_URI_HOST,strlen(rsp_uri_host),rsp_uri_host);/*3 nolu option set edildi*/
- if (ret != 0)
- {
- LOG(3, "Response mesajı Uri-Host set edilemedi. Paket ilgili field olmadan yaratılıyor...");
- }else{
- LOG(3, "Response mesajı set edilen Urı-Host -->%s<--", rsp_uri_host);
- }
- ret = coap_msg_add_op(response_msg,COAP_MSG_URI_PORT,strlen(rsp_uri_port),rsp_uri_port);/*7 nolu option set edildi*/
- if (ret != 0)
- {
- LOG(3, "Response mesajı Uri-Port set edilemedi. Paket ilgili field olmadan yaratılıyor...");
- }else{
- LOG(3,"Response mesajı set edilen Uri-Port -->%s<--", rsp_uri_port);
- }
- temp = NULL;
- }else{
- /*@todo ilgili mesajın rqi veya unique bir parametresi eklenecek*/
- ERR("Response mesajında POA cekilemedi");
- }
- temp = getNodeValFromHeap(heap, flat_index, to, 0);
- if(temp != NULL){
- ret = coap_msg_add_op(response_msg,COAP_MSG_URI_PATH,strlen(temp),temp);/*11 nolu option set edildi*/
- if (ret != 0)
- {
- LOG(3, "Response mesajı TO set edilemedi. Paket ilgili field olmadan yaratılıyor...");
- }else{
- LOG(3,"Resposen mesajı set edilen TO -->%s<--", temp);
- }
- temp = NULL;
- }
- search_check = searchNodeInHeap(heap, flat_index, conf, 0, &countt);
- if (search_check == -1){
- ERR("Coap Server Response verinin içinde CONF bulamadı!!!");
- }else{
- LOG(3, "Response mesajı içinde CONF bulundu. Parse ediliyor");
- conf_ = getNodeValFromHeap(heap, flat_index, conf, 0);
- if(conf_ == NULL){
- ERR("Response mesajı içinden CONF çekilemedi");
- }else{
- LOG(3, "Response mesajı içinden CONF çekildi -->%s<--", conf_);
- temp = g_hash_table_lookup(hash_coap_conf,conf_);
- ret = coap_msg_add_op(response_msg,COAP_MSG_CONTENT_FORMAT,strlen(temp),temp);/*12 nılu option set edildi*/
- if (ret != 0){
- LOG(3, "Response mesajı COAP_MSG_CONTENT_FORMAT set edilemedi. Paket ilgili field olmadan yaratılıyor...");
- }else{
- LOG(3, "Coap response mesajı CONF set edildi -->%s<--", temp);
- }
- temp=NULL;
- }
- }
- temp = getNodeValFromHeap(heap, flat_index, fr, 0);
- if(temp != NULL){
- ret = coap_msg_add_op(response_msg,oneM2M_FR,strlen(temp),temp);/*from set edildi*/
- if (ret != 0){
- LOG(3, "Response mesajı FROM set edilemedi. Paket ilgili field olmadan yaratılıyor...");
- }else{
- LOG(3, "Response mesajı FROM set edildi -->%s<--", temp);
- }
- temp = NULL;
- }else{
- INFO("Response mesajında FROM bulunamadı");
- }
- char *rqi_;
- rqi_ = getNodeValFromHeap(heap, flat_index, rqi, 0);
- if(rqi_ != NULL){
- LOG(3, "rqi -->%s<--", rqi_);
- ret = coap_msg_add_op(response_msg,oneM2M_RQI,strlen(rqi_),rqi_);
- if (ret != 0)
- {
- coap_msg_destroy(response_msg);
- ERR("Response mesajı Request Identifier set edilemedi. Paket yok edildi...");
- return ret;
- }else{
- LOG(3, "Response mesajı RQI set edildi -->%s<--", rqi_);
- }
- }else{
- INFO("Response mesajında RQI bulunamdı");
- }
- temp = getNodeValFromHeap(heap, flat_index, ot, 0);
- if(temp != NULL){
- LOG(3, "ot -->%s<--", temp);
- ret = coap_msg_add_op(response_msg,oneM2M_OT,strlen(temp),temp);/*originatingTimestamp set edildi*/
- if (ret != 0){
- LOG(3, "Response mesajı OriginatingTimestamp set edilemedi. Paket ilgili field olmadan yaratılıyor...");
- }else{
- LOG(3, "Response mesajı OT set edildi -->%s<--", temp);
- }
- temp = NULL;
- }else{
- INFO("Response mesajında OT bulunamdı");
- }
- temp = getNodeValFromHeap(heap, flat_index, rset, 0);
- if(temp != NULL){
- ret = coap_msg_add_op(response_msg,oneM2M_RSET,strlen(temp), temp);/*resultExpirationTimestamp set edildi*/
- if (ret != 0)
- {
- LOG(3, "Response mesajı ResultExpirationTimestamp set edilemedi. Paket ilgili field olmadan yaratılıyor...");
- }else{
- LOG(3, "Response mesajı RSET set edildi -->%s<--", temp);
- }
- temp = NULL;
- }else{
- INFO("Response mesajında RSET bulunamadı");
- }
- char ec_[256];
- if (getNodeIntFromHeap(heap, flat_index, ec, 0) > 0){
- sprintf(ec_, "%d", getNodeIntFromHeap(heap, flat_index, ec, 0));
- LOG(3, "ec -->%s<--", ec_);
- ret = coap_msg_add_op(response_msg,oneM2M_EC,strlen(ec_),ec_);
- if (ret != 0)
- {
- LOG(3, "Response mesajı eventCategory set edilemedi. Paket ilgili field olmadan yaratılıyor...");
- }else{
- LOG(3, "Response mesajı EC set edildi -->%s<--", ec_);
- }
- }else{
- INFO("Response mesajı içinde EC bulunamdı");
- }
- int pc_index = searchNodeInHeap(heap, flat_index, pc, 0, &countt);
- if (pc_index == -1){
- LOG(3, "Coap Server Lindadan aldığı verinin içinde content bulamadı!!");
- }
- else if (strcmp(conf_, "vnd.onem2m-prsp+xml") == 0){
- LOG(3, "Flat Content XML e dönüştürülecek");
- /*content geldi, asagisi xml uzunluk doner*/
- pc_index = flatten_to_xml( heap, get_heap_data(heap, pc_index), &xml_buffer);
- if (pc_index == -1){
- ERR("Flat content XML yapılamadı!!");
- BOCEK;
- }
- else{
- LOG(3, "Dönüş XML i \n\n%s", xml_buffer->content );
- ret=coap_msg_set_payload(response_msg, (char *)xml_buffer->content, strlen((char *)xml_buffer->content));
- if (ret !=0)
- {
- LOG(3, "Response mesajı Content set edilemedi. Paket ilgili field olmadan yaratılıyor...");
- }
- }
- }else if(strcmp(conf_, "vnd.onem2m-prsp+json") == 0){
- search_check = flatten_to_json(heap, pc_index, &json_buffer);
- if (search_check == -1){
- ERR("Flat content JSON yapılamadı!!!");
- BOCEK;
- }else{
- LOG(3, "Dönüş JSON mesajı %s", json_buffer);
- ret = coap_msg_set_payload(response_msg, json_buffer, strlen(json_buffer));
- if (ret != 0){
- LOG(3, "Response mesajı Content set edilemedi. Paket ilgili field olmadan yaratılıyor...");
- }
- }
- }
- ret = coap_msg_set_type(response_msg, COAP_MSG_ACK);
- if (ret != 0)
- {
- coap_msg_destroy(response_msg);
- ERR("Response mesajı type set edilemedi. Paket yok edildi...");
- return ret;
- }
- char rsc__[256];
- if (getNodeIntFromHeap(heap, flat_index, rsc, 0) > 0){
- sprintf(rsc__, "%d", getNodeIntFromHeap(heap, flat_index, rsc, 0));
- LOG(3, "rsc -->%s<--", rsc__);
- ret = coap_msg_set_code(response_msg, atoi(g_hash_table_lookup(hash_pri_to_coap_code,rsc__)),atoi(g_hash_table_lookup(hash_pri_to_coap_detail,rsc__)));
- if (ret != 0)
- {
- coap_msg_destroy(response_msg);
- ERR("Response mesajı Status Code set edilemedi. Paket yok edildi...");
- return ret;
- }
- ret = coap_msg_add_op(response_msg,oneM2M_RSC,strlen(rsc__),rsc__);
- if (ret != 0)
- {
- LOG(3, "Response mesajı responseStatusCode set edilemedi. Paket ilgili field olmadan yaratılıyor...");
- }else{
- LOG(3, "Response mesajı RSC set edildi -->%s<--", rsc__);
- }
- }else{
- ERR("Response mesajı RSC okunamadı");
- }
- temp = getNodeValFromHeap(heap, flat_index, conf, 0);
- if(temp != NULL){
- LOG(3, "rsp_conf -->%s<--", temp);
- }
- LOG(3,"Hash talblolarında aranacak olan key RQI -->%s<--",temp);
- LOG(3,"Token Hash tablosunda bulunan toplam key sayisi -->%d<--", g_hash_table_size(hash));
- if(g_hash_table_lookup(hash,rqi_)==NULL){
- ERR("Response mesajında gelen RQI, hash tablosunda eşleşmedi");
- return -1;
- }else{
- char *hash_alinan=NULL;
- hash_alinan = (char *)g_hash_table_lookup(hash,rqi_);
- ret = coap_msg_set_token(response_msg,hash_alinan,strlen(hash_alinan));
- if (ret != 0)
- {
- coap_msg_destroy(response_msg);
- ERR("Response mesajı Token set edilemedi. Paket yok edildi...");
- return ret;
- }
- }
- LOG(3,"Trans-struct Hash tablosunda toplam bulunan key sayısı -->%d<--", g_hash_table_size(hash_table_struct));
- if(g_hash_table_lookup(hash_table_struct,rqi_)==NULL){
- ERR("Response mesajında gelen RQI -->%s<--, hash tablosunda eşleşmedi",rqi_);
- return -1;
- }else{
- LOG(3, "Response mesajı RQI-->transaction structı eşleşti");
- trans_struct =(coap_server_trans_t *) (g_hash_table_lookup(hash_table_struct, rqi_));
- }
- table_check=g_hash_table_remove(hash,rqi_);
- if(!table_check){
- ERR("RQI -->%s<-- key olarak token hash tablosundan silinemedi!!!",rqi_);
- }else if(table_check){
- LOG(3,"RQI -->%s<-- başarıyla token hash tablosundan silindi",rqi_)
- }
- table_check=g_hash_table_remove(hash_table_struct,rqi_);
- if(!table_check){
- ERR("RQI -->%s<-- key olarak trans struct hash tablosundan silinemedi!!!",rqi_);
- }else if(table_check){
- LOG(3,"RQI -->%s<-- başarıyla trans-struct hash tablosundan silindi",rqi_)
- }
- print_coap_msge("send:", response_msg);
- num = coap_server_trans_send(trans_struct, response_msg);
- if (num < 0)
- {
- coap_server_trans_destroy(trans_struct);
- return num;
- }
- return 0;
- }
- /*
- @brief Determine whether a request warrants a piggy-backed
- response or a separate response
- This function makes the decision on whether to send a separate
- response or a piggy-backed response by searching for the URI
- path taken from the request message structure in a user supplied
- URI path list. The idea being that some resources will consistently
- require time to retrieve and others will not.
- @param[in] server Pointer to a server structure
- @param[in] msg Pointer to a message structure
- @returns Response type
- @retval COAP_SERVER_PIGGYBACKED Piggy-backed response
- @retval COAP_SERVER_SEPARATE Separate response
- */
- static int coap_server_get_resp_type(coap_server_t *server, coap_msg_t *msg)
- {
- coap_msg_op_t *op = NULL;
- size_t val_len = 0;
- size_t add = 0;
- size_t len = 0;
- char val_buf[COAP_MSG_OP_URI_PATH_MAX_LEN] = {0};
- char buf[COAP_MSG_OP_URI_PATH_MAX_LEN] = {0};
- char *val = NULL;
- char *p = NULL;
- int match = 0;
- p = buf;
- len = sizeof(buf) - 1;
- op = coap_msg_get_first_op(msg);
- while (op != NULL)
- {
- strncpy(p, "/", len);
- add = (1 < len) ? 1 : len;
- p += add;
- len -= add;
- val = coap_msg_op_get_val(op);
- val_len = coap_msg_op_get_len(op);
- if (val_len > sizeof(val_buf) - 1)
- val_len = sizeof(val_buf) - 1;
- memcpy(val_buf, val, val_len);
- strncpy(p, val_buf, len);
- add = (val_len < len) ? val_len : len;
- p += add;
- len -= add;
- op = coap_msg_op_get_next(op);
- }
- if (p == buf)
- {
- buf[0] = '/';
- }
- match = coap_server_path_list_match(&server->sep_list, buf);
- return match ? COAP_SERVER_SEPARATE : COAP_SERVER_PIGGYBACKED;
- }
- /*
- @brief Receive a request from the client and send the response
- @param[in,out] server Pointer to a client structure
- @returns Operation status
- @retval 0 Success
- @retval <0 Error
- */
- int coap_server_exchange(coap_server_t *server)
- {
- struct sockaddr_in6 client_sin = {0};
- coap_server_trans_t *trans = NULL;
- coap_msg_t recv_msg = {0};
- coap_msg_t send_msg = {0};
- socklen_t client_sin_len = 0;
- unsigned op_num = 0;
- ssize_t num = 0;
- int resp_type = 0;
- int ret = 0;
- bool hash_check;
- /* accept incoming connection */
- ret = coap_server_accept(server, &client_sin, &client_sin_len);
- if (ret < 0)
- {
- return ret;
- }
- /* find or create transaction */
- trans = coap_server_find_trans(server, &client_sin, client_sin_len);
- if (trans == NULL)
- {
- trans = coap_server_find_empty_trans(server);
- if (trans == NULL)
- {
- trans = coap_server_find_oldest_trans(server);
- coap_server_trans_destroy(trans);
- }
- ret = coap_server_trans_create(trans, server, &client_sin, client_sin_len);
- if (ret < 0)
- {
- return ret;
- }
- #ifdef COAP_DTLS_EN
- /* if DTLS is enabled then coap_server_trans_create has consumed */
- /* the received data as part of the handshake, we need to wait for */
- /* more data to arrive and identify the sender */
- return 0;
- #endif
- }
- coap_server_glb.poa_host=trans->client_addr;
- sprintf((char *)coap_server_glb.poa_port,"%u",ntohs(trans->client_sin.sin6_port));
- /* receive message */
- coap_msg_create(&recv_msg);
- num = coap_server_trans_recv(trans, &recv_msg);
- if (num < 0)
- {
- coap_msg_destroy(&recv_msg);
- coap_server_trans_destroy(trans);
- return num;
- }
- /* check for duplicate request */
- if (coap_server_trans_match_req(trans, &recv_msg))
- {
- if (coap_msg_get_type(&recv_msg) == COAP_MSG_CON)
- {
- /* message deduplication */
- /* acknowledge the (confirmable) request again */
- /* do not send the response again */
- coap_log_info("Received duplicate confirmable request from address %s and port %u", trans->client_addr, ntohs(trans->client_sin.sin6_port));
- ret = coap_server_trans_send_ack(trans, &recv_msg);
- coap_msg_destroy(&recv_msg);
- if (ret < 0)
- {
- coap_server_trans_destroy(trans);
- return ret;
- }
- return 0;
- }
- else if (coap_msg_get_type(&recv_msg) == COAP_MSG_NON)
- {
- /* message deduplication */
- /* do not acknowledge the (non-confirmable) request again */
- /* do not send the response again */
- coap_log_info("Received duplicate non-confirmable request from address %s and port %u", trans->client_addr, ntohs(trans->client_sin.sin6_port));
- coap_msg_destroy(&recv_msg);
- return 0;
- }
- }
- /* check for an ack for a previous response */
- if (coap_server_trans_match_resp(trans, &recv_msg))
- {
- if (coap_msg_get_type(&recv_msg) == COAP_MSG_ACK)
- {
- /* the server must stop retransmitting its response */
- /* on any matching acknowledgement or reset message */
- coap_log_info("Received acknowledgement from address %s and port %u", trans->client_addr, ntohs(trans->client_sin.sin6_port));
- ret = coap_server_trans_stop_ack_timer(trans);
- coap_msg_destroy(&recv_msg);
- if (ret < 0)
- {
- coap_server_trans_destroy(trans);
- return ret;
- }
- return 0;
- }
- else if (coap_msg_get_type(&recv_msg) == COAP_MSG_RST)
- {
- /* the server must stop retransmitting its response */
- /* on any matching acknowledgement or reset message */
- coap_log_info("Received reset from address %s and port %u", trans->client_addr, ntohs(trans->client_sin.sin6_port));
- ret = coap_server_trans_stop_ack_timer(trans);
- coap_msg_destroy(&recv_msg);
- if (ret < 0)
- {
- coap_server_trans_destroy(trans);
- return ret;
- }
- return 0;
- }
- }
- /* check for a valid request */
- if ((coap_msg_get_type(&recv_msg) == COAP_MSG_ACK)
- || (coap_msg_get_type(&recv_msg) == COAP_MSG_RST)
- || (coap_msg_get_code_class(&recv_msg) != COAP_MSG_REQ))
- {
- ret = coap_server_trans_reject(trans, &recv_msg);
- coap_msg_destroy(&recv_msg);
- coap_server_trans_destroy(trans);
- return ret;
- }
- op_num = coap_server_check_options(&recv_msg);
- if (op_num != 0)
- {
- ret = coap_server_trans_reject_bad_option(trans, &recv_msg, op_num);
- coap_msg_destroy(&recv_msg);
- coap_server_trans_destroy(trans);
- return ret;
- }
- /* clear details of the previous request/response */
- coap_server_trans_clear_req(trans);
- coap_server_trans_clear_resp(trans);
- if (coap_msg_get_type(&recv_msg) == COAP_MSG_CON)
- {
- coap_log_info("Received confirmable request from address %s and port %u", trans->client_addr, ntohs(trans->client_sin.sin6_port));
- }
- else if (coap_msg_get_type(&recv_msg) == COAP_MSG_NON)
- {
- coap_log_info("Received non-confirmable request from address %s and port %u", trans->client_addr, ntohs(trans->client_sin.sin6_port));
- }
- /* determine response type */
- if (coap_msg_get_type(&recv_msg) == COAP_MSG_CON)
- {
- resp_type = coap_server_get_resp_type(server, &recv_msg);
- if (resp_type == COAP_SERVER_SEPARATE)
- coap_log_info("Request URI path requires a separate response to address %s and port %u", trans->client_addr, ntohs(trans->client_sin.sin6_port));
- else
- coap_log_info("Request URI path requires a piggy-backed response to address %s and port %u", trans->client_addr, ntohs(trans->client_sin.sin6_port));
- }
- /* send an acknowledgement if necessary */
- if ((coap_msg_get_type(&recv_msg) == COAP_MSG_CON)
- && (resp_type == COAP_SERVER_SEPARATE))
- {
- ret = coap_server_trans_send_ack(trans, &recv_msg);
- if (ret < 0)
- {
- coap_server_trans_destroy(trans);
- coap_msg_destroy(&recv_msg);
- return ret;
- }
- }
- coap_msg_op_t *op_recv = NULL;
- int true_1 =0;
- op_recv=coap_msg_get_first_op(&recv_msg);
- if(op_recv->num != 257){
- while(true_1 == 0){
- op_recv=coap_msg_op_get_next(op_recv);
- if(op_recv->num == 257){
- true_1 = 1;
- }
- }
- }
- char *token_hash = (char *)malloc(sizeof(char)*8);
- memcpy(token_hash,coap_msg_get_token(&recv_msg),(sizeof(char))*8);
- ret=request_func(&recv_msg,&rqi_hash_key);
- LOG(3, "Request mesajında gelen RQI--> %s",rqi_hash_key);
- hash_check = g_hash_table_insert(hash, rqi_hash_key, GINT_TO_POINTER(token_hash));
- if (hash_check){
- LOG(3,"RQI--> %s ile TOKEN hash tablosuna eklendi",rqi_hash_key);
- }else{
- LOG(3,"RQI--> %s daha önceden token hash tablosuna kayıt olmus, yeni value ile update edildi",rqi_hash_key);
- }
- LOG(3, "Request mesajı işlendikten sonra, token hash tablosunda bulunan toplam key sayısı %d", g_hash_table_size(hash));
- coap_server_trans_t *trans2 = (coap_server_trans_t *)malloc(sizeof(coap_server_trans_t));
- memcpy(trans2, trans, sizeof(coap_server_trans_t));
- hash_check = g_hash_table_insert(hash_table_struct, rqi_hash_key, GUINT_TO_POINTER(trans2));
- if (hash_check){
- LOG(3,"RQI--> %s ile trans-struct hash tablosuna eklendi",rqi_hash_key);
- }else{
- LOG(3,"RQI--> %s daha önceden trans-struct hash tablosuna kayıt olmus, yeni value ile update edildi",rqi_hash_key);
- }
- INFO("Request mesajı işlendikten sonra, Trans-struct hash tablosunda bulunan toplam key sayısı %d", g_hash_table_size(hash_table_struct));
- ret = (*server->handle)(server, &recv_msg, &send_msg);
- return ret;
- }
- int coap_server_run(coap_server_t *server)
- {
- int check;
- atexit(last_wish);
- #ifdef MALLOC_TRACE
- mtrace();
- #endif
- log_level=3;
- hash_table_struct = g_hash_table_new(g_str_hash, g_str_equal);
- hash = g_hash_table_new(g_str_hash, g_str_equal);
- hash_pri_to_coap_code = g_hash_table_new(g_str_hash, g_str_equal);
- hash_pri_to_coap_detail = g_hash_table_new(g_str_hash, g_str_equal);
- hash_coap_uri_query = g_hash_table_new(g_str_hash, g_str_equal);
- hash_coap_conf = g_hash_table_new(g_str_hash, g_str_equal);
- check = hash_pri_to_coap_code_init(hash_pri_to_coap_code);
- if (check < 0){
- ERR("hash_pri_to_coap_code can not initialized ");
- }else{
- check = hash_pri_to_coap_detail_init(hash_pri_to_coap_detail);
- if (check < 0){
- ERR("hash_pri_to_coap_detail can not initialized ");
- }else{
- check = hash_coap_uri_query_init(hash_coap_uri_query);
- if (check < 0){
- ERR("hash_coap_uri_query can not initialized ");
- }else{
- check = hash_coap_conf_init(hash_coap_conf);
- if (check < 0){
- ERR("hash_coap_rsc can not initialized ");
- }else{
- if ( cfg_read(config_name, config_value) < 0 ){
- ERR("Sistem baslagıcında config dosyası okunamadı. Default değerler ile devam edilecek.");
- /*Config_values Default values*/
- config_value[0]="xmlns:m2m";
- config_value[1]="http://www.onem2m.org/xml/protocols";
- config_value[2]="127.0.0.1";
- config_value[3]="2306";
- }
- cfg_disp(config_name, config_name, stdout);
- linda= linda_connect(linda_ip, atoi(linda_port), COAP_SERVER_GROUP, "COAP_SERVER_GROUP");
- if ( linda == NULL ){
- ERR("Lindaya bağlanamadı!!");
- return -1;
- }
- linda_fd = linda_get_fd(linda);
- LOG(3,"Linda ile bağlantılı yapılan FD numarası --> %d",linda_fd);
- if ( linda_fd < 0){
- ERR("Linda FD okunamadı");
- return -1;
- }
- linda_rdy(linda);
- while (1){
- ret = coap_server_listen(server);
- if (ret < 0)
- {
- coap_log_error("server listen: %s", strerror(-ret));
- return ret;
- }
- }
- }
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement