-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforwarding.h
More file actions
69 lines (53 loc) · 1.56 KB
/
Copy pathforwarding.h
File metadata and controls
69 lines (53 loc) · 1.56 KB
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
#ifndef FORWARDING_H
#define FORWARDING_H
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
int forwarding(char* buffer, char* sucIp, int sucPort) {
printf("FORWARDING\n");
struct sockaddr_in their_addr;
struct hostent *he;
int sockfd;
// Create socket
sockfd = socket(PF_INET, SOCK_DGRAM, 0);
if(sockfd == -1) {
printf("Error: No socket created.\n");
return -1;
}
// Resolve hostname to IP Address
if((he = gethostbyname(sucIp)) == NULL) { // get the host info
herror("gethostbyname");
}
// Setup transport address
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(sucPort);
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
long addr_size = sizeof(their_addr);
memset(their_addr.sin_zero, '\0', sizeof their_addr.sin_zero);
// Connect
if(connect(sockfd, (struct sockaddr*)(&their_addr), sizeof(their_addr)) == -1) {
printf("Error: Connecting failed.\n");
return -1;
}
printf("Connected to server.\n");
//Send
if(sendto(sockfd, buffer, 14, 0, (struct sockaddr*)(&their_addr), addr_size) == -1) {
printf("Error: Forwarding failed.");
return -1;
}
printf("Forwarded... \n");
//Close
if(close(sockfd) == -1) {
printf("Error: Closing failed.");
return -1;
}
return 0;
}
#endif //FORWARDING_H