forked from GrupoNove/09_RC_2012
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DummySmb.c
executable file
·267 lines (221 loc) · 6.7 KB
/
DummySmb.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//smb.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#define SMBPORT 58000
#define STATPORT 59000
#define NG 9
#define BUFFER_SIZE 128
void getArgs(int argc, char **argv, int *SMBport, char **STATname, int *STATport) {
if (argc == 1) {
*SMBport = SMBPORT + NG;
*STATname = "localhost";
*STATport = STATPORT;
} else if (argc == 3) {
if (!strcmp(argv[1], "-p")) {
*SMBport = atoi(argv[2]);
*STATname = "localhost";
*STATport = STATPORT;
}
else if (!strcmp(argv[1], "-n")) {
*STATname = argv[2];
*SMBport = SMBPORT + NG;
*STATport = STATPORT;
}
else if (!strcmp(argv[1], "-t")) {
*STATport = atoi(argv[2]);
*SMBport = SMBPORT + NG;
*STATname = "localhost";
} else {
printf("\nArgument not known: %s\n", argv[1]);
exit(1);
}
} else if(argc == 5) {
if (!strcmp(argv[1], "-n") && !strcmp(argv[3], "-p")) {
*STATname = argv[2];
*SMBport = atoi(argv[4]);
*STATport = STATPORT;
}
else if (!strcmp(argv[1], "-p") && !strcmp(argv[3], "-n")) {
*SMBport = atoi(argv[2]);
*STATname = argv[4];
*STATport = STATPORT;
}
else if (!strcmp(argv[1], "-p") && !strcmp(argv[3], "-t")) {
*SMBport = atoi(argv[2]);
*STATport = atoi(argv[4]);
*STATname = "localhost";
}
else if (!strcmp(argv[1], "-t") && !strcmp(argv[3], "-p")) {
*STATport = atoi(argv[2]);
*SMBport = atoi(argv[4]);
*STATname = "localhost";
}
else if (!strcmp(argv[1], "-t") && !strcmp(argv[3], "-n")) {
*STATport = atoi(argv[2]);
*STATname = argv[4];
*SMBport = SMBPORT + NG;
}
else if (!strcmp(argv[1], "-n") && !strcmp(argv[3], "-t")) {
*STATname = argv[2];
*STATport = atoi(argv[4]);
*SMBport = SMBPORT + NG;
} else {
printf("\nArgument not known: %s %s\n", argv[2], argv[4]);
exit(1);
}
} else if (argc == 7) {
if (!strcmp(argv[1], "-p") && !strcmp(argv[3], "-n") && !strcmp(argv[5], "-t")) {
*SMBport = atoi(argv[2]);
*STATname = argv[4];
*STATport = atoi(argv[6]);
}
else if (!strcmp(argv[1], "-n") && !strcmp(argv[3], "-p") && !strcmp(argv[5], "-t")) {
*STATname = argv[2];
*SMBport = atoi(argv[4]);
*STATport = atoi(argv[6]);
}
else if (!strcmp(argv[1], "-p") && !strcmp(argv[3], "-t") && !strcmp(argv[5], "-n")) {
*SMBport = atoi(argv[2]);
*STATport = atoi(argv[4]);
*STATname = argv[6];
}
else if (!strcmp(argv[1], "-t") && !strcmp(argv[3], "-p") && !strcmp(argv[5], "-n")) {
*STATport = atoi(argv[2]);
*SMBport = atoi(argv[4]);
*STATname = argv[6];
}
else if (!strcmp(argv[1], "-t") && !strcmp(argv[3], "-n") && !strcmp(argv[5], "-p")) {
*STATport = atoi(argv[6]);
*STATname = argv[2];
*SMBport = atoi(argv[4]);
} else {
printf("\nArgument not known: %s %s\n", argv[2], argv[4]);
exit(1);
}
}
else {
printf("\nCorrect sintaxe: SMB [-p SMBport] [-n *STATname] [-t STATport]\n");
exit(1);
}
}
int main(int argc, char **argv) {
int fd, addrlen, ret, newfd;
int fdStat, nStat, nreadStat;
int addrStatlen;
struct sockaddr_in addrStat;
int nread, nwritten, nbytes;
struct sockaddr_in addr;
char *ptr, buffer[BUFFER_SIZE], buffer2[BUFFER_SIZE];
struct hostent *hostptr;
int SMBport, STATport;
char *STATname;
char *userTokens;
char reg[BUFFER_SIZE], udp[BUFFER_SIZE];
struct in_addr* ip;
char hostname[BUFFER_SIZE];
getArgs(argc, argv, &SMBport, &STATname, &STATport);
fdStat = socket(AF_INET, SOCK_DGRAM, 0);
if(fdStat == -1) {
fprintf(stderr, "ERROR: Couldn't create de UDP socket...\n");
exit(1);
}
hostptr = gethostbyname(STATname);
memset((void*)&addrStat, (int)'\0', sizeof(addrStat));
addrStat.sin_family = AF_INET;
addrStat.sin_addr.s_addr = ((struct in_addr*)(hostptr->h_addr_list[0]))->s_addr;
addrStat.sin_port = htons(STATport);
if(gethostname(hostname, BUFFER_SIZE)==-1) {
fprintf(stderr, "ERROR: Couldn't send message to STAT...\n");
exit(1);
}
hostptr = gethostbyname(hostname);
addrStatlen = sizeof(addrStat);
ip = (struct in_addr*)(hostptr->h_addr_list[1]);
if(ip == NULL) ip = (struct in_addr*)(hostptr->h_addr_list[0]);
sprintf(reg, "OLA %s %d\n", inet_ntoa(*ip), SMBport);
nStat = sendto(fdStat, reg, strlen(reg), 0, (struct sockaddr*)&addrStat, addrStatlen);
if(nStat == -1) {
fprintf(stderr, "ERROR: Couldn't send message to STAT...\n");
exit(1);
}
nreadStat = recvfrom(fdStat, buffer, BUFFER_SIZE, 0, (struct sockaddr*)&addrStat, &addrStatlen);
if(nreadStat == -1) {
fprintf(stderr, "ERROR: Couldn't receive message from STAT...\n");
exit(1);
}
printf("%s", buffer);
if((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
fprintf(stderr, "ERROR: Couldn't create de TCP socket...\n");
exit(1);
}
memset((void*)&addr, (int)'\0', sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(SMBport);
ret = bind(fd, (struct sockaddr*)&addr, sizeof(addr));
if(ret == -1) {
fprintf(stderr, "ERROR: Couldn't bind socket...\n");
exit(1);
}
if(listen(fd,5) == -1) {
fprintf(stderr, "ERROR: Couldn't listen...\n");
exit(1);
}
while(1) {
addrlen = sizeof(addr);
if((newfd = accept(fd, (struct sockaddr*)&addr, &addrlen)) == -1) {
fprintf(stderr, "ERROR: Couldn't accept TCP connection...\n");
exit(1);
}
nread = read(newfd, buffer, BUFFER_SIZE);
if(nread == -1) {
fprintf(stderr, "ERROR: Couldn't read message from User...\n");
exit(1);
}
printf("%s", buffer);
userTokens = strtok(buffer, " ");
if(strcmp(userTokens, "REQ") == 0) {
userTokens = strtok(NULL, " ");
if(userTokens != NULL) {
sprintf(reg, "OPOP %s %d %s\n", inet_ntoa(*ip), SMBport, userTokens);
nStat = sendto(fdStat, reg, strlen(reg), 0, (struct sockaddr*)&addrStat, addrStatlen);
if(nStat == -1) {
fprintf(stderr, "ERROR: Couldn't send message to STAT...\n");
exit(1);
}
printf("OK: sent msg to stat\n");
memset(buffer2, '\0', sizeof(buffer2));
nreadStat = recvfrom(fdStat, buffer2, BUFFER_SIZE, 0, (struct sockaddr*)&addrStat, &addrStatlen);
if(nreadStat == -1) {
fprintf(stderr, "ERROR: Couldn't receive message from STAT...\n");
exit(1);
}
nwritten = write(newfd, buffer2, strlen(buffer2));
if(nwritten == -1)
exit(1);
printf("%s", buffer2);
} else {
fprintf(stderr, "ERROR: message unknown...\n");
ptr = strcpy(buffer, "KO\n");
nwritten = write(newfd, ptr, strlen(ptr));
if(nwritten == -1)
exit(1);
}
} else {
fprintf(stderr, "ERROR: message unknown...\n");
ptr = strcpy(buffer, "KO\n");
nwritten = write(newfd, ptr, strlen(ptr));
if(nwritten == -1)
exit(1);
}
close(newfd);
}
close(fd);
exit(0);
}