-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcommunication.cpp
44 lines (35 loc) · 1.17 KB
/
communication.cpp
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
/*
* Copyright 2016 Riccardo Melioli. All Rights Reserved.
*/
#include "communication.h"
const char* send_packet(unsigned char* packet, const char* dst, const char* port, int size_ip,
int size_tcp) {
struct sockaddr_in sockaddr;
int sd;
const int on = 1;
if ((sd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) {
std::cout << "Couldn't create raw socket" << std::endl;
exit(1);
}
if (setsockopt(sd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)) < 0) {
std::cout << "setsockopt() failed" << std::endl;
exit(1);
}
memset(&sockaddr, 0, sizeof(sockaddr));
sockaddr.sin_family = AF_INET;
sockaddr.sin_port = atoi(port);
sockaddr.sin_addr.s_addr = inet_addr(dst);
if (sendto(sd, packet, size_ip + size_tcp, 0, (struct sockaddr *) &sockaddr,
sizeof(struct sockaddr)) < 0) {
std::cout << "Packet couldn't be sent" << std::endl;
exit(1);
}
struct timeval timeout;
timeout.tv_sec = 3;
timeout.tv_usec = 0;
setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
char recvPacket[4096];
recv(sd, recvPacket, sizeof(recvPacket), 0);
std::string s = recvPacket;
return s.c_str();
}