1/*
2  Copyright (C) 2000-2005 SKYRIX Software AG
3
4  This file is part of SOPE.
5
6  SOPE is free software; you can redistribute it and/or modify it under
7  the terms of the GNU Lesser General Public License as published by the
8  Free Software Foundation; either version 2, or (at your option) any
9  later version.
10
11  SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12  WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14  License for more details.
15
16  You should have received a copy of the GNU Lesser General Public
17  License along with SOPE; see the file COPYING.  If not, write to the
18  Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19  02111-1307, USA.
20*/
21
22#if !defined(WIN32)
23
24#include "NGLocalSocketDomain.h"
25#include "NGLocalSocketAddress.h"
26#include "NGSocket.h"
27
28#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__DragonFly__)
29#  include <sys/types.h>
30#  include <sys/socket.h>
31#else
32#  include <sys/un.h>
33#endif
34
35#include "common.h"
36
37@implementation NGLocalSocketDomain
38
39static NGLocalSocketDomain *domain = nil;
40
41+ (void)initialize {
42  BOOL isInitialized = NO;
43  if (!isInitialized) {
44    isInitialized = YES;
45    [NGSocket initialize];
46    domain = [[NGLocalSocketDomain alloc] init];
47  }
48}
49
50+ (id)domain {
51  return domain;
52}
53
54// NGSocketDomain
55
56- (id<NGSocketAddress>)addressWithRepresentation:(void *)_data
57  size:(unsigned int)_size
58{
59  NGLocalSocketAddress *address = nil;
60
61  address = [[NGLocalSocketAddress alloc] initWithDomain:self
62                                          internalRepresentation:_data
63                                          size:_size];
64  return AUTORELEASE(address);
65}
66
67- (BOOL)prepareAddress:(id<NGSocketAddress>)_address
68  forBindWithSocket:(id<NGSocket>)_socket
69{
70  if ([_socket conformsToProtocol:@protocol(NGPassiveSocket)]) {
71    NSString *path = [(NGLocalSocketAddress *)_address path];
72
73    // ignore errors ..
74    [[NSFileManager defaultManager] removeFileAtPath:path handler:nil];
75  }
76  return YES;
77}
78- (BOOL)cleanupAddress:(id<NGSocketAddress>)_address
79  afterCloseOfSocket:(id<NGSocket>)_socket
80{
81  if ([_socket conformsToProtocol:@protocol(NGPassiveSocket)]) {
82#if 0
83    NSString *path = [(NGLocalSocketAddress *)_address path];
84
85    // ignore errors ..
86    [[NSFileManager defaultManager] removeFileAtPath:path handler:nil];
87#endif
88  }
89  return YES;
90}
91
92- (int)socketDomain {
93  return AF_LOCAL;
94}
95
96- (int)addressRepresentationSize { // maximum size
97  return sizeof(struct sockaddr_un);
98}
99
100- (int)protocol {
101  return 0;
102}
103
104/* NSCopying */
105
106- (id)copyWithZone:(NSZone *)_zone {
107  /* domains are immutable, just return self on copy .. */
108  return [self retain];
109}
110
111/* NSCoding */
112
113- (void)encodeWithCoder:(NSCoder *)_encoder {
114}
115- (id)initWithCoder:(NSCoder *)_decoder {
116  [self release]; self = nil;
117  return [domain retain]; /* replace with singleton */
118}
119
120- (id)awakeAfterUsingCoder:(NSCoder *)_decoder {
121  if (self != domain) {
122    [self release]; self = nil;
123    return [domain retain]; /* replace with singleton */
124  }
125  else
126    return self;
127}
128
129/* description */
130
131- (NSString *)description {
132  return [NSString stringWithFormat:@"<UnixDomain: self=0x%p>", self];
133}
134
135@end /* NGLocalSocketDomain */
136
137#endif // !WIN32
138