- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我需要一个列出以太网 USB 适配器接口(interface)的 C 函数。在互联网上进行一些搜索后,我在 ubuntu 16.04 中创建了以下小代码:
#include <stdio.h>
#include <stdlib.h>
/* The following include files for the network interface. */
#include <sys/types.h>
#include <sys/socket.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if_types.h>
/* For "toupper". */
#include <ctype.h>
/* For "strerror". */
#include <string.h>
/* For "errno". */
#include <errno.h>
int main() {
struct ifaddrs *addrs,*tmp;
getifaddrs(&addrs);
tmp = addrs;
struct sockaddr_dl * sdl;
while (tmp)
{
if (tmp->ifa_addr && tmp->ifa_addr->sa_family == AF_PACKET)
printf("%s\n", tmp->ifa_name);
tmp = tmp->ifa_next;
sdl = (struct sockaddr_dl *) tmp->ifa_addr;
if (sdl->sdl_type == IFT_USB) { //IFT_USB
printf("%s is usb adapter\n", tmp->ifa_name);
}
}
freeifaddrs(addrs);
}
如果我用 gcc 编译这段小代码,我会得到错误:
fatal error: net/if_dl.h: No such file or directory
我评论了//#include <net/if_dl.h>
然后我再次编译并得到另一个错误:
fatal error: net/if_types.h: No such file or directory
然后我评论了//#include <net/if_types.h>
我编译所以我得到了这个错误:
ethernetifaces.c: In function ‘main’:
ethernetifaces.c:48:13: error: dereferencing pointer to incomplete type ‘struct sockaddr_dl’
if (sdl->sdl_type == IFT_USB) { //IFT_USB
^
ethernetifaces.c:48:27: error: ‘IFT_USB’ undeclared (first use in this function)
if (sdl->sdl_type == IFT_USB) { //IFT_USB
^
ethernetifaces.c:48:27: note: each undeclared identifier is reported only once for each function it appears in
似乎是因为应该包含“struct sockaddr_dl”和“IFT_USB”的头文件被注释掉了。
有人知道什么是新的头文件吗?或者是否有gcc选项命令可以在编译时看到头文件?
内核版本:4.15.0-54-generic
最佳答案
我部分解决了您安装 freebsd-glue
的问题( sudo apt-get install freebsd-glue
) 并进行一些变通。
首先是创建文件 if_types.h 从互联网上搜索并将其插入到目录 /usr/include/freebsd/net.
中
第二个是注释行#include_next <net/if_dl.h>
进入文件 if_dl.h
( /usr/include/freebsd/net/if_dl.h
)
第三个是插入结构体的声明sockaddr_dl
到你的代码中。
这是您的代码和一些解决方法:
#include <stdio.h>
#include <stdlib.h>
/*The following include files for the network interface. */
#include <sys/types.h>
#include <sys/socket.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <freebsd/net/if_dl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <freebsd/net/if_types.h>
/* For "toupper". */
#include <ctype.h>
/* For "strerror". */
#include <string.h>
/* For "errno". */
#include <errno.h>
/*
* Structure of a Link-Level sockaddr:
*/
struct sockaddr_dl {
unsigned char sdl_len; /* Total length of sockaddr */
unsigned char sdl_family; /* AF_LINK */
unsigned short sdl_index; /* if != 0, system given index for interface */
unsigned char sdl_type; /* interface type */
unsigned char sdl_nlen; /* interface name length, no trailing 0 reqd. */
unsigned char sdl_alen; /* link level address length */
unsigned char sdl_slen; /* link layer selector length */
char sdl_data[46]; /* minimum work area, can be larger;
contains both if name and ll address */
};
int main() {
struct ifaddrs *addrs,*tmp;
getifaddrs(&addrs);
tmp = addrs;
struct sockaddr_dl * sdl;
while (tmp)
{
if (tmp->ifa_addr && tmp->ifa_addr->sa_family == AF_PACKET) {
sdl = (struct sockaddr_dl *) tmp->ifa_addr;
printf("%s %d - ", tmp->ifa_name, sdl->sdl_type);
if (sdl->sdl_type == IFT_USB) { //IFT_USB
printf("%s is usb adapter", tmp->ifa_name);
}
puts("");
}
tmp = tmp->ifa_next;
}
freeifaddrs(addrs);
}
这是 if_types.h 文件:
/*
* Copyright (c) 2000-2012 Apple Inc. All rights reserved.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. The rights granted to you under the License
* may not be used to create, or enable the creation or redistribution of,
* unlawful or unlicensed copies of an Apple operating system, or to
* circumvent, violate, or enable the circumvention or violation of, any
* terms of an Apple operating system software license agreement.
*
* Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_END@
*/
/*
* Copyright (c) 1989, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)if_types.h 8.2 (Berkeley) 4/20/94
* $FreeBSD: src/sys/net/if_types.h,v 1.8.2.3 2001/07/03 11:01:41 ume Exp $
*/
#ifndef _NET_IF_TYPES_H_
#define _NET_IF_TYPES_H_
// #include <sys/appleapiopts.h>
/*
* Interface types for benefit of parsing media address headers.
* This list is derived from the SNMP list of ifTypes, currently
* documented in RFC1573.
* The current list of assignments is maintained at:
* http://www.iana.org/assignments/smi-numbers
*/
#define IFT_OTHER 0x1 /* none of the following */
#define IFT_1822 0x2 /* old-style arpanet imp */
#define IFT_HDH1822 0x3 /* HDH arpanet imp */
#define IFT_X25DDN 0x4 /* x25 to imp */
#define IFT_X25 0x5 /* PDN X25 interface (RFC877) */
#define IFT_ETHER 0x6 /* Ethernet CSMACD */
#define IFT_ISO88023 0x7 /* CMSA CD */
#define IFT_ISO88024 0x8 /* Token Bus */
#define IFT_ISO88025 0x9 /* Token Ring */
#define IFT_ISO88026 0xa /* MAN */
#define IFT_STARLAN 0xb
#define IFT_P10 0xc /* Proteon 10MBit ring */
#define IFT_P80 0xd /* Proteon 80MBit ring */
#define IFT_HY 0xe /* Hyperchannel */
#define IFT_FDDI 0xf
#define IFT_LAPB 0x10
#define IFT_SDLC 0x11
#define IFT_T1 0x12
#define IFT_CEPT 0x13 /* E1 - european T1 */
#define IFT_ISDNBASIC 0x14
#define IFT_ISDNPRIMARY 0x15
#define IFT_PTPSERIAL 0x16 /* Proprietary PTP serial */
#define IFT_PPP 0x17 /* RFC 1331 */
#define IFT_LOOP 0x18 /* loopback */
#define IFT_EON 0x19 /* ISO over IP */
#define IFT_XETHER 0x1a /* obsolete 3MB experimental ethernet */
#define IFT_NSIP 0x1b /* XNS over IP */
#define IFT_SLIP 0x1c /* IP over generic TTY */
#define IFT_ULTRA 0x1d /* Ultra Technologies */
#define IFT_DS3 0x1e /* Generic T3 */
#define IFT_SIP 0x1f /* SMDS */
#define IFT_FRELAY 0x20 /* Frame Relay DTE only */
#define IFT_RS232 0x21
#define IFT_PARA 0x22 /* parallel-port */
#define IFT_ARCNET 0x23
#define IFT_ARCNETPLUS 0x24
#define IFT_ATM 0x25 /* ATM cells */
#define IFT_MIOX25 0x26
#define IFT_SONET 0x27 /* SONET or SDH */
#define IFT_X25PLE 0x28
#define IFT_ISO88022LLC 0x29
#define IFT_LOCALTALK 0x2a
#define IFT_SMDSDXI 0x2b
#define IFT_FRELAYDCE 0x2c /* Frame Relay DCE */
#define IFT_V35 0x2d
#define IFT_HSSI 0x2e
#define IFT_HIPPI 0x2f
#define IFT_MODEM 0x30 /* Generic Modem */
#define IFT_AAL5 0x31 /* AAL5 over ATM */
#define IFT_SONETPATH 0x32
#define IFT_SONETVT 0x33
#define IFT_SMDSICIP 0x34 /* SMDS InterCarrier Interface */
#define IFT_PROPVIRTUAL 0x35 /* Proprietary Virtual/internal */
#define IFT_PROPMUX 0x36 /* Proprietary Multiplexing */
/*
* IFT_GIF, IFT_FAITH and IFT_FAITH are not based on IANA assignments.
* Note: IFT_STF has a defined ifType: 0xd7 (215), but we use 0x39.
*/
#define IFT_GIF 0x37 /*0xf0*/
#define IFT_FAITH 0x38 /*0xf2*/
#define IFT_STF 0x39 /*0xf3*/
#define IFT_L2VLAN 0x87 /* Layer 2 Virtual LAN using 802.1Q */
#define IFT_IEEE8023ADLAG 0x88 /* IEEE802.3ad Link Aggregate */
#define IFT_IEEE1394 0x90 /* IEEE1394 High Performance SerialBus*/
#define IFT_USB 0xa0 /* USB net*/
#define IFT_BRIDGE 0xd1 /* Transparent bridge interface */
#define IFT_ENC 0xf4 /* Encapsulation */
#define IFT_PFLOG 0xf5 /* Packet filter logging */
#define IFT_PFSYNC 0xf6 /* Packet filter state syncing */
#define IFT_CARP 0xf8 /* Common Address Redundancy Protocol */
#define IFT_PKTAP 0xfe /* Packet tap pseudo interface */
#define IFT_CELLULAR 0xff /* Packet Data over Cellular */
#define IFT_PDP IFT_CELLULAR /* deprecated; use IFT_CELLULAR */
#endif
这是(修改后的)文件 if_dl.h:
//#include_next <net/if_dl.h>
#ifndef _FREEBSD_NET_IF_DL_H_
#define _FREEBSD_NET_IF_DL_H_
__BEGIN_DECLS
void link_addr(const char *, struct sockaddr_dl *);
char *link_ntoa(const struct sockaddr_dl *);
__END_DECLS
#endif
现在代码可以编译了,但我不确定代码是否能够识别 usb 网络适配器。
关于c - 列出以太网 USB 适配器接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56984151/
编写一个仅用于集中其他接口(interface)的接口(interface)是好的做法还是坏的做法? interface InterfaceA : InterfaceB, InterfaceC { }
有没有一种方法可以确定具体类型从任意接口(interface)列表?我知道类型转换,但我想知道所有满意的接口(interface)。 例如,给定: type Mover interface { Mo
我正在尝试制作斐波那契堆。 (在我正在上的算法课中多次提到它们,我想检查一下。)我希望堆使用任何类型的节点,所以我定义了一个 Node 接口(interface): package node type
这是我的代码: type IA interface { FB() IB } type IB interface { Bar() string } type A struct {
示例 A: // pseudo code interface IFoo { void bar(); } class FooPlatformA : IFoo { void bar() {
合并它编译的 leppies 反馈 - 但 IMO 有一些缺点,我希望编译器强制每个子类定义它们自己的 Uri 属性。现在的代码: [] type UriUserControl() = inh
我正在构建一个项目,该项目从用户那里获取一个术语,然后执行谷歌搜索并返回一个 json 格式的标题列表。 我正在使用 serpwow API 来执行谷歌搜索并试图解析响应。 但是我收到的错误是: pa
我只想在其他接口(interface)中实现某些接口(interface),我不希望它们能够被类直接继承。 提前致谢! 最佳答案 您不能在 C# 中执行此操作 - 任何类都可以实现它有权访问的任何接口
我是 Go 的新手,还有一些我还没有掌握的技巧 例如,我有一个可以这样调用的函数: myVar.InitOperation("foo",Operator.EQUAL,"bar") myVar.Init
我有一个通用接口(interface)来描述对输出流的访问,如下所示: interface IOutput { function writeInteger(aValue:Int):Void;
我正在做一个项目,我想通过某种接口(interface)(最好是 USB)将光电探测器电路安装到计算机上。但是,由于我是新手,所以我不知道应该朝哪个方向处理这个问题。假设我有一个带有 USB 连接的光
背景 我正在尝试创建一个简单的应用程序,以真正理解DDD + TDD + etc的整个堆栈。我的目标是在运行时动态注入DAL存储库类。这让我 域和应用程序服务层可测试。我打算用“穷人的DI”来完成 现
在 Java 中,接口(interface)扩展接口(interface)是完全合法的。 UML 中的这种关系看起来像“扩展”关系(实线、闭合、未填充的箭头)还是“实现”关系(虚线、闭合、未填充的箭头
我想创建一个具有相等和比较函数默认实现的接口(interface)。 如果我从类型 IKeyable 中删除所有内容除了Key成员,只要我不添加默认实现,它就是一个有效的接口(interface)。从
COM 中的双接口(interface)是能够通过 DispInterface 或 VTable 方法访问的接口(interface)。 现在有人可以告诉我这两种方法之间到底有什么区别吗? 我认为 V
我有一个类方法,它返回一个可以迭代的员工列表。返回列表的最佳方式是什么?通常我只返回一个 ArrayList。然而,据我了解,界面更适合这种类型的操作。哪个是最好使用的界面?另外,为什么返回接口(in
我想从包装类外部实例化一个内部非静态接口(interface)。 这可能吗? 考虑以下代码: shared class AOuterClass() { Integer val = 3; shared
我为一个类编写了一个接口(interface),如下所示: public interface IGenericMultipleRepository { Lazy> addresses { ge
我是 UML 的初学者,现在我正在创建一个序列图,问题是我想根据用户输入实现 DAO 接口(interface)。如何在时序图中正确绘制以实现接口(interface)。 最佳答案 您不会在 SD 上
要使用 jsr 303 验证创建有条件验证的组,请将接口(interface)类传递给注释,如下所示: @NotEmpty (groups={UpdateValue.class}) 我有很多不同的接口
我是一名优秀的程序员,十分优秀!