4 * Copyright (C) 2001-2003 FhG Fokus
6 * This file is part of ser, a free SIP server.
8 * ser is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version
13 * For a license to use the ser software under conditions
14 * other than those described here, or to purchase support for this
15 * software, please contact iptel.org by e-mail at the following addresses:
18 * ser is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 * 2006-10-13 created (vlada)
30 * 2006-12-14 fixed calculation of body length (vlada)
35 * \brief SIP-router core ::
42 #include <arpa/inet.h>
43 #include <openssl/sha.h>
48 * ****************************************************************************
49 * Declaration of functions *
50 * ****************************************************************************
52 int stun_parse_header(struct stun_msg* req, USHORT_T* error_code);
55 struct stun_unknown_att** unknown,
56 USHORT_T* error_code);
57 void stun_delete_unknown_attrs(struct stun_unknown_att* unknown);
58 struct stun_unknown_att* stun_alloc_unknown_attr(USHORT_T type);
59 int stun_add_address_attr(struct stun_msg* res,
65 int add_unknown_attr(struct stun_msg* res, struct stun_unknown_att* unknown);
66 int add_error_code(struct stun_msg* res, USHORT_T error_code);
67 int copy_str_to_buffer(struct stun_msg* res, const char* data, UINT_T pad);
68 int reallock_buffer(struct stun_buffer* buffer, UINT_T len);
69 int buf_copy(struct stun_buffer* msg, void* source, UINT_T len);
70 void clean_memory(struct stun_msg* req,
71 struct stun_msg* res, struct stun_unknown_att* unknown);
72 int stun_create_response(
75 struct receive_info* ri,
76 struct stun_unknown_att* unknown,
78 int stun_add_common_integer_attr(struct stun_msg* res, USHORT_T type, UINT_T value);
79 int stun_add_common_text_attr(struct stun_msg* res, USHORT_T type, char* value,
84 * ****************************************************************************
85 * Definition of functions *
86 * ****************************************************************************
91 * buf - incoming message
92 * len - length of incoming message
93 * ri - information about socket that received a message and
94 * also information about sender (its IP, port, protocol)
96 * This function ensures processing of incoming message. It's common for both
97 * TCP and UDP protocol. There is no other function as an interface.
99 * Return value: 0 if there is no environment error
100 * -1 if there is some enviroment error such as insufficiency
104 int stun_process_msg(char* buf, unsigned len, struct receive_info* ri)
106 struct stun_msg msg_req;
107 struct stun_msg msg_res;
108 struct dest_info dst;
109 struct stun_unknown_att* unknown;
112 memset(&msg_req, 0, sizeof(msg_req));
113 memset(&msg_res, 0, sizeof(msg_res));
115 msg_req.msg.buf.s = buf;
116 msg_req.msg.buf.len = len;
118 error_code = RESPONSE_OK;
120 if (stun_parse_header(&msg_req, &error_code) != 0) {
124 if (error_code == RESPONSE_OK) {
125 if (stun_parse_body(&msg_req, &unknown, &error_code) != 0) {
130 if (stun_create_response(&msg_req, &msg_res, ri,
131 unknown, error_code) != 0) {
135 init_dst_from_rcv(&dst, ri);
139 su2ip_addr(&ip, &dst.to);
140 LOG(L_DBG, "DEBUG: stun_process_msg: decoded request from (%s:%d)\n", ip_addr2a(&ip),
141 su_getport(&dst.to));
144 /* send STUN response */
145 if (msg_send(&dst, msg_res.msg.buf.s, msg_res.msg.buf.len) != 0) {
150 LOG(L_DBG, "DEBUG: stun_process_msg: send response\n");
152 clean_memory(&msg_req, &msg_res, unknown);
157 LOG(L_DBG, "DEBUG: stun_process_msg: failed to decode request\n");
159 clean_memory(&msg_req, &msg_res, unknown);
164 * stun_parse_header():
165 * - req: request from host that should be processed
166 * - error_code: indication of any protocol error
168 * This function ensures parsing of incoming header.
170 * Return value: 0 if there is no environment error
171 * -1 if there is some enviroment error such as insufficiency
175 int stun_parse_header(struct stun_msg* req, USHORT_T* error_code)
178 if (sizeof(req->hdr) > req->msg.buf.len) {
179 /* the received message does not contain whole header */
180 LOG(L_INFO, "INFO: stun_parse_header: incomplete header of STUN message\n");
181 /* Any better solution? IMHO it's not possible to send error response
182 * because the transaction ID is not available.
187 memcpy(&req->hdr, req->msg.buf.s, sizeof(struct stun_hdr));
188 req->hdr.type = ntohs(req->hdr.type);
190 /* the SER supports only Binding Request right now */
191 if (req->hdr.type != BINDING_REQUEST) {
192 LOG(L_INFO, "INFO: stun_parse_header: unsupported type of STUN message: %x\n",
194 /* resending of same message is not welcome */
195 *error_code = GLOBAL_FAILURE_ERR;
198 req->hdr.len = ntohs(req->hdr.len);
200 /* check if there is correct magic cookie */
201 req->old = (req->hdr.id.magic_cookie == htonl(MAGIC_COOKIE)) ? 0 : 1;
204 LOG(L_DBG, "DEBUG: stun_parse_header: request is old: %i\n", req->old);
211 * - req: request from host that should be processed
212 * - unknown: this is a link list header of attributes
213 * that are unknown to SER; defaul value is NULL
214 * - error_code: indication of any protocol error
216 * Return value: 0 if there is no environment error
217 * -1 if there is some enviroment error such as insufficiency
221 struct stun_msg* req,
222 struct stun_unknown_att** unknown,
223 USHORT_T* error_code)
226 struct stun_attr attr;
229 struct stun_unknown_att* tmp_unknown;
230 struct stun_unknown_att* body;
233 attr_size = sizeof(struct stun_attr);
234 buf = &req->msg.buf.s[sizeof(struct stun_hdr)];
237 * Mark the body lenght as unparsed.
239 not_parsed = req->msg.buf.len - sizeof(struct stun_hdr);
241 if (not_parsed != req->hdr.len) {
243 LOG(L_DBG, "DEBUG: stun_parse_body: body too short to be valid\n");
245 *error_code = BAD_REQUEST_ERR;
249 tmp_unknown = *unknown;
252 while (not_parsed > 0 && *error_code == RESPONSE_OK) {
253 memset(&attr, 0, attr_size);
255 /* check if there are 4 bytes for attribute type and its value */
256 if (not_parsed < 4) {
258 LOG(L_DBG, "DEBUG: stun_parse_body: attribute header short to be valid\n");
260 *error_code = BAD_REQUEST_ERR;
264 memcpy(&attr, buf, attr_size);
267 not_parsed -= attr_size;
269 /* check if there is enought unparsed space for attribute's value */
270 if (not_parsed < ntohs(attr.len)) {
272 LOG(L_DBG, "DEBUG: stun_parse_body: remaining message is shorter then attribute length\n");
274 *error_code = BAD_REQUEST_ERR;
278 /* check if the attribute is known to the server */
279 switch (ntohs(attr.type)) {
282 case MAPPED_ADDRESS_ATTR:
283 case XOR_MAPPED_ADDRESS_ATTR:
284 case ALTERNATE_SERVER_ATTR:
285 case RESPONSE_ADDRESS_ATTR:
286 case SOURCE_ADDRESS_ATTR:
287 case REFLECTED_FROM_ATTR:
288 case CHANGE_REQUEST_ATTR:
289 case CHANGED_ADDRESS_ATTR:
290 padded_len = ntohs(attr.len);
292 LOG(L_DBG, "DEBUG: stun_parse_body: known attributes\n");
296 /* following attributes must be padded to 4 bytes */
298 case ERROR_CODE_ATTR:
299 case UNKNOWN_ATTRIBUTES_ATTR:
301 padded_len = PADDED_TO_FOUR(ntohs(attr.len));
303 LOG(L_DBG, "DEBUG: stun_parse_body: padded to four\n");
307 /* MESSAGE_INTEGRITY must be padded to sixty four bytes*/
308 case MESSAGE_INTEGRITY_ATTR:
310 LOG(L_DBG, "DEBUG: stun_parse_body: message integrity attribute found\n");
312 padded_len = PADDED_TO_SIXTYFOUR(ntohs(attr.len));
315 case FINGERPRINT_ATTR:
317 LOG(L_DBG, "DEBUG: stun_parse_body: fingerprint attribute found\n");
319 padded_len = SHA_DIGEST_LENGTH;
321 if (not_parsed > SHA_DIGEST_LENGTH) {
323 LOG(L_DBG, "DEBUG: stun_parse_body: fingerprint is not the last attribute\n");
325 /* fingerprint must be last parameter in request */
326 *error_code = BAD_REQUEST_ERR;
333 * the attribute is uknnown to the server
334 * let see if it's necessary to generate error response
337 LOG(L_DBG, "DEBUG: low endian: attr - 0x%x const - 0x%x\n", ntohs(attr.type), MANDATORY_ATTR);
338 LOG(L_DBG, "DEBUG: big endian: attr - 0x%x const - 0x%x\n", attr.type, htons(MANDATORY_ATTR));
340 if (ntohs(attr.type) <= MANDATORY_ATTR) {
342 LOG(L_DBG, "DEBUG: stun_parse_body: mandatory unknown attribute found - 0x%x\n", ntohs(attr.type));
344 tmp_unknown = stun_alloc_unknown_attr(attr.type);
345 if (tmp_unknown == NULL) {
348 if (*unknown == NULL) {
349 *unknown = body = tmp_unknown;
352 body->next = tmp_unknown;
358 LOG(L_DBG, "DEBUG: stun_parse_body: optional unknown attribute found - 0x%x\n", ntohs(attr.type));
361 padded_len = ntohs(attr.len);
365 /* check if there is enough unparsed space for the padded attribute
366 (the padded length might be greater then the attribute length)
368 if (not_parsed < padded_len) {
372 not_parsed -= padded_len;
376 * The unknown attribute error code must set after parsing of whole body
377 * because it's necessary to obtain all of unknown attributes!
379 if (*error_code == RESPONSE_OK && *unknown != NULL) {
380 *error_code = UNKNOWN_ATTRIBUTE_ERR;
387 * stun_create_response():
388 * - req: original request from host
389 * - res: this will represent response to host
390 * - ri: information about request, necessary because of IP
392 * - unknown: link list of unknown attributes
393 * - error_code: indication of any protocol error
395 * The function stun_create_response ensures creating response to a host.
396 * The type of response depends on value of error_code parameter.
398 * Return value: 0 if there is no environment error
399 * -1 if there is some enviroment error such as insufficiency
403 int stun_create_response(
404 struct stun_msg* req,
405 struct stun_msg* res,
406 struct receive_info* ri,
407 struct stun_unknown_att* unknown,
411 * Alloc some space for response.
412 * Optimalization? - maybe it would be better to use biggish static array.
414 res->msg.buf.s = (char *) pkg_malloc(sizeof(char)*STUN_MSG_LEN);
415 if (res->msg.buf.s == NULL) {
416 LOG(L_ERR, "ERROR: STUN: out of memory\n");
420 memset(res->msg.buf.s, 0, sizeof(char)*STUN_MSG_LEN);
421 res->msg.buf.len = 0;
422 res->msg.empty = STUN_MSG_LEN;
424 /* use magic cookie and transaction ID from request */
425 memcpy(&res->hdr.id, &req->hdr.id, sizeof(struct transaction_id));
426 /* the correct body length will be added ASAP it will be known */
427 res->hdr.len = htons(0);
429 if (error_code == RESPONSE_OK) {
431 LOG(L_DBG, "DEBUG: stun_create_response: creating normal response\n");
433 res->hdr.type = htons(BINDING_RESPONSE);
435 /* copy header into msg buffer */
436 if (buf_copy(&res->msg, (void *) &res->hdr,
437 sizeof(struct stun_hdr)) != 0) {
439 LOG(L_DBG, "DEBUG: stun_create_response: failed to copy buffer\n");
445 * If the SER received message in old format, then the body will
446 * contain MAPPED-ADDRESS attribute instead of XOR-MAPPED-ADDRESS
450 if (stun_add_address_attr(res, ri->src_ip.af, ri->src_port,
451 ri->src_ip.u.addr32, XOR_MAPPED_ADDRESS_ATTR,
454 LOG(L_DBG, "DEBUG: stun_create_response: failed to add address\n");
460 if (stun_add_address_attr(res, ri->src_ip.af, ri->src_port,
461 ri->src_ip.u.addr32, MAPPED_ADDRESS_ATTR, !XOR) != 0) {
463 LOG(L_DBG, "DEBUG: stun_create_response: failed to add address\n");
471 LOG(L_DBG, "DEBUG: stun_create_response: creating error response\n");
473 res->hdr.type = htons(BINDING_ERROR_RESPONSE);
475 if (buf_copy(&res->msg, (void *) &res->hdr,
476 sizeof(struct stun_hdr)) != 0) {
478 LOG(L_DBG, "DEBUG: stun_create_response: failed to copy buffer\n");
483 if (add_error_code(res, error_code) != 0) {
485 LOG(L_DBG, "DEBUG: stun_create_response: failed to add error code\n");
490 if (unknown != NULL) {
491 if (add_unknown_attr(res, unknown) != 0) {
493 LOG(L_DBG, "DEBUG: stun_create_response: failed to add unknown attribute\n");
502 * add optional information about server; attribute SOFTWARE is part of
505 if (stun_add_common_text_attr(res, SOFTWARE_ATTR, SERVER_HDR, PAD4)!=0) {
507 LOG(L_DBG, "DEBUG: stun_create_response: failed to add common text attribute\n");
513 res->hdr.len = htons(res->msg.buf.len - sizeof(struct stun_hdr));
514 memcpy(&res->msg.buf.s[sizeof(USHORT_T)], (void *) &res->hdr.len,
522 * - res: response representation
523 * - unknown: link list of unknown attributes
525 * The function add_unknown_attr ensures copy of link list of unknown
526 * attributes into response buffer.
528 * Return value: 0 if there is no environment error
529 * -1 if there is some enviroment error such as insufficiency
533 int add_unknown_attr(struct stun_msg* res, struct stun_unknown_att* unknown)
535 struct stun_attr attr;
536 struct stun_unknown_att* tmp_unknown;
541 orig_len = res->msg.buf.len;
542 tmp_unknown = unknown;
544 attr.type = htons(UNKNOWN_ATTRIBUTES_ATTR);
547 if (buf_copy(&res->msg, (void *) &attr, sizeof(struct stun_attr)) != 0) {
549 LOG(L_DBG, "DEBUG: add_unknown_attr: failed to copy buffer\n");
554 while (tmp_unknown != NULL) {
555 if (buf_copy(&res->msg, (void *)&tmp_unknown->type,
556 sizeof(USHORT_T)) != 0) {
558 LOG(L_DBG, "DEBUG: add_unknown_attr: failed to copy unknown attribute\n");
562 tmp_unknown = tmp_unknown->next;
566 attr.len = htons(res->msg.buf.len - orig_len);
567 memcpy(&res->msg.buf.s[orig_len], (void *)&attr, sizeof(struct stun_attr));
569 /* check if there is an odd number of unknown attributes and if yes,
570 * repeat one of them because of padding to 32
572 if (counter/2 != 0 && unknown != NULL) {
573 if (buf_copy(&res->msg, (void *)&unknown->type, sizeof(USHORT_T))!=0) {
575 LOG(L_DBG, "DEBUG: add_unknown_attr: failed to padd\n");
586 * - res: response representation
587 * - error_code: value of error type
589 * The function add_error_code ensures copy of link list of unknown
590 * attributes into response buffer.
592 * Return value: 0 if there is no environment error
593 * -1 if there is some enviroment error such as insufficiency
596 int add_error_code(struct stun_msg* res, USHORT_T error_code)
598 struct stun_attr attr;
604 orig_len = res->msg.buf.len;
607 /* the type and length will be copy as last one because of unknown length*/
608 if (res->msg.buf.len < sizeof(struct stun_attr)) {
609 if (reallock_buffer(&res->msg, sizeof(struct stun_attr)) != 0) {
611 LOG(L_DBG, "DEBUG: add_error_code: failed to reallocate buffer\n");
616 res->msg.buf.len += sizeof(struct stun_attr);
617 res->msg.empty -= sizeof(struct stun_attr);
619 /* first two bytes are empty */
622 if (buf_copy(&res->msg, (void *) &two_bytes, sizeof(USHORT_T)) != 0) {
624 LOG(L_DBG, "DEBUG: add_error_code: failed to copy buffer\n");
629 err[0] = error_code / 100;
630 err[1] = error_code % 100;
631 if (buf_copy(&res->msg, (void *) err, sizeof(UCHAR_T)*2) != 0) {
635 switch (error_code) {
636 case TRY_ALTERNATE_ERR:
637 text_pad = copy_str_to_buffer(res, TRY_ALTERNATE_TXT, PAD4);
639 case BAD_REQUEST_ERR:
640 text_pad = copy_str_to_buffer(res, BAD_REQUEST_TXT, PAD4);
642 case UNAUTHORIZED_ERR:
643 text_pad = copy_str_to_buffer(res, UNAUTHORIZED_TXT, PAD4);
645 case UNKNOWN_ATTRIBUTE_ERR:
646 text_pad = copy_str_to_buffer(res, UNKNOWN_ATTRIBUTE_TXT, PAD4);
648 case STALE_CREDENTIALS_ERR:
649 text_pad = copy_str_to_buffer(res, STALE_CREDENTIALS_TXT, PAD4);
651 case INTEGRITY_CHECK_ERR:
652 text_pad = copy_str_to_buffer(res, INTEGRITY_CHECK_TXT, PAD4);
654 case MISSING_USERNAME_ERR:
655 text_pad = copy_str_to_buffer(res, MISSING_USERNAME_TXT, PAD4);
658 text_pad = copy_str_to_buffer(res, USE_TLS_TXT, PAD4);
660 case MISSING_REALM_ERR:
661 text_pad = copy_str_to_buffer(res, MISSING_REALM_TXT, PAD4);
663 case MISSING_NONCE_ERR:
664 text_pad = copy_str_to_buffer(res, MISSING_NONCE_TXT, PAD4);
666 case UNKNOWN_USERNAME_ERR:
667 text_pad = copy_str_to_buffer(res, UNKNOWN_USERNAME_TXT, PAD4);
669 case STALE_NONCE_ERR:
670 text_pad = copy_str_to_buffer(res, STALE_NONCE_TXT, PAD4);
672 case SERVER_ERROR_ERR:
673 text_pad = copy_str_to_buffer(res, SERVER_ERROR_TXT, PAD4);
675 case GLOBAL_FAILURE_ERR:
676 text_pad = copy_str_to_buffer(res, GLOBAL_FAILURE_TXT, PAD4);
679 LOG(L_ERR, "ERROR: STUN: Unknown error code.\n");
684 LOG(L_DBG, "DEBUG: add_error_code: text_pad is negative\n");
688 attr.type = htons(ERROR_CODE_ATTR);
689 /* count length of "value" field -> without type and lehgth field */
690 attr.len = htons(res->msg.buf.len - orig_len -
691 text_pad - sizeof(struct stun_attr));
692 memcpy(&res->msg.buf.s[orig_len], (void *)&attr, sizeof(struct stun_attr));
701 * copy_str_to_buffer()
702 * - res: response representation
703 * - data: text data, in our case almost text representation of error
704 * - pad: the size of pad (for how much bytes the string should be
707 * The function copy_str_to_buffer ensures copy of text buffer into response
710 * Return value: 0 if there is no environment error
711 * -1 if there is some enviroment error such as insufficiency
714 int copy_str_to_buffer(struct stun_msg* res, const char* data, UINT_T pad)
720 data_len = strlen(data);
721 memset(&empty, 0, pad);
723 pad_len = (pad - data_len%pad) % pad;
725 if (buf_copy(&res->msg, (void *) data, sizeof(UCHAR_T)*data_len) != 0) {
727 LOG(L_DBG, "DEBUG: copy_str_to_buffer: failed to copy buffer\n");
733 if (buf_copy(&res->msg, &empty, pad_len) != 0) {
735 LOG(L_DBG, "DEBUG: copy_str_to_buffer: failed to pad\n");
745 * stun_add_address_attr()
746 * - res: response representation
747 * - af: address family
749 * - ip_addr: represent both IPv4 and IPv6, the differences is in
751 * - type: type of attribute
752 * - do_xor: if the port should be XOR-ed or not.
754 * The function stun_add_address_attr ensures copy of any IP attribute into
757 * Return value: 0 if there is no environment error
758 * -1 if there is some enviroment error such as insufficiency
761 int stun_add_address_attr(struct stun_msg* res,
768 struct stun_attr attr;
774 attr.type = htons(type);
775 res->ip_addr.port = htons((do_xor) ? (port ^ MAGIC_COOKIE_2B) : port);
778 ip_struct_len = sizeof(struct stun_ip_addr) - 3*sizeof(UINT_T);
779 res->ip_addr.family = htons(IPV4_FAMILY);
780 memcpy(res->ip_addr.ip, ip_addr, IPV4_LEN);
781 res->ip_addr.ip[0] = (do_xor) ?
782 res->ip_addr.ip[0] ^ htonl(MAGIC_COOKIE) : res->ip_addr.ip[0];
786 ip_struct_len = sizeof(struct stun_ip_addr);
787 res->ip_addr.family = htons(IPV6_FAMILY);
788 memcpy(&res->ip_addr.ip, ip_addr, IPV6_LEN);
789 memcpy(id, &res->hdr.id, sizeof(struct transaction_id));
790 for (i=0; i<IP_ADDR; i++) {
791 res->ip_addr.ip[i] = (do_xor) ?
792 res->ip_addr.ip[i] ^ id[i] : res->ip_addr.ip[i];
795 #endif /* USE_IPV6 */
800 attr.len = htons(ip_struct_len);
802 /* copy type and attribute's length */
803 if (buf_copy(&res->msg, (void *) &attr, sizeof(struct stun_attr)) != 0) {
807 /* copy family, port and IP */
808 if (buf_copy(&res->msg, (void *) &res->ip_addr, ip_struct_len) != 0) {
816 * stun_alloc_unknown_attr()
817 * - type: type of unknown attribute
819 * The function stun_alloc_unknown_attr ensures allocationg new element for
820 * the link list of unknown attributes.
822 * Return value: pointer to new element of link list in positive case
823 * NULL if there is some enviroment error such as insufficiency
826 struct stun_unknown_att* stun_alloc_unknown_attr(USHORT_T type)
828 struct stun_unknown_att* attr;
830 attr = (struct stun_unknown_att *) pkg_malloc(sizeof(struct stun_unknown_att));
832 LOG(L_ERR, "ERROR: STUN: out of memory\n");
843 * stun_delete_unknown_attrs()
844 * - unknown: link list of unknown attributes
846 * The function stun_delete_unknown_attrs ensures deleting of link list
850 void stun_delete_unknown_attrs(struct stun_unknown_att* unknown)
852 struct stun_unknown_att* tmp_unknown;
854 if (unknown == NULL) {
858 while(unknown->next) {
859 tmp_unknown = unknown->next;
860 unknown->next = tmp_unknown->next;
861 pkg_free(tmp_unknown);
868 * - msg: buffer where the data will be copy to
869 * - source: source data buffer
870 * - len: number of bytes that should be copied
872 * The function buf_copy copies "len" bytes from source into msg buffer
874 * Return value: 0 if there is no environment error
875 * -1 if there is some enviroment error such as insufficiency
878 int buf_copy(struct stun_buffer* msg, void* source, UINT_T len)
880 if (msg->empty < len) {
881 if (reallock_buffer(msg, len) != 0) {
886 memcpy(&msg->buf.s[msg->buf.len], source, len);
895 * - buffer: original buffer
896 * - len: represents minimum of bytes that must be available after
899 * The function reallock_buffer reallocks buffer. New buffer's length will be
900 * original length plus bigger from len and STUN_MSG_LEN constant.
902 * Return value: 0 if there is no environment error
903 * -1 if there is some enviroment error such as insufficiency
906 int reallock_buffer(struct stun_buffer* buffer, UINT_T len)
911 new_len = (STUN_MSG_LEN < len) ? STUN_MSG_LEN+len : STUN_MSG_LEN;
913 tmp_buf = (char *) pkg_realloc(buffer->buf.s,
914 buffer->buf.len + buffer->empty + new_len);
916 LOG(L_ERR, "ERROR: STUN: out of memory\n");
920 buffer->buf.s = tmp_buf;
921 buffer->empty += new_len;
928 * - res: structure representing response message
929 * - unknown: link list of unknown attributes
931 * The function clean_memory should free dynamic allocated memory.
935 void clean_memory(struct stun_msg* req,
936 struct stun_msg* res, struct stun_unknown_att* unknown)
939 pkg_free(req->msg.buf.s);
942 if (res->msg.buf.s != NULL) {
943 pkg_free(res->msg.buf.s);
945 stun_delete_unknown_attrs(unknown);
949 * stun_add_common_integer_attr()
950 * - res: structure representing response
951 * - type: type of attribute
952 * - value: attribute's value
954 * The function stun_add_common_integer_attr copy attribute with integer value
955 * into response buffer.
957 * Return value: 0 if there is no environment error
958 * -1 if there is some enviroment error such as insufficiency
961 int stun_add_common_integer_attr(struct stun_msg* res,
965 struct stun_attr attr;
967 attr.type = htons(type);
968 attr.len = htons(sizeof(UINT_T));
970 if (buf_copy(&res->msg, (void *) &attr, sizeof(struct stun_attr)) != 0) {
974 value = htonl(value);
975 if (buf_copy(&res->msg, (void *) &value, sizeof(UINT_T)) != 0) {
983 * stun_add_common_text_attr()
984 * - res: structure representing response
985 * - type: type of attribute
986 * - value: attribute's value
989 * The function stun_add_common_text_attr copy attribute with string value
990 * into response buffer.
992 * Return value: 0 if there is no environment error
993 * -1 if there is some enviroment error such as insufficiency
996 int stun_add_common_text_attr(struct stun_msg* res,
1001 struct stun_attr attr;
1003 if (value == NULL) {
1004 LOG(L_INFO, "INFO: stun_add_common_text_attr: value is NULL\n");
1008 attr.type = htons(type);
1009 attr.len = htons(strlen(value));
1011 if (buf_copy(&res->msg, (void *) &attr, sizeof(struct stun_attr)) != 0) {
1015 if (copy_str_to_buffer(res, value, pad) < 0) {
1023 #endif /* USE_STUN */