1 /* Misc util functions */
3 char const *cvs_id_util
= "$Id: util.c,v 1.14 2006/04/05 01:45:57 bodea Exp $";
8 #include <sys/socket.h>
9 #include <netinet/in.h>
10 #include <arpa/inet.h>
19 // format ipv4 addr as a dotted-quad; n chooses one of 4 static buffers
21 char *fmtaddr(in_addr_t addr
, int n
)
23 static char addrs
[4][16];
30 return strcpy(addrs
[n
], inet_ntoa(in
));
33 void *shared_malloc(unsigned int size
)
36 p
= mmap(NULL
, size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
, 0, 0);
45 extern int cluster_sockfd
, tunfd
, udpfd
, controlfd
, daefd
, snoopfd
, ifrfd
, ifr6fd
, rand_fd
;
48 pid_t
fork_and_close()
57 if (config
->scheduler_fifo
)
59 struct sched_param params
= {0};
60 params
.sched_priority
= 0;
61 if (sched_setscheduler(0, SCHED_OTHER
, ¶ms
))
63 LOG(0, 0, 0, "Error setting scheduler to OTHER after fork: %s\n", strerror(errno
));
64 LOG(0, 0, 0, "This is probably really really bad.\n");
68 signal(SIGPIPE
, SIG_DFL
);
69 signal(SIGCHLD
, SIG_DFL
);
70 signal(SIGHUP
, SIG_DFL
);
71 signal(SIGUSR1
, SIG_DFL
);
72 signal(SIGQUIT
, SIG_DFL
);
73 signal(SIGKILL
, SIG_DFL
);
74 signal(SIGTERM
, SIG_DFL
);
77 if (clifd
!= -1) close(clifd
);
78 if (cluster_sockfd
!= -1) close(cluster_sockfd
);
79 if (tunfd
!= -1) close(tunfd
);
80 if (udpfd
!= -1) close(udpfd
);
81 if (controlfd
!= -1) close(controlfd
);
82 if (daefd
!= -1) close(daefd
);
83 if (snoopfd
!= -1) close(snoopfd
);
84 if (rand_fd
!= -1) close(rand_fd
);
85 if (epollfd
!= -1) close(epollfd
);
87 for (i
= 0; radfds
&& i
< RADIUS_FDS
; i
++)
91 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
92 if (bgp_peers
[i
].sock
!= -1)
93 close(bgp_peers
[i
].sock
);
99 ssize_t
recvfromto(int s
, void *buf
, size_t len
, int flags
,
100 struct sockaddr
*from
, socklen_t
*fromlen
, struct in_addr
*toaddr
)
104 struct cmsghdr
*cmsg
;
108 memset(&msg
, 0, sizeof(msg
));
110 msg
.msg_namelen
= *fromlen
;
118 msg
.msg_control
= cbuf
;
119 msg
.msg_controllen
= sizeof(cbuf
);
121 if ((r
= recvmsg(s
, &msg
, flags
)) < 0)
125 *fromlen
= msg
.msg_namelen
;
127 memset(toaddr
, 0, sizeof(*toaddr
));
128 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
))
130 if (cmsg
->cmsg_level
== SOL_IP
&& cmsg
->cmsg_type
== IP_PKTINFO
)
132 struct in_pktinfo
*i
= (struct in_pktinfo
*) CMSG_DATA(cmsg
);
133 memcpy(toaddr
, &i
->ipi_addr
, sizeof(*toaddr
));
141 ssize_t
sendtofrom(int s
, void const *buf
, size_t len
, int flags
,
142 struct sockaddr
const *to
, socklen_t tolen
, struct in_addr
const *from
)
145 struct cmsghdr
*cmsg
;
147 struct in_pktinfo pktinfo
;
148 char cbuf
[CMSG_SPACE(sizeof(pktinfo
))];
150 memset(&msg
, 0, sizeof(msg
));
151 msg
.msg_name
= (struct sockaddr
*) to
;
152 msg
.msg_namelen
= tolen
;
154 vec
.iov_base
= (void *) buf
;
160 msg
.msg_control
= cbuf
;
161 msg
.msg_controllen
= sizeof(cbuf
);
163 cmsg
= CMSG_FIRSTHDR(&msg
);
164 cmsg
->cmsg_level
= SOL_IP
;
165 cmsg
->cmsg_type
= IP_PKTINFO
;
166 cmsg
->cmsg_len
= CMSG_LEN(sizeof(pktinfo
));
168 memset(&pktinfo
, 0, sizeof(pktinfo
));
169 memcpy(&pktinfo
.ipi_spec_dst
, from
, sizeof(*from
));
170 memcpy(CMSG_DATA(cmsg
), &pktinfo
, sizeof(pktinfo
));
172 return sendmsg(s
, &msg
, flags
);