1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2002-2006 Ben Kibbey <bjk@arbornet.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
40 int parse_ics_output(char *str
)
45 int send_to_ics(int sockfd
, const char *format
, ...)
54 len
= vasprintf(&line
, format
, ap
);
56 line
= Malloc(LINE_MAX
);
57 len
= vsnprintf(line
, LINE_MAX
, format
, ap
);
72 if ((n
= select(sockfd
+ 1, NULL
, &fds
, NULL
, &tv
)) > 0) {
73 if (FD_ISSET(sockfd
, &fds
)) {
74 n
= send(sockfd
, line
, len
, 0);
80 message(ERROR
, ANYKEY
, "Attempt #%i. send(): %s", ++try,
86 message(NULL
, ANYKEY
, "try #%i: send() error to socket. "
87 "Expected %i, got %i.", ++try, len
, n
);
103 int ics_connect(int *sockfd
)
105 struct sockaddr_in sa
;
108 memset(&sa
, 0, sizeof(struct sockaddr_in
));
110 if ((h
= gethostbyname(config
.ics_server
)) == NULL
) {
111 message(ERROR
, ANYKEY
, "%s: %s", config
.ics_server
, hstrerror(h_errno
));
116 if (inet_aton(argv[1], &sa.sin_addr) == 0)
117 errx(EXIT_FAILURE, "malformed address");
120 sa
.sin_addr
.s_addr
= inet_addr(h
->h_addr
);
121 sa
.sin_family
= AF_INET
;
122 sa
.sin_port
= htons(config
.ics_port
);
124 if ((*sockfd
= socket(PF_INET
, SOCK_STREAM
, 0)) == -1) {
125 message(ERROR
, ANYKEY
, "socket(): %s", strerror(errno
));
129 fcntl(*sockfd
, F_SETFL
, O_NONBLOCK
);
132 if (bind(*sockfd, (struct sockaddr *)&sa, sizeof(struct sockaddr)) == -1) {
133 message(ERROR, ANYKEY, "bind(): %s", strerror(errno));
138 if (connect(*sockfd
, (struct sockaddr
*)&sa
, sizeof(struct sockaddr
))
140 message(ERROR
, ANYKEY
, "connect(): %s", strerror(errno
));