File: recvmsg.c

package info (click to toggle)
lksctp-tools 1.0.17%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,708 kB
  • ctags: 1,184
  • sloc: ansic: 13,499; sh: 4,162; makefile: 251
file content (101 lines) | stat: -rw-r--r-- 2,936 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* SCTP kernel Implementation: User API extensions.
 *
 * sctp_recvmsg.c
 *
 * Distributed under the terms of the LGPL v2.1 as described in
 *    http://www.gnu.org/copyleft/lesser.txt 
 *
 * This file is part of the user library that offers support for the
 * SCTP kernel Implementation. The main purpose of this
 * code is to provide the SCTP Socket API mappings for user
 * application to interface with the SCTP in kernel.
 *
 * This implementation is based on the Socket API Extensions for SCTP
 * defined in <draft-ietf-tsvwg-sctpsocket-10.txt>
 *
 * Copyright (c) 2003 International Business Machines, Corp.
 *
 * Written or modified by:
 *  Ryan Layer	<rmlayer@us.ibm.com>
 *
 * An implementation may provide a library function (or possibly system
 * call) to assist the user with the advanced features of SCTP. Note
 * that in order for the sctp_sndrcvinfo structure to be filled in by
 * sctp_recvmsg() the caller must enable the sctp_data_io_events with
 * the SCTP_EVENTS option.
 * 
 * sctp_recvmsg(). Its syntax is,
 * 
 * int sctp_recvmsg(int s,
 *		    void *msg,
 *		    size_t len,
 *		    struct sockaddr *from,
 *		    socklen_t *fromlen,
 *		    struct sctp_sndrcvinfo *sinfo,
 *		    int *msg_flags)
 * 
 * 
 * s          - is the socket descriptor
 * msg        - is a message buffer to be filled.
 * len        - is the length of the message buffer.
 * from       - is a pointer to a address to be filled with
 *		the sender of this messages address.
 * fromlen    - is the from length.
 * sinfo      - A pointer to a sctp_sndrcvinfo structure
 *		to be filled upon receipt of the message.
 * msg_flags  - A pointer to a integer to be filled with
 *		any message flags (e.g. MSG_NOTIFICATION).
 */

#include <string.h>
#include <errno.h>
#include <sys/socket.h>   /* struct sockaddr_storage, setsockopt() */
#include <netinet/sctp.h>

int sctp_recvmsg(int s, void *msg, size_t len, struct sockaddr *from,
		 socklen_t *fromlen, struct sctp_sndrcvinfo *sinfo,
		 int *msg_flags)
{
	int error;
	struct iovec iov;
	struct msghdr inmsg;
	char incmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
	struct cmsghdr *cmsg = NULL;

	memset(&inmsg, 0, sizeof (inmsg));

	iov.iov_base = msg;
	iov.iov_len = len;

	inmsg.msg_name = from;
	inmsg.msg_namelen = fromlen ? *fromlen : 0;
	inmsg.msg_iov = &iov;
	inmsg.msg_iovlen = 1;
	inmsg.msg_control = incmsg;
	inmsg.msg_controllen = sizeof(incmsg);

	error = recvmsg(s, &inmsg, msg_flags ? *msg_flags : 0);
	if (error < 0)
		return error;

	if (fromlen)
		*fromlen = inmsg.msg_namelen;
	if (msg_flags)
		*msg_flags = inmsg.msg_flags;

	if (!sinfo)
		return error;

	for (cmsg = CMSG_FIRSTHDR(&inmsg); cmsg != NULL;
				 cmsg = CMSG_NXTHDR(&inmsg, cmsg)){
		if ((IPPROTO_SCTP == cmsg->cmsg_level) &&
		    (SCTP_SNDRCV == cmsg->cmsg_type))
			break;
	}

        /* Copy sinfo. */
	if (cmsg)
		memcpy(sinfo, CMSG_DATA(cmsg), sizeof(struct sctp_sndrcvinfo));

	return (error);
}