The Pedigree Project 0.1
net-test.c
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#include <errno.h>
21#include <fcntl.h>
22#include <netdb.h>
23#include <signal.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <unistd.h>
28
29#include <arpa/inet.h>
30#include <netinet/in.h>
31#include <sys/socket.h>
32#include <sys/time.h>
33#include <sys/types.h>
34
35int main(int argc, char** argv) {
36 int sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
37 if (sock == -1) {
38 printf("Couldn't get a socket: %d [%s]\n", errno, strerror(errno));
39 return 1;
40 }
41
42 char tmp[2048];
43 while (1) {
44 // select modifies both arguments, so each iteration needs fresh state.
45 struct timeval timeout = {.tv_sec = 30, .tv_usec = 0};
46 fd_set readfd;
47 FD_ZERO(&readfd);
48 FD_SET(sock, &readfd);
49
50 int ready = select(sock + 1, &readfd, 0, 0, &timeout);
51 if (ready < 0) {
52 if (errno == EINTR)
53 continue;
54 printf("select failed: %d [%s]\n", errno, strerror(errno));
55 close(sock);
56 return 1;
57 }
58 if (!ready)
59 continue;
60
61 int n = read(sock, tmp, sizeof(tmp));
62 if (n > 0)
63 printf("interface received %d bytes\n", n);
64 }
65
66 return 0;
67}