follower.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. package main
  2. //#cgo CFLAGS: -fopenmp -O2
  3. //#cgo LDFLAGS: -lcrypto -lm -fopenmp
  4. //#include "../c/dpf.h"
  5. //#include "../c/okv.h"
  6. //#include "../c/dpf.c"
  7. //#include "../c/okv.c"
  8. import "C"
  9. //sssssssssssssssss
  10. import (
  11. "2PPS/lib"
  12. "crypto/rand"
  13. "crypto/rsa"
  14. "crypto/sha256"
  15. "crypto/tls"
  16. "crypto/x509"
  17. "crypto/x509/pkix"
  18. "encoding/pem"
  19. "fmt"
  20. "math/big"
  21. "net"
  22. "strconv"
  23. "sync"
  24. "time"
  25. "unsafe"
  26. "golang.org/x/crypto/nacl/box"
  27. )
  28. //this stores all neccessary information for each client
  29. type clientKeys struct {
  30. SharedSecret [32]byte
  31. PirQuery [][]byte
  32. }
  33. //uses clients publicKey as key
  34. var clientData = make(map[[32]byte]clientKeys)
  35. var topicList []byte
  36. var topicAmount int
  37. var followerPrivateKey *[32]byte
  38. var followerPublicKey *[32]byte
  39. var leaderPublicKey *[32]byte
  40. //needs to be changed at leader/follower/client at the same time
  41. const neededSubscriptions = 1
  42. const dataLength = 64
  43. const numThreads = 12
  44. var dbWriteSize int = 4
  45. var maxTimePerRound time.Duration = 5 * time.Second
  46. var round int = 1
  47. var startTime time.Time
  48. var ignoreMe []byte
  49. func main() {
  50. generatedPublicKey, generatedPrivateKey, err := box.GenerateKey(rand.Reader)
  51. if err != nil {
  52. panic(err)
  53. }
  54. followerPrivateKey = generatedPrivateKey
  55. followerPublicKey = generatedPublicKey
  56. C.initializeServer(C.int(numThreads))
  57. followerConnectionPrivateKey, err := rsa.GenerateKey(rand.Reader, 2048)
  58. if err != nil {
  59. panic(err)
  60. }
  61. // Generate a pem block with the private key
  62. keyPem := pem.EncodeToMemory(&pem.Block{
  63. Type: "RSA PRIVATE KEY",
  64. Bytes: x509.MarshalPKCS1PrivateKey(followerConnectionPrivateKey),
  65. })
  66. tml := x509.Certificate{
  67. // you can add any attr that you need
  68. NotBefore: time.Now(),
  69. NotAfter: time.Now().AddDate(5, 0, 0),
  70. // you have to generate a different serial number each execution
  71. SerialNumber: big.NewInt(123123),
  72. Subject: pkix.Name{
  73. CommonName: "New Name",
  74. Organization: []string{"New Org."},
  75. },
  76. BasicConstraintsValid: true,
  77. }
  78. cert, err := x509.CreateCertificate(rand.Reader, &tml, &tml, &followerConnectionPrivateKey.PublicKey, followerConnectionPrivateKey)
  79. if err != nil {
  80. panic(err)
  81. }
  82. // Generate a pem block with the certificate
  83. certPem := pem.EncodeToMemory(&pem.Block{
  84. Type: "CERTIFICATE",
  85. Bytes: cert,
  86. })
  87. tlsCert, err := tls.X509KeyPair(certPem, keyPem)
  88. if err != nil {
  89. panic(err)
  90. }
  91. config := &tls.Config{Certificates: []tls.Certificate{tlsCert}}
  92. fmt.Println("start leader")
  93. //listens for leader
  94. lnLeader, err := tls.Listen("tcp", ":4442", config)
  95. if err != nil {
  96. panic(err)
  97. }
  98. defer lnLeader.Close()
  99. leaderConnection, err := lnLeader.Accept()
  100. if err != nil {
  101. panic(err)
  102. }
  103. //send publicKey to leader
  104. writeTo(leaderConnection, followerPublicKey[:])
  105. //receives leader PublicKey
  106. var tmpLeaderPubKey [32]byte
  107. _, err = leaderConnection.Read(tmpLeaderPubKey[:])
  108. if err != nil {
  109. panic(err)
  110. }
  111. leaderPublicKey = &tmpLeaderPubKey
  112. //setup ends here
  113. //locks access to DB
  114. m := &sync.RWMutex{}
  115. wg := &sync.WaitGroup{}
  116. for {
  117. fmt.Println("Phase 1")
  118. //create write db for this round
  119. for i := 0; i < dbWriteSize; i++ {
  120. C.createDb(C.int(0), C.int(dataLength))
  121. }
  122. //receives the virtualAddresses
  123. virtualAddresses := make([]int, dbWriteSize+1)
  124. for i := 0; i <= dbWriteSize; i++ {
  125. virtualAddress := readFrom(leaderConnection, 4)
  126. virtualAddresses[i] = byteToInt(virtualAddress)
  127. }
  128. for i := 0; i < numThreads; i++ {
  129. wg.Add(1)
  130. leaderConnection, err := lnLeader.Accept()
  131. if err != nil {
  132. panic(err)
  133. }
  134. leaderConnection.SetDeadline(time.Time{})
  135. startTime = time.Now()
  136. go phase1(i, leaderConnection, m, wg, virtualAddresses)
  137. }
  138. wg.Wait()
  139. fmt.Println("Phase 2")
  140. leaderConnection, err := lnLeader.Accept()
  141. if err != nil {
  142. panic(err)
  143. }
  144. leaderConnection.SetDeadline(time.Time{})
  145. phase2(leaderConnection)
  146. fmt.Println("Phase 3")
  147. if round == 1 {
  148. //addTestTweets()
  149. }
  150. //no tweets -> continue to phase 1 and mb get tweets
  151. topicList, topicAmount = lib.GetTopicList(0)
  152. if len(topicList) == 0 {
  153. continue
  154. }
  155. for i := 0; i < numThreads; i++ {
  156. wg.Add(1)
  157. leaderConnection, err := lnLeader.Accept()
  158. if err != nil {
  159. panic(err)
  160. }
  161. leaderConnection.SetDeadline(time.Time{})
  162. startTime = time.Now()
  163. go phase3(leaderConnection, wg, m)
  164. }
  165. wg.Wait()
  166. lib.CleanUpdbR(round)
  167. round++
  168. }
  169. }
  170. func phase1(id int, leaderWorkerConnection net.Conn, m *sync.RWMutex, wg *sync.WaitGroup, virtualAddresses []int) {
  171. for {
  172. gotClient := readFrom(leaderWorkerConnection, 1)
  173. //this worker is done
  174. if gotClient[0] == 0 {
  175. wg.Done()
  176. return
  177. }
  178. //setup the worker-specific db
  179. dbSize := int(C.dbSize)
  180. db := make([][]byte, dbSize)
  181. for i := 0; i < dbSize; i++ {
  182. db[i] = make([]byte, int(C.db[i].dataSize))
  183. }
  184. //gets clients publicKey
  185. var clientPublicKey *[32]byte
  186. var tmpClientPublicKey [32]byte
  187. _, err := leaderWorkerConnection.Read(tmpClientPublicKey[:])
  188. if err != nil {
  189. fmt.Println("no error handling")
  190. panic(err)
  191. }
  192. clientPublicKey = &tmpClientPublicKey
  193. m.RLock()
  194. clientKeys := clientData[tmpClientPublicKey]
  195. m.RUnlock()
  196. clientKeys, pirQuery, errorBool := handlePirQuery(clientKeys, leaderWorkerConnection, 0, tmpClientPublicKey, true)
  197. if errorBool {
  198. continue
  199. }
  200. getSendVirtualAddress(pirQuery[0], virtualAddresses, clientKeys.SharedSecret, leaderWorkerConnection)
  201. m.Lock()
  202. clientData[*clientPublicKey] = clientKeys
  203. m.Unlock()
  204. //gets dpfQuery from leader
  205. dpfLengthBytes, errorBool := readFromWError(leaderWorkerConnection, 4)
  206. if errorBool {
  207. continue
  208. }
  209. dpfLength := byteToInt(dpfLengthBytes)
  210. dpfQueryBEncrypted, errorBool := readFromWError(leaderWorkerConnection, dpfLength)
  211. if errorBool {
  212. continue
  213. }
  214. //decrypt dpfQueryB for sorting into db
  215. var decryptNonce [24]byte
  216. copy(decryptNonce[:], dpfQueryBEncrypted[:24])
  217. dpfQueryB, ok := box.Open(nil, dpfQueryBEncrypted[24:], &decryptNonce, clientPublicKey, followerPrivateKey)
  218. if !ok {
  219. panic("dpfQueryB decryption not ok")
  220. }
  221. ds := int(C.db[0].dataSize)
  222. dataShareFollower := make([]byte, ds)
  223. pos := C.getUint128_t(C.int(virtualAddresses[dbWriteSize]))
  224. C.evalDPF(C.ctx[id], (*C.uchar)(&dpfQueryB[0]), pos, C.int(ds), (*C.uchar)(&dataShareFollower[0]))
  225. writeTo(leaderWorkerConnection, dataShareFollower)
  226. dataShareLeader, errorBool := readFromWError(leaderWorkerConnection, ds)
  227. if errorBool {
  228. continue
  229. }
  230. auditXOR := make([]byte, ds)
  231. passedAudit := true
  232. for i := 0; i < ds; i++ {
  233. auditXOR[i] = dataShareLeader[i] ^ dataShareFollower[i]
  234. //client tried to write to a position that is not a virtuallAddress
  235. if auditXOR[i] != 0 {
  236. passedAudit = false
  237. }
  238. }
  239. if passedAudit {
  240. //run dpf, xor into local db
  241. for i := 0; i < dbSize; i++ {
  242. ds := int(C.db[i].dataSize)
  243. dataShare := make([]byte, ds)
  244. pos := C.getUint128_t(C.int(virtualAddresses[i]))
  245. C.evalDPF(C.ctx[id], (*C.uchar)(&dpfQueryB[0]), pos, C.int(ds), (*C.uchar)(&dataShare[0]))
  246. for j := 0; j < ds; j++ {
  247. db[i][j] = db[i][j] ^ dataShare[j]
  248. }
  249. }
  250. //xor the worker's DB into the main DB
  251. for i := 0; i < dbSize; i++ {
  252. m.Lock()
  253. C.xorIn(C.int(i), (*C.uchar)(&db[i][0]))
  254. m.Unlock()
  255. }
  256. }
  257. }
  258. }
  259. func phase2(leaderWorkerConnection net.Conn) {
  260. //gets current seed
  261. seedFollower := make([]byte, 16)
  262. C.readSeed((*C.uchar)(&seedFollower[0]))
  263. //get data
  264. dbSize := int(C.dbSize)
  265. tmpdbFollower := make([][]byte, dbSize)
  266. for i := range tmpdbFollower {
  267. tmpdbFollower[i] = make([]byte, dataLength)
  268. }
  269. for i := 0; i < dbSize; i++ {
  270. C.readData(C.int(i), (*C.uchar)(&tmpdbFollower[i][0]))
  271. }
  272. //receive seed from leader
  273. seedLeader := readFrom(leaderWorkerConnection, 16)
  274. //receive data from leader
  275. tmpdbLeader := make([][]byte, dbSize)
  276. for i := range tmpdbLeader {
  277. tmpdbLeader[i] = make([]byte, dataLength)
  278. }
  279. for i := 0; i < dbSize; i++ {
  280. tmpdbLeader[i] = readFrom(leaderWorkerConnection, dataLength)
  281. }
  282. //writes seed to leader
  283. writeTo(leaderWorkerConnection, seedFollower)
  284. //write data to leader
  285. for i := 0; i < dbSize; i++ {
  286. writeTo(leaderWorkerConnection, tmpdbFollower[i])
  287. }
  288. //put together the db
  289. tmpdb := make([][]byte, dbSize)
  290. for i := range tmpdb {
  291. tmpdb[i] = make([]byte, dataLength)
  292. }
  293. //get own Ciphers
  294. ciphersFollowers := make([]*C.uchar, dbSize)
  295. for i := 0; i < dbSize; i++ {
  296. ciphersFollowers[i] = (*C.uchar)(C.malloc(16))
  297. }
  298. for i := 0; i < dbSize; i++ {
  299. C.getCipher(0, C.int(i), ciphersFollowers[i])
  300. }
  301. //receive ciphers from leader
  302. ciphersLeader := make([]byte, dbSize*16)
  303. for i := 0; i < dbSize; i++ {
  304. _, err := leaderWorkerConnection.Read(ciphersLeader[i*16:])
  305. if err != nil {
  306. panic(err)
  307. }
  308. }
  309. //send own Ciphers to leader
  310. for i := 0; i < dbSize; i++ {
  311. writeTo(leaderWorkerConnection, C.GoBytes(unsafe.Pointer(ciphersFollowers[i]), 16))
  312. }
  313. //put in ciphers from leader
  314. for i := 0; i < dbSize; i++ {
  315. C.putCipher(0, C.int(i), (*C.uchar)(&ciphersLeader[i*16]))
  316. }
  317. for i := 0; i < dbSize; i++ {
  318. C.decryptRow(C.int(i), (*C.uchar)(&tmpdb[i][0]), (*C.uchar)(&tmpdbLeader[i][0]), (*C.uchar)(&tmpdbFollower[i][0]), (*C.uchar)(&seedLeader[0]), (*C.uchar)(&seedFollower[0]))
  319. }
  320. var tweets []lib.Tweet
  321. for i := 0; i < dbSize; i++ {
  322. //discard cover message
  323. if tmpdb[i][0] == 0 {
  324. continue
  325. } else {
  326. //reconstruct tweet
  327. var position int = 0
  328. var topics []string
  329. var topic string
  330. var text string
  331. for _, letter := range tmpdb[i] {
  332. if string(letter) == ";" {
  333. if topic != "" {
  334. topics = append(topics, topic)
  335. topic = ""
  336. }
  337. position++
  338. } else {
  339. if position == 0 {
  340. if string(letter) == "," {
  341. topics = append(topics, topic)
  342. topic = ""
  343. } else {
  344. topic = topic + string(letter)
  345. }
  346. } else if position == 1 {
  347. text = text + string(letter)
  348. }
  349. }
  350. }
  351. tweet := lib.Tweet{"", -1, topics, text, round}
  352. tweets = append(tweets, tweet)
  353. }
  354. }
  355. //fmt.Println("tweets recovered: ", tweets)
  356. //sort into read db
  357. lib.NewEntries(tweets, 0)
  358. //reset write db after the tweets were moved to read db
  359. C.resetDb()
  360. //gets current dbWriteSize from leader
  361. dbWriteSizeBytes := readFrom(leaderWorkerConnection, 4)
  362. dbWriteSize = byteToInt(dbWriteSizeBytes)
  363. }
  364. func addTestTweets() {
  365. //creates test tweets
  366. tweets := make([]lib.Tweet, 5)
  367. for i := range tweets {
  368. j := i
  369. if i == 1 {
  370. j = 0
  371. }
  372. text := "Text " + strconv.Itoa(i)
  373. var topics []string
  374. topics = append(topics, "Topic "+strconv.Itoa(j))
  375. tweets[i] = lib.Tweet{"", -1, topics, text, i}
  376. }
  377. lib.NewEntries(tweets, 0)
  378. }
  379. func phase3(leaderWorkerConnection net.Conn, wg *sync.WaitGroup, m *sync.RWMutex) {
  380. for {
  381. gotClient, errorBool := readFromWError(leaderWorkerConnection, 1)
  382. if errorBool {
  383. continue
  384. }
  385. //this worker is done
  386. if gotClient[0] == 0 {
  387. wg.Done()
  388. return
  389. }
  390. subPhase, errorBool := readFromWError(leaderWorkerConnection, 1)
  391. if errorBool {
  392. continue
  393. }
  394. var clientPublicKey [32]byte
  395. _, err := leaderWorkerConnection.Read(clientPublicKey[:])
  396. if err != nil {
  397. fmt.Println("no error handling")
  398. panic(err)
  399. }
  400. //gets the client data
  401. m.RLock()
  402. clientKeys := clientData[clientPublicKey]
  403. m.RUnlock()
  404. if subPhase[0] == 0 || subPhase[0] == 1 {
  405. clientKeys, _, errorBool = handlePirQuery(clientKeys, leaderWorkerConnection, int(subPhase[0]), clientPublicKey, false)
  406. if errorBool {
  407. continue
  408. }
  409. }
  410. getSendTweets(clientKeys, nil, leaderWorkerConnection)
  411. wantsArchive, errorBool := readFromWError(leaderWorkerConnection, 1)
  412. if errorBool {
  413. continue
  414. }
  415. if wantsArchive[0] == 1 {
  416. _, archiveQuerys, errorBool := handlePirQuery(clientKeys, leaderWorkerConnection, -1, clientPublicKey, false)
  417. if errorBool {
  418. continue
  419. }
  420. getSendTweets(clientKeys, archiveQuerys, leaderWorkerConnection)
  421. }
  422. //saves clientKeys
  423. m.Lock()
  424. clientData[clientPublicKey] = clientKeys
  425. m.Unlock()
  426. }
  427. }
  428. //gets tweet from db and sends them to leader
  429. func getSendTweets(clientKeys clientKeys, archiveQuerys [][]byte, leaderWorkerConnection net.Conn) {
  430. tmpNeededSubscriptions := neededSubscriptions
  431. if archiveQuerys != nil {
  432. tmpNeededSubscriptions = len(archiveQuerys)
  433. }
  434. for i := 0; i < tmpNeededSubscriptions; i++ {
  435. //gets all requested tweets
  436. var tweets []byte
  437. if archiveQuerys == nil {
  438. tweets = lib.GetTweets(clientKeys.PirQuery[i], dataLength, 0)
  439. } else {
  440. tweets = lib.GetTweets(archiveQuerys[i], dataLength, 1)
  441. }
  442. //expand sharedSecret so it is of right length
  443. expandBy := len(tweets) / 32
  444. var expandedSharedSecret []byte
  445. for i := 0; i < expandBy; i++ {
  446. expandedSharedSecret = append(expandedSharedSecret, clientKeys.SharedSecret[:]...)
  447. }
  448. //Xor's sharedSecret with all tweets
  449. lib.Xor(expandedSharedSecret[:], tweets)
  450. lib.Xor(tweets, expandedSharedSecret[:])
  451. //sends tweets to leader
  452. tweetsLengthBytes := intToByte(len(tweets))
  453. writeTo(leaderWorkerConnection, tweetsLengthBytes)
  454. writeTo(leaderWorkerConnection, tweets)
  455. }
  456. }
  457. //returns true if client connection is lost
  458. func handlePirQuery(clientKeys clientKeys, leaderWorkerConnection net.Conn, subPhase int, clientPublicKey [32]byte, doAuditing bool) (clientKeys, [][]byte, bool) {
  459. archiveNeededSubscriptions := make([]byte, 4)
  460. if subPhase == -1 {
  461. archiveNeededSubscriptions, errorBool := readFromWError(leaderWorkerConnection, 4)
  462. if errorBool {
  463. return clientKeys, nil, true
  464. }
  465. ignoreMe = archiveNeededSubscriptions
  466. }
  467. //gets the msg length
  468. msgLengthBytes, errorBool := readFromWError(leaderWorkerConnection, 4)
  469. if errorBool {
  470. return clientKeys, nil, true
  471. }
  472. msgLength := byteToInt(msgLengthBytes)
  473. //gets the message
  474. message, errorBool := readFromWError(leaderWorkerConnection, msgLength)
  475. if errorBool {
  476. return clientKeys, nil, true
  477. }
  478. var decryptNonce [24]byte
  479. copy(decryptNonce[:], message[:24])
  480. decrypted, ok := box.Open(nil, message[24:], &decryptNonce, &clientPublicKey, followerPrivateKey)
  481. if !ok {
  482. panic("pirQuery decryption not ok")
  483. }
  484. //gets sharedSecret
  485. if subPhase == 0 {
  486. //bs!
  487. var newSharedSecret [32]byte
  488. for index := 0; index < 32; index++ {
  489. newSharedSecret[index] = decrypted[index]
  490. }
  491. clientKeys.SharedSecret = newSharedSecret
  492. decrypted = decrypted[32:]
  493. if doAuditing {
  494. result := make([][]byte, 1)
  495. result[0] = decrypted
  496. return clientKeys, result, false
  497. }
  498. //follower updates sharedSecret
  499. } else if subPhase == 1 {
  500. sharedSecret := clientKeys.SharedSecret
  501. sharedSecret = sha256.Sum256(sharedSecret[:])
  502. clientKeys.SharedSecret = sharedSecret
  503. }
  504. //follower expects pirQuery
  505. //transforms byteArray to ints of wanted topics
  506. tmpNeededSubscriptions := neededSubscriptions
  507. tmpTopicAmount := topicAmount
  508. if subPhase == -1 {
  509. tmpNeededSubscriptions = byteToInt(archiveNeededSubscriptions)
  510. _, tmpTopicAmount = lib.GetTopicList(1)
  511. }
  512. pirQueryFlattened := decrypted
  513. pirQuerys := make([][]byte, tmpNeededSubscriptions)
  514. for i := range pirQuerys {
  515. pirQuerys[i] = make([]byte, tmpTopicAmount)
  516. }
  517. for i := 0; i < tmpNeededSubscriptions; i++ {
  518. pirQuerys[i] = pirQueryFlattened[i*tmpTopicAmount : (i+1)*tmpTopicAmount]
  519. }
  520. //sets the pirQuery for the client in case whe are not archiving
  521. if subPhase != -1 {
  522. clientKeys.PirQuery = pirQuerys
  523. }
  524. return clientKeys, pirQuerys, false
  525. }
  526. func getSendVirtualAddress(pirQuery []byte, virtualAddresses []int, sharedSecret [32]byte, leaderWorkerConnection net.Conn) {
  527. //xores all requested addresses into virtuallAddress
  528. virtualAddress := make([]byte, 4)
  529. for index, num := range pirQuery {
  530. if num == 1 {
  531. currentAddress := intToByte(virtualAddresses[index])
  532. for i := 0; i < 4; i++ {
  533. virtualAddress[i] = virtualAddress[i] ^ currentAddress[i]
  534. }
  535. }
  536. }
  537. //xores the sharedSecret
  538. for i := 0; i < 4; i++ {
  539. virtualAddress[i] = virtualAddress[i] ^ sharedSecret[i]
  540. }
  541. writeTo(leaderWorkerConnection, virtualAddress)
  542. }
  543. //sends the array to the connection
  544. func writeTo(connection net.Conn, array []byte) {
  545. _, err := connection.Write(array)
  546. if err != nil {
  547. panic(err)
  548. }
  549. }
  550. //reads an array which is returned and of size "size" from the connection
  551. //returned bool is one if connection to client was lost
  552. func readFrom(connection net.Conn, size int) []byte {
  553. array := make([]byte, size)
  554. _, err := connection.Read(array)
  555. if err != nil {
  556. panic(err)
  557. }
  558. return array
  559. }
  560. //reads an array which is returned and of size "size" from the connection
  561. //returns true if connection to client is lost
  562. func readFromWError(connection net.Conn, size int) ([]byte, bool) {
  563. array := make([]byte, size+1)
  564. _, err := connection.Read(array)
  565. if err != nil {
  566. panic(err)
  567. }
  568. if array[0] == 1 {
  569. return nil, true
  570. }
  571. return array[1:], false
  572. }
  573. func transformBytesToStringArray(topicsAsBytes []byte) []string {
  574. var topics []string
  575. var topic string
  576. var position int = 0
  577. for _, letter := range topicsAsBytes {
  578. if string(letter) == "," {
  579. topics[position] = topic
  580. topic = ""
  581. position++
  582. } else {
  583. topic = topic + string(letter)
  584. }
  585. }
  586. return topics
  587. }
  588. func byteToInt(myBytes []byte) (x int) {
  589. x = int(myBytes[3])<<24 + int(myBytes[2])<<16 + int(myBytes[1])<<8 + int(myBytes[0])
  590. return
  591. }
  592. func intToByte(myInt int) (retBytes []byte) {
  593. retBytes = make([]byte, 4)
  594. retBytes[3] = byte((myInt >> 24) & 0xff)
  595. retBytes[2] = byte((myInt >> 16) & 0xff)
  596. retBytes[1] = byte((myInt >> 8) & 0xff)
  597. retBytes[0] = byte(myInt & 0xff)
  598. return
  599. }