-
Notifications
You must be signed in to change notification settings - Fork 1
/
gtp_generator.c
69 lines (64 loc) · 1.92 KB
/
gtp_generator.c
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <ifaddrs.h>
#include <stdint.h>
typedef struct gtp_header_t
{
uint8_t flag;
uint8_t type;
uint16_t length;
uint32_t teid;
}__attribute__((packed)) gtp_header;
int main(int argc, char **argv)
{
int sockfd,udpfd,n;
struct sockaddr_in servaddr,remoteaddr;
void *sendbuffer,*recvbuffer;
sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(5202);
printf("%d\n",setsockopt(sockfd,SOL_SOCKET,SO_BINDTODEVICE,"enp0s9",6));
while(1)
{
recvbuffer = calloc(65535,sizeof(char));
printf("before receive:\n");
n = recvfrom(sockfd, recvbuffer,65535, 0, NULL, NULL);
printf("after receive: %d\n",n);
sendbuffer = calloc(65535,sizeof(char));
gtp_header* gtpuheader=calloc(1,sizeof(gtp_header));
gtpuheader->flag = 0x30;
gtpuheader->type = 255;
gtpuheader->length = htons(n+sizeof(gtp_header));
gtpuheader->teid = htonl(0);
memcpy(sendbuffer,gtpuheader, sizeof(gtp_header));
memcpy(sendbuffer+sizeof(gtp_header), recvbuffer, n);
printf("after read and adding header:\n");
udpfd = socket(AF_INET, SOCK_DGRAM, 0);
bzero(&remoteaddr, sizeof(remoteaddr));
remoteaddr.sin_family = AF_INET;
remoteaddr.sin_port = htons(2152);
inet_pton(AF_INET, "10.0.2.6", &remoteaddr.sin_addr);
printf("%d\n",setsockopt(udpfd,SOL_SOCKET,SO_BINDTODEVICE,"enp0s9",6));
sendto(udpfd,sendbuffer,n+sizeof(gtp_header),0,(struct sockaddr*)&remoteaddr,sizeof(remoteaddr));
free(sendbuffer);
free(gtpuheader);
free(recvbuffer);
close(udpfd);
}
}