This repository has been archived by the owner on Mar 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc.c
179 lines (124 loc) · 3.7 KB
/
func.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <string.h>
#include "func.h"
int PrepareServer(int portno) {
int sockfd;
struct sockaddr_in serv_addr;
fprintf(stderr, "Trying to open socket\n");
/* First call to socket() function */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("ERROR opening socket");
exit(1);
}
fprintf(stderr, "Trying to set Socket to SO_REUSEADDR\n");
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, sizeof(int)) < 0)
perror("setsockopt(SO_REUSEADDR) failed");
/* Initialize socket structure */
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
fprintf(stderr, "Trying to bind\n");
/* Now bind the host address using bind() call.*/
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
perror("ERROR on binding");
exit(1);
}
return sockfd;
}
void ForkingServer() {
fprintf(stderr, "Starting ForkingServer\n");
socklen_t clilen;
int sockfd, newsockfd;
struct sockaddr_in cli_addr;
char buffer[256];
sockfd = PrepareServer(5001);
/* Now start listening for the clients, here
* process will go in sleep mode and will wait
* for the incoming connection
*/
listen(sockfd,5);
clilen = sizeof(cli_addr);
while (1) {
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0) {
perror("ERROR on accept");
exit(1);
}
/* Create child process */
int pid = fork();
if (pid < 0) {
perror("ERROR on fork");
exit(1);
}
if (pid == 0) {
/* This is the client process */
close(sockfd);
doprocessing(newsockfd);
exit(0);
}
else {
close(newsockfd);
}
} /* end of while */
}
void MonoServer() {
fprintf(stderr, "Starting MonoServer\n");
socklen_t clilen;
int sockfd, newsockfd;
struct sockaddr_in cli_addr;
char buffer[256];
sockfd = PrepareServer(5001);
/* Now start listening for the clients, here process will
* go in sleep mode and will wait for the incoming connection
*/
listen(sockfd,5);
clilen = sizeof(cli_addr);
/* Accept actual connection from the client */
// newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
while (1){
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
//fprintf(stderr, "got socket %d\n", newsockfd);
if (newsockfd < 0) {
perror("ERROR on accept");
exit(1);
}
doprocessing(newsockfd);
//fprintf(stderr, "processed socket %d\n", newsockfd);
close(newsockfd);
//fprintf(stderr, "closed socket %d\n", newsockfd);
}
fprintf(stderr, "could not write to socket\n");
if (newsockfd < 0) {
perror("ERROR on accept");
exit(1);
}
}
void doprocessing (int sock) {
int n;
char buffer[256];
bzero(buffer,256);
n = read(sock,buffer,255);
if (n < 0) {
perror("ERROR reading from socket");
exit(1);
}
fprintf(stderr, "Here is the message: %s\n",buffer);
if (strcmp(buffer, "deadbeef") == 0){
fprintf(stderr, "Message is evil\n");
n = write(sock,"beef is dead",12);
__builtin_trap();
}
n = write(sock,"I got your message",18);
if (n < 0) {
fprintf(stderr, "could not write to socket\n");
//perror("ERROR writing to socket");
//exit(1);
}
}