/[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.16 - (show annotations)
Mon May 5 11:11:06 2025 UTC (10 months, 1 week ago) by sysadm
Branch: MAIN
Changes since 1.15: +7 -3 lines
Content type: text/x-csrc
Refine

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

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