ObjFW  Diff

Differences From Artifact [00da26e5f2]:

To Artifact [ac9c1835dc]:


18
19
20
21
22
23
24



25
26
27
28
29

30
31

32

33







34
35
36
37
38
39
40
41
42
43
44
45
46
47
48















49
50







































































































































































51
52
53
54
55
56
57
#include <errno.h>

#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif

#import "OFDDPSocket.h"



#import "OFSocket.h"
#import "OFSocket+Private.h"

#import "OFAlreadyOpenException.h"
#import "OFBindDDPSocketFailedException.h"

#import "OFInvalidArgumentException.h"
#import "OFNotOpenException.h"

#import "OFReadFailedException.h"

#import "OFWriteFailedException.h"








#ifdef OF_HAVE_NETAT_APPLETALK_H
# include <netat/ddp.h>
# include <sys/ioctl.h>

/* Unfortulately, there is no struct for the following in userland headers */
struct ATInterfaceConfig {
	char interfaceName[16];
	unsigned int flags;
	struct at_addr address, router;
	unsigned short netStart, netEnd;
	at_nvestr_t zoneName;
};
#endif
















@implementation OFDDPSocket
@dynamic delegate;








































































































































































- (OFSocketAddress)bindToNetwork: (uint16_t)network
			    node: (uint8_t)node
			    port: (uint8_t)port
		    protocolType: (uint8_t)protocolType
{
#ifdef OF_MACOS







>
>
>





>


>

>

>
>
>
>
>
>
>















>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include <errno.h>

#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif

#import "OFDDPSocket.h"
#import "OFDictionary.h"
#import "OFNumber.h"
#import "OFPair.h"
#import "OFSocket.h"
#import "OFSocket+Private.h"

#import "OFAlreadyOpenException.h"
#import "OFBindDDPSocketFailedException.h"
#import "OFGetOptionFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFNotOpenException.h"
#import "OFOutOfRangeException.h"
#import "OFReadFailedException.h"
#import "OFSetOptionFailedException.h"
#import "OFWriteFailedException.h"

#ifdef HAVE_NET_IF_H
# include <net/if.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
# include <sys/ioctl.h>
#endif

#ifdef OF_HAVE_NETAT_APPLETALK_H
# include <netat/ddp.h>
# include <sys/ioctl.h>

/* Unfortulately, there is no struct for the following in userland headers */
struct ATInterfaceConfig {
	char interfaceName[16];
	unsigned int flags;
	struct at_addr address, router;
	unsigned short netStart, netEnd;
	at_nvestr_t zoneName;
};
#endif

#ifdef OF_HAVE_APPLETALK_IFCONFIG
const OFAppleTalkInterfaceConfigurationKey
    OFAppleTalkInterfaceConfigurationNode =
    @"OFAppleTalkInterfaceConfigurationNode";
const OFAppleTalkInterfaceConfigurationKey
    OFAppleTalkInterfaceConfigurationNetwork =
    @"OFAppleTalkInterfaceConfigurationNetwork";
const OFAppleTalkInterfaceConfigurationKey
    OFAppleTalkInterfaceConfigurationPhase =
    @"OFAppleTalkInterfaceConfigurationPhase";
const OFAppleTalkInterfaceConfigurationKey
    OFAppleTalkInterfaceConfigurationNetworkRange =
    @"OFAppleTalkInterfaceConfigurationNetworkRange";
#endif

@implementation OFDDPSocket
@dynamic delegate;

#ifdef OF_HAVE_APPLETALK_IFCONFIG
+ (void)setConfiguration: (OFAppleTalkInterfaceConfiguration)config
	    forInterface: (OFString *)interfaceName
{
	OFNumber *network, *node, *phase;
	OFPair OF_GENERIC(OFNumber *, OFNumber *) *range;
	int sock;
	struct ifreq request;
	struct sockaddr_at *sat;
	uint16_t rangeStart, rangeEnd;

	if (interfaceName.UTF8StringLength > IFNAMSIZ - 1)
		@throw [OFOutOfRangeException exception];

	network = [config
	    objectForKey: OFAppleTalkInterfaceConfigurationNetwork];
	node = [config objectForKey: OFAppleTalkInterfaceConfigurationNode];
	phase = [config objectForKey: OFAppleTalkInterfaceConfigurationPhase];
	range = [config
	    objectForKey: OFAppleTalkInterfaceConfigurationNetworkRange];

	if (network == nil || node == nil)
		@throw [OFInvalidArgumentException exception];

	if (phase != nil && phase.unsignedCharValue != 1 &&
	    phase.unsignedCharValue != 2)
		@throw [OFInvalidArgumentException exception];

# ifdef OF_MACOS
	if ((sock = socket(AF_APPLETALK, SOCK_RAW, 0)) < 0)
# else
	if ((sock = socket(AF_APPLETALK, SOCK_DGRAM, 0)) < 0)
# endif
		@throw [OFSetOptionFailedException
		    exceptionWithObject: nil
				  errNo: OFSocketErrNo()];

	memset(&request, 0, sizeof(request));
	strncpy(request.ifr_name, interfaceName.UTF8String, IFNAMSIZ - 1);
	sat = (struct sockaddr_at *)&request.ifr_addr;
	sat->sat_family = AF_APPLETALK;
	sat->sat_net = OFToBigEndian16(network.unsignedShortValue);
	sat->sat_node = node.unsignedCharValue;
	/*
	 * The netrange is hidden in sat_zero and different OSes use different
	 * struct names for it, so the portable way is setting sat_zero
	 * directly.
	 */
	sat->sat_zero[0] = (phase != nil ? phase.unsignedCharValue : 2);
	if (range != nil) {
		rangeStart = [range.firstObject unsignedShortValue];
		rangeEnd = [range.secondObject unsignedShortValue];
	} else {
		rangeStart = rangeEnd = network.unsignedShortValue;
	}
	sat->sat_zero[2] = rangeStart >> 8;
	sat->sat_zero[3] = rangeStart & 0xFF;
	sat->sat_zero[4] = rangeEnd >> 8;
	sat->sat_zero[5] = rangeEnd & 0xFF;

	if (ioctl(sock, SIOCSIFADDR, &request) != 0)
		@throw [OFSetOptionFailedException
		    exceptionWithObject: nil
				  errNo: OFSocketErrNo()];

	close(sock);
}

+ (OFAppleTalkInterfaceConfiguration)
    configurationForInterface: (OFString *)interfaceName
{
	int sock;
	struct ifreq request;
	struct sockaddr_at *sat;
# ifndef OF_LINUX
	uint16_t rangeStart, rangeEnd;
	OFPair *range;
# endif

	if (interfaceName.UTF8StringLength > IFNAMSIZ - 1)
		@throw [OFOutOfRangeException exception];

# ifdef OF_MACOS
	if ((sock = socket(AF_APPLETALK, SOCK_RAW, 0)) < 0)
# else
	if ((sock = socket(AF_APPLETALK, SOCK_DGRAM, 0)) < 0)
# endif
		@throw [OFGetOptionFailedException
		    exceptionWithObject: nil
				  errNo: OFSocketErrNo()];

	memset(&request, 0, sizeof(request));
	strncpy(request.ifr_name, interfaceName.UTF8String, IFNAMSIZ - 1);

	if (ioctl(sock, SIOCGIFADDR, &request) < 0) {
		int errNo = OFSocketErrNo();

		/* No AppleTalk configured on this interface. */
		if (errNo == EADDRNOTAVAIL) {
			close(sock);
			return nil;
		}

		@throw [OFGetOptionFailedException exceptionWithObject: nil
								 errNo: errNo];
	}

	sat = (struct sockaddr_at *)&request.ifr_addr;

	close(sock);

# ifndef OF_LINUX
	/*
	 * Linux currently doesn't fill out the phase or netrange.
	 *
	 * The netrange is hidden in sat_zero and different OSes use different
	 * struct names for it, so the portable way is setting sat_zero
	 * directly.
	 */
	rangeStart = sat->sat_zero[2] << 8 | sat->sat_zero[3];
	rangeEnd = sat->sat_zero[4] << 8 | sat->sat_zero[5];
	range = [OFPair
	    pairWithFirstObject: [OFNumber numberWithUnsignedShort: rangeStart]
		   secondObject: [OFNumber numberWithUnsignedShort: rangeEnd]];
# endif

	return [OFDictionary dictionaryWithKeysAndObjects:
	    OFAppleTalkInterfaceConfigurationNode,
	    [OFNumber numberWithUnsignedChar: sat->sat_node],
	    OFAppleTalkInterfaceConfigurationNetwork,
	    [OFNumber numberWithUnsignedShort: OFFromBigEndian16(sat->sat_net)],
# ifndef OF_LINUX
	    OFAppleTalkInterfaceConfigurationPhase,
	    [OFNumber numberWithUnsignedChar: sat->sat_zero[0]],
	    OFAppleTalkInterfaceConfigurationNetworkRange, range,
# endif
	    nil];
}

+ (void)removeConfigurationForInterface: (OFString *)interfaceName
{
	int sock;
	struct ifreq request;

	if (interfaceName.UTF8StringLength > IFNAMSIZ - 1)
		@throw [OFOutOfRangeException exception];

# ifdef OF_MACOS
	if ((sock = socket(AF_APPLETALK, SOCK_RAW, 0)) < 0)
# else
	if ((sock = socket(AF_APPLETALK, SOCK_DGRAM, 0)) < 0)
# endif
		@throw [OFSetOptionFailedException
		    exceptionWithObject: nil
				  errNo: OFSocketErrNo()];

	memset(&request, 0, sizeof(request));
	strncpy(request.ifr_name, interfaceName.UTF8String, IFNAMSIZ - 1);
	request.ifr_addr.sa_family = AF_APPLETALK;

	if (ioctl(sock, SIOCDIFADDR, &request) != 0)
		@throw [OFSetOptionFailedException
		    exceptionWithObject: nil
				  errNo: OFSocketErrNo()];
}
#endif

- (OFSocketAddress)bindToNetwork: (uint16_t)network
			    node: (uint8_t)node
			    port: (uint8_t)port
		    protocolType: (uint8_t)protocolType
{
#ifdef OF_MACOS