This repository has been archived by the owner on May 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ipfs_bindings.go
336 lines (271 loc) · 7.71 KB
/
ipfs_bindings.go
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Useful links:
// https://github.com/ipfs/go-ipfs/issues/3060
// https://github.com/ipfs/examples/tree/master/examples
package main
import (
"fmt"
"context"
"os"
"bytes"
"sort"
"unsafe"
"time"
"io"
"strings"
"io/ioutil"
core "github.com/ipfs/go-ipfs/core"
repo "github.com/ipfs/go-ipfs/repo"
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
config "github.com/ipfs/go-ipfs/repo/config"
path "github.com/ipfs/go-ipfs/path"
"github.com/ipfs/go-ipfs/core/coreunix"
peer "gx/ipfs/QmZoWKhxUmZ2seW4BzX6fJkNR8hh9PsGModr7q171yq2SS/go-libp2p-peer"
)
// #cgo CFLAGS: -DIN_GO=1 -ggdb -I ${SRCDIR}/../../include
// #cgo android LDFLAGS: -Wl,--unresolved-symbols=ignore-all
//#include <stdlib.h>
//#include <stddef.h>
//#include <stdint.h>
//#include <ipfs_cache/ipfs_error_codes.h>
//
//// Don't export these functions into C or we'll get "unused function" warnings
//// (Or errors saying functions are defined more than once if the're not static).
//
//#if IN_GO
//static void execute_void_cb(void* func, int err, void* arg)
//{
// ((void(*)(int, void*)) func)(err, arg);
//}
//static void execute_data_cb(void* func, int err, void* data, size_t size, void* arg)
//{
// ((void(*)(int, char*, size_t, void*)) func)(err, data, size, arg);
//}
//#endif // if IN_GO
import "C"
const (
nBitsForKeypair = 2048
repoRoot = "./repo"
debug = false
// This next option makes IPNS resolution much faster:
//
// https://blog.ipfs.io/34-go-ipfs-0.4.14#ipns-improvements
//
// But:
//
// 1. It's an experimental feature which may result in inefficient message
// flooding when many users are participating. Thus it's disabled by
// default. Use only for debugging (if at all).
//
// 2. It doesn't help with the first publish and first resolve, though
// it seems that once the publisher and resolver found their places
// in the DHT, the two operations become almost instant.
enablePubSubIPNS = true
)
func main() {
}
func doesnt_exist_or_is_empty(path string) bool {
f, err := os.Open(path)
if err != nil {
if os.IsNotExist(err) {
return true
}
return false
}
defer f.Close()
_, err = f.Readdirnames(1)
if err == io.EOF {
return true
}
return false
}
// "/ip4/0.0.0.0/tcp/4001" -> "/ip4/0.0.0.0/tcp/0"
// "/ip6/::/tcp/4001" -> "/ip6/::/tcp/0"
func setRandomPort(ep string) string {
parts := strings.Split(ep, "/")
l := len(parts);
if l == 0 { return ep }
parts[l-1] = "0"
return strings.Join(parts, "/")
}
func openOrCreateRepo(repoRoot string) (repo.Repo, error) {
if doesnt_exist_or_is_empty(repoRoot) {
conf, err := config.Init(os.Stdout, nBitsForKeypair)
if err != nil {
return nil, err
}
// Don't use hardcoded swarm ports (usually 4001), otherwise
// we wouldn't be able to run multiple IPFS instances on the
// same PC.
for i, addr := range conf.Addresses.Swarm {
conf.Addresses.Swarm[i] = setRandomPort(addr)
}
if err := fsrepo.Init(repoRoot, conf); err != nil {
return nil, err
}
}
return fsrepo.Open(repoRoot)
}
func printSwarmAddrs(node *core.IpfsNode) {
if !node.OnlineMode() {
fmt.Println("Swarm not listening, running in offline mode.")
return
}
var addrs []string
for _, addr := range node.PeerHost.Addrs() {
addrs = append(addrs, addr.String())
}
sort.Sort(sort.StringSlice(addrs))
for _, addr := range addrs {
fmt.Printf("Swarm listening on %s\n", addr)
}
}
type Cache struct {
node *core.IpfsNode
ctx context.Context
cancel context.CancelFunc
}
var g Cache
func start_cache(repoRoot string) C.int {
g.ctx, g.cancel = context.WithCancel(context.Background())
r, err := openOrCreateRepo(repoRoot);
if err != nil {
fmt.Println("err", err);
return C.IPFS_FAILED_TO_CREATE_REPO
}
g.node, err = core.NewNode(g.ctx, &core.BuildCfg{
Online: true,
Permanent: true,
Repo: r,
ExtraOpts: map[string]bool{
"ipnsps": enablePubSubIPNS,
},
})
g.node.SetLocal(false)
printSwarmAddrs(g.node)
return C.IPFS_SUCCESS
}
//export go_ipfs_cache_start
func go_ipfs_cache_start(c_repoPath *C.char) C.int {
repoRoot := C.GoString(c_repoPath)
return start_cache(repoRoot);
}
//export go_ipfs_cache_async_start
func go_ipfs_cache_async_start(c_repoPath *C.char, fn unsafe.Pointer, fn_arg unsafe.Pointer) {
repoRoot := C.GoString(c_repoPath)
go func() {
err := start_cache(repoRoot);
C.execute_void_cb(fn, err, fn_arg)
}()
}
//export go_ipfs_cache_stop
func go_ipfs_cache_stop() {
g.cancel()
}
//export go_ipfs_cache_resolve
func go_ipfs_cache_resolve(c_ipns_id *C.char, fn unsafe.Pointer, fn_arg unsafe.Pointer) {
ipns_id := C.GoString(c_ipns_id)
go func() {
if debug {
fmt.Println("go_ipfs_cache_resolve start");
defer fmt.Println("go_ipfs_cache_resolve end");
}
ctx := g.ctx
n := g.node
p := path.Path("/ipns/" + ipns_id)
node, err := core.Resolve(ctx, n.Namesys, n.Resolver, p)
if err != nil {
C.execute_data_cb(fn, C.IPFS_RESOLVE_FAILED, nil, C.size_t(0), fn_arg)
return
}
data := []byte(node.Cid().String())
cdata := C.CBytes(data)
defer C.free(cdata)
C.execute_data_cb(fn, C.IPFS_SUCCESS, cdata, C.size_t(len(data)), fn_arg)
}()
}
// IMPORTANT: The returned value needs to be explicitly `free`d.
//export go_ipfs_cache_ipns_id
func go_ipfs_cache_ipns_id() *C.char {
pid, err := peer.IDFromPrivateKey(g.node.PrivateKey)
if err != nil {
return nil
}
cstr := C.CString(pid.Pretty())
return cstr
}
func publish(ctx context.Context, duration time.Duration, n *core.IpfsNode, cid string) error {
path, err := path.ParseCidToPath(cid)
if err != nil {
fmt.Println("go_ipfs_cache_publish failed to parse cid \"", cid, "\"");
return err
}
k := n.PrivateKey
eol := time.Now().Add(duration)
err = n.Namesys.PublishWithEOL(ctx, k, path, eol)
if err != nil {
fmt.Println("go_ipfs_cache_publish failed");
return err
}
return nil
}
//export go_ipfs_cache_publish
func go_ipfs_cache_publish(cid *C.char, seconds C.int64_t, fn unsafe.Pointer, fn_arg unsafe.Pointer) {
id := C.GoString(cid)
go func() {
if debug {
fmt.Println("go_ipfs_cache_publish start");
defer fmt.Println("go_ipfs_cache_publish end");
}
// https://stackoverflow.com/questions/17573190/how-to-multiply-duration-by-integer
err := publish(g.ctx, time.Duration(seconds) * time.Second, g.node, id);
if err != nil {
C.execute_void_cb(fn, C.IPFS_PUBLISH_FAILED, fn_arg)
return
}
C.execute_void_cb(fn, C.IPFS_SUCCESS, fn_arg)
}()
}
//export go_ipfs_cache_add
func go_ipfs_cache_add(data unsafe.Pointer, size C.size_t, fn unsafe.Pointer, fn_arg unsafe.Pointer) {
msg := C.GoBytes(data, C.int(size))
go func() {
if debug {
fmt.Println("go_ipfs_cache_add start");
defer fmt.Println("go_ipfs_cache_add end");
}
cid, err := coreunix.Add(g.node, bytes.NewReader(msg))
if err != nil {
fmt.Println("Error: failed to insert content ", err)
C.execute_data_cb(fn, C.IPFS_ADD_FAILED, nil, C.size_t(0), fn_arg)
return;
}
cdata := C.CBytes([]byte(cid))
defer C.free(cdata)
C.execute_data_cb(fn, C.IPFS_SUCCESS, cdata, C.size_t(len(cid)), fn_arg)
}()
}
//export go_ipfs_cache_cat
func go_ipfs_cache_cat(c_cid *C.char, fn unsafe.Pointer, fn_arg unsafe.Pointer) {
cid := C.GoString(c_cid)
go func() {
if debug {
fmt.Println("go_ipfs_cache_cat start");
defer fmt.Println("go_ipfs_cache_cat end");
}
reader, err := coreunix.Cat(g.ctx, g.node, cid)
if err != nil {
fmt.Println("go_ipfs_cache_cat failed to Cat");
C.execute_data_cb(fn, C.IPFS_CAT_FAILED, nil, C.size_t(0), fn_arg)
return
}
bytes, err := ioutil.ReadAll(reader)
if err != nil {
fmt.Println("go_ipfs_cache_cat failed to read");
C.execute_data_cb(fn, C.IPFS_READ_FAILED, nil, C.size_t(0), fn_arg)
return
}
cdata := C.CBytes(bytes)
defer C.free(cdata)
C.execute_data_cb(fn, C.IPFS_SUCCESS, cdata, C.size_t(len(bytes)), fn_arg)
}()
}