Mercurial > hg > waaad
view extensions/sec_nosec/sns_recv_unprotect.c @ 106:e243c9a234cd
Completed the sec_nosec extension
author | Sebastien Decugis <sdecugis@nict.go.jp> |
---|---|
date | Mon, 28 Jul 2008 13:59:54 +0900 |
parents | a6fb0680654e |
children | c956cc2dbaed |
line wrap: on
line source
/********************************************************************************************************* * Software License Agreement (BSD License) * * Author: Sebastien Decugis <sdecugis@nict.go.jp> * * * * Copyright (c) 2008, WIDE Project and NICT * * All rights reserved. * * * * Redistribution and use of this software in source and binary forms, with or without modification, are * * permitted provided that the following conditions are met: * * * * * Redistributions of source code must retain the above * * copyright notice, this list of conditions and the * * following disclaimer. * * * * * Redistributions in binary form must reproduce the above * * copyright notice, this list of conditions and the * * following disclaimer in the documentation and/or other * * materials provided with the distribution. * * * * * Neither the name of the WIDE Project or NICT nor the * * names of its contributors may be used to endorse or * * promote products derived from this software without * * specific prior written permission of WIDE Project and * * NICT. * * * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED * * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * *********************************************************************************************************/ /* Code of the recv_unprotect callback function. * See <waaad/security-api.h> for more information. */ #include "sec_nosec.h" /* We just receive the buffer "as is" on the connection object, and rebuild a message (boundaries are lost with TCP) */ int sns_recv_unprotect (sec_session_t * session, void ** ext_session, void ** data, size_t *length) { unsigned char header[4]; unsigned char * newmsg; ssize_t ret = 0; size_t received = 0; TRACE_ENTRY("%p %p %p %p", session, ext_session, data, length); if (!session || !session->recv_data || !data || !length) { TRACE_DEBUG(INFO, "Invalid argument"); return EINVAL; } /* First, receive only a message header. */ while (received < sizeof(header)) { ret = (*session->recv_data) (session->conn, &header[received], sizeof(header) - received); if (ret == 0) { /* Shutdown in progress */ TRACE_DEBUG(INFO, "The recv_data function returned 0"); return ENOTCONN; } if (ret < 0) { /* Error */ ret = errno; TRACE_DEBUG(INFO, "The recv_data function failed: %s", strerror(ret)); return ret; } received += ret; } *length = (size_t)header[1] << 16 + (size_t)header[2] << 8 + (size_t)header[3]; /* Check the received word is a valid begining of a Diameter message */ if ((header[0] != MSG_VERSION) /* MSG_VERSION defined in <waaad/message-api.h> */ || (*length > DIAMETER_MSG_SIZE_MAX)) { /* to avoid too big mallocs */ /* The message is suspect */ TRACE_DEBUG(INFO, "Received suspect message header: ver = %d, size = %d", (int)header[0], *length); return EBADMSG; } /* Ok, now we can really receive the data */ newmsg = malloc( *length ); if (newmsg == NULL) { log_error("Memory allocation failed: %s\n", strerror(errno)); TRACE_DEBUG(INFO, "malloc failed"); return ENOMEM; } memcpy(newmsg, header, sizeof(header)); while (received < *length) { ret = (*session->recv_data) (session->conn, newmsg + received, (*length) - received); if (ret == 0) { /* Shutdown in progress */ TRACE_DEBUG(INFO, "The recv_data function returned 0"); free(newmsg); return ENOTCONN; } if (ret < 0) { /* Error */ ret = errno; TRACE_DEBUG(INFO, "The recv_data function failed: %s", strerror(ret)); free(newmsg); return ret; } received += ret; } /* We have received a full message, return it */ *data = (void *) newmsg; return 0; }