]>
jfr.im git - irc/quakenet/newserv.git/blob - nickrate/nickrate.c
1 #include "../server/server.h"
2 #include "../core/hooks.h"
3 #include "../core/events.h"
4 #include "../nick/nick.h"
7 #include <sys/socket.h>
9 #include "../core/error.h"
13 #include <sys/ioctl.h>
15 #include <netinet/in.h>
17 #include "../lib/version.h"
23 int nickrate_listenfd
;
25 void nr_nick(int hooknum
, void *arg
);
26 void nr_handlelistensocket(int fd
, short events
);
27 int nr_openlistensocket(int portnum
);
32 registerhook(HOOK_NICK_NEWNICK
, &nr_nick
);
33 registerhook(HOOK_NICK_LOSTNICK
, &nr_nick
);
35 nickrate_listenfd
=nr_openlistensocket(6002);
36 if (nickrate_listenfd
>0) {
37 registerhandler(nickrate_listenfd
,POLLIN
,&nr_handlelistensocket
);
43 deregisterhook(HOOK_NICK_NEWNICK
, &nr_nick
);
44 deregisterhook(HOOK_NICK_LOSTNICK
, &nr_nick
);
45 deregisterhandler(nickrate_listenfd
,1);
48 void nr_nick(int hooknum
, void *arg
) {
51 if (serverlist
[homeserver(np
->numeric
)].linkstate
== LS_LINKED
) {
52 if (hooknum
==HOOK_NICK_NEWNICK
) {
60 int nr_openlistensocket(int portnum
) {
61 struct sockaddr_in sin
;
65 if ((fd
=socket(AF_INET
,SOCK_STREAM
,0))==-1) {
66 Error("proxyscan",ERR_ERROR
,"Unable to open listening socket (%d).",errno
);
70 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *) &opt
, sizeof(opt
))!=0) {
71 Error("proxyscan",ERR_ERROR
,"Unable to set SO_REUSEADDR on listen socket.");
75 /* Initialiase the addresses */
76 memset(&sin
,0,sizeof(sin
));
77 sin
.sin_family
=AF_INET
;
78 sin
.sin_port
=htons(portnum
);
80 if (bind(fd
, (struct sockaddr
*) &sin
, sizeof(sin
))) {
81 Error("proxyscan",ERR_ERROR
,"Unable to bind listen socket (%d).",errno
);
87 if (ioctl(fd
, FIONBIO
, &opt
)!=0) {
88 Error("proxyscan",ERR_ERROR
,"Unable to set listen socket non-blocking.");
95 /* Here's what we do when poll throws the listen socket at us:
96 * accept() the connection, spam a short message and immediately
99 void nr_handlelistensocket(int fd
, short events
) {
100 struct sockaddr_in sin
;
101 socklen_t addrsize
=sizeof(sin
);
104 if ((newfd
=accept(fd
, (struct sockaddr
*)&sin
, &addrsize
))>0) {
105 /* Got new connection */
106 sprintf(buf
,"%u\n",nicks
);
107 write(newfd
,buf
,strlen(buf
));
108 sprintf(buf
,"%u\n",quits
);
109 write(newfd
,buf
,strlen(buf
));