7 #include <sys/socket.h>
8 #include <netinet/in.h>
10 #include <arpa/inet.h>
13 #include <libnotify/notify.h>
15 #define PORT "3490" // the port users will be connecting to
17 #define BACKLOG 10 // how many pending connections queue will hold
19 void sigchld_handler(int s
)
21 while(waitpid(-1, NULL
, WNOHANG
) > 0);
25 void *get_in_addr(struct sockaddr
*sa
)
27 if (sa
->sa_family
== AF_INET
) {
28 return &(((struct sockaddr_in
*)sa
)->sin_addr
);
34 int sockfd
, activefd
; // listen on sockfd, new connection on activefd
35 struct addrinfo hints
, *servinfo
, *p
;
36 struct sockaddr_storage their_addr
; // connector's address information
40 char source
[INET6_ADDRSTRLEN
];
41 // char CLIENT_IP[15] = "107.21.205.69\0";
44 memset(&hints
, 0, sizeof hints
); // make sure the struct is empty
45 hints
.ai_family
= AF_INET
; // IPv4
46 hints
.ai_socktype
= SOCK_STREAM
;
47 hints
.ai_flags
= AI_PASSIVE
; // use my IP
49 if ((rv
= getaddrinfo(NULL
, PORT
, &hints
, &servinfo
)) != 0) {
50 fprintf(stderr
, "getaddrinfo: %s\n", gai_strerror(rv
));
54 // servinfo now points to a linked list of 1 or more struct addrinfos
56 // loop through all the results and bind to the first we can
57 for(p
= servinfo
; p
!= NULL
; p
= p
->ai_next
) {
58 if ((sockfd
= socket(p
->ai_family
, p
->ai_socktype
,
59 p
->ai_protocol
)) == -1) {
60 perror("irssi-notify: socket");
64 if (setsockopt(sockfd
, SOL_SOCKET
, SO_REUSEADDR
, &yes
,
70 if (bind(sockfd
, p
->ai_addr
, p
->ai_addrlen
) == -1) {
72 perror("irssi-notify: bind");
80 fprintf(stderr
, "irssi-notify: failed to bind\n");
84 freeaddrinfo(servinfo
); // all done with this structure
86 if (listen(sockfd
, BACKLOG
) == -1) {
91 sa
.sa_handler
= sigchld_handler
; // reap all dead processes
92 sigemptyset(&sa
.sa_mask
);
93 sa
.sa_flags
= SA_RESTART
;
94 if (sigaction(SIGCHLD
, &sa
, NULL
) == -1) {
99 printf("irssi-notify: waiting for connections...\n");
101 while(1) { // main accept() loop
102 sin_size
= sizeof their_addr
;
103 activefd
= accept(sockfd
, (struct sockaddr
*)&their_addr
, &sin_size
);
104 if (activefd
== -1) {
109 inet_ntop(their_addr
.ss_family
,
110 get_in_addr((struct sockaddr
*)&their_addr
),
111 source
, sizeof source
);
113 if (strcmp(source
, "50.16.219.8") != 0 &&
114 strcmp(source
, "127.0.0.1") != 0){
119 if (!fork()) { // this is the child process
120 close(sockfd
); // child doesn't need the listener
122 if (recv(activefd
, buf
, 99, 0) == -1) {
127 printf("irssi-notify: recieved '%s'\n", buf
);
128 notify_init("irssi-notify");
129 NotifyNotification
* notification
=
130 notify_notification_new("", buf
, "irssi-notify");
131 notify_notification_show(notification
, NULL
);