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

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.13! misho       6:  * $Id: utils.c,v 1.1.1.1.2.12 2011/10/14 12:45:09 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:                }
1.1.1.1.2.13! misho     209:                if (ntohl(hdr->ansh_nonce) != *crypted)
        !           210:                        VERB(4) LOG("Detect change of nonce from %x to %x", *crypted, ntohl(hdr->ansh_nonce));
1.1.1.1.2.1  misho     211: 
                    212:                *crypted = ntohl(hdr->ansh_nonce);
                    213:        }
1.1       misho     214: 
                    215:        /* check crc of packet */
                    216:        crc = hdr->ansh_crc;
                    217:        hdr->ansh_crc ^= hdr->ansh_crc;
                    218:        hdr->ansh_crc = htonl(crcAdler((u_char*) hdr, ntohs(hdr->ansh_len)));
                    219:        if (crc != hdr->ansh_crc) {
                    220:                VERB(3) LOG("Packet with wrong crc ...");
                    221:                return ANSH_FLG_ERR;
                    222:        }
                    223: 
                    224:        /* copy data */
                    225:        if (data && datlen) {
                    226:                memset(data, 0, *datlen);
                    227:                *datlen = ntohs(hdr->ansh_len) - sizeof(struct ansh_hdr);
                    228:                memcpy(data, buf + sizeof(struct ip) + sizeof(struct icmp) + sizeof(struct ansh_hdr), *datlen);
                    229:        }
                    230: 
1.1.1.1.2.6  misho     231:        if (seq)
                    232:                *seq = ntohl(hdr->ansh_seq);
1.1       misho     233:        if (id)
                    234:                *id = ntohs(icmp->icmp_id);
                    235:        return hdr->ansh_flg;
                    236: }
                    237: 
                    238: int
1.1.1.1.2.6  misho     239: icmpSend(int s, u_int seq, u_short id, char flg, u_int crypted, u_char *data, int datlen, 
                    240:                struct sockaddr *sa, socklen_t salen)
1.1       misho     241: {
                    242:        u_char *pos, buf[USHRT_MAX] = { 0 };
                    243:        struct icmp *icmp;
                    244:        struct ansh_hdr *hdr;
                    245:        int ret = 0;
                    246: 
                    247:        assert(data);
                    248:        if ((sizeof buf - sizeof(struct icmp) + sizeof(struct ansh_hdr)) < datlen)
                    249:                return ANSH_FLG_ERR;
                    250: 
                    251:        icmp = (struct icmp*) buf;
                    252:        hdr = (struct ansh_hdr*) (buf + sizeof(struct icmp));
                    253:        pos = buf + sizeof(struct icmp) + sizeof(struct ansh_hdr);
                    254: 
                    255:        memcpy(pos, data, datlen);
                    256: 
                    257:        hdr->ansh_ver = ANSH_VERSION;
                    258:        hdr->ansh_flg = flg;
                    259:        hdr->ansh_len = htons(datlen + sizeof(struct ansh_hdr));
1.1.1.1.2.1  misho     260:        hdr->ansh_nonce = htonl(crypted);
1.1.1.1.2.6  misho     261:        hdr->ansh_seq = htonl(seq);
1.1       misho     262:        hdr->ansh_crc = 0;
                    263:        hdr->ansh_crc = htonl(crcAdler((u_char*) hdr, ntohs(hdr->ansh_len)));
                    264: 
                    265:        icmp->icmp_type = ICMP_ECHOREPLY;
                    266:        icmp->icmp_code = ANSH_CODE;
                    267:        icmp->icmp_id = htons(id);
                    268:        icmp->icmp_seq = htons(datlen);
                    269:        icmp->icmp_cksum = 0;
                    270:        icmp->icmp_cksum = crcIP(buf, sizeof(struct icmp) + sizeof(struct ansh_hdr) + datlen);
                    271: 
                    272:        if ((ret = sendto(s, buf, sizeof(struct icmp) + sizeof(struct ansh_hdr) + datlen, 
                    273:                                        0, sa, salen)) == -1) {
                    274:                ERR("Send sendto() #%d - %s", errno, strerror(errno));
                    275:                return ANSH_FLG_ERR;
                    276:        } else
                    277:                VERB(4) LOG("Put packet with len=%d", ret);
                    278:        if (ret != sizeof(struct icmp) + sizeof(struct ansh_hdr) + datlen) {
                    279:                VERB(3) LOG("Sended data %d is different from source data len %d", ret, 
1.1.1.1.2.9  misho     280:                                (int) (sizeof(struct icmp) + sizeof(struct ansh_hdr) + datlen));
1.1       misho     281:                return ANSH_FLG_ERR;
                    282:        }
                    283: 
                    284:        return ret;
                    285: }
                    286: 
