How to use handshake method of ipc Package

Best Syzkaller code snippet using ipc.handshake

uapi.go

Source:uapi.go Github

copy

Full Screen

...83 // Do the work in an anonymous function so that we can use defer.84 func() {85 peer.RLock()86 defer peer.RUnlock()87 keyf("public_key", (*[32]byte)(&peer.handshake.remoteStatic))88 keyf("preshared_key", (*[32]byte)(&peer.handshake.presharedKey))89 sendf("protocol_version=1")90 if peer.endpoint != nil {91 sendf("endpoint=%s", peer.endpoint.DstToString())92 }93 /* Custom change begin */94 sendf("handshakeAttempts=%d", peer.timers.actualHandshakeAttempts)95 keyf("chainKey", (*[32]byte)(&peer.handshake.sharedUniqueSecret))96 //sendf("chainKey=%s", (*[32]byte)(&peer.handshake.remoteEphemeral))97 //device.log.Verbosef("%v - chainKey: %s", peer, string(peer.handshake.chainKey[:]))98 //device.log.Verbosef("%v - chainKey: %s", peer, string(peer.handshake.remoteEphemeral[:]))99 /* Custom change end */100 nano := atomic.LoadInt64(&peer.stats.lastHandshakeNano)101 secs := nano / time.Second.Nanoseconds()102 nano %= time.Second.Nanoseconds()103 sendf("last_handshake_time_sec=%d", secs)104 sendf("last_handshake_time_nsec=%d", nano)105 sendf("tx_bytes=%d", atomic.LoadUint64(&peer.stats.txBytes))106 sendf("rx_bytes=%d", atomic.LoadUint64(&peer.stats.rxBytes))107 sendf("persistent_keepalive_interval=%d", atomic.LoadUint32(&peer.persistentKeepaliveInterval))108 device.allowedips.EntriesForPeer(peer, func(prefix netip.Prefix) bool {109 sendf("allowed_ip=%s", prefix.String())110 return true111 })112 }()113 }114 }()115 // send lines (does not require resource locks)116 if _, err := w.Write(buf.Bytes()); err != nil {117 return ipcErrorf(ipc.IpcErrorIO, "failed to write output: %w", err)118 }119 return nil120}121// IpcSetOperation implements the WireGuard configuration protocol "set" operation.122// See https://www.wireguard.com/xplatform/#configuration-protocol for details.123func (device *Device) IpcSetOperation(r io.Reader) (err error) {124 device.ipcMutex.Lock()125 defer device.ipcMutex.Unlock()126 defer func() {127 if err != nil {128 device.log.Errorf("%v", err)129 }130 }()131 peer := new(ipcSetPeer)132 deviceConfig := true133 scanner := bufio.NewScanner(r)134 for scanner.Scan() {135 line := scanner.Text()136 if line == "" {137 // Blank line means terminate operation.138 peer.handlePostConfig()139 return nil140 }141 parts := strings.Split(line, "=")142 if len(parts) != 2 {143 return ipcErrorf(ipc.IpcErrorProtocol, "failed to parse line %q, found %d =-separated parts, want 2", line, len(parts))144 }145 key := parts[0]146 value := parts[1]147 if key == "public_key" {148 if deviceConfig {149 deviceConfig = false150 }151 peer.handlePostConfig()152 // Load/create the peer we are now configuring.153 err := device.handlePublicKeyLine(peer, value)154 if err != nil {155 return err156 }157 continue158 }159 var err error160 if deviceConfig {161 err = device.handleDeviceLine(key, value)162 } else {163 err = device.handlePeerLine(peer, key, value)164 }165 if err != nil {166 return err167 }168 }169 peer.handlePostConfig()170 if err := scanner.Err(); err != nil {171 return ipcErrorf(ipc.IpcErrorIO, "failed to read input: %w", err)172 }173 return nil174}175func (device *Device) handleDeviceLine(key, value string) error {176 switch key {177 case "private_key":178 var sk NoisePrivateKey179 err := sk.FromMaybeZeroHex(value)180 if err != nil {181 return ipcErrorf(ipc.IpcErrorInvalid, "failed to set private_key: %w", err)182 }183 device.log.Verbosef("UAPI: Updating private key")184 device.SetPrivateKey(sk)185 case "listen_port":186 port, err := strconv.ParseUint(value, 10, 16)187 if err != nil {188 return ipcErrorf(ipc.IpcErrorInvalid, "failed to parse listen_port: %w", err)189 }190 // update port and rebind191 device.log.Verbosef("UAPI: Updating listen port")192 device.net.Lock()193 device.net.port = uint16(port)194 device.net.Unlock()195 if err := device.BindUpdate(); err != nil {196 return ipcErrorf(ipc.IpcErrorPortInUse, "failed to set listen_port: %w", err)197 }198 case "fwmark":199 mark, err := strconv.ParseUint(value, 10, 32)200 if err != nil {201 return ipcErrorf(ipc.IpcErrorInvalid, "invalid fwmark: %w", err)202 }203 device.log.Verbosef("UAPI: Updating fwmark")204 if err := device.BindSetMark(uint32(mark)); err != nil {205 return ipcErrorf(ipc.IpcErrorPortInUse, "failed to update fwmark: %w", err)206 }207 case "replace_peers":208 if value != "true" {209 return ipcErrorf(ipc.IpcErrorInvalid, "failed to set replace_peers, invalid value: %v", value)210 }211 device.log.Verbosef("UAPI: Removing all peers")212 device.RemoveAllPeers()213 default:214 return ipcErrorf(ipc.IpcErrorInvalid, "invalid UAPI device key: %v", key)215 }216 return nil217}218// An ipcSetPeer is the current state of an IPC set operation on a peer.219type ipcSetPeer struct {220 *Peer // Peer is the current peer being operated on221 dummy bool // dummy reports whether this peer is a temporary, placeholder peer222 created bool // new reports whether this is a newly created peer223 pkaOn bool // pkaOn reports whether the peer had the persistent keepalive turn on224}225func (peer *ipcSetPeer) handlePostConfig() {226 if peer.Peer == nil || peer.dummy {227 return228 }229 if peer.created {230 peer.disableRoaming = peer.device.net.brokenRoaming && peer.endpoint != nil231 }232 if peer.device.isUp() {233 peer.Start()234 if peer.pkaOn {235 peer.SendKeepalive()236 }237 peer.SendStagedPackets()238 }239}240func (device *Device) handlePublicKeyLine(peer *ipcSetPeer, value string) error {241 // Load/create the peer we are configuring.242 var publicKey NoisePublicKey243 err := publicKey.FromHex(value)244 if err != nil {245 return ipcErrorf(ipc.IpcErrorInvalid, "failed to get peer by public key: %w", err)246 }247 // Ignore peer with the same public key as this device.248 device.staticIdentity.RLock()249 peer.dummy = device.staticIdentity.publicKey.Equals(publicKey)250 device.staticIdentity.RUnlock()251 if peer.dummy {252 peer.Peer = &Peer{}253 } else {254 peer.Peer = device.LookupPeer(publicKey)255 }256 peer.created = peer.Peer == nil257 if peer.created {258 peer.Peer, err = device.NewPeer(publicKey)259 if err != nil {260 return ipcErrorf(ipc.IpcErrorInvalid, "failed to create new peer: %w", err)261 }262 device.log.Verbosef("%v - UAPI: Created", peer.Peer)263 }264 return nil265}266func (device *Device) handlePeerLine(peer *ipcSetPeer, key, value string) error {267 switch key {268 case "update_only":269 // allow disabling of creation270 if value != "true" {271 return ipcErrorf(ipc.IpcErrorInvalid, "failed to set update only, invalid value: %v", value)272 }273 if peer.created && !peer.dummy {274 device.RemovePeer(peer.handshake.remoteStatic)275 peer.Peer = &Peer{}276 peer.dummy = true277 }278 case "remove":279 // remove currently selected peer from device280 if value != "true" {281 return ipcErrorf(ipc.IpcErrorInvalid, "failed to set remove, invalid value: %v", value)282 }283 if !peer.dummy {284 device.log.Verbosef("%v - UAPI: Removing", peer.Peer)285 device.RemovePeer(peer.handshake.remoteStatic)286 }287 peer.Peer = &Peer{}288 peer.dummy = true289 case "preshared_key":290 device.log.Verbosef("%v - UAPI: Updating preshared key", peer.Peer)291 peer.handshake.mutex.Lock()292 err := peer.handshake.presharedKey.FromHex(value)293 peer.handshake.mutex.Unlock()294 if err != nil {295 return ipcErrorf(ipc.IpcErrorInvalid, "failed to set preshared key: %w", err)296 }297 case "endpoint":298 device.log.Verbosef("%v - UAPI: Updating endpoint", peer.Peer)299 endpoint, err := device.net.bind.ParseEndpoint(value)300 if err != nil {301 return ipcErrorf(ipc.IpcErrorInvalid, "failed to set endpoint %v: %w", value, err)302 }303 peer.Lock()304 defer peer.Unlock()305 peer.endpoint = endpoint306 case "persistent_keepalive_interval":307 device.log.Verbosef("%v - UAPI: Updating persistent keepalive interval", peer.Peer)308 secs, err := strconv.ParseUint(value, 10, 16)309 if err != nil {310 return ipcErrorf(ipc.IpcErrorInvalid, "failed to set persistent keepalive interval: %w", err)311 }312 old := atomic.SwapUint32(&peer.persistentKeepaliveInterval, uint32(secs))313 // Send immediate keepalive if we're turning it on and before it wasn't on.314 peer.pkaOn = old == 0 && secs != 0315 case "replace_allowed_ips":316 device.log.Verbosef("%v - UAPI: Removing all allowedips", peer.Peer)317 if value != "true" {318 return ipcErrorf(ipc.IpcErrorInvalid, "failed to replace allowedips, invalid value: %v", value)319 }320 if peer.dummy {321 return nil322 }323 device.allowedips.RemoveByPeer(peer.Peer)324 case "allowed_ip":325 device.log.Verbosef("%v - UAPI: Adding allowedip", peer.Peer)326 prefix, err := netip.ParsePrefix(value)327 if err != nil {328 return ipcErrorf(ipc.IpcErrorInvalid, "failed to set allowed ip: %w", err)329 }330 if peer.dummy {331 return nil332 }333 device.allowedips.Insert(prefix, peer.Peer)334 case "protocol_version":335 if value != "1" {336 return ipcErrorf(ipc.IpcErrorInvalid, "invalid protocol version: %v", value)337 }338 default:339 return ipcErrorf(ipc.IpcErrorInvalid, "invalid UAPI peer key: %v", key)340 }341 return nil342}343func (device *Device) IpcGet() (string, error) {344 buf := new(strings.Builder)345 if err := device.IpcGetOperation(buf); err != nil {346 return "", err347 }348 return buf.String(), nil349}350func (device *Device) IpcSet(uapiConf string) error {351 return device.IpcSetOperation(strings.NewReader(uapiConf))352}353/* Custom change begin */354func (device *Device) IpcSetPSK(uapiConf string) error {355 return device.IpcSetPSKOperation(strings.NewReader(uapiConf))356}357func (device *Device) IpcSetPSKOperation(r io.Reader) (err error) {358 // Lock device359 device.ipcMutex.Lock()360 defer device.ipcMutex.Unlock()361 defer func() {362 if err != nil {363 device.log.Errorf("%v", err)364 }365 }()366 // Read config367 peer := new(ipcSetPeer)368 peer.Peer = &Peer{}369 scanner := bufio.NewScanner(r)370 for scanner.Scan() {371 line := scanner.Text()372 device.log.Verbosef("%v - UAPI: Updating preshared key", peer.Peer)373 if line == "" {374 // Blank line means terminate operation.375 peer.handlePostConfig()376 return nil377 }378 parts := strings.Split(line, "=")379 if len(parts) != 2 {380 return ipcErrorf(ipc.IpcErrorProtocol, "failed to parse line %q, found %d =-separated parts, want 2", line, len(parts))381 }382 key := parts[0]383 value := parts[1]384 // Get pk if line is public_key385 if key == "public_key" {386 var publicKey NoisePublicKey387 err := publicKey.FromHex(value)388 if err != nil {389 return ipcErrorf(ipc.IpcErrorInvalid, "failed to get peer by public key: %w", err)390 }391 peer.Peer = device.LookupPeer(publicKey)392 }393 // loadPSK if line is preshared_key394 if key == "preshared_key" {395 if peer == nil {396 return ipcErrorf(ipc.IpcErrorInvalid, "failed to find peer: %w", err)397 }398 peer.handshake.mutex.Lock()399 err := peer.handshake.presharedKey.FromHex(value)400 peer.handshake.mutex.Unlock()401 if err != nil {402 return ipcErrorf(ipc.IpcErrorInvalid, "failed to set preshared key: %w", err)403 }404 peer.handlePostConfig()405 }406 }407 if err := scanner.Err(); err != nil {408 return ipcErrorf(ipc.IpcErrorIO, "failed to read input: %w", err)409 }410 return nil411}412/* Custom change end */413func (device *Device) IpcHandle(socket net.Conn) {414 defer socket.Close()...

