FreeNOS
sendmsg.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2021 Niek Linnenbank
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <NetworkClient.h>
19#include <stdlib.h>
20#include <unistd.h>
21#include <sys/socket.h>
22#include <errno.h>
23
24extern C int sendmsg(int sockfd, const struct msghdr *msg, int flags)
25{
26 static u8 buf[1024];
29 const struct sockaddr *addr = (const struct sockaddr *) msg->msg_name;
30 Size bytes = sizeof(*info);
31
32 if (msg->msg_namelen != sizeof(struct sockaddr))
33 {
34 return ERANGE;
35 }
36
37 info->address = addr->addr;
38 info->port = addr->port;
40
41 // Prepare the array of NetworkClient::PacketInfo structs
42 for (Size i = 0; i < msg->msg_iovlen && bytes < sizeof(buf); i++)
43 {
44 pkt->address = (Address) msg->msg_iov[i].iov_base;
45 pkt->size = msg->msg_iov[i].iov_len;
46
47 bytes += sizeof(*pkt);
48 pkt++;
49 }
50
51 return ::write(sockfd, buf, bytes);
52}
u32 flags
Definition IntelACPI.h:3
#define ERANGE
Result too large.
Definition errno.h:259
C int sendmsg(int sockfd, const struct msghdr *msg, int flags)
Send multiple datagrams to a remote host.
Definition sendmsg.cpp:24
unsigned long Address
A memory address.
Definition Types.h:131
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128
#define C
Used to define external C functions.
Definition Macros.h:134
unsigned char u8
Unsigned 8-bit number.
Definition Types.h:59
Describes a single packet.
Socket information.
void * iov_base
Definition socket.h:46
size_t iov_len
Definition socket.h:47
Describes one or more datagrams.
Definition socket.h:56
struct iovec * msg_iov
Definition socket.h:59
socklen_t msg_namelen
Definition socket.h:58
void * msg_name
Definition socket.h:57
size_t msg_iovlen
Definition socket.h:60
Defines a socket address and port pair.
Definition socket.h:36
u32 addr
Definition socket.h:37