/[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.14 - (show annotations)
Wed Apr 30 09:18:19 2025 UTC (10 months, 2 weeks ago) by sysadm
Branch: MAIN
Changes since 1.13: +3 -0 lines
Content type: text/x-csrc
Add missing header files
Update compile dependency

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 int namelen, seq, netint, result, flags;
31 struct sockaddr_in sin;
32 char temp[256];
33 fd_set testfds;
34 struct timeval timeout;
35
36 socket_server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
37
38 if (socket_server < 0)
39 {
40 log_error("Create socket failed\n");
41 exit(1);
42 }
43
44 sin.sin_family = AF_INET;
45 sin.sin_addr.s_addr =
46 (strlen(hostaddr) > 0 ? inet_addr(hostaddr) : INADDR_ANY);
47 sin.sin_port = htons(port);
48
49 if (bind(socket_server, (struct sockaddr *)&sin, sizeof(sin)) < 0)
50 {
51 log_error("Bind address %s:%u failed\n",
52 inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
53 exit(2);
54 }
55
56 if (listen(socket_server, 10) < 0)
57 {
58 log_error("Socket listen failed\n");
59 exit(3);
60 }
61
62 strcpy(hostaddr_server, inet_ntoa(sin.sin_addr));
63 port_server = ntohs(sin.sin_port);
64
65 log_std("Listening at %s:%d\n", hostaddr_server, port_server);
66
67 namelen = sizeof(sin);
68 while (!SYS_exit)
69 {
70 FD_ZERO(&testfds);
71 FD_SET(socket_server, &testfds);
72
73 timeout.tv_sec = 1;
74 timeout.tv_usec = 0;
75
76 result = SignalSafeSelect(FD_SETSIZE, &testfds, NULL, NULL, &timeout);
77 if (result < 0)
78 {
79 log_error("Accept connection error\n");
80 continue;
81 }
82
83 if (result == 0)
84 {
85 continue;
86 }
87
88 if (FD_ISSET(socket_server, &testfds))
89 {
90 flags = fcntl(socket_server, F_GETFL, 0);
91 fcntl(socket_server, F_SETFL, flags | O_NONBLOCK);
92 while ((socket_client =
93 accept(socket_server, (struct sockaddr *)&sin, &namelen)) < 0)
94 {
95 if (errno != EWOULDBLOCK && errno != ECONNABORTED && errno != EINTR)
96 {
97 log_error("Accept connection error\n");
98 break;
99 }
100 }
101 fcntl(socket_server, F_SETFL, flags);
102 }
103
104 if (socket_client < 0)
105 {
106 log_error("Accept connection error\n");
107 continue;
108 }
109
110 strcpy(hostaddr_client, (const char *)inet_ntoa(sin.sin_addr));
111 port_client = ntohs(sin.sin_port);
112
113 log_std("Accept connection from %s:%d\n", hostaddr_client,
114 port_client);
115
116 if (fork_server() < 0)
117 {
118 log_error("Fork error\n");
119 }
120
121 if (close(socket_client) == -1)
122 {
123 log_error("Close client socket failed\n");
124 }
125 }
126
127 if (close(socket_server) == -1)
128 {
129 log_error("Close server socket failed\n");
130 }
131
132 return 0;
133 }

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