-
Notifications
You must be signed in to change notification settings - Fork 0
/
sneaky_client.c
91 lines (85 loc) · 2.42 KB
/
sneaky_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
#include <string.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <unistd.h>
#define BUFFSIZE 10000
int main(int argc, char *argv[])
{
int sd;
struct sockaddr_in server_address;
char buffer[BUFFSIZE];
char shellDir[BUFFSIZE];
//int portNumber;
char serverIP[29];
int rc = 0;
//checks number of command args
if (argc < 2){
printf ("usage is client <ipaddr> \n");
exit(1);
}
// creates socket connection
sd = socket(AF_INET, SOCK_STREAM, 0);
// socket varification
if (sd == -1) {
printf("Failed to create socket conection...\n");
exit(1);
}
else
printf("Successfully created socket connection..\n");
//portNumber = strtol(argv[2], NULL, 10);
strcpy(serverIP, argv[1]);
server_address.sin_family = AF_INET;
server_address.sin_port = htons(24000);
server_address.sin_addr.s_addr = inet_addr(serverIP);
// checks connetion between client and server
if (connect (sd, (struct sockaddr *) &server_address, sizeof (struct sockaddr_in ))<0){
close (sd);
perror ("error connecting stream socket ");
exit (1);
} else printf("Successfully Connected..\n");
int n;
for (;;) {
//clears memory buffer
memset(buffer, 0, BUFFSIZE);
//memset(shellDir,0,80);
//reads input sent from server about working dir
rc = read(sd, buffer, 200);
if (rc == 0 ){
printf("Server has closed\n");
close (sd); // close the socket
break;
}
//emulates shell
printf("[~%s]$ ", buffer);
n = 0;
memset(buffer, 0, BUFFSIZE);
//gets command string from user
while ((buffer[n++] = getchar()) != '\n');
//checks to see if user wants to exit shell
if ((strncmp(buffer, "exit", 4)) == 0) {
printf("Client Sneakily Exiting...\n");
break;
}
int lastChar = strlen(buffer) - 1;
if (buffer[lastChar] == '\n'){
buffer[lastChar] = '\0';
}
//sends command to sever
write(sd, buffer, sizeof(buffer));
//clears buffer to use for recieving
memset(buffer, 0, BUFFSIZE);
//reads input sent from server about command
rc = read(sd, buffer, sizeof(buffer));
if (rc == 0 ){
printf("Server has closed\n");
close (sd); // close the socket
break;
}
//prints message from server
printf("\n%s\n", buffer);
}
return 0;
}