⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 543.htm

📁 unix高级编程原吗
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>CTerm非常精华下载</title>
</head>
<body bgcolor="#FFFFFF">
<table border="0" width="100%" cellspacing="0" cellpadding="0" height="577">
<tr><td width="32%" rowspan="3" height="123"><img src="DDl_back.jpg" width="300" height="129" alt="DDl_back.jpg"></td><td width="30%" background="DDl_back2.jpg" height="35"><p align="center"><a href="http://apue.dhs.org"><font face="黑体"><big><big>apue</big></big></font></a></td></tr>
<tr>
<td width="68%" background="DDl_back2.jpg" height="44"><big><big><font face="黑体"><p align="center">               ● UNIX网络编程                       (BM: clown)                </font></big></big></td></tr>
<tr>
<td width="68%" height="44" bgcolor="#000000"><font face="黑体"><big><big><p   align="center"></big></big><a href="http://cterm.163.net"><img src="banner.gif" width="400" height="60" alt="banner.gif"border="0"></a></font></td>
</tr>
<tr><td width="100%" colspan="2" height="100" align="center" valign="top"><br><p align="center">[<a href="index.htm">回到开始</a>][<a href="516.htm">上一层</a>][<a href="544.htm">下一篇</a>]
<hr><p align="left"><small>:pcap_dlpi.c <br>

---------------------------------------------------------------------------- <br>

---- <br>

: scz 于 2001-2-5 21:10:58 加贴在 绿盟科技论坛(bbs.nsfocus.com)--UNIX系统安全 <br>

: <br>

/* <br>

* Copyright (c) 1993, 1994 <br>

*    The Regents of the University of California.  All rights reserved. <br>

* <br>

* Redistribution and use in source and binary forms, with or without <br>

* modification, are permitted provided that: (1) source code distributions <br>

* retain the above copyright notice and this paragraph in its entirety, (2) <br>

* distributions including binary code include the above copyright notice and <br>

  <br>

* this paragraph in its entirety in the documentation or other materials <br>

* provided with the distribution, and (3) all advertising materials mentioni <br>

ng <br>

* features or use of this software display the following acknowledgement: <br>

* ``This product includes software developed by the University of California <br>

, <br>

* Lawrence Berkeley Laboratory and its contributors.'' Neither the name of <br>

* the University nor the names of its contributors may be used to endorse <br>

* or promote products derived from this software without specific prior <br>



* written permission. <br>

* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED <br>

* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF <br>

* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. <br>

* <br>

* This code contributed by Atanu Ghosh (atanu@cs.ucl.ac.uk), <br>

* University College London. <br>

*/ <br>

#ifndef lint <br>

static  char rcsid[] = <br>

    "@(#)$Header: pcap-dlpi.c,v 1.18 94/06/20 19:07:56 leres Exp $ (LBL)"; <br>

#endif <br>

/* <br>

* Packet capture routine for dlpi under SunOS 5 <br>

* <br>

* Notes: <br>

* <br>

*    - Apparently the DLIOCRAW ioctl() is specific to SunOS. <br>

* <br>

*    - There is a bug in bufmod(7) such that setting the snapshot <br>

*      length results in data being left of the front of the packet. <br>

* <br>

* <br>

*    - It might be desirable to use pfmod(7) to filter packets in the <br>

*      kernel. <br>

*/ <br>

#include <sys/types.h> <br>

#include <sys/time.h> <br>

#include <sys/bufmod.h> <br>

#include <sys/dlpi.h> <br>

#include <sys/signal.h> <br>

#include <sys/stream.h> <br>

#include <sys/systeminfo.h> <br>

#include <net/bpf.h> <br>

#include <ctype.h> <br>

#include <errno.h> <br>

#include <fcntl.h> <br>

#include <memory.h> <br>

#include <stdio.h> <br>

#include <stdlib.h> <br>

#include <string.h> <br>

#include <stropts.h> <br>

#include <unistd.h> <br>

#include "pcap-int.h" <br>

#define    MAXDLBUF    8192 <br>



/* Forwards */ <br>

static int send_request(int, char *, int, char *, char *); <br>

static int dlattachreq(int, u_long, char *); <br>

static int dlpromisconreq(int, u_long, char *); <br>

static int dlokack(int, char *, char *); <br>

static int strioctl(int, int, int, char *); <br>

#ifdef SOLARIS <br>

static char *getrelease(long *, long *, long *); <br>

#endif <br>

static void dlbindreq(int, u_long, u_long, u_long, u_long, u_long); <br>

static int dlbindack(int, char *); <br>

int <br>

pcap_stats(pcap_t *p, struct pcap_stat *ps) <br>

{ <br>

    *ps = p->md.stat; <br>

    return (0); <br>

} <br>

int <br>

pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) <br>

