-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecho_s.c
More file actions
221 lines (187 loc) · 5.78 KB
/
echo_s.c
File metadata and controls
221 lines (187 loc) · 5.78 KB
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
// file: echo_s.c
// author: F. Dolisy
// date: 11/26/2020
// purpose: CS3377
// description:
// this program runs a multithreaded server that can listen to a max of 3 user inputted ports and both tcp and udp sockets
#include <stdio.h>
#include <signal.h>
#include <string.h> //strlen
#include <stdlib.h>
#include <errno.h>
#include <unistd.h> //close
#include <arpa/inet.h> //close
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/time.h> //FD_SET, FD_ISSET, FD_ZERO macros
#include<time.h>
int total_sockets = 0;
fd_set rset;
int max(int x, int y)
{
if (x > y)
return x;
else
return y;
}
// a socket containing its file descriptor and its type(udp or tcp)
struct Socket_Info
{
int fd;
int socket_type;
};
struct Socket_Info sockets[6];
//add a socket to array
void add_socket(int fd, int type)
{
sockets[total_sockets].fd = fd;
sockets[total_sockets].socket_type = type;
total_sockets++;
}
//function to make a tcp and udp socket for a specific port
int make_socket_for_port(struct sockaddr_in* serverAddr)
{
//add a listening tcp socket for desired port
int tcp_socket = socket(AF_INET, SOCK_STREAM, 0);
if(tcp_socket < 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
int ret = bind(tcp_socket, (struct sockaddr*)serverAddr, sizeof(*serverAddr));
if(ret < 0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(tcp_socket, 10) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
add_socket(tcp_socket, SOCK_STREAM);
//add a listening udp socket for desired port
int udp_socket = socket(AF_INET, SOCK_DGRAM, 0);
bind(udp_socket, (struct sockaddr*)serverAddr, sizeof(*serverAddr));
add_socket(udp_socket, SOCK_DGRAM);
FD_SET(tcp_socket, &rset);
FD_SET(udp_socket, &rset);
//return the max to be usd for the select later on
return max(tcp_socket, udp_socket);
}
//send a message to the log
void log_message(char* from, char* message, int logSock, struct sockaddr_in *logAddr)
{
char buf[2048];
//get time of message
char cur_time[128];
time_t t;
struct tm* ptm;
t = time(NULL);
ptm = localtime(&t);
strftime(cur_time, 128, "%d-%b-%Y %H:%M:%S", ptm);
message[strlen(message) - 1] = '\0';
memset(buf, 0, sizeof(buf));
sprintf(buf, " %s \"%s\" was recieved from %s", cur_time, message, from);
sendto(logSock, (const char*)buf, sizeof(buf), 0, (struct sockaddr*)logAddr, sizeof(*logAddr));
}
int main(int argc , char *argv[])
{
int nready,len, logSock, newSocket;
struct sockaddr_in newAddr;
struct sockaddr_in logAddr;
struct sockaddr_in serverAddr;
socklen_t addr_size = sizeof(newAddr);
char buffer[1024];
pid_t childpid, childpid2;
int largestMax = -1;
//make a socket for the log connection
logSock = socket(AF_INET, SOCK_DGRAM, 0);
memset(&logAddr, '\0', sizeof(logAddr));
logAddr.sin_family = AF_INET;
logAddr.sin_addr.s_addr = INADDR_ANY;
logAddr.sin_port = htons(9999);
//bind(logSock, (const struct sockaddr*)&serverAddr, sizeof(serverAddr));
FD_ZERO(&rset);
//loop through and create tcp and udp sockets for all ports entered
int port_arg;
for(port_arg = 1; port_arg < argc; port_arg++)
{
int port = atoi(argv[port_arg]);
memset(&serverAddr, '\0', sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons(port);
int max = make_socket_for_port(&serverAddr);
//used for the select function
if (max > largestMax)
largestMax = max;
}
fd_set saved_rset;
memcpy(&saved_rset, &rset, sizeof(fd_set));
while(1)
{
//select the active socket
int max = largestMax + 1;
nready = select(max, &rset, NULL, NULL, NULL);
int i;
for(i = 0; i < total_sockets; ++i)
{
if (FD_ISSET(sockets[i].fd, &rset) == 0)
continue;
//if active socket is of type TCP
if (sockets[i].socket_type == SOCK_STREAM)
{
newSocket = accept(sockets[i].fd, (struct sockaddr*)&newAddr, &addr_size);
if(newSocket < 0)
{
perror("accept");
exit(EXIT_FAILURE);
}
}
//multithreading to handle multiple clients
if((childpid = fork()) == -1)
{
printf("closing socket\n");
close(sockets[i].fd);
}
else if(childpid > 0)
{
while(1)
{
//if socket is of type TCP
if(sockets[i].socket_type == SOCK_STREAM)
{
//recieve and echo a message back
bzero(buffer, sizeof(buffer));
recv(newSocket, buffer, 1024, 0);
printf("Message recieved from client: %s\n", buffer);
int buf2[2024];
memcpy(buf2, buffer, sizeof(buffer));
log_message(inet_ntoa(newAddr.sin_addr),buf2, logSock, &logAddr);
char* newline = '\n';
strncat(buf2, &newline, 1);
send(newSocket, buffer, strlen(buffer),0);
}
else //socket is of type UDP
{
//recieve and echo a message back
len = sizeof(newAddr);
bzero(buffer, sizeof(buffer));
recvfrom(sockets[i].fd, buffer, sizeof(buffer), 0, (struct sockaddr*)&newAddr, &len);
printf("Message recieved from client: %s", buffer);
log_message(inet_ntoa(newAddr.sin_addr), buffer, logSock, &logAddr);
char* newline = '\n';
strncat(buffer, &newline, 1);
sendto(sockets[i].fd, (const char*)buffer, sizeof(buffer), 0, (struct sockaddr*)&newAddr, sizeof(newAddr));
}
}
}
}
// save the set of file descriptors for the select function
memcpy(&rset, &saved_rset, sizeof(fd_set));
}
close(newSocket);
return 0;
}