/[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.11 - (show annotations)
Sat May 7 09:28:12 2005 UTC (20 years, 10 months ago) by sysadm
Branch: MAIN
Changes since 1.10: +43 -7 lines
Content type: text/x-csrc
*** empty log message ***

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

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