Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Rtty
Commits
e8276d5a
Commit
e8276d5a
authored
7 years ago
by
Jianhui Zhao
Browse files
Options
Download
Email Patches
Plain Diff
Optimize code
Signed-off-by:
Jianhui Zhao
<
jianhuizhao329@gmail.com
>
parent
c7a96f36
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
.gitignore
+2
-0
.gitignore
src/main.c
+22
-13
src/main.c
src/utils.c
+4
-2
src/utils.c
with
28 additions
and
15 deletions
+28
-15
.gitignore
+
2
-
0
View file @
e8276d5a
...
...
@@ -50,3 +50,5 @@ modules.order
Module.symvers
Mkfile.old
dkms.conf
build
This diff is collapsed.
Click to expand it.
src/main.c
+
22
-
13
View file @
e8276d5a
...
...
@@ -21,6 +21,7 @@
#include
<sys/wait.h>
#include
<pty.h>
#include
<uwsc/uwsc.h>
#include
<libubox/ulog.h>
#include
<libubox/blobmsg_json.h>
#include
"utils.h"
...
...
@@ -89,11 +90,11 @@ static void keepalive(struct uloop_timeout *utm)
free
(
str
);
if
(
!
active
--
)
{
gcl
->
free
(
gcl
);
gcl
=
NULL
;
ULOG_ERR
(
"keepalive timeout
\n
"
);
if
(
auto_reconnect
)
do_connect
();
else
uloop_end
();
return
;
}
...
...
@@ -182,7 +183,7 @@ static void uwsc_onopen(struct uwsc_client *cl)
active
=
3
;
keepalive_timer
.
cb
=
keepalive
;
uloop_timeout_set
(
&
keepalive_timer
,
KEEPALIVE_INTERVAL
*
1000
);
uwsc_log_debug
(
"onopen"
);
ULOG_INFO
(
"onopen
\n
"
);
}
static
void
uwsc_onmessage
(
struct
uwsc_client
*
cl
,
char
*
msg
,
uint64_t
len
,
enum
websocket_op
op
)
...
...
@@ -195,7 +196,7 @@ static void uwsc_onmessage(struct uwsc_client *cl, char *msg, uint64_t len, enum
blobmsg_add_json_from_string
(
&
b
,
msg
);
if
(
blobmsg_parse
(
pol
,
ARRAY_SIZE
(
pol
),
tb
,
blob_data
(
b
.
head
),
blob_len
(
b
.
head
))
!=
0
)
{
uwsc_log_err
(
"Parse failed"
);
ULOG_ERR
(
"Parse failed
\n
"
);
return
;
}
...
...
@@ -242,7 +243,7 @@ static void uwsc_onmessage(struct uwsc_client *cl, char *msg, uint64_t len, enum
active
=
3
;
}
else
if
(
!
strcmp
(
type
,
"add"
))
{
if
(
tb
[
RTTYD_ERR
])
{
uwsc_log_err
(
"add failed: %s"
,
blobmsg_get_string
(
tb
[
RTTYD_ERR
]));
ULOG_ERR
(
"add failed: %s
\n
"
,
blobmsg_get_string
(
tb
[
RTTYD_ERR
]));
uloop_end
();
}
}
...
...
@@ -250,13 +251,13 @@ static void uwsc_onmessage(struct uwsc_client *cl, char *msg, uint64_t len, enum
static
void
uwsc_onerror
(
struct
uwsc_client
*
cl
)
{
uwsc_log_err
(
"onerror:%d"
,
cl
->
error
);
ULOG_ERR
(
"onerror:%d
\n
"
,
cl
->
error
);
}
static
void
uwsc_onclose
(
struct
uwsc_client
*
cl
)
{
active
=
0
;
uwsc_log_debug
(
"onclose"
);
ULOG_ERR
(
"onclose
\n
"
);
if
(
auto_reconnect
)
{
cl
->
free
(
cl
);
...
...
@@ -314,6 +315,7 @@ static void usage(const char *prog)
" -h host # Server host
\n
"
" -p port # Server port
\n
"
" -a # Auto reconnect to the server
\n
"
" -v # verbose
\n
"
,
prog
);
exit
(
1
);
}
...
...
@@ -324,18 +326,19 @@ int main(int argc, char **argv)
char
mac
[
13
]
=
""
;
const
char
*
host
=
NULL
;
int
port
=
0
;
bool
verbose
=
false
;
if
(
setuid
(
0
)
<
0
)
{
fprintf
(
stderr
,
"Operation not permitted
\n
"
);
ULOG_ERR
(
"Operation not permitted
\n
"
);
return
-
1
;
}
if
(
find_login
()
<
0
)
{
fprintf
(
stderr
,
"The program 'login' is not found
\n
"
);
ULOG_ERR
(
"The program 'login' is not found
\n
"
);
return
-
1
;
}
while
((
opt
=
getopt
(
argc
,
argv
,
"i:h:p:I:a"
))
!=
-
1
)
{
while
((
opt
=
getopt
(
argc
,
argv
,
"i:h:p:I:a
v
"
))
!=
-
1
)
{
switch
(
opt
)
{
case
'i'
:
...
...
@@ -355,21 +358,27 @@ int main(int argc, char **argv)
case
'a'
:
auto_reconnect
=
true
;
break
;
case
'v'
:
verbose
=
true
;
break
;
default:
/* '?' */
usage
(
argv
[
0
]);
}
}
if
(
!
verbose
)
ulog_threshold
(
LOG_ERR
);
if
(
!
did
[
0
])
{
if
(
!
mac
[
0
])
{
fprintf
(
stderr
,
"You must specify the ifname or id
\n
"
);
ULOG_ERR
(
"You must specify the ifname or id
\n
"
);
usage
(
argv
[
0
]);
}
strcpy
(
did
,
mac
);
}
if
(
!
host
||
!
port
)
{
fprintf
(
stderr
,
"You must specify the host and port
\n
"
);
ULOG_ERR
(
"You must specify the host and port
\n
"
);
usage
(
argv
[
0
]);
}
...
...
This diff is collapsed.
Click to expand it.
src/utils.c
+
4
-
2
View file @
e8276d5a
...
...
@@ -20,10 +20,12 @@
#include
<unistd.h>
#include
<stdint.h>
#include
<string.h>
#include
<errno.h>
#include
<sys/ioctl.h>
#include
<net/if.h>
#include
<net/if_arp.h>
#include
<uwsc/uwsc.h>
#include
<libubox/ulog.h>
int
get_iface_mac
(
const
char
*
ifname
,
char
*
dst
,
int
len
)
{
...
...
@@ -32,7 +34,7 @@ int get_iface_mac(const char *ifname, char *dst, int len)
uint8_t
*
hw
;
if
((
sock
=
socket
(
AF_INET
,
SOCK_DGRAM
,
0
))
<
0
)
{
uwsc_log_err
(
"socket"
);
ULOG_ERR
(
"create socket failed:%s
\n
"
,
strerror
(
errno
)
);
return
-
1
;
}
...
...
@@ -42,7 +44,7 @@ int get_iface_mac(const char *ifname, char *dst, int len)
strncpy
(
ifr
.
ifr_name
,
ifname
,
sizeof
(
ifr
.
ifr_name
));
if
(
ioctl
(
sock
,
SIOCGIFHWADDR
,
&
ifr
)
<
0
)
{
uwsc_log_err
(
"ioctl"
);
ULOG_ERR
(
"ioctl failed:%s
\n
"
,
strerror
(
errno
)
);
close
(
sock
);
return
-
1
;
}
...
...
This diff is collapsed.
Click to expand it.
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
Menu
Projects
Groups
Snippets
Help