FreeNOS
sendto.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 <NetworkClient.h>
19#include <stdlib.h>
20#include <unistd.h>
21#include <string.h>
22#include <sys/socket.h>
23#include <errno.h>
24
25extern C int sendto(int sockfd, const void *buf, size_t len, int flags,
26 const struct sockaddr *addr, socklen_t addrlen)
27{
28 char packet[2048];
30
31 if (len > sizeof(packet) - addrlen)
32 return ERANGE;
33
34 info.address = addr->addr;
35 info.port = addr->port;
37
38 memcpy(packet, &info, sizeof(info));
39 memcpy(packet + sizeof(info), buf, len);
40
41 return ::write(sockfd, packet, len + sizeof(info));
42}
u32 flags
Definition IntelACPI.h:3
C int sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t addrlen)
Send a single datagram to a remote host.
Definition sendto.cpp:25
C void * memcpy(void *dest, const void *src, size_t count)
Copy memory from one place to another.
Definition memcpy.cpp:20
#define ERANGE
Result too large.
Definition errno.h:259
Size socklen_t
Definition socket.h:50
#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