Skip to content
Open
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
4 changes: 3 additions & 1 deletion conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Configuration struct {
LogAllPrinterAttrs bool // Get *all* printer attrs, for logging
ColorConsole bool // Enable ANSI colors on console
Quirks QuirksDb // Quirks data base
Interface int // index of the interface to use
}

// Conf contains a global instance of program configuration
Expand All @@ -55,6 +56,7 @@ var Conf = Configuration{
LogMaxBackupFiles: 5,
LogAllPrinterAttrs: false,
ColorConsole: true,
Interface: -1,
}

// ConfLoad loads the program configuration
Expand Down Expand Up @@ -113,7 +115,7 @@ func confLoadInternal(path string) error {
case confMatchName(rec.Key, "dns-sd"):
err = rec.LoadNamedBool(&Conf.DNSSdEnable, "disable", "enable")
case confMatchName(rec.Key, "interface"):
err = rec.LoadNamedBool(&Conf.LoopbackOnly, "all", "loopback")
err = rec.LoadInetInterface(&Conf.Interface)
case confMatchName(rec.Key, "ipv6"):
err = rec.LoadNamedBool(&Conf.IPV6Enable, "disable", "enable")
}
Expand Down
28 changes: 7 additions & 21 deletions dnssd_avahi.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build linux || freebsd
// +build linux freebsd

/* ipp-usb - HTTP reverse proxy, backed by IPP-over-USB connection to device
*
Expand Down Expand Up @@ -76,10 +75,10 @@ func newDnssdSysdep(log *Logger, instance string,
}

// Obtain index of loopback interface
loopback, err := Loopback()
if err != nil {
goto ERROR // Very unlikely to happen
}
// loopback, err := Loopback()
// if err != nil {
// goto ERROR // Very unlikely to happen
// }

// Obtain AvahiPoll
poll, err = avahiGetPoll()
Expand Down Expand Up @@ -124,14 +123,7 @@ func newDnssdSysdep(log *Logger, instance string,
avahiEgroupMap[sysdep.egroup] = sysdep

// Compute iface and proto, adjust fqdn
iface = C.AVAHI_IF_UNSPEC
if Conf.LoopbackOnly {
iface = loopback
old := sysdep.fqdn
sysdep.fqdn = "localhost"
sysdep.log.Debug(' ', "DNS-SD: FQDN: %q->%q", old, sysdep.fqdn)
}

iface = Conf.Interface
proto = C.AVAHI_PROTO_UNSPEC
if !Conf.IPV6Enable {
proto = C.AVAHI_PROTO_INET
Expand All @@ -156,16 +148,10 @@ func newDnssdSysdep(log *Logger, instance string,
cInstance = C.CString(instance)
}

// Handle loopback-only mode
ifaceInUse := iface
if svc.Loopback {
ifaceInUse = loopback
}

// Register service type
rc = C.avahi_entry_group_add_service_strlst(
sysdep.egroup,
C.AvahiIfIndex(ifaceInUse),
C.AvahiIfIndex(iface),
C.AvahiProtocol(proto),
0,
cInstance,
Expand All @@ -187,7 +173,7 @@ func newDnssdSysdep(log *Logger, instance string,
cSubtype := C.CString(subtype)
rc = C.avahi_entry_group_add_service_subtype(
sysdep.egroup,
C.AvahiIfIndex(ifaceInUse),
C.AvahiIfIndex(iface),
C.AvahiProtocol(proto),
0,
cInstance,
Expand Down
51 changes: 51 additions & 0 deletions inet_interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//go:build linux || freebsd

/* ipp-usb - HTTP reverse proxy, backed by IPP-over-USB connection to device
*
* Copyright (C) 2020 and up by Alexander Pevzner (pzz@apevzner.com)
* See LICENSE for license terms and conditions
*
* INET interface index discovery
*/

package main

// #cgo pkg-config: avahi-client
//
// #include <stdlib.h>
// #include <avahi-client/publish.h>
// #include <avahi-common/error.h>
// #include <avahi-common/thread-watch.h>
// #include <avahi-common/watch.h>
import "C"

import (
"errors"
"fmt"
"net"
)

// InetInterface returns index of named interface
func InetInterface(name string) (int, error) {
switch name {
case "all":
return C.AVAHI_IF_UNSPEC, nil
case "lo":
case "loopback":
return Loopback()
default:
break
}

interfaces, err := net.Interfaces()
if err == nil {
for _, iface := range interfaces {
if iface.Name == name {
return iface.Index, nil
}
}
err = errors.New("not found")
}

return 0, fmt.Errorf("Inet interface discovery: %s", err)
}
10 changes: 10 additions & 0 deletions inifile.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,16 @@ func (rec *IniRecord) LoadIPPort(out *int) error {
return nil
}

func (rec *IniRecord) LoadInetInterface(out *int) error {
iFaceIndex, err := InetInterface(rec.Value)
if err == nil {
Conf.LoopbackOnly = rec.Value == "lo" || rec.Value == "loopback"
*out = iFaceIndex
return err
}
return rec.errBadValue(" %s not found", rec.Value)
}

// LoadBool loads boolean value
// The destination remains untouched in a case of an error
func (rec *IniRecord) LoadBool(out *bool) error {
Expand Down
3 changes: 2 additions & 1 deletion ipp-usb.conf
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
# printer to the local network. This way you can share your printer
# with other computers in the network, as well as with iOS and Android
# devices.
interface = loopback # all | loopback
# Name of the interface (eg. eth0 or lo) or all or loopback
interface = loopback # all | loopback | eth0 | vlan.1

# Enable or disable IPv6
ipv6 = enable # enable | disable
Expand Down
2 changes: 1 addition & 1 deletion systemd-udev/ipp-usb.service
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[Unit]
Description=Daemon for IPP over USB printer support
Documentation=man:ipp-usb(8)
After=cups.service avahi-daemon.service
After=cups.service avahi-daemon.service network-online.target
Wants=avahi-daemon.service

[Service]
Expand Down