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 <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/time.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 
29 #include <arpa/inet.h>
30 #include <netdb.h>
31 #include <netinet/in.h>
32 #include <sys/socket.h>
33 
34 #include <signal.h>
35 
36 int main(int argc, char **argv)
37 {
38  int sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
39  if (sock == -1)
40  {
41  printf("Couldn't get a socket: %d [%s]\n", errno, strerror(errno));
42  return 1;
43  }
44 
45  struct timeval t;
46  t.tv_sec = 30;
47 
48  fd_set readfd;
49  FD_SET(sock, &readfd);
50 
51  char *tmp = (char *) malloc(2048);
52  while (1)
53  {
54  select(sock + 1, &readfd, 0, 0, &t);
55  int n = read(sock, tmp, 2048);
56  if (n > 0)
57  printf("interface received %d bytes\n", n);
58  }
59 
60  return 0;
61 }