Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
emulab
emulab-devel
Commits
d897729c
Commit
d897729c
authored
Jan 19, 2006
by
Junxing Zhang
Browse files
remove two old stubsnd.c stubrcv.c
parent
fd2d58f3
Changes
2
Hide whitespace changes
Inline
Side-by-side
pelab/stub/stubrcv.c
deleted
100644 → 0
View file @
fd2d58f3
/*
** stubrcv.c
*/
#include "stub.h"
//Global
void
print_header
(
char
*
buf
){
int
i
,
j
,
len
=
14
;
printf
(
"Buffer header len: %d
\n
"
,
len
);
for
(
i
=
0
;
i
<
len
;
i
++
){
j
=
buf
[
i
]
&
0x000000ff
;
printf
(
"%d: %u
\n
"
,
i
,
buf
[
i
]);
}
}
void
receive_packets
(
int
sockfd_snd
)
{
unsigned
long
delay
;
//delay in usec
// unsigned long loss; //loss*1000000
// unsigned long abw; //abw in kbps
char
inbuf
[
MAX_PKTSIZE
],
outbuf
[
3
*
SIZEOF_LONG
];
int
numbytes
,
longsz
,
nleft
;
unsigned
long
tmpulong
,
sndsec
,
sndusec
,
pkttype
;
struct
timeval
application_sendtime
;
struct
hostent
*
my_hostent
;
char
hostname
[
128
];
char
*
next
;
longsz
=
sizeof
(
long
);
//blocking until the sender closes the socket
while
((
numbytes
=
recv
(
sockfd_snd
,
inbuf
,
longsz
,
0
))
!=
0
)
{
//read packet type first
if
(
numbytes
==
-
1
)
{
perror
(
"recv"
);
exit
(
1
);
}
//get the rcv timestamp in case it is traffic
gettimeofday
(
&
application_sendtime
,
NULL
);
//check the packet type
memcpy
(
&
tmpulong
,
inbuf
,
longsz
);
pkttype
=
ntohl
(
tmpulong
);
if
(
pkttype
==
CODE_TRAFFIC
)
{
//read the rest of the traffic packet
next
=
inbuf
;
nleft
=
MAX_PKTSIZE
-
longsz
;
while
(
nleft
>
0
)
{
if
((
numbytes
=
recv
(
sockfd_snd
,
next
,
nleft
,
0
))
==
-
1
)
{
perror
(
"recv"
);
exit
(
1
);
}
nleft
-=
numbytes
;
next
+=
numbytes
;
}
//get the traffic send time
memcpy
(
&
tmpulong
,
inbuf
,
longsz
);
sndsec
=
ntohl
(
tmpulong
);
memcpy
(
&
tmpulong
,
inbuf
+
longsz
,
longsz
);
sndusec
=
ntohl
(
tmpulong
);
delay
=
(
application_sendtime
.
tv_sec
-
sndsec
)
*
1000000
+
(
application_sendtime
.
tv_usec
-
sndusec
);
if
(
getenv
(
"Debug"
)
!=
NULL
)
printf
(
"One Way Delay (usec): %ld
\n
"
,
delay
);
}
else
{
//inquiry
gethostname
(
hostname
,
sizeof
(
hostname
));
if
((
my_hostent
=
gethostbyname
(
hostname
))
==
NULL
)
{
// get the host info
herror
(
"gethostbyname"
);
exit
(
1
);
}
memcpy
(
outbuf
,
my_hostent
->
h_addr
,
SIZEOF_LONG
);
tmpulong
=
htonl
(
CODE_DELAY
);
memcpy
(
outbuf
+
SIZEOF_LONG
,
&
tmpulong
,
SIZEOF_LONG
);
tmpulong
=
htonl
(
delay
);
memcpy
(
outbuf
+
SIZEOF_LONG
+
SIZEOF_LONG
,
&
tmpulong
,
SIZEOF_LONG
);
if
(
send
(
sockfd_snd
,
outbuf
,
3
*
SIZEOF_LONG
,
0
)
==
-
1
){
perror
(
"ERROR: send_packets() - send()"
);
exit
(
1
);
}
//loss and bw come here
}
//if
}
//while
}
void
sigchld_handler
(
int
s
)
{
while
(
waitpid
(
-
1
,
NULL
,
WNOHANG
)
>
0
);
}
int
main
(
void
)
{
int
sockfd_snd
,
sockfd_rcv
;
struct
sockaddr_in
my_addr
;
// my address information
struct
sockaddr_in
their_addr
;
// connector's address information
socklen_t
sin_size
;
struct
sigaction
sa
;
int
yes
=
1
;
if
((
sockfd_rcv
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
))
==
-
1
)
{
perror
(
"socket"
);
exit
(
1
);
}
if
(
setsockopt
(
sockfd_rcv
,
SOL_SOCKET
,
SO_REUSEADDR
,
&
yes
,
sizeof
(
int
))
==
-
1
)
{
perror
(
"setsockopt"
);
exit
(
1
);
}
my_addr
.
sin_family
=
AF_INET
;
// host byte order
my_addr
.
sin_port
=
htons
(
TRAFFIC_PORT
);
// short, network byte order
my_addr
.
sin_addr
.
s_addr
=
INADDR_ANY
;
// automatically fill with my IP
memset
(
&
(
my_addr
.
sin_zero
),
'\0'
,
8
);
// zero the rest of the struct
if
(
getenv
(
"Debug"
)
!=
NULL
)
printf
(
"Listen on %s
\n
"
,
inet_ntoa
(
my_addr
.
sin_addr
));
if
(
bind
(
sockfd_rcv
,
(
struct
sockaddr
*
)
&
my_addr
,
sizeof
(
struct
sockaddr
))
==
-
1
)
{
perror
(
"bind"
);
exit
(
1
);
}
if
(
listen
(
sockfd_rcv
,
CONCURRENT_RECEIVERS
)
==
-
1
)
{
perror
(
"listen"
);
exit
(
1
);
}
sa
.
sa_handler
=
sigchld_handler
;
// reap all dead processes
sigemptyset
(
&
sa
.
sa_mask
);
sa
.
sa_flags
=
SA_RESTART
;
if
(
sigaction
(
SIGCHLD
,
&
sa
,
NULL
)
==
-
1
)
{
perror
(
"sigaction"
);
exit
(
1
);
}
sin_size
=
sizeof
(
struct
sockaddr_in
);
while
(
1
)
{
// main accept() loop
if
((
sockfd_snd
=
accept
(
sockfd_rcv
,
(
struct
sockaddr
*
)
&
their_addr
,
&
sin_size
))
==
-
1
)
{
perror
(
"accept"
);
continue
;
}
if
(
getenv
(
"Debug"
)
!=
NULL
)
printf
(
"server: got connection from %s
\n
"
,
inet_ntoa
(
their_addr
.
sin_addr
));
if
(
!
fork
())
{
// this is the child process
close
(
sockfd_rcv
);
// child doesn't need the listener
receive_packets
(
sockfd_snd
);
close
(
sockfd_snd
);
exit
(
0
);
}
close
(
sockfd_snd
);
// parent doesn't need this
}
close
(
sockfd_rcv
);
return
0
;
}
pelab/stub/stubsnd.c
deleted
100644 → 0
View file @
fd2d58f3
/*
** stubsnd.c
*/
#include "stub.h"
connection
db
[
CONCURRENT_RECEIVERS
];
void
init_db
(
void
)
{
int
i
;
for
(
i
=
0
;
i
<
CONCURRENT_RECEIVERS
;
i
++
){
db
[
i
].
valid
=
0
;
}
}
void
insert_db
(
unsigned
long
ip
,
int
sockfd
)
{
int
i
,
next
=
-
1
;
time_t
now
=
time
(
NULL
);
double
thisdiff
,
maxdiff
=
0
;
//find an unused entry or LRU entry
for
(
i
=
0
;
i
<
CONCURRENT_RECEIVERS
;
i
++
){
if
(
db
[
i
].
valid
==
0
)
{
next
=
i
;
break
;
}
else
{
thisdiff
=
difftime
(
now
,
db
[
i
].
last_usetime
);
if
(
thisdiff
>
maxdiff
)
{
maxdiff
=
thisdiff
;
next
=
i
;
}
}
}
if
(
db
[
next
].
valid
==
1
)
{
close
(
db
[
next
].
sockfd
);
}
db
[
next
].
valid
=
1
;
db
[
next
].
ip
=
ip
;
db
[
next
].
sockfd
=
sockfd
;
db
[
next
].
last_usetime
=
now
;
}
int
search_db
(
unsigned
long
indexip
){
int
i
;
for
(
i
=
0
;
i
<
CONCURRENT_RECEIVERS
;
i
++
){
if
(
db
[
i
].
valid
==
1
&&
db
[
i
].
ip
==
indexip
)
{
db
[
i
].
last_usetime
=
time
(
NULL
);
return
db
[
i
].
sockfd
;
}
}
return
-
1
;
//no sockfd is -1
}
void
clean_exit
(
int
code
){
int
i
;
for
(
i
=
0
;
i
<
CONCURRENT_RECEIVERS
;
i
++
){
if
(
db
[
i
].
valid
==
1
){
close
(
db
[
i
].
sockfd
);
}
}
exit
(
code
);
}
/* -------------------------------------------------------------------
* returns the TCP window size (on the sending buffer, SO_SNDBUF),
* or -1 on error.
* ------------------------------------------------------------------- */
int
getsock_tcp_windowsize
(
int
inSock
)
{
int
rc
;
int
theTCPWin
=
0
;
socklen_t
len
;
int
mySock
=
inSock
;
#ifdef SO_SNDBUF
if
(
inSock
<
0
)
{
/* no socket given, return system default
* allocate our own new socket */
mySock
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
}
/* send buffer -- query for buffer size */
len
=
sizeof
(
theTCPWin
);
rc
=
getsockopt
(
mySock
,
SOL_SOCKET
,
SO_SNDBUF
,
(
char
*
)
&
theTCPWin
,
&
len
);
if
(
rc
<
0
)
{
return
rc
;
}
if
(
inSock
<
0
)
{
/* we allocated our own socket, so deallocate it */
close
(
mySock
);
}
#endif
return
theTCPWin
;
}
/* end getsock_tcp_windowsize */
void
print_header
(
char
*
buf
){
int
i
,
j
,
len
=
14
;
printf
(
"Buffer header len: %d
\n
"
,
len
);
for
(
i
=
0
;
i
<
len
;
i
++
){
j
=
buf
[
i
]
&
0x000000ff
;
printf
(
"%d: %u
\n
"
,
i
,
j
);
}
}
int
get_socket
(
unsigned
long
destaddr
){
int
sockfd_traffic
;
struct
sockaddr_in
their_addr
;
// connector's address information
if
((
sockfd_traffic
=
search_db
(
destaddr
))
==
-
1
)
{
if
((
sockfd_traffic
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
))
==
-
1
)
{
perror
(
"socket"
);
clean_exit
(
1
);
}
their_addr
.
sin_family
=
AF_INET
;
// host byte order
their_addr
.
sin_port
=
htons
(
TRAFFIC_PORT
);
// short, network byte order
their_addr
.
sin_addr
.
s_addr
=
destaddr
;
memset
(
&
(
their_addr
.
sin_zero
),
'\0'
,
8
);
// zero the rest of the struct
if
(
getenv
(
"Debug"
)
!=
NULL
)
printf
(
"Try to connect to %s
\n
"
,
inet_ntoa
(
their_addr
.
sin_addr
));
if
(
connect
(
sockfd_traffic
,
(
struct
sockaddr
*
)
&
their_addr
,
sizeof
(
struct
sockaddr
))
==
-
1
)
{
perror
(
"connect"
);
clean_exit
(
1
);
}
insert_db
(
destaddr
,
sockfd_traffic
);
}
return
sockfd_traffic
;
}
int
receive_message
(
int
sockfd_monitor
,
char
*
buf
)
{
int
numbytes
;
struct
timeval
tv
;
fd_set
readset
;
unsigned
long
timeout
;
timeout
=
QUANTA
/
2
;
//QUANTA should be even
tv
.
tv_sec
=
timeout
/
1000000
;
tv
.
tv_usec
=
timeout
%
1000000
;
FD_ZERO
(
&
readset
);
FD_SET
(
sockfd_monitor
,
&
readset
);
bzero
(
buf
,
MAX_PKTSIZE
);
//poll for feed-in
if
(
select
(
sockfd_monitor
+
1
,
&
readset
,
NULL
,
NULL
,
&
tv
)
>
0
)
{
if
((
numbytes
=
recv
(
sockfd_monitor
,
buf
,
MAX_PKTSIZE
,
0
))
==
-
1
)
{
return
-
1
;
//no data
}
if
(
numbytes
==
0
){
return
0
;
//socket closed
}
if
(
getenv
(
"Debug"
)
!=
NULL
)
{
print_header
(
buf
);
printf
(
"numbytes: %d
\n
"
,
numbytes
);
}
return
1
;
//received message
}
//if
return
-
1
;
//no data
}
void
send_message
(
int
sockfd_monitor
,
char
*
buf
)
{
char
inbuf
[
3
*
SIZEOF_LONG
],
outbuf
[
SIZEOF_LONG
];
struct
timeval
tv
;
fd_set
writeset
;
unsigned
long
timeout
,
tmpulong
;
int
i
;
timeout
=
QUANTA
/
2
;
//QUANTA should be even
tv
.
tv_sec
=
timeout
/
1000000
;
tv
.
tv_usec
=
timeout
%
1000000
;
FD_ZERO
(
&
writeset
);
FD_SET
(
sockfd_monitor
,
&
writeset
);
//poll for feed-back
tmpulong
=
CODE_INQUIRY
;
memcpy
(
outbuf
,
&
tmpulong
,
SIZEOF_LONG
);
for
(
i
=
0
;
i
<
CONCURRENT_RECEIVERS
;
i
++
){
if
(
db
[
i
].
valid
==
1
)
{
if
(
send
(
db
[
i
].
sockfd
,
outbuf
,
SIZEOF_LONG
,
0
)
==
-
1
){
perror
(
"ERROR: send_messages() - send() traffic"
);
clean_exit
(
1
);
}
//no incomplete receive check here
if
((
recv
(
db
[
i
].
sockfd
,
inbuf
,
3
*
SIZEOF_LONG
,
0
))
==
-
1
)
{
perror
(
"ERROR: send_messages() - recv()"
);
clean_exit
(
1
);
}
if
(
select
(
sockfd_monitor
+
1
,
NULL
,
&
writeset
,
NULL
,
&
tv
)
>
0
)
{
if
(
send
(
sockfd_monitor
,
inbuf
,
3
*
SIZEOF_LONG
,
0
)
==
-
1
){
perror
(
"ERROR: send_messages() - send() monitor"
);
clean_exit
(
1
);
}
}
//if
}
//if
}
//for
}
void
send_packets
(
int
sockfd_traffic
,
char
*
packet_buffer
){
int
i
;
struct
timeval
application_sendtime
;
unsigned
long
tmpulong
;
srandom
(
getpid
());
for
(
i
=
0
;
i
<
MAX_PKTSIZE
;
i
++
)
packet_buffer
[
i
]
=
(
char
)(
random
()
&
0x000000ff
);
//put the packet type, application send time at the first eight bytes
tmpulong
=
htonl
(
CODE_TRAFFIC
);
memcpy
(
packet_buffer
,
&
tmpulong
,
SIZEOF_LONG
);
gettimeofday
(
&
application_sendtime
,
NULL
);
tmpulong
=
htonl
(
application_sendtime
.
tv_sec
);
memcpy
(
packet_buffer
+
SIZEOF_LONG
,
&
tmpulong
,
SIZEOF_LONG
);
tmpulong
=
htonl
(
application_sendtime
.
tv_usec
);
memcpy
(
packet_buffer
+
SIZEOF_LONG
+
SIZEOF_LONG
,
&
tmpulong
,
SIZEOF_LONG
);
//add send loop here!
if
(
send
(
sockfd_traffic
,
packet_buffer
,
MAX_PKTSIZE
,
0
)
==
-
1
){
perror
(
"ERROR: send_packets() - send()"
);
clean_exit
(
1
);
}
}
void
sigchld_handler
(
int
s
)
{
while
(
waitpid
(
-
1
,
NULL
,
WNOHANG
)
>
0
);
}
int
main
(
int
argc
,
char
*
argv
[])
{
int
sockfd_control
,
sockfd_monitor
,
sockfd_traffic
;
struct
sigaction
sa
;
char
buf
[
MAX_PKTSIZE
];
struct
sockaddr_in
their_addr
;
// connector's address information
struct
sockaddr_in
my_addr
;
// my address information
socklen_t
sin_size
;
int
yes
=
1
,
i
,
longsz
,
rcvflag
;
unsigned
long
tmpulong
,
destaddr
,
destnum
;
char
*
nextptr
;
if
((
sockfd_control
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
))
==
-
1
)
{
perror
(
"socket"
);
exit
(
1
);
}
if
(
setsockopt
(
sockfd_control
,
SOL_SOCKET
,
SO_REUSEADDR
,
&
yes
,
sizeof
(
int
))
==
-
1
)
{
perror
(
"setsockopt"
);
exit
(
1
);
}
my_addr
.
sin_family
=
AF_INET
;
my_addr
.
sin_port
=
htons
(
CONTROL_PORT
);
my_addr
.
sin_addr
.
s_addr
=
INADDR_ANY
;
// automatically fill with my IP
memset
(
&
(
my_addr
.
sin_zero
),
'\0'
,
8
);
// zero the rest of the struct
if
(
bind
(
sockfd_control
,
(
struct
sockaddr
*
)
&
my_addr
,
sizeof
(
struct
sockaddr
))
==
-
1
)
{
perror
(
"bind"
);
exit
(
1
);
}
if
(
listen
(
sockfd_control
,
1
)
==
-
1
)
{
perror
(
"listen"
);
exit
(
1
);
}
sin_size
=
sizeof
(
struct
sockaddr_in
);
init_db
();
sa
.
sa_handler
=
sigchld_handler
;
// reap all dead processes
sigemptyset
(
&
sa
.
sa_mask
);
sa
.
sa_flags
=
SA_RESTART
;
if
(
sigaction
(
SIGCHLD
,
&
sa
,
NULL
)
==
-
1
)
{
perror
(
"sigaction"
);
exit
(
1
);
}
longsz
=
sizeof
(
long
);
while
(
1
)
{
// main accept() loop
if
((
sockfd_monitor
=
accept
(
sockfd_control
,
(
struct
sockaddr
*
)
&
their_addr
,
&
sin_size
))
==
-
1
)
{
perror
(
"accept"
);
continue
;
}
if
(
getenv
(
"Debug"
)
!=
NULL
)
printf
(
"sender: got connection from %s
\n
"
,
inet_ntoa
(
their_addr
.
sin_addr
));
//Make the monitor socket non-blocking
if
(
fcntl
(
sockfd_monitor
,
F_SETFL
,
O_NONBLOCK
)
<
0
){
perror
(
"fcntl(sockfd_monitor, F_SETFL, O_NONBLOCK):"
);
exit
(
-
1
);
}
while
(
1
)
{
rcvflag
=
receive_message
(
sockfd_monitor
,
buf
);
if
(
rcvflag
==
0
)
break
;
//socket closed by the peer
if
(
rcvflag
>
0
)
{
nextptr
=
buf
+
longsz
;
memcpy
(
&
tmpulong
,
nextptr
,
longsz
);
destnum
=
ntohl
(
tmpulong
);
nextptr
+=
longsz
;
for
(
i
=
0
;
i
<
destnum
;
i
++
){
memcpy
(
&
tmpulong
,
nextptr
,
longsz
);
destaddr
=
tmpulong
;
//address should stay in Network Order!
nextptr
+=
longsz
;
sockfd_traffic
=
get_socket
(
destaddr
);
if
(
!
fork
())
{
// this is the child process
close
(
sockfd_control
);
// child doesn't need the listener
close
(
sockfd_monitor
);
send_packets
(
sockfd_traffic
,
buf
);
exit
(
0
);
//child doesn't close traffic connections
}
}
//for
}
//if
send_message
(
sockfd_monitor
,
buf
);
}
//while receive
close
(
sockfd_monitor
);
}
close
(
sockfd_control
);
return
0
;
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment