github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/primitive/inet_addr.go (about)

     1  // Copyright 2020 DataStax
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package primitive
    16  
    17  import (
    18  	"errors"
    19  	"fmt"
    20  	"io"
    21  	"net"
    22  )
    23  
    24  // [inetaddr] is modeled by net.IP
    25  
    26  func ReadInetAddr(source io.Reader) (net.IP, error) {
    27  	if length, err := ReadByte(source); err != nil {
    28  		return nil, fmt.Errorf("cannot read [inetaddr] length: %w", err)
    29  	} else {
    30  		if length == net.IPv4len {
    31  			decoded := make([]byte, net.IPv4len)
    32  			if _, err := io.ReadFull(source, decoded); err != nil {
    33  				return nil, fmt.Errorf("cannot read [inetaddr] IPv4 content: %w", err)
    34  			}
    35  			return net.IPv4(decoded[0], decoded[1], decoded[2], decoded[3]), nil
    36  		} else if length == net.IPv6len {
    37  			decoded := make([]byte, net.IPv6len)
    38  			if _, err := io.ReadFull(source, decoded); err != nil {
    39  				return nil, fmt.Errorf("cannot read [inetaddr] IPv6 content: %w", err)
    40  			}
    41  			return decoded, nil
    42  		} else {
    43  			return nil, errors.New("unknown inet address length: " + string(length))
    44  		}
    45  	}
    46  }
    47  
    48  func WriteInetAddr(inetAddr net.IP, dest io.Writer) error {
    49  	if inetAddr == nil {
    50  		return errors.New("cannot write nil [inetaddr]")
    51  	}
    52  	var length byte
    53  	if inetAddr.To4() != nil {
    54  		length = net.IPv4len
    55  	} else {
    56  		length = net.IPv6len
    57  	}
    58  	if err := WriteByte(length, dest); err != nil {
    59  		return fmt.Errorf("cannot write [inetaddr] length: %w", err)
    60  	}
    61  	if length == net.IPv4len {
    62  		if n, err := dest.Write(inetAddr.To4()); err != nil {
    63  			return fmt.Errorf("cannot write [inetaddr] IPv4 content: %w", err)
    64  		} else if n < net.IPv4len {
    65  			return errors.New("not enough capacity to write [inetaddr] IPv4 content")
    66  		}
    67  	} else {
    68  		if n, err := dest.Write(inetAddr.To16()); err != nil {
    69  			return fmt.Errorf("cannot write [inetaddr] IPv6 content: %w", err)
    70  		} else if n < net.IPv6len {
    71  			return errors.New("not enough capacity to write [inetaddr] IPv content")
    72  		}
    73  	}
    74  	return nil
    75  }
    76  
    77  func LengthOfInetAddr(inetAddr net.IP) (length int, err error) {
    78  	if inetAddr == nil {
    79  		return -1, errors.New("cannot compute nil [inetaddr] length")
    80  	}
    81  	length = LengthOfByte
    82  	if inetAddr.To4() != nil {
    83  		length += net.IPv4len
    84  	} else {
    85  		length += net.IPv6len
    86  	}
    87  	return length, nil
    88  }