aboutsummaryrefslogtreecommitdiffstats
path: root/test/network/tcpserver.c
blob: 639fbf868da2ec8b8a400bbcb0e73cf6450f66f8 (plain) (blame)
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
/*
 * Copyright (C) 2014-2023 Firejail Authors
 *
 * This file is part of firejail project
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>


int main(int argc, char **argv) {
	int fd, newfd, client_len;
	struct sockaddr_in serv_addr, client_addr;
	int n, pid;

	if (argc < 2) {
		printf("Usage: ./server port-number\n");
		return 1;
	}
	int portno = atoi(argv[1]);

	// init socket
	fd = socket(AF_INET, SOCK_STREAM, 0);
	if (fd < 0) {
		perror("ERROR opening socket");
		return 1;
	}

	// Initialize socket structure
	memset(&serv_addr, 0, sizeof(serv_addr));

	serv_addr.sin_family = AF_INET;
	serv_addr.sin_addr.s_addr = INADDR_ANY;
	serv_addr.sin_port = htons(portno);

	// bind
	if (bind(fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
		perror("bind");
		return 1;
	}

	// listen - 5 pending conncections
	if (listen(fd, 5) < 0) {
		perror("listen");
		return 1;
	}
	client_len = sizeof(client_addr);

	while (1) {
		newfd = accept(fd, (struct sockaddr *) &client_addr, &client_len);

		if (newfd < 0) {
			perror("accept");
			return 1;
		}

		/* Create child process */
		pid = fork();

		if (pid < 0) {
			perror("fork");
			return 1;
		}

		if (pid == 0) {
			// child
			close(fd);
#define MAXBUF 4096
			char buf[MAXBUF];
			memset(buf, 0, MAXBUF);

			int rcv = read(newfd, buf, MAXBUF - 1);
			if (rcv < 0) {
				perror("read");
				exit(1);
			}

			int sent = write(newfd, "response\n", 9);
			if (sent < 9) {
				perror("write");
				return 1;
			}

			exit(0);
		}
		else
			close(newfd);
	}

	return 0;
}