Login
[x]
Log in using an account from:
Fedora Account System
Red Hat Associate
Red Hat Customer
Or login using a Red Hat Bugzilla account
Forgot Password
Login:
Hide Forgot
Create an Account
Red Hat Bugzilla – Attachment 311401 Details for
Bug 453325
RFE: export useful resolver interfaces
[?]
New
Simple Search
Advanced Search
My Links
Browse
Requests
Reports
Current State
Search
Tabular reports
Graphical reports
Duplicates
Other Reports
User Changes
Plotly Reports
Bug Status
Bug Severity
Non-Defaults
|
Product Dashboard
Help
Page Help!
Bug Writing Guidelines
What's new
Browser Support Policy
5.0.4.rh83 Release notes
FAQ
Guides index
User guide
Web Services
Contact
Legal
This site requires JavaScript to be enabled to function correctly, please enable it.
Example code used in server to look up DNS SRV records
server.c (text/plain), 4.87 KB, created by
Jeff Garzik
on 2008-07-09 18:36:28 UTC
(
hide
)
Description:
Example code used in server to look up DNS SRV records
Filename:
MIME Type:
Creator:
Jeff Garzik
Created:
2008-07-09 18:36:28 UTC
Size:
4.87 KB
patch
obsolete
>#define _GNU_SOURCE >#include <sys/types.h> >#include <sys/stat.h> >#include <sys/socket.h> >#include <sys/epoll.h> >#include <fcntl.h> >#include <stdlib.h> >#include <unistd.h> >#include <string.h> >#include <netdb.h> >#include <stdio.h> >#include <signal.h> >#include <locale.h> >#include <netinet/in.h> >#include <stdbool.h> >#include <arpa/nameser.h> >#include <resolv.h> >#include <syslog.h> >#include <argp.h> > >enum { > CLD_EPOLL_INIT_SIZE = 200, /* passed to epoll_create(2) */ > CLD_EPOLL_MAX_EVT = 100, /* max events per poll */ > CLD_DEF_PORT = 8080, /* default bind(2) port */ > > SFL_FOREGROUND = (1 << 0), /* run in foreground */ >}; > >enum server_poll_type { > spt_udp_srv, /* UDP server */ >}; > >enum { > UDP_MAX_SZ = 1280, >}; > >struct server_poll { > enum server_poll_type poll_type; /* spt_xxx above */ > void *ptr; >}; > >struct server_stats { > unsigned long poll; /* number polls */ > unsigned long event; /* events dispatched */ > unsigned long udp_rx; /* UDP RX'd msgs */ > unsigned long udp_rx_bytes; /* UDP RX'd bytes */ > unsigned long udp_bad; /* Invalid UDP pkts */ > unsigned long udp_content; /* UDP decrypt/check failed */ > unsigned long max_evt; /* epoll events max'd out */ > unsigned long cli_rx; /* client messages RX'd */ > unsigned long peer_rx; /* peer messages RX'd */ >}; > >struct list_head { > struct list_head *next, *prev; >}; > >struct peer { > struct list_head node; > > int prio; > int weight; > int port; > > char name[64]; > char addrname[64]; > > struct sockaddr addr; > size_t addrlen; >}; > >struct server { > unsigned long flags; /* SFL_xxx above */ > > char *data_dir; /* database/log dir */ > char *pid_file; /* PID file */ > > int port; /* bind port */ > > int epoll_fd; /* epoll fd */ > > int udp_fd; /* UDP server desc */ > struct server_poll udp_poll; /* poll info */ > struct epoll_event udp_evt; /* epoll info */ > > struct server_stats stats; /* global statistics */ > > char *peer_srv; /* DNS SRV name */ > struct list_head peers; >}; > > >static struct server cld_srv = { > .peer_srv = "yyz.home.", >}; > >static void die(const char *msg) >{ > syslog(LOG_ERR, "%s", msg); > exit(1); >} > >static int peer_push_one(uint16_t prio, uint16_t weight, uint16_t port, > const char *target, struct sockaddr *addr, > size_t addrlen) >{ > struct peer *peer; > > peer = calloc(1, sizeof(*peer)); > if (!peer) { > syslog(LOG_ERR, "OOM in peer_push_one"); > return 0; > } > >#if 0 > INIT_LIST_HEAD(&peer->node); >#endif > > peer->prio = prio; > peer->weight = weight; > peer->port = port; > strcpy(peer->name, target); > memcpy(&peer->addr, addr, addrlen); > peer->addrlen = addrlen; > > if (getnameinfo(addr, addrlen, peer->addrname, sizeof(peer->addrname), > NULL, 0, NI_NUMERICHOST | NI_NUMERICSERV)) { > syslog(LOG_ERR, "getnameinfo failed"); > goto err_out; > } > >#if 0 > list_add_tail(&peer->node, &cld_srv.peers); >#endif > > syslog(LOG_INFO, "peer %s:%d added", > peer->addrname, > peer->port); > > return 1; > >err_out: > free(peer); > return 0; >} > >static void peer_push(uint16_t prio, uint16_t weight, uint16_t port, > const char *target) >{ > struct addrinfo hints; > struct addrinfo *result, *ai; > int rc, res = 0; > char port_s[8]; > > memset(&hints, 0, sizeof(hints)); > hints.ai_family = AF_UNSPEC; > hints.ai_socktype = SOCK_DGRAM; > hints.ai_flags = AI_PASSIVE; > > sprintf(port_s, "%u", port); > > rc = getaddrinfo(target, port_s, &hints, &result); > if (rc) { > syslog(LOG_WARNING, "getaddrinfo: %s", gai_strerror(rc)); > return; > } > > ai = result; > while (ai) { > res += peer_push_one(prio, weight, port, target, > ai->ai_addr, ai->ai_addrlen); > ai = ai->ai_next; > } > > freeaddrinfo(result); > > if (!res) > syslog(LOG_WARNING, "SRV record '%s' yielded no peers", target); >} > >static int dns_srv_lookups(void) >{ > unsigned char buf[4096]; > int len, rrnum; > ns_msg hand; > ns_rr rr; > char peer_srv_full[64]; > > snprintf(peer_srv_full, sizeof(peer_srv_full), > "_cld._tcp.%s", cld_srv.peer_srv); > > len = res_query(peer_srv_full, ns_c_in, ns_t_srv, > buf, sizeof(buf)); > if (len < 0) { > syslog(LOG_ERR, "DNS SRV query for %s failed", peer_srv_full); > return 1; > } > > if (ns_initparse(buf, len, &hand) < 0) > die("ns_initparse failed"); > > if (ns_msg_getflag(hand, ns_f_rcode) != ns_r_noerror) > die("SRV lookup failed to return NOERROR (success)"); > > for (rrnum = 0; rrnum < ns_msg_count(hand, ns_s_an); rrnum++) { > const unsigned char *p; > uint16_t prio, weight, port; > char targ[65]; > > if (ns_parserr(&hand, ns_s_an, rrnum, &rr) < 0) > die("ns_parserr failed"); > > if (ns_rr_type(rr) != ns_t_srv) > die("ns_rr_type != ns_t_srv"); > if (ns_rr_class(rr) != ns_c_in) > die("ns_rr_class != ns_c_in"); > > p = ns_rr_rdata(rr); > > if (ns_rr_rdlen(rr) < (2 + 2 + 2 + 1)) > die("rr parse: packet RDATA truncated"); > NS_GET16(prio, p); > NS_GET16(weight, p); > NS_GET16(port, p); > > if (ns_name_uncompress(ns_msg_base(hand), > ns_msg_end(hand), p, targ, sizeof(targ)) < 0) > die("ns_name_uncompress failed"); > > peer_push(prio, weight, port, targ); > } > > return 0; >} >
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 453325
: 311401 |
311402