Imported Upstream version 2.88
[platform/upstream/dnsmasq.git] / src / outpacket.c
1 /* dnsmasq is Copyright (c) 2000-2022 Simon Kelley
2
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License as published by
5    the Free Software Foundation; version 2 dated June, 1991, or
6    (at your option) version 3 dated 29 June, 2007.
7  
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12      
13    You should have received a copy of the GNU General Public License
14    along with this program.  If not, see <http://www.gnu.org/licenses/>.
15 */
16
17
18 #include "dnsmasq.h"
19  
20 #ifdef HAVE_DHCP6
21
22 static size_t outpacket_counter;
23
24 void end_opt6(int container)
25 {
26    void *p = daemon->outpacket.iov_base + container + 2;
27    u16 len = outpacket_counter - container - 4 ;
28    
29    PUTSHORT(len, p);
30 }
31
32 void reset_counter(void)
33 {
34   /* Clear out buffer when starting from beginning */
35   if (daemon->outpacket.iov_base)
36     memset(daemon->outpacket.iov_base, 0, daemon->outpacket.iov_len);
37  
38   save_counter(0);
39 }
40
41 int save_counter(int newval)
42 {
43   int ret = outpacket_counter;
44   
45   if (newval != -1)
46     outpacket_counter = newval;
47
48   return ret;
49 }
50
51 void *expand(size_t headroom)
52 {
53   void *ret;
54
55   if (expand_buf(&daemon->outpacket, outpacket_counter + headroom))
56     {
57       ret = daemon->outpacket.iov_base + outpacket_counter;
58       outpacket_counter += headroom;
59       return ret;
60     }
61   
62   return NULL;
63 }
64     
65 int new_opt6(int opt)
66 {
67   int ret = outpacket_counter;
68   void *p;
69
70   if ((p = expand(4)))
71     {
72       PUTSHORT(opt, p);
73       PUTSHORT(0, p);
74     }
75
76   return ret;
77 }
78
79 void *put_opt6(void *data, size_t len)
80 {
81   void *p;
82
83   if ((p = expand(len)) && data)
84     memcpy(p, data, len);   
85   
86   return p;
87 }
88   
89 void put_opt6_long(unsigned int val)
90 {
91   void *p;
92   
93   if ((p = expand(4)))  
94     PUTLONG(val, p);
95 }
96
97 void put_opt6_short(unsigned int val)
98 {
99   void *p;
100
101   if ((p = expand(2)))
102     PUTSHORT(val, p);   
103 }
104
105 void put_opt6_char(unsigned int val)
106 {
107   unsigned char *p;
108
109   if ((p = expand(1)))
110     *p = val;   
111 }
112
113 void put_opt6_string(char *s)
114 {
115   put_opt6(s, strlen(s));
116 }
117
118 #endif