aboutsummaryrefslogtreecommitdiff
path: root/src/gnunet/service/dht/blocks/generic.go
blob: 2962025e6e4a2f4bd36ea99f37d4d694ab2ee527 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// This file is part of gnunet-go, a GNUnet-implementation in Golang.
// Copyright (C) 2019-2022 Bernd Fix  >Y<
//
// gnunet-go is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// gnunet-go is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: AGPL3.0-or-later

package blocks

import (
	"encoding/hex"
	"fmt"
	"gnunet/crypto"
	"gnunet/enums"
	"gnunet/util"

	"github.com/bfix/gospel/data"
)

//----------------------------------------------------------------------
// Query/Block interfaces for generic DHT handling
//----------------------------------------------------------------------

// DHT Query interface
type Query interface {

	// Key returns the DHT key for a block
	Key() *crypto.HashCode

	// Type returns the requested block type
	Type() uint16

	// Flags returns the query flags
	Flags() uint16

	// Verify the integrity of a retrieved block (optional). Override in
	// custom query types to implement block-specific integrity checks
	// (see GNSQuery for example).
	Verify(blk Block) error

	// Decrypt block content (optional). Override in custom query types to
	// implement block-specific encryption (see GNSQuery for example).
	Decrypt(blk Block) error

	// String returns the human-readable representation of a query
	String() string
}

// DHT Block interface
type Block interface {

	// Data returns the DHT block data (unstructured without type and
	// expiration information.
	Data() []byte

	// Return the block type
	Type() uint16

	// Expire returns the block expiration
	Expire() util.AbsoluteTime

	// Verify the integrity of a block (optional). Override in custom query
	// types to implement block-specific integrity checks (see GNSBlock for
	// example). This verification is usually weaker than the verification
	// method from a Query (see GNSBlock.Verify for explanation).
	Verify() (bool, error)

	// String returns the human-readable representation of a block
	String() string
}

// Unwrap (raw) block to a specific block type
func Unwrap(blk Block, obj interface{}) error {
	return data.Unmarshal(obj, blk.Data())
}

//----------------------------------------------------------------------
// Generic interface implementations without persistent attributes
//----------------------------------------------------------------------

// GenericQuery is the binary representation of a DHT key
type GenericQuery struct {
	// Key for repository queries (local/remote)
	key *crypto.HashCode

	// block type requested
	btype uint16

	// query flags
	flags uint16

	// Params holds additional query parameters
	Params util.ParameterSet
}

// Key interface method implementation
func (q *GenericQuery) Key() *crypto.HashCode {
	return q.key
}

// Type returns the requested block type
func (q *GenericQuery) Type() uint16 {
	return q.btype
}

// Flags returns the query flags
func (q *GenericQuery) Flags() uint16 {
	return q.flags
}

// Verify interface method implementation
func (q *GenericQuery) Verify(b Block) error {
	// no verification, no errors ;)
	return nil
}

// Decrypt interface method implementation
func (q *GenericQuery) Decrypt(b Block) error {
	// no decryption, no errors ;)
	return nil
}

// String returns the human-readable representation of a block
func (q *GenericQuery) String() string {
	return fmt.Sprintf("GenericQuery{btype=%d,key=%s}", q.btype, hex.EncodeToString(q.Key().Bits))
}

// NewGenericQuery creates a simple Query from hash code.
func NewGenericQuery(key []byte, btype enums.BlockType, flags uint16) *GenericQuery {
	return &GenericQuery{
		key:    crypto.NewHashCode(key),
		btype:  uint16(btype),
		flags:  flags,
		Params: make(util.ParameterSet),
	}
}

//----------------------------------------------------------------------

// GenericBlock is the block in simple binary representation
type GenericBlock struct {
	block  []byte            // block data
	btype  uint16            // block type
	expire util.AbsoluteTime // expiration date
}

// Data interface method implementation
func (b *GenericBlock) Data() []byte {
	return b.block
}

// Type returns the block type
func (b *GenericBlock) Type() uint16 {
	return b.btype
}

// Expire returns the block expiration
func (b *GenericBlock) Expire() util.AbsoluteTime {
	return b.expire
}

// String returns the human-readable representation of a block
func (b *GenericBlock) String() string {
	return fmt.Sprintf("GenericBlock{type=%d,expires=%s,data=[%d]}",
		b.btype, b.expire.String(), len(b.block))
}

// Verify interface method implementation
func (b *GenericBlock) Verify() (bool, error) {
	// no verification, no errors ;)
	return true, nil
}

// NewGenericBlock creates a Block from binary data.
func NewGenericBlock(buf []byte) *GenericBlock {
	return &GenericBlock{
		block:  util.Clone(buf),
		btype:  uint16(enums.BLOCK_TYPE_ANY), // unknown block type
		expire: util.AbsoluteTimeNever(),     // never expires
	}
}