4 Copyright (C) 2004,2005 Neil Cafferkey
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24 #include <exec/memory.h>
26 #include <utility/utility.h>
28 #include <proto/exec.h>
29 #include <proto/dos.h>
30 #include <proto/utility.h>
45 #define UTILITY_VERSION 39
46 #define DOS_VERSION 36
49 IMPORT
struct ExecBase
*AbsExecBase
;
52 struct ExecBase
*SysBase
;
53 struct DosLibrary
*DOSBase
;
54 struct UtilityBase
*UtilityBase
;
56 static UPINT
ParseHexString(TEXT
*str
, UBYTE
*buffer
, UPINT buffer_size
);
57 static UPINT
StrLen(const TEXT
*s
);
59 const TEXT
template[] =
60 "SSID/K,KEY/K,TEXTKEY/K,NOKEY/S,MANAGED/S,ADHOC/S,CHANNEL/K/N";
61 const TEXT version_string
[] = "$VER: SetPrism2Defaults 1.2 (23.7.2005)";
62 const TEXT dos_name
[] = DOSNAME
;
63 const TEXT utility_name
[] = UTILITYNAME
;
64 const TEXT options_name
[] = "Prism 2 options";
65 static const struct TagItem tag_list_template
[] =
69 {P2OPT_Encryption
, S2ENC_NONE
},
70 {P2OPT_PortType
, S2PORT_MANAGED
},
74 static const struct TagItem name_tag_list
[] =
76 {ANO_NameSpace
, TRUE
}, /* Work-around for old MorphOS bug */
83 struct RDArgs
*read_args
;
84 LONG error
= 0, result
= RETURN_OK
;
85 UBYTE key_buffer
[IEEE802_11_WEP128LEN
];
96 args
= {NULL
, NULL
, NULL
, FALSE
, FALSE
, FALSE
, NULL
};
97 struct NamedObject
*options
;
98 struct TagItem
*tag_list
, *tag_item
;
102 UWORD key_option_count
= 0;
107 SysBase
= AbsExecBase
;
109 DOSBase
= (struct DosLibrary
*)OpenLibrary(dos_name
, DOS_VERSION
);
113 (struct UtilityBase
*)OpenLibrary(utility_name
, UTILITY_VERSION
);
115 if(UtilityBase
== NULL
)
118 /* Parse arguments */
120 read_args
= ReadArgs(template, (PINT
*)&args
, NULL
);
121 if(read_args
== NULL
)
127 if(args
.textkey
!= NULL
)
131 if(key_option_count
> 1 || args
.managed
&& args
.adhoc
)
132 error
= ERROR_TOO_MANY_ARGS
;
135 /* Get pre-existing options object or create a new one */
139 options
= FindNamedObject(NULL
, options_name
, NULL
);
143 options
= AllocNamedObjectA(options_name
, name_tag_list
);
146 if(AddNamedObject(NULL
, options
))
148 tag_list
= CloneTagItems(tag_list_template
);
151 options
->no_Object
= tag_list
;
152 ssid
= AllocMem(IEEE802_11_MAXIDLEN
,
153 MEMF_PUBLIC
| MEMF_CLEAR
);
156 key
= AllocMem(sizeof(struct WEPKey
),
157 MEMF_PUBLIC
| MEMF_CLEAR
);
162 key
->length
= IEEE802_11_WEP64LEN
;
163 tag_item
= FindTagItem(P2OPT_SSID
, tag_list
);
164 tag_item
->ti_Data
= (UPINT
)ssid
;
165 tag_item
= FindTagItem(P2OPT_WEPKey
, tag_list
);
166 tag_item
->ti_Data
= (UPINT
)key
;
175 FreeNamedObject(options
);
183 /* Set new options */
187 tag_list
= (APTR
)options
->no_Object
;
189 if(args
.ssid
!= NULL
)
191 tag_item
= FindTagItem(P2OPT_SSID
, tag_list
);
192 length
= StrLen(args
.ssid
);
193 if(length
<= IEEE802_11_MAXIDLEN
)
194 CopyMem(args
.ssid
, (APTR
)tag_item
->ti_Data
, length
);
201 tag_item
= FindTagItem(P2OPT_WEPKey
, tag_list
);
203 ParseHexString(args
.key
, key_buffer
, IEEE802_11_WEP128LEN
);
205 error
= ERROR_BAD_NUMBER
;
206 else if(length
!= IEEE802_11_WEP64LEN
&&
207 length
!= IEEE802_11_WEP128LEN
)
208 error
= ERROR_BAD_NUMBER
;
211 key
= (APTR
)tag_item
->ti_Data
;
212 key
->length
= length
;
213 CopyMem(key_buffer
, key
->key
, length
);
217 if(args
.textkey
!= NULL
)
219 tag_item
= FindTagItem(P2OPT_WEPKey
, tag_list
);
220 length
= StrLen(args
.textkey
);
222 error
= ERROR_INVALID_COMPONENT_NAME
;
223 else if(length
!= IEEE802_11_WEP64LEN
&&
224 length
!= IEEE802_11_WEP128LEN
)
225 error
= ERROR_INVALID_COMPONENT_NAME
;
228 key
= (APTR
)tag_item
->ti_Data
;
229 key
->length
= length
;
230 CopyMem(args
.textkey
, key
->key
, length
);
237 tag_item
= FindTagItem(P2OPT_Encryption
, tag_list
);
238 if(args
.key
!= NULL
|| args
.textkey
!= NULL
)
239 tag_item
->ti_Data
= S2ENC_WEP
;
241 tag_item
->ti_Data
= S2ENC_NONE
;
243 tag_item
= FindTagItem(P2OPT_PortType
, tag_list
);
245 tag_item
->ti_Data
= S2PORT_MANAGED
;
247 tag_item
->ti_Data
= S2PORT_ADHOC
;
249 if(args
.channel
!= NULL
)
251 tag_item
= FindTagItem(P2OPT_Channel
, tag_list
);
252 if(*args
.channel
>= 3 && *args
.channel
<= 14)
253 tag_item
->ti_Data
= *args
.channel
;
255 error
= ERROR_BAD_NUMBER
;
261 /* Print error message */
266 PrintFault(error
, NULL
);
269 result
= RETURN_FAIL
;
271 /* Close libraries and exit */
273 CloseLibrary((struct Library
*)UtilityBase
);
274 CloseLibrary((struct Library
*)DOSBase
);
281 static UPINT
ParseHexString(TEXT
*str
, UBYTE
*buffer
, UPINT buffer_size
)
288 end
= buffer
+ buffer_size
;
289 while((ch
= *str
++) != '\0' && buffer
< end
)
294 if(ch
!= '-' && ch
!= ':' && ch
!= ' ')
296 if(ch
>= '0' && ch
<= '9')
298 else if(ch
>= 'A' && ch
<= 'F')
322 static UPINT
StrLen(const TEXT
*s
)
326 for(p
= s
; *p
!= '\0'; p
++);