github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/go/util/fdmessagestream.go (about) 1 // Copyright (c) 2014, Kevin Walsh. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package util 16 17 import ( 18 "errors" 19 "fmt" 20 "net" 21 "os" 22 "strings" 23 ) 24 25 // DeserializeFileMessageStream takes a string description of the form 26 // "tao::FileMessageChannel(X)" and returns a MessageStream that uses file 27 // X to communicate. 28 func DeserializeFileMessageStream(s string) (*MessageStream, error) { 29 r := strings.TrimPrefix(s, "tao::FileMessageChannel(") 30 if r == s { 31 return nil, errors.New("unrecognized channel spec " + s) 32 } 33 filename := strings.TrimSuffix(r, ")") 34 if filename == r { 35 return nil, errors.New("unrecognized channel spec " + s) 36 } 37 38 rw, err := os.OpenFile(filename, os.O_RDWR, 0700) 39 if err != nil { 40 return nil, err 41 } 42 return NewMessageStream(rw), nil 43 } 44 45 // DeserializeFDMessageStream takes a string description of the form 46 // "tao::FDMessageStream(X, Y)" and returns a MessageStream that uses file 47 // descriptor X as the reader and file descriptor Y as the writer. 48 func DeserializeFDMessageStream(s string) (*MessageStream, error) { 49 var readfd, writefd uintptr 50 _, err := fmt.Sscanf(s, "tao::FDMessageChannel(%d, %d)", &readfd, &writefd) 51 if err != nil { 52 return nil, errors.New("unrecognized channel spec " + s) 53 } 54 if readfd == writefd { 55 rw := os.NewFile(readfd, "read/write pipe") 56 return NewMessageStream(rw), nil 57 } 58 r := os.NewFile(readfd, "read pipe") 59 w := os.NewFile(writefd, "write pipe") 60 rw := NewPairReadWriteCloser(r, w) 61 return NewMessageStream(rw), nil 62 } 63 64 // DeserializeUnixSocketMessageStream takes a string filename and returns a 65 // MessageStream that is based on the Unix socket for this file. 66 func DeserializeUnixSocketMessageStream(f string) (*MessageStream, error) { 67 conn, err := net.Dial("unix", f) 68 if err != nil { 69 return nil, err 70 } 71 72 return NewMessageStream(conn), nil 73 }