iOS跨网段广播

这段代码展示了如何在iOS设备上实现跨网段的UDP广播。通过指定WiFi名称、密码和UDP端口,程序创建并发送包含SSID、密码和其他加密信息的数据包,用于设备间的快速配对。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

-(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);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值