FreeNOS
recvfrom.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 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 <stdlib.h>
19 #include <unistd.h>
20 #include <string.h>
21 #include <NetworkClient.h>
22 #include <sys/socket.h>
23 #include <errno.h>
24 
25 extern C int recvfrom(int sockfd, void *buf, size_t len, int flags,
26  struct sockaddr *addr, socklen_t addrlen)
27 {
28  char packet[2048];
30 
31  if (len > sizeof(packet) - addrlen)
32  return ERANGE;
33 
34  Error r = ::read(sockfd, packet, sizeof(packet));
35  if (r < 0)
36  return r;
37 
38  memcpy(&info, packet, sizeof(info));
39  addr->addr = info.address;
40  addr->port = info.port;
41 
42  memcpy(buf, packet + sizeof(info), r - sizeof(info));
43  return r - sizeof(info);
44 }
socklen_t
Size socklen_t
Definition: socket.h:50
string.h
flags
u32 flags
Definition: IntelACPI.h:66
NetworkClient::SocketInfo::address
IPV4::Address address
Definition: NetworkClient.h:67
sockaddr::port
u16 port
Definition: socket.h:38
NetworkClient::SocketInfo::port
u16 port
Definition: NetworkClient.h:68
sockaddr
Defines a socket address and port pair.
Definition: socket.h:35
read
ssize_t read(int fildes, void *buf, size_t nbyte)
Read from a file.
Definition: read.cpp:22
recvfrom
C int recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *addr, socklen_t addrlen)
Receive a single datagram from a socket.
Definition: recvfrom.cpp:25
C
#define C
Used to define external C functions.
Definition: Macros.h:134
Error
slong Error
Error code defined in Error.h.
Definition: Types.h:159
sockaddr::addr
u32 addr
Definition: socket.h:37
unistd.h
NetworkClient::SocketInfo
Socket information.
Definition: NetworkClient.h:65
socket.h
ERANGE
#define ERANGE
Result too large.
Definition: errno.h:259
stdlib.h
memcpy
void * memcpy(void *dest, const void *src, size_t count)
Copy memory from one place to another.
Definition: memcpy.cpp:20
NetworkClient.h
errno.h