aboutsummaryrefslogtreecommitdiff
path: root/src/gnunet/message/msg_namecache.go
blob: 517f11b9c3bf1b5c7e0bdafa12eeb3af071ea2a8 (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
// 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 message

import (
	"encoding/hex"
	"fmt"
	"gnunet/crypto"
	"gnunet/service/dht/blocks"
	"gnunet/util"
)

//----------------------------------------------------------------------
// NAMECACHE_LOOKUP_BLOCK
//----------------------------------------------------------------------

// NamecacheLookupMsg is request message for lookups in local namecache
type NamecacheLookupMsg struct {
	MsgSize uint16           `order:"big"` // total size of message
	MsgType uint16           `order:"big"` // NAMECACHE_LOOKUP_BLOCK (431)
	ID      uint32           `order:"big"` // Request Id
	Query   *crypto.HashCode // Query data
}

// NewNamecacheLookupMsg creates a new default message.
func NewNamecacheLookupMsg(query *crypto.HashCode) *NamecacheLookupMsg {
	if query == nil {
		query = crypto.NewHashCode(nil)
	}
	return &NamecacheLookupMsg{
		MsgSize: 72,
		MsgType: NAMECACHE_LOOKUP_BLOCK,
		ID:      0,
		Query:   query,
	}
}

// String returns a human-readable representation of the message.
func (m *NamecacheLookupMsg) String() string {
	return fmt.Sprintf("NamecacheLookupMsg{Id=%d,Query=%s}",
		m.ID, hex.EncodeToString(m.Query.Bits))
}

// Header returns the message header in a separate instance.
func (m *NamecacheLookupMsg) Header() *Header {
	return &Header{m.MsgSize, m.MsgType}
}

//----------------------------------------------------------------------
// NAMECACHE_LOOKUP_BLOCK_RESPONSE
//----------------------------------------------------------------------

// NamecacheLookupResultMsg is the response message for namecache lookups.
type NamecacheLookupResultMsg struct {
	MsgSize       uint16                `order:"big"` // total size of message
	MsgType       uint16                `order:"big"` // NAMECACHE_LOOKUP_BLOCK_RESPONSE (432)
	ID            uint32                `order:"big"` // Request Id
	Expire        util.AbsoluteTime     ``            // Expiration time
	DerivedKeySig *crypto.ZoneSignature ``            // Derived public key
	EncData       []byte                `size:"*"`    // Encrypted block data
}

// NewNamecacheLookupResultMsg creates a new default message.
func NewNamecacheLookupResultMsg() *NamecacheLookupResultMsg {
	return &NamecacheLookupResultMsg{
		MsgSize:       112,
		MsgType:       NAMECACHE_LOOKUP_BLOCK_RESPONSE,
		ID:            0,
		Expire:        *new(util.AbsoluteTime),
		DerivedKeySig: nil,
		EncData:       nil,
	}
}

// String returns a human-readable representation of the message.
func (m *NamecacheLookupResultMsg) String() string {
	return fmt.Sprintf("NamecacheLookupResultMsg{id=%d,expire=%s}",
		m.ID, m.Expire)
}

// Header returns the message header in a separate instance.
func (m *NamecacheLookupResultMsg) Header() *Header {
	return &Header{m.MsgSize, m.MsgType}
}

//----------------------------------------------------------------------
// NAMECACHE_CACHE_BLOCK
//----------------------------------------------------------------------

// NamecacheCacheMsg is the request message to put a name into the local cache.
type NamecacheCacheMsg struct {
	MsgSize       uint16                `order:"big"` // total size of message
	MsgType       uint16                `order:"big"` // NAMECACHE_CACHE_BLOCK (433)
	ID            uint32                `order:"big"` // Request Id
	Expire        util.AbsoluteTime     ``            // Expiration time
	DerivedKeySig *crypto.ZoneSignature ``            // Derived public key and signature
	EncData       []byte                `size:"*"`    // Encrypted block data
}

// NewNamecacheCacheMsg creates a new default message.
func NewNamecacheCacheMsg(block *blocks.GNSBlock) *NamecacheCacheMsg {
	msg := &NamecacheCacheMsg{
		MsgSize:       108,
		MsgType:       NAMECACHE_BLOCK_CACHE,
		ID:            0,
		Expire:        *new(util.AbsoluteTime),
		DerivedKeySig: nil,
		EncData:       make([]byte, 0),
	}
	if block != nil {
		msg.DerivedKeySig = block.DerivedKeySig
		msg.Expire = block.Body.Expire
		size := len(block.Body.Data)
		msg.EncData = make([]byte, size)
		copy(msg.EncData, block.Body.Data)
		msg.MsgSize += uint16(size)
	}
	return msg
}

// String returns a human-readable representation of the message.
func (m *NamecacheCacheMsg) String() string {
	return fmt.Sprintf("NewNamecacheCacheMsg{id=%d,expire=%s}",
		m.ID, m.Expire)
}

// Header returns the message header in a separate instance.
func (m *NamecacheCacheMsg) Header() *Header {
	return &Header{m.MsgSize, m.MsgType}
}

//----------------------------------------------------------------------
// NAMECACHE_BLOCK_CACHE_RESPONSE
//----------------------------------------------------------------------

// NamecacheCacheResponseMsg is the reponse message for a put request
type NamecacheCacheResponseMsg struct {
	MsgSize uint16 `order:"big"` // total size of message
	MsgType uint16 `order:"big"` // NAMECACHE_LOOKUP_BLOCK_RESPONSE (432)
	ID      uint32 `order:"big"` // Request Id
	Result  int32  `order:"big"` // Result code
}

// NewNamecacheCacheResponseMsg creates a new default message.
func NewNamecacheCacheResponseMsg() *NamecacheCacheResponseMsg {
	return &NamecacheCacheResponseMsg{
		MsgSize: 12,
		MsgType: NAMECACHE_BLOCK_CACHE_RESPONSE,
		ID:      0,
		Result:  0,
	}
}

// String returns a human-readable representation of the message.
func (m *NamecacheCacheResponseMsg) String() string {
	return fmt.Sprintf("NamecacheCacheResponseMsg{id=%d,result=%d}",
		m.ID, m.Result)
}

// Header returns the message header in a separate instance.
func (m *NamecacheCacheResponseMsg) Header() *Header {
	return &Header{m.MsgSize, m.MsgType}
}