Full Screen

Full Screen

handshake

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ipc := ipc.Ipc{}4 ipc.Handshake()5 fmt.Println("Handshake completed")6}7import (8func main() {9 ipc := ipc.Ipc{}10 ipc.Handshake()11 fmt.Println("Handshake completed")12}13import (14func main() {15 ipc := ipc.Ipc{}16 ipc.Handshake()17 fmt.Println("Handshake completed")18}19import (20func main() {21 ipc := ipc.Ipc{}22 ipc.Handshake()23 fmt.Println("Handshake completed")24}25import (26func main() {27 ipc := ipc.Ipc{}28 ipc.Handshake()29 fmt.Println("Handshake completed")30}31import (32func main() {33 ipc := ipc.Ipc{}34 ipc.Handshake()35 fmt.Println("Handshake completed")36}37import (38func main() {39 ipc := ipc.Ipc{}40 ipc.Handshake()41 fmt.Println("Handshake completed")42}43import (44func main() {45 ipc := ipc.Ipc{}46 ipc.Handshake()47 fmt.Println("Handshake completed")48}49import (50func main() {51 ipc := ipc.Ipc{}52 ipc.Handshake()53 fmt.Println("Handshake completed")54}55import (

Full Screen

Full Screen

handshake

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 pipe, err := syscall.Mkfifo("/tmp/fifo", 0666)4 if err != nil {5 fmt.Println(err)6 os.Exit(1)7 }8 fmt.Println("Pipe created: ", pipe)9}10import (11func main() {12 f, err := os.OpenFile("/tmp/fifo", os.O_RDONLY, 0666)13 if err != nil {14 fmt.Println(err)15 os.Exit(1)16 }17 data, err := ioutil.ReadAll(f)18 if err != nil {19 fmt.Println(err)20 os.Exit(1)21 }22 fmt.Println("Data read from pipe: ", string(data))23}24import (25func main() {26 f, err := os.OpenFile("/tmp/fifo", os.O_WRONLY, 0666)27 if err != nil {28 fmt.Println(err)29 os.Exit(1)30 }31 _, err = f.Write([]byte("Hello, World!"))32 if err != nil {33 fmt.Println(err)34 os.Exit(1)35 }36}

Full Screen

Full Screen

handshake

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 var channel = make(chan string)4 go ipc.HandShake(channel)5 fmt.Println(<-channel)6 fmt.Println(<-channel)7}8import (9func main() {10 var channel = make(chan string)11 go ipc.HandShake(channel)12 fmt.Println(<-channel)13 fmt.Println(<-channel)14}15import (16func main() {17 var channel = make(chan string)18 go ipc.HandShake(channel)19 fmt.Println(<-channel)20 fmt.Println(<-channel)21}22import (23func main() {24 var channel = make(chan string)25 go ipc.HandShake(channel)26 fmt.Println(<-channel)27 fmt.Println(<-channel)28}29import (30func main() {31 var channel = make(chan string)32 go ipc.HandShake(channel)33 fmt.Println(<-channel)34 fmt.Println(<-channel)35}36import (37func main() {38 var channel = make(chan string)39 go ipc.HandShake(channel)40 fmt.Println(<-channel)41 fmt.Println(<-channel)42}43import (44func main() {45 var channel = make(chan string)46 go ipc.HandShake(channel)47 fmt.Println(<-channel)48 fmt.Println(<-channel)49}

Full Screen

Full Screen

handshake

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ipc := IPC.NewIPC()4 ipc.SetChannelName("testChannel")5 ipc.SetTimeout(5 * time.Second)6 ipc.SetHandshakeMethod("handshake")7 ipc.SetHandshakeMessage("hello")8 ipc.SetHandshakeResponse("hello")9 ipc.SetHandshakeResponseTimeout(5 * time.Second)10 ipc.SetHandshakeResponseExpected(true)

Full Screen

Full Screen

handshake

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 r, w, err := os.Pipe()4 if err != nil {5 fmt.Println(err)6 os.Exit(1)7 }8 process, err := os.StartProcess("./1", []string{"1"}, &os.ProcAttr{9 Env: os.Environ(),10 Files: []*os.File{r, w, w},11 Sys: &syscall.SysProcAttr{},12 })13 if err != nil {14 fmt.Println(err)15 os.Exit(1)16 }17 w.Write([]byte("Hello from 2"))18 process.Wait()19}20import (21func main() {22 r, w, err := os.Pipe()23 if err != nil {24 fmt.Println(err)25 os.Exit(1)26 }27 process, err := os.StartProcess("./2", []string{"2"}, &os.ProcAttr{28 Env: os.Environ(),29 Files: []*os.File{r, w, w},30 Sys: &syscall.SysProcAttr{},31 })32 if err != nil {33 fmt.Println(err)34 os.Exit(1)35 }36 buf := make([]byte, 100)37 r.Read(buf)38 fmt.Println(string(buf))39 process.Wait()40}41import (42func main() {

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful