Get MAC address using C

不過 , 這邊有指定 eth0 , 若 server 有很多 interface 的話就要注意一下了.

#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>

int main( int argc, char *argv[] )
{
	int s;
	struct ifreq buffer;

	s = socket(PF_INET, SOCK_DGRAM, 0);

	memset(&buffer, 0x00, sizeof(buffer));
	
	strcpy(buffer.ifr_name, "eth0");

	ioctl(s, SIOCGIFHWADDR, &buffer);

	close(s);
	
	for( s = 0; s < 6; s++ )
	{
		printf("%.2X ", (unsigned char)buffer.ifr_hwaddr.sa_data[s]);
	}

	printf("\n");

	return 0;
}

[好文推薦] A TUTORIAL ON POINTERS AND ARRAYS IN C

"C 語言" … ㄏㄏ.. 這麼古老且重要的語言,從 1972年開始到現在至少超過30年! 學會 pointer 後 C 就算是懂 90% 了

REF: http://en.wikipedia.org/wiki/C_%28programming_language%29

FROM URL : http://home.netcom.com/~tjensen/ptr/pointers.htm

TABLE OF CONTENTS

Preface

Introduction

Chapter 1: What is a Pointer?

Chapter 2: Pointer Types and Arrays.

Chapter 3: Pointers and Strings

Chapter 4: More on Strings

Chapter 5: Pointers and Structures

Chapter 6: More on Strings and Arrays of Strings

Chapter 7: More on Multi-Dimensional Arrays

Chapter 8: Pointers to Arrays

Chapter 9: Pointers and Dynamic Allocation of Memory

Chapter 10: Pointers to Functions

The Expat XML Parser – XML 分析/拆解工具 – C – 這個讚! 用 C 寫 spider 會用到

Expat is an XML parser library written in C. It is a stream-oriented parser in which an application registers handlers for things the parser might find in the XML document (like start tags). An introductory article on using Expat is available on xml.com.

Function reference : http://www.xml.com/pub/a/1999/09/expat/reference.html

新的轉碼方法 , libiconv , BIG5 轉 UTF8

這段 code , 很好用, 就貼在這兒了, 下次可以直接剪下跟貼上

重點有兩個:

1. From encoding 是 CP950

2. libiconv 不要用 libc 的, 因為缺一個 function : iconvctl

int fnConvert(const char *from, const char *to, char* save, int savelen, char *src, int srclen)
{
    iconv_t cd;
    char   *inbuf = src;
    char *outbuf = save;
    size_t outbufsize = savelen;
    int status = 0;
    size_t  savesize = 0;
    size_t inbufsize = srclen+1;
    char* inptr = inbuf;
    size_t      insize = inbufsize;
    char* outptr = outbuf;
    size_t outsize = outbufsize;

    if ( ( cd = iconv_open(to, from) ) == (iconv_t)-1 )
    {
        status = -1;
        goto done;
    }

    iconv(cd,NULL,NULL,NULL,NULL);
    if (inbufsize == 0)
    {
        status = -1;
        goto done;
    }
    while (insize > 0)
    {
        size_t res = iconv(cd, &inptr,&insize,&outptr,&outsize);
        if (outptr != outbuf)
        {
            int saved_errno = errno;
            int outsize = outptr - outbuf;
            strncpy(save+savesize, outbuf, outsize);
            errno = saved_errno;
        }
        if (res == (size_t)(-1))
        {
            if (errno == EILSEQ)
            {
                int one = 1;
                iconvctl(cd,ICONV_SET_DISCARD_ILSEQ,&one);
                status = -3;
            }
            else if (errno == EINVAL)
            {
                if (inbufsize == 0)
                {
                    status = -4;
                    goto done;
                }
                else
                {
                    break;
                }
            }
            else if (errno == E2BIG)
            {
                status = -5;
                goto done;
            }
            else
            {
                status = -6;
                goto done;
            }
        }
    }
    status = strlen(save);
done:
    iconv_close(cd);
    return status;
}

EXAMPLE:
int fnB2U( unsigned char *str )
{
    int srclen, destlen, status;
    unsigned char tmp[2048];

    status = -1;
    if ( ( srclen = strlen( str ) ) == 0 )
        return status;
    destlen  = sizeof( unsigned char ) * ( srclen * 2 ) + 1;
    tmp[0] = '';
    if ( ( status = fnConvert ( "CP950", "UTF-8", tmp, destlen, str, srclen) ) > 0 )
        strcpy( str, tmp);
    else
        printf( "status = %dn");
    return( status);
}

