Annotation of ansh/src/utils.c, revision 1.1.1.1.2.12

1.1       misho       1: /*************************************************************************
                      2:  * (C) 2011 AITNET - Sofia/Bulgaria - <office@aitnet.org>
                      3:  *  by Michael Pounov <misho@elwix.org>
                      4:  *
                      5:  * $Author: misho $
1.1.1.1.2.12! misho       6:  * $Id: utils.c,v 1.1.1.1.2.11 2011/10/14 12:03:10 misho Exp $
1.1       misho       7:  *
1.1.1.1.2.8  misho       8:  *************************************************************************
                      9: The ELWIX and AITNET software is distributed under the following
                     10: terms:
                     11: 
                     12: All of the documentation and software included in the ELWIX and AITNET
                     13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
                     14: 
                     15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
                     16:        by Michael Pounov <misho@elwix.org>.  All rights reserved.
                     17: 
                     18: Redistribution and use in source and binary forms, with or without
                     19: modification, are permitted provided that the following conditions
                     20: are met:
                     21: 1. Redistributions of source code must retain the above copyright
                     22:    notice, this list of conditions and the following disclaimer.
                     23: 2. Redistributions in binary form must reproduce the above copyright
                     24:    notice, this list of conditions and the following disclaimer in the
                     25:    documentation and/or other materials provided with the distribution.
                     26: 3. All advertising materials mentioning features or use of this software
                     27:    must display the following acknowledgement:
                     28: This product includes software developed by Michael Pounov <misho@elwix.org>
                     29: ELWIX - Embedded LightWeight unIX and its contributors.
                     30: 4. Neither the name of AITNET nor the names of its contributors
                     31:    may be used to endorse or promote products derived from this software
                     32:    without specific prior written permission.
                     33: 
                     34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
                     35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     44: SUCH DAMAGE.
                     45: */
1.1       misho      46: #include "global.h"
                     47: 
                     48: 
                     49: void
                     50: Get1stEth(char *psDev, int devlen)
                     51: {
                     52:        struct ifaddrs *ifa;
                     53: 
                     54:        assert(psDev);
                     55:        assert(devlen > 0);
                     56: 
                     57:        getifaddrs(&ifa);
                     58:        strlcpy(psDev, ifa->ifa_name, devlen);
                     59:        freeifaddrs(ifa);
                     60: }
                     61: 
                     62: int
                     63: PrepareL2(const char *psDev, int *bpflen)
                     64: {
                     65:        int h, n = 1;
                     66:        register int i;
                     67:        char szStr[STRSIZ];
                     68:        struct ifreq ifr;
1.1.1.1.2.3  misho      69:        struct bpf_program fcode = { 0 };
                     70:        struct bpf_insn insns[] = {
                     71:                BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
                     72:                BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ANSH_ID, 0, 1),
                     73:                BPF_STMT(BPF_RET + BPF_K, -1),
                     74:                BPF_STMT(BPF_RET + BPF_K, 0),
                     75:        };
1.1       misho      76: 
                     77:        FTRACE(3);
                     78:        assert(psDev);
                     79: 
1.1.1.1.2.3  misho      80:        fcode.bf_len = sizeof(insns) / sizeof(struct bpf_insn);
                     81:        fcode.bf_insns = insns;
                     82: 
1.1       misho      83:        for (i = 0; i < 10; i++) {
                     84:                memset(szStr, 0, sizeof szStr);
                     85:                snprintf(szStr, sizeof szStr, "/dev/bpf%d", i);
                     86:                h = open(szStr, O_RDWR);
                     87:                if (h > 2)
                     88:                        break;
                     89:        }
                     90:        if (h < 3) {
                     91:                printf("Error:: open bpf %s #%d - %s\n", szStr, errno, strerror(errno));
                     92:                return -1;
                     93:        }
                     94: 
1.1.1.1.2.5  misho      95:        if (ioctl(h, BIOCIMMEDIATE, &n) == -1) {
1.1.1.1.2.3  misho      96:                printf("Error:: set interface %s to bpf #%d - %s\n", psDev, errno, strerror(errno));
                     97:                close(h);
                     98:                return -1;
                     99:        }
