Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
FFS Gluon
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
firmware
FFS Gluon
Commits
abcc86bf
Commit
abcc86bf
authored
10 years ago
by
Nils Schneider
Browse files
Options
Downloads
Patches
Plain Diff
gluon-neighbour-info: global timeout
parent
0222f342
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
package/gluon-neighbour-info/src/gluon-neighbour-info.c
+49
-11
49 additions, 11 deletions
package/gluon-neighbour-info/src/gluon-neighbour-info.c
with
49 additions
and
11 deletions
package/gluon-neighbour-info/src/gluon-neighbour-info.c
+
49
−
11
View file @
abcc86bf
...
@@ -33,18 +33,52 @@
...
@@ -33,18 +33,52 @@
#include
<netinet/in.h>
#include
<netinet/in.h>
#include
<arpa/inet.h>
#include
<arpa/inet.h>
#include
<string.h>
#include
<string.h>
#include
<time.h>
void
usage
()
{
void
usage
()
{
puts
(
"Usage: gluon-neighbour-info [-h] [-s] -d <dest> -p <port> -i <if0> -r <request>"
);
puts
(
"Usage: gluon-neighbour-info [-h] [-s]
[-t <sec>]
-d <dest> -p <port> -i <if0> -r <request>"
);
puts
(
" -p <int> UDP port"
);
puts
(
" -p <int> UDP port"
);
puts
(
" -d <ip6> multicast group, e.g. ff02:0:0:0:0:0:2:1001"
);
puts
(
" -d <ip6> multicast group, e.g. ff02:0:0:0:0:0:2:1001"
);
puts
(
" -i <string> interface, e.g. eth0 "
);
puts
(
" -i <string> interface, e.g. eth0 "
);
puts
(
" -r <string> request, e.g. nodeinfo"
);
puts
(
" -r <string> request, e.g. nodeinfo"
);
puts
(
" -t <sec> timeout in seconds (default: 3)"
);
puts
(
" -s output as server-sent events"
);
puts
(
" -s output as server-sent events"
);
puts
(
" -h this help
\n
"
);
puts
(
" -h this help
\n
"
);
}
}
int
request
(
const
int
sock
,
const
struct
sockaddr_in6
*
client_addr
,
const
char
*
request
,
bool
sse
)
{
void
getclock
(
struct
timeval
*
tv
)
{
struct
timespec
ts
;
clock_gettime
(
CLOCK_MONOTONIC
,
&
ts
);
tv
->
tv_sec
=
ts
.
tv_sec
;
tv
->
tv_usec
=
ts
.
tv_nsec
/
1000
;
}
/* Assumes a and b are normalized */
void
tv_subtract
(
struct
timeval
*
r
,
struct
timeval
*
a
,
struct
timeval
*
b
)
{
r
->
tv_usec
=
a
->
tv_usec
-
b
->
tv_usec
;
r
->
tv_sec
=
a
->
tv_sec
-
b
->
tv_sec
;
if
(
r
->
tv_usec
<
0
)
{
r
->
tv_usec
+=
1000000
;
r
->
tv_sec
-=
1
;
}
}
ssize_t
recvtimeout
(
int
socket
,
void
*
buffer
,
size_t
length
,
int
flags
,
struct
timeval
*
timeout
,
struct
timeval
*
offset
)
{
struct
timeval
now
,
delta
;
ssize_t
ret
;
setsockopt
(
socket
,
SOL_SOCKET
,
SO_RCVTIMEO
,
timeout
,
sizeof
(
*
timeout
));
ret
=
recv
(
socket
,
buffer
,
length
,
flags
);
getclock
(
&
now
);
tv_subtract
(
&
delta
,
&
now
,
offset
);
tv_subtract
(
timeout
,
timeout
,
&
delta
);
return
ret
;
}
int
request
(
const
int
sock
,
const
struct
sockaddr_in6
*
client_addr
,
const
char
*
request
,
bool
sse
,
int
timeout
)
{
ssize_t
ret
;
ssize_t
ret
;
char
buffer
[
8192
];
char
buffer
[
8192
];
...
@@ -55,15 +89,15 @@ int request(const int sock, const struct sockaddr_in6 *client_addr, const char *
...
@@ -55,15 +89,15 @@ int request(const int sock, const struct sockaddr_in6 *client_addr, const char *
exit
(
EXIT_FAILURE
);
exit
(
EXIT_FAILURE
);
}
}
struct
timeval
tv
;
struct
timeval
tv_timeout
,
tv_offset
;
tv
.
tv_sec
=
2
;
tv_timeout
.
tv_sec
=
timeout
;
tv
.
tv_usec
=
0
;
tv_timeout
.
tv_usec
=
0
;
if
(
setsockopt
(
sock
,
SOL_SOCKET
,
SO_RCVTIMEO
,
&
tv
,
sizeof
(
tv
))
<
0
)
{
perror
(
"Error"
);
getclock
(
&
tv_offset
);
}
while
(
1
)
{
while
(
1
)
{
ret
=
recv
(
sock
,
buffer
,
sizeof
(
buffer
),
0
);
ret
=
recvtimeout
(
sock
,
buffer
,
sizeof
(
buffer
),
0
,
&
tv_timeout
,
&
tv_offset
);
if
(
ret
<
0
)
if
(
ret
<
0
)
break
;
break
;
...
@@ -103,10 +137,11 @@ int main(int argc, char **argv) {
...
@@ -103,10 +137,11 @@ int main(int argc, char **argv) {
int
port_set
=
0
;
int
port_set
=
0
;
int
destination_set
=
0
;
int
destination_set
=
0
;
int
timeout
=
3
;
bool
sse
=
false
;
bool
sse
=
false
;
int
c
;
int
c
;
while
((
c
=
getopt
(
argc
,
argv
,
"p:d:r:i:sh"
))
!=
-
1
)
while
((
c
=
getopt
(
argc
,
argv
,
"p:d:r:i:
t:
sh"
))
!=
-
1
)
switch
(
c
)
{
switch
(
c
)
{
case
'p'
:
case
'p'
:
client_addr
.
sin6_port
=
htons
(
atoi
(
optarg
));
client_addr
.
sin6_port
=
htons
(
atoi
(
optarg
));
...
@@ -127,6 +162,9 @@ int main(int argc, char **argv) {
...
@@ -127,6 +162,9 @@ int main(int argc, char **argv) {
case
'r'
:
case
'r'
:
request_string
=
optarg
;
request_string
=
optarg
;
break
;
break
;
case
't'
:
timeout
=
atoi
(
optarg
);
break
;
case
's'
:
case
's'
:
sse
=
true
;
sse
=
true
;
break
;
break
;
...
@@ -144,7 +182,7 @@ int main(int argc, char **argv) {
...
@@ -144,7 +182,7 @@ int main(int argc, char **argv) {
if
(
sse
)
if
(
sse
)
fputs
(
"Content-Type: text/event-stream
\n\n
"
,
stdout
);
fputs
(
"Content-Type: text/event-stream
\n\n
"
,
stdout
);
request
(
sock
,
&
client_addr
,
request_string
,
sse
);
request
(
sock
,
&
client_addr
,
request_string
,
sse
,
timeout
);
if
(
sse
)
if
(
sse
)
fputs
(
"event: eot
\n
data: null
\n\n
"
,
stdout
);
fputs
(
"event: eot
\n
data: null
\n\n
"
,
stdout
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment