Swift getaddrinfo - swift

POSIX getaddrinfo allocates memory that must later be freed using freeaddrinfo.
See http://manpages.ubuntu.com/manpages/xenial/en/man3/getaddrinfo.3.html
To simplify the API, I've created this function:
import Foundation
enum SystemError: Swift.Error {
case getaddrinfo(Int32, Int32?)
}
public func getaddrinfo(node: String?, service: String?, hints: addrinfo?) throws -> [addrinfo] {
var err: Int32
var res: UnsafeMutablePointer<addrinfo>?
if var hints = hints {
err = getaddrinfo(node, service, &hints, &res)
} else {
err = getaddrinfo(node, service, nil, &res)
}
if err == EAI_SYSTEM {
throw SystemError.getaddrinfo(err, errno)
}
if err != 0 {
throw SystemError.getaddrinfo(err, nil)
}
defer {
freeaddrinfo(res)
}
var result = [addrinfo]()
var ai = res?.pointee
while ai != nil {
result.append(ai!)
ai = ai!.ai_next?.pointee
}
return result
}
I don't feel that the function is correct, though.
How can the Swift memory model know that getaddrinfo allocates memory, and that Swift should not overwrite that memory with own stuff?
How can Swift know that freeaddrinfo deletes the whole list, and that it should copy out ai information that has been assigned to the result array?
What's the correct way to interface with getaddrinfo?

Memory allocated by getaddrinfo (e.g. by malloc) will not be given to any other
dynamic memory allocation function in the same running process
until released by freeaddrinfo (e.g. by free).
Therefore the Swift runtime will not trample on that memory (if we
assume that it has no programming errors such as wrong pointer calculations).
Also struct addrinfo is a value type, so
result.append(ai!)
will append a copy of the pointed-to structure to the array.
But there is still a problem. Some members of struct addrinfo
are pointers
public var ai_canonname: UnsafeMutablePointer<Int8>! /* canonical name for hostname */
public var ai_addr: UnsafeMutablePointer<sockaddr>! /* binary address */
which may point into the memory allocated by getaddrinfo and therefore
invalid after freeaddrinfo, and dereferencing them after your
function returns causes undefined behaviour.
Therefore you must either postpone the freeaddrinfo until the
address list is not needed anymore, or copy the information.
This is a bit cumbersome because ai_addr may point to a
IPv4 or IPv6 socket address structure which have different length.
The following code demonstrates how the address list can be copied
to an array of sockaddr_storage structures (which are large enough
to hold any IP address). This code has not been thoroughly tested,
so use it with care.
public func getaddrinfo(node: String, service: String, hints: addrinfo?) throws -> [sockaddr_storage] {
var err: Int32
var res: UnsafeMutablePointer<addrinfo>?
if var hints = hints {
err = getaddrinfo(node, service, &hints, &res)
} else {
err = getaddrinfo(node, service, nil, &res)
}
if err == EAI_SYSTEM {
throw SystemError.getaddrinfo(err, errno)
}
if err != 0 {
throw SystemError.getaddrinfo(err, nil)
}
defer {
freeaddrinfo(res)
}
guard let firstAddr = res else {
return []
}
var result = [sockaddr_storage]()
for addr in sequence(first: firstAddr, next: { $0.pointee.ai_next }) {
var sockAddr = sockaddr_storage()
memcpy(&sockAddr, addr.pointee.ai_addr, Int(addr.pointee.ai_addrlen))
result.append(sockAddr)
}
return result
}
Remarks:
If you are only interested in a single address family then
you can replace sockaddr_storage by sockaddr_in or
sockaddr_in6.
I have made the node and service parameters non-optional.
The reason is that Swift currently has a Bug when passing more
than one optional String to a C function, see
Why does Swift return an unexpected pointer when converting an optional String into an UnsafePointer?.
sequence() is used here to traverse the linked list instead of
a while loop.

Related

How to insert slice