{ <br>

    register int cc, n; <br>

    register u_char *bp, *ep, *pk; <br>



    register struct bpf_insn *fcode; <br>

    register struct sb_hdr *sbp; <br>

    int flags; <br>

    struct strbuf data; <br>

    struct pcap_pkthdr pkthdr; <br>

    flags = 0; <br>

    cc = p->cc; <br>

    if (cc == 0) { <br>

        data.buf = (char *)p->buffer; <br>

        data.maxlen = MAXDLBUF; <br>

        data.len = 0; <br>

        do { <br>

            if (getmsg(p->fd, NULL, &data, &flags) < 0) { <br>

                /* Don't choke when we get ptraced */ <br>

                if (errno == EINTR) { <br>

                    cc = 0; <br>

                    continue; <br>

                } <br>

                strcpy(p->errbuf, pcap_strerror(errno)); <br>

                return (-1); <br>

            } <br>

            cc = data.len; <br>



        } while (cc == 0); <br>

        bp = p->buffer; <br>

    } else <br>

        bp = p->bp; <br>

    /* Loop through packets */ <br>

    fcode = p->fcode.bf_insns; <br>

    ep = bp + cc; <br>

    n = 0; <br>

    while (bp < ep) { <br>

        sbp = (struct sb_hdr *)bp; <br>

        p->md.stat.ps_drop += sbp->sbh_drops; <br>

        ++p->md.stat.ps_recv; <br>

        pk = bp + sizeof(*sbp); <br>

        bp += sbp->sbh_totlen; <br>

        if (bpf_filter(fcode, pk, sbp->sbh_origlen, sbp->sbh_msglen)) { <br>

            pkthdr.ts = sbp->sbh_timestamp; <br>

            pkthdr.len = sbp->sbh_origlen; <br>

            pkthdr.caplen = sbp->sbh_msglen; <br>

            (*callback)(user, &pkthdr, pk); <br>

            if (++n >= cnt && cnt >= 0) { <br>

                p->cc = ep - bp; <br>

                p->bp = bp; <br>



                return (n); <br>

            } <br>

        } <br>

    } <br>

    p->cc = 0; <br>

    return (n); <br>

} <br>

/* XXX to_ms isn't implemented? */ <br>

pcap_t * <br>

pcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf <br>

) <br>

{ <br>

    register pcap_t *p; <br>

    long    buf[MAXDLBUF]; <br>

    int ppa; <br>

    int cppa; <br>

    u_long ss, flag; <br>

#ifdef SOLARIS <br>

    char *release; <br>

    long osmajor, osminor, osmicro; <br>

#endif <br>

    char dname[100]; <br>



    /*nfswatch */ <br>

    struct strioctl si; <br>

    struct timeval timeout; <br>

    u_int chunksz; <br>

    p = (pcap_t *)malloc(sizeof(*p)); <br>

    if (p == NULL) { <br>

        strcpy(ebuf, pcap_strerror(errno)); <br>

        return (NULL); <br>

    } <br>

    memset(p, 0, sizeof(*p)); <br>

    /* <br>

    ** 1) In order to get the ppa take the last character of the device <br>

    ** name if it is a number then fail the open. <br>

    ** <br>

    ** 2) If the name starts with a '/' then this is an absolute pathname, <br>

    ** otherwise prepend '/dev/'. <br>

    ** <br>

    ** 3) Remove the trailing digit and try and open the device <br>

    ** not staggeringly intuitive but it should work. <br>

    ** <br>

    ** If there are more than 9 devices this code will fail. <br>

    */ <br>

    */ <br>

    cppa = device[strlen(device) - 1]; <br>

    if (!isdigit(cppa)) { <br>

        sprintf(ebuf, "%c is not a digit, therefore not a valid ppa", <br>

            cppa); <br>

        goto bad; <br>

    } <br>

    dname[0] = '\0'; <br>

    if (device[0] != '/') <br>

        strcpy(dname, "/dev/"); <br>

    strcat(dname, device); <br>

    dname[strlen(dname) - 1] = '\0'; <br>

    if ((p->fd = open(dname, O_RDWR)) < 0) { <br>

        sprintf(ebuf, "%s: %s", dname, pcap_strerror(errno)); <br>

        goto bad; <br>

    } <br>

    p->snapshot = snaplen; <br>

    ppa = cppa - '0'; <br>

    /* <br>

    ** Attach. <br>

    */ <br>

    if (dlattachreq(p->fd, ppa, ebuf) < 0 || <br>

        dlokack(p->fd, (char *)buf, ebuf) < 0) <br>



        goto bad; <br>

    /* <br>

    * Bind to the specific unit. nfswatch <br>

    */ <br>

#define DLPI_DEFAULTSAP    0x0800            /* IP protocol        */ <br>

    dlbindreq(p->fd, DLPI_DEFAULTSAP, 0, DL_CLDLS, 0, 0); <br>

    if (dlbindack(p->fd, (char *)buf) < 0) { <br>

        sprintf(ebuf, "dlbindack failed.\n"); <br>

        goto bad; <br>

    } <br>

    if (promisc) { <br>

        /* <br>

        ** enable promiscuous. <br>

        */ <br>

        if (dlpromisconreq(p->fd, DL_PROMISC_PHYS, ebuf) < 0 || <br>

            dlokack(p->fd, (char *)buf, ebuf) < 0) <br>

            goto bad; <br>

        if (dlpromisconreq(p->fd, DL_PROMISC_SAP, ebuf) < 0 || <br>

            dlokack(p->fd, (char *)buf, ebuf) < 0) <br>

            goto bad; <br>

        /* <br>

        ** enable multicast, you would have thought promiscuous <br>



        ** would be sufficient. <br>

        */ <br>

        if (dlpromisconreq(p->fd, DL_PROMISC_MULTI, ebuf) < 0 || <br>

            dlokack(p->fd, (char *)buf, ebuf) < 0) <br>

            goto bad; <br>

    } <br>

#ifdef    DLIOCRAW <br>

    /* <br>

    ** This is a non standard SunOS hack to get the ethernet header. <br>

    */ <br>

    if (strioctl(p->fd, DLIOCRAW, 0, NULL) < 0) { <br>

        sprintf(ebuf, "DLIOCRAW: %s", pcap_strerror(errno)); <br>

        goto bad; <br>

    } <br>

#endif <br>

    /* <br>

    ** Another non standard call to get the data nicely buffered <br>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -