Advertisement
Guest User

Unix Socket code for D (Untested)

a guest
Oct 20th, 2011
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 2.49 KB | None | 0 0
  1. // this struct normally resides in <sys/un.h> (for C)
  2. /// a sockaddr for local connections (AF_UNIX)
  3. struct sockaddr_un {
  4.     ushort sun_family = AF_UNIX; // addr family
  5.     char sun_path[108]; // pathname
  6. };
  7.  
  8. /// an Address for local addresses (AF_UNIX) - (derived from std.socket.Address) with public getters
  9. class LocalAddress : Address {
  10.     // NOTE: this is currently not used and untested.
  11.     sockaddr_un addr;
  12.    
  13.     /// constructs a LocalAddress of the unnamed type
  14.     this() {
  15.         addr.sun_family = AF_UNIX;
  16.     }
  17.    
  18.     this(char[] path) {
  19.         addr.sun_family = AF_UNIX;
  20.         // see man 7 unix for further information about the path/address types
  21.         if(path && path.length>0) {
  22.             // unnamed type
  23.             if(path[0] == '\0' && path[1] == '\0')
  24.                 return;
  25.             // abstract type
  26.             if(path[0] == '\0'){
  27.                 if(path.length > 108)
  28.                     throw new Exception("path to long - must be at most 108 chars");
  29.                 addr.sun_path[0..path.length] = path[];
  30.             }
  31.             // pathname type
  32.             path ~= '\0'; // just to be sure
  33.             int len = cast(int)strlen(cast(char*)addr.sun_path);
  34.             if(len > 107) // nullterminated path doesn't fit into sun_path
  35.                 throw new Exception("path to long - must be at most 108 chars");
  36.             addr.sun_path[0..len] = path[0..len];
  37.         }
  38.     }
  39.    
  40.     this(sockaddr_un *sadr){
  41.         if(sadr.sun_family != AF_UNIX) {
  42.             throw new Exception("this was not a AF_UNIX socketaddr!");
  43.         }
  44.         addr = *sadr;
  45.     }
  46.    
  47.     protected sockaddr* name() { return cast(sockaddr*) &addr; }
  48.     protected int nameLen() {
  49.         // if the address is the unnamed type
  50.         if(addr.sun_path[0] == '\0' && addr.sun_path[1] == '\0')
  51.             return cast(int)ushort.sizeof;
  52.        
  53.         // if the address is the abstract type
  54.         if(addr.sun_path[0] == '\0')
  55.             return cast(int)addr.sizeof;
  56.        
  57.         // at this point it has to be the pathname type
  58.         assert(addr.sun_path[107] == '\0');
  59.         int len = cast(int)strlen(cast(char*)addr.sun_path) + 1; // + terminating \0
  60.         return cast(int)(ushort.sizeof + len);
  61.     }
  62.    
  63.     AddressFamily addressFamily() { return AddressFamily.UNIX; }
  64.    
  65.     string toString() { return "local:"~getPath(); }
  66.    
  67.     char[] getPath() {
  68.         // if the address is the unnamed type
  69.         if(addr.sun_path[0] == '\0' && addr.sun_path[1] == '\0')
  70.             return "\0\0";
  71.        
  72.         // if the address is the abstract type
  73.         if(addr.sun_path[0] == '\0')
  74.             return addr.sun_path;
  75.        
  76.         // at this point it has to be the pathname type
  77.         assert(addr.sun_path[107] == '\0');
  78.         int len = cast(int)strlen(cast(char*)addr.sun_path)+1; // + terminating \0
  79.         return addr.sun_path[0..len];
  80.     }
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement