-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
167 lines (137 loc) · 4.65 KB
/
server.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* @yalabsi
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <ctype.h>
#define CHARMAX 1024 // maximum chars in a buffer
#define ClIMAX 1024 // maximum client count
int port = 1234;
char* history[10];
int hist_track =0;
char * displayHistory()
{
char* string_history = malloc(10240);
fprintf(stdout, "%s", string_history);
sprintf(string_history, "%s %s", string_history, "echo \"Shell command history:\n");
if(hist_track == 0)
sprintf(string_history, "%s %s", string_history, "There's no commands in history yet\n");
if(hist_track >= 10) {
int j, i= hist_track %10;
for(j =i+1; j<10; j++)
sprintf( string_history, "%s %s", string_history, history[j]);
for(j =0; j<i; j++)
sprintf( string_history, "%s %s", string_history, history[j]);
}else{
int i;
for(i=0; i< hist_track; i++)
sprintf( string_history, "%s %s", string_history, history[i]);
}
sprintf(string_history, "%s %s", string_history, "\"");
return string_history;
}
int main(int argc, char *argv[])
{
//sockets keeps track of the clients
int sock, sockets[ClIMAX];
char buf[CHARMAX + 1], command[CHARMAX + 1];
FILE *temp1, *temp2;
char *cp;
printf("Starting Server");
/*
* Based the initialization of the socket off
* http://space.wccnet.edu/~chasselb/linux275/ClassNotes/sockets/sbasics.htm
*
*/
sock = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in server_address = { 0 }, Client_address = { 0 };
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = INADDR_ANY;
server_address.sin_port = htons(port);
bind(sock, (struct sockaddr *) &server_address, sizeof(server_address));
listen(sock, 128);
int current = 0;
pid_t pid=0;
while (1) {
fflush(stdout);
//child only reaches this point after being disconnected
//we terminate it so that only the parent handles connections
if(pid != 0){
exit(0);
}
printf("\nWaiting For Client\n");
//parent waiting for a connection
socklen_t Client_address_len = sizeof(Client_address);
sockets[current] = accept(sock,(struct sockaddr *)&Client_address,&Client_address_len);
pid = fork();
if(pid == 0){
current++;
continue;
}
temp2 = fdopen(sockets[current],"r");
while (1) {
// if we cant get the string from the stream or
// if the user inputs quit then end the while loop
// to quit the program
if (fgets(buf,CHARMAX,temp2) == NULL)
break;
int i;
for(i=0; i<1024; i++)
if(buf[i] == '\n' || buf[i] == '\0')
break;
else
buf[i] = buf[i] - 5;
if(strstr(buf,"history") != NULL) {
fprintf(stdout, "%s", displayHistory());
temp1 = popen(displayHistory(), "r");
}else if(strstr(buf,"!!") != NULL){
temp1 = popen(history[hist_track % 10], "r");
}else if(strstr(buf,"!10") != NULL){
temp1 = popen(history[(hist_track % 10)], "r"); // 10
}else if(strstr(buf,"!1") != NULL){
temp1 = popen(history[(hist_track+1) % 10], "r");
}else if(strstr(buf,"!2") != NULL){
temp1 = popen(history[(hist_track+2) % 10], "r");
}else if(strstr(buf,"!3") != NULL){
temp1 = popen(history[(hist_track+3) % 10], "r");
}else if(strstr(buf,"!4") != NULL){
temp1 = popen(history[(hist_track+4) % 10], "r");
}else if(strstr(buf,"!5") != NULL){
temp1 = popen(history[(hist_track+5) % 10], "r");
}else if(strstr(buf,"!6") != NULL){
temp1 = popen(history[(hist_track+6) % 10], "r");
}else if(strstr(buf,"!7") != NULL){
temp1 = popen(history[(hist_track+7) % 10], "r");
}else if(strstr(buf,"!8") != NULL){
temp1 = popen(history[(hist_track+8) % 10], "r");
}else if(strstr(buf,"!9") != NULL){
temp1 = popen(history[(hist_track+9) % 10], "r");
}else{
temp1 = popen(buf, "r");
}
// popen runs the shell command and stores the output
history[hist_track % 10] = strdup(buf);
hist_track++;
if (temp1 == NULL)
printf("Error");
while (1) {
// if there was an error somehow with copying the stream
if (fgets(command, CHARMAX, temp1) == NULL)
break;
// print to the appropriate client
write(sockets[current], command, strlen(command));
}
// close the temp shell stream
pclose(temp1);
command[0] = 16;
write(sockets[current],command,1);
}
//closes the socket and its stream
fclose(temp2);
close(sockets[current]);
}
return 0;
}