1.1.1.1.2.7  misho     100:        n = USHRT_MAX + 1;
                    101:        if (ioctl(h, BIOCSBLEN, &n) == -1) {
                    102:                printf("Error:: set buffer interface %s buffer length #%d - %s\n", psDev, errno, strerror(errno));
                    103:                close(h);
                    104:                return -1;
                    105:        }
1.1.1.1.2.5  misho     106:        strlcpy(ifr.ifr_name, psDev, sizeof ifr.ifr_name);
                    107:        if (ioctl(h, BIOCSETIF, &ifr) == -1) {
                    108:                printf("Error:: bind interface %s to bpf #%d - %s\n", psDev, errno, strerror(errno));
1.1       misho     109:                close(h);
                    110:                return -1;
                    111:        }
1.1.1.1.2.3  misho     112:        if (ioctl(h, BIOCSETF, &fcode) == -1) {
                    113:                printf("Error:: set filter interface %s to bpf #%d - %s\n", psDev, errno, strerror(errno));
                    114:                close(h);
                    115:                return -1;
                    116:        }
1.1       misho     117:        if (ioctl(h, BIOCGBLEN, bpflen) == -1) {
1.1.1.1.2.7  misho     118:                printf("Error:: get buffer interface %s buffer length #%d - %s\n", psDev, errno, strerror(errno));
1.1       misho     119:                close(h);
                    120:                return -1;
                    121:        }
                    122: 
1.1.1.1.2.3  misho     123:        n = fcntl(h, F_GETFL);
                    124:        fcntl(h, F_SETFL, n | O_NONBLOCK);
                    125: 
1.1       misho     126:        VERB(3) LOG("Openned device handle %d with bpf buflen %d", h, *bpflen);
                    127:        return h;
                    128: }
                    129: 
                    130: int
                    131: PrepareL3(const struct sockaddr *sa, int *bpflen)
                    132: {
                    133:        int h, n = 1;
                    134: 
                    135:        FTRACE(3);
                    136:        assert(sa);
                    137: 
                    138:        h = socket(sa->sa_family, SOCK_RAW, IPPROTO_ICMP);
                    139:        if (h == -1) {
                    140:                printf("Error:: Cant open raw socket #%d - %s\n", errno, strerror(errno));
                    141:                return -1;
                    142:        }
                    143:        /*
                    144:        if (setsockopt(h, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
                    145:                printf("Error:: Cant set raw socket #%d - %s\n", errno, strerror(errno));
                    146:                close(h);
                    147:                return -1;
                    148:        }
                    149:        */
                    150:        if (bind(h, sa, sizeof(struct sockaddr)) == -1) {
                    151:                printf("Error:: Cant bind to raw socket #%d - %s\n", errno, strerror(errno));
                    152:                close(h);
                    153:                return -1;
                    154:        }
                    155: 
                    156:        n = fcntl(h, F_GETFL);
                    157:        fcntl(h, F_SETFL, n | O_NONBLOCK);
                    158: 
                    159:        *bpflen = USHRT_MAX;
                    160:        VERB(3) LOG("Openned socket handle %d", h);
                    161:        return h;
                    162: }
                    163: 
                    164: char
1.1.1.1.2.6  misho     165: icmpRecv(int s, u_int * __restrict seq, u_short * __restrict id, u_int * __restrict crypted, 
                    166:                u_char * __restrict data, int * __restrict datlen, struct sockaddr *sa, socklen_t *salen)
