UPnP

package module
v0.0.0-...-759385d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 15, 2016 License: MIT Imports: 15 Imported by: 0

README

UPnP in go

Easy for dialog with a UPnP device.

(Sorry if my english is bad in the project or the examples, I try)

I built a tool at https://github.com/micmonay/UpnpTools

This exemple is for get the IPV4 in your router. For exemple your router has duty a UPnP active

up := upnp.NewUPNP(upnp.SERVICE_GATEWAY_IPV4_V2) // or upnp.SERVICE_GATEWAY_IPV4_V1
Interface, err := upnp.GetInterfaceByName("eth0")
if err != nil {
	log.Println(err)
	return
}
// Get all devices compatible for the service name (timeout 1 second)
devices := up.GetAllCompatibleDevice(Interface, 1)
if len(devices) == 0 {
	return
}
// Get services
services := devices[0].GetServicesByType(upnp.SERVICE_GATEWAY_IPV4_V2) // or upnp.SERVICE_GATEWAY_IPV4_V1
if len(services) == 0 {
	return
}
// if you have a one routeur it's ok, other ...
service := services[0]
if service == nil {
	log.Println("not found service")
	return
}
// send request
response, err := service.GetAction("GetExternalIPAddress").Send()
if err != nil {
	log.Println(err)
	return
}
// get response argument
ip, err := response.GetValueArgument("NewExternalIPAddress")
if err != nil {
	log.Println(err)
	return
}
	fmt.Println("Your WAN ip address is " + ip)

If your action containe arguments at sending.

Action := Services[0].GetAction("ActionName")
Action.AddVariable("nameOfArgument","value")
Action.Send()

Documentation

Overview

Easy for dialog with a UPnP device in golang

Index

Examples

Constants

View Source
const (
	ALL_DEVICE              = "upnp:rootdevice"
	SERVICE_GATEWAY_STATE   = "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1"
	SERVICE_GATEWAY_IPV4_V1 = "urn:schemas-upnp-org:service:WANIPConnection:1"
	SERVICE_GATEWAY_IPV4_V2 = "urn:schemas-upnp-org:service:WANIPConnection:2"
	SERVICE_GATEWAY_IPV6    = "urn:schemas-upnp-org:service:WANIPv6FirewallControl:1"
)

Exemple of service research

Variables

This section is empty.

Functions

func GetIPAdress

func GetIPAdress(_interface *net.Interface) string

GetIPAdress for get ip adress

func GetInterfaceByIP

func GetInterfaceByIP(ip string) (*net.Interface, error)

GetInterfaceByIP get interface by ip 192.168.1.1 (search by contain not exactly)

func GetInterfaceByName

func GetInterfaceByName(name string) (*net.Interface, error)

GetInterfaceByName get interface by name

Types

type Action

type Action struct {
	// contains filtered or unexported fields
}

Action usued for generate and read action

func (*Action) AddVariable

func (a *Action) AddVariable(argument string, value string)

AddVariable add data for argument

func (*Action) GetInArguments

func (a *Action) GetInArguments() []*Argument

GetInArguments for get arguments

func (*Action) GetLastRequest

func (a *Action) GetLastRequest() string

GetLastRequest return last requet xml for debug

func (*Action) GetLastResponse

func (a *Action) GetLastResponse() string

GetLastResponse return last response xml

func (*Action) GetName

func (a *Action) GetName() string

GetName for get action name

func (*Action) Send

func (a *Action) Send() (*Response, error)

Send data and return response or error

type Argument

type Argument struct {
	// contains filtered or unexported fields
}

Argument arguement

func (*Argument) GetAllowedValues

func (a *Argument) GetAllowedValues() []string

GetAllowedValues return value possible

func (*Argument) GetDefault

func (a *Argument) GetDefault() string

GetDefault return default string or ""

func (*Argument) GetDirection

func (a *Argument) GetDirection() string

GetDirection return in or out

func (*Argument) GetMaximum

func (a *Argument) GetMaximum() string

GetMaximum return maximum or ""

func (*Argument) GetMinimum

func (a *Argument) GetMinimum() string

GetMinimum return minimum or ""

func (*Argument) GetName

