首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >parse ip to mac

parse ip to mac

原创
作者头像
tankaro
发布于 2024-11-29 15:17:19
发布于 2024-11-29 15:17:19
4450
举报
代码语言:txt
复制
int parse_ip_to_mac(char *pIP, uint8_t *pMac, int macSize)
{
    char *input = pIP;
    char *tmp = pIP;
    int i = 0;
    char val[MAX_SUB_IP_VAL_SZIE] = "";
    uint8_t output[6] = {0};

    if((NULL == pIP) || (NULL == pMac) || (6 != macSize))
    {
        printf("[%s +%d %s] (NULL == pIP) || (NULL == pMac) || (6 != macSize) failed\n", __FILE__, __LINE__, __func__);
        return -1;
    }
    #if 0
    printf("[%s +%d %s] pIP=%s\n", __FILE__, __LINE__, __func__, pIP);
    printf("pMac :");
	for (i = 0; i < macSize; i++)
	{
		printf("0x%x(%d) ", pMac[i], pMac[i]);
	}
    printf("\n");
    #endif
    i = 0;
    while(NULL != input)
    {
        tmp = strchr(input, '.');
        if(NULL == tmp)
        {
            memset(val, '\0', MAX_SUB_IP_VAL_SZIE);
            strcpy(val, input);
            *(output+i) = atoi(val);
            //printf("[%s +%d %s] input=%s val=%s tmp=%s *(output+i)=%d\n", __FILE__, __LINE__, __func__, input, val, tmp, *(output+i));
            break;
        }
        //printf("[%s +%d %s] input=%s tmp=%s\n", __FILE__, __LINE__, __func__, input, tmp);
        //printf("[%s +%d %s] tmp - input=%ld\n", __FILE__, __LINE__, __func__, tmp - input);
        
        memset(val, '\0', MAX_SUB_IP_VAL_SZIE);
        memcpy(val, input, tmp - input);
        //printf("[%s +%d %s] input=%s val=%s tmp=%s\n", __FILE__, __LINE__, __func__, input, val, tmp);
        *(output+i) = atoi(val);
        //printf("[%s +%d %s] input=%s val=%s tmp=%s atoi(val)=%d *(output+i)=%d\n", __FILE__, __LINE__, __func__, input, val, tmp, atoi(val), *(output+i));
        i++;
        input = tmp + 1;
    }
	for (i = 0; i < 2; i++)
	{
		*(pMac+i+4) = *(output+i + 2);
	}
    #if 0
    printf("pMac :");
	for (i = 0; i < macSize; i++)
	{
		printf("0x%x(%d) ", pMac[i], pMac[i]);
	}
    printf("\n");
    #endif
    return 0;
}

代码语言:txt
复制
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "multicast_ip_to_mac.h"

int main(int argc, char *argv[])
{
    char desip[MAX_FILENAME_SIZE] = "224.1.8.17";

    unsigned char desmac[6] = {0x01, 0x00, 0x5e, 1, 4, 16};//目的mac <--------------- 

    printf("[%s +%d %s] get_str_version()=%s get_number_version()=%d\n", __FILE__, __LINE__, __func__, get_str_version(), get_number_version());
    printf("[%s +%d %s] desip=%s\n", __FILE__, __LINE__, __func__, desip);
    printf("desmac :");
	for (int i = 0; i < 6; i++)
	{
		printf("0x%x(%d) ", desmac[i], desmac[i]);
	}
    printf("\n");

    parse_ip_to_mac(desip, desmac, 6);

    printf("desmac :");
	for (int i = 0; i < 6; i++)
	{
		printf("0x%x(%d) ", desmac[i], desmac[i]);
	}
    printf("\n");


    return 0;
}
代码语言:txt
复制
./multicast_ip_to_mac.elf 
[main.cpp +13 main] get_str_version()=number str version 0.0.1.0 get_number_version()=10
[main.cpp +14 main] desip=224.1.8.17
desmac :0x1(1) 0x0(0) 0x5e(94) 0x1(1) 0x4(4) 0x10(16) 
desmac :0x1(1) 0x0(0) 0x5e(94) 0x1(1) 0x8(8) 0x11(17) 

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档