1.1       misho     167: {
                    168:        int ret = 0;
                    169:        struct icmp *icmp;
                    170:        struct ansh_hdr *hdr;
                    171:        u_char buf[USHRT_MAX] = { 0 };
                    172:        u_int crc;
                    173: 
                    174:        ret = recvfrom(s, buf, sizeof buf, 0, sa, salen);
                    175:        if (ret == -1) {
                    176:                ERR("Receive recvfrom() #%d - %s", errno, strerror(errno));
                    177:                return ANSH_FLG_ERR;
                    178:        } else
                    179:                VERB(4) LOG("Get packet with len=%d", ret);
                    180: 
                    181:        /* check header len */
                    182:        if (ret < (sizeof(struct ip) + sizeof(struct icmp) + sizeof(struct ansh_hdr))) {
                    183:                VERB(1) LOG("Discard packet too short %d ...", ret);
                    184:                return ANSH_FLG_ERR;
                    185:        } else
                    186:                icmp = (struct icmp*) (buf + sizeof(struct ip));
                    187: 
                    188:        /* check echo magic ansh code */
                    189:        if (icmp->icmp_type != ICMP_ECHOREPLY || icmp->icmp_code != ANSH_CODE) {
                    190:                VERB(3) LOG("Packet isnt for me %d ... icmp_code=%d", ret, icmp->icmp_code);
                    191:                return ANSH_FLG_ERR;
                    192:        } else
                    193:                hdr = (struct ansh_hdr*) (buf + sizeof(struct ip) + sizeof(struct icmp));
                    194: 
                    195:        /* check version and total size of packet */
                    196:        if (hdr->ansh_ver != ANSH_VERSION) {
                    197:                VERB(3) LOG("Packet with wrong version ...");
                    198:                return ANSH_FLG_ERR;
                    199:        }
1.1.1.1.2.1  misho     200:        if (crypted) {
                    201:                if (hdr->ansh_nonce && !*crypted) {
                    202:                        VERB(3) LOG("Channel INSECURED:: Crypted communication not supported at this moment ...");
                    203:                        return ANSH_FLG_ERR;
                    204:                }
                    205:                if (!hdr->ansh_nonce && *crypted) {
                    206:                        VERB(3) LOG("Channel SECURED:: Plain text communication not supported at this moment ...");
                    207:                        return ANSH_FLG_ERR;
                    208:                }
                    209: 
                    210:                *crypted = ntohl(hdr->ansh_nonce);
                    211:        }
1.1       misho     212: 
                    213:        /* check crc of packet */
                    214:        crc = hdr->ansh_crc;
                    215:        hdr->ansh_crc ^= hdr->ansh_crc;
                    216:        hdr->ansh_crc = htonl(crcAdler((u_char*) hdr, ntohs(hdr->ansh_len)));
                    217:        if (crc != hdr->ansh_crc) {
                    218:                VERB(3) LOG("Packet with wrong crc ...");
                    219:                return ANSH_FLG_ERR;
                    220:        }
                    221: 
                    222:        /* copy data */
                    223:        if (data && datlen) {
                    224:                memset(data, 0, *datlen);
                    225:                *datlen = ntohs(hdr->ansh_len) - sizeof(struct ansh_hdr);
                    226:                memcpy(data, buf + sizeof(struct ip) + sizeof(struct icmp) + sizeof(struct ansh_hdr), *datlen);
                    227:        }
                    228: 
1.1.1.1.2.6  misho     229:        if (seq)
                    230:                *seq = ntohl(hdr->ansh_seq);
1.1       misho     231:        if (id)
                    232:                *id = ntohs(icmp->icmp_id);
                    233:        return hdr->ansh_flg;
                    234: }
                    235: 
                    236: int
1.1.1.1.2.6  misho     237: icmpSend(int s, u_int seq, u_short id, char flg, u_int crypted, u_char *data, int datlen, 
                    238:                struct sockaddr *sa, socklen_t salen)
