Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions build_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ cc_emboss_library, which creates a header file and can be used as a dep in a

There is also a convenience macro, `emboss_cc_library()`, which creates an
`emboss_library` and a `cc_emboss_library` based on it.

For Wireshark Lua dissector generation, use `emboss_lua_library()`.
"""

load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
load("//compiler/back_end/lua:build_defs.bzl", _lua_emboss_library = "lua_emboss_library")

def emboss_cc_library(name, srcs, deps = [], import_dirs = [], enable_enum_traits = True, **kwargs):
"""Constructs a C++ library from an .emb file."""
Expand Down Expand Up @@ -257,3 +260,33 @@ cc_emboss_library = rule(
},
provides = [CcInfo, EmbossInfo],
)

def emboss_lua_library(name, srcs, deps = [], import_dirs = [], **kwargs):
"""Constructs a Wireshark Lua dissector from an .emb file.

Args:
name: The name of the library.
srcs: List of .emb source files (must be exactly one).
deps: List of emboss_library dependencies.
import_dirs: List of import directories.
**kwargs: Additional arguments.
"""
if len(srcs) != 1:
fail(
"Must specify exactly one Emboss source file for emboss_lua_library.",
"srcs",
)

emboss_library(
name = name + "_ir",
srcs = srcs,
deps = [dep + "_ir" for dep in deps],
import_dirs = import_dirs,
**kwargs
)

_lua_emboss_library(
name = name,
deps = [":" + name + "_ir"],
**kwargs
)
54 changes: 54 additions & 0 deletions compiler/back_end/lua/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Emboss Wireshark Lua dissector code generator.

load("@rules_python//python:py_binary.bzl", "py_binary")
load("@rules_python//python:py_library.bzl", "py_library")
load("@rules_python//python:py_test.bzl", "py_test")

package(
default_visibility = [
"//visibility:private",
],
)

py_binary(
name = "emboss_codegen_lua",
srcs = ["emboss_codegen_lua.py"],
python_version = "PY3",
visibility = ["//visibility:public"],
deps = [
":dissector_generator",
"//compiler/util:ir_data",
],
)

py_library(
name = "dissector_generator",
srcs = ["dissector_generator.py"],
deps = [
"//compiler/util:ir_data",
"//compiler/util:ir_util",
],
)

py_test(
name = "dissector_generator_test",
srcs = ["dissector_generator_test.py"],
deps = [
":dissector_generator",
"//compiler/util:ir_data",
],
)
146 changes: 146 additions & 0 deletions compiler/back_end/lua/QUICKSTART.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# Quick Start Guide: Wireshark Lua Dissector Generator

This guide will help you quickly generate and use Wireshark dissectors from your Emboss protocol definitions.

## Step 1: Define Your Protocol in Emboss

Create a `.emb` file with your protocol definition:

```emboss
# myprotocol.emb

-- My custom network protocol
[(wireshark_filter): "myproto"]

[$default byte_order: "LittleEndian"]

-- Packet types
enum PacketType:
-- Data packet
DATA = 0x01
-- Acknowledgment
ACK = 0x02

-- Protocol header
struct Header:
-- Packet type identifier
0 [+1] PacketType type
-- Packet sequence number
1 [+4] UInt seq_num
-- Payload length
5 [+2] UInt length
```

## Step 2: Add to Your BUILD File

```python
load("//:build_defs.bzl", "emboss_lua_library")

emboss_lua_library(
name = "myprotocol_dissector",
srcs = ["myprotocol.emb"],
)
```

## Step 3: Build the Dissector

```bash
bazel build :myprotocol_dissector
```

The generated `.lua` file will be in `bazel-bin/myprotocol.emb.lua`

## Step 4: Install in Wireshark

Copy the generated `.lua` file to your Wireshark plugins directory:

**Linux:**
```bash
cp bazel-bin/myprotocol.emb.lua ~/.local/lib/wireshark/plugins/
```

**macOS:**
```bash
cp bazel-bin/myprotocol.emb.lua ~/.wireshark/plugins/
```

**Windows:**
```powershell
copy bazel-bin\myprotocol.emb.lua %APPDATA%\Wireshark\plugins\
```

## Step 5: Configure Port Registration

Edit the generated `.lua` file and uncomment the registration code at the bottom:

```lua
-- Register the dissector
local udp_table = DissectorTable.get("udp.port")
udp_table:add(12345, myproto_proto) -- Replace 12345 with your port
```

For TCP:
```lua
local tcp_table = DissectorTable.get("tcp.port")
tcp_table:add(12345, myproto_proto)
```

## Step 6: Load in Wireshark

1. Open Wireshark
2. Reload Lua plugins: Analyze → Reload Lua Plugins (or Ctrl+Shift+L)
3. Your dissector is now active!

## Testing Your Dissector

1. Capture some traffic on your configured port
2. Wireshark should automatically use your dissector
3. You should see your fields displayed with their names and descriptions
4. Enum values will show as text (e.g., "DATA" instead of "0x01")

## Troubleshooting

**Dissector not loading?**
- Check Wireshark's Lua console (Tools → Lua → Console) for errors
- Verify the .lua file is in the correct plugins directory
- Make sure you reloaded Lua plugins

**Fields not displaying?**
- Verify your port registration matches your traffic
- Check that byte order matches your data
- Ensure field sizes are correct in your .emb file

**Want to see all available filters?**
- In Wireshark, go to Edit → Preferences → Protocols
- Find your protocol in the list
- Or use the filter expression builder (Analyze → Display Filter Expression)

## Advanced Features

### Custom Filter Names

```emboss
struct MyStruct:
[(wireshark_filter): "myproto.custom"]
0 [+1] UInt field1
[(wireshark_filter): "myproto.special_field"]
```

### Nested Structures

```emboss
struct Inner:
0 [+2] UInt value

struct Outer:
0 [+2] Inner inner_data
2 [+4] UInt other_field
```

The dissector will maintain the hierarchy.

## Next Steps

- See the full README.md for all features
- Check out example_protocol.emb for a comprehensive example
- Read about Emboss language features in the main Emboss documentation
Loading