#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> 을 물어온다.
The network interface served by the static IP is configured by creating the configuration entry in the "/etc/network/interfaces" file as the following.
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.
간단하게 해석하자면, 물리적으로 연결이 되었음을 탐지하게 되면, 자동으로 설정을 실행한다는 의미이다.
(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
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
// 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;
원래는 파일의 생성일을 알아보려고 찾았는데..
리눅스에서는 파일 생성일은 저장을 안한다고 한다.
struct stat
{
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for filesystem I/O */
blkcnt_t st_blocks; /* number of blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};
위는 man fstat의 내용으로, 위에 보듯, time_t 구조체가 세개가 있는데
last access / modification / change 세가지 뿐이다.
int fseek(FILE *stream, long offset, int whence);
long ftell(FILE *stream);
void rewind(FILE *stream);
int fgetpos(FILE *stream, fpos_t *pos);
int fsetpos(FILE *stream, fpos_t *pos);
FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);
DESCRIPTION
getline() reads an entire line from stream, storing the address of the buffer containing the text into *lineptr.
The buffer is null-terminated and includes the newline character, if one was found.
If *lineptr is NULL, then getline() will allocate a buffer for storing the line, which should be freed by the
user program. Alternatively, before calling getline(), *lineptr can contain a pointer to a malloc()-allocated
buffer *n bytes in size. If the buffer is not large enough to hold the line, getline() resizes it with realloc(),
updating *lineptr and *n as necessary. In either case, on a successful call, *lineptr and *n will be updated to
reflect the buffer address and allocated size respectively.
getdelim() works like getline(), except a line delimiter other than newline can be specified as the delimiter
argument. As with getline(), a delimiter character is not added if one was not present in the input before end of
file was reached.
함수형을 보면, 파일에서 한줄씩(그러니까 CR/LF 까지) 읽어 오는데,
주의해야 할 점은, getline() 함수 내부적으로 malloc()를 호출 한다는 것이다.
즉, 만약에 malloc()을 해주지 않으려면, NULL 값인 포인터를 넘겨주면
알아서 malloc() 하거나 realloc() 해주므로 많은 신경을 쓰지 않아도 된다.
단, 마지막 getline() 호출 후, 사용이 끝나면 반드시 free()를 해주어야 한다.