/[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.15 - (show annotations)
Sun May 4 14:54:55 2025 UTC (10 months, 1 week ago) by sysadm
Branch: MAIN
Changes since 1.14: +3 -2 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 (strlen(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 strcpy(hostaddr_server, inet_ntoa(sin.sin_addr));
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 strcpy(hostaddr_client, (const char *)inet_ntoa(sin.sin_addr));
112 port_client = ntohs(sin.sin_port);
113
114 log_std("Accept connection from %s:%d\n", hostaddr_client,
115 port_client);
116
117 if (fork_server() < 0)
118 {
119 log_error("Fork error\n");
120 }
121
122 if (close(socket_client) == -1)
123 {
124 log_error("Close client socket failed\n");
125 }
126 }
127
128 if (close(socket_server) == -1)
129 {
130 log_error("Close server socket failed\n");
131 }
132
133 return 0;
134 }

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