+static void udp_make_socket(struct udp *st, struct udpsock *us)
+{
+ const union iaddr *addr=&us->addr;
+ struct udp *cc=st; /* for now */
+ struct udp *uc=st; /* for now */
+ us->fd=socket(addr->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
+ if (us->fd<0) {
+ fatal_perror("udp (%s:%d): socket",cc->loc.file,cc->loc.line);
+ }
+ if (fcntl(us->fd, F_SETFL, fcntl(us->fd, F_GETFL)|O_NONBLOCK)==-1) {
+ fatal_perror("udp (%s:%d): fcntl(set O_NONBLOCK)",
+ cc->loc.file,cc->loc.line);
+ }
+ setcloexec(us->fd);
+#ifdef CONFIG_IPV6
+ if (addr->sa.sa_family==AF_INET6) {
+ int r;
+ int optval=1;
+ socklen_t optlen=sizeof(optval);
+ r=setsockopt(us->fd,IPPROTO_IPV6,IPV6_V6ONLY,&optval,optlen);
+ if (r) fatal_perror("udp (%s:%d): setsockopt(,IPV6_V6ONLY,&1,)",
+ cc->loc.file,cc->loc.line);
+ }
+#endif
+
+ if (uc->authbind) {
+ pid_t c;
+ int status;
+
+ /* XXX this fork() and waitpid() business needs to be hidden
+ in some system-specific library functions. */
+ c=fork();
+ if (c==-1) {
+ fatal_perror("udp_phase_hook: fork() for authbind");
+ }
+ if (c==0) {
+ char *argv[5], addrstr[33], portstr[5];
+ const char *addrfam;
+ int port;
+ switch (addr->sa.sa_family) {
+ case AF_INET:
+ sprintf(addrstr,"%08lX",(long)addr->sin.sin_addr.s_addr);
+ port=addr->sin.sin_port;
+ addrfam=NULL;
+ break;
+#ifdef CONFIG_IPV6
+ case AF_INET6: {
+ int i;
+ for (i=0; i<16; i++)
+ sprintf(addrstr+i*2,"%02X",addr->sin6.sin6_addr.s6_addr[i]);
+ port=addr->sin6.sin6_port;
+ addrfam="6";
+ break;
+ }
+#endif /*CONFIG_IPV6*/
+ default:
+ fatal("udp (%s:%d): unsupported address family for authbind",
+ cc->loc.file,cc->loc.line);
+ }
+ sprintf(portstr,"%04X",port);
+ argv[0]=uc->authbind;
+ argv[1]=addrstr;
+ argv[2]=portstr;
+ argv[3]=(char*)addrfam;
+ argv[4]=NULL;
+ dup2(us->fd,0);
+ execvp(uc->authbind,argv);
+ _exit(255);
+ }
+ while (waitpid(c,&status,0)==-1) {
+ if (errno==EINTR) continue;
+ fatal_perror("udp (%s:%d): authbind",cc->loc.file,cc->loc.line);
+ }
+ if (WIFSIGNALED(status)) {
+ fatal("udp (%s:%d): authbind died on signal %d",cc->loc.file,
+ cc->loc.line, WTERMSIG(status));
+ }
+ if (WIFEXITED(status) && WEXITSTATUS(status)!=0) {
+ fatal("udp (%s:%d): authbind died with status %d",cc->loc.file,
+ cc->loc.line, WEXITSTATUS(status));
+ }
+ } else {
+ if (bind(us->fd, &addr->sa, iaddr_socklen(addr))!=0) {
+ fatal_perror("udp (%s:%d): bind",cc->loc.file,cc->loc.line);
+ }
+ }
+}
+
+static void udp_phase_hook(void *sst, uint32_t new_phase)
+{
+ struct udp *st=sst;
+ struct udp *socks=st; /* for now */
+ int i;
+ for (i=0; i<socks->n_socks; i++)
+ udp_make_socket(st,&socks->socks[i]);
+
+ register_for_poll(st,udp_beforepoll,udp_afterpoll,"udp");
+}
+