diff options
Diffstat (limited to 'src/gnunet/service/revocation/service.go')
-rw-r--r-- | src/gnunet/service/revocation/service.go | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/src/gnunet/service/revocation/service.go b/src/gnunet/service/revocation/service.go new file mode 100644 index 0000000..f2a6ec3 --- /dev/null +++ b/src/gnunet/service/revocation/service.go | |||
@@ -0,0 +1,160 @@ | |||
1 | // This file is part of gnunet-go, a GNUnet-implementation in Golang. | ||
2 | // Copyright (C) 2019, 2020 Bernd Fix >Y< | ||
3 | // | ||
4 | // gnunet-go is free software: you can redistribute it and/or modify it | ||
5 | // under the terms of the GNU Affero General Public License as published | ||
6 | // by the Free Software Foundation, either version 3 of the License, | ||
7 | // or (at your option) any later version. | ||
8 | // | ||
9 | // gnunet-go is distributed in the hope that it will be useful, but | ||
10 | // WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | // Affero General Public License for more details. | ||
13 | // | ||
14 | // You should have received a copy of the GNU Affero General Public License | ||
15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
16 | // | ||
17 | // SPDX-License-Identifier: AGPL3.0-or-later | ||
18 | |||
19 | package revocation | ||
20 | |||
21 | import ( | ||
22 | "io" | ||
23 | |||
24 | "gnunet/message" | ||
25 | "gnunet/service" | ||
26 | "gnunet/transport" | ||
27 | |||
28 | "github.com/bfix/gospel/crypto/ed25519" | ||
29 | "github.com/bfix/gospel/logger" | ||
30 | ) | ||
31 | |||
32 | //---------------------------------------------------------------------- | ||
33 | // "GNUnet Revocation" service implementation | ||
34 | //---------------------------------------------------------------------- | ||
35 | |||
36 | // RevocationService | ||
37 | type RevocationService struct { | ||
38 | RevocationModule | ||
39 | } | ||
40 | |||
41 | // NewRevocationService | ||
42 | func NewRevocationService() service.Service { | ||
43 | // instantiate service and assemble a new Revocation handler. | ||
44 | inst := new(RevocationService) | ||
45 | return inst | ||
46 | } | ||
47 | |||
48 | // Start the Revocation service | ||
49 | func (s *RevocationService) Start(spec string) error { | ||
50 | return nil | ||
51 | } | ||
52 | |||
53 | // Stop the Revocation service | ||
54 | func (s *RevocationService) Stop() error { | ||
55 | return nil | ||
56 | } | ||
57 | |||
58 | // Serve a client channel. | ||
59 | func (s *RevocationService) ServeClient(ctx *service.SessionContext, mc *transport.MsgChannel) { | ||
60 | |||
61 | reqId := 0 | ||
62 | loop: | ||
63 | for { | ||
64 | // receive next message from client | ||
65 | reqId++ | ||
66 | logger.Printf(logger.DBG, "[revocation:%d:%d] Waiting for client request...\n", ctx.Id, reqId) | ||
67 | msg, err := mc.Receive(ctx.Signaller()) | ||
68 | if err != nil { | ||
69 | if err == io.EOF { | ||
70 | logger.Printf(logger.INFO, "[revocation:%d:%d] Client channel closed.\n", ctx.Id, reqId) | ||
71 | } else if err == transport.ErrChannelInterrupted { | ||
72 | logger.Printf(logger.INFO, "[revocation:%d:%d] Service operation interrupted.\n", ctx.Id, reqId) | ||
73 | } else { | ||
74 | logger.Printf(logger.ERROR, "[revocation:%d:%d] Message-receive failed: %s\n", ctx.Id, reqId, err.Error()) | ||
75 | } | ||
76 | break loop | ||
77 | } | ||
78 | logger.Printf(logger.INFO, "[revocation:%d:%d] Received request: %v\n", ctx.Id, reqId, msg) | ||
79 | |||
80 | // handle request | ||
81 | switch m := msg.(type) { | ||
82 | case *message.RevocationQueryMsg: | ||
83 | //---------------------------------------------------------- | ||
84 | // REVOCATION_QUERY | ||
85 | //---------------------------------------------------------- | ||
86 | go func(id int, m *message.RevocationQueryMsg) { | ||
87 | logger.Printf(logger.INFO, "[revocation:%d:%d] Query request received.\n", ctx.Id, id) | ||
88 | var resp *message.RevocationQueryResponseMsg | ||
89 | ctx.Add() | ||
90 | defer func() { | ||
91 | // send response | ||
92 | if resp != nil { | ||
93 | if err := mc.Send(resp, ctx.Signaller()); err != nil { | ||
94 | logger.Printf(logger.ERROR, "[revocation:%d:%d] Failed to send response: %s\n", ctx.Id, id, err.Error()) | ||
95 | } | ||
96 | } | ||
97 | // go-routine finished | ||
98 | logger.Printf(logger.DBG, "[revocation:%d:%d] Query request finished.\n", ctx.Id, id) | ||
99 | ctx.Remove() | ||
100 | }() | ||
101 | |||
102 | pkey := ed25519.NewPublicKeyFromBytes(m.Zone) | ||
103 | valid, err := s.Query(ctx, pkey) | ||
104 | if err != nil { | ||
105 | logger.Printf(logger.ERROR, "[revocation:%d:%d] Failed to query revocation status: %s\n", ctx.Id, id, err.Error()) | ||
106 | if err == transport.ErrChannelInterrupted { | ||
107 | resp = nil | ||
108 | } | ||
109 | return | ||
110 | } | ||
111 | resp = message.NewRevocationQueryResponseMsg(valid) | ||
112 | }(reqId, m) | ||
113 | |||
114 | case *message.RevocationRevokeMsg: | ||
115 | //---------------------------------------------------------- | ||
116 | // REVOCATION_REVOKE | ||
117 | //---------------------------------------------------------- | ||
118 | go func(id int, m *message.RevocationRevokeMsg) { | ||
119 | logger.Printf(logger.INFO, "[revocation:%d:%d] Revoke request received.\n", ctx.Id, id) | ||
120 | var resp *message.RevocationRevokeResponseMsg | ||
121 | ctx.Add() | ||
122 | defer func() { | ||
123 | // send response | ||
124 | if resp != nil { | ||
125 | if err := mc.Send(resp, ctx.Signaller()); err != nil { | ||
126 | logger.Printf(logger.ERROR, "[revocation:%d:%d] Failed to send response: %s\n", ctx.Id, id, err.Error()) | ||
127 | } | ||
128 | } | ||
129 | // go-routine finished | ||
130 | logger.Printf(logger.DBG, "[revocation:%d:%d] Revoke request finished.\n", ctx.Id, id) | ||
131 | ctx.Remove() | ||
132 | }() | ||
133 | |||
134 | rd := NewRevDataFromMsg(m) | ||
135 | valid, err := s.Revoke(ctx, rd) | ||
136 | if err != nil { | ||
137 | logger.Printf(logger.ERROR, "[revocation:%d:%d] Failed to revoke key: %s\n", ctx.Id, id, err.Error()) | ||
138 | if err == transport.ErrChannelInterrupted { | ||
139 | resp = nil | ||
140 | } | ||
141 | return | ||
142 | } | ||
143 | resp = message.NewRevocationRevokeResponseMsg(valid) | ||
144 | }(reqId, m) | ||
145 | |||
146 | default: | ||
147 | //---------------------------------------------------------- | ||
148 | // UNKNOWN message type received | ||
149 | //---------------------------------------------------------- | ||
150 | logger.Printf(logger.ERROR, "[revocation:%d:%d] Unhandled message of type (%d)\n", ctx.Id, reqId, msg.Header().MsgType) | ||
151 | break loop | ||
152 | } | ||
153 | } | ||
154 | // close client connection | ||
155 | mc.Close() | ||
156 | |||
157 | // cancel all tasks running for this session/connection | ||
158 | logger.Printf(logger.INFO, "[revocation:%d] Start closing session... [%d]\n", ctx.Id, ctx.Waiting()) | ||
159 | ctx.Cancel() | ||
160 | } | ||