summaryrefslogtreecommitdiff
path: root/src/bin/great.c
blob: f3bae172adac73830bd15b350e8c0c5449cdd0b0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <net/if_gre.h>

/*great = gre auto tunnel*/

int main(int argc,char *argv[]) {
 struct sockaddr_in sa;
 struct sockaddr_in from;
 char in;
 socklen_t sl=sizeof(struct sockaddr_in);
 int so_inet=socket(AF_INET,SOCK_DGRAM,0);
 int so=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
 memset(&sa,0,(size_t)sizeof(struct sockaddr_in));
 memset(&from,0,(size_t)sizeof(struct sockaddr_in));
 if(argc < 4) {
  printf("usage: %s interface address port\n",argv[0]);
  printf("example: %s gre0 192.168.0.2 1013\n",argv[0]);
  return 1;
 }
 sa.sin_addr.s_addr=inet_addr(argv[2]);
 sa.sin_port=htons(atoi(argv[3]));
 sa.sin_family=PF_INET;
 bind(so,(struct sockaddr *)&sa,sizeof(sa));
 recvfrom(so,&in,1,0,(struct sockaddr *)&from,&sl);
 struct in_addr addr = {from.sin_addr.s_addr};
 printf("setting remote end to %s:%d\n",inet_ntoa(addr),ntohs(from.sin_port));
 connect(so,(struct sockaddr *)&from,sl);

 struct ifreq *data;
 data=malloc(sizeof(struct ifreq));

 memset(data,0,sizeof(struct ifreq));
 strcpy(data->ifr_name,argv[1]);
 data->ifr_value=so;
 printf("gressock: %d\n",ioctl(so_inet,GRESSOCK,data));//set socket

 free(data);
 return 0;
}