AI disclosure: This post was written with the help of an LLM, and the code in it was written by AI. I, a human, carried out and verified all the steps.
I wanted my parents to be able to scan over the network without installing Samsung’s software. I already had a CUPS print server in a Proxmox LXC container, so I used the same container to make my Samsung SCX-3405W available as an eSCL scanner. macOS Image Capture found it and could scan, but the scan head kept moving forward and backward, and a 300 dpi color scan took more than two minutes.
I reproduced the slowdown with SANE directly, traced it to the TCP receive loop in the xerox_mfp backend, and rebuilt just that backend. The same direct scan went from 140.638 seconds to 18.336 seconds, an 87% reduction in elapsed time. The faster run was a command-line test. The AirSane rollout is the next step.

Testing the Samsung directly with SANE
The CUPS queue gave me the printer’s LAN address. I installed SANE’s command-line tools and added the following entry to /etc/sane.d/xerox_mfp.conf. Use your own scanner’s address if you reproduce this.
apt update
apt install sane-utils
# Samsung SCX-3405W
tcp 192.168.1.142 9400
With xerox_mfp enabled in /etc/sane.d/dll.conf, I checked detection and scanned a page.
grep -x 'xerox_mfp' /etc/sane.d/dll.conf
scanimage -L
scanimage \
--device-name="xerox_mfp:tcp 192.168.1.142 9400" \
--resolution 150 \
--format=png > /tmp/testscan.png
SANE identified the device as a Samsung SCX-3400 Series multifunction peripheral. The test scan succeeded.
Sharing the scanner through AirSane
I built AirSane in the container. It exposes the SANE scanner over eSCL and uses Avahi to advertise it on the local network. (AirSane project documentation)
apt install git cmake g++ make \
libsane-dev libjpeg-dev libpng-dev \
libavahi-client-dev libusb-1.0-0-dev \
avahi-daemon avahi-utils
cd /opt
git clone https://github.com/SimulPiscator/AirSane.git
cmake -S AirSane -B AirSane-build -DCMAKE_BUILD_TYPE=Release
cmake --build AirSane-build
cmake --install AirSane-build
systemctl daemon-reload
These were the settings for /etc/default/airsane. Port 8090 serves the web interface and eSCL. LOCAL_SCANNERS_ONLY=false includes the Samsung even though SANE reaches it over TCP.
INTERFACE=*
LISTEN_PORT=8090
HOTPLUG=true
MDNS_ANNOUNCE=true
LOCAL_SCANNERS_ONLY=false
WEB_INTERFACE=true
This container only needed one scanner backend. I kept xerox_mfp enabled and disabled the separate airscan entry so the server would avoid discovering its own eSCL advertisement. On a host with other scanners, keep their backends enabled.
Disable every SANE backend in dll.conf, then explicitly enable only xerox_mfp:
sed -i.bak '/^[[:space:]]*[^#[:space:]]/s/^/#/' /etc/sane.d/dll.conf
sed -i 's/^#[[:space:]]*xerox_mfp[[:space:]]*$/xerox_mfp/' /etc/sane.d/dll.conf
AirSane creates its own eSCL advertisement, so back up and disable the separate airscan backend to avoid advertising the service back to itself:
cp /etc/sane.d/dll.d/airscan /etc/sane.d/airscan.dll.bak
sed -i '/^[[:space:]]*[^#[:space:]]/s/^/#/' /etc/sane.d/dll.d/airscan
Verify that the only active backends are expected and that the saned account can discover the Samsung:
grep -HvE '^[[:space:]]*(#|$)' /etc/sane.d/dll.conf /etc/sane.d/dll.d/*
runuser -u saned -- scanimage -L
Enable Avahi and AirSane, restart the AirSane service to load the configuration, and inspect its status and recent log entries:
systemctl enable --now avahi-daemon
systemctl enable --now airsaned
systemctl restart airsaned
systemctl status airsaned --no-pager
journalctl -u airsaned -n 30 --no-pager
Finally, confirm that Avahi is publishing the AirSane eSCL service:
avahi-browse -rt _uscan._tcp
The saned account could see the Samsung, and macOS Image Capture discovered it and completed a scan through AirSane. It was slow enough that I went back to scanimage to see where the time was going. The browser interface at http://cups.local:8090/ was another way to test the service.
Finding the one-second waits
I ran scanimage as saned inside the container, bypassing AirSane and macOS. A 150 dpi color JPEG took 72.746 seconds with the stock backend. At 300 dpi I turned on driver logging.
time runuser -u saned -- env \
SANE_DEBUG_XEROX_MFP=4 \
scanimage \
--device-name="xerox_mfp:tcp 192.168.1.142 9400" \
--resolution 300 \
--mode Color \
--format=jpeg \
> /tmp/testscan-debug.jpg \
2> /tmp/xerox_mfp.log
That run took 140.638 seconds. The log had 124 receive timeout messages. In one control exchange the backend requested 1,024 bytes, got 32, and logged Resource temporarily unavailable about a second after it began waiting. One image-data read requested 65,536 bytes and timed out after receiving 22,364. The low CPU time and repeated waits put my attention on the backend’s TCP receive loop.
In SANE 1.2.1, backend/xerox_mfp-tcp.c used a one-second receive timeout. tcp_dev_request() was trying to fill the requested buffer, even when the protocol had already delivered its full response. The higher-level code in backend/xerox_mfp.c already knew how to tell where those responses ended.
- For a control command,
dev->reslen = sizeof(dev->res)supplied buffer capacity. The response itself has a three-byte header, with a total length ofdev->res[2] + 3. A 32-byte reply could therefore leave the old loop waiting for another 992 bytes. - For image data,
dev_acquire()read the announced block size intodev->blocklen. The TCP read can stop at the end of that block, or at the caller’s buffer limit when it needs several reads.
Patching the TCP backend
The installed SANE packages were 1.2.1-7build4 on amd64. I used the matching Ubuntu source package and kept the build under /opt/sane-patched.
scanimage --version
dpkg -l | grep -E 'libsane|sane-utils'
mkdir -p /opt/sane-patched
cd /opt/sane-patched
My first source download failed because APT had no deb-src entries. For Ubuntu’s Deb822 source files, change Types: deb to Types: deb deb-src in the relevant stanzas in /etc/apt/sources.list.d/ubuntu.sources. For a legacy list, add matching deb-src entries for the Ubuntu repositories in use. After enabling source entries, I downloaded and unpacked the package.
apt update
apt install dpkg-dev
apt-get source sane-backends=1.2.1-7build4
cd /opt/sane-patched/sane-backends-1.2.1
cp backend/xerox_mfp-tcp.c ../xerox_mfp-tcp.c.original
nano backend/xerox_mfp-tcp.c
I replaced only the full tcp_dev_request() function with the implementation below. It reads the command header first, validates the response code and declared length, and loops until it receives the required bytes. For raw image reads it uses the smaller of the buffer capacity and the remainder of the image block. It retries interrupted reads and returns an I/O error if the connection closes or a read fails before the expected bytes arrive. The socket timeout still handles genuinely stalled reads.
int tcp_dev_request(struct device *dev,
SANE_Byte *cmd, size_t cmdlen,
SANE_Byte *resp, size_t *resplen)
{
size_t bytes_recv = 0;
size_t expected;
size_t capacity;
ssize_t rc;
int is_command = cmd && cmdlen;
/* Send request, if any */
if (is_command) {
rc = sanei_tcp_write(dev->dn, cmd, cmdlen);
if (rc < 0 || (size_t)rc != cmdlen) {
DBG(1, "%s: failed to send command\n", __func__);
return SANE_STATUS_IO_ERROR;
}
}
/* Commands without a response, e.g. READ_IMAGE */
if (!resp || !resplen) {
if (resplen)
*resplen = 0;
return SANE_STATUS_GOOD;
}
capacity = *resplen;
*resplen = 0;
if (is_command) {
/* Read the protocol header first */
if (capacity < 3)
return SANE_STATUS_IO_ERROR;
expected = 3;
} else {
/* Never read beyond the remaining image block */
if (dev->blocklen <= 0)
return SANE_STATUS_IO_ERROR;
expected = capacity;
if ((size_t)dev->blocklen < expected)
expected = (size_t)dev->blocklen;
}
while (bytes_recv < expected) {
rc = recv(dev->dn,
resp + bytes_recv,
expected - bytes_recv,
0);
if (rc < 0 && errno == EINTR)
continue;
if (rc <= 0) {
DBG(1, "%s: receive failed: %s (%lu/%lu bytes)\n",
__func__,
rc == 0 ? "connection closed" : strerror(errno),
(u_long)bytes_recv,
(u_long)expected);
*resplen = bytes_recv;
return SANE_STATUS_IO_ERROR;
}
bytes_recv += (size_t)rc;
/* Determine the complete command response length */
if (is_command && bytes_recv == 3) {
if (resp[0] != RES_CODE)
return SANE_STATUS_IO_ERROR;
expected = (size_t)resp[2] + 3;
if (expected > capacity)
return SANE_STATUS_IO_ERROR;
}
}
*resplen = bytes_recv;
DBG(3, "%s: received %lu/%lu bytes\n",
__func__,
(u_long)bytes_recv,
(u_long)expected);
return SANE_STATUS_GOOD;
}
Building and testing one shared library
The source tree had configure.ac and autogen.sh but no generated configure script. My first ./configure attempt failed with No such file or directory. From the source tree, I generated the build files and compiled the backend.
apt-get build-dep sane-backends
apt install autoconf automake libtool autoconf-archive autopoint pkg-config
./autogen.sh
./configure \
--prefix=/opt/sane-patched/test-install \
--sysconfdir=/etc \
BACKENDS="xerox_mfp"
make
ls -lh backend/.libs/libsane-xerox_mfp.so*
The build produced backend/.libs/libsane-xerox_mfp.so.1.2.1 and the usual symlinks. I copied just that library under the name the SANE backend loader looks for. This test skipped make install and left Ubuntu’s packaged library in place.
mkdir -p /opt/sane-patched/lib
install -m 0644 \
backend/.libs/libsane-xerox_mfp.so.1.2.1 \
/opt/sane-patched/lib/libsane-xerox_mfp.so.1
time runuser -u saned -- env \
LD_LIBRARY_PATH=/opt/sane-patched/lib \
SANE_CONFIG_DIR=/etc/sane.d \
SANE_DEBUG_XEROX_MFP=4 \
SANE_DEBUG_DLL=4 \
scanimage \
--device-name="xerox_mfp:tcp 192.168.1.142 9400" \
--resolution 300 \
--mode Color \
--format=jpeg \
> /tmp/testscan-patched.jpg \
2> /tmp/xerox_mfp-patched.log
The SANE loader log showed that it opened /opt/sane-patched/lib/libsane-xerox_mfp.so.1. The trace also showed an 82,304-byte image band arriving in two complete reads, 65,536 bytes and 16,768 bytes.
| Direct SANE scan at 300 dpi, color JPEG | Stock backend | Patched backend |
|---|---|---|
| Wall-clock time | 140.638 s | 18.336 s |
| TCP receive timeout messages | 124 | 0 |
| User CPU time | 0.202 s | 0.184 s |
| System CPU time | 0.119 s | 0.118 s |
That is 122.302 seconds saved, or about 87.0%, for one before-and-after pair on the same scanner and container. The timings cover the direct scan command, including JPEG output. Visual comparison and repeated runs are still part of acceptance.