follower.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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. //phase1
  118. fmt.Println("phase1")
  119. //create write db for this round
  120. for i := 0; i < dbWriteSize; i++ {
  121. C.createDb(C.int(0), C.int(dataLength))
  122. }
  123. //receives the virtualAddresses
  124. virtualAddresses := make([]int, dbWriteSize+1)
  125. for i := 0; i <= dbWriteSize; i++ {
  126. virtualAddress := readFrom(leaderConnection, 4)
  127. virtualAddresses[i] = byteToInt(virtualAddress)
  128. }
  129. for i := 0; i < numThreads; i++ {
  130. wg.Add(1)
  131. leaderConnection, err := lnLeader.Accept()
  132. if err != nil {
  133. panic(err)
  134. }
  135. leaderConnection.SetDeadline(time.Time{})
  136. startTime = time.Now()
  137. go phase1(i, leaderConnection, m, wg, virtualAddresses)
  138. }
  139. wg.Wait()
  140. //phase2
  141. fmt.Println("phase2")
  142. leaderConnection, err := lnLeader.Accept()
  143. if err != nil {
  144. panic(err)
  145. }
  146. leaderConnection.SetDeadline(time.Time{})
  147. phase2(leaderConnection)
  148. //phase3
  149. fmt.Println("phase3")
  150. if round == 1 {
  151. //addTestTweets()
  152. }
  153. //no tweets -> continue to phase 1 and mb get tweets
  154. topicList, topicAmount = lib.GetTopicList(0)
  155. if len(topicList) == 0 {
  156. continue
  157. }
  158. for i := 0; i < numThreads; i++ {
  159. wg.Add(1)
  160. leaderConnection, err := lnLeader.Accept()
  161. if err != nil {
  162. panic(err)
  163. }
  164. leaderConnection.SetDeadline(time.Time{})
  165. startTime = time.Now()
  166. go phase3(leaderConnection, wg, m)
  167. }
  168. wg.Wait()
  169. lib.CleanUpdbR(round)
  170. round++
  171. }
  172. }
  173. func phase1(id int, leaderWorkerConnection net.Conn, m *sync.RWMutex, wg *sync.WaitGroup, virtualAddresses []int) {
  174. for {
  175. gotClient := readFrom(leaderWorkerConnection, 1)
  176. //this worker is done
  177. if gotClient[0] == 0 {
  178. wg.Done()
  179. return
  180. }
  181. //setup the worker-specific db
  182. dbSize := int(C.dbSize)
  183. db := make([][]byte, dbSize)
  184. for i := 0; i < dbSize; i++ {
  185. db[i] = make([]byte, int(C.db[i].dataSize))
  186. }
  187. //gets clients publicKey
  188. var clientPublicKey *[32]byte
  189. var tmpClientPublicKey [32]byte
  190. _, err := leaderWorkerConnection.Read(tmpClientPublicKey[:])
  191. if err != nil {
  192. fmt.Println("no error handling")
  193. panic(err)
  194. }
  195. clientPublicKey = &tmpClientPublicKey
  196. m.RLock()
  197. clientKeys := clientData[tmpClientPublicKey]
  198. m.RUnlock()
  199. clientKeys, pirQuery, errorBool := handlePirQuery(clientKeys, leaderWorkerConnection, 0, tmpClientPublicKey, true)
  200. if errorBool {
  201. continue
  202. }
  203. getSendVirtualAddress(pirQuery[0], virtualAddresses, clientKeys.SharedSecret, leaderWorkerConnection)
  204. m.Lock()
  205. clientData[*clientPublicKey] = clientKeys
  206. m.Unlock()
  207. //gets dpfQuery from leader
  208. dpfLengthBytes, errorBool := readFromWError(leaderWorkerConnection, 4)
  209. if errorBool {
  210. continue
  211. }
  212. dpfLength := byteToInt(dpfLengthBytes)
  213. dpfQueryBEncrypted, errorBool := readFromWError(leaderWorkerConnection, dpfLength)
  214. if errorBool {
  215. continue
  216. }
  217. //decrypt dpfQueryB for sorting into db
  218. var decryptNonce [24]byte
  219. copy(decryptNonce[:], dpfQueryBEncrypted[:24])
  220. dpfQueryB, ok := box.Open(nil, dpfQueryBEncrypted[24:], &decryptNonce, clientPublicKey, followerPrivateKey)
  221. if !ok {
  222. panic("dpfQueryB decryption not ok")
  223. }
  224. ds := int(C.db[0].dataSize)
  225. dataShareFollower := make([]byte, ds)
  226. pos := C.getUint128_t(C.int(virtualAddresses[dbWriteSize]))
  227. C.evalDPF(C.ctx[id], (*C.uchar)(&dpfQueryB[0]), pos, C.int(ds), (*C.uchar)(&dataShareFollower[0]))
  228. writeTo(leaderWorkerConnection, dataShareFollower)
  229. dataShareLeader, errorBool := readFromWError(leaderWorkerConnection, ds)
  230. if errorBool {
  231. continue
  232. }
  233. auditXOR := make([]byte, ds)
  234. passedAudit := true
  235. for i := 0; i < ds; i++ {
  236. auditXOR[i] = dataShareLeader[i] ^ dataShareFollower[i]
  237. //client tried to write to a position that is not a virtuallAddress
  238. if auditXOR[i] != 0 {
  239. passedAudit = false
  240. }
  241. }
  242. if passedAudit {
  243. //run dpf, xor into local db
  244. for i := 0; i < dbSize; i++ {
  245. ds := int(C.db[i].dataSize)
  246. dataShare := make([]byte, ds)
  247. pos := C.getUint128_t(C.int(virtualAddresses[i]))
  248. C.evalDPF(C.ctx[id], (*C.uchar)(&dpfQueryB[0]), pos, C.int(ds), (*C.uchar)(&dataShare[0]))
  249. for j := 0; j < ds; j++ {
  250. db[i][j] = db[i][j] ^ dataShare[j]
  251. }
  252. }
  253. //xor the worker's DB into the main DB
  254. for i := 0; i < dbSize; i++ {
  255. m.Lock()
  256. C.xorIn(C.int(i), (*C.uchar)(&db[i][0]))
  257. m.Unlock()
  258. }
  259. }
  260. }
  261. }
  262. func phase2(leaderWorkerConnection net.Conn) {
  263. //gets current seed
  264. seedFollower := make([]byte, 16)
  265. C.readSeed((*C.uchar)(&seedFollower[0]))
  266. //get data
  267. dbSize := int(C.dbSize)
  268. tmpdbFollower := make([][]byte, dbSize)
  269. for i := range tmpdbFollower {
  270. tmpdbFollower[i] = make([]byte, dataLength)
  271. }
  272. for i := 0; i < dbSize; i++ {
  273. C.readData(C.int(i), (*C.uchar)(&tmpdbFollower[i][0]))
  274. }
  275. //receive seed from leader
  276. seedLeader := readFrom(leaderWorkerConnection, 16)
  277. //receive data from leader
  278. tmpdbLeader := make([][]byte, dbSize)
  279. for i := range tmpdbLeader {
  280. tmpdbLeader[i] = make([]byte, dataLength)
  281. }
  282. for i := 0; i < dbSize; i++ {
  283. tmpdbLeader[i] = readFrom(leaderWorkerConnection, dataLength)
  284. }
  285. //writes seed to leader
  286. writeTo(leaderWorkerConnection, seedFollower)
  287. //write data to leader
  288. for i := 0; i < dbSize; i++ {
  289. writeTo(leaderWorkerConnection, tmpdbFollower[i])
  290. }
  291. //put together the db
  292. tmpdb := make([][]byte, dbSize)
  293. for i := range tmpdb {
  294. tmpdb[i] = make([]byte, dataLength)
  295. }
  296. //get own Ciphers
  297. ciphersFollowers := make([]*C.uchar, dbSize)
  298. for i := 0; i < dbSize; i++ {
  299. ciphersFollowers[i] = (*C.uchar)(C.malloc(16))
  300. }
  301. for i := 0; i < dbSize; i++ {
  302. C.getCipher(0, C.int(i), ciphersFollowers[i])
  303. }
  304. //receive ciphers from leader
  305. ciphersLeader := make([]byte, dbSize*16)
  306. for i := 0; i < dbSize; i++ {
  307. _, err := leaderWorkerConnection.Read(ciphersLeader[i*16:])
  308. if err != nil {
  309. panic(err)
  310. }
  311. }
  312. //send own Ciphers to leader
  313. for i := 0; i < dbSize; i++ {
  314. writeTo(leaderWorkerConnection, C.GoBytes(unsafe.Pointer(ciphersFollowers[i]), 16))
  315. }
  316. //put in ciphers from leader
  317. for i := 0; i < dbSize; i++ {
  318. C.putCipher(0, C.int(i), (*C.uchar)(&ciphersLeader[i*16]))
  319. }
  320. for i := 0; i < dbSize; i++ {
  321. 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]))
  322. }
  323. var tweets []lib.Tweet
  324. for i := 0; i < dbSize; i++ {
  325. //discard cover message
  326. if tmpdb[i][0] == 0 {
  327. continue
  328. } else {
  329. //reconstruct tweet
  330. var position int = 0
  331. var topics []string
  332. var topic string
  333. var text string
  334. for _, letter := range tmpdb[i] {
  335. if string(letter) == ";" {
  336. if topic != "" {
  337. topics = append(topics, topic)
  338. topic = ""
  339. }
  340. position++
  341. } else {
  342. if position == 0 {
  343. if string(letter) == "," {
  344. topics = append(topics, topic)
  345. topic = ""
  346. } else {
  347. topic = topic + string(letter)
  348. }
  349. } else if position == 1 {
  350. text = text + string(letter)
  351. }
  352. }
  353. }
  354. tweet := lib.Tweet{"", -1, topics, text, round}
  355. tweets = append(tweets, tweet)
  356. }
  357. }
  358. //fmt.Println("tweets recovered: ", tweets)
  359. //sort into read db
  360. lib.NewEntries(tweets, 0)
  361. //reset write db after the tweets were moved to read db
  362. C.resetDb()
  363. //gets current dbWriteSize from leader
  364. dbWriteSizeBytes := readFrom(leaderWorkerConnection, 4)
  365. dbWriteSize = byteToInt(dbWriteSizeBytes)
  366. }
  367. func addTestTweets() {
  368. //creates test tweets
  369. tweets := make([]lib.Tweet, 5)
  370. for i := range tweets {
  371. j := i
  372. if i == 1 {
  373. j = 0
  374. }
  375. text := "Text " + strconv.Itoa(i)
  376. var topics []string
  377. topics = append(topics, "Topic "+strconv.Itoa(j))
  378. tweets[i] = lib.Tweet{"", -1, topics, text, i}
  379. }
  380. lib.NewEntries(tweets, 0)
  381. }
  382. func phase3(leaderWorkerConnection net.Conn, wg *sync.WaitGroup, m *sync.RWMutex) {
  383. for {
  384. gotClient, errorBool := readFromWError(leaderWorkerConnection, 1)
  385. if errorBool {
  386. continue
  387. }
  388. //this worker is done
  389. if gotClient[0] == 0 {
  390. wg.Done()
  391. return
  392. }
  393. subPhase, errorBool := readFromWError(leaderWorkerConnection, 1)
  394. if errorBool {
  395. continue
  396. }
  397. var clientPublicKey [32]byte
  398. _, err := leaderWorkerConnection.Read(clientPublicKey[:])
  399. if err != nil {
  400. fmt.Println("no error handling")
  401. panic(err)
  402. }
  403. //gets the client data
  404. m.RLock()
  405. clientKeys := clientData[clientPublicKey]
  406. m.RUnlock()
  407. if subPhase[0] == 0 || subPhase[0] == 1 {
  408. clientKeys, _, errorBool = handlePirQuery(clientKeys, leaderWorkerConnection, int(subPhase[0]), clientPublicKey, false)
  409. if errorBool {
  410. continue
  411. }
  412. }
  413. getSendTweets(clientKeys, nil, leaderWorkerConnection)
  414. wantsArchive, errorBool := readFromWError(leaderWorkerConnection, 1)
  415. if errorBool {
  416. continue
  417. }
  418. if wantsArchive[0] == 1 {
  419. _, archiveQuerys, errorBool := handlePirQuery(clientKeys, leaderWorkerConnection, -1, clientPublicKey, false)
  420. if errorBool {
  421. continue
  422. }
  423. getSendTweets(clientKeys, archiveQuerys, leaderWorkerConnection)
  424. }
  425. //saves clientKeys
  426. m.Lock()
  427. clientData[clientPublicKey] = clientKeys
  428. m.Unlock()
  429. }
  430. }
  431. //gets tweet from db and sends them to leader
  432. func getSendTweets(clientKeys clientKeys, archiveQuerys [][]byte, leaderWorkerConnection net.Conn) {
  433. tmpNeededSubscriptions := neededSubscriptions
  434. if archiveQuerys != nil {
  435. tmpNeededSubscriptions = len(archiveQuerys)
  436. }
  437. for i := 0; i < tmpNeededSubscriptions; i++ {
  438. //gets all requested tweets
  439. var tweets []byte
  440. if archiveQuerys == nil {
  441. tweets = lib.GetTweets(clientKeys.PirQuery[i], dataLength, 0)
  442. } else {
  443. tweets = lib.GetTweets(archiveQuerys[i], dataLength, 1)
  444. }
  445. //expand sharedSecret so it is of right length
  446. expandBy := len(tweets) / 32
  447. var expandedSharedSecret []byte
  448. for i := 0; i < expandBy; i++ {
  449. expandedSharedSecret = append(expandedSharedSecret, clientKeys.SharedSecret[:]...)
  450. }
  451. //Xor's sharedSecret with all tweets
  452. lib.Xor(expandedSharedSecret[:], tweets)
  453. lib.Xor(tweets, expandedSharedSecret[:])
  454. //sends tweets to leader
  455. tweetsLengthBytes := intToByte(len(tweets))
  456. writeTo(leaderWorkerConnection, tweetsLengthBytes)
  457. writeTo(leaderWorkerConnection, tweets)
  458. }
  459. }
  460. //returns true if client connection is lost
  461. func handlePirQuery(clientKeys clientKeys, leaderWorkerConnection net.Conn, subPhase int, clientPublicKey [32]byte, doAuditing bool) (clientKeys, [][]byte, bool) {
  462. archiveNeededSubscriptions := make([]byte, 4)
  463. if subPhase == -1 {
  464. archiveNeededSubscriptions, errorBool := readFromWError(leaderWorkerConnection, 4)
  465. if errorBool {
  466. return clientKeys, nil, true
  467. }
  468. ignoreMe = archiveNeededSubscriptions
  469. }
  470. //gets the msg length
  471. msgLengthBytes, errorBool := readFromWError(leaderWorkerConnection, 4)
  472. if errorBool {
  473. return clientKeys, nil, true
  474. }
  475. msgLength := byteToInt(msgLengthBytes)
  476. //gets the message
  477. message, errorBool := readFromWError(leaderWorkerConnection, msgLength)
  478. if errorBool {
  479. return clientKeys, nil, true
  480. }
  481. var decryptNonce [24]byte
  482. copy(decryptNonce[:], message[:24])
  483. decrypted, ok := box.Open(nil, message[24:], &decryptNonce, &clientPublicKey, followerPrivateKey)
  484. if !ok {
  485. panic("pirQuery decryption not ok")
  486. }
  487. //gets sharedSecret
  488. if subPhase == 0 {
  489. //bs!
  490. var newSharedSecret [32]byte
  491. for index := 0; index < 32; index++ {
  492. newSharedSecret[index] = decrypted[index]
  493. }
  494. clientKeys.SharedSecret = newSharedSecret
  495. decrypted = decrypted[32:]
  496. if doAuditing {
  497. result := make([][]byte, 1)
  498. result[0] = decrypted
  499. return clientKeys, result, false
  500. }
  501. //follower updates sharedSecret
  502. } else if subPhase == 1 {
  503. sharedSecret := clientKeys.SharedSecret
  504. sharedSecret = sha256.Sum256(sharedSecret[:])
  505. clientKeys.SharedSecret = sharedSecret
  506. }
  507. //follower expects pirQuery
  508. //transforms byteArray to ints of wanted topics
  509. tmpNeededSubscriptions := neededSubscriptions
  510. tmpTopicAmount := topicAmount
  511. if subPhase == -1 {
  512. tmpNeededSubscriptions = byteToInt(archiveNeededSubscriptions)
  513. _, tmpTopicAmount = lib.GetTopicList(1)
  514. }
  515. pirQueryFlattened := decrypted
  516. pirQuerys := make([][]byte, tmpNeededSubscriptions)
  517. for i := range pirQuerys {
  518. pirQuerys[i] = make([]byte, tmpTopicAmount)
  519. }
  520. for i := 0; i < tmpNeededSubscriptions; i++ {
  521. pirQuerys[i] = pirQueryFlattened[i*tmpTopicAmount : (i+1)*tmpTopicAmount]
  522. }
  523. //sets the pirQuery for the client in case whe are not archiving
  524. if subPhase != -1 {
  525. clientKeys.PirQuery = pirQuerys
  526. }
  527. return clientKeys, pirQuerys, false
  528. }
  529. func getSendVirtualAddress(pirQuery []byte, virtualAddresses []int, sharedSecret [32]byte, leaderWorkerConnection net.Conn) {
  530. //xores all requested addresses into virtuallAddress
  531. virtualAddress := make([]byte, 4)
  532. for index, num := range pirQuery {
  533. if num == 1 {
  534. currentAddress := intToByte(virtualAddresses[index])
  535. for i := 0; i < 4; i++ {
  536. virtualAddress[i] = virtualAddress[i] ^ currentAddress[i]
  537. }
  538. }
  539. }
  540. //xores the sharedSecret
  541. for i := 0; i < 4; i++ {
  542. virtualAddress[i] = virtualAddress[i] ^ sharedSecret[i]
  543. }
  544. writeTo(leaderWorkerConnection, virtualAddress)
  545. }
  546. //sends the array to the connection
  547. func writeTo(connection net.Conn, array []byte) {
  548. _, err := connection.Write(array)
  549. if err != nil {
  550. panic(err)
  551. }
  552. }
  553. //reads an array which is returned and of size "size" from the connection
  554. //returned bool is one if connection to client was lost
  555. func readFrom(connection net.Conn, size int) []byte {
  556. array := make([]byte, size)
  557. _, err := connection.Read(array)
  558. if err != nil {
  559. panic(err)
  560. }
  561. return array
  562. }
  563. //reads an array which is returned and of size "size" from the connection
  564. //returns true if connection to client is lost
  565. func readFromWError(connection net.Conn, size int) ([]byte, bool) {
  566. array := make([]byte, size+1)
  567. _, err := connection.Read(array)
  568. if err != nil {
  569. panic(err)
  570. }
  571. if array[0] == 1 {
  572. return nil, true
  573. }
  574. return array[1:], false
  575. }
  576. func transformBytesToStringArray(topicsAsBytes []byte) []string {
  577. var topics []string
  578. var topic string
  579. var position int = 0
  580. for _, letter := range topicsAsBytes {
  581. if string(letter) == "," {
  582. topics[position] = topic
  583. topic = ""
  584. position++
  585. } else {
  586. topic = topic + string(letter)
  587. }
  588. }
  589. return topics
  590. }
  591. func byteToInt(myBytes []byte) (x int) {
  592. x = int(myBytes[3])<<24 + int(myBytes[2])<<16 + int(myBytes[1])<<8 + int(myBytes[0])
  593. return
  594. }
  595. func intToByte(myInt int) (retBytes []byte) {
  596. retBytes = make([]byte, 4)
  597. retBytes[3] = byte((myInt >> 24) & 0xff)
  598. retBytes[2] = byte((myInt >> 16) & 0xff)
  599. retBytes[1] = byte((myInt >> 8) & 0xff)
  600. retBytes[0] = byte(myInt & 0xff)
  601. return
  602. }