이번에 닥친일은 ethaddr 중 특정 부분을 추출하는 일인데
sed를 써야 하나? shell에서 해야 하나? 이래저래 테스트 하는데..

sh 에서는.. busybox의 ash 라서 그런지 $(string:n:m) 방식의 추출이 되지 않았고
유일하게 되는게 $(string#substr) 로 일치하는 스트링을 삭제하는 것 뿐이었다.
그리고 sed는 .. 라인단위로 하다 보니. 일치하는 문자만을 삭제하려니.. OTL

그런 이유로 tr이라는 녀석이 걸려 나오게 되었다.
tr은 translate or delete characters 라는데.. 도대체 r은 어디서 튀어 나온 녀석일까 ㄱ-




아무튼, uboot 에서 사용하는 예약어인 ethaddr 에서 MAC 부분을 추출하려고 하면
fw_printenv 와 쉘 그리고 tr을 조합하면 된다.

일단 fw_printenv의 값은
 ethaddr=00:00:00:00:00:00
으로 출력되고 이 값을 변수에 넣어준다.

ETH_TEMP=`fw_printenv ethaddr`
ETH_ADDR=echo ${ETH_TEMP#ethaddr=} | tr -d :

그러면 간단하게 ethaddr= 뒤의 MAC 어드레스가 : 없이 나오게 된다.


[링크 : http://k.daum.net/qna/view.html?qid=2f8nN&l_cid=Q&l_st=1] tr
[링크 : http://linux.die.net/man/1/tr] tr man page
[링크 : http://www.faqs.org/docs/abs/HTML/string-manipulation.html]  sh 에서 스트링
[링크 : http://www.ibm.com/developerworks/kr/library/l-sed1.html]      sed 사용법
Posted by 구차니
Linux API/network2009. 6. 4. 18:31
#include "stdio.h"
#include "string.h"
#include "net/if.h"
#include "sys/ioctl.h"

//
// Global public data
//
unsigned char cMacAddr[8]; // Server's MAC address

static int GetSvrMacAddress( char *pIface )
{
int nSD; // Socket descriptor
struct ifreq sIfReq; // Interface request
struct if_nameindex *pIfList; // Ptr to interface name index
struct if_nameindex *pListSave; // Ptr to interface name index

//
// Initialize this function
//
pIfList = (struct if_nameindex *)NULL;
pListSave = (struct if_nameindex *)NULL;
#ifndef SIOCGIFADDR
// The kernel does not support the required ioctls
return( 0 );
#endif

//
// Create a socket that we can use for all of our ioctls
//
nSD = socket( PF_INET, SOCK_STREAM, 0 );
if ( nSD < 0 )
{
// Socket creation failed, this is a fatal error
printf( "File %s: line %d: Socket failed\n", __FILE__, __LINE__ );
return( 0 );
}

//
// Obtain a list of dynamically allocated structures
//
pIfList = pListSave = if_nameindex();

//
// Walk thru the array returned and query for each interface's
// address
//
for ( pIfList; *(char *)pIfList != 0; pIfList++ )
{
//
// Determine if we are processing the interface that we
// are interested in
//
if ( strcmp(pIfList->if_name, pIface) )
// Nope, check the next one in the list
continue;
strncpy( sIfReq.ifr_name, pIfList->if_name, IF_NAMESIZE );

//
// Get the MAC address for this interface
//
if ( ioctl(nSD, SIOCGIFHWADDR, &sIfReq) != 0 )
{
// We failed to get the MAC address for the interface
printf( "File %s: line %d: Ioctl failed\n", __FILE__, __LINE__ );
return( 0 );
}
memmove( (void *)&cMacAddr[0], (void *)&sIfReq.ifr_ifru.ifru_hwaddr.sa_data[0], 6 );
break;
}

//
// Clean up things and return
//
if_freenameindex( pListSave );
close( nSD );
return( 1 );
}

int main( int argc, char * argv[] )
{
//
// Initialize this program
//
bzero( (void *)&cMacAddr[0], sizeof(cMacAddr) );
if ( !GetSvrMacAddress("eth0") )
{
// We failed to get the local host's MAC address
printf( "Fatal error: Failed to get local host's MAC address\n" );
}
printf( "HWaddr %02X:%02X:%02X:%02X:%02X:%02X\n",
cMacAddr[0], cMacAddr[1], cMacAddr[2],
cMacAddr[3], cMacAddr[4], cMacAddr[5] );

//
// And exit
//
exit( 0 );
}
[링크 : http://www.linuxquestions.org/questions/programming-9/getting-mac-address-with-c-linux-325037/]

위의 소스 작동하는 것 확인함.
아무튼, struct ifreq 에 ioctl함수로 받아오는 식으로 구현되어 있음.

man 7 netdevice
struct ifreq {
       char ifr_name[IFNAMSIZ]; /* Interface name */
       union {
           struct sockaddr ifr_addr;
           struct sockaddr ifr_dstaddr;
           struct sockaddr ifr_broadaddr;
           struct sockaddr ifr_netmask;
           struct sockaddr ifr_hwaddr;
           short           ifr_flags;
           int           ifr_ifindex;
           int           ifr_metric;
           int           ifr_mtu;
           struct ifmap    ifr_map;
           char           ifr_slave[IFNAMSIZ];
           char           ifr_newname[IFNAMSIZ];
           char *           ifr_data;
       };
       };

[링크 : http://unixhelp.ed.ac.uk/CGI/man-cgi?netdevice+7]

$ vi /usr/inclue/bits/socket.h
161 /* Structure describing a generic socket address.  */
162 struct sockaddr
163   {
164     __SOCKADDR_COMMON (sa_);    /* Common data: address family and length.  */
165     char sa_data[14];           /* Address data.  */
166   };

[링크 : http://adywicaksono.wordpress.com/2007/11/08/detecting-mac-address-using-c-application/]
[링크 : http://www.joinc.co.kr/modules/moniwiki/wiki.php/Code/C/ifinfo]

'Linux API > network' 카테고리의 다른 글

gateway 정보  (0) 2009.06.05
linux에서 ip/mac address 받아오기 관련 링크  (0) 2009.06.05
network 관련 include 할 파일 목록  (0) 2009.06.05
offsetof() - stddef.h  (0) 2009.06.05
SIOCGIF가 모야?  (0) 2009.06.05
Posted by 구차니
회사일2009. 6. 2. 15:43
IEEE 에서 MAC 어드레스 구매가 가능하며 구매 단위는 16,000,000(1600만개, 24bit)
1600$ 기본으로 구매가능하며 회사 이름을 숨기려 할시에는 2000$ + 된다. (환율 1500기준 240만원, 540만원)
https://standards.ieee.org/cgi-bin/wtp/request?rt=OUI

그리고 수량이 많지 않다면,
DS2502-E48칩(OTP-EPROM) 을 이용하여 사용하면 될듯 한데 문제는 가격
$1.30 @ 1k 이라고 한국 맥심 홈페이지에 나와 있다.(링크는 위의 칩 이름에 클릭)
1600개 이상 생산할일이 있으면 그냥 MAC Address 구매하는게 싸게 치일 듯 하다.

[참고 : http://torystory.tistory.com/9]
[참고 : http://irmus.tistory.com/41]



2010.01.19 추가

예전에는 회사 목록도 나왔는데 그 목록은 어디서 봐야하는지 찾을수가 없다 -_-
일단 가격에 대한 내용은 아래의 링크로 변경

[링크 : http://standards.ieee.org/develop/regauth/oui/index.html]

Posted by 구차니