func (a *Argument) GetName() string

GetName return name

func (*Argument) GetStep

func (a *Argument) GetStep() string

GetStep return step or ""

func (*Argument) GetType

func (a *Argument) GetType() string

GetType return type for argument

type Device

type Device struct {
	XMLName xml.Name `xml:"device"`

	FriendlyName     string     `xml:"friendlyName"`
	Manufacturer     string     `xml:"manufacturer"`
	ManufacturerURL  string     `xml:"manufacturerURL"`
	ModelDescription string     `xml:"modelDescription"`
	ModelName        string     `xml:"modelName"`
	ModelNumber      string     `xml:"modelNumber"`
	ModelURL         string     `xml:"modelURL"`
	SerialNumber     string     `xml:"serialNumber"`
	UDN              string     `xml:"UDN"`
	PresentationURL  string     `xml:"presentationURL"`
	Icons            []*Icon    `xml:"iconList>icon"`
	Devices          []*Device  `xml:"deviceList>device"`
	Services         []*Service `xml:"serviceList>service"`
	// contains filtered or unexported fields
}

Device data

func (*Device) GetAllService

func (d *Device) GetAllService() []*Service

GetAllService returnn all services child in child

func (*Device) GetDevices

func (d *Device) GetDevices() []*Device

GetDevices return devices child

func (d *Device) GetIconLink() []*Icon

GetIconLink link icon

func (*Device) GetServices

func (d *Device) GetServices() []*Service

GetServices return contain service

func (*Device) GetServicesByType

func (d *Device) GetServicesByType(typeName string) []*Service

GetServicesByType return service if is a service name

func (*Device) HasDevice

func (d *Device) HasDevice() bool

HasDevice have device

func (*Device) HasIcon

func (d *Device) HasIcon() bool

HasIcon have icon

func (*Device) HasService

func (d *Device) HasService() bool

HasService return true if contain a service

type Error

type Error struct {
	XMLName          xml.Name `xml:"UPnPError"`
	ErrorCode        string   `xml:"errorCode"`
	ErrorDescription string   `xml:"errorDescription"`
}

Error format upnp error

type Icon

type Icon struct {
	XMLName  xml.Name `xml:"icon"`
	Mimetype string   `xml:"mimetype"`
	Width    int      `xml:"width"`
	Height   int      `xml:"height"`
	Depth    int      `xml:"depth"`
	URL      string   `xml:"url"`
}

Icon icon info

type Response

type Response struct {
	// contains filtered or unexported fields
}

Response contain a response upnp

func (*Response) GetError

func (r *Response) GetError() *Error

GetError if response have an error upnp

func (*Response) GetValueArgument

func (r *Response) GetValueArgument(nameArgument string) (string, error)

GetValueArgument get value of argument

func (*Response) Success

func (r *Response) Success() bool

Success true if ok

func (*Response) ToString

func (r *Response) ToString() string

ToString build a string for debug

type Root

type Root struct {
	XMLName  xml.Name `xml:"root"`
	Location *url.URL
	Device   *Device `xml:"device"`
}

Root root description file

func NewXMLUPNPFile

func NewXMLUPNPFile(_url string) (*Root, error)

NewXMLUPNPFile get description file

func (*Root) GetDevice

func (r *Root) GetDevice() *Device

GetDevice return child device

func (*Root) GetLocation

func (r *Root) GetLocation() *url.URL

GetLocation return location url

type Service

type Service struct {
	XMLName     xml.Name `xml:"service"`
	ServiceType string   `xml:"serviceType"`
	ServiceID   string   `xml:"serviceId"`
	ControlURL  string   `xml:"controlURL"`
	EventSubURL string   `xml:"eventSubURL"`
	SCPDURL     string   `xml:"SCPDURL"`
	// contains filtered or unexported fields
}

Service for manipulation service

func (*Service) GetAction

func (s *Service) GetAction(name string) *Action

GetAction return action selected by name

func (*Service) GetActions

func (s *Service) GetActions() []*Action

GetActions return all actions for the service

func (*Service) GetSCPD

func (s *Service) GetSCPD() (*scpd.SCPD, error)

GetSCPD return service description

type UPNP

