225 lines
6.4 KiB
C
225 lines
6.4 KiB
C
/*
|
|
******************************************************************************
|
|
* SOURCE FILE
|
|
*
|
|
* Document no: @(#) $Name: VXWORKS_ITER40_2019102207 $ $RCSfile: iplite_sockmsg.c,v $ $Revision: 1.9 $
|
|
* $Source: /home/interpeak/CVSRoot/iplite2/src/iplite_sockmsg.c,v $
|
|
* $Author: hxie $
|
|
* $State: Exp $ $Locker: $
|
|
*
|
|
* INTERPEAK_COPYRIGHT_STRING
|
|
* Design and implementation by Kenneth Jonsson <kenneth@interpeak.se>
|
|
******************************************************************************
|
|
*/
|
|
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 1 DESCRIPTION
|
|
****************************************************************************
|
|
* Functions for socket packet API.
|
|
*/
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 2 CONFIGURATION
|
|
****************************************************************************
|
|
*/
|
|
|
|
#define IPCOM_SKIP_NATIVE_SOCK_API
|
|
|
|
#ifndef IPCOM_KERNEL
|
|
#define IPCOM_KERNEL
|
|
#endif
|
|
#include "iplite_config.h"
|
|
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 3 INCLUDE FILES
|
|
****************************************************************************
|
|
*/
|
|
|
|
#define IPCOM_USE_CLIB_PROTO
|
|
#include <ipcom_clib.h>
|
|
#include <ipcom_sock.h>
|
|
#include "iplite_sock.h"
|
|
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 4 DEFINES
|
|
****************************************************************************
|
|
*/
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 5 TYPES
|
|
****************************************************************************
|
|
*/
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 6 EXTERN PROTOTYPES
|
|
****************************************************************************
|
|
*/
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 7 LOCAL PROTOTYPES
|
|
****************************************************************************
|
|
*/
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 8 DATA
|
|
****************************************************************************
|
|
*/
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 9 STATIC FUNCTIONS
|
|
****************************************************************************
|
|
*/
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 10 GLOBAL FUNCTIONS
|
|
****************************************************************************
|
|
*/
|
|
|
|
/*
|
|
****************************************************************************
|
|
* 11 PUBLIC FUNCTIONS
|
|
****************************************************************************
|
|
*/
|
|
|
|
/*
|
|
*===========================================================================
|
|
* ipcom_sendmsg
|
|
*===========================================================================
|
|
* Description: Simplistic wrapper sendmsg function (can't send control data).
|
|
* Parameters:
|
|
* Returns:
|
|
*
|
|
*/
|
|
IP_PUBLIC Ip_ssize_t
|
|
ipcom_sendmsg(Ip_fd fd, IP_CONST struct Ip_msghdr *msg, int flags)
|
|
{
|
|
Ip_u8 *buf;
|
|
Ip_size_t i;
|
|
Ip_size_t length;
|
|
|
|
/* Count the total length. */
|
|
for (length = 0, i = 0; i < msg->msg_iovlen; i++)
|
|
length += msg->msg_iov[i].iov_len;
|
|
|
|
buf = ipcom_malloc(length);
|
|
if (buf == IP_NULL)
|
|
return (Ip_ssize_t)ipcom_errno_set(IP_ERRNO_ENOMEM);
|
|
|
|
for (length = 0, i = 0; i < msg->msg_iovlen; i++)
|
|
{
|
|
ipcom_memcpy(buf + length, msg->msg_iov[i].iov_base, msg->msg_iov[i].iov_len);
|
|
length += msg->msg_iov[i].iov_len;
|
|
}
|
|
|
|
i = ipcom_sendto_pktinfo(fd, buf, length, flags, msg->msg_name, msg->msg_namelen, msg);
|
|
ipcom_free(buf);
|
|
|
|
return (Ip_ssize_t)i;
|
|
}
|
|
|
|
|
|
/*
|
|
*===========================================================================
|
|
* ipcom_recvmsg
|
|
*===========================================================================
|
|
* Description: Receives data from a socket.
|
|
* Parameters: fd - A socket descriptor.
|
|
* msg - Receive parameters.
|
|
* flags - Currently unused.
|
|
* Returns: 0 = a packet was successfully received, -1 = error.
|
|
*
|
|
*/
|
|
IP_PUBLIC Ip_ssize_t
|
|
ipcom_recvmsg(Ip_fd fd, struct Ip_msghdr *msg, int flags)
|
|
{
|
|
Ip_u8 *buf;
|
|
Ip_size_t i;
|
|
Ip_size_t bytes, length, copied;
|
|
|
|
/* No support for ancillary data */
|
|
#ifndef IP_PORT_VXWORKS
|
|
msg->msg_controllen = 0;
|
|
#endif
|
|
|
|
/* Count the maximum total length. */
|
|
for (length = 0, i = 0; i < msg->msg_iovlen; i++)
|
|
length += msg->msg_iov[i].iov_len;
|
|
|
|
buf = ipcom_malloc(length);
|
|
if (buf == IP_NULL)
|
|
{
|
|
#ifdef IP_PORT_VXWORKS
|
|
msg->msg_controllen = 0;
|
|
#endif
|
|
return (Ip_ssize_t)ipcom_errno_set(IP_ERRNO_ENOMEM);
|
|
}
|
|
|
|
length = ipcom_recvfrom_pktinfo(fd,
|
|
buf,
|
|
length,
|
|
flags,
|
|
msg->msg_name,
|
|
(Ip_socklen_t *) &msg->msg_namelen,
|
|
msg);
|
|
if(length > 0)
|
|
{
|
|
for (copied = 0, i = 0; copied < length && i < msg->msg_iovlen; i++)
|
|
{
|
|
bytes = IP_MIN(length - copied, msg->msg_iov[i].iov_len);
|
|
ipcom_memcpy(msg->msg_iov[i].iov_base, buf + copied, bytes);
|
|
copied += bytes;
|
|
}
|
|
}
|
|
#ifdef IP_PORT_VXWORKS
|
|
else
|
|
{
|
|
msg->msg_controllen = 0;
|
|
}
|
|
#endif
|
|
ipcom_free(buf);
|
|
|
|
return (Ip_ssize_t)length;
|
|
}
|
|
|
|
/*
|
|
*===========================================================================
|
|
* ipcom_socketwritev
|
|
*===========================================================================
|
|
* Description:
|
|
* Parameters:
|
|
* Returns:
|
|
*
|
|
*/
|
|
IP_PUBLIC Ip_ssize_t
|
|
ipcom_socketwritev(Ip_fd fd, IP_CONST struct Ip_iovec *iov, int iovlen)
|
|
{
|
|
struct Ip_msghdr msghdr;
|
|
|
|
ipcom_memset(&msghdr, 0, sizeof(msghdr));
|
|
msghdr.msg_iov = (struct Ip_iovec *) iov;
|
|
msghdr.msg_iovlen = iovlen;
|
|
|
|
return ipcom_sendmsg(fd, &msghdr, 0);
|
|
}
|
|
|
|
|
|
/*
|
|
****************************************************************************
|
|
* END OF FILE
|
|
****************************************************************************
|
|
*/
|
|
|