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
25extern 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}
u32 flags
Definition IntelACPI.h:3
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 void * memcpy(void *dest, const void *src, size_t count)
Copy memory from one place to another.
Definition memcpy.cpp:20
C ssize_t read(int fildes, void *buf, size_t nbyte)
Read from a file.
Definition read.cpp:22
#define ERANGE
Result too large.
Definition errno.h:259
Size socklen_t
Definition socket.h:50
slong Error
Error code defined in Error.h.
Definition Types.h:159
#define C
Used to define external C functions.
Definition Macros.h:134
Socket information.
Defines a socket address and port pair.
Definition socket.h:36
u32 addr
Definition socket.h:37
u16 port
Definition socket.h:38