6 berkeley - 145 -
struct in_addr {
unsigned long s_addr;
};
ina struct sockaddr_in
struct in_addr
ina.sin_addr.s_addr 4 IP
4 IP #defines
6.5.2
1
IP
2
h to n
to Network Short Host
s H-to-n-s htons()
68000
n h to s l IP
stolh() Short to Long Host?
l htons() “Host to Network Short”
4 bytes
l htonl() “Host to Network Long”
8 bytes
l ntohs() “Network to Host Short “
4 bytes
l ntohl() “Network to Host Long “
8 bytes
htonl()
Internet
- 146 - Linux
struct sockaddr_in sin_addr sin_port IP UDP
sin_family sin_port IP UDP sin_family
sin_family
sin_addr
struct sockaddr_in
3
Linux IP
IP
struct sockaddr_in ina IP 166.111.69.52
IP ina inet_addr()
IP
ina.sin_addr.s_addr = inet_addr “166.111.69.52” ;
l inet_addr() htonl()
l inet_addr()
–1 255.255.255.255 !!
–1
IP IP ..
Network to ASCII
IP
struct in_addr . ..
.
inet_ntoa() ntoa
printf “%s”, inet_ntoa(ina.sin_addr) ;
struct in_addr
l inet_ntoa() struct in_addr
l inet_ntoa()
inet_ntoa() static
inet_ntoa() inet_ntoa()
char *a1, a2;
a1 = inet_ntoa(ina1.sin_addr); /* this is 166.111.69.52 */
a2 = inet_ntoa(ina2.sin_addr); /* this is 166.111.69.53 */
printf(“address 1: %s\n”,a1);
printf(“address 2: %s\n”,a2);
address 1: 166.111.69.53
address 2: 166.111.69.53
6 berkeley inet_ntoa() - 147 -
strcpy()
IP
6.6 BSD
Linux
Banding
l socket()
l bind()
l connect()
l listen()
l accept()
l send()
l recv()
l sendto()
l recvfrom()
l close()
l shutdown()
l setsockopt()
l getsockopt()
l getpeername()
l getsockname()
l gethostbyname()
l gethostbyaddr()
l getprotobyname()
l fcntl()
6.6.1
socket
#include
#include
int socket int domain , int type , int protocol ;
- 148 - Linux
int domain int type int protocol socket()
domain AF_INET struct sockaddr_in type
SOCK_DGRAM
socket SOCK_STREAM
types
protocol 0 socket man pages
domain AF_INET
SOCK_STREAM SOCK_DGRAM
socket() –1 errno perror() man
socket()
pages
6.6.2
bind()
socket() socket
l listen()
Telnet a.b.c.d 4000
l connect()
bind()
#include
#include
int bind (int sockfd , struct sockaddr *my_addr , int addrlen) ;
l sockfd socket()
l my_addr struct sockaddr
IP sizeof(struct sockaddr)
l addrlen
#include
#include
#include
#define MYPORT 4000
main()
{
int sockfd ;
6 berkeley - 149 -
struct sockaddr_in my_addr ;
sockfd = socket(AF_INET, SOCK_STREAM, 0); /* */
/* ! */ */
*/
my_addr.sin_family = AF_INET ; /*
*/
my_addr.sin_port = htons MYPORT ; /*
my_addr.sin_addr.s_addr = inet_addr(“166.111.69.52”) ;
bzero(&(my_addr.sin_zero), 8); /*
/* 0 */
/* bind */
bind (sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr) ;
l my_addr.sin_port Linux
l my_addr.sin_addr.s_addr
l man pages
IP
bind()
my_addr.sin_port = 0 ; /* */
*/
my_addr.sin_addr.s_addr = INADDR_ANY ; /*
s_addr
my_addr.sin_port 0 bind()
INADDR_ANY
my_addr.sin_addr.s_addr INADDR_ANY bind()
IP bind()
INADDR_ANY 00 0
INADDR_ANY #define
INADDR_ANY #define INADDR_ANY 100
OK
my_addr.sin_port = htons(0); /* */
my_addr.sin_addr.s_addr = htonl(INADDR_ANY) ; /* IP */
INADDR_ANY bind
bind() –1 errn
1024 bind() root
1024 65535
- 150 - Linux
bind() bind()
Telnet connect() connect()
166.111.69.52
bind() socket
6.6.3
Telnet
socket()
23 Telnet
Telnet
connect()
connect()
#include
#include
int connect (int sockfd, struct sockaddr *serv_addr, int addrlen);
connect()
l sockfd socket()
l serv_addr IP
l addrlen sizeof(struct sockaddr)
#include
#include
#include
#define DEST_IP “166.111.69.52”
#define DEST_PORT 23
main()
{
int sockfd ;
/* */
struct sockaddr_in dest_addr ;
/* */
sockfd = socket AF_INET, SOCK_STREAM, 0 ;
/* */
dest_addr.sin_family = AF_INET ;
/* */
dest_addr.sin_port = htons DEST_PORT ;
6 berkeley - 151 -
dest_addr.sin_addr.s_addr = inet_addr DEST_IP ;
/* 0 */
bzero(&(dest_addr.sin_zero), 8 ;
/* connect() */
connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr));
connect()
–1 errno
bind()
Linux
,
l socket() send()
l bind() accept()
l listen()
l accept()
l recv()
6.6.4
listen()
listen() accept()
listen()
#include
int listen(int sockfd, int backlog);
listen() socket()
l sockfd –1
l backlog
backlog
listen accept() accept()
accept() backlog
listen()
5 10
listen()
listen()
errno
bind()
- 152 - Linux
socket() ; accept() */
bind() ;
listen() ;
/*
listen() accept()
listen()
6.6.5 accept() connect()
l listen() accept()
listen backlog
l
listen()
l accept()
l
accept()
listen() recv()
send()
accept()
#include
int accept(int sockfd, void *addr, int *addrlen);
accept() listen()
l sockfd struct sockaddr_in
l addr
IP
l addrlen accept()
sizeof(struct sockaddr_in) accept() addr addrlen bytes
accept() addr addrlen accept() addrlen
accept() accept() –1
errno
#include
#include
#include
/* */
6 berkeley - 153 -
#define MYPORT 4000
/* accept() */
#define BACKLOG 10 sock_fd new_fd */
main()
{
/*
int sockfd, new_fd ;
/* */
struct sockaddr_in my_addr ;
/* */
struct sockaddr_in their_addr ;
int sin_size; */
/*
sockfd = socket(AF_INET, SOCK_STREAM, 0) ;
/* */
my_addr.sin_family = AF_INET ;
/* */
my_addr.sin_port = htons(MYPORT) ;
/* IP */
my_addr.sin_addr.s_addr = INADDR_ANY ;
/* */
bzero(&(my_addr.sin_zero), 8) ;
/* */
bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));
listen(sockfd, BACKLOG);
sin_size = sizeof(struct sockaddr_in);
new_fd = accept(sockfd, &their_addr, &sin_size);
new_fd send() recv()
close())
sock_fd
- 154 - socket() Linux
connect()
l write()
l read()
l
6.6.6
sendto() recvfrom()
send()
#include
#include
int send(int sockfd, const void *msg, int len, int flags);
send
l sockfd
l msg
l len
l flags 0 send man pages
send()
char *msg = “Hello! World!”;
int len, bytes_sent;
len = strlen(msg);
bytes_sent = send(sockfd, msg, len, 0);
send() send() send()
send() –1
send() recv()
send() len
1K send()
send()
send()
errno
recv()
recv()
#include
#include
6 berkeley - 155 -
int recv(int sockfd, void *buf, int len, unsigned int flags ; recv()
buf errno
recv() 0
l sockfd
l buf
l len
l flags recv()
man pages
recv()
–1
Ya! Linux
UDP
6.6.7
IP
sendto() recvfrom()
#include
#include
int sendto int sockfd, const void *msg, int len, unsigned int flags,
const struct sockaddr *to, int tolen ;
l sockfd send()
l msg 0 send man pages
l len
l flags
l to struct sockaddr IP
l tolen struct sockaddr sizeof(struct sockaddr)
send() sendto() send()
–1
errno
recv() recvfrom()
recvfrom()
#include
- 156 - Linux
#include
int recvfrom(int sockfd, void *buf, int len, unsigned int flags
struct sockaddr *from, int *fromlen);
l sockfd 0 recv()
l buf struct sockaddr
l len IP
l flags recv()
sizeof struct
man pages struct sockaddr
l from
l fromlen int form
sockaddr formlen
recvfrom() –1 errno
flags ,
(timeout)
(man pages) recvfrom
send() recv()
cnnect()
send() recv()
6.6.8 close()
close(sockfd);
close()
shutdown() shutdown()
#include 1
int shutdown int sockfd, int how ;
l sockfd 0
l how
2 close() 6 berkeley - 157 -
shutdown() –1
0
errno shutdown()
UDP connect() bug
socket
6.6.9
Linux
Linux bind() getsockopt()
Linux/UNIX options setsockopt()
Linux
setsockopt()
#include
#include
int getsockopt(int sockfd, int level, int name, char *value, int *optlen);
int setsockopt(int sockfd, int level, int name, char *value, int *optlen);
l sockfd protocol level TCP/IP IPPROTO_TCP
l level
l name SOL_SOCKET setsockopt()
l value man page
l optlen
getsockopt()
Linux
setsockopt()
/* */
opt = 1; len = sizeof(opt);
/* */
setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&opt,&len);
setsockopt() man pages
6.6.10
IP
- 158 - Linux
#include
int getpeername(int sockfd, struct sockaddr *addr, int *addrlen);
l sockfd struct sockaddr struct sockaddr_in
l addr int sizeof(struct sockaddr)
l addrlen –1
IP
errno inet_ntoa() gethostbyaddr()
6.6.11 getpeername()
gethostbyname()
gethostname()
–1 errno
IP
#include
int gethostname(char *hostname, size_t size);
l hostname
l size hostname
0
6.7
6.7.1
DNS DNS Domain Name Service IP
IP
bind() connect() sendto()
$ telnet bbs.tsinghua.edu.cn
Telnet 202.112.58.200 DNS
6.7.2 gethostbyname()
DNS
6 berkeley - 159 -
#include Struct hostent
struct hostent *gethostbyname(const char *name);
struct hostent
struct hostent {
char *h_name;
char **h_aliases;
int h_addrtype;
int h_length;
char **h_addr_list;
};
#define h_addr h_addr_list[0]
l h_name NULL AF_INET
l h_aliases 0
l h_addrtype
l h_length
l h_addr_list
l h_addr - h_addr_list struct hostent NULL
gethostbyname() h_errno herror()
errno
6.7.3
#include
#include
#include
#include
#include
#include
int
main (int argc, char *argv[])
{
struct hostent *h;
/* */
- 160 - Linux
if (argc != 2)
/* */
fprintf (stderr “usage: getip address\n”);
/* */
exit(1);
}
/* */ */
if (h=gethostbyname(argv[1])) == NULL
{
/* gethostbyname
herror(“gethostbyname”);
/* */
exit(1);
}
/* */
printf(“Host name : %s\n”, h->h_name);
printf(“IP Address : %s\n”, inet_ntoa (*((struct in_addr *)h->h_addr)) ;
/* */
return 0;
} perror()
gethostbyname()
h_errno errno herror()
gethostbyname()
bbs.tsinghua.edu.cn struct hostent IP
IP
h->h_addr char* inet_ntoa() struct in_addr
h->h_addr struct in_addr*
6.8
Telnet 23
Telnetd login
SOCK_STREAM, SOCK_DGRAM
6 berkeley - 161 -
telnet/telnetd ftp/ftpd bootp/bootpd ftp
ftpd
fork()
6.8.1
$ telnet remotehostname 4000 Hello,World!
remotehostname telnet
#include */ if ((sockfd =
#include
#include
#include
#include
#include
#include
#include
/* */
#define MYPORT 4000
/* accept */
#define BACKLOG 10
main()
{ new_fd */
/* sock_fd socket()
int sock_fd, new_fd ;
/* */
struct sockaddr_in my_addr;
/* */
struct sockaddr_in their_addr;
int sin_size;
/*
- 162 - Linux
socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
/* */
perror(“socket”);
exit(1);
}
/* */
my_addr.sin_family = AF_INET;
/* */
my_addr.sin_port = htons(MYPORT);
/* IP s_addr */
my_addr.sin_addr.s_addr = INADDR_ANY;
/* */ */ if (bind(sockfd, (struct sockaddr *)&my_addr,
bzero(&(my_addr.sin_zero), 8); */
/*
sizeof(struct sockaddr)) == -1)
{
/* bind()
perror(“bind”);
exit(1);
}
/* */
if (listen(sockfd, BACKLOG) == -1)
{ */
/* listen
perror(“listen”);
exit(1);
}
while(1)
{
/* accept() */
sin_size = sizeof(struct sockaddr_in);
/* */
6 berkeley - 163 -
if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1)
{
/* accept() */
perror(“accept”);
continue;
}
/* */
printf “server: got connection from %s\n”, inet_ntoa(their_addr.sin_addr) ;
/* */
if (!fork())
/* */
/* */
if (send(new_fd, “Hello, world!\n”, 14, 0) == -1)
{ */
/*
perror(“send”);
close(new_fd);
exit(0);
} */
/* new_fd
close(new_fd);
}
}
/* */
while(waitpid(-1,NULL,WNOHANG) > 0);
}
main()
6.8.2 Hello, World!
connect()
4000
#include
#include
#include
#include
#include
- 164 - Linux
#include
#include
#include
/* */
#define PORT 4000
/* */
#define MAXDATASIZE 100
int
main(int argc, char *argv[])
{
/* */
int sockfd, numbytes;
char buf[MAXDATASIZE];
struct hostent *he;
/* */
struct sockaddr_in their_addr;
/* */ */
if (argc != 2)
{
/*
fprintf(stderr,“usage: client hostname\n”);
exit(1);
}
/* */
if ((he=gethostbyname(argv[1])) == NULL)
/* gethostbyname() */
herror(“gethostbyname”);
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
/* socket() */
perror(“socket”);
exit(1);
}
6 berkeley - 165 -
/* */
their_addr.sin_family = AF_INET;
/* */
their_addr.sin_port = htons(PORT);
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
/* */
bzero(&(their_addr.sin_zero), 8);
if connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1
{
/* connect() */
perror(“connect”);
exit(1);
}
if (numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1
{
/* */
perror(“recv”);
exit(1);
}
buf[numbytes] = ‘\0’; server client “Connection
printf(“Received: %s”,buf);
close(sockfd); 5000 5000 talker.c
return 0;
}
client
refused”
6.8.3
listener.c
listener
talker UDP
listener.c
#include
- 166 - Linux
#include
#include
#include
#include
#include
#include
#include
/* */
#define MYPORT 5000
/* */
#define MAXBUFLEN 100
main()
{
int sockfd;
/* */
struct sockaddr_in my_addr;
/* */
struct sockaddr_in their_addr;
int addr_len, numbytes;
char buf[MAXBUFLEN];
/* */
if (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1
{
/* */
perror(“socket”);
exit(1);
}
/* */
my_addr.sin_family = AF_INET;
/* */
my_addr.sin_port = htons(MYPORT);
/* IP */
my_addr.sin_addr.s_addr = INADDR_ANY;
6 berkeley - 167 -
/* */
bzero(&(my_addr.sin_zero), 8);
/* */
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
{
/* */
perror(“bind”);
exit(1);
}
addr_len = sizeof(struct sockaddr);
/* */
if ((numbytes=recvfrom(sockfd, buf, MAXBUFLEN, 0,
(struct sockaddr *)&their_addr, &addr_len)) == -1)
{
/* recvfrom() */
perror(“recvfrom”);
exit(1);
}
/* */
printf(“got packet from %s\n”,inet_ntoa(their_addr.sin_addr));
printf(“packet is %d bytes long\n”,numbytes);
buf[numbytes] = ‘\0’;
printf(“packet contains \”%s\“\n”,buf);
/* */ SOCK_DGRAM
close(sockfd);
}
socket()
listen() accept()
talker.c
#include
#include
#include
#include
#include
#include
- 168 - Linux
#include
#include
#include
/* */
#define MYPORT 5000
int main(int argc, char *argv[])
{
int sockfd;
/* */
struct sockaddr_in their_addr;
struct hostent *he;
int numbytes;
if (argc != 3) */
{
/*
fprintf(stderr,“usage: talker hostname message\n”);
exit(1);
}
if ((he=gethostbyname(argv[1])) == NULL) */
{
/*
herror(“gethostbyname”);
exit(1);
}
if (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
/* */
perror (“socket”);
exit(1);
}
/* */
their_addr.sin_family = AF_INET;
/* */
6 berkeley - 169 -
their_addr.sin_port = htons(MYPORT);
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
/* */
bzero(&(their_addr.sin_zero), 8);
if ((numbytes=sendto(sockfd, argv[2], strlen(argv[2]), 0,
(struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1)
{
/* */
perror(“recvfrom”);
exit(1);
}
printf(“sent %d bytes to %s\n”,numbytes,inet_ntoa(their_addr.sin_addr));
/* */
close(sockfd);
return 0;
}
listener
talker
talker talker connect() listener
sent() recv() talker connect()
6.9
6.9.1
TCP UDP
1 255 1
255 IP TCP
TCP
.
IP
- 170 - Linux
6.9.2 23 25 25
23 1024 Root
1024
1024
RedHat 6.0 /etc/services
[root@bbs /etc]# cat /etc/services
# /etc/services:
# $Id: services,v 1.4 1997/05/20 19:41:21 tobias Exp $
#
# Network services, Internet style
#
# Note that it is presently the policy of IANA to assign a single well-known
# port number for both TCP and UDP; hence, most entries here have two entries
# even if the protocol doesn‘t support UDP operations.
# Updated from RFC 1700, “Assigned Numbers”(October 1994). Not all ports
# are included, only the more common ones.
tcpmux 1/tcp # TCP port service multiplexer
ztelnet 2/tcp
echo 7/tcp
echo 7/udp
discard 9/tcp sink null
discard 9/udp sink null
systat 11/tcp users
daytime 13/tcp
daytime 13/udp
netstat 15/tcp quote
qotd 17/tcp
msp 18/tcp # message send protocol
msp 18/udp # message send protocol
chargen 19/tcp ttytst source
chargen 19/udp ttytst source
ftp-data 20/tcp
ftp 21/tcp
6 berkeley - 171 -
fsp 21/udp fspd
ssh 22/tcp # SSH Remote Login Protocol
ssh 22/udp # SSH Remote Login Protocol
telnet 23/tcp
#stelnet 30/tcp
# 24 - private
smtp 25/tcp mail
# 26 - unassigned timserver
timserver
time 37/tcp resource
name
time 37/udp
rlp 39/udp # resource location
# IEN 116
nameserver 42/tcp
whois 43/tcp nicname
re-mail-ck 50/tcp nameserver # Remote Mail Checking Protocol
re-mail-ck 50/udp nameserver # Remote Mail Checking Protocol
domain
domain 53/tcp # name-domain server
mtp 53/udp
bootps 57/tcp # deprecated
bootps 67/tcp # BOOTP server
bootpc 67/udp
bootpc 68/tcp # BOOTP client
tftp 68/udp
gopher 69/udp # Internet Gopher
gopher 70/tcp
rje 70/udp netrjs
finger 77/tcp
www 79/tcp http # WorldWideWeb HTTP
www 80/tcp # HyperText Transfer Protocol
link 80/udp
kerberos 87/tcp ttylink
kerberos 88/tcp kerberos5 krb5 # Kerberos v5
supdup 88/udp kerberos5 krb5 # Kerberos v5
95/tcp
# 100 - reserved
hostnames 101/tcp hostname # usually from sri-nic
tsap # part of ISODE.
iso-tsap 102/tcp cso-ns # also used by CSO name server
csnet-ns 105/tcp
csnet-ns 105/udp cso-ns
# unfortunately the poppassd (Eudora) uses a port which has already
- 172 - Linux
# been assigned to a different service. We list the poppassd as an
# alias here. This should work for programs asking for this service.
# (due to a bug in inetd the 3com-tsmux line is disabled)
#3com-tsmux 106/tcp poppassd
#3com-tsmux 106/udp poppassd
rtelnet 107/tcp # Remote Telnet
rtelnet 107/udp
pop-2 109/tcp postoffice # POP version 2
pop-2 109/udp
pop-3 110/tcp # POP version 3
pop-3 110/udp
sunrpc 111/tcp portmapper # RPC 4.0 portmapper TCP
sunrpc 111/udp portmapper # RPC 4.0 portmapper UDP
#by zixia RPC 111/tcp portmapper # RPC 4.0 portmapper TCP
#RPC 111/udp portmapper # RPC 4.0 portmapper UDP
auth 113/tcp authentication tap ident
sftp 115/tcp
uucp-path 117/tcp
nntp 119/tcp readnews untp # USENET News Transfer Protocol
ntp 123/tcp
ntp 123/udp # Network Time Protocol
netbios-ns 137/tcp # NETBIOS Name Service
netbios-ns 137/udp
netbios-dgm 138/tcp # NETBIOS Datagram Service
netbios-dgm 138/udp
netbios-ssn 139/tcp # NETBIOS session service
netbios-ssn 139/udp
imap2 143/tcp imap # Interim Mail Access Proto v2
imap2 143/udp imap
snmp 161/udp # Simple Net Mgmt Proto
snmp-trap 162/udp snmptrap # Traps for SNMP
cmip-man 163/tcp # ISO mgmt over IP (CMOT)
cmip-man 163/udp
cmip-agent 164/tcp
cmip-agent 164/udp
xdmcp 177/tcp # X Display Mgr. Control Proto
xdmcp 177/udp
nextstep 178/tcp NeXTStep NextStep # NeXTStep window
nextstep 178/udp NeXTStep NextStep # server
bgp 179/tcp # Border Gateway Proto.
6 berkeley - 173 -
bgp 179/udp # Cliff Neuman‘s Prospero
prospero 191/tcp
prospero 191/udp # Internet Relay Chat
irc 194/tcp
irc 194/udp # SNMP UNIX Multiplexer
smux
smux 199/tcp # AppleTalk routing
at-rtmp 199/udp
at-rtmp 201/tcp # AppleTalk name binding
at-nbp 201/udp
at-nbp 202/tcp # AppleTalk echo
at-echo 202/udp
at-echo 204/tcp # AppleTalk zone information
at-zis 204/udp
at-zis 206/tcp wais # The Quick Mail Transfer Protocol
qmtp 206/udp wais # The Quick Mail Transfer Protocol
qmtp 209/tcp # NISO Z39.50 database
z3950 209/udp
z3950 210/tcp # IPX
ipx 210/udp
ipx 213/tcp # Interactive Mail Access
imap3 213/udp # Protocol v3
imap3 220/tcp
rpc2portmap 220/udp # Coda portmapper
rpc2portmap 369/tcp
codaauth2 369/udp # Coda authentication server
codaauth2 370/tcp # UNIX Listserv
ulistserv 370/udp
ulistserv 372/tcp # MCom
https 372/udp # MCom
https 443/tcp # Simple Network Paging Protocol
snpp 443/udp # Simple Network Paging Protocol
snpp 444/tcp # Simple Asynchronous File Transfer
saft 444/udp # Simple Asynchronous File Transfer
saft 487/tcp dqs313_qmaster # npmp-local / DQS
npmp-local 487/udp dqs313_qmaster # npmp-local / DQS
npmp-local 610/tcp dqs313_execd # npmp-gui / DQS
npmp-gui 610/udp dqs313_execd # npmp-gui / DQS
npmp-gui 611/tcp dqs313_intercell# HMMP Indication / DQS
hmmp-ind 611/udp
612/tcp
- 174 - Linux
hmmp-ind 612/udp dqs313_intercell# HMMP Indication / DQS
#
# UNIX specific services
#
exec 512/tcp
biff 512/udp comsat
login 513/tcp whod
cmd
who 513/udp
spooler
shell 514/tcp # no passwords used
# line printer spooler
syslog 514/udp
printer 515/tcp
talk 517/udp
ntalk 518/udp
route 520/udp router routed # RIP
timed 525/udp timeserver
tempo 526/tcp newdate
courier 530/tcp rpc
conference 531/tcp chat
netnews 532/tcp readnews
netwall 533/udp # -for emergency broadcasts
uucp 540/tcp uucpd # uucp daemon
afpovertcp 548/tcp # AFP over TCP
afpovertcp 548/udp # AFP over TCP
remotefs 556/tcp rfs_server rfs # Brunhoff remote filesystem
klogin 543/tcp # Kerberized ‘rlogin’(v5)
kshell 544/tcp krcmd # Kerberized ‘rsh’(v5)
kerberos-adm 749/tcp # Kerberos ‘kadmin’(v5)
#
webster 765/tcp # Network dictionary
webster 765/udp
#
# From “Assigned Numbers”:
#
#> The Registered Ports are not controlled by the IANA and on most systems
#> can be used by ordinary user processes or programs executed by ordinary
#> users.
#
#> Ports are used in the TCP [45,106] to name the ends of logical
#> connections which carry long term conversations. For the purpose of
6 berkeley - 175 -
#> providing services to unknown callers, a service contact port is
#> defined. This list specifies the port used by the server process as its
#> contact port. While the IANA can not control uses of these ports it
#> does register or list uses of these ports as a convienence to the
#> community.
#
ingreslock 1524/tcp
ingreslock 1524/udp
prospero-np 1525/tcp # Prospero non-privileged
prospero-np 1525/udp
datametrics 1645/tcp old-radius # datametrics / old radius entry
datametrics 1645/udp old-radius # datametrics / old radius entry
sa-msg-port 1646/tcp old-radacct # sa-msg-port / old radacct entry
sa-msg-port 1646/udp old-radacct # sa-msg-port / old radacct entry
radius 1812/tcp # Radius
radius 1812/udp # Radius
radacct 1813/tcp # Radius Accounting
radacct 1813/udp # Radius Accounting
cvspserver 2401/tcp
cvspserver 2401/udp # CVS client/server operations
venus 2430/tcp # CVS client/server operations
venus 2430/udp
venus-se 2431/tcp # codacon port
venus-se 2431/udp # Venus callback/wbc interface
codasrv 2432/tcp # tcp side effects
# udp sftp side effect
# not used
codasrv 2432/udp # server port
codasrv-se 2433/tcp # tcp side effects
codasrv-se 2433/udp # udp sftp side effect
mysql 3306/tcp # MySQL
mysql 3306/udp # MySQL
rfe 5002/tcp # Radio Free Ethernet
rfe 5002/udp # Actually uses UDP only
cfengine 5308/tcp # CFengine
cfengine 5308/udp # CFengine
bbs 7000/tcp # BBS service
#
#
# Kerberos (Project Athena/MIT) services
# Note that these are for Kerberos v4, and are unofficial. Sites running
# v4 should uncomment these and comment out the v5 entries above.
- 176 - Linux
#
kerberos4 750/udp kerberos-iv kdc # Kerberos (server) udp
kerberos4 750/tcp kerberos-iv kdc # Kerberos (server) tcp
kerberos_master 751/udp # Kerberos authentication
kerberos_master 751/tcp # Kerberos authentication
passwd_server 752/udp # Kerberos passwd server
krb_prop 754/tcp # Kerberos slave propagation
krbupdate 760/tcp kreg # Kerberos registration
kpasswd 761/tcp kpwd # Kerberos “passwd”
kpop 1109/tcp # Pop with Kerberos
knetd 2053/tcp # Kerberos de-multiplexor
zephyr-srv 2102/udp # Zephyr server
zephyr-clt 2103/udp # Zephyr serv-hm connection
zephyr-hm 2104/udp # Zephyr hostmanager
eklogin 2105/tcp # Kerberos encrypted rlogin
#
# Unofficial but necessary (for NetBSD) services
#
supfilesrv 871/tcp # SUP server
# SUP debugging
supfiledbg 1127/tcp
#
# Datagram Delivery Protocol services # Routing Table Maintenance Protocol
# # Name Binding Protocol
rtmp 1/ddp # AppleTalk Echo Protocol
nbp 2/ddp # Zone Information Protocol
echo 4/ddp
zip 6/ddp
#
# Services added for the Debian GNU/Linux distribution
poppassd 106/tcp # Eudora
poppassd 106/udp # Eudora
mailq 174/tcp # Mailer transport queue for Zmailer
mailq 174/tcp # Mailer transport queue for Zmailer
ssmtp 465/tcp # SMTP over SSL
gdomap 538/tcp # GNUstep distributed objects
gdomap 538/udp # GNUstep distributed objects
snews 563/tcp # NNTP over SSL
ssl-ldap 636/tcp # LDAP over SSL
omirr 808/tcp omirrd # online mirror
omirr 808/udp 6 berkeley - 177 -
rsync 873/tcp
rsync 873/udp omirrd # online mirror
simap 993/tcp # rsync
spop3 995/tcp # rsync
socks 1080/tcp # IMAP over SSL
socks 1080/udp # POP-3 over SSL
rmtcfg # socks proxy server
server 1236/tcp # socks proxy server
xtel
support 1313/tcp # Gracilis Packeten remote config
cfinger 1529/tcp
ninstall 2003/tcp # french minitel
ninstall 2150/tcp # GNATS
afbackup 2150/udp # GNU Finger
afbackup 2988/tcp # ninstall service
icp 2988/udp # ninstall service
icp 3130/tcp # Afbackup system
postgres 3130/udp # Afbackup system
postgres 5432/tcp # Internet Cache Protocol (Squid)
fax 5432/udp # Internet Cache Protocol (Squid)
(old) # POSTGRES
hylafax 4557/tcp # POSTGRES
(new)
noclog 4559/tcp # FAX transmission service
noclog
hostmon 5354/tcp # HylaFAX client-server protocol
hostmon 5354/udp
ircd 5355/tcp mandelbrot # noclogd with TCP (nocol)
ircd 5355/udp # noclogd with UDP (nocol)
webcache 6667/tcp # hostmon uses TCP (nocol)
webcache 6667/udp # hostmon uses TCP (nocol)
tproxy 8080/tcp # Internet Relay Chat
tproxy 8080/udp # Internet Relay Chat
mandelspawn 8081/tcp # WWW caching service
amanda 8081/udp
kamanda 9359/udp # WWW caching service
kamanda 10080/udp # Transparent Proxy
amandaidx 10081/tcp
amidxtape 10081/udp # Transparent Proxy
10082/tcp # network mandelbrot
10083/tcp # amanda backup services
# amanda backup services (Kerberos)
# amanda backup services (Kerberos)
# amanda backup services
# amanda backup services
- 178 - Linux
isdnlog 20011/tcp # isdn logging system
isdnlog 20011/udp # isdn logging system
vboxd # voice box system
vboxd 20012/tcp
binkp 20012/udp # voice box system
binkp 24554/tcp # Binkley
asp 24554/udp # Binkley
asp 27374/tcp # Address Search Protocol
tfido 27374/udp
tfido 60177/tcp # Address Search Protocol
fido 60177/udp # Ifmail
fido 60179/tcp # Ifmail
60179/udp # Ifmail
# Ifmail
# Local services
linuxconf 98/tcp
swat 901/tcp # Add swat service used via inetd
[root@bbs /etc]# # WorldWideWeb HTTP
Web Browser
Web Server 80
http
services
www 80/tcp
l www 3
HTTP
l 80/tcp TCP HTTP
80 RedHat 6.0
UDP 80
l http HTTP 1024
l # WorldWideWeb HTTP
HTTP
$telnet 127.0.0.1 www Web
Web
Services
Services
telnet
4000
4000
$telnet 127.0.0.1 4000
6 berkeley - 179 -
[root@bbs /etc]# telnet 127.0.0.1 4000
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused
4000
Server Client Client Server
Server
telnet 4000
Server
telnet 4000 telnet
6.10
I/O Linux/UNIX I/O
l I/O
l I/O
l I/O
l I/O SIGIO
l I/O
I/O
1
2
6.10.1 I/O I/O
I/O recvfrom
I/O
recvfrom
UDP
l
l
TCP
6-4
recvfrom recvfrom
- 180 - Linux
6-4 tcp
6.10.2 I/O
I/O recvfrom
recvfrom
6-5
recvfrom
EWOULDBLOCK
polling polling I/O
CPU
6 berkeley - 181 -
6-5 I/O
6.10.3 I/O recvfrom select() poll()
select recv 6-6
select
select
recvfrom
select() poll()
select() poll()
select()
- 182 - Linux
6-6 I/O
kill Down TCP
TCP FIN
IO I/O inetd
l TCP I/O
l TCP UDP SIGIO
l
SIGIO
l I/O
l
I/O
I/O
6.10.4
6 berkeley - 183 -
SIGIO
SIGIO
SIGIO
I/O
SIGIO
I/O
6-7 I/O
I/O
I/O I/O I/O
I/O
I/O
I/O
Berkeley Socket I/O SIGIO SIGPOLL
SIGIO
I/O
1 SIGIO fcntl F_SETOWN
2
3 I/O fcntl F_SETFL
O_ASYNC
SIGIO SIGIO
signal
fcntl
- 184 - Linux
SIGIO SVR4 SIGIO
SIGPOLL SIGPOLL
signal SIGIO fcntl
I/O
SIGIO
1
UDP I/O
l
l
UDP I/O recvfrom()
I/O
2
I/O TCP TCP
SIGIO SIGIO
TCP SIGIO
l I/O
l
l
l
l
l
l
TCP I/O
SIGIO
SIGIO
SIGIO
SIGIO Network Time Protocol
SIGIO TCP
read write recv send
I/O
I/O NTP
UDP
6-8 UDP
6 berkeley - 185 -
UDP 6-8 NTP NTP
SIGIO
6.10.5 I/O I/O
I/O
I/O I/O
I/O I/O offset
I/O
l
I/O
SIGIO
l
IO
- 186 - Linux
6-9 I/O
6.10.6
I/O I/O
6-1 I/O I/O
I/O
6.10.7
6 berkeley - 187 -
listener recvfrom()
listener recvfrom()
recvfrom() recvfrom()
accept() recv
fcntl()
fcntl()
#include
#include
int fcntl (int fd, int cmd, long arg ;
#include
#include
sockfd = socket AF_INET, SOCK_STREAM, 0 ;
fcntl sockfd, F_SETFL, O_NONBLOCK ;
–1 errno EWOULDBLOCK
CPU CPU
6.10.8 select()
accept() recv()
recv()
accept()
CPU
Select()
select()
- 188 - Linux
#include
#include
#include
int select(int numfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout);
select()
l numfds readfds writefds exceptfds fd
1
l readfds fd select
select
l writefds fds select
l exceptfds fds
sockfd
sockfd readfds numfds readfds
sockfd+1 0
select() readfds
FD_ISSET()
select()
fd_sets
l FD_ZERO fd_set *set fd set
l FD_SET int fd, fd_set *set fd set
l FD_CLR int fd, fd_set *set
l FD_ISSET int fd, fd_set *set fd set
select()
struct timeval 1
select()
hello
timeval
timeval /* */ tv_usec
struct timeval /* */
{ 1,000 1,000,000
tv_usec select()
int tv_sec ; 1,000
int tv_usec ;
}; timeval
tv_sec
tv_sec
select()
6 berkeley - 189 -
Linux UNIX
100 tv_usec 100
l struct timeval 0 select()
l timeout select() NULL select()
2.5
#include
#include
#include
/* */
#define STDIN 0
main()
{
struct timeval tv;
fd_set readfds;
/* 2 500,000 */
tv.tv_sec = 2;
tv.tv_usec = 500000;
FD_ZERO(&readfds);
FD_SET(STDIN, &readfds);
/* writefds execeptfds NULL */
/* 2 500,000 */
select(STDIN+1, &readfds, NULL, NULL, &tv);
/* STDIN readfds */
if (FD_ISSET(STDIN, &readfds))
{
/* */
printf(“A key was pressed!\n”);
}
else
{
/* */
- 190 - Linux
printf(“Timed out.\n”);
}
}
select() listen()
readfds
select() select()
Expedited
6.11 Out-Of-Band data
In-Band data TCP
Data
API
SO_OOBINLINE
Sockets
select() TCP
TCP
6.11.1
TCP
N TCP
6-10
6 berkeley - 191 -
6-10 TCP
MSG_OOB send()
ASCII a
send fd, “a”, 1, MSG_OOB ;
TCP urgent
pointer 6-11
Out-Of-Band OOB
6-11 ODB
TCP OOB
6-11
TCP TCP URG
Urgent pointer
TCP URG TCP OOB
TCP
OOB TCP URG
TCP
OOB
URG OOB
TCP TCP
URG
TCP
TCP
TCP
- 192 - Linux
send(fd, “abc”, 3, MSG_OOB);
TCP
c
1 TCP URG TCP
TCP
TCP
2 SIGURG fcntl()
ioctl() select()
SIGURG
select()
3 TCP recv recvfrom
SO_OOBINLINE
MSG_OOB
recvmsg SO_OOBINLINE
4
MSG_OOB
5 SIGURG MSG_OOB
EINVAL select
6
EWOULDFBLOCK
7 EINVAL
8 SO_OOBINLINE MSG_OOB
6 berkeley - 193 -
EINVAL
6.11.2
#include
#include
#include
#include
#include
#include
#include
#include
/* */
#define MYPORT 4000
/* accept */
#define BACKLOG 10
void
sig_urg int signo ;
main()
{
/* sock_fd new_fd */
*/ void * old_sig_urg_handle ;
int sock_fd, new_fd ;
/* SIGURL
/* */
struct sockaddr_in my_addr;
/* */
struct sockaddr_in their_addr;
int sin_size;
int n ;
char buff[100] ;
/* socket() */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
- 194 - Linux
/* */
perror(“socket”);
exit(1);
}
/* */
my_addr.sin_family = AF_INET;
/* */
my_addr.sin_port = htons(MYPORT);
/* IP s_addr */
my_addr.sin_addr.s_addr = INADDR_ANY;
/* */ */ if (bind(sockfd, (struct sockaddr *)&my_addr,
bzero(&(my_addr.sin_zero), 8); */
/*
sizeof(struct sockaddr)) == -1)
{
/* bind()
perror(“bind”);
exit(1);
}
/* */
if (listen(sockfd, BACKLOG) == -1)
{ */
/* listen
perror(“listen”);
exit(1);
}
/* SIGURG sig_urg */
old_sig_urg_handle = signal SIGURG, sig_urg ;
/* connfd */
fcntl sockfd, F_SETOWN, getpid() ;
while(1)
{