ObjFW  Diff

Differences From Artifact [8054bd9b01]:

To Artifact [c079b4e165]:


12
13
14
15
16
17
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
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#include <errno.h>
#include "unistd.h"

#import "OFApplication.h"
#import "OFArray.h"



#import "OFOptionsParser.h"
#import "OFSocket.h"
#import "OFStdIOStream.h"

#import "OFInvalidFormatException.h"

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

@interface OFATalkCfg: OFObject <OFApplicationDelegate>
@end

OF_APPLICATION_DELEGATE(OFATalkCfg)

static void
configureInterface(OFString *interface, uint16_t network, uint8_t node,
    uint8_t phase, uint16_t rangeStart, uint16_t rangeEnd)
{
	int sock;
	struct ifreq request;
	struct sockaddr_at *sat;

	if (interface.UTF8StringLength > IFNAMSIZ - 1) {
		[OFStdErr writeFormat: @"%@: Interface name too long!\n",
				       [OFApplication programName]];
		[OFApplication terminateWithStatus: 1];
	}

#ifdef OF_MACOS
	if ((sock = socket(AF_APPLETALK, SOCK_RAW, 0)) < 0) {
#else
	if ((sock = socket(AF_APPLETALK, SOCK_DGRAM, 0)) < 0) {
#endif
		int errNo = OFSocketErrNo();

		[OFStdErr writeFormat: @"%@: Failed to create socket: %@\n",
				       [OFApplication programName],
				       OFStrError(errNo)];

#ifdef OF_LINUX
		if (errNo == EAFNOSUPPORT)
			[OFStdErr writeLine: @"Did you forget to run "
					     @"\"modprobe appletalk\"?"];
#endif

		[OFApplication terminateWithStatus: 1];
	}

	memset(&request, 0, sizeof(request));
	strncpy(request.ifr_name, interface.UTF8String, IFNAMSIZ - 1);
	sat = (struct sockaddr_at *)&request.ifr_addr;
	sat->sat_family = AF_APPLETALK;
	sat->sat_net = OFToBigEndian16(network);
	sat->sat_node = node;
	/*
	 * 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;
	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) {
		[OFStdErr writeFormat: @"%@: Failed to set address: %@\n",
				       [OFApplication programName],
				       OFStrError(OFSocketErrNo())];
		[OFApplication terminateWithStatus: 1];
	}

	close(sock);
}

@implementation OFATalkCfg
- (void)applicationDidFinishLaunching: (OFNotification *)notification
{

	OFString *nodeString = nil, *networkString = nil, *phaseString = nil;
	OFString *rangeString = nil;
	const OFOptionsParserOption options[] = {
		{ '\0', @"network", 1, NULL, &networkString },
		{ '\0', @"node", 1, NULL, &nodeString },
		{ '\0', @"phase", 1, NULL, &phaseString },
		{ '\0', @"range", 1, NULL, &rangeString },







<



>
>
>

|




<
<
<
<
<
<
<





<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<



>







12
13
14
15
16
17
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
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#include <errno.h>


#import "OFApplication.h"
#import "OFArray.h"
#import "OFDDPSocket.h"
#import "OFDictionary.h"
#import "OFNumber.h"
#import "OFOptionsParser.h"
#import "OFPair.h"
#import "OFStdIOStream.h"

#import "OFInvalidFormatException.h"








@interface OFATalkCfg: OFObject <OFApplicationDelegate>
@end

OF_APPLICATION_DELEGATE(OFATalkCfg)






























































@implementation OFATalkCfg
- (void)applicationDidFinishLaunching: (OFNotification *)notification
{
	OFMutableDictionary *config = [OFMutableDictionary dictionary];
	OFString *nodeString = nil, *networkString = nil, *phaseString = nil;
	OFString *rangeString = nil;
	const OFOptionsParserOption options[] = {
		{ '\0', @"network", 1, NULL, &networkString },
		{ '\0', @"node", 1, NULL, &nodeString },
		{ '\0', @"phase", 1, NULL, &phaseString },
		{ '\0', @"range", 1, NULL, &rangeString },
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
		return;
	}
	if (network > UINT16_MAX) {
		[OFStdErr writeFormat: @"%@: --network out of range!\n",
				       [OFApplication programName]];
		[OFApplication terminateWithStatus: 1];
	}



	if (nodeString == nil) {
		[OFStdErr writeFormat: @"%@: --node not specified!\n",
				       [OFApplication programName]];
		[OFApplication terminateWithStatus: 1];
	}
	@try {
		node = [nodeString unsignedLongLongValueWithBase: 0];
	} @catch (OFInvalidFormatException *e) {
		[OFStdErr writeFormat: @"%@: Invalid format for --node!\n",
				       [OFApplication programName]];
		[OFApplication terminateWithStatus: 1];
		return;
	}
	if (node > UINT8_MAX) {
		[OFStdErr writeFormat: @"%@: --node out of range!\n",
				       [OFApplication programName]];
		[OFApplication terminateWithStatus: 1];
	}



	if (phaseString != nil) {
		@try {
			phase = [phaseString unsignedLongLongValueWithBase: 0];
		} @catch (OFInvalidFormatException *e) {
			[OFStdErr writeFormat:
			    @"%@: Invalid format for "@"--phase!\n",
			    [OFApplication programName]];
			[OFApplication terminateWithStatus: 1];
			return;
		}

		if (phase > 2) {
			[OFStdErr writeFormat: @"%@: --phase out of range!\n",
					       [OFApplication programName]];
			[OFApplication terminateWithStatus: 1];
		}
	} else

		phase = 2;



	if (rangeString != nil) {




		rangeArray = [rangeString componentsSeparatedByString: @"-"];
		if (rangeArray.count != 2) {
			[OFStdErr writeFormat:
			    @"%@: Invalid format for --range!\n",
			    [OFApplication programName]];
			[OFApplication terminateWithStatus: 1];
		}







>
>



















>
>

















|
>
|
>
|
>

>
>
>
>







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
		return;
	}
	if (network > UINT16_MAX) {
		[OFStdErr writeFormat: @"%@: --network out of range!\n",
				       [OFApplication programName]];
		[OFApplication terminateWithStatus: 1];
	}
	[config setObject: [OFNumber numberWithUnsignedShort: (uint16_t)network]
		   forKey: OFAppleTalkInterfaceConfigurationNetwork];

	if (nodeString == nil) {
		[OFStdErr writeFormat: @"%@: --node not specified!\n",
				       [OFApplication programName]];
		[OFApplication terminateWithStatus: 1];
	}
	@try {
		node = [nodeString unsignedLongLongValueWithBase: 0];
	} @catch (OFInvalidFormatException *e) {
		[OFStdErr writeFormat: @"%@: Invalid format for --node!\n",
				       [OFApplication programName]];
		[OFApplication terminateWithStatus: 1];
		return;
	}
	if (node > UINT8_MAX) {
		[OFStdErr writeFormat: @"%@: --node out of range!\n",
				       [OFApplication programName]];
		[OFApplication terminateWithStatus: 1];
	}
	[config setObject: [OFNumber numberWithUnsignedChar: (uint8_t)node]
		   forKey: OFAppleTalkInterfaceConfigurationNode];

	if (phaseString != nil) {
		@try {
			phase = [phaseString unsignedLongLongValueWithBase: 0];
		} @catch (OFInvalidFormatException *e) {
			[OFStdErr writeFormat:
			    @"%@: Invalid format for "@"--phase!\n",
			    [OFApplication programName]];
			[OFApplication terminateWithStatus: 1];
			return;
		}

		if (phase > 2) {
			[OFStdErr writeFormat: @"%@: --phase out of range!\n",
					       [OFApplication programName]];
			[OFApplication terminateWithStatus: 1];
		}

		[config setObject: [OFNumber
				       numberWithUnsignedChar: (uint8_t)phase]
			   forKey: OFAppleTalkInterfaceConfigurationPhase];
	}

	if (rangeString != nil) {
		const OFAppleTalkInterfaceConfigurationKey key =
		    OFAppleTalkInterfaceConfigurationNetworkRange;
		OFPair *range;

		rangeArray = [rangeString componentsSeparatedByString: @"-"];
		if (rangeArray.count != 2) {
			[OFStdErr writeFormat:
			    @"%@: Invalid format for --range!\n",
			    [OFApplication programName]];
			[OFApplication terminateWithStatus: 1];
		}
242
243
244
245
246
247
248
249


250

251

252
253

254
255
256

257
258
259
260
		}

		if (rangeStart > UINT16_MAX || rangeEnd > UINT16_MAX) {
			[OFStdErr writeFormat: @"%@: --range out of range!\n",
					       [OFApplication programName]];
			[OFApplication terminateWithStatus: 1];
		}
	} else {


		rangeStart = network;

		rangeEnd = network;

	}


	configureInterface(optionsParser.remainingArguments.firstObject,
	    (uint16_t)network, (uint8_t)node, (uint8_t)phase,
	    (uint16_t)rangeStart, (uint16_t)rangeEnd);


	[OFApplication terminate];
}
@end







|
>
>
|
>
|
>


>
|
<
<
>




188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205


206
207
208
209
210
		}

		if (rangeStart > UINT16_MAX || rangeEnd > UINT16_MAX) {
			[OFStdErr writeFormat: @"%@: --range out of range!\n",
					       [OFApplication programName]];
			[OFApplication terminateWithStatus: 1];
		}

		range = [OFPair
		    pairWithFirstObject: [OFNumber numberWithUnsignedShort:
					     (uint16_t)rangeStart]
			   secondObject: [OFNumber numberWithUnsignedShort:
					     (uint16_t)rangeEnd]];
		[config setObject: range forKey: key];
	}

	[OFDDPSocket setConfiguration: config
			 forInterface: optionsParser.remainingArguments


					   .firstObject];

	[OFApplication terminate];
}
@end