Exemple #1
0
/*------------------------------------------------------------------------
 * int OpenPort(ttp_session_t *session);
 *
 * Creates a new UDP socket for transmitting the file data associated
 * with our pending transfer and receives the destination port number
 * from the client.  Returns 0 on success and non-zero on failure.
 *------------------------------------------------------------------------*/
func (session *Session) OpenPort() error {
	/* create the address structure */
	var udp_address net.UDPAddr
	if session.parameter.client == "" {
		address := session.client_fd.RemoteAddr()
		addr := address.(*net.TCPAddr)
		udp_address.IP = addr.IP
		udp_address.Zone = addr.Zone
	} else {
		addr, err := net.ResolveIPAddr("ip", session.parameter.client)
		if err != nil {
			return err
		}
		if addr.Network() == "ipv6" {
			session.parameter.ipv6 = true
		}
		udp_address.IP = addr.IP
		udp_address.Zone = addr.Zone
	}

	if udp_address.String() == "" {
		return nil
	}

	/* read in the port number from the client */
	buf := make([]byte, 2)
	_, err := session.client_fd.Read(buf)
	if err != nil {
		return err
	}

	x := binary.BigEndian.Uint16(buf)
	udp_address.Port = int(x)

	if session.parameter.verbose {
		Warn("Sending to client port ", x)
	}

	fd, err := createUdpSocket(session.parameter, &udp_address)
	if err != nil {
		return Warn("Could not create UDP socket", err)
	}
	session.transfer.udp_fd = fd
	session.transfer.udp_address = &udp_address
	return nil
}