-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunameconcurrent.c
235 lines (212 loc) · 6.25 KB
/
unameconcurrent.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
// Create an iterative server that supports tcp and udp and can provide the outcome of
// any uname command possible (i.e. uname -a, uname -v or combination of command line switches).
// Your server will receive as request only the switches requested for the command (i.e. a, or vi).
// Your server must be run with the following command: unameserver (-tcp|-udp) [-port PortNumber].
// If no port is specified you server should bind to a free port and display port allocated in the
// stdout (i.e. "Listening at port number XX"). If port is given no need to print out port number.
// unameserver (-tcp|-udp) [-port PortNumber]
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>// for inet_addr()
#include <arpa/inet.h> // for inet_addr()
#include <fcntl.h> // non-blocking
#include <unistd.h> // for close()
#include <string.h> // for strcmp()
#include <signal.h>
void tcp_child(int);
void udp_child(int, char*, struct sockaddr_in, int);
char* uNameCall(char*);
int main(int argc, char *argv[]){
const char type_tcp[] = "-tcp";
const char type_udp[] = "-udp";
const char port_opt[] = "-port";
int type, sockfd, newSockfd;
pid_t pid;
int port = -1;
int counter = 0;
struct timeval tv;
tv.tv_sec = 5;
tv.tv_usec = 0;
signal(SIGCHLD, SIG_IGN);
if(argc < 2){
const char t = argc - 1;
fprintf(stderr, "Error: Incorrect number of arguemnts. Acceptable format: unameserver (-tcp|-udp) *(-port PortNumber) \n* = Optional ");
return 0;
}
// determine connection type
if(strcmp(type_tcp, argv[1])==0){
type = 0;
printf("type: tcp \n");
}
else if(strcmp(type_udp, argv[1])==0){
type = 1;
printf("type: udp \n");
}
else{
fprintf(stderr, "Error: Incorrect type.");
return 0;
}
// determine port number
if(argc==4){
if(strcmp(port_opt, argv[2])==0){
port = (int)atoi(argv[3]);
}
else{
fprintf(stderr, "Error: Incorrect number of arguemnts. Acceptable format: unameserver (-tcp|-udp) *(-port PortNumber) \n* = Optional ");
}
}
// create socket based on connection type
if(type==0){
sockfd = socket(AF_INET, SOCK_STREAM, 0);
}
else{
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
}
// setup destination address struct
struct sockaddr_in address;
address.sin_family = AF_INET;
if(port>0){
address.sin_port = htons(port);
}
address.sin_addr.s_addr = INADDR_ANY;
int addrlen = sizeof(address);
// binding
if(bind(sockfd, (struct sockaddr *)&address, sizeof(address))<0){
printf("bind failed");
return 0;
}
if(port<0){
printf("Listening on port: %i \n ****** \n\n", ntohs(address.sin_port));
}
if(type==0){ //TCP
//Listening
if(listen(sockfd, 5) < 0){
printf("listen failed");
return 0;
}
for(;;)
{
newSockfd = accept(sockfd, (struct sockaddr *)&address, (socklen_t *)&addrlen);
if(newSockfd<0){
printf("accept failed");
return 0;
}
// timeout option
setsockopt(newSockfd, SOL_SOCKET, SO_SNDTIMEO, (struct timeval *)&tv, sizeof(tv));
setsockopt(newSockfd, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&tv, sizeof(tv));
pid = fork();
if(pid==0){
// child process
close(sockfd);
tcp_child(newSockfd);
}
else if(pid>0){
// parent process
counter++;
printf("Child #: %d\n\n", counter);
close(newSockfd);
}
else if(pid<0){
printf("error forking");
return 0;
}
}
}
else if(type==1){ //UDP
for(;;)
{
int len = -1;
char response[20];
len = recvfrom(sockfd, (char*)&response, sizeof(response)-1, 0, (struct sockaddr *)&address, (socklen_t *)&addrlen);
if(len>0){
response[len] = '\0';
pid = fork();
if(pid==0){
// child process
//udp_child(sockfd, result, address, addrlen);
printf("Client Response: %s \n", (char *)&response);
response[strcspn(response, "\r\n")] = 0;
char *options = (char *)&response;
char *result = uNameCall(options);
printf("Server Response: %s \n", result);
int sent = sendto(sockfd, result, strlen(result), 0, (struct sockaddr *)&address, (socklen_t )addrlen);
if(sent==-1){
fprintf(stderr, "Error: Unableto send. Closing socket.\n");
close(sockfd);
exit(0);
}
close(sockfd);
exit(0);
}
else if(pid>0){
// parent process
counter++;
printf("Child #: %d\n\n", counter);
}
else if(pid<0){
printf("error forking");
return 0;
}
}
}
}
}
void tcp_child(int newSockfd){
int len = -1;
char response[20];
len = recv(newSockfd, (char*)&response, sizeof(response)-1, 0);
//response[len] = '\0';
if(len>0){
response[len] = '\0';
}
printf("Client Request: %s \n", (char *)&response);
response[strcspn(response, "\r\n")] = 0;
char *options = (char *)&response;
char *result = uNameCall(options);
printf("Server Response: %s \n", result);
int sent = send(newSockfd, result, strlen(result), 0);
if(sent==-1){
fprintf(stderr, "Error: Unableto send. Closing socket.");
close(newSockfd);
exit(0);
}
//close(newSockfd);
printf("Closing Connection\n\n *** \n\n");
close(newSockfd);
exit(0);
}
void udp_child(int sockfd, char* result, struct sockaddr_in address, int addrlen){
printf("Server Response: %s \n", result);
int sent = sendto(sockfd, result, strlen(result), 0, (struct sockaddr *)&address, (socklen_t )addrlen);
if(sent==-1){
fprintf(stderr, "Error: Unableto send. Closing socket.");
close(sockfd);
exit(0);
}
close(sockfd);
exit(0);
}
char* uNameCall(char* parameters){
FILE *fp;
char path[1035];
char *uname = "uname -";
char *options = parameters;
char buf[256];
snprintf(buf, sizeof(buf), "%s%s", uname, options);
/* Open the command for reading. */
fp = popen(buf, "r");
if (fp == NULL) {
printf("Failed to run command\n" );
exit(1);
}
/* Read the output a line at a time - output it. */
while (fgets(path, sizeof(path)-1, fp) != NULL) {
}
/* close */
int t = pclose(fp);
char *result = path;
return result;
}