follower.go 19 KB

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