Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // this struct normally resides in <sys/un.h> (for C)
- /// a sockaddr for local connections (AF_UNIX)
- struct sockaddr_un {
- ushort sun_family = AF_UNIX; // addr family
- char sun_path[108]; // pathname
- };
- /// an Address for local addresses (AF_UNIX) - (derived from std.socket.Address) with public getters
- class LocalAddress : Address {
- // NOTE: this is currently not used and untested.
- sockaddr_un addr;
- /// constructs a LocalAddress of the unnamed type
- this() {
- addr.sun_family = AF_UNIX;
- }
- this(char[] path) {
- addr.sun_family = AF_UNIX;
- // see man 7 unix for further information about the path/address types
- if(path && path.length>0) {
- // unnamed type
- if(path[0] == '\0' && path[1] == '\0')
- return;
- // abstract type
- if(path[0] == '\0'){
- if(path.length > 108)
- throw new Exception("path to long - must be at most 108 chars");
- addr.sun_path[0..path.length] = path[];
- }
- // pathname type
- path ~= '\0'; // just to be sure
- int len = cast(int)strlen(cast(char*)addr.sun_path);
- if(len > 107) // nullterminated path doesn't fit into sun_path
- throw new Exception("path to long - must be at most 108 chars");
- addr.sun_path[0..len] = path[0..len];
- }
- }
- this(sockaddr_un *sadr){
- if(sadr.sun_family != AF_UNIX) {
- throw new Exception("this was not a AF_UNIX socketaddr!");
- }
- addr = *sadr;
- }
- protected sockaddr* name() { return cast(sockaddr*) &addr; }
- protected int nameLen() {
- // if the address is the unnamed type
- if(addr.sun_path[0] == '\0' && addr.sun_path[1] == '\0')
- return cast(int)ushort.sizeof;
- // if the address is the abstract type
- if(addr.sun_path[0] == '\0')
- return cast(int)addr.sizeof;
- // at this point it has to be the pathname type
- assert(addr.sun_path[107] == '\0');
- int len = cast(int)strlen(cast(char*)addr.sun_path) + 1; // + terminating \0
- return cast(int)(ushort.sizeof + len);
- }
- AddressFamily addressFamily() { return AddressFamily.UNIX; }
- string toString() { return "local:"~getPath(); }
- char[] getPath() {
- // if the address is the unnamed type
- if(addr.sun_path[0] == '\0' && addr.sun_path[1] == '\0')
- return "\0\0";
- // if the address is the abstract type
- if(addr.sun_path[0] == '\0')
- return addr.sun_path;
- // at this point it has to be the pathname type
- assert(addr.sun_path[107] == '\0');
- int len = cast(int)strlen(cast(char*)addr.sun_path)+1; // + terminating \0
- return addr.sun_path[0..len];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement