|
| 1 | +<img src="https://elytrium.net/src/img/elytrium.webp" alt="Elytrium" align="right"> |
| 2 | + |
| 3 | +# pcap-java |
| 4 | + |
| 5 | +[](https://ely.su/discord) |
| 6 | + |
| 7 | +Lightweight libpcap Java (JNI) wrapper. |
| 8 | + |
| 9 | +## Why pcap-java? |
| 10 | + |
| 11 | +- pcap-java uses JNI to interact with the native stuff, while other libraries use JNA. |
| 12 | + |
| 13 | +## How to |
| 14 | + |
| 15 | +Sample code: |
| 16 | + |
| 17 | +```java |
| 18 | + Pcap.init(); |
| 19 | + PcapHandle handle = Pcap.openLive("any", 120, 1, 10); |
| 20 | + PcapDumper dumper = handle.dumpOpen("dump.pcap"); |
| 21 | + BpfProgram filter = handle.compile("tcp and dst port 80"); |
| 22 | + handle.setFilter(filter); |
| 23 | + filter.free(); |
| 24 | + |
| 25 | + LinkType datalink = this.handle.datalink(); |
| 26 | + new Thread(() -> { |
| 27 | + try { |
| 28 | + this.handle.loop(-1, (packetHeader, rawPacket) -> { |
| 29 | + try { |
| 30 | + Packet packet = new Packet(); |
| 31 | + packet.decode(rawPacket, datalink); |
| 32 | + System.out.println(packet); |
| 33 | + dumper.dump(packetHeader, rawPacket); |
| 34 | + dumper.flush(); |
| 35 | + } catch (LayerDecodeException | PcapException e) { |
| 36 | + e.printStackTrace(); |
| 37 | + } |
| 38 | + }); |
| 39 | + } catch (PcapException e) { |
| 40 | + e.printStackTrace(); |
| 41 | + } |
| 42 | + }); |
| 43 | + |
| 44 | + dumper.close(); |
| 45 | + handle.close(); |
| 46 | +``` |
| 47 | + |
| 48 | +## How to include it |
| 49 | + |
| 50 | +#### Setup your project via adding our maven repository to your pom.xml or build.gradle file. |
| 51 | + |
| 52 | +- Maven: |
| 53 | + |
| 54 | +```xml |
| 55 | + <repositories> |
| 56 | + <repository> |
| 57 | + <id>elytrium-repo</id> |
| 58 | + <url>https://maven.elytrium.net/repo/</url> |
| 59 | + </repository> |
| 60 | + </repositories> |
| 61 | + |
| 62 | + <dependencies> |
| 63 | + <dependency> |
| 64 | + <groupId>net.elytrium</groupId> |
| 65 | + <artifactId>pcap</artifactId> |
| 66 | + <version>1.0.0</version> |
| 67 | + </dependency> |
| 68 | + </dependencies> |
| 69 | +``` |
| 70 | + |
| 71 | +- Gradle: |
| 72 | + |
| 73 | +```groovy |
| 74 | + repositories { |
| 75 | + maven { |
| 76 | + setName("elytrium-repo") |
| 77 | + setUrl("https://maven.elytrium.net/repo/") |
| 78 | + } |
| 79 | + } |
| 80 | +
|
| 81 | + dependencies { |
| 82 | + implementation("net.elytrium:pcap:1.0.0") |
| 83 | + } |
| 84 | +``` |
| 85 | + |
| 86 | +## See also |
| 87 | + |
| 88 | +- [LimboFilter TCP Listener](https://github.com/Elytrium/LimboFilter/blob/master/src/main/java/net/elytrium/limbofilter/listener/TcpListener.java) - Another code example. |
| 89 | + |
| 90 | +## Donation |
| 91 | + |
| 92 | +Your donations are really appreciated. Donations wallets/links/cards: |
| 93 | + |
| 94 | +- MasterCard Debit Card (Tinkoff Bank): ``5536 9140 0599 1975`` |
| 95 | +- Qiwi Wallet: ``PFORG`` or [this link](https://my.qiwi.com/form/Petr-YSpyiLt9c6) |
| 96 | +- YooMoney Wallet: ``4100 1721 8467 044`` or [this link](https://yoomoney.ru/quickpay/shop-widget?writer=seller&targets=Donation&targets-hint=&default-sum=&button-text=11&payment-type-choice=on&mobile-payment-type-choice=on&hint=&successURL=&quickpay=shop&account=410017218467044) |
| 97 | +- Monero (XMR): 86VQyCz68ApebfFgrzRFAuYLdvd3qG8iT9RHcru9moQkJR9W2Q89Gt3ecFQcFu6wncGwGJsMS9E8Bfr9brztBNbX7Q2rfYS |
0 commit comments