+static void udp_phase_hook(void *sst, uint32_t new_phase)
+{
+ struct udp *st=sst;
+ struct sockaddr_in addr;
+
+ st->fd=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
+ if (st->fd<0) {
+ fatal_perror("udp (%s:%d): socket",st->loc.file,st->loc.line);
+ }
+ if (fcntl(st->fd, F_SETFL, fcntl(st->fd, F_GETFL)|O_NONBLOCK)==-1) {
+ fatal_perror("udp (%s:%d): fcntl(set O_NONBLOCK)",
+ st->loc.file,st->loc.line);
+ }
+ if (fcntl(st->fd, F_SETFD, FD_CLOEXEC)==-1) {
+ fatal_perror("udp (%s:%d): fcntl(set FD_CLOEXEC)",
+ st->loc.file,st->loc.line);
+ }
+
+ FILLZERO(addr);
+ addr.sin_family=AF_INET;
+ addr.sin_addr.s_addr=htonl(st->addr);
+ addr.sin_port=htons(st->port);
+ if (st->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[4], addrstr[9], portstr[5];
+ sprintf(addrstr,"%08lX",(long)addr.sin_addr.s_addr);
+ sprintf(portstr,"%04X",addr.sin_port);
+ argv[0]=st->authbind;
+ argv[1]=addrstr;
+ argv[2]=portstr;
+ argv[3]=NULL;
+ dup2(st->fd,0);
+ execvp(st->authbind,argv);
+ _exit(255);
+ }
+ while (waitpid(c,&status,0)==-1) {
+ if (errno==EINTR) continue;
+ fatal_perror("udp (%s:%d): authbind",st->loc.file,st->loc.line);
+ }
+ if (WIFSIGNALED(status)) {
+ fatal("udp (%s:%d): authbind died on signal %d",st->loc.file,
+ st->loc.line, WTERMSIG(status));
+ }
+ if (WIFEXITED(status) && WEXITSTATUS(status)!=0) {
+ fatal("udp (%s:%d): authbind died with status %d",st->loc.file,
+ st->loc.line, WEXITSTATUS(status));
+ }
+ } else {
+ if (bind(st->fd, (struct sockaddr *)&addr, sizeof(addr))!=0) {
+ fatal_perror("udp (%s:%d): bind",st->loc.file,st->loc.line);
+ }
+ }
+
+ register_for_poll(st,udp_beforepoll,udp_afterpoll,1,"udp");
+}
+