/[LeafOK_CVS]/lbbs/src/net_server.c
ViewVC logotype

Contents of /lbbs/src/net_server.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.18 - (show annotations)
Tue May 6 05:31:26 2025 UTC (10 months, 1 week ago) by sysadm
Branch: MAIN
Changes since 1.17: +3 -4 lines
Content type: text/x-csrc
Update copyright and license information

1 /***************************************************************************
2 net_server.c - description
3 -------------------
4 Copyright : (C) 2004-2025 by Leaflet
5 Email : leaflet@leafok.com
6 ***************************************************************************/
7
8 /***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 3 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
17 #include "net_server.h"
18 #include "common.h"
19 #include "log.h"
20 #include "io.h"
21 #include "fork.h"
22 #include "tcplib.h"
23 #include <sys/socket.h>
24 #include <arpa/inet.h>
25
26 int net_server(const char *hostaddr, in_port_t port)
27 {
28 unsigned int namelen;
29 int result;
30 int flags;
31 struct sockaddr_in sin;
32 fd_set testfds;
33 struct timeval timeout;
34
35 socket_server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
36
37 if (socket_server < 0)
38 {
39 log_error("Create socket failed\n");
40 exit(1);
41 }
42
43 sin.sin_family = AF_INET;
44 sin.sin_addr.s_addr =
45 (strnlen(hostaddr, sizeof(hostaddr)) > 0 ? inet_addr(hostaddr) : INADDR_ANY);
46 sin.sin_port = htons(port);
47
48 if (bind(socket_server, (struct sockaddr *)&sin, sizeof(sin)) < 0)
49 {
50 log_error("Bind address %s:%u failed\n",
51 inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
52 exit(2);
53 }
54
55 if (listen(socket_server, 10) < 0)
56 {
57 log_error("Socket listen failed\n");
58 exit(3);
59 }
60
61 strncpy(hostaddr_server, inet_ntoa(sin.sin_addr), sizeof(hostaddr_server) - 1);
62 hostaddr_server[sizeof(hostaddr_server) - 1] = '\0';
63
64 port_server = ntohs(sin.sin_port);
65
66 log_std("Listening at %s:%d\n", hostaddr_server, port_server);
67
68 namelen = sizeof(sin);
69 while (!SYS_exit)
70 {
71 FD_ZERO(&testfds);
72 FD_SET(socket_server, &testfds);
73
74 timeout.tv_sec = 1;
75 timeout.tv_usec = 0;
76
77 result = SignalSafeSelect(FD_SETSIZE, &testfds, NULL, NULL, &timeout);
78 if (result < 0)
79 {
80 log_error("Accept connection error\n");
81 continue;
82 }
83
84 if (result == 0)
85 {
86 continue;
87 }
88
89 if (FD_ISSET(socket_server, &testfds))
90 {
91 flags = fcntl(socket_server, F_GETFL, 0);
92 fcntl(socket_server, F_SETFL, flags | O_NONBLOCK);
93 while ((socket_client =
94 accept(socket_server, (struct sockaddr *)&sin, &namelen)) < 0)
95 {
96 if (errno != EWOULDBLOCK && errno != ECONNABORTED && errno != EINTR)
97 {
98 log_error("Accept connection error\n");
99 break;
100 }
101 }
102 fcntl(socket_server, F_SETFL, flags);
103 }
104
105 if (socket_client < 0)
106 {
107 log_error("Accept connection error\n");
108 continue;
109 }
110
111 strncpy(hostaddr_client, inet_ntoa(sin.sin_addr), sizeof(hostaddr_client) - 1);
112 hostaddr_client[sizeof(hostaddr_client) - 1] = '\0';
113
114 port_client = ntohs(sin.sin_port);
115
116 log_std("Accept connection from %s:%d\n", hostaddr_client,
117 port_client);
118
119 if (fork_server() < 0)
120 {
121 log_error("Fork error\n");
122 }
123
124 if (close(socket_client) == -1)
125 {
126 log_error("Close client socket failed\n");
127 }
128 }
129
130 if (close(socket_server) == -1)
131 {
132 log_error("Close server socket failed\n");
133 }
134
135 return 0;
136 }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1