1 /* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details.
7 #include <sys/socket.h>
8 #include <sys/signal.h>
9 #include <netinet/in.h>
12 #include "mixp_local.h"
14 int ixp_serversock_tcp(const char* addr
, int port
, char** errstr
)
17 struct sockaddr_in in_addr
;
19 signal(SIGPIPE
,SIG_IGN
);
20 if ((fd
=socket(AF_INET
, SOCK_STREAM
, 0))<0)
22 *errstr
= "cannot open socket";
26 in_addr
.sin_family
= AF_INET
;
27 in_addr
.sin_port
= htons(port
);
28 in_addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
30 if (bind(fd
,(struct sockaddr
*)&in_addr
, sizeof(struct sockaddr_in
))<0)
32 *errstr
= "cannot bind socket";
37 if (listen(fd
, IXP_MAX_CACHE
)<0)
39 *errstr
= "cannot listen on socket";
48 ixp_listen(MIXP_SERVER
*s
, int fd
, void *aux
,
49 void (*read
)(MIXP_CONNECTION
*c
),
50 void (*close
)(MIXP_CONNECTION
*c
)
56 fprintf(stderr
,"ixp_listen() NULL server struct passed\n");
60 c
= calloc(1,sizeof(MIXP_CONNECTION
));
72 ixp_hangup(MIXP_CONNECTION
*c
) {
77 for(tc
=&s
->conn
; *tc
; tc
=&(*tc
)->next
)
86 shutdown(c
->fd
, SHUT_RDWR
);
93 prepare_select(MIXP_SERVER
*s
) {
97 for(c
= s
->conn
; c
; c
= c
->next
)
101 FD_SET(c
->fd
, &s
->rd
);
106 handle_conns(MIXP_SERVER
*s
) {
107 MIXP_CONNECTION
*c
, *n
;
108 for(c
= s
->conn
; c
; c
= n
) {
110 if(FD_ISSET(c
->fd
, &s
->rd
))
116 ixp_serverloop(MIXP_SERVER
*s
) {
124 r
= select(s
->maxfd
+ 1, &s
->rd
, 0, 0, 0);
128 return "fatal select error";
136 ixp_server_close(MIXP_SERVER
*s
) {
137 MIXP_CONNECTION
*c
, *next
;
138 for(c
= s
->conn
; c
; c
= next
) {