get my ip address useing c / sample code

C CODE

902c2c377de669ec360e37664cc1f041

/*
  FILENAME : my_local_ip.c
*/
#include
#include
#include
#include
#include
#include
#include
#include 

int main(int argc, char **argv) {
  int s;
  struct sockaddr_in sin;
  int slen;
  int port;

  if( argc<3 ) {
    printf("Usage: %s  \n", argv[0]);
    printf("\n\tsample %s 203.66.88.89 80\n",argv[0]);
    return -1;
  }

  port = atoi(argv[2]);

  s = socket(PF_INET, SOCK_STREAM, 0);
  memset(&sin, 0, sizeof(sin));
  sin.sin_family = PF_INET;
  sin.sin_port = htons(port);
  sin.sin_addr.s_addr = inet_addr(argv[1]);
  if(connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
    printf("127.0.0.1");
    return -1;
  }

  memset(&sin, 0, sizeof(sin));
  slen = sizeof(sin);
  if(getsockname(s, (struct sockaddr *)&sin, &slen) < 0) {
    printf("127.0.0.1");
    close(s);
    return -1;
  }

  printf("%s", inet_ntoa(sin.sin_addr));

  close(s);
  return 0;
}

可以依自己的需求來改這SHELL:

#!/bin/sh
ADDR=`my_local_ip 168.95.1.1 53`
FILENAME=${ADDR=}".txt"

echo $FILENAME

解決 error while loading shared libraries 的方法

執行程式時, 若遇到

./update_item: error while loading shared libraries: libsqlrclientwrapper-0.40.so.1: cannot open shared object file: No such file or directory

像這樣的錯誤訊息 , 表示程式中所需的 so 檔(share library) 沒找到 , 要修改 /etc/ld.so.conf 加上這個目錄 , 改完要下 ldconfig –v 讓路徑生效

可以用 ldd 看該程式所使用到的 shared library 有那些, 及 lib 路徑

—–

以下是 gentoo 的標準解法 , 其他版本的 linux 則不太相同!!

cat /etc/ld.so.conf   這個檔是由 env-update 產生的 , 所以再繼續看一下 /etc/env.d 下 , 究竟有啥

# ld.so.conf autogenerated by env-update; make all changes to
# contents of /etc/env.d directory
/usr/local/lib
/usr/i686-pc-linux-gnu/lib
/usr/lib/gcc/i686-pc-linux-gnu/4.1.2
/usr/lib/gcc/i686-pc-linux-gnu/4.1.1
/usr/local/firstworks/lib

參考資料, 這是 compile sqlrelay 列出來的 information

See any operating system documentation about shared libraries for

more information, such as the ld(1) and ld.so(8) manual pages.

———————————————————————-

/bin/sh ../../libtool –mode=finish /usr/local/firstworks/lib

PATH=”$PATH:/sbin” ldconfig -n /usr/local/firstworks/lib

———————————————————————-

Libraries have been installed in:

/usr/local/firstworks/lib

If you ever happen to want to link against installed libraries

in a given directory, LIBDIR, you must either use libtool, and

specify the full pathname of the library, or use the `-LLIBDIR’

flag during linking and do at least one of the following:

– add LIBDIR to the `LD_LIBRARY_PATH’ environment variable

during execution

– add LIBDIR to the `LD_RUN_PATH’ environment variable

during linking

– use the `-Wl,–rpath -Wl,LIBDIR’ linker flag

– have your system administrator add LIBDIR to `/etc/ld.so.conf’

在 /etc/env.d 下建立一個 90sqlrelay

內容是

LDPATH="/usr/local/firstworks/lib"

再run : env-update , 就看到 /etc/ld.so.conf 多了正確的 lib path 了

 

// ———————–
[12/6/13 下午4:37:54] RUTEN AA: 到 /usr/lib 找出原生的 .so 檔,應該會是 libsqlrclientwrapper-0.40.so.xxx.xxx
[12/6/13 下午4:38:13] RUTEN AA: ln -s libsqlrclientwrapper-0.40.so.xxx.xxx 到 loss 的 libsqlrclientwrapper-0.40.so.1.0.0
[12/6/13 下午4:38:17] RUTEN AA: 再 ldconfig
[12/6/13 下午4:40:53] RUTEN AA: 可以用 ldconfig -p 看是不是已經把 libsqlrclientwrapper-0.40.so.1 給 load 進來了