本文整理汇总了C++中xsocket函数的典型用法代码示例。如果您正苦于以下问题:C++ xsocket函数的具体用法?C++ xsocket怎么用?C++ xsocket使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了xsocket函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: setup_socket
/* Setup the address and return socket fd */
static int setup_socket(struct sockaddr_un *sun) {
int fd = xsocket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
memset(sun, 0, sizeof(*sun));
sun->sun_family = AF_LOCAL;
memcpy(sun->sun_path, REQUESTOR_DAEMON_PATH, REQUESTOR_DAEMON_PATH_LEN);
return fd;
}
示例2: my_server
void my_server(char *ip, int port, char *ip2, int port2)
{
int error;
t_serv_info *serv;
t_dest_info *dest;
struct protoent *pe;
serv = malloc(sizeof(serv));
dest = malloc(sizeof(dest));
pe = getprotobyname("TCP");
serv->sock = xsocket(PF_INET, SOCK_STREAM, pe->p_proto);
serv->sin.sin_addr.s_addr = INADDR_ANY;
serv->sin.sin_family = AF_INET;
serv->sin.sin_port = htons(port);
serv->ip = ip;
serv->port = port;
error = xbind(serv->sock,
(const struct sockaddr *)&serv->sin, sizeof(serv->sin));
error = xlisten(serv->sock, 42);
serv->client_len = sizeof(serv->sin_client);
dest->ip = ip2;
dest->port = port2;
main_loop(error, serv, dest);
close(serv->sock);
}
示例3: get_address
/* Exits on error */
static int get_address(char *dev, int *htype)
{
struct ifreq ifr;
struct sockaddr_ll me;
socklen_t alen;
int s;
s = xsocket(PF_PACKET, SOCK_DGRAM, 0);
memset(&ifr, 0, sizeof(ifr));
strncpy_IFNAMSIZ(ifr.ifr_name, dev);
xioctl(s, SIOCGIFINDEX, &ifr);
memset(&me, 0, sizeof(me));
me.sll_family = AF_PACKET;
me.sll_ifindex = ifr.ifr_ifindex;
me.sll_protocol = htons(ETH_P_LOOP);
xbind(s, (struct sockaddr*)&me, sizeof(me));
alen = sizeof(me);
getsockname(s, (struct sockaddr*)&me, &alen);
//never happens:
//if (getsockname(s, (struct sockaddr*)&me, &alen) == -1)
// bb_perror_msg_and_die("getsockname");
close(s);
*htype = me.sll_hatype;
return me.sll_halen;
}
示例4: listen_socket
/* 2. ip was always INADDR_ANY */
int listen_socket(/*uint32_t ip,*/ int port, const char *inf)
{
int fd;
struct ifreq interface;
struct sockaddr_in addr;
DEBUG("Opening listen socket on *:%d %s", port, inf);
fd = xsocket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
setsockopt_reuseaddr(fd);
if (setsockopt_broadcast(fd) == -1)
bb_perror_msg_and_die("SO_BROADCAST");
strncpy(interface.ifr_name, inf, IFNAMSIZ);
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &interface, sizeof(interface)) == -1)
bb_perror_msg_and_die("SO_BINDTODEVICE");
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
/* addr.sin_addr.s_addr = ip; - all-zeros is INADDR_ANY */
xbind(fd, (struct sockaddr *)&addr, sizeof(addr));
return fd;
}
示例5: vconfig_main
void vconfig_main(void)
{
struct vlan_ioctl_args request;
char *cmd;
int fd;
fd = xsocket(AF_INET, SOCK_STREAM, 0);
memset(&request, 0, sizeof(struct vlan_ioctl_args));
cmd = toys.optargs[0];
if (!strcmp(cmd, "set_name_type")) {
char *types[] = {"VLAN_PLUS_VID", "DEV_PLUS_VID", "VLAN_PLUS_VID_NO_PAD",
"DEV_PLUS_VID_NO_PAD"};
int i, j = sizeof(types)/sizeof(*types);
for (i=0; i<j; i++) if (!strcmp(toys.optargs[1], types[i])) break;
if (i == j) {
for (i=0; i<j; i++) puts(types[i]);
error_exit("%s: unknown '%s'", cmd, toys.optargs[1]);
}
request.u.name_type = i;
request.cmd = SET_VLAN_NAME_TYPE_CMD;
xioctl(fd, SIOCSIFVLAN, &request);
return;
}
// Store interface name
xstrncpy(request.device1, toys.optargs[1], 16);
if (!strcmp(cmd, "add")) {
request.cmd = ADD_VLAN_CMD;
if (toys.optargs[2]) request.u.VID = atolx_range(toys.optargs[2], 0, 4094);
if (request.u.VID == 1)
xprintf("WARNING: VLAN 1 does not work with many switches.\n");
} else if (!strcmp(cmd, "rem")) request.cmd = DEL_VLAN_CMD;
else if (!strcmp(cmd, "set_flag")) {
request.cmd = SET_VLAN_FLAG_CMD;
if (toys.optargs[2]) request.u.flag = atolx_range(toys.optargs[2], 0, 1);
if (toys.optargs[3]) request.vlan_qos = atolx_range(toys.optargs[3], 0, 7);
} else if(strcmp(cmd, "set_egress_map") == 0) {
request.cmd = SET_VLAN_EGRESS_PRIORITY_CMD;
if (toys.optargs[2])
request.u.skb_priority = atolx_range(toys.optargs[2], 0, INT_MAX);
if (toys.optargs[3]) request.vlan_qos = atolx_range(toys.optargs[3], 0, 7);
} else if(strcmp(cmd, "set_ingress_map") == 0) {
request.cmd = SET_VLAN_INGRESS_PRIORITY_CMD;
if (toys.optargs[2])
request.u.skb_priority = atolx_range(toys.optargs[2], 0, INT_MAX);
//To set flag we must have to set vlan_qos
if (toys.optargs[3]) request.vlan_qos = atolx_range(toys.optargs[3], 0, 7);
} else {
xclose(fd);
perror_exit("Unknown command %s", cmd);
}
xioctl(fd, SIOCSIFVLAN, &request);
xprintf("Successful %s on device %s\n", cmd, toys.optargs[1]);
}
示例6: udhcp_read_interface
int FAST_FUNC udhcp_read_interface(const char *interface, int *ifindex, uint32_t *nip, uint8_t *mac, uint16_t *mtu)
{
/* char buffer instead of bona-fide struct avoids aliasing warning */
char ifr_buf[sizeof(struct ifreq)];
struct ifreq *const ifr = (void *)ifr_buf;
int fd;
struct sockaddr_in *our_ip;
memset(ifr, 0, sizeof(*ifr));
fd = xsocket(AF_INET, SOCK_RAW, IPPROTO_RAW);
ifr->ifr_addr.sa_family = AF_INET;
strncpy_IFNAMSIZ(ifr->ifr_name, interface);
if (nip) {
if (ioctl_or_perror(fd, SIOCGIFADDR, ifr,
"is interface %s up and configured?", interface)
) {
close(fd);
return -1;
}
our_ip = (struct sockaddr_in *) &ifr->ifr_addr;
*nip = our_ip->sin_addr.s_addr;
log1("IP %s", inet_ntoa(our_ip->sin_addr));
}
if (ifindex) {
if (ioctl_or_warn(fd, SIOCGIFINDEX, ifr) != 0) {
close(fd);
return -1;
}
log1("Adapter index %d", ifr->ifr_ifindex);
*ifindex = ifr->ifr_ifindex;
}
if (mac) {
if (ioctl_or_warn(fd, SIOCGIFHWADDR, ifr) != 0) {
close(fd);
return -1;
}
memcpy(mac, ifr->ifr_hwaddr.sa_data, 6);
log1("MAC %02x:%02x:%02x:%02x:%02x:%02x",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}
if (mtu) {
if (ioctl_or_warn(fd, SIOCGIFMTU, ifr) != 0) {
close(fd);
return -1;
}
log1("Adapter mtu %d", ifr->ifr_mtu);
*mtu = ifr->ifr_mtu;
}
close(fd);
return 0;
}
示例7: vconfig_main
int vconfig_main(int argc, char **argv)
{
struct vlan_ioctl_args ifr;
const char *p;
int fd;
if (argc < 3) {
bb_show_usage();
}
/* Don't bother closing the filedes. It will be closed on cleanup. */
/* Will die if 802.1q is not present */
xopen(conf_file_name, O_RDONLY);
memset(&ifr, 0, sizeof(struct vlan_ioctl_args));
++argv;
p = xfind_str(cmds+2, *argv);
ifr.cmd = *p;
if (argc != p[-1]) {
bb_show_usage();
}
if (ifr.cmd == SET_VLAN_NAME_TYPE_CMD) { /* set_name_type */
ifr.u.name_type = *xfind_str(name_types+1, argv[1]);
} else {
if (strlen(argv[1]) >= IF_NAMESIZE) {
bb_error_msg_and_die("if_name >= %d chars", IF_NAMESIZE);
}
strcpy(ifr.device1, argv[1]);
p = argv[2];
/* I suppose one could try to combine some of the function calls below,
* since ifr.u.flag, ifr.u.VID, and ifr.u.skb_priority are all same-sized
* (unsigned) int members of a unions. But because of the range checking,
* doing so wouldn't save that much space and would also make maintainence
* more of a pain. */
if (ifr.cmd == SET_VLAN_FLAG_CMD) { /* set_flag */
ifr.u.flag = xatoul_range(p, 0, 1);
/* DM: in order to set reorder header, qos must be set */
ifr.vlan_qos = xatoul_range(argv[3], 0, 7);
} else if (ifr.cmd == ADD_VLAN_CMD) { /* add */
ifr.u.VID = xatoul_range(p, 0, VLAN_GROUP_ARRAY_LEN-1);
} else if (ifr.cmd != DEL_VLAN_CMD) { /* set_{egress|ingress}_map */
ifr.u.skb_priority = xatou(p);
ifr.vlan_qos = xatoul_range(argv[3], 0, 7);
}
}
fd = xsocket(AF_INET, SOCK_STREAM, 0);
if (ioctl(fd, SIOCSIFVLAN, &ifr) < 0) {
bb_perror_msg_and_die("ioctl error for %s", *argv);
}
return 0;
}
示例8: proceed_request_stack
static void proceed_request_stack(int srv)
{
struct parse_state srv_state = { .state = ST_CMD,
.p_idx = 0 };
struct request *r;
int i=0;
for(r = req_stack ; r != NULL ; r = r->next) {
proceed_request(srv, r);
while(parse(srv, &srv_state, proceed_response));
}
}
static void client()
{
int sd;
struct request quit = { .command = CMD_QUIT };
struct sockaddr_un s_addr = { .sun_family = AF_UNIX };
struct sigaction act = { .sa_handler = srv_timeout,
.sa_flags = 0 };
/* ensure this lient won't live more than REQUEST_TIMEOUT seconds */
sigfillset(&act.sa_mask);
sigaction(SIGALRM, &act, NULL);
alarm(REQUEST_TIMEOUT);
/* socket creation */
sd = xsocket(AF_UNIX, SOCK_STREAM, 0);
/* bind socket to the specified sock_path */
strncpy(s_addr.sun_path, sock_path, UNIX_PATH_MAX);
/* connect to the server */
xconnect(sd, (struct sockaddr *)&s_addr, SUN_LEN(&s_addr));
proceed_request_stack(sd);
/* quit only after the request has been proceeded */
proceed_request(sd, &quit);
}
int main(int argc, char * const argv[])
{
/* get program name */
prog_name = (const char *)strrchr(argv[0], '/');
prog_name = prog_name ? (prog_name + 1) : argv[0];
/* parse command line and build
the request stack */
cmdline(argc, argv);
/* start client */
client();
return EXIT_SUCCESS;
}
示例9: serveur
int serveur(t_serv *e)
{
int fd;
fd = xsocket();
xbind(fd, e);
xlisten(fd);
e->fd_type[fd] = FD_SERVEUR;
e->fct_read[fd] = serveur_read;
}
示例10: do_ioctl_get_ifindex
/* Dies on error */
static int do_ioctl_get_ifindex(char *dev)
{
struct ifreq ifr;
int fd;
strncpy_IFNAMSIZ(ifr.ifr_name, dev);
fd = xsocket(AF_INET, SOCK_DGRAM, 0);
xioctl(fd, SIOCGIFINDEX, &ifr);
close(fd);
return ifr.ifr_ifindex;
}
示例11: arp_main
int arp_main(int argc ATTRIBUTE_UNUSED, char **argv)
{
char *hw_type;
char *protocol;
/* Initialize variables... */
ap = get_aftype(DFLT_AF);
if (!ap)
bb_error_msg_and_die("%s: %s not supported", DFLT_AF, "address family");
getopt32(argv, "A:p:H:t:i:adnDsv", &protocol, &protocol,
&hw_type, &hw_type, &device);
argv += optind;
if (option_mask32 & ARP_OPT_A || option_mask32 & ARP_OPT_p) {
ap = get_aftype(protocol);
if (ap == NULL)
bb_error_msg_and_die("%s: unknown %s", protocol, "address family");
}
if (option_mask32 & ARP_OPT_A || option_mask32 & ARP_OPT_p) {
hw = get_hwtype(hw_type);
if (hw == NULL)
bb_error_msg_and_die("%s: unknown %s", hw_type, "hardware type");
hw_set = 1;
}
//if (option_mask32 & ARP_OPT_i)... -i
if (ap->af != AF_INET) {
bb_error_msg_and_die("%s: kernel only supports 'inet'", ap->name);
}
/* If no hw type specified get default */
if (!hw) {
hw = get_hwtype(DFLT_HW);
if (!hw)
bb_error_msg_and_die("%s: %s not supported", DFLT_HW, "hardware type");
}
if (hw->alen <= 0) {
bb_error_msg_and_die("%s: %s without ARP support",
hw->name, "hardware type");
}
sockfd = xsocket(AF_INET, SOCK_DGRAM, 0);
/* Now see what we have to do here... */
if (option_mask32 & (ARP_OPT_d|ARP_OPT_s)) {
if (argv[0] == NULL)
bb_error_msg_and_die("need host name");
if (option_mask32 & ARP_OPT_s)
return arp_set(argv);
return arp_del(argv);
}
//if (option_mask32 & ARP_OPT_a) - default
return arp_show(argv[0]);
}
示例12: xsocket
static char *do_ioctl_get_ifname(int idx)
{
struct ifreq ifr;
int fd;
int err;
ifr.ifr_ifindex = idx;
fd = xsocket(AF_INET, SOCK_DGRAM, 0);
err = ioctl_or_warn(fd, SIOCGIFNAME, &ifr);
close(fd);
return err ? NULL : xstrndup(ifr.ifr_name, sizeof(ifr.ifr_name));
}
示例13: get_ctl_fd
/* Exits on error */
static int get_ctl_fd(void)
{
int fd;
fd = socket(PF_INET, SOCK_DGRAM, 0);
if (fd >= 0)
return fd;
fd = socket(PF_PACKET, SOCK_DGRAM, 0);
if (fd >= 0)
return fd;
return xsocket(PF_INET6, SOCK_DGRAM, 0);
}
示例14: init_serveur
int init_serveur(t_desc *serv)
{
int s;
struct sockaddr_in ser;
s = xsocket(PF_INET, SOCK_STREAM, 0);
ser.sin_family = AF_INET;
ser.sin_port = htons(serv->port);
ser.sin_addr.s_addr = INADDR_ANY;
xbind(s, ser);
return (s);
}
示例15: do_ioctl_get_iftype
static int do_ioctl_get_iftype(char *dev)
{
struct ifreq ifr;
int fd;
int err;
strncpy_IFNAMSIZ(ifr.ifr_name, dev);
fd = xsocket(AF_INET, SOCK_DGRAM, 0);
err = ioctl_or_warn(fd, SIOCGIFHWADDR, &ifr);
close(fd);
return err ? -1 : ifr.ifr_addr.sa_family;
}