-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclustershell_client.c
231 lines (180 loc) · 6.33 KB
/
clustershell_client.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>
#define MAX_NUM_CLI 64
#define MAX_CMD_LEN 1024
#define MAX_BUF_SIZE 4096
#define CLIENT_PORT 5100
#define SERVER_PORT 5200
#define CONFIG_FILE "clustershell.cfg"
typedef struct _CONFIG_ENTRY {
char * name;
char * ip;
} CONFIG_ENTRY;
typedef struct _CMD_STRUCT {
int node;
char * cmd;
} CMD_STRUCT;
void err_exit(const char * err_msg, int sock_fd) {
perror(err_msg);
if (sock_fd > 0) {
close(sock_fd);
printf("Closed socket %d...\n", sock_fd);
}
exit(EXIT_FAILURE);
}
CONFIG_ENTRY ** read_config(const char * filename) {
CONFIG_ENTRY ** config = malloc((MAX_NUM_CLI + 1) * sizeof(CONFIG_ENTRY *));
FILE * config_fp = fopen(filename, "r");
if (config_fp == NULL)
err_exit("Error opening config. Exiting...\n", -1);
char name[12], ip[20];
size_t i;
while(fscanf(config_fp, " %s", name) != EOF) {
fscanf(config_fp, " %s", ip);
config[i] = malloc(sizeof(CONFIG_ENTRY));
config[i]->name = strdup(name);
config[i]->ip = strdup(ip);
++i;
}
config[i] = NULL;
return config;
}
void free_config(CONFIG_ENTRY ** config) {
size_t i = 0;
while (config[i] != NULL) {
free(config[i]->name);
free(config[i]->ip);
free(config[i]);
++i;
}
free(config);
}
int server_init(int port) {
struct sockaddr_in serv_addr = {0};
int serv_sock = socket(AF_INET, SOCK_STREAM, 0);
if (serv_sock < 0)
err_exit("Error in socket. Exiting...\n", -1);
int sockopt_optval = 1;
if (setsockopt(serv_sock, SOL_SOCKET, SO_REUSEADDR, &sockopt_optval, sizeof(sockopt_optval)) < 0)
err_exit("Error in setsockopt. Exiting...\n", serv_sock);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(port);
if (bind(serv_sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
err_exit("Error in bind. Exiting...\n", serv_sock);
if (listen(serv_sock, 5) < 0)
err_exit("Error in listen. Exiting...\n", serv_sock);
return serv_sock;
}
int client_init(char * ip, int port) {
struct sockaddr_in serv_addr = {0};
serv_addr.sin_family = AF_INET;
if (ip == NULL)
serv_addr.sin_addr.s_addr = INADDR_ANY;
else
inet_aton(ip, &(serv_addr.sin_addr));
serv_addr.sin_port = htons(port);
int client_sock = socket(AF_INET, SOCK_STREAM, 0);
if (connect(client_sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
err_exit("Error in connect. Exiting...\n", client_sock);
return client_sock;
}
char * execute_single_cmd(char * cmd, size_t cmd_size, size_t * out_size) {
char * tmp_cmd = strdup(cmd);
char * strtok_saveptr;
*out_size = 0;
char * token = strtok_r(tmp_cmd, " ", &strtok_saveptr);
if (token == NULL)
return NULL;
if (strcmp(token, "cd") == 0) {
// handle 'cd
token = strtok_r(NULL, "|", &strtok_saveptr); // remaining token
if (chdir(token) < 0)
perror("Error in changing directory...");
*out_size = 0;
return "";
}
else {
// run the command through shell
char * data = cmd + strlen(cmd) + 1;
int pipe_fd[2];
if (pipe(pipe_fd) < 0)
err_exit("Error in pipe. Exiting...\n", -1);
write(pipe_fd[1], data, cmd_size-strlen(cmd)-1);
close(pipe_fd[1]);
dup2(pipe_fd[0], 0);
FILE * cmd_fp = popen(cmd, "r");
if (cmd_fp == NULL)
perror("Error in popen. Exiting...\n");
char * cmd_out = malloc((MAX_BUF_SIZE+1) * sizeof(char));
*out_size = fread(cmd_out, 1, MAX_BUF_SIZE+1, cmd_fp);
if (*out_size < 0)
err_exit("Error in fread. Exiting...\n", -1);
cmd_out[*out_size] = '\0';
pclose(cmd_fp);
return cmd_out;
}
free(tmp_cmd);
return "";
}
void prompt() {
char cwd[200];
getcwd(cwd, 200);
printf("\n(%s) >> ", cwd);
}
int main() {
pid_t conn_handler = fork();
if (conn_handler < 0)
err_exit("Error in fork. Exiting...\n", -1);
else if (conn_handler == 0) {
// Handle communication with server and run commands requested by server
int client_sock; // 'client_sock' represents actual server
int serv_sock = server_init(CLIENT_PORT); // 'ser_sock' is the current node (this client)
while (true) {
struct sockaddr_in client_addr;
int client_len = sizeof(client_addr);
client_sock = accept(serv_sock, (struct sockaddr *) &client_addr, &client_len);
if (client_sock < 0)
err_exit("Error in accept. Exiting...\n", client_sock);
char cmd[MAX_CMD_LEN+1];
size_t nbytes = read(client_sock, cmd, MAX_CMD_LEN+1);
if (nbytes < 0)
err_exit("Error in read. Exiting...\n", client_sock);
cmd[nbytes] = '\0';
size_t out_size;
char * cmd_out = execute_single_cmd(cmd, nbytes, &out_size);
// reply back to server with response of the command
write(client_sock, cmd_out, out_size);
free(cmd_out);
close(client_sock);
}
}
else {
// Handle shell
int client_connect = client_init(NULL, SERVER_PORT);
while(true) {
prompt();
char * cmd = malloc(sizeof(char) * (MAX_CMD_LEN + 1));
size_t max_cmd_len = MAX_CMD_LEN;
size_t cmd_len = getline(&cmd, &max_cmd_len, stdin);
cmd[cmd_len - 1] = '\0';
cmd_len = strlen(cmd);
if(write(client_connect, cmd, cmd_len) < 0)
err_exit("Error in writing to server. Exiting...\n", client_connect);
char* server_resp = malloc(sizeof(char) * (MAX_BUF_SIZE + 1));
size_t resp_size = read(client_connect, server_resp, MAX_BUF_SIZE);
if(resp_size < 0)
err_exit("Error in reading from server. Exiting...\n", client_connect);
server_resp[resp_size] = '\0';
printf("%s\n", server_resp);
}
close(client_connect);
}
return EXIT_SUCCESS;
}