Linux API/network2011. 9. 26. 15:38
프로그래밍을 하다보면 짜증나는 것 중에 하나가 바로 이넘의 Byte Order이다.
Intel CPU가 Little Endian 이다 보니 Big Endian 과 유사한 비트 스트림의 경우에는
순서를 죄다 뒤집어주어야 한다. (어셈블러로 그냥 이런 명령어 하나 좀 만들어 주지 -_-)

uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostshort);
uint32_t ntohl(uint32_t netlong);
uint16_t ntohs(uint16_t netshort);

The htonl() function converts the unsigned integer hostlong from host byte order to network byte order.
The htons() function converts the unsigned short integer hostshort from host byte order to network byte order.

The ntohl() function converts the unsigned integer netlong from network byte order to host byte order.
The ntohs() function converts the unsigned short integer netshort from network byte order to host byte order.

On the i80x86 the host byte order is Least Significant Byte first, whereas the network byte order, as used on the Internet, is Most Significant Byte first.
 
[링크 : http://linux.die.net/man/3/htonl

머.. 정리 하자면
hton 은 ip 주소를 보낼때
ntoh 는 ip 주소를 받았을때 사용하면 될듯? 
Posted by 구차니
Linux API/network2009. 12. 7. 13:42
0.0.0.0 은 간단하게 all allow의 의미라고 한다.







MS 넘들은 뻑하면 "의도된 설계입니다"
이 한마디로 구렁이 담 넘어 가듯 넘어간단 말이야 ㄱ-

The Winsock application that is listening on the designated port was written so that it binds to any local IP address by using INADDR_ANY. This means that the application will listen to all local interfaces and you can connect to the port of any of them. This is why netstat -an shows IP address 0.0.0.0 listening on the port.

[링크 : http://support.microsoft.com/?scid=kb%3Ben-us%3B175952&x=6&y=11]



-------------
$ netstat -an | more
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State
tcp        0      0 0.0.0.0:2049                0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:674                 0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:139                 0.0.0.0:*                   LISTEN

$ netstat -a | more
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State
tcp        0      0 *:nfs                       *:*                         LISTEN
tcp        0      0 *:acap                      *:*                         LISTEN
tcp        0      0 *:netbios-ssn               *:*                         LISTEN

리눅스 상에서는 0.0.0.0 은 *로 표시 되기도 한다.
(-n 옵션은 numeric하게 출력할지 아니면 /etc/services 의 문자열로 출력할지를 결정하는 옵션이다)

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

INADDR_ANY/INADDR_BROADCAST/INADDR_NONE 매크로  (0) 2011.09.29
hton(), ntoh()  (0) 2011.09.26
ioctl을 이용한 정보수집  (0) 2009.11.30
termios 구조체를 이용한 자국반향(echo) 제어  (0) 2009.08.27
canonical / non-canonical  (0) 2009.08.27
Posted by 구차니
Linux API/v4l2009. 12. 6. 19:41
  [CAMEL PC-100 : http://blog.danawa.com/prod/?prod_c=956003]
  [더불정보기술 X-LUK DBC-UP : http://blog.danawa.com/prod/?prod_c=911980]

위에 두녀석을 다시 꼽아 보니 둘다 video0가 생성이 된다.
CAMEL PC-100은 내장 마이크/사운드 장치가 있고
X-LUK DBC-UP은 내장 마이크 조차 없는 녀석이다(단지 PC용 monitor 프로그램이 있어서 산것일뿐 ㄱ-)

아무튼 /dev를 비교해보니 아래와 같이 추가가 되었다.

 CAMEL PC-10  더불정보기술 X-LUK DBC-UP
/dev
crw-rw----+ 1 root audio    14,  20 2009-12-06 19:22 audio1
crw-rw----+ 1 root audio    14,  19 2009-12-06 19:22 dsp1
crw-rw----+ 1 root audio    14,  16 2009-12-06 19:22 mixer1
drwxr-xr-x  4 root root          80 2009-12-06 19:22 v4l
crw-rw----+ 1 root video    81,   0 2009-12-06 19:22 video0

~$ tree /dev/v4l
/dev/v4l
|-- by-id
|   `-- usb-ARKMICRO_USB2.0_PC_CAMERA-video-index0 -> ../../video0
`-- by-path
    `-- pci-0000:00:10.4-usb-0:3:1.0-video-index0 -> ../../video0
/dev



drwxr-xr-x  4 root root          80 2009-12-06 19:30 v4l
crw-rw----+ 1 root video    81,   0 2009-12-06 19:30 video0

$ tree /dev/v4l
/dev/v4l
|-- by-id
|   `-- usb-Vimicro_Corp._PC_Camera-video-index0 -> ../../video0
`-- by-path
    `-- pci-0000:00:10.1-usb-0:1:1.0-video-index0 -> ../../video0

아무튼, video0 장치는 둘다 생기고, video4linux 장비로 인식을 한다. (ffmpeg이 왜 안되는겨 ㅠ.ㅠ)


CAMEL PC-10
$ ffmpeg -y -f video4linux -r 30 -s 320x240 -i /dev/video0 http://localhost:8090/feed1.ffm
FFmpeg version SVN-r19352-4:0.5+svn20090706-2ubuntu2, Copyright (c) 2000-2009 Fabrice Bellard, et al.
  configuration: --extra-version=4:0.5+svn20090706-2ubuntu2 --prefix=/usr --enable-avfilter --enable-avfilter-lavf --enable-vdpau --enable-bzlib --enable-libgsm --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-pthreads --enable-zlib --disable-stripping --disable-vhook --enable-gpl --enable-postproc --enable-swscale --enable-x11grab --enable-libdc1394 --extra-cflags=-I/build/buildd/ffmpeg-0.5+svn20090706/debian/include --enable-shared --disable-static
  libavutil     49.15. 0 / 49.15. 0
  libavcodec    52.20. 0 / 52.20. 0
  libavformat   52.31. 0 / 52.31. 0
  libavdevice   52. 1. 0 / 52. 1. 0
  libavfilter    0. 4. 0 /  0. 4. 0
  libswscale     0. 7. 1 /  0. 7. 1
  libpostproc   51. 2. 0 / 51. 2. 0
  built on Oct 13 2009 22:15:16, gcc: 4.4.1
[video4linux @ 0x85222c0]VIDIOCSWIN: Bad address
/dev/video0: I/O error occurred
Usually that means that input file is truncated and/or corrupted.


더불정보기술 X-LUK DBC-UP
$ ffmpeg -y -f video4linux -r 30 -s 320x240 -i /dev/video1 http://localhost:8090/feed1.ffm
FFmpeg version SVN-r19352-4:0.5+svn20090706-2ubuntu2, Copyright (c) 2000-2009 Fabrice Bellard, et al.
  configuration: --extra-version=4:0.5+svn20090706-2ubuntu2 --prefix=/usr --enable-avfilter --enable-avfilter-lavf --enable-vdpau --enable-bzlib --enable-libgsm --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-pthreads --enable-zlib --disable-stripping --disable-vhook --enable-gpl --enable-postproc --enable-swscale --enable-x11grab --enable-libdc1394 --extra-cflags=-I/build/buildd/ffmpeg-0.5+svn20090706/debian/include --enable-shared --disable-static
  libavutil     49.15. 0 / 49.15. 0
  libavcodec    52.20. 0 / 52.20. 0
  libavformat   52.31. 0 / 52.31. 0
  libavdevice   52. 1. 0 / 52. 1. 0
  libavfilter    0. 4. 0 /  0. 4. 0
  libswscale     0. 7. 1 /  0. 7. 1
  libpostproc   51. 2. 0 / 51. 2. 0
  built on Oct 13 2009 22:15:16, gcc: 4.4.1
Input #0, video4linux, from '/dev/video1':
  Duration: N/A, start: 1260102822.110153, bitrate: 27647 kb/s
    Stream #0.0: Video: rawvideo, yuv420p, 320x240, 27647 kb/s, 30 tbr, 1000k tbn, 30 tbc
Output #0, ffm, to 'http://localhost:8090/feed1.ffm':
    Stream #0.0: Video: mpeg1video, yuv420p, 320x240, q=3-31, 512 kb/s, 1000k tbn, 30 tbc
Stream mapping:
  Stream #0.0 -> #0.0
Press [q] to stop encoding
[mpeg1video @ 0x9940160]rc buffer underflow
VIDIOCMCAPTURE: Invalid argument
frame=    1 fps=  0 q=31.7 Lsize=      12kB time=10000000000.00 bitrate=   0.0kbits/s
video:8kB audio:0kB global headers:0kB muxing overhead 54.352468%



2010.02.18 추가

열화와 같은 성원(?)으로 드라이버 및 프로그램을 올려 놓습니다.
X-LUK DBC-UP 에는 xp용 드라이버와 감시프로그램이
vista 에는 vista용 드라이버가 들어있습니다.


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

v4l2 debug  (0) 2022.07.13
v4l2-ctl 밝기 조절  (0) 2022.07.06
v4l2 timestamp  (0) 2017.04.19
uv4l  (0) 2015.09.13
Posted by 구차니
Linux API/network2009. 11. 30. 17:35
ioctl은

#include <sys/ioctl.h>
int ioctl(int d, int request, ...);

이런 모양인데, int d는 descriptor로 socket을 통해 받아와야 한다.
그리고 request는 /usr/include/bits/ioctls.h 파일에 정의된 내용을 사용하면 된다.


잠시 socket을 소개하자면

#include <sys/socket.h>
int socket(int domain, int type, int protocol);

domain
    Name                Purpose                          Man page
    PF_UNIX, PF_LOCAL   Local communication              unix(7)
    PF_INET             IPv4 Internet protocols          ip(7)
    PF_INET6            IPv6 Internet protocols
    PF_IPX              IPX - Novell protocols
    PF_NETLINK          Kernel user interface device     netlink(7)
    PF_X25              ITU-T X.25 / ISO-8208 protocol   x25(7)
    PF_AX25             Amateur radio AX.25 protocol
    PF_ATMPVC           Access to raw ATM PVCs
    PF_APPLETALK        Appletalk                        ddp(7)
    PF_PACKET           Low level packet interface       packet(7)

type
   SOCK_STREAM
          Provides sequenced, reliable, two-way, connection-based byte streams.  An  out-of-band  data  transmission
          mechanism may be supported.

   SOCK_DGRAM
          Supports datagrams (connectionless, unreliable messages of a fixed maximum length).

   SOCK_SEQPACKET(is not implemented for AF_INET)
          Provides  a  sequenced,  reliable,  two-way connection-based data transmission path for datagrams of fixed
          maximum length; a consumer is required to read an entire packet with each read system call.

   SOCK_RAW
          Provides raw network protocol access.

   SOCK_RDM
          Provides a reliable datagram layer that does not guarantee ordering.

   SOCK_PACKET
          Obsolete and should not be used in new programs; see packet(7).


protocol
    Normally only a single protocol exists to support a particular socket type within a given protocol family, in which case protocol can be specified as 0.

값들을 필요로 한다.

[링크 : http://www.joinc.co.kr/modules/moniwiki/wiki.php/article/network_information#s-2.2]

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

hton(), ntoh()  (0) 2011.09.26
netstat 에서 0.0.0.0의 의미  (2) 2009.12.07
termios 구조체를 이용한 자국반향(echo) 제어  (0) 2009.08.27
canonical / non-canonical  (0) 2009.08.27
struct in_addr  (0) 2009.08.18
Posted by 구차니
Linux API/network2009. 8. 27. 14:47
Try It Out − A Password Program with termios

1. Our password program, password.c, begins with the following definitions:
#include <termios.h>
#include <stdio.h>
#define PASSWORD_LEN 8
int main()
{
    struct termios initialrsettings, newrsettings;
    char password[PASSWORD_LEN + 1];

2. Next, add in a line to get the current settings from the standard input and copy them into the termios
structure that we created above.
    tcgetattr(fileno(stdin), &initialrsettings);

3. Make a copy of the original settings to replace them at the end. Turn off the ECHO flag on the
newrsettings and ask the user for their password:
    newrsettings = initialrsettings;
    newrsettings.c_lflag &= ~ECHO;
    printf("Enter password: ");

4. Next, set the terminal attributes to newrsettings and read in the password. Lastly, reset the terminal
attributes to their original setting and print the password to render all the previous effort useless.
    if(tcsetattr(fileno(stdin), TCSAFLUSH, &newrsettings) != 0) {
        fprintf(stderr,"Could not set attributes\n");
    }
    else {
        fgets(password, PASSWORD_LEN, stdin);
        tcsetattr(fileno(stdin), TCSANOW, &initialrsettings);
        fprintf(stdout, "\nYou entered %s\n", password);
    }
    exit(0);
}

[링크 : http://people.freedesktop.org/~nagappan/beginning-linux-programming.pdf]

[링크 : http://ttongfly.net/zbxe/?document_srl=45222]
ttongfly.net 의 내용은 아마도 위의 beginning linux programming의 내용을 번역한 것으로 보인다.

위와 같은 작동은 간단하게 쉘에서
"stty -echo" << echo 끔
"stty echo" << echo 켬
로 가능하다.

하지만, 프로그램에서 쉘 호출 해봤자, 자기 자신에게 적용이 안되므로
termios를 이용해서 직접 제어를 해야 하는데, termios는 말그대로 TERMinal IO Stucture 이다.
소스에서 include 할녀석은 <termios.h> 이고 이녀석은 <bits/termios.h> 을 물어온다.

$ cat /usr/include/bits/termios.h
...
struct termios
  {
    tcflag_t c_iflag;           /* input mode flags */
    tcflag_t c_oflag;           /* output mode flags */
    tcflag_t c_cflag;           /* control mode flags */
    tcflag_t c_lflag;           /* local mode flags */
    cc_t c_line;                        /* line discipline */
    cc_t c_cc[NCCS];            /* control characters */
    speed_t c_ispeed;           /* input speed */
    speed_t c_ospeed;           /* output speed */
#define _HAVE_STRUCT_TERMIOS_C_ISPEED 1
#define _HAVE_STRUCT_TERMIOS_C_OSPEED 1
  };
...
/* c_lflag bits */
#define ISIG    0000001
#define ICANON  0000002
#if defined __USE_MISC || defined __USE_XOPEN
    # define XCASE  0000004
#endif
#define ECHO    0000010
#define ECHOE   0000020
#define ECHOK   0000040
#define ECHONL  0000100
#define NOFLSH  0000200
#define TOSTOP  0000400
#ifdef __USE_MISC
    # define ECHOCTL 0001000
    # define ECHOPRT 0002000
    # define ECHOKE  0004000
    # define FLUSHO  0010000
    # define PENDIN  0040000
#endif
#define IEXTEN  0100000

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

netstat 에서 0.0.0.0의 의미  (2) 2009.12.07
ioctl을 이용한 정보수집  (0) 2009.11.30
canonical / non-canonical  (0) 2009.08.27
struct in_addr  (0) 2009.08.18
ifup/ifdown  (0) 2009.08.18
Posted by 구차니
Linux API/network2009. 8. 27. 14:24
리눅스 터미널 프로그래밍에서
캐노니컬 모드(canonical)는 한줄 단위로 받아들이고
넌 캐노니컬 모드(non-canonical)는 문자 단위로 받아들인다.

[링크 : http://ttongfly.net/zbxe/?document_srl=45222]
Posted by 구차니
Linux API/network2009. 8. 18. 14:51
vi /usr/include/linux/in.h

53 /* Internet address. */
54 struct in_addr {
55         __u32   s_addr;
56 };

char *inet_ntoa(struct in_addr in);

에서 사용하는 구조체이다.
단일 변수 하나의 구조체인데..
굳이 구조체로 만든 이유는.. IPv6 등을 위한걸려나..



사족 : 귀찮으면 int형으로 그냥 쓰면된다. 그런데.. 이 내용 저번에 적었던거 같은데... 기억이 안나네 -ㅁ-?
Posted by 구차니
Linux API/network2009. 8. 18. 11:16
이 녀석들은 데비안 출신이었나?

5.3.5. The network interface with the static IP

The network interface served by the static IP is configured by creating the configuration entry in the "/etc/network/interfaces" file as the following.

allow-hotplug eth0
iface eth0 inet static
address 192.168.11.100
netmask 255.255.255.0
broadcast 192.168.11.255
gateway 192.168.11.1
dns-domain lan
dns-nameservers 192.168.11.1

When the Linux kernel detects the physical interface eth0, the allow-hotplug stanza will cause ifup to bring up the interface and the iface stanza will cause ifup to use the static IP to configure the interface.


[링크 : http://www.debian.org/doc/manuals/debian-reference/ch05.en.html#_the_network_interface_with_the_static_ip]


간단하게 해석하자면, 물리적으로 연결이 되었음을 탐지하게 되면, 자동으로 설정을 실행한다는 의미이다.
(interface-eth0-를 up 시키고 iface구절을 실행함)


Check to see if a Network Link/Interface is up|down

ip link show eth0
//the iproute package is required. If there is no ip package, apt-get install iproute

If eth0 has a Network link, the following will be returned:
2: eth0: <BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:26:57:4e brd ff:ff:ff:ff:ff:ff

If eth0 has no Network link, the following will be returned:
2: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:26:57:4e brd ff:ff:ff:ff:ff:ff

The "UP" is the operative word.


[링크 : http://wiki.kartbuilding.net/index.php/Ifup-ifdown]


실제로 ifup, ifdown 시에 제대로 interface가 up / down 되었는지 확인하는 방법이다.
Posted by 구차니
Linux API/network2009. 7. 30. 15:48
랜선을 뽑고 꼽으면 아래와 같은 메시지가 출력된다.

eth0 : link down
eth0 : link up

dmesg로 매번 감시할수도 없는 노릇이고 그래서 코드로 하는 법을 찾아 왔는데
아직까지는 컴파일도 못시켜 봤다..(써글넘의 타입)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <stdint.h>
#include <net/if.h>
#include <netinet/in.h>
#include <ctype.h>

typedef uint32_t u32;
typedef uint16_t u16;
typedef uint8_t u8;

#include <linux/sockios.h>
#include <linux/ethtool.h>

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
// Get the link status. This returns TRUE if the link is up and FALSE
// otherwise.
//
int32_t link_stat()
{
        int s;
        struct ifreq ifr;
        struct ethtool_value eth;

        if((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP)) == FAIL)
                ERRORS(Socket);

        bzero(&ifr, sizeof(ifr));
        strcpy(ifr.ifr_name, Eth0);
        ifr.ifr_data = (caddr_t) &eth;
        eth.cmd = ETHTOOL_GLINK;

        if(ioctl(s, SIOCETHTOOL, &ifr) == FAIL)
                ERRORS(Ioctl);
        (void)close(s);

        return (eth.data) ? TRUE:FALSE;
}

void main()
{
        int ret;
        ret = link_stat();

        if(ret == TRUE) printf("link up\n");
        else printF("link down\n");
}

[링크 : http://lkml.indiana.edu/hypermail/linux/kernel/0505.2/0754.html]

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

struct in_addr  (0) 2009.08.18
ifup/ifdown  (0) 2009.08.18
리눅스 소켓 프로그래밍 - linux socket programming  (0) 2009.07.17
fstat - 파일의 상태 얻어오기  (2) 2009.07.02
Linux File Descriptor / File pointer  (0) 2009.06.30
Posted by 구차니
Linux API/network2009. 7. 17. 15:04

내용



[링크 : http://wiki.kldp.org/Translations/html/Socket_Programming-KLDP/Socket_Programming-KLDP.html]
[링크 : http://www.lug.or.kr/docs/LINUX/others/97-11-5.htm]

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

ifup/ifdown  (0) 2009.08.18
[확인중] 이더넷 링크 상태 확인하기 - howto get link status  (0) 2009.07.30
fstat - 파일의 상태 얻어오기  (2) 2009.07.02
Linux File Descriptor / File pointer  (0) 2009.06.30
getline()  (0) 2009.06.25
Posted by 구차니