1.1.1.1.2.5  misho     287: static int
1.1.1.1.2.9  misho     288: _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     289: {
                    290:        u_char *pos, buf[USHRT_MAX] = { 0 };
                    291:        struct ether_header *e = (struct ether_header*) buf;
                    292:        struct ansh_hdr *hdr;
                    293:        int ret = 0;
                    294: 
                    295:        assert(data);
                    296:        if ((sizeof buf - ETHER_HDR_LEN + sizeof(struct ansh_hdr)) < datlen)
                    297:                return ANSH_FLG_ERR;
                    298: 
1.1.1.1.2.3  misho     299:        e->ether_type = ntohs(ANSH_ID);
1.1.1.1.2.9  misho     300:        memcpy(e->ether_dhost, ea->ether_addr_octet, ETHER_ADDR_LEN);
1.1       misho     301:        hdr = (struct ansh_hdr*) (buf + ETHER_HDR_LEN);
                    302:        pos = ((u_char*) hdr) + sizeof(struct ansh_hdr);
                    303: 
                    304:        memcpy(pos, data, datlen);
                    305: 
                    306:        hdr->ansh_ver = ANSH_VERSION;
                    307:        hdr->ansh_flg = flg;
                    308:        hdr->ansh_len = htons(datlen + sizeof(struct ansh_hdr));
1.1.1.1.2.2  misho     309:        hdr->ansh_nonce = htonl(crypted);
1.1.1.1.2.6  misho     310:        hdr->ansh_seq = htonl(seq);
1.1       misho     311:        hdr->ansh_crc = 0;
                    312:        hdr->ansh_crc = htonl(crcAdler((u_char*) hdr, ntohs(hdr->ansh_len)));
                    313: 
                    314:        if ((ret = write(s, buf, ETHER_HDR_LEN + sizeof(struct ansh_hdr) + datlen)) == -1) {
                    315:                ERR("Send packet() #%d - %s", errno, strerror(errno));
                    316:                return ANSH_FLG_ERR;
                    317:        } else
                    318:                VERB(4) LOG("Put packet with len=%d", ret);
                    319:        if (ret != ETHER_HDR_LEN + sizeof(struct ansh_hdr) + datlen) {
                    320:                VERB(3) LOG("Sended data %d is different from source data len %d", ret, 
1.1.1.1.2.9  misho     321:                                (int) (ETHER_HDR_LEN + sizeof(struct ansh_hdr) + datlen));
1.1       misho     322:                return ANSH_FLG_ERR;
                    323:        }
                    324: 
                    325:        return ret;
                    326: }
                    327: 
1.1.1.1.2.5  misho     328: int
1.1.1.1.2.9  misho     329: pktSend(int s, u_int seq, char flg, u_int crypted, u_char *data, int datlen, struct io_ether_addr *ea)
1.1       misho     330: {
1.1.1.1.2.5  misho     331:        int wlen, ret = 0;
                    332:        u_char *pos = data;
1.1       misho     333: 
1.1.1.1.2.5  misho     334:        while (datlen > -1) {
1.1.1.1.2.6  misho     335:                wlen = _pkt_Send(s, seq, flg, crypted, pos, (datlen > 512) ? 512 : datlen, ea);
1.1.1.1.2.5  misho     336:                if (wlen == -1)
                    337:                        return -1;
                    338:                else {
                    339:                        pos += wlen;
                    340:                        datlen -= wlen;
                    341:                        ret += wlen;
                    342:                }
1.1       misho     343:        }
                    344: 
1.1.1.1.2.5  misho     345:        return ret;
                    346: }
1.1       misho     347: 
1.1.1.1.2.5  misho     348: static char
1.1.1.1.2.6  misho     349: _pkt_Recv(u_char * __restrict buf, int rlen, u_int * __restrict seq, u_int * __restrict crypted, 
1.1.1.1.2.5  misho     350:                u_char * __restrict data, int * __restrict datlen, 
                    351:                u_char ** __restrict next, int * __restrict nextlen)
                    352: {
                    353:        int bias;
                    354:        struct bpf_hdr *bpf;
                    355:        struct ansh_hdr *hdr;
                    356:        u_int crc;
                    357: 
                    358:        if (rlen < (sizeof(struct bpf_hdr) + ETHER_HDR_LEN + sizeof(struct ansh_hdr))) {
                    359:                VERB(1) LOG("Discard packet too short %d ...", rlen);
1.1       misho     360:                return ANSH_FLG_ERR;
                    361:        } else {
                    362:                bpf = (struct bpf_hdr*) buf;
                    363:                hdr = (struct ansh_hdr*) (buf + bpf->bh_hdrlen + ETHER_HDR_LEN);
                    364:        }
                    365: 
1.1.1.1.2.5  misho     366:        /* slice readed data to packets */
                    367:        if ((bias = BPF_WORDALIGN(bpf->bh_hdrlen + bpf->bh_caplen)) < rlen) {
                    368:                *next = buf + bias;
                    369:                *nextlen = rlen - bias;
                    370:        } else {
                    371:                *next = NULL;
                    372:                *nextlen = 0;
                    373:        }
                    374: 
1.1       misho     375:        /* check version and total size of packet */
                    376:        if (hdr->ansh_ver != ANSH_VERSION) {
                    377:                VERB(3) LOG("Packet with wrong version ... %d", hdr->ansh_ver);
                    378:                return ANSH_FLG_ERR;
                    379:        }
1.1.1.1.2.2  misho     380:        if (crypted) {
                    381:                if (hdr->ansh_nonce && !*crypted) {
                    382:                        VERB(3) LOG("Channel INSECURED:: Crypted communication not supported at this moment ...");
                    383:                        return ANSH_FLG_ERR;
                    384:                }
                    385:                if (!hdr->ansh_nonce && *crypted) {
                    386:                        VERB(3) LOG("Channel SECURED:: Plain text communication not supported at this moment ...");
                    387:                        return ANSH_FLG_ERR;
                    388:                }
1.1.1.1.2.13! misho     389:                if (ntohl(hdr->ansh_nonce) != *crypted)
        !           390:                        VERB(4) LOG("Detect change of nonce from %x to %x", *crypted, ntohl(hdr->ansh_nonce));
1.1.1.1.2.2  misho     391: 
                    392:                *crypted = ntohl(hdr->ansh_nonce);
                    393:        }
                    394: 
1.1       misho     395:        /* check crc of packet */
                    396:        crc = hdr->ansh_crc;
                    397:        hdr->ansh_crc ^= hdr->ansh_crc;
                    398:        hdr->ansh_crc = htonl(crcAdler((u_char*) hdr, ntohs(hdr->ansh_len)));
                    399:        if (crc != hdr->ansh_crc) {
                    400:                VERB(3) LOG("Packet with wrong crc ...");
                    401:                return ANSH_FLG_ERR;
                    402:        }
                    403: 
1.1.1.1.2.5  misho     404:        /* select data */
1.1       misho     405:        if (data) {
                    406:                *datlen = ntohs(hdr->ansh_len) - sizeof(struct ansh_hdr);
1.1.1.1.2.4  misho     407:                memcpy(data, buf + bpf->bh_hdrlen + ETHER_HDR_LEN + sizeof(struct ansh_hdr), *datlen);
1.1       misho     408:        }
                    409: 
1.1.1.1.2.6  misho     410:        if (seq)
                    411:                *seq = ntohl(hdr->ansh_seq);
1.1.1.1.2.5  misho     412:        return hdr->ansh_flg;
                    413: }
                    414: 
                    415: char
1.1.1.1.2.6  misho     416: pktRecv(int s, u_int * __restrict seq, u_int * __restrict crypted, u_char * __restrict data, 
                    417:                int * __restrict datlen, struct ether_header *eth)