1.1       misho     239: {
                    240:        u_char *pos, buf[USHRT_MAX] = { 0 };
                    241:        struct icmp *icmp;
                    242:        struct ansh_hdr *hdr;
                    243:        int ret = 0;
                    244: 
                    245:        assert(data);
                    246:        if ((sizeof buf - sizeof(struct icmp) + sizeof(struct ansh_hdr)) < datlen)
                    247:                return ANSH_FLG_ERR;
                    248: 
                    249:        icmp = (struct icmp*) buf;
                    250:        hdr = (struct ansh_hdr*) (buf + sizeof(struct icmp));
                    251:        pos = buf + sizeof(struct icmp) + sizeof(struct ansh_hdr);
                    252: 
                    253:        memcpy(pos, data, datlen);
                    254: 
                    255:        hdr->ansh_ver = ANSH_VERSION;
                    256:        hdr->ansh_flg = flg;
                    257:        hdr->ansh_len = htons(datlen + sizeof(struct ansh_hdr));
1.1.1.1.2.1  misho     258:        hdr->ansh_nonce = htonl(crypted);
1.1.1.1.2.6  misho     259:        hdr->ansh_seq = htonl(seq);
1.1       misho     260:        hdr->ansh_crc = 0;
                    261:        hdr->ansh_crc = htonl(crcAdler((u_char*) hdr, ntohs(hdr->ansh_len)));
                    262: 
                    263:        icmp->icmp_type = ICMP_ECHOREPLY;
                    264:        icmp->icmp_code = ANSH_CODE;
                    265:        icmp->icmp_id = htons(id);
                    266:        icmp->icmp_seq = htons(datlen);
                    267:        icmp->icmp_cksum = 0;
                    268:        icmp->icmp_cksum = crcIP(buf, sizeof(struct icmp) + sizeof(struct ansh_hdr) + datlen);
                    269: 
                    270:        if ((ret = sendto(s, buf, sizeof(struct icmp) + sizeof(struct ansh_hdr) + datlen, 
                    271:                                        0, sa, salen)) == -1) {
                    272:                ERR("Send sendto() #%d - %s", errno, strerror(errno));
                    273:                return ANSH_FLG_ERR;
                    274:        } else
                    275:                VERB(4) LOG("Put packet with len=%d", ret);
                    276:        if (ret != sizeof(struct icmp) + sizeof(struct ansh_hdr) + datlen) {
                    277:                VERB(3) LOG("Sended data %d is different from source data len %d", ret, 
1.1.1.1.2.9  misho     278:                                (int) (sizeof(struct icmp) + sizeof(struct ansh_hdr) + datlen));
1.1       misho     279:                return ANSH_FLG_ERR;
                    280:        }
                    281: 
                    282:        return ret;
                    283: }
                    284: 
1.1.1.1.2.5  misho     285: static int
1.1.1.1.2.9  misho     286: _pkt_Send(int s, u_int seq, char flg, u_int crypted, u_char *data, int datlen, struct io_ether_addr *ea)
1.1       misho     287: {
                    288:        u_char *pos, buf[USHRT_MAX] = { 0 };
                    289:        struct ether_header *e = (struct ether_header*) buf;
                    290:        struct ansh_hdr *hdr;
                    291:        int ret = 0;
                    292: 
                    293:        assert(data);
                    294:        if ((sizeof buf - ETHER_HDR_LEN + sizeof(struct ansh_hdr)) < datlen)
                    295:                return ANSH_FLG_ERR;
                    296: 
1.1.1.1.2.3  misho     297:        e->ether_type = ntohs(ANSH_ID);
1.1.1.1.2.9  misho     298:        memcpy(e->ether_dhost, ea->ether_addr_octet, ETHER_ADDR_LEN);
1.1       misho     299:        hdr = (struct ansh_hdr*) (buf + ETHER_HDR_LEN);
                    300:        pos = ((u_char*) hdr) + sizeof(struct ansh_hdr);
                    301: 
                    302:        memcpy(pos, data, datlen);
                    303: 
                    304:        hdr->ansh_ver = ANSH_VERSION;
                    305:        hdr->ansh_flg = flg;
                    306:        hdr->ansh_len = htons(datlen + sizeof(struct ansh_hdr));
1.1.1.1.2.2  misho     307:        hdr->ansh_nonce = htonl(crypted);
1.1.1.1.2.6  misho     308:        hdr->ansh_seq = htonl(seq);
1.1       misho     309:        hdr->ansh_crc = 0;
                    310:        hdr->ansh_crc = htonl(crcAdler((u_char*) hdr, ntohs(hdr->ansh_len)));
                    311: 
                    312:        if ((ret = write(s, buf, ETHER_HDR_LEN + sizeof(struct ansh_hdr) + datlen)) == -1) {
                    313:                ERR("Send packet() #%d - %s", errno, strerror(errno));
                    314:                return ANSH_FLG_ERR;
                    315:        } else
                    316:                VERB(4) LOG("Put packet with len=%d", ret);
                    317:        if (ret != ETHER_HDR_LEN + sizeof(struct ansh_hdr) + datlen) {
                    318:                VERB(3) LOG("Sended data %d is different from source data len %d", ret, 
1.1.1.1.2.9  misho     319:                                (int) (ETHER_HDR_LEN + sizeof(struct ansh_hdr) + datlen));
1.1       misho     320:                return ANSH_FLG_ERR;
                    321:        }
                    322: 
                    323:        return ret;
                    324: }
                    325: 