I have a postgres db that I would like to generate tables for and write to using Gorp, however I get an error message when I try to insert due to the slices contained within my structs "sql: converting argument $4 type: unsupported type []core.EmbeddedStruct, a slice of struct.
My structs look as follows:
type Struct1 struct {
ID string
Name string
Location string
EmbeddedStruct []EmbeddedStruct
}
type EmbeddedStruct struct {
ID string
Name string
struct1Id string
EmbeddedStruct2 []EmbeddedStruct2
}
type EmbeddedStruct2 struct {
ID string
Name string
embeddedStructId string
}
func (repo *PgStruct1Repo) Write(t *core.Struct1) error {
trans, err := createTransaction(repo.dbMap)
defer closeTransaction(trans)
if err != nil {
return err
}
// Check to see if struct1 item already exists
exists, err := repo.exists(t.ID, trans)
if err != nil {
return err
}
if !exists {
log.Debugf("saving new struct1 with ID %s", t.ID)
err = trans.Insert(t)
if err != nil {
return err
}
return nil
}
return nil
}
Does anyone have any experience with/or know if Gorp supports inserting slices? From what I've read it seems to only support slices for SELECT statements
Gorp supports inserting a variadic number of slices, so if you have a slice records, you can do:
err = db.Insert(records...)
However, from your question it seems you want to save a single record that has a slice struct field.
https://github.com/go-gorp/gorp
gorp doesn't know anything about the relationships between your structs (at least not yet).
So, you have to handle the relationship yourself. The way I personally would solve this issue is to have Gorp ignore the slice on the parent:
type Struct1 struct {
ID string
Name string
Location string
EmbeddedStruct []EmbeddedStruct `db:"-"`
}
And then use the PostInsert hook to save the EmbeddedStruct (side note, this is a poor name as it is not actually an embedded struct)
func (s *Struct1) PostInsert(sql gorp.SqlExecutor) error {
for i := range s.EmbeddedStruct {
s.EmbeddedStruct[i].struct1Id = s.ID
}
return sql.Insert(s.EmbeddedStruct...)
}
And then repeat the process on EmbeddedStruct2.
Take care to setup the relationships properly on the DB side to ensure referential integrity (e.g. ON DELETE CASCADE / RESTRICT), and it would probably be a good idea to wrap the whole thing in a transaction.

Prevent runtime panic in bson.ObjectIdHex

i'm trying to convert string of objectid to bson ObjectId format with mgo,
errCheck := d.C("col").FindId(bson.ObjectIdHex(obid[0])).One(&Result)
idk why, but if i give a wrong / invalid input string, my application got runtime panic
how i can prevent that ? thank you
bson.ObjectIdHex() documents that it will panic if you pass an invalid object id:
ObjectIdHex returns an ObjectId from the provided hex representation. Calling this function with an invalid hex representation will cause a runtime panic. See the IsObjectIdHex function.
If you want to avoid this, first check your input string using bson.IsObjectIdHex(), and only proceed to call bson.ObjectIdHex() if your input is valid:
if bson.IsObjectIdHex(obid[0]) {
// It's valid, calling bson.ObjectIdHex() will not panic...
}
As #icza said in the last answer. you should check validity if ObjectId.
And you can use panic recover defer to handle any kind of error in future
package main
import (
"fmt"
"gopkg.in/mgo.v2/bson"
"path/filepath"
"runtime"
"strings"
)
func main() {
r := Result{}
getData(&r)
}
func IdentifyPanic() string {
var name, file string
var line int
var pc [16]uintptr
n := runtime.Callers(3, pc[:])
for _, pc := range pc[:n] {
fn := runtime.FuncForPC(pc)
if fn == nil {
continue
}
file, line = fn.FileLine(pc)
name = fn.Name()
if !strings.HasPrefix(name, "runtime.") {
break
}
}
file = filepath.Base(file)
switch {
case name != "":
return fmt.Sprintf("%v:%v", file, line)
case file != "":
return fmt.Sprintf("%v:%v", file, line)
}
return fmt.Sprintf("pc:%x", pc)
}
type Result struct {
success int
data string
}
func getData(result *Result){
defer func() {
if err := recover(); err != nil {
ip := IdentifyPanic()
errorMessage := fmt.Sprintf("%s Error: %s", ip, err)
fmt.Println(errorMessage)
result.success = 0
}
}()
if bson.IsObjectIdHex(obid[0]) { // this line copied from #icza answer
// It's valid, calling bson.ObjectIdHex() will not panic... // this line copied from #icza answer
errCheck := d.C("col").FindId(bson.ObjectIdHex(obid[0])).One(&res)
result.success = 1
result.data = "your result (res). this is just the exam"
}else{
result.success = 0
}
}

Reading Data from Socket Golang