1.1.1.1.2.5  misho     418: {
1.1.1.1.2.6  misho     419:        u_char *buf, *next, *ptr, *pos = data;
1.1.1.1.2.5  misho     420:        int nextlen, rlen, buflen, ptrlen;
                    421:        char flg;
                    422:        struct bpf_hdr *bpf;
                    423:        struct ether_header *e;
                    424: 
1.1.1.1.2.6  misho     425:        if (!eth || !data || !datlen)
1.1.1.1.2.5  misho     426:                return ANSH_FLG_ERR;
1.1.1.1.2.6  misho     427:        else
                    428:                memset(data, 0, *datlen);
1.1.1.1.2.5  misho     429: 
                    430:        if (!(buf = malloc(*datlen))) {
                    431:                ERR("malloc() #%d - %s", errno, strerror(errno));
                    432:                return ANSH_FLG_ERR;
                    433:        }
                    434: 
                    435:        rlen = read(s, buf, *datlen);
                    436:        if (rlen == -1) {
                    437:                ERR("Receive packet() #%d - %s", errno, strerror(errno));
                    438:                free(buf);
                    439:                return ANSH_FLG_ERR;
                    440:        } else
                    441:                VERB(4) LOG("Get packet with len=%d", rlen);
                    442: 
                    443:        /* check header len */
                    444:        if (rlen < (sizeof(struct bpf_hdr) + ETHER_HDR_LEN + sizeof(struct ansh_hdr))) {
                    445:                VERB(1) LOG("Discard packet too short %d ...", rlen);
                    446:                free(buf);
                    447:                return ANSH_FLG_ERR;
                    448:        } else {
                    449:                bpf = (struct bpf_hdr*) buf;
                    450:                e = (struct ether_header*) (buf + bpf->bh_hdrlen);
                    451:                memcpy(eth, e, ETHER_HDR_LEN);
                    452:        }
                    453: 
                    454:        ptr = next = buf;
                    455:        ptrlen = nextlen = rlen;
1.1.1.1.2.6  misho     456:        if ((flg = _pkt_Recv(ptr, ptrlen, seq, crypted, pos, &buflen, &next, &nextlen)) == -1) {
1.1.1.1.2.5  misho     457:                free(buf);
                    458:                return ANSH_FLG_ERR;
                    459:        } else {
                    460:                pos += buflen;
                    461:                *datlen = buflen;
                    462:                ptr = next;
                    463:                ptrlen = nextlen;
                    464:        }
1.1.1.1.2.6  misho     465:        /* get additional packets from buffer */
1.1.1.1.2.5  misho     466:        while (next && nextlen > 0)
1.1.1.1.2.6  misho     467:                if (_pkt_Recv(ptr, ptrlen, seq, crypted, pos, &buflen, &next, &nextlen) == -1)
1.1.1.1.2.5  misho     468:                        break;
                    469:                else {
                    470:                        pos += buflen;
                    471:                        *datlen += buflen;
                    472:                        ptr = next;
                    473:                        ptrlen = nextlen;
                    474:                }
                    475: 
1.1       misho     476:        free(buf);
1.1.1.1.2.6  misho     477: 
1.1.1.1.2.5  misho     478:        return flg;
1.1       misho     479: }
                    480: 
                    481: void *
                    482: TOfunc(sched_task_t *task)
                    483: {
                    484:        struct tagProc *proc;
                    485: 
                    486:        FTRACE(3);
                    487: 
                    488:        /* not found argument, drop data */
                    489:        if (!(proc = TASK_ARG(task)))
                    490:                return (void*) -1;
                    491: 
                    492:        if (proc->proc_pid)
                    493:                kill(proc->proc_pid, SIGTERM);
                    494: 
                    495:        return NULL;
                    496: }
                    497: 
1.1.1.1.2.1  misho     498: u_char *
                    499: cryptBuffer(u_char *buf, int rlen, u_int ctr)
                    500: {
                    501:        u_char *str, ivec[AES_BLOCK_SIZE] = { 0 };
                    502:        u_int rctr = htonl(ctr);
                    503: 
                    504:        FTRACE(3);
                    505: 
                    506:        if (!buf)
                    507:                return NULL;
                    508: 
                    509:        memcpy(ivec, &ctr, sizeof ctr);
                    510:        memcpy(ivec + 4, &rctr, sizeof rctr);
                    511:        memcpy(ivec + 8, &ctr, sizeof ctr);
                    512:        memcpy(ivec + 12, &rctr, sizeof rctr);
                    513: 
1.1.1.1.2.10  misho     514:        if (io_ctr_AES(buf, rlen, &str, (u_char*) Key, ivec) == -1)
1.1.1.1.2.1  misho     515:                return NULL;
                    516: 
                    517:        return str;
                    518: }
1.1.1.1.2.11  misho     519: 
                    520: int
                    521: stopProcess(sched_root_task_t * __restrict root, proc_head_t * __restrict h, pid_t pid, sched_task_func_t func)
                    522: {
                    523:        struct tagProc *p;
                    524: 
                    525:        FTRACE(3);
                    526: 
                    527:        SLIST_FOREACH(p, h, proc_next)
                    528:                if (p->proc_pid == pid) {
                    529:                        break;
                    530:                }
1.1.1.1.2.12  misho     531:        VERB(3) LOG("pid=%d found=%p\n", pid, p);
1.1.1.1.2.11  misho     532:        if (!p)
                    533:                return 1;
                    534: 
                    535:        ioFreePTY(p->proc_pty, p->proc_ttyname);
1.1.1.1.2.12  misho     536:        if (p->proc_pty)
                    537:                schedCancelby(root, NULL, CRITERIA_FD, (void*) ((intptr_t) p->proc_pty), NULL);
1.1.1.1.2.11  misho     538: 
1.1.1.1.2.12  misho     539:        p->proc_pty = 0;
1.1.1.1.2.11  misho     540:        p->proc_pid = 0;
                    541:        p->proc_seq = 0;
                    542:        p->proc_flg = ANSH_FLG_EOF;
                    543:        p->proc_rlen_[FD2NET] = 0;
                    544: 
                    545:        schedCallOnce(root, func, p, p->proc_sock);
                    546:        return 0;
                    547: }

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