leader.go 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  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. //ssssssssssssssss
  10. import (
  11. "crypto/rand"
  12. "crypto/rsa"
  13. "crypto/sha256"
  14. "crypto/tls"
  15. "crypto/x509"
  16. "crypto/x509/pkix"
  17. "encoding/pem"
  18. "fmt"
  19. "math"
  20. "math/big"
  21. mr "math/rand"
  22. "net"
  23. "sort"
  24. "strconv"
  25. "strings"
  26. "sync"
  27. "time"
  28. lib "2PPS/lib"
  29. "unsafe"
  30. "golang.org/x/crypto/nacl/box"
  31. )
  32. //this stores all neccessary information for each client
  33. type clientKeys struct {
  34. roundsParticipating int
  35. PublicKey *[32]byte
  36. SharedSecret [32]byte
  37. PirQuery [][]byte
  38. }
  39. var clientData = make(map[net.Addr]clientKeys)
  40. const follower string = "127.0.0.1:4442"
  41. //Maximum Transport Unit
  42. const mtu int = 1100
  43. var leaderPrivateKey *[32]byte
  44. var leaderPublicKey *[32]byte
  45. var followerPublicKey *[32]byte
  46. const maxNumberOfClients = 10000000
  47. var topicList []byte
  48. var topicAmount int
  49. var archiveTopicAmount int
  50. // every roundsBeforeUpdate the client updates his pirQuery
  51. const roundsBeforeUpdate = 5
  52. const neededSubscriptions = 1
  53. const numThreads = 12
  54. const dataLength = 256
  55. const minDBWriteSize = 10
  56. var dbWriteSize float64 = 20000
  57. var collisionCounter []float64
  58. var clientsConnected int
  59. var maxTimePerRound time.Duration = 5 * time.Second
  60. var clientsServedPhase1 []int
  61. var clientsServedPhase3 []int
  62. var startPhase1 time.Time
  63. var startPhase2 time.Time
  64. var startPhase3 time.Time
  65. //counts the number of rounds
  66. var round int = 0
  67. var startTime time.Time
  68. var startTimeRound time.Time
  69. //channel for goroutine communication with clients
  70. var phase1Channel = make(chan net.Conn, maxNumberOfClients)
  71. var phase3Channel = make(chan net.Conn, maxNumberOfClients)
  72. //variables for calculating the dbWrite size
  73. const publisherRounds int = 10
  74. var publisherAmount float64
  75. var publisherHistory [publisherRounds]int
  76. //todo! handle client dc during phase1/3
  77. func main() {
  78. //prevents race conditions for wrtiting
  79. m := &sync.RWMutex{}
  80. startTime = time.Now()
  81. clientsServedPhase1 = make([]int, 1000)
  82. clientsServedPhase3 = make([]int, 1000)
  83. generatedPublicKey, generatedPrivateKey, err := box.GenerateKey(rand.Reader)
  84. if err != nil {
  85. panic(err)
  86. }
  87. //why is this neccessary?
  88. leaderPrivateKey = generatedPrivateKey
  89. leaderPublicKey = generatedPublicKey
  90. C.initializeServer(C.int(numThreads))
  91. //calls follower for setup
  92. conf := &tls.Config{
  93. InsecureSkipVerify: true,
  94. }
  95. followerConnection, err := tls.Dial("tcp", follower, conf)
  96. if err != nil {
  97. panic(err)
  98. }
  99. followerConnection.SetDeadline(time.Time{})
  100. //receives follower publicKey
  101. var tmpFollowerPubKey [32]byte
  102. _, err = followerConnection.Read(tmpFollowerPubKey[:])
  103. if err != nil {
  104. panic(err)
  105. }
  106. followerPublicKey = &tmpFollowerPubKey
  107. //send publicKey to follower
  108. writeTo(followerConnection, leaderPublicKey[:])
  109. writeTo(followerConnection, intToByte(neededSubscriptions))
  110. //goroutine for accepting new clients
  111. go func() {
  112. leaderConnectionPrivateKey, err := rsa.GenerateKey(rand.Reader, 2048)
  113. if err != nil {
  114. panic(err)
  115. }
  116. // Generate a pem block with the private key
  117. keyPem := pem.EncodeToMemory(&pem.Block{
  118. Type: "RSA PRIVATE KEY",
  119. Bytes: x509.MarshalPKCS1PrivateKey(leaderConnectionPrivateKey),
  120. })
  121. tml := x509.Certificate{
  122. // you can add any attr that you need
  123. NotBefore: time.Now(),
  124. NotAfter: time.Now().AddDate(5, 0, 0),
  125. // you have to generate a different serial number each execution
  126. SerialNumber: big.NewInt(123123),
  127. Subject: pkix.Name{
  128. CommonName: "New Name",
  129. Organization: []string{"New Org."},
  130. },
  131. BasicConstraintsValid: true,
  132. }
  133. cert, err := x509.CreateCertificate(rand.Reader, &tml, &tml, &leaderConnectionPrivateKey.PublicKey, leaderConnectionPrivateKey)
  134. if err != nil {
  135. panic(err)
  136. }
  137. // Generate a pem block with the certificate
  138. certPem := pem.EncodeToMemory(&pem.Block{
  139. Type: "CERTIFICATE",
  140. Bytes: cert,
  141. })
  142. tlsCert, err := tls.X509KeyPair(certPem, keyPem)
  143. if err != nil {
  144. panic(err)
  145. }
  146. config := &tls.Config{Certificates: []tls.Certificate{tlsCert}}
  147. //listens for clients
  148. lnClients, err := tls.Listen("tcp", ":4441", config)
  149. if err != nil {
  150. panic(err)
  151. }
  152. defer lnClients.Close()
  153. for {
  154. clientConnection, err := lnClients.Accept()
  155. if err != nil {
  156. fmt.Println("error", err)
  157. }
  158. clientConnection.SetDeadline(time.Time{})
  159. //sends topicList so client can participate in phase 3 asap
  160. errorBool := sendTopicLists(clientConnection, followerConnection, true)
  161. if errorBool {
  162. break
  163. }
  164. //send leader publicKey
  165. _, err = clientConnection.Write(leaderPublicKey[:])
  166. if err != nil {
  167. fmt.Println("error", err)
  168. clientConnection.Close()
  169. break
  170. }
  171. //send follower publicKey
  172. _, err = clientConnection.Write(followerPublicKey[:])
  173. if err != nil {
  174. fmt.Println("error", err)
  175. clientConnection.Close()
  176. break
  177. }
  178. var clientPublicKey *[32]byte
  179. var tmpClientPublicKey [32]byte
  180. //gets publicKey from client
  181. _, err = clientConnection.Read(tmpClientPublicKey[:])
  182. if err != nil {
  183. fmt.Println("error", err)
  184. clientConnection.Close()
  185. break
  186. }
  187. _, err = clientConnection.Write(intToByte(neededSubscriptions))
  188. if err != nil {
  189. fmt.Println("error", err)
  190. clientConnection.Close()
  191. break
  192. }
  193. _, err = clientConnection.Write(intToByte(int(startTime.Unix())))
  194. if err != nil {
  195. fmt.Println("error", err)
  196. clientConnection.Close()
  197. break
  198. }
  199. clientPublicKey = &tmpClientPublicKey
  200. //this is the key for map(client data)
  201. remoteAddress := clientConnection.RemoteAddr()
  202. //pirQuery will be added in phase 3
  203. //bs! only want to set roundsParticipating and answerAmount to 0, mb there is a better way
  204. //will work for now
  205. var emptyArray [32]byte
  206. var emptyByteArray [][]byte
  207. keys := clientKeys{0, clientPublicKey, emptyArray, emptyByteArray}
  208. m.Lock()
  209. clientData[remoteAddress] = keys
  210. m.Unlock()
  211. phase1Channel <- clientConnection
  212. clientsConnected++
  213. if clientsConnected%1000 == 0 {
  214. fmt.Println("clientsConnected", clientsConnected)
  215. }
  216. }
  217. }()
  218. wg := &sync.WaitGroup{}
  219. //the current phase
  220. phase := make([]byte, 1)
  221. for {
  222. startPhase1 = time.Now()
  223. startTimeRound = time.Now()
  224. phase[0] = 1
  225. round++
  226. fmt.Println("clientsServedPhase1", clientsServedPhase1[round-1])
  227. fmt.Println("clientsServedPhase3", clientsServedPhase3[round-1])
  228. fmt.Println("dbWriteSize", dbWriteSize)
  229. fmt.Println("Phase 1 Round", round)
  230. //creates a new write Db for this round
  231. for i := 0; i < int(dbWriteSize); i++ {
  232. C.createDb(C.int(1), C.int(dataLength))
  233. }
  234. //creates a new db containing virtual addresses for auditing
  235. virtualAddresses := createVirtualAddresses()
  236. //send all virtualAddresses to follower
  237. for i := 0; i <= int(dbWriteSize); i++ {
  238. writeTo(followerConnection, intToByte(virtualAddresses[i]))
  239. }
  240. //moves all clients to phase1
  241. if len(phase3Channel) > 0 {
  242. for client := range phase3Channel {
  243. phase1Channel <- client
  244. if len(phase3Channel) == 0 {
  245. break
  246. }
  247. }
  248. }
  249. for id := 0; id < numThreads; id++ {
  250. wg.Add(1)
  251. followerConnection, err := tls.Dial("tcp", follower, conf)
  252. if err != nil {
  253. panic(err)
  254. }
  255. followerConnection.SetDeadline(time.Time{})
  256. go phase1(id, phase, followerConnection, wg, m, virtualAddresses)
  257. }
  258. wg.Wait()
  259. fmt.Println("fullDurationPhase1", time.Since(startPhase1).Seconds())
  260. //Phase 2
  261. startPhase2 = time.Now()
  262. followerConnection, err := tls.Dial("tcp", follower, conf)
  263. if err != nil {
  264. panic(err)
  265. }
  266. followerConnection.SetDeadline(time.Time{})
  267. phase2(followerConnection)
  268. fmt.Println("fullDurationPhase2", time.Since(startPhase2).Seconds())
  269. //Phase 3
  270. //moves all clients to phase3
  271. if len(phase1Channel) > 0 {
  272. for client := range phase1Channel {
  273. phase3Channel <- client
  274. if len(phase1Channel) == 0 {
  275. break
  276. }
  277. }
  278. }
  279. startPhase3 = time.Now()
  280. //no tweets -> continue to phase 1 and mb get tweets
  281. topicList, topicAmount = lib.GetTopicList(0)
  282. if len(topicList) == 0 {
  283. continue
  284. }
  285. phase[0] = 3
  286. startTimeRound = time.Now()
  287. for id := 0; id < numThreads; id++ {
  288. wg.Add(1)
  289. followerConnection, err := tls.Dial("tcp", follower, conf)
  290. if err != nil {
  291. panic(err)
  292. }
  293. followerConnection.SetDeadline(time.Time{})
  294. go phase3(id, phase, followerConnection, wg, m)
  295. }
  296. wg.Wait()
  297. fmt.Println("fullDurationPhase3", time.Since(startPhase3).Seconds())
  298. lib.CleanUpdbR(round)
  299. }
  300. }
  301. func phase1(id int, phase []byte, followerConnection net.Conn, wg *sync.WaitGroup, m *sync.RWMutex, virtualAddresses []int) {
  302. roundAsBytes := intToByte(round)
  303. gotClient := make([]byte, 1)
  304. gotClient[0] = 0
  305. //wait until time is up
  306. for len(phase1Channel) == 0 {
  307. if time.Since(startTimeRound) > maxTimePerRound {
  308. //tells follower that this worker is done
  309. writeTo(followerConnection, gotClient)
  310. wg.Done()
  311. return
  312. }
  313. time.Sleep(1 * time.Second)
  314. }
  315. for clientConnection := range phase1Channel {
  316. clientsServedPhase1[round] = clientsServedPhase1[round] + 1
  317. if clientsServedPhase1[round]%1000 == 0 {
  318. fmt.Println("clientsServedPhase1", clientsServedPhase1[round])
  319. fmt.Println("timeTaken", time.Since(startPhase1))
  320. }
  321. gotClient[0] = 1
  322. //tells follower that this worker got a clientConnection
  323. writeTo(followerConnection, gotClient)
  324. //sends clients publicKey to follower
  325. m.RLock()
  326. clientPublicKey := clientData[clientConnection.RemoteAddr()].PublicKey
  327. m.RUnlock()
  328. writeTo(followerConnection, clientPublicKey[:])
  329. //setup the worker-specific db
  330. dbSize := int(C.dbSize)
  331. db := make([][]byte, dbSize)
  332. for i := 0; i < dbSize; i++ {
  333. db[i] = make([]byte, int(C.db[i].dataSize))
  334. }
  335. //tells client that phase 1 has begun
  336. errorBool := writeToWError(clientConnection, phase, followerConnection, 5)
  337. if errorBool {
  338. contBool := handleClientDC(wg, followerConnection, phase1Channel)
  339. if contBool {
  340. continue
  341. } else {
  342. return
  343. }
  344. }
  345. //tells client current dbWriteSize
  346. errorBool = writeToWError(clientConnection, intToByte(int(dbWriteSize)), followerConnection, 5)
  347. if errorBool {
  348. contBool := handleClientDC(wg, followerConnection, phase1Channel)
  349. if contBool {
  350. continue
  351. } else {
  352. return
  353. }
  354. }
  355. //tells client current round
  356. errorBool = writeToWError(clientConnection, roundAsBytes, followerConnection, 5)
  357. if errorBool {
  358. contBool := handleClientDC(wg, followerConnection, phase1Channel)
  359. if contBool {
  360. continue
  361. } else {
  362. return
  363. }
  364. }
  365. //begin auditing
  366. //auditingStart := time.Now()
  367. m.RLock()
  368. var clientKeys = clientData[clientConnection.RemoteAddr()]
  369. m.RUnlock()
  370. clientKeys, pirQuery, errorBool := handlePirQuery(clientKeys, clientConnection, followerConnection, 0, true)
  371. if errorBool {
  372. contBool := handleClientDC(wg, followerConnection, phase1Channel)
  373. if contBool {
  374. continue
  375. } else {
  376. return
  377. }
  378. }
  379. errorBool = getSendVirtualAddress(pirQuery[0], virtualAddresses, clientKeys.SharedSecret, clientConnection, followerConnection)
  380. if errorBool {
  381. contBool := handleClientDC(wg, followerConnection, phase1Channel)
  382. if contBool {
  383. continue
  384. } else {
  385. return
  386. }
  387. }
  388. if id == 0 {
  389. //fmt.Println("Auditing duration", time.Since(auditingStart).Seconds())
  390. }
  391. m.Lock()
  392. clientData[clientConnection.RemoteAddr()] = clientKeys
  393. m.Unlock()
  394. //accept dpfQuery from client
  395. dpfLengthBytes, errorBool := readFrom(clientConnection, 4, followerConnection, 5)
  396. if errorBool {
  397. contBool := handleClientDC(wg, followerConnection, phase1Channel)
  398. if contBool {
  399. continue
  400. } else {
  401. return
  402. }
  403. }
  404. dpfLength := byteToInt(dpfLengthBytes)
  405. dpfQueryAEncrypted, errorBool := readFrom(clientConnection, dpfLength, followerConnection, 5)
  406. if errorBool {
  407. contBool := handleClientDC(wg, followerConnection, phase1Channel)
  408. if contBool {
  409. continue
  410. } else {
  411. return
  412. }
  413. }
  414. dpfQueryBEncrypted, errorBool := readFrom(clientConnection, dpfLength, followerConnection, 5)
  415. if errorBool {
  416. contBool := handleClientDC(wg, followerConnection, phase1Channel)
  417. if contBool {
  418. continue
  419. } else {
  420. return
  421. }
  422. }
  423. writeToWError(followerConnection, dpfLengthBytes, nil, 0)
  424. writeToWError(followerConnection, dpfQueryBEncrypted, nil, 0)
  425. //decrypt dpfQueryA for sorting into db
  426. var decryptNonce [24]byte
  427. copy(decryptNonce[:], dpfQueryAEncrypted[:24])
  428. dpfQueryA, ok := box.Open(nil, dpfQueryAEncrypted[24:], &decryptNonce, clientPublicKey, leaderPrivateKey)
  429. if !ok {
  430. panic("dpfQueryA decryption not ok")
  431. }
  432. ds := int(C.db[0].dataSize)
  433. dataShareLeader := make([]byte, ds)
  434. pos := C.getUint128_t(C.int(virtualAddresses[int(dbWriteSize)]))
  435. C.evalDPF(C.ctx[id], (*C.uchar)(&dpfQueryA[0]), pos, C.int(ds), (*C.uchar)(&dataShareLeader[0]))
  436. dataShareFollower, _ := readFrom(followerConnection, ds, nil, 0)
  437. writeToWError(followerConnection, dataShareLeader, nil, 0)
  438. auditXOR := make([]byte, ds)
  439. passedAudit := true
  440. for i := 0; i < ds; i++ {
  441. auditXOR[i] = dataShareLeader[i] ^ dataShareFollower[i]
  442. //client tried to write to a position that is not a virtuallAddress
  443. if auditXOR[i] != 0 {
  444. clientConnection.Close()
  445. passedAudit = false
  446. }
  447. }
  448. if passedAudit {
  449. //run dpf, xor into local db
  450. for i := 0; i < dbSize; i++ {
  451. ds := int(C.db[i].dataSize)
  452. dataShare := make([]byte, ds)
  453. pos := C.getUint128_t(C.int(virtualAddresses[i]))
  454. C.evalDPF(C.ctx[id], (*C.uchar)(&dpfQueryA[0]), pos, C.int(ds), (*C.uchar)(&dataShare[0]))
  455. for j := 0; j < ds; j++ {
  456. db[i][j] = db[i][j] ^ dataShare[j]
  457. }
  458. }
  459. //xor the worker's DB into the main DB
  460. for i := 0; i < dbSize; i++ {
  461. m.Lock()
  462. C.xorIn(C.int(i), (*C.uchar)(&db[i][0]))
  463. m.Unlock()
  464. }
  465. phase3Channel <- clientConnection
  466. }
  467. //loop that waits for new client or leaves phase1 if time is up
  468. for {
  469. if time.Since(startTimeRound) < maxTimePerRound {
  470. //this worker handles the next client
  471. if len(phase1Channel) > 0 {
  472. break
  473. //this worker waits for next client
  474. } else {
  475. time.Sleep(1 * time.Second)
  476. }
  477. //times up
  478. } else {
  479. //tells follower that this worker is done
  480. gotClient[0] = 0
  481. writeTo(followerConnection, gotClient)
  482. wg.Done()
  483. return
  484. }
  485. }
  486. }
  487. }
  488. func phase2(followerConnection net.Conn) {
  489. //gets current seed
  490. seedLeader := make([]byte, 16)
  491. C.readSeed((*C.uchar)(&seedLeader[0]))
  492. //get data
  493. dbSize := int(C.dbSize)
  494. tmpdbLeader := make([][]byte, dbSize)
  495. for i := range tmpdbLeader {
  496. tmpdbLeader[i] = make([]byte, dataLength)
  497. }
  498. for i := 0; i < dbSize; i++ {
  499. C.readData(C.int(i), (*C.uchar)(&tmpdbLeader[i][0]))
  500. }
  501. //writes seed to follower
  502. writeTo(followerConnection, seedLeader)
  503. //write data to follower
  504. //this is surely inefficent
  505. for i := 0; i < dbSize; i++ {
  506. writeTo(followerConnection, tmpdbLeader[i])
  507. }
  508. //receive seed from follower
  509. seedFollower, _ := readFrom(followerConnection, 16, nil, 0)
  510. //receive data from follower
  511. tmpdbFollower := make([][]byte, dbSize)
  512. for i := range tmpdbFollower {
  513. tmpdbFollower[i] = make([]byte, dataLength)
  514. }
  515. for i := 0; i < dbSize; i++ {
  516. tmpdbFollower[i], _ = readFrom(followerConnection, dataLength, nil, 0)
  517. }
  518. //put together the db
  519. tmpdb := make([][]byte, dbSize)
  520. for i := range tmpdb {
  521. tmpdb[i] = make([]byte, dataLength)
  522. }
  523. //get own Ciphers
  524. ciphersLeader := make([]*C.uchar, dbSize)
  525. for i := 0; i < dbSize; i++ {
  526. ciphersLeader[i] = (*C.uchar)(C.malloc(16))
  527. }
  528. for i := 0; i < dbSize; i++ {
  529. C.getCipher(1, C.int(i), ciphersLeader[i])
  530. }
  531. //send own Ciphers to follower
  532. for i := 0; i < dbSize; i++ {
  533. writeTo(followerConnection, C.GoBytes(unsafe.Pointer(ciphersLeader[i]), 16))
  534. }
  535. //receive ciphers from follower
  536. ciphersFollower := make([]byte, dbSize*16)
  537. for i := 0; i < dbSize; i++ {
  538. _, err := followerConnection.Read(ciphersFollower[i*16:])
  539. if err != nil {
  540. panic(err)
  541. }
  542. }
  543. //put in ciphers from follower
  544. for i := 0; i < dbSize; i++ {
  545. C.putCipher(1, C.int(i), (*C.uchar)(&ciphersFollower[i*16]))
  546. }
  547. //decrypt each row
  548. for i := 0; i < dbSize; i++ {
  549. 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]))
  550. }
  551. var tweets []lib.Tweet
  552. var currentPublisherAmount int = 0
  553. var collisions float64
  554. for i := 0; i < dbSize; i++ {
  555. //discard cover message
  556. if tmpdb[i][1] == 0 {
  557. continue
  558. //collision
  559. } else if -1 == strings.Index(string(tmpdb[i]), ";;") {
  560. currentPublisherAmount++
  561. currentPublisherAmount++
  562. collisions++
  563. continue
  564. } else {
  565. currentPublisherAmount++
  566. //reconstruct tweet
  567. var position int = 0
  568. var topics []string
  569. var topic string
  570. var text string
  571. for _, letter := range tmpdb[i] {
  572. if string(letter) == ";" {
  573. if topic != "" {
  574. topics = append(topics, topic)
  575. topic = ""
  576. }
  577. position++
  578. } else {
  579. if position == 0 {
  580. if string(letter) == "," {
  581. topics = append(topics, topic)
  582. topic = ""
  583. } else {
  584. //if topics are
  585. //int
  586. topic = topic + fmt.Sprint(int(letter))
  587. //string
  588. //topic = topic + string(letter)
  589. }
  590. } else if position == 1 {
  591. text = text + string(letter)
  592. }
  593. }
  594. }
  595. tweet := lib.Tweet{"", -1, topics, text, round}
  596. if text != "" {
  597. tweets = append(tweets, tweet)
  598. } else {
  599. //this is a odd(number) way collisions
  600. collisions++
  601. }
  602. }
  603. }
  604. collisionCounter = append(collisionCounter, collisions)
  605. if collisions/dbWriteSize > 0.05 {
  606. fmt.Println("Collisions this round", collisions, "dbWriteSize", dbWriteSize)
  607. }
  608. //fmt.Println("tweets recovered: ", tweets)
  609. //sort into read db
  610. //fmt.Println("newTweets", tweets)
  611. lib.NewEntries(tweets, 0)
  612. C.resetDb()
  613. //calculates the publisherAverage over the last publisherRounds rounds
  614. index := round % publisherRounds
  615. publisherHistory[index] = currentPublisherAmount
  616. fmt.Println("currentPublisherAmount", currentPublisherAmount)
  617. var publisherAmount int
  618. for _, num := range publisherHistory {
  619. publisherAmount += num
  620. }
  621. publisherAverage := 0
  622. if round < publisherRounds {
  623. publisherAverage = publisherAmount / round
  624. } else {
  625. publisherAverage = publisherAmount / publisherRounds
  626. }
  627. //calculates the dbWriteSize for this round
  628. dbWriteSize = math.Ceil(19.5 * float64(publisherAverage))
  629. if dbWriteSize < minDBWriteSize {
  630. dbWriteSize = minDBWriteSize
  631. }
  632. //writes dbWriteSize of current round to follower
  633. writeTo(followerConnection, intToByte(int(dbWriteSize)))
  634. }
  635. func addTestTweets() {
  636. //creates test tweets
  637. tweets := make([]lib.Tweet, 5)
  638. for i := range tweets {
  639. j := i
  640. if i == 1 {
  641. j = 0
  642. }
  643. text := "Text " + strconv.Itoa(i)
  644. var topics []string
  645. topics = append(topics, "Topic "+strconv.Itoa(j))
  646. tweets[i] = lib.Tweet{"", -1, topics, text, i}
  647. }
  648. lib.NewEntries(tweets, 0)
  649. }
  650. //opti! mb it is quicker to send updated topicLists to clients first so pirQuerys are ready
  651. func phase3(id int, phase []byte, followerConnection net.Conn, wg *sync.WaitGroup, m *sync.RWMutex) {
  652. gotClient := make([]byte, 1)
  653. gotClient[0] = 0
  654. //wait until time is up
  655. for len(phase3Channel) == 0 {
  656. if time.Since(startTimeRound) > maxTimePerRound*2 {
  657. //tells follower that this worker is done
  658. writeToWError(followerConnection, gotClient, nil, 0)
  659. wg.Done()
  660. return
  661. }
  662. time.Sleep(1 * time.Second)
  663. }
  664. for clientConnection := range phase3Channel {
  665. clientsServedPhase3[round] = clientsServedPhase3[round] + 1
  666. if clientsServedPhase3[round]%1000 == 0 {
  667. fmt.Println("clientsServedPhase3", clientsServedPhase3[round])
  668. fmt.Println("timeTaken", time.Since(startPhase3).Seconds())
  669. }
  670. gotClient[0] = 1
  671. //tells follower that this worker got a clientConnection
  672. writeToWError(followerConnection, gotClient, nil, 0)
  673. //tells client current phase
  674. errorBool := writeToWError(clientConnection, phase, followerConnection, 2)
  675. if errorBool {
  676. contBool := handleClientDC(wg, followerConnection, phase3Channel)
  677. if contBool {
  678. continue
  679. } else {
  680. return
  681. }
  682. }
  683. /*
  684. possible Values
  685. 0 : new client
  686. leader expects sharedSecrets, expects pirQuery
  687. 1 : update needed
  688. leader sends topicList, performs local update of sharedSecret, expects pirQuery
  689. 2 : no update needed
  690. nothing
  691. */
  692. subPhase := make([]byte, 1)
  693. //gets the data for the current client
  694. m.RLock()
  695. var clientKeys = clientData[clientConnection.RemoteAddr()]
  696. m.RUnlock()
  697. var roundsParticipating = clientKeys.roundsParticipating
  698. //client participates for the first time
  699. if roundsParticipating == 0 {
  700. subPhase[0] = 0
  701. } else if roundsParticipating%roundsBeforeUpdate == 0 {
  702. subPhase[0] = 1
  703. } else {
  704. subPhase[0] = 2
  705. }
  706. //tells client what leader expects
  707. errorBool = writeToWError(clientConnection, subPhase, followerConnection, 2)
  708. if errorBool {
  709. contBool := handleClientDC(wg, followerConnection, phase3Channel)
  710. if contBool {
  711. continue
  712. } else {
  713. return
  714. }
  715. }
  716. //tells follower what will happen
  717. writeToWError(followerConnection, subPhase, nil, 0)
  718. //sends clients publicKey so follower knows which client is being served
  719. writeTo(followerConnection, clientKeys.PublicKey[:])
  720. //increases rounds participating for client
  721. clientKeys.roundsParticipating = roundsParticipating + 1
  722. //declaring variables here to prevent dupclicates later
  723. m.RLock()
  724. var sharedSecret [32]byte = clientData[clientConnection.RemoteAddr()].SharedSecret
  725. m.RUnlock()
  726. if subPhase[0] == 0 {
  727. errorBool := sendTopicLists(clientConnection, followerConnection, false)
  728. if errorBool {
  729. contBool := handleClientDC(wg, followerConnection, phase3Channel)
  730. if contBool {
  731. continue
  732. } else {
  733. return
  734. }
  735. }
  736. clientKeys, _, errorBool = handlePirQuery(clientKeys, clientConnection, followerConnection, int(subPhase[0]), false)
  737. if errorBool {
  738. contBool := handleClientDC(wg, followerConnection, phase3Channel)
  739. if contBool {
  740. continue
  741. } else {
  742. return
  743. }
  744. }
  745. } else if subPhase[0] == 1 {
  746. errorBool := sendTopicLists(clientConnection, followerConnection, false)
  747. if errorBool {
  748. contBool := handleClientDC(wg, followerConnection, phase3Channel)
  749. if contBool {
  750. continue
  751. } else {
  752. return
  753. }
  754. }
  755. //updates sharedSecret
  756. sharedSecret = sha256.Sum256(sharedSecret[:])
  757. clientKeys.SharedSecret = sharedSecret
  758. clientKeys, _, errorBool = handlePirQuery(clientKeys, clientConnection, followerConnection, int(subPhase[0]), false)
  759. if errorBool {
  760. contBool := handleClientDC(wg, followerConnection, phase3Channel)
  761. if contBool {
  762. continue
  763. } else {
  764. return
  765. }
  766. }
  767. }
  768. errorBool = getSendTweets(clientKeys, nil, clientConnection, followerConnection, m)
  769. if errorBool {
  770. contBool := handleClientDC(wg, followerConnection, phase3Channel)
  771. if contBool {
  772. continue
  773. } else {
  774. return
  775. }
  776. }
  777. wantsArchive, errorBool := readFrom(clientConnection, 1, followerConnection, 2)
  778. if errorBool {
  779. contBool := handleClientDC(wg, followerConnection, phase3Channel)
  780. if contBool {
  781. continue
  782. } else {
  783. return
  784. }
  785. }
  786. writeToWError(followerConnection, wantsArchive, nil, 0)
  787. if wantsArchive[0] == 1 && archiveTopicAmount > 0 {
  788. _, archiveQuerys, errorBool := handlePirQuery(clientKeys, clientConnection, followerConnection, -1, false)
  789. if errorBool {
  790. contBool := handleClientDC(wg, followerConnection, phase3Channel)
  791. if contBool {
  792. continue
  793. } else {
  794. return
  795. }
  796. }
  797. errorBool = getSendTweets(clientKeys, archiveQuerys, clientConnection, followerConnection, m)
  798. if errorBool {
  799. contBool := handleClientDC(wg, followerConnection, phase3Channel)
  800. if contBool {
  801. continue
  802. } else {
  803. return
  804. }
  805. }
  806. }
  807. //saves all changes for client
  808. m.Lock()
  809. clientData[clientConnection.RemoteAddr()] = clientKeys
  810. m.Unlock()
  811. phase1Channel <- clientConnection
  812. for {
  813. if time.Since(startTimeRound) < 2*maxTimePerRound {
  814. //this worker handles the next client
  815. if len(phase3Channel) > 0 {
  816. break
  817. //this worker waits for next client
  818. } else {
  819. time.Sleep(1 * time.Second)
  820. }
  821. //times up
  822. } else {
  823. //tells follower that this worker is done
  824. gotClient[0] = 0
  825. writeToWError(followerConnection, gotClient, nil, 0)
  826. wg.Done()
  827. return
  828. }
  829. }
  830. }
  831. }
  832. //returns true if there is another client
  833. func handleClientDC(wg *sync.WaitGroup, followerConnection net.Conn, channel chan net.Conn) bool {
  834. //loop that waits for new client or leaves phase1 if time is up
  835. for {
  836. if time.Since(startTimeRound) < maxTimePerRound {
  837. //this worker handles the next client
  838. if len(channel) > 0 {
  839. return true
  840. //this worker waits for next client
  841. } else {
  842. time.Sleep(1 * time.Second)
  843. }
  844. //times up
  845. } else {
  846. //tells follower that this worker is done
  847. gotClient := make([]byte, 1)
  848. gotClient[0] = 0
  849. writeTo(followerConnection, gotClient)
  850. wg.Done()
  851. return false
  852. }
  853. }
  854. }
  855. func createVirtualAddresses() []int {
  856. //array will be filled with unique random ascending values
  857. //adapted from: https://stackoverflow.com/questions/20039025/java-array-of-unique-randomly-generated-integers
  858. //+1 to have a position to evaluate each received message
  859. arraySize := int(dbWriteSize) + 1
  860. var maxInt int = int(math.Pow(2, 31))
  861. virtualAddresses := make([]int, arraySize)
  862. for i := 0; i < arraySize; i++ {
  863. virtualAddresses[i] = mr.Intn(maxInt)
  864. for j := 0; j < i; j++ {
  865. if virtualAddresses[i] == virtualAddresses[j] {
  866. i--
  867. break
  868. }
  869. }
  870. }
  871. sort.Ints(virtualAddresses)
  872. return virtualAddresses
  873. }
  874. func getSendVirtualAddress(pirQuery []byte, virtualAddresses []int, sharedSecret [32]byte, clientConnection, followerConnection net.Conn) bool {
  875. //xores all requested addresses into virtuallAddress
  876. virtualAddress := make([]byte, 4)
  877. for index, num := range pirQuery {
  878. if num == 1 {
  879. currentAddress := intToByte(virtualAddresses[index])
  880. for i := 0; i < 4; i++ {
  881. virtualAddress[i] = virtualAddress[i] ^ currentAddress[i]
  882. }
  883. }
  884. }
  885. //xores the sharedSecret
  886. for i := 0; i < 4; i++ {
  887. virtualAddress[i] = virtualAddress[i] ^ sharedSecret[i]
  888. }
  889. virtualAddressFollower, _ := readFrom(followerConnection, 4, nil, 0)
  890. //xores the data from follower
  891. for i := 0; i < 4; i++ {
  892. virtualAddress[i] = virtualAddress[i] ^ virtualAddressFollower[i]
  893. }
  894. errorBool := writeToWError(clientConnection, virtualAddress, followerConnection, 5)
  895. return errorBool
  896. }
  897. func getSendTweets(clientKeys clientKeys, archiveQuerys [][]byte, clientConnection, followerConnection net.Conn, m *sync.RWMutex) bool {
  898. //todo! repeat for archive
  899. tmpNeededSubscriptions := neededSubscriptions
  900. if tmpNeededSubscriptions > topicAmount {
  901. tmpNeededSubscriptions = topicAmount
  902. }
  903. if archiveQuerys != nil {
  904. tmpNeededSubscriptions = len(archiveQuerys)
  905. }
  906. //fmt.Println("tmpNeededSubscriptions", tmpNeededSubscriptions)
  907. tweets := make([][]byte, tmpNeededSubscriptions)
  908. for i := 0; i < tmpNeededSubscriptions; i++ {
  909. //gets all requested tweets
  910. if archiveQuerys == nil {
  911. tweets[i] = lib.GetTweets(clientKeys.PirQuery[i], dataLength, 0, *clientKeys.PublicKey)
  912. } else {
  913. tweets[i] = lib.GetTweets(archiveQuerys[i], dataLength, 1, *clientKeys.PublicKey)
  914. }
  915. //expand sharedSecret so it is of right length
  916. expandBy := len(tweets[i]) / 32
  917. var expandedSharedSecret []byte
  918. for i := 0; i < expandBy; i++ {
  919. expandedSharedSecret = append(expandedSharedSecret, clientKeys.SharedSecret[:]...)
  920. }
  921. //Xor's sharedSecret with all tweets
  922. lib.Xor(expandedSharedSecret[:], tweets[i])
  923. //fmt.Println(tweets[0])
  924. blockLength := len(tweets[i])
  925. receivedTweets, _ := readFrom(followerConnection, blockLength, nil, 0)
  926. //fmt.Println("receivedTweets", blockLength, len(receivedTweets))
  927. //fmt.Println("pubKey", *clientKeys.PublicKey, "Bytes", tweets, "follower", receivedTweets)
  928. lib.Xor(receivedTweets, tweets[i])
  929. }
  930. //sends tweets to client
  931. for i := 0; i < tmpNeededSubscriptions; i++ {
  932. tweetsLengthBytes := intToByte(len(tweets[i]))
  933. errorBool := writeToWError(clientConnection, tweetsLengthBytes, followerConnection, 2)
  934. if errorBool {
  935. return true
  936. }
  937. errorBool = writeToWError(clientConnection, tweets[i], followerConnection, 2)
  938. if errorBool {
  939. return true
  940. }
  941. }
  942. return false
  943. }
  944. func handlePirQuery(clientKeys clientKeys, clientConnection net.Conn, followerConnection net.Conn, subPhase int, doAuditing bool) (clientKeys, [][]byte, bool) {
  945. clientPublicKey := clientKeys.PublicKey
  946. //gets the msg length
  947. msgLengthBytes, errorBool := readFrom(clientConnection, 4, followerConnection, 5)
  948. if errorBool {
  949. return clientKeys, nil, true
  950. }
  951. msgLength := byteToInt(msgLengthBytes)
  952. //gets the leader box
  953. leaderBox, errorBool := readFrom(clientConnection, msgLength, followerConnection, 5)
  954. if errorBool {
  955. return clientKeys, nil, true
  956. }
  957. //gets the follower box
  958. followerBox, errorBool := readFrom(clientConnection, msgLength, followerConnection, 5)
  959. if errorBool {
  960. return clientKeys, nil, true
  961. }
  962. //todo! repeat for archive
  963. tmpNeededSubscriptions := neededSubscriptions
  964. if tmpNeededSubscriptions > topicAmount {
  965. tmpNeededSubscriptions = topicAmount
  966. }
  967. tmpTopicAmount := topicAmount
  968. if subPhase == -1 {
  969. archiveNeededSubscriptions, errorBool := readFrom(clientConnection, 4, followerConnection, 5)
  970. if errorBool {
  971. return clientKeys, nil, true
  972. }
  973. writeToWError(followerConnection, archiveNeededSubscriptions, nil, 0)
  974. tmpNeededSubscriptions = byteToInt(archiveNeededSubscriptions)
  975. tmpTopicAmount = archiveTopicAmount
  976. }
  977. if doAuditing {
  978. tmpNeededSubscriptions = 1
  979. tmpTopicAmount = int(dbWriteSize)
  980. }
  981. //send length to follower
  982. writeToWError(followerConnection, msgLengthBytes, nil, 0)
  983. //send box to follower
  984. writeToWError(followerConnection, followerBox, nil, 0)
  985. var decryptNonce [24]byte
  986. copy(decryptNonce[:], leaderBox[:24])
  987. decrypted, ok := box.Open(nil, leaderBox[24:], &decryptNonce, clientPublicKey, leaderPrivateKey)
  988. if !ok {
  989. fmt.Println("pirQuery decryption not ok")
  990. return clientKeys, nil, true
  991. }
  992. //if sharedSecret is send
  993. if subPhase == 0 {
  994. var tmpSharedSecret [32]byte
  995. for index := 0; index < 32; index++ {
  996. tmpSharedSecret[index] = decrypted[index]
  997. }
  998. clientKeys.SharedSecret = tmpSharedSecret
  999. decrypted = decrypted[32:]
  1000. }
  1001. if doAuditing {
  1002. result := make([][]byte, 1)
  1003. result[0] = decrypted
  1004. return clientKeys, result, false
  1005. }
  1006. //transforms byteArray to ints of wanted topics
  1007. pirQueryFlattened := decrypted
  1008. pirQuerys := make([][]byte, tmpNeededSubscriptions)
  1009. for i := range pirQuerys {
  1010. pirQuerys[i] = make([]byte, tmpTopicAmount)
  1011. }
  1012. for i := 0; i < tmpNeededSubscriptions; i++ {
  1013. pirQuerys[i] = pirQueryFlattened[i*tmpTopicAmount : (i+1)*tmpTopicAmount]
  1014. }
  1015. //sets the pirQuery for the client in case whe are not archiving, and not Auditing
  1016. if subPhase != -1 {
  1017. clientKeys.PirQuery = pirQuerys
  1018. }
  1019. return clientKeys, pirQuerys, false
  1020. }
  1021. func transformBytesToStringArray(topicsAsBytes []byte) []string {
  1022. var topics []string
  1023. var topic string
  1024. var position int = 0
  1025. for _, letter := range topicsAsBytes {
  1026. if string(letter) == "," {
  1027. topics[position] = topic
  1028. topic = ""
  1029. position++
  1030. } else {
  1031. topic = topic + string(letter)
  1032. }
  1033. }
  1034. return topics
  1035. }
  1036. func byteToInt(myBytes []byte) (x int) {
  1037. x = int(myBytes[3])<<24 + int(myBytes[2])<<16 + int(myBytes[1])<<8 + int(myBytes[0])
  1038. return
  1039. }
  1040. //returns true if error occured
  1041. func sendTopicLists(clientConnection, followerConnection net.Conn, setup bool) bool {
  1042. for i := 0; i < 2; i++ {
  1043. var topicList []byte
  1044. if i == 0 {
  1045. topicList, topicAmount = lib.GetTopicList(i)
  1046. } else {
  1047. topicList, archiveTopicAmount = lib.GetTopicList(i)
  1048. }
  1049. topicListLengthBytes := intToByte(len(topicList))
  1050. if !setup {
  1051. err := writeToWError(clientConnection, topicListLengthBytes, followerConnection, 5)
  1052. if err {
  1053. return true
  1054. }
  1055. err = writeToWError(clientConnection, topicList, followerConnection, 5)
  1056. if err {
  1057. return true
  1058. }
  1059. } else {
  1060. _, err := clientConnection.Write(topicListLengthBytes)
  1061. if err != nil {
  1062. return true
  1063. }
  1064. _, err = clientConnection.Write(topicList)
  1065. if err != nil {
  1066. return true
  1067. }
  1068. }
  1069. }
  1070. return false
  1071. }
  1072. //sends the array to the connection
  1073. func writeTo(connection net.Conn, array []byte) {
  1074. remainingLength := len(array)
  1075. for remainingLength > 0 {
  1076. if remainingLength >= mtu {
  1077. _, err := connection.Write(array[:mtu])
  1078. if err != nil {
  1079. panic(err)
  1080. }
  1081. array = array[mtu:]
  1082. remainingLength -= mtu
  1083. } else {
  1084. _, err := connection.Write(array)
  1085. if err != nil {
  1086. panic(err)
  1087. }
  1088. remainingLength = 0
  1089. }
  1090. }
  1091. }
  1092. func writeToWError(connection net.Conn, array []byte, followerConnection net.Conn, size int) bool {
  1093. if connection.RemoteAddr().String() == follower {
  1094. arrayWError := make([]byte, 1)
  1095. arrayWError = append(arrayWError, array[:]...)
  1096. remainingLength := len(arrayWError)
  1097. for remainingLength > 0 {
  1098. if remainingLength >= mtu {
  1099. _, err := connection.Write(arrayWError[:mtu])
  1100. if err != nil {
  1101. return handleError(connection, followerConnection, size, err)
  1102. }
  1103. arrayWError = arrayWError[mtu:]
  1104. remainingLength -= mtu
  1105. } else {
  1106. _, err := connection.Write(arrayWError)
  1107. if err != nil {
  1108. return handleError(connection, followerConnection, size, err)
  1109. }
  1110. remainingLength = 0
  1111. }
  1112. }
  1113. } else {
  1114. remainingLength := len(array)
  1115. for remainingLength > 0 {
  1116. if remainingLength >= mtu {
  1117. _, err := connection.Write(array[:mtu])
  1118. if err != nil {
  1119. return handleError(connection, followerConnection, size, err)
  1120. }
  1121. array = array[mtu:]
  1122. remainingLength -= mtu
  1123. } else {
  1124. _, err := connection.Write(array)
  1125. if err != nil {
  1126. return handleError(connection, followerConnection, size, err)
  1127. }
  1128. remainingLength = 0
  1129. }
  1130. }
  1131. }
  1132. return false
  1133. }
  1134. func handleError(connection, followerConnection net.Conn, size int, err error) bool {
  1135. if err != nil {
  1136. //lets follower know that client has disconnected unexpectedly
  1137. if connection.RemoteAddr().String() != follower {
  1138. if size > mtu {
  1139. fmt.Println("have a look here")
  1140. }
  1141. fmt.Println("error", err)
  1142. array := make([]byte, size)
  1143. array[0] = 1
  1144. _, err = followerConnection.Write(array)
  1145. if err != nil {
  1146. panic(err)
  1147. }
  1148. return true
  1149. } else {
  1150. panic(err)
  1151. }
  1152. }
  1153. return false
  1154. }
  1155. //reads an array which is returned and of size "size" from the connection
  1156. //returns true if error occured
  1157. func readFrom(connection net.Conn, size int, followerConnection net.Conn, sizeError int) ([]byte, bool) {
  1158. var array []byte
  1159. remainingSize := size
  1160. for remainingSize > 0 {
  1161. var err error
  1162. toAppend := make([]byte, mtu)
  1163. if remainingSize > mtu {
  1164. _, err = connection.Read(toAppend)
  1165. array = append(array, toAppend...)
  1166. remainingSize -= mtu
  1167. } else {
  1168. _, err = connection.Read(toAppend[:remainingSize])
  1169. array = append(array, toAppend[:remainingSize]...)
  1170. remainingSize = 0
  1171. }
  1172. if err != nil {
  1173. //lets follower know that client has disconnected unexpectedly
  1174. if connection.RemoteAddr().String() != follower {
  1175. fmt.Println(err)
  1176. array := make([]byte, sizeError)
  1177. array[0] = 1
  1178. _, err = followerConnection.Write(array)
  1179. if err != nil {
  1180. panic(err)
  1181. }
  1182. return nil, true
  1183. } else {
  1184. panic(err)
  1185. }
  1186. }
  1187. }
  1188. return array, false
  1189. }
  1190. func intToByte(myInt int) (retBytes []byte) {
  1191. retBytes = make([]byte, 4)
  1192. retBytes[3] = byte((myInt >> 24) & 0xff)
  1193. retBytes[2] = byte((myInt >> 16) & 0xff)
  1194. retBytes[1] = byte((myInt >> 8) & 0xff)
  1195. retBytes[0] = byte(myInt & 0xff)
  1196. return
  1197. }