3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 #include <sys/types.h>
37 int sock
; // main socket
38 char buf
[257]; // receive buffer
39 unsigned addrlen
; // length of clientInfo
42 struct sockaddr_in sockName
; // name of socket
43 struct sockaddr_in clientInfo
; // connected client information
47 extern client_t client_list
;
49 /** Network Send function */
50 int net_send (int sock
, char *buf
, unsigned len
)
52 return send (sock
, buf
, len
, 0);
55 /** Network Init function */
59 if ((sock
= socket (AF_INET
, SOCK_STREAM
, IPPROTO_TCP
)) == -1) {
60 printf ("> ERROR -> I can't create main socket");
64 /* Set to nonblocking socket mode */
65 int oldFlag
= fcntl (sock
, F_GETFL
, 0);
66 if (fcntl (sock
, F_SETFL
, oldFlag
| O_NONBLOCK
) == -1) {
67 printf ("> ERROR -> Problem with set socket mode");
72 /* Unblock re-use of previously binded port, when server die or something else */
73 unsigned long yes
= 1;
74 setsockopt (sock
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &yes
, sizeof (yes
));
77 // Put data to structure sockaddr_in
78 // 1) Family of protocol
79 sockName
.sin_family
= AF_INET
;
80 // 2) Number of listen port
81 sockName
.sin_port
= htons (DEFAULT_NET_PORT
);
82 // 3) Set local ip address. You can connect in with whatever IP
83 sockName
.sin_addr
.s_addr
= INADDR_ANY
;
86 if (bind (sock
, (struct sockaddr
*) &sockName
, sizeof (sockName
)) == -1) {
87 printf ("> ERROR -> Port is used or danied");
91 // Crete front (max 100 sessions in one time)
92 if (listen (sock
, 100) == -1) {
93 printf ("> ERROR -> I can't create front");
97 addrlen
= sizeof (clientInfo
);
102 /** Network Loop function */
105 int newsock
= accept (sock
, (struct sockaddr
*) &clientInfo
, &addrlen
);
107 /* new client is connected */
109 if (errno
== EAGAIN
&& newsock
!= -1) {
113 client_new (newsock
);
115 /* Set to nonblocking socket mode */
116 int oldFlag
= fcntl (newsock
, F_GETFL
, 0);
117 if (fcntl (newsock
, F_SETFL
, oldFlag
| O_NONBLOCK
) == -1) {
118 printf ("> ERROR -> Problem with set socket mode\n");
122 printf ("-> New client was connected\n");
125 /* HACK: this is pretty needed for less cpu load */
129 for (c
= client_list
.next
; c
!= &client_list
; c
= c
->next
) {
130 int ret
= recv (c
->sock
, buf
, 256, 0);
132 /* new data are available */
136 /* handle received data */
137 proto_handler (c
, buf
, ret
);
150 printf ("-> Client was disconnected\n");