-
Notifications
You must be signed in to change notification settings - Fork 2
/
server_ops.c
110 lines (99 loc) · 3.27 KB
/
server_ops.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "server_ops.h"
/*------------------------------------------------------------------------------------------------------------------
-- SOURCE FILE: server_ops.c - This source file holds functions for the server.
--
-- FUNCTIONS: void decrypt_packet_server();
-- int server_file_io(char * recv_buffer);
--
-- DATE: 2014/09/20
--
-- REVISIONS: (Date and Description)
--
-- DESIGNER: Luke Tao
--
-- PROGRAMMER: Luke Tao
--
-- NOTES: These 2 functions are used by the server, for server file I/O operations and packet decryption.
----------------------------------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------------------------------
-- FUNCTION: decrypt_packet_server
--
-- DATE: 2014/09/20
--
-- REVISIONS: (Date and Description)
--
-- DESIGNER: Luke Tao
--
-- PROGRAMMER: Luke Tao
--
-- INTERFACE: void decrypt_packet_server()
--
-- RETURNS: void.
--
-- NOTES: This server essentially reads incoming packets, decrypts both the source IP and port for each packet, and then
-- writes them to a file via server_file_io() function.
----------------------------------------------------------------------------------------------------------------------*/
void decrypt_packet_server()
{
struct recv_tcp
{
struct ip ip;
struct tcphdr tcp;
char buffer[TCP_BUFFER];
} recv_pkt;
int recv_len;
char * data;
char src_port_data[2];
signal(SIGINT, sig_proc);
recv_sock = create_raw_socket(RECV_SOCKET);
while(1)
{
if ((recv_len = read(recv_sock, (struct recv_tcp *)&recv_pkt, TCP_BUFFER)) < 0) {
perror("Cannot read socket. Are you root?\n");
break;
}
/* Check to see if the packet has the SYN/ACK flag set and is the same window size as the client */
if((recv_pkt.tcp.syn == 1) && (ntohs(recv_pkt.tcp.window) == channel_info.w_size))
{
data = convert_ip_to_string(recv_pkt.ip.ip_src);
printf("Receiving Data from Forged Src IP: %s\n", data);
printf("Receiving Data from Src Port: %c\n", ntohs(recv_pkt.tcp.source) / 128);
sprintf(src_port_data, "%c", ntohs(recv_pkt.tcp.source) / 128);
strcat(data, src_port_data); /* Join src IP string with the src port string */
if(server_file_io(data) < 0)
{
perror("Cannot write to file\n");
exit(1);
}
free(data);
}
}
}
/*--------------------------------------------------------------------------------------------------------------------
-- FUNCTION: server_file_io
--
-- DATE: 2014/09/20
--
-- REVISIONS: (Date and Description)
--
-- DESIGNER: Luke Tao
--
-- PROGRAMMER: Luke Tao
--
-- INTERFACE: int server_file_io(char * recv_buffer)
--
-- RETURNS: 0 on successful file writing, -1 if there's a problem opening a file.
--
-- NOTES: This server function essentially opens a file, writes the receiving data into the file, then closes it.
----------------------------------------------------------------------------------------------------------------------*/
int server_file_io(char * recv_buffer)
{
FILE * output;
if((output = fopen(channel_info.filename, "a+")) == NULL){
fprintf(stderr, "Cannot open %s for appending.\n", channel_info.filename);
return -1;
}
fprintf(output, "%s", recv_buffer);
fclose(output);
return 0;
}