-(void)QuickPairing
{
NSString *ssid = self.wifiname;//@"Black"; //wifi名称
NSString *password = self.wifipwd;//@"wabjtam123"; //wifi密码
int port = 30001; //udp绑定端口号
NSString *devMac = [self.mac substringToIndex:16];
const char *buf = [devMac UTF8String];
unsigned char mac[] = {0x00, 0x00, 0x00, 0x0c, 0x43, 0x27, 0x00, 0x04};
for (int i = 0; i < 8; i ++)
{
mac[i] = hex2dec(&buf[i*2], 2);
}
NSString *str = [NSString stringWithFormat:@"%@\n%@",ssid,password];
NSUInteger strLen = str.length;
if (strLen %2 != 0)
{
strLen++;
}
else
{
strLen+=2;
}
char data[strLen];
memset(data, 0, strLen);
memccpy(data, [str UTF8String], 0, str.length);
unsigned char portbuf[2];
portbuf[0] = port;
portbuf[1] = port>>8;
unsigned char xorCheck = 0;
unsigned char andCheck = 0;
unsigned int temp;
NSMutableArray *array = [[NSMutableArray alloc] init];
temp = 0xffff00e5 | [self randomNumber]<<8; //或一个随机数(随机数范围0、1、2、3)
[array addObject:[NSData dataWithBytes:&temp length:4]];
temp = 0xffff00e5 | (0x0100 <<2) | ([self randomNumber]<<8);
[array addObject:[NSData dataWithBytes:&temp length:4]];
temp = 0xffff00e5 | (0x0200 <<2) | ([self randomNumber]<<8);
[array addObject:[NSData dataWithBytes:&temp length:4]];
temp = 0xffff00e5 | (0x0300 <<2) | ([self randomNumber]<<8);
[array addObject:[NSData dataWithBytes:&temp length:4]];
xorCheck = xorCheck ^ portbuf[0] ^ portbuf[1];
andCheck = andCheck + portbuf[0] + portbuf[1];
portbuf[0] = [self maskontent:portbuf[0] WithMask:mac AndOffset:0x28];
portbuf[1] = [self maskontent:portbuf[1] WithMask:mac AndOffset:0x28];
temp = 0xe5 | 0x0a00<<2 | portbuf[0] << 16 | portbuf[1]<<24;
[array addObject:[NSData dataWithBytes:&temp length:4]];
unsigned int length = (int)strLen/2;
for (int i=0; i<length; i++)
{
unsigned char num = (0x0b+i)<<2;
portbuf[0] = data[2*i];
portbuf[1] = data[2*i+1];
xorCheck = xorCheck ^ portbuf[0] ^ portbuf[1];
andCheck = andCheck + portbuf[0] + portbuf[1];
num |= ([self randomNumber]);
portbuf[0] = [self maskontent:portbuf[0] WithMask:mac AndOffset:num];
portbuf[1] = [self maskontent:portbuf[1] WithMask:mac AndOffset:num];
temp = 0xe5 | num <<8 | portbuf[0] << 16 | portbuf[1]<<24;
[array addObject:[NSData dataWithBytes:&temp length:4]];
}
unsigned char len = strLen + 2;
temp = 0xe5 | 0x0400<<2 | len << 16 | len << 24 | ([self randomNumber]<<8);
[array insertObject:[NSData dataWithBytes:&temp length:4] atIndex:4];
temp = 0xe5 | 0x0500<<2 | len << 16 | len << 24 | ([self randomNumber]<<8);
[array insertObject:[NSData dataWithBytes:&temp length:4] atIndex:5];
temp = 0xe5 | 0x0600<<2 | len << 16 | len << 24 | ([self randomNumber]<<8);
[array insertObject:[NSData dataWithBytes:&temp length:4] atIndex:6];
temp = 0xe5 | 0x0700<<2 | xorCheck << 16 | andCheck << 24 | ([self randomNumber]<<8);
[array insertObject:[NSData dataWithBytes:&temp length:4] atIndex:7];
temp = 0xe5 | 0x0800<<2 | xorCheck << 16 | andCheck << 24 | ([self randomNumber]<<8);
[array insertObject:[NSData dataWithBytes:&temp length:4] atIndex:8];
temp = 0xe5 | 0x0900<<2 | xorCheck << 16 | andCheck << 24 | ([self randomNumber]<<8);
[array insertObject:[NSData dataWithBytes:&temp length:4] atIndex:9];
while (self.flow == NO)
{
for(int i = 0; i < 10; i ++)
{
for (NSData *d in array)
{
unsigned char *ad = (unsigned char *)[d bytes];
NSString *add = [NSString stringWithFormat:@"%d.%d.%d.%d",*ad,*(ad+1),*(ad+2),*(ad+3)];
[[UDPChannel defaultUDPChannel] sendBinaryData:d toHost:add];
}
usleep(10000);
}
}
}
iOS跨网段广播
最新推荐文章于 2023-09-14 10:10:19 发布