프로그래밍을 하다보면 짜증나는 것 중에 하나가 바로 이넘의 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.
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.
$ 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 의 문자열로 출력할지를 결정하는 옵션이다)
#include <sys/ioctl.h>
int ioctl(int d, int request, ...);
이런 모양인데, int d는 descriptor로 socket을 통해 받아와야 한다.
그리고 request는 /usr/include/bits/ioctls.h 파일에 정의된 내용을 사용하면 된다.
/* Copyright (C) 1996, 1997, 1998, 2003 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
#ifndef _SYS_IOCTL_H
# error "Never use directly; include instead."
#endif
/* Use the definitions from the kernel header files. */
#include "asm/ioctls.h"
/* Routing table calls. */
#define SIOCADDRT 0x890B /* add routing table entry */
#define SIOCDELRT 0x890C /* delete routing table entry */
#define SIOCRTMSG 0x890D /* call to routing system */
/* Socket configuration controls. */
#define SIOCGIFNAME 0x8910 /* get iface name */
#define SIOCSIFLINK 0x8911 /* set iface channel */
#define SIOCGIFCONF 0x8912 /* get iface list */
#define SIOCGIFFLAGS 0x8913 /* get flags */
#define SIOCSIFFLAGS 0x8914 /* set flags */
#define SIOCGIFADDR 0x8915 /* get PA address */
#define SIOCSIFADDR 0x8916 /* set PA address */
#define SIOCGIFDSTADDR 0x8917 /* get remote PA address */
#define SIOCSIFDSTADDR 0x8918 /* set remote PA address */
#define SIOCGIFBRDADDR 0x8919 /* get broadcast PA address */
#define SIOCSIFBRDADDR 0x891a /* set broadcast PA address */
#define SIOCGIFNETMASK 0x891b /* get network PA mask */
#define SIOCSIFNETMASK 0x891c /* set network PA mask */
#define SIOCGIFMETRIC 0x891d /* get metric */
#define SIOCSIFMETRIC 0x891e /* set metric */
#define SIOCGIFMEM 0x891f /* get memory address (BSD) */
#define SIOCSIFMEM 0x8920 /* set memory address (BSD) */
#define SIOCGIFMTU 0x8921 /* get MTU size */
#define SIOCSIFMTU 0x8922 /* set MTU size */
#define SIOCSIFNAME 0x8923 /* set interface name */
#define SIOCSIFHWADDR 0x8924 /* set hardware address */
#define SIOCGIFENCAP 0x8925 /* get/set encapsulations */
#define SIOCSIFENCAP 0x8926
#define SIOCGIFHWADDR 0x8927 /* Get hardware address */
#define SIOCGIFSLAVE 0x8929 /* Driver slaving support */
#define SIOCSIFSLAVE 0x8930
#define SIOCADDMULTI 0x8931 /* Multicast address lists */
#define SIOCDELMULTI 0x8932
#define SIOCGIFINDEX 0x8933 /* name -> if_index mapping */
#define SIOGIFINDEX SIOCGIFINDEX /* misprint compatibility :-) */
#define SIOCSIFPFLAGS 0x8934 /* set/get extended flags set */
#define SIOCGIFPFLAGS 0x8935
#define SIOCDIFADDR 0x8936 /* delete PA address */
#define SIOCSIFHWBROADCAST 0x8937 /* set hardware broadcast addr */
#define SIOCGIFCOUNT 0x8938 /* get number of devices */
#define SIOCGIFBR 0x8940 /* Bridging support */
#define SIOCSIFBR 0x8941 /* Set bridging options */
#define SIOCGIFTXQLEN 0x8942 /* Get the tx queue length */
#define SIOCSIFTXQLEN 0x8943 /* Set the tx queue length */
/* ARP cache control calls. */
/* 0x8950 - 0x8952 * obsolete calls, don't re-use */
#define SIOCDARP 0x8953 /* delete ARP table entry */
#define SIOCGARP 0x8954 /* get ARP table entry */
#define SIOCSARP 0x8955 /* set ARP table entry */
/* RARP cache control calls. */
#define SIOCDRARP 0x8960 /* delete RARP table entry */
#define SIOCGRARP 0x8961 /* get RARP table entry */
#define SIOCSRARP 0x8962 /* set RARP table entry */
/* Driver configuration calls */
#define SIOCGIFMAP 0x8970 /* Get device parameters */
#define SIOCSIFMAP 0x8971 /* Set device parameters */
/* DLCI configuration calls */
#define SIOCADDDLCI 0x8980 /* Create new DLCI device */
#define SIOCDELDLCI 0x8981 /* Delete DLCI device */
/* Device private ioctl calls. */
/* These 16 ioctls are available to devices via the do_ioctl() device
vector. Each device should include this file and redefine these
names as their own. Because these are device dependent it is a good
idea _NOT_ to issue them to random objects and hope. */
#define SIOCDEVPRIVATE 0x89F0 /* to 89FF */
/*
* These 16 ioctl calls are protocol private
*/
#define SIOCPROTOPRIVATE 0x89E0 /* to 89EF */
잠시 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.
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);
}
위와 같은 작동은 간단하게 쉘에서
"stty -echo" << echo 끔
"stty echo" << echo 켬
로 가능하다.
하지만, 프로그램에서 쉘 호출 해봤자, 자기 자신에게 적용이 안되므로
termios를 이용해서 직접 제어를 해야 하는데, termios는 말그대로 TERMinal IO Stucture 이다.
소스에서 include 할녀석은 <termios.h> 이고 이녀석은 <bits/termios.h> 을 물어온다.