/[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.13 - (show annotations)
Mon Apr 28 03:31:00 2025 UTC (10 months, 2 weeks ago) by sysadm
Branch: MAIN
Changes since 1.12: +99 -100 lines
Content type: text/x-csrc
Update

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

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