I'm trying to read data from a telnet session in golang. I wrote the following functions in an attempt to accomplish this.
Initially I was having an issue where I was reading from a socket with no data so it would lock and never return. BufferSocketData is my attempt to work around this issue as I can't know if there is data to read. The idea is it will wait 1 second before determining there is not data in the socket and return an empty string.
GetData seems to work the first time there is new data in the buffer, but beyond that it gets no new data. I'm sure this has something to do with my use of goroutines and channels, I'm new to go and I'm sure I'm not using them correctly.
Any ideas as to why my subsequent reads return no data?
/*
ReadDataFromSocket - Attempts to read any data in the socket.
*/
func ReadDataFromSocket(sock io.Reader, c chan string) {
var recvData = make([]byte, 1024)
var numBytes, _ = sock.Read(recvData)
c <- string(recvData[:numBytes])
}
/*
BufferSocketData - Read information from the socket and store it in the buffer.
*/
func (tn *TelnetLib) BufferSocketData(inp chan string, out chan string) {
var data string
var timeout int64 = 1000 // 1 second timeout.
var start = utils.GetTimestamp()
for utils.GetTimestamp()-start < timeout {
select {
case data = <-inp:
default:
}
if data != "" {
break
}
}
out <- data
}
/*
GetData - Start goroutines to get and buffer data.
*/
func (tn *TelnetLib) GetData() {
var sockCh = make(chan string)
var buffCh = make(chan string)
go ReadDataFromSocket(tn.Conn, sockCh)
go tn.BufferSocketData(sockCh, buffCh)
var data = <-buffCh
if data != "" {
tn.Buffer += data
}
}
Please let me know if you need any additional information.
Use SetReadDeadline to read data with a time limit:
func (tn *TelnetLib) GetData() {
tn.Conn.SetReadDeadline(time.Second)
recvData := make([]byte, 1024)
n, err := tn.Conn.Read(recvData)
if n > 0 {
// do something with recvData[:n]
}
if e, ok := err.(interface{ Timeout() bool }); ok && e.Timeout() {
// handle timeout
} else if err != nil {
// handle error
}
}
Note that a single call Read may not read all data sent by the peer. You may want to accumulate data by calling Read in a loop or call io.ReadFull.

Proper method to hash an arbitrary object

I am writing a data structure that needs to hash an arbitrary object. The following function seems to fail if I give an int is the parameter.
func Hash( obj interface{} ) []byte {
digest := md5.New()
if err := binary.Write(digest, binary.LittleEndian, obj); err != nil {
panic(err)
}
return digest.Sum()
}
Calling this on an int results in:
panic: binary.Write: invalid type int
What is the right way to do this?
I found that a good way to do this is to serialize the object using the "gob" package, along the following lines:
var (
digest = md5.New()
encoder = gob.NewEncoder(digest)
)
func Hash(obj interface{}) []byte {
digest.Reset()
if err := encoder.Encode(obj); err != nil {
panic(err)
}
return digest.Sum()
}
Edit: This does not work as intended (see below).
binary.Write writes "a fixed-size value or a pointer to a fixed-size value." Type int is not a fixed size value; int is "either 32 or 64 bits." Use a fixed-size value like int32.

How to get Domain Name of IP address and IP address from Domain Name in Objective C?

I am able to get the current IP address of my device/machine that I am using - by using this question's answer.
I have gone through this question. Java allows to get the IP address from a domain name. Is it possible in Objective C? How?
The second question is How to get the name of device/machine by using its IP address. Say for example I have an IP address 192.168.0.74 = What is the device name? in Objective C?
I'm not sure if this is the best way to do this, but it works for me, mostly. I put in StackOverflow's IP addresses (69.59.196.211) and it gave me back stackoverflow.com, but I put in one of Google's IP addresses (210.55.180.158) and it gave me back cache.googlevideo.com (for all results, not just the first one).
int error;
struct addrinfo *results = NULL;
error = getaddrinfo("69.59.196.211", NULL, NULL, &results);
if (error != 0)
{
NSLog (#"Could not get any info for the address");
return; // or exit(1);
}
for (struct addrinfo *r = results; r; r = r->ai_next)
{
char hostname[NI_MAXHOST] = {0};
error = getnameinfo(r->ai_addr, r->ai_addrlen, hostname, sizeof hostname, NULL, 0 , 0);
if (error != 0)
{
continue; // try next one
}
else
{
NSLog (#"Found hostname: %s", hostname);
break;
}
}
freeaddrinfo(results);
There can be multiple names for the address, so you might not want to stop at the first one you find.
I wrote a Swift version of the accepted answer, though I'm not 100% sure of its correctness.
func reverseDNS(ip: String) -> String {
var results: UnsafeMutablePointer<addrinfo>? = nil
defer {
if let results = results {
freeaddrinfo(results)
}
}
let error = getaddrinfo(ip, nil, nil, &results)
if (error != 0) {
print("Unable to reverse ip: \(ip)")
return ip
}
for addrinfo in sequence(first: results, next: { $0?.pointee.ai_next }) {
guard let pointee = addrinfo?.pointee else {
print("Unable to reverse ip: \(ip)")
return ip
}
let hname = UnsafeMutablePointer<Int8>.allocate(capacity: Int(NI_MAXHOST))
defer {
hname.deallocate()
}
let error = getnameinfo(pointee.ai_addr, pointee.ai_addrlen, hname, socklen_t(NI_MAXHOST), nil, 0, 0)
if (error != 0) {
continue
}
return String(cString: hname)
}
return ip
}
You need to read the routing table - basically the same way as "netstat -r" command does. The netstat implementation is opensource - it's in the package
network_cmds-396.6
at
http://www.opensource.apple.com/release/mac-os-x-1082/
so you can check what it does. The default gateway contains the "G" flag but shouldn't connect the "I" flag (when both wifi and cell are active, wifi is used for internet connection - the cell gateway is not used and is given the "I" flag).
Hope it helps.