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 
24 extern 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 }
NetworkClient::PacketInfo
Describes a single packet.
Definition: NetworkClient.h:79
write
ssize_t write(int fildes, const void *buf, size_t nbyte)
Write on a file.
Definition: write.cpp:22
flags
u32 flags
Definition: IntelACPI.h:66
NetworkClient::SocketInfo::address
IPV4::Address address
Definition: NetworkClient.h:67
msghdr::msg_iov
struct iovec * msg_iov
Definition: socket.h:59
Address
unsigned long Address
A memory address.
Definition: Types.h:131
NetworkClient::PacketInfo::address
Address address
Definition: NetworkClient.h:81
NetworkClient::SocketInfo::port
u16 port
Definition: NetworkClient.h:68
sockaddr
Defines a socket address and port pair.
Definition: socket.h:35
msghdr::msg_iovlen
size_t msg_iovlen
Definition: socket.h:60
C
#define C
Used to define external C functions.
Definition: Macros.h:134
msghdr
Describes one or more datagrams.
Definition: socket.h:55
iovec::iov_base
void * iov_base
Definition: socket.h:46
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
msghdr::msg_name
void * msg_name
Definition: socket.h:57
iovec::iov_len
size_t iov_len
Definition: socket.h:47
sockaddr::addr
u32 addr
Definition: socket.h:37
unistd.h
NetworkClient::SocketInfo
Socket information.
Definition: NetworkClient.h:65
u8
unsigned char u8
Unsigned 8-bit number.
Definition: Types.h:59
socket.h
ERANGE
#define ERANGE
Result too large.
Definition: errno.h:259
stdlib.h
NetworkClient.h
msghdr::msg_namelen
socklen_t msg_namelen
Definition: socket.h:58
NetworkClient::SocketInfo::action
u16 action
Definition: NetworkClient.h:69
sendmsg
C int sendmsg(int sockfd, const struct msghdr *msg, int flags)
Send multiple datagrams to a remote host.
Definition: sendmsg.cpp:24
NetworkClient::SendMultiple
@ SendMultiple
Definition: NetworkClient.h:56
errno.h
NetworkClient::PacketInfo::size
Size size
Definition: NetworkClient.h:82