Merge branch 'fdn-mods'
[l2tpns.git] / util.c
1 /* Misc util functions */
2
3 char const *cvs_id_util = "$Id: util.c,v 1.14 2006/04/05 01:45:57 bodea Exp $";
4
5 #include <unistd.h>
6 #include <errno.h>
7 #include <sched.h>
8 #include <sys/socket.h>
9 #include <netinet/in.h>
10 #include <arpa/inet.h>
11 #include <string.h>
12 #include <sys/mman.h>
13
14 #include "l2tpns.h"
15 #ifdef BGP
16 #include "bgp.h"
17 #endif
18
19 // format ipv4 addr as a dotted-quad; n chooses one of 4 static buffers
20 // to use
21 char *fmtaddr(in_addr_t addr, int n)
22 {
23 static char addrs[4][16];
24 struct in_addr in;
25
26 if (n < 0 || n >= 4)
27 return "";
28
29 in.s_addr = addr;
30 return strcpy(addrs[n], inet_ntoa(in));
31 }
32
33 void *shared_malloc(unsigned int size)
34 {
35 void * p;
36 p = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0);
37
38 if (p == MAP_FAILED)
39 p = NULL;
40
41 return p;
42 }
43
44 extern int forked;
45 extern int cluster_sockfd, tunfd, udpfd, controlfd, daefd, snoopfd, ifrfd, ifr6fd, rand_fd;
46 extern int *radfds;
47
48 pid_t fork_and_close()
49 {
50 pid_t pid = fork();
51 int i;
52
53 if (pid)
54 return pid;
55
56 forked++;
57 if (config->scheduler_fifo)
58 {
59 struct sched_param params = {0};
60 params.sched_priority = 0;
61 if (sched_setscheduler(0, SCHED_OTHER, &params))
62 {
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");
65 }
66 }
67
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);
75
76 // Close sockets
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);
86
87 for (i = 0; radfds && i < RADIUS_FDS; i++)
88 close(radfds[i]);
89
90 #ifdef BGP
91 for (i = 0; i < BGP_NUM_PEERS; i++)
92 if (bgp_peers[i].sock != -1)
93 close(bgp_peers[i].sock);
94 #endif /* BGP */
95
96 return pid;
97 }
98
99 ssize_t recvfromto(int s, void *buf, size_t len, int flags,
100 struct sockaddr *from, socklen_t *fromlen, struct in_addr *toaddr)
101 {
102 ssize_t r;
103 struct msghdr msg;
104 struct cmsghdr *cmsg;
105 struct iovec vec;
106 char cbuf[128];
107
108 memset(&msg, 0, sizeof(msg));
109 msg.msg_name = from;
110 msg.msg_namelen = *fromlen;
111
112 vec.iov_base = buf;
113 vec.iov_len = len;
114 msg.msg_iov = &vec;
115 msg.msg_iovlen = 1;
116 msg.msg_flags = 0;
117
118 msg.msg_control = cbuf;
119 msg.msg_controllen = sizeof(cbuf);
120
121 if ((r = recvmsg(s, &msg, flags)) < 0)
122 return r;
123
124 if (fromlen)
125 *fromlen = msg.msg_namelen;
126
127 memset(toaddr, 0, sizeof(*toaddr));
128 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
129 {
130 if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_PKTINFO)
131 {
132 struct in_pktinfo *i = (struct in_pktinfo *) CMSG_DATA(cmsg);
133 memcpy(toaddr, &i->ipi_addr, sizeof(*toaddr));
134 break;
135 }
136 }
137
138 return r;
139 }
140
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)
143 {
144 struct msghdr msg;
145 struct cmsghdr *cmsg;
146 struct iovec vec;
147 struct in_pktinfo pktinfo;
148 char cbuf[CMSG_SPACE(sizeof(pktinfo))];
149
150 memset(&msg, 0, sizeof(msg));
151 msg.msg_name = (struct sockaddr *) to;
152 msg.msg_namelen = tolen;
153
154 vec.iov_base = (void *) buf;
155 vec.iov_len = len;
156 msg.msg_iov = &vec;
157 msg.msg_iovlen = 1;
158 msg.msg_flags = 0;
159
160 msg.msg_control = cbuf;
161 msg.msg_controllen = sizeof(cbuf);
162
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));
167
168 memset(&pktinfo, 0, sizeof(pktinfo));
169 memcpy(&pktinfo.ipi_spec_dst, from, sizeof(*from));
170 memcpy(CMSG_DATA(cmsg), &pktinfo, sizeof(pktinfo));
171
172 return sendmsg(s, &msg, flags);
173 }