1.1.1.1.2.5  misho     326: int
1.1.1.1.2.9  misho     327: pktSend(int s, u_int seq, char flg, u_int crypted, u_char *data, int datlen, struct io_ether_addr *ea)
1.1       misho     328: {
1.1.1.1.2.5  misho     329:        int wlen, ret = 0;
                    330:        u_char *pos = data;
1.1       misho     331: 
1.1.1.1.2.5  misho     332:        while (datlen > -1) {
1.1.1.1.2.6  misho     333:                wlen = _pkt_Send(s, seq, flg, crypted, pos, (datlen > 512) ? 512 : datlen, ea);
1.1.1.1.2.5  misho     334:                if (wlen == -1)
                    335:                        return -1;
                    336:                else {
                    337:                        pos += wlen;
                    338:                        datlen -= wlen;
                    339:                        ret += wlen;
                    340:                }
1.1       misho     341:        }
                    342: 
1.1.1.1.2.5  misho     343:        return ret;
                    344: }
1.1       misho     345: 
1.1.1.1.2.5  misho     346: static char
1.1.1.1.2.6  misho     347: _pkt_Recv(u_char * __restrict buf, int rlen, u_int * __restrict seq, u_int * __restrict crypted, 
1.1.1.1.2.5  misho     348:                u_char * __restrict data, int * __restrict datlen, 
                    349:                u_char ** __restrict next, int * __restrict nextlen)
                    350: {
                    351:        int bias;
                    352:        struct bpf_hdr *bpf;
                    353:        struct ansh_hdr *hdr;
                    354:        u_int crc;
                    355: 
                    356:        if (rlen < (sizeof(struct bpf_hdr) + ETHER_HDR_LEN + sizeof(struct ansh_hdr))) {
                    357:                VERB(1) LOG("Discard packet too short %d ...", rlen);
1.1       misho     358:                return ANSH_FLG_ERR;
                    359:        } else {
                    360:                bpf = (struct bpf_hdr*) buf;
                    361:                hdr = (struct ansh_hdr*) (buf + bpf->bh_hdrlen + ETHER_HDR_LEN);
                    362:        }
                    363: 
1.1.1.1.2.5  misho     364:        /* slice readed data to packets */
                    365:        if ((bias = BPF_WORDALIGN(bpf->bh_hdrlen + bpf->bh_caplen)) < rlen) {
                    366:                *next = buf + bias;
                    367:                *nextlen = rlen - bias;
                    368:        } else {
                    369:                *next = NULL;
                    370:                *nextlen = 0;
                    371:        }
                    372: 
1.1       misho     373:        /* check version and total size of packet */
                    374:        if (hdr->ansh_ver != ANSH_VERSION) {
                    375:                VERB(3) LOG("Packet with wrong version ... %d", hdr->ansh_ver);
                    376:                return ANSH_FLG_ERR;
                    377:        }
1.1.1.1.2.2  misho     378:        if (crypted) {
                    379:                if (hdr->ansh_nonce && !*crypted) {
                    380:                        VERB(3) LOG("Channel INSECURED:: Crypted communication not supported at this moment ...");
                    381:                        return ANSH_FLG_ERR;
                    382:                }
                    383:                if (!hdr->ansh_nonce && *crypted) {
                    384:                        VERB(3) LOG("Channel SECURED:: Plain text communication not supported at this moment ...");
                    385:                        return ANSH_FLG_ERR;
                    386:                }
                    387: 
                    388:                *crypted = ntohl(hdr->ansh_nonce);
                    389:        }
                    390: 
1.1       misho     391:        /* check crc of packet */
                    392:        crc = hdr->ansh_crc;
                    393:        hdr->ansh_crc ^= hdr->ansh_crc;
                    394:        hdr->ansh_crc = htonl(crcAdler((u_char*) hdr, ntohs(hdr->ansh_len)));
                    395:        if (crc != hdr->ansh_crc) {
                    396:                VERB(3) LOG("Packet with wrong crc ...");
                    397:                return ANSH_FLG_ERR;
                    398:        }
                    399: 
1.1.1.1.2.5  misho     400:        /* select data */
1.1       misho     401:        if (data) {
                    402:                *datlen = ntohs(hdr->ansh_len) - sizeof(struct ansh_hdr);
1.1.1.1.2.4  misho     403:                memcpy(data, buf + bpf->bh_hdrlen + ETHER_HDR_LEN + sizeof(struct ansh_hdr), *datlen);
1.1       misho     404:        }
                    405: 
1.1.1.1.2.6  misho     406:        if (seq)
                    407:                *seq = ntohl(hdr->ansh_seq);
1.1.1.1.2.5  misho     408:        return hdr->ansh_flg;
                    409: }
                    410: 
                    411: char