type UPNP struct {
	// contains filtered or unexported fields
}

UPNP is a struct for dialog on UPNP

func NewUPNP

func NewUPNP(serviceType string) *UPNP

NewUPNP for create new UPNP request for type device

Example

ExampleNewUPNP example for get external ipv4 address from gateway

package main

import (
	"bufio"
	"fmt"
	"net"
	"os"
	"strconv"
	"strings"
)

func getInput() string {
	reader := bufio.NewReader(os.Stdin)
	str, _ := reader.ReadString('\n')
	return strings.TrimSpace(str)
}
func selectInterface() (*net.Interface, error) {
	addrs, _ := net.Interfaces()
	for n, addr := range addrs {
		ip, err := addr.Addrs()
		if err != nil {
			continue
		}
		fmt.Println(n, " : ", addr.Name, " {", addr.HardwareAddr, "} ", ip)
	}
	fmt.Print("Please select interface number : ")
	interfaceNum, err := strconv.ParseUint(getInput(), 10, 64)
	if err != nil {
		return nil, err
	}
	return &addrs[interfaceNum], nil
}

// ExampleNewUPNP example for get external ipv4 address from gateway
func main() {
	up := NewUPNP(SERVICE_GATEWAY_IPV4_V2)
	_interface, err := selectInterface()
	if err != nil {
		panic(err)
	}
	devices := up.GetAllCompatibleDevice(_interface, 1)
	if len(devices) == 0 {
		return
	}
	services := devices[0].GetServicesByType(SERVICE_GATEWAY_IPV4_V2)
	if len(services) == 0 {
		return
	}
	service := services[0]
	response, err := service.GetAction("GetExternalIPAddress").Send()
	if err != nil {
		panic(err)
	}
	fmt.Println(response.ToString())
	fmt.Print("Press enter")
	getInput()
}
Output:

external ip

func NewUPNPAllService

func NewUPNPAllService() *UPNP

NewUPNPAllService for create new UPNP request for all type device

func (*UPNP) GetAllCompatibleDevice

func (u *UPNP) GetAllCompatibleDevice(_interface *net.Interface, timeout int) []*Device

GetAllCompatibleDevice return All devices

func (*UPNP) GetLinkDeviceOnly

func (u *UPNP) GetLinkDeviceOnly(_interface *net.Interface, timeout int) []string

GetLinkDeviceOnly For get all link description file

type XMLAction

type XMLAction struct {
	XMLName   xml.Name
	Xmlns     string        `xml:"xmlns:u,attr"`
	Variables []XMLVariable `xml:",any"`
}

XMLAction action

type XMLActionHead

type XMLActionHead struct {
	XMLName       xml.Name `xml:"s:Envelope"`
	Xmlns         string   `xml:"xmlns:s,attr"`
	EncodingStyle string   `xml:"s:encodingStyle,attr"`
	Body          XMLBody  `xml:"s:Body"`
}

XMLActionHead Header

type XMLBody

type XMLBody struct {
	Action XMLAction `xml:",any"`
}

XMLBody body

type XMLRepAction

type XMLRepAction struct {
	XMLName   xml.Name
	Xmlns     string           `xml:"xmlns:u,attr"`
	Variables []XMLRepVariable `xml:",any"`
}

XMLRepAction Action

type XMLRepActionHead

type XMLRepActionHead struct {
	XMLName       xml.Name   `xml:"Envelope"`
	Xmlns         string     `xml:"xmlns:s,attr"`
	EncodingStyle string     `xml:"encodingStyle,attr"`
	Body          XMLRepBody `xml:"Body"`
}

XMLRepActionHead Header

type XMLRepBody

type XMLRepBody struct {
	Action XMLRepAction `xml:",any"`
}

XMLRepBody Body

type XMLRepVariable

type XMLRepVariable struct {
	XMLName xml.Name

	Value string `xml:",innerxml"`
}

XMLRepVariable variable

type XMLVariable

type XMLVariable struct {
	XMLName xml.Name
	Value   string `xml:",innerxml"`
}

XMLVariable variable

Directories

Path Synopsis
dependence for https://github.com/micmonay/UPnP
dependence for https://github.com/micmonay/UPnP

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL