Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gamera_videos_no_ui
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
liusq
gamera_videos_no_ui
Commits
742e07a4
Commit
742e07a4
authored
Oct 10, 2024
by
“liusq”
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改ping命令-05
parent
7e011b02
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
130 additions
and
44 deletions
+130
-44
Common.cpp
+100
-0
Common.h
+18
-0
NonConnectedCameraHandle.cpp
+12
-2
VidesData.h
+0
-42
No files found.
Common.cpp
View file @
742e07a4
...
@@ -200,5 +200,105 @@ bool Common::pingAddress(const QString &address) {
...
@@ -200,5 +200,105 @@ bool Common::pingAddress(const QString &address) {
}
}
return
false
;
return
false
;
}
}
void
Common
::
get_network_info
(
const
char
*
interface
,
QString
&
mac
,
QString
&
subnet_mask
,
QString
&
gateway
)
{
int
fd
;
struct
ifreq
ifr
;
struct
rtentry
route
;
unsigned
char
*
mac_addr
;
struct
sockaddr_in
*
addr
;
struct
ifaddrs
*
ifaddr
,
*
ifa
;
// Create a socket to perform ioctl requests
fd
=
socket
(
AF_INET
,
SOCK_DGRAM
,
0
);
if
(
fd
<
0
)
{
perror
(
"socket"
);
return
;
}
// Get the MAC address
memset
(
&
ifr
,
0
,
sizeof
(
ifr
));
strncpy
(
ifr
.
ifr_name
,
interface
,
IFNAMSIZ
-
1
);
if
(
ioctl
(
fd
,
SIOCGIFHWADDR
,
&
ifr
)
==
0
)
{
mac_addr
=
(
unsigned
char
*
)
ifr
.
ifr_hwaddr
.
sa_data
;
mac
=
QString
::
asprintf
(
"%02x:%02x:%02x:%02x:%02x:%02x"
,
mac_addr
[
0
],
mac_addr
[
1
],
mac_addr
[
2
],
mac_addr
[
3
],
mac_addr
[
4
],
mac_addr
[
5
]);
}
else
{
perror
(
"SIOCGIFHWADDR"
);
mac
=
"Unknown"
;
}
// Get the subnet mask
if
(
ioctl
(
fd
,
SIOCGIFNETMASK
,
&
ifr
)
==
0
)
{
addr
=
(
struct
sockaddr_in
*
)
&
ifr
.
ifr_netmask
;
subnet_mask
=
QString
::
fromUtf8
(
inet_ntoa
(
addr
->
sin_addr
));
}
else
{
perror
(
"SIOCGIFNETMASK"
);
subnet_mask
=
"Unknown"
;
}
// Get the default gateway
FILE
*
fp
=
fopen
(
"/proc/net/route"
,
"r"
);
if
(
fp
!=
NULL
)
{
char
buffer
[
256
];
while
(
fgets
(
buffer
,
sizeof
(
buffer
),
fp
))
{
char
iface
[
IFNAMSIZ
];
unsigned
long
dest
,
gateway_addr
;
if
(
sscanf
(
buffer
,
"%s %lx %lx"
,
iface
,
&
dest
,
&
gateway_addr
)
==
3
)
{
if
(
dest
==
0
)
{
// Default route
struct
in_addr
gwaddr
;
gwaddr
.
s_addr
=
gateway_addr
;
gateway
=
QString
::
fromUtf8
(
inet_ntoa
(
gwaddr
));
break
;
}
}
}
fclose
(
fp
);
}
else
{
perror
(
"fopen"
);
gateway
=
"Unknown"
;
}
// Clean up
close
(
fd
);
}
// 确定当前网络接口
void
Common
::
determine_interface
(
char
*
interface
)
{
struct
ifaddrs
*
ifaddr
,
*
ifa
;
int
family
,
s
;
char
host
[
NI_MAXHOST
];
if
(
getifaddrs
(
&
ifaddr
)
==
-
1
)
{
perror
(
"getifaddrs"
);
exit
(
EXIT_FAILURE
);
}
// Walk through linked list, maintaining head pointer so we can free list later
for
(
ifa
=
ifaddr
;
ifa
!=
NULL
;
ifa
=
ifa
->
ifa_next
)
{
if
(
ifa
->
ifa_addr
==
NULL
)
continue
;
family
=
ifa
->
ifa_addr
->
sa_family
;
// Check for IPv4 or IPv6
if
(
family
==
AF_INET
||
family
==
AF_INET6
)
{
s
=
getnameinfo
(
ifa
->
ifa_addr
,
(
family
==
AF_INET
)
?
sizeof
(
struct
sockaddr_in
)
:
sizeof
(
struct
sockaddr_in6
),
host
,
NI_MAXHOST
,
NULL
,
0
,
NI_NUMERICHOST
);
if
(
s
!=
0
)
{
printf
(
"getnameinfo() failed: %s
\n
"
,
gai_strerror
(
s
));
exit
(
EXIT_FAILURE
);
}
// Check if the interface is up and running
if
(
ifa
->
ifa_flags
&
IFF_UP
&&
ifa
->
ifa_flags
&
IFF_RUNNING
)
{
strncpy
(
interface
,
ifa
->
ifa_name
,
IFNAMSIZ
-
1
);
break
;
}
}
}
freeifaddrs
(
ifaddr
);
}
Common
::~
Common
(){}
Common
::~
Common
(){}
Common.h
View file @
742e07a4
#ifndef COMMON_H
#ifndef COMMON_H
#define COMMON_H
#define COMMON_H
#include <ctime>
#include <ctime>
#include <chrono>
#include <chrono>
#include <thread>
#include <thread>
...
@@ -17,6 +19,17 @@
...
@@ -17,6 +19,17 @@
#include <netinet/ip_icmp.h>
#include <netinet/ip_icmp.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <unistd.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <net/if_arp.h>
#include <linux/route.h>
#include <ifaddrs.h>
#include <netdb.h>
// ICMP 头部结构体
// ICMP 头部结构体
struct
ICMPHeader
{
struct
ICMPHeader
{
uint8_t
type
;
// 类型
uint8_t
type
;
// 类型
...
@@ -59,6 +72,11 @@ public:
...
@@ -59,6 +72,11 @@ public:
bool
receivePingReply
(
int
sockfd
,
int
sequence
);
bool
receivePingReply
(
int
sockfd
,
int
sequence
);
//确定当前网络接口
void
determine_interface
(
char
*
interface
);
void
get_network_info
(
const
char
*
interface
,
QString
&
mac
,
QString
&
subnet_mask
,
QString
&
gateway
)
;
bool
pingAddress
(
const
QString
&
address
)
;
bool
pingAddress
(
const
QString
&
address
)
;
...
...
NonConnectedCameraHandle.cpp
View file @
742e07a4
...
@@ -23,9 +23,19 @@ NonConnectedCameraHandle* NonConnectedCameraHandle::getInstance()
...
@@ -23,9 +23,19 @@ NonConnectedCameraHandle* NonConnectedCameraHandle::getInstance()
bool
NonConnectedCameraHandle
::
changeCameraIp
(
vides_data
::
localDevice
&
device
){
bool
NonConnectedCameraHandle
::
changeCameraIp
(
vides_data
::
localDevice
&
device
){
Common
&
instace
=
Common
::
getInstance
();
char
interface
[
IFNAMSIZ
];
// 确定当前网络接口
instace
.
determine_interface
(
interface
);
QString
localMac
,
subnetMask
,
gateway
;
QString
localMac
,
subnetMask
,
gateway
;
bool
success
=
vides_data
::
GetNetworkInfoByQNetworkInterface
(
localMac
,
subnetMask
,
gateway
);
// 获取网络信息
if
(
!
success
)
{
instace
.
get_network_info
(
interface
,
localMac
,
subnetMask
,
gateway
);
qInfo
()
<<
QString
(
"SN(%1): 获取本地MAC:%2,%3,%4"
).
arg
(
device
.
sSn
).
arg
(
localMac
)
.
arg
(
subnetMask
).
arg
(
gateway
);
if
(
localMac
==
"Unknown"
&&
subnetMask
==
"Unknown"
&&
gateway
==
"Unknown"
)
{
qInfo
()
<<
QString
(
"SN(%1): 获取本地MAC失败"
).
arg
(
device
.
sSn
);
qInfo
()
<<
QString
(
"SN(%1): 获取本地MAC失败"
).
arg
(
device
.
sSn
);
return
false
;
return
false
;
}
}
...
...
VidesData.h
View file @
742e07a4
...
@@ -440,48 +440,6 @@ inline bool isInSameSubnet(const QString &ip1, const QString &ip2, const QString
...
@@ -440,48 +440,6 @@ inline bool isInSameSubnet(const QString &ip1, const QString &ip2, const QString
// 比较结果
// 比较结果
return
result1
==
result2
;
return
result1
==
result2
;
}
}
inline
bool
GetNetworkInfoByQNetworkInterface
(
QString
&
mac
,
QString
&
subnetMask
,
QString
&
gateway
)
{
QList
<
QNetworkInterface
>
interfaces
=
QNetworkInterface
::
allInterfaces
();
foreach
(
QNetworkInterface
interface
,
interfaces
)
{
if
(
interface
.
flags
().
testFlag
(
QNetworkInterface
::
IsUp
)
&&
interface
.
flags
().
testFlag
(
QNetworkInterface
::
IsRunning
)
&&
!
interface
.
flags
().
testFlag
(
QNetworkInterface
::
IsLoopBack
))
{
mac
=
interface
.
hardwareAddress
();
qInfo
()
<<
"mac===>."
<<
mac
;
QList
<
QNetworkAddressEntry
>
addressEntries
=
interface
.
addressEntries
();
foreach
(
QNetworkAddressEntry
entry
,
addressEntries
)
{
if
(
entry
.
ip
().
protocol
()
==
QAbstractSocket
::
IPv4Protocol
)
{
subnetMask
=
entry
.
netmask
().
toString
();
qInfo
()
<<
"subnetMask===>."
<<
subnetMask
;
// 获取网关地址
QFile
file
(
"/proc/net/route"
);
if
(
!
file
.
open
(
QIODevice
::
ReadOnly
|
QIODevice
::
Text
))
{
qInfo
()
<<
"Failed to open /proc/net/route file."
;
continue
;
}
QTextStream
in
(
&
file
);
while
(
!
in
.
atEnd
())
{
QString
line
=
in
.
readLine
();
QStringList
parts
=
line
.
split
(
QRegExp
(
"
\\
s+"
));
if
(
parts
.
size
()
>=
3
&&
parts
[
1
]
==
"00000000"
)
{
gateway
=
parts
[
2
];
gateway
=
gateway
.
mid
(
6
,
2
)
+
":"
+
gateway
.
mid
(
4
,
2
)
+
":"
+
gateway
.
mid
(
2
,
2
)
+
":"
+
gateway
.
mid
(
0
,
2
);
gateway
=
gateway
.
replace
(
":"
,
"."
);
gateway
=
QHostAddress
(
gateway
).
toString
();
qInfo
()
<<
"gateway===>."
<<
gateway
;
return
true
;
}
}
}
}
}
}
return
false
;
// Return false if no suitable interface is found or gateway not found
}
inline
bool
pingAddress
(
const
QString
&
address
)
{
inline
bool
pingAddress
(
const
QString
&
address
)
{
QProcess
cmd
;
QProcess
cmd
;
...
...
Write
Preview
Markdown
is supported
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