1.1.1.1.2.6  misho     412: pktRecv(int s, u_int * __restrict seq, u_int * __restrict crypted, u_char * __restrict data, 
                    413:                int * __restrict datlen, struct ether_header *eth)
1.1.1.1.2.5  misho     414: {
1.1.1.1.2.6  misho     415:        u_char *buf, *next, *ptr, *pos = data;
1.1.1.1.2.5  misho     416:        int nextlen, rlen, buflen, ptrlen;
                    417:        char flg;
                    418:        struct bpf_hdr *bpf;
                    419:        struct ether_header *e;
                    420: 
1.1.1.1.2.6  misho     421:        if (!eth || !data || !datlen)
1.1.1.1.2.5  misho     422:                return ANSH_FLG_ERR;
1.1.1.1.2.6  misho     423:        else
                    424:                memset(data, 0, *datlen);
1.1.1.1.2.5  misho     425: 
                    426:        if (!(buf = malloc(*datlen))) {
                    427:                ERR("malloc() #%d - %s", errno, strerror(errno));
                    428:                return ANSH_FLG_ERR;
                    429:        }
                    430: 
                    431:        rlen = read(s, buf, *datlen);
                    432:        if (rlen == -1) {
                    433:                ERR("Receive packet() #%d - %s", errno, strerror(errno));
                    434:                free(buf);
                    435:                return ANSH_FLG_ERR;
                    436:        } else
                    437:                VERB(4) LOG("Get packet with len=%d", rlen);
                    438: 
                    439:        /* check header len */
                    440:        if (rlen < (sizeof(struct bpf_hdr) + ETHER_HDR_LEN + sizeof(struct ansh_hdr))) {
                    441:                VERB(1) LOG("Discard packet too short %d ...", rlen);
                    442:                free(buf);
                    443:                return ANSH_FLG_ERR;
                    444:        } else {
                    445:                bpf = (struct bpf_hdr*) buf;
                    446:                e = (struct ether_header*) (buf + bpf->bh_hdrlen);
                    447:                memcpy(eth, e, ETHER_HDR_LEN);
                    448:        }
                    449: 
                    450:        ptr = next = buf;
                    451:        ptrlen = nextlen = rlen;
1.1.1.1.2.6  misho     452:        if ((flg = _pkt_Recv(ptr, ptrlen, seq, crypted, pos, &buflen, &next, &nextlen)) == -1) {
1.1.1.1.2.5  misho     453:                free(buf);
                    454:                return ANSH_FLG_ERR;
                    455:        } else {
                    456:                pos += buflen;
                    457:                *datlen = buflen;
                    458:                ptr = next;
                    459:                ptrlen = nextlen;
                    460:        }
1.1.1.1.2.6  misho     461:        /* get additional packets from buffer */
1.1.1.1.2.5  misho     462:        while (next && nextlen > 0)
1.1.1.1.2.6  misho     463:                if (_pkt_Recv(ptr, ptrlen, seq, crypted, pos, &buflen, &next, &nextlen) == -1)
1.1.1.1.2.5  misho     464:                        break;
                    465:                else {
                    466:                        pos += buflen;
                    467:                        *datlen += buflen;
                    468:                        ptr = next;
                    469:                        ptrlen = nextlen;
                    470:                }
                    471: 
1.1       misho     472:        free(buf);
1.1.1.1.2.6  misho     473: 
1.1.1.1.2.5  misho     474:        return flg;
1.1       misho     475: }
                    476: 
                    477: void *
                    478: TOfunc(sched_task_t *task)
                    479: {
                    480:        struct tagProc *proc;
                    481: 
                    482:        FTRACE(3);
                    483: 
                    484:        /* not found argument, drop data */
                    485:        if (!(proc = TASK_ARG(task)))
                    486:                return (void*) -1;
                    487: 
                    488:        if (proc->proc_pid)
                    489:                kill(proc->proc_pid, SIGTERM);
                    490: 
                    491:        return NULL;
                    492: }
                    493: 
1.1.1.1.2.1  misho     494: u_char *
                    495: cryptBuffer(u_char *buf, int rlen, u_int ctr)
                    496: {
                    497:        u_char *str, ivec[AES_BLOCK_SIZE] = { 0 };
                    498:        u_int rctr = htonl(ctr);
                    499: 
                    500:        FTRACE(3);
                    501: 
                    502:        if (!buf)
                    503:                return NULL;
                    504: 
                    505:        memcpy(ivec, &ctr, sizeof ctr);
                    506:        memcpy(ivec + 4, &rctr, sizeof rctr);
                    507:        memcpy(ivec + 8, &ctr, sizeof ctr);
                    508:        memcpy(ivec + 12, &rctr, sizeof rctr);
                    509: 
1.1.1.1.2.10  misho     510:        if (io_ctr_AES(buf, rlen, &str, (u_char*) Key, ivec) == -1)
1.1.1.1.2.1  misho     511:                return NULL;
                    512: 
                    513:        return str;
                    514: }
1.1.1.1.2.11  misho     515: 
                    516: int
                    517: stopProcess(sched_root_task_t * __restrict root, proc_head_t * __restrict h, pid_t pid, sched_task_func_t func)
                    518: {
                    519:        struct tagProc *p;
                    520: 
                    521:        FTRACE(3);
                    522: 
                    523:        SLIST_FOREACH(p, h, proc_next)
                    524:                if (p->proc_pid == pid) {
                    525:                        break;
                    526:                }
1.1.1.1.2.12! misho     527:        VERB(3) LOG("pid=%d found=%p\n", pid, p);
1.1.1.1.2.11  misho     528:        if (!p)
                    529:                return 1;
                    530: 
                    531:        ioFreePTY(p->proc_pty, p->proc_ttyname);
1.1.1.1.2.12! misho     532:        if (p->proc_pty)
        !           533:                schedCancelby(root, NULL, CRITERIA_FD, (void*) ((intptr_t) p->proc_pty), NULL);
1.1.1.1.2.11  misho     534: 
1.1.1.1.2.12! misho     535:        p->proc_pty = 0;
1.1.1.1.2.11  misho     536:        p->proc_pid = 0;
                    537:        p->proc_seq = 0;
                    538:        p->proc_flg = ANSH_FLG_EOF;
                    539:        p->proc_rlen_[FD2NET] = 0;
                    540: 
                    541:        schedCallOnce(root, func, p, p->proc_sock);
                    542:        return 0;
                    543: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>