2 * Copyright (c) 2006 Tsuyoshi SAKAMOTO <skmt.free@gmail.com>,
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
23 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31 /********************************************
33 ********************************************
48 #include <sys/types.h>
52 #include <sys/ioctl.h>
53 #include <arpa/inet.h>
61 /********************************************
63 ********************************************
67 /********************************************
69 ********************************************
73 /********************************************
75 ********************************************
94 /********************************************
96 ********************************************
102 void cfmakeraw(struct termios *);
105 int ic_select(int, long);
106 void ic_plog(int, const char *, ...);
107 void ic_log(const char *, ...);
108 void ic_recv_ntohs(ic_data *);
109 void ic_set_termios(net_termios *, struct termios *);
110 void ic_set_winsz(net_winsz *, struct winsize *);
111 void ic_get_termios(struct termios *, net_termios *);
112 void ic_get_winsz(struct winsize *, net_winsz *);
113 void ic_set_data(ic_data *, u_char, u_char, u_short, u_char *, int);
114 void ic_set_header(ic_data *, u_char, u_char, u_short, u_short);
116 ssize_t xread(int, void *, size_t);
117 ssize_t xwrite(int, void *, size_t);
119 void vsys_err(int, const char *, ...);
120 void vsys(int, const char *, ...);
122 u_short xchecksum(u_short *, int);
126 void *xmalloc(size_t);
127 void *xrealloc(void *, size_t);
129 char *xstrdup(char *);
133 static int xselect(int, fd_set *, fd_set *, fd_set *, struct timeval *);
136 /*============================================================================
138 *============================================================================
142 /*-------------------------------------------------------------------------
143 * cfmakeraw for Solaris
145 *-------------------------------------------------------------------------
148 cfmakeraw(struct termios *t)
150 memset(t, 0, sizeof(struct termios));
152 t->c_iflag |= IGNBRK;
153 t->c_cflag |= CS8|CREAD;
161 /*-------------------------------------------------------------------------
162 * termios and window size
164 *-------------------------------------------------------------------------
167 ic_set_termios(net_termios *pn, struct termios *pt) {
169 pn->c_iflag = htonl(pt->c_iflag);
170 pn->c_oflag = htonl(pt->c_oflag);
171 pn->c_cflag = htonl(pt->c_cflag);
172 pn->c_lflag = htonl(pt->c_lflag);
174 pn->c_ispeed = htonl(pt->c_ispeed);
175 pn->c_ospeed = htonl(pt->c_ospeed);
177 for (i = 0; i < NCCS; ++i) {
178 pn->c_cc[i] = pt->c_cc[i];
184 ic_set_winsz(net_winsz *pn, struct winsize *pt) {
185 pn->ws_row = htons(pt->ws_row);
186 pn->ws_col = htons(pt->ws_col);
187 pn->ws_xpixel = htons(pt->ws_xpixel);
188 pn->ws_ypixel = htons(pt->ws_ypixel);
193 ic_get_termios(struct termios *pt, net_termios *pn) {
195 pt->c_iflag = ntohl(pn->c_iflag);
196 pt->c_oflag = ntohl(pn->c_oflag);
197 pt->c_cflag = ntohl(pn->c_cflag);
198 pt->c_lflag = ntohl(pn->c_lflag);
200 pt->c_ispeed = ntohl(pn->c_ispeed);
201 pt->c_ospeed = ntohl(pn->c_ospeed);
203 for (i = 0; i < NCCS; ++i) {
204 pt->c_cc[i] = pn->c_cc[i];
210 ic_get_winsz(struct winsize *pt, net_winsz *pn) {
211 pt->ws_row = ntohs(pn->ws_row);
212 pt->ws_col = ntohs(pn->ws_col);
213 pt->ws_xpixel = ntohs(pn->ws_xpixel);
214 pt->ws_ypixel = ntohs(pn->ws_ypixel);
219 /*-------------------------------------------------------------------------
222 *-------------------------------------------------------------------------
225 ic_plog(int lvl, const char *format, ...) {
227 va_start(ap, format);
230 vfprintf(stderr, format, ap);
231 fprintf(stderr, "\n");
235 vsyslog(level[lvl], format, ap);
244 ic_log(const char *format, ...) {
246 va_start(ap, format);
249 vfprintf(stderr, format, ap);
250 fprintf(stderr, "\n");
258 /*-------------------------------------------------------------------------
259 * convert net to host byte order
261 *-------------------------------------------------------------------------
264 ic_recv_ntohs(ic_data *pic) {
271 piph = (struct ip *)p;
272 picmph = (icmp_echo *)(p + (IC_IPHLWRS * 4));
273 pich = (ic_header *)(p + (IC_IPHLWRS * 4) + sizeof(icmp_echo));
275 picmph->id = ntohs(picmph->id);
276 picmph->seq = ntohs(picmph->seq);
277 pich->length = ntohs(pich->length);
283 /*-------------------------------------------------------------------------
284 * select with error check
285 * arg: follow select()'s interface
286 * ret: >=0(success), -1(failure)
287 *-------------------------------------------------------------------------
290 xselect(int nfd, fd_set *rfd, fd_set *wfd, fd_set *ofd, struct timeval *pt) {
294 if ((n = select(nfd, rfd, wfd, ofd, pt)) < 0) {
306 * common select interface for icmsh
309 ic_select(int nfd, long timeout) {
311 struct timeval tv, *ptv;
314 memset(&rfd, 0, sizeof(rfd));
322 if ((n = xselect(nfd + 1, &rfd, NULL, NULL, ptv)) < 0) {
328 if (FD_ISSET(nfd, &rfd))
336 /*-------------------------------------------------------------------------
337 * ic_data common interface
340 *-------------------------------------------------------------------------
343 ic_set_data(ic_data *p, u_char type, u_char flag, u_short length, u_char *buf, int bufsz) {
344 u_char *ppay = (u_char *)&(p->data.payload[0]) + sizeof(ic_header);
345 p->data.ich.type = type;
346 p->data.ich.flag = flag;
347 p->data.ich.length = htons(length);
349 strncpy((char *)ppay, (char *)buf, bufsz);
354 ic_set_header(ic_data *p, u_char type, u_char code, u_short id, u_short seq) {
355 p->icmph.type = type;
356 p->icmph.code = code;
357 p->icmph.id = htons(id);
358 p->icmph.seq = htons(seq);
359 p->icmph.cksum = xchecksum((u_short *)&(p->icmph), IC_DATASIZE);
364 /*-------------------------------------------------------------------------
367 *-------------------------------------------------------------------------
370 ic_kill(char *proc, pid_t p, int sig) {
372 if ((rc = kill(p, sig)) != 0)
373 ic_plog(SINF, "(%s) kill failure (%s)", proc, strerror(errno));
379 ic_waitpid(char *proc, pid_t p, int *status, int options) {
381 if ((wpid = waitpid(p, status, options)) < 0)
382 ic_plog(SINF, "(%s) waitpid failure (%s)", proc, strerror(errno));
388 /*-------------------------------------------------------------------------
389 * read with error check
391 *-------------------------------------------------------------------------
394 xread(int fd, void *buff, size_t size)
405 if ((nread = read(fd, p, left)) < 0) {
411 else if (nread == 0) {
415 p = (char *)p + nread;
418 return (size - left);
422 /*-------------------------------------------------------------------------
423 * write with error check
425 *-------------------------------------------------------------------------
428 xwrite(int fd, void *buff, size_t size)
439 if ((nwrite = write(fd, p, left)) < 0) {
446 p = (char *)p + nwrite;
453 /*-------------------------------------------------------------------------
456 *-------------------------------------------------------------------------
459 vsys_err(int priority, const char *format, ...)
463 va_start(ap, format);
464 vsyslog(priority, format, ap);
472 vsys(int priority, const char *format, ...)
476 va_start(ap, format);
477 vsyslog(priority, format, ap);
485 /*-------------------------------------------------------------------------
488 *-------------------------------------------------------------------------
491 xchecksum(u_short *addr, int len) {
504 sum += *(u_char *) w;
506 sum = (sum & 0xffff) + (sum >> 16);
514 /*-------------------------------------------------------------------------
515 * fork with error check
517 *-------------------------------------------------------------------------
524 if ((pid = fork()) < 0)
531 /*-------------------------------------------------------------------------
532 * malloc family with error check
534 *-------------------------------------------------------------------------
544 if ((tmp = malloc(size)) == NULL)
547 memset(tmp, 0, size);
553 xrealloc(void *src, size_t size)
560 if ((dst = realloc(src, size)) == NULL)
567 /*-------------------------------------------------------------------------
568 * strdup with error check
570 *-------------------------------------------------------------------------
580 if ((tmp = strdup(ptr)) == NULL)