xref: /netbsd-src/usr.sbin/mopd/common/put.c (revision da9817918ec7e88db2912a2882967c7570a83f47)
1 /*	$NetBSD: put.c,v 1.4 2009/04/17 04:16:57 lukem Exp $	*/
2 
3 /*
4  * Copyright (c) 1993-95 Mats O Jansson.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Mats O Jansson.
17  * 4. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: put.c,v 1.4 2009/04/17 04:16:57 lukem Exp $");
35 #endif
36 
37 #include "os.h"
38 #include "mopdef.h"
39 #include "put.h"
40 
41 void
42 mopPutChar(pkt, idx, value)
43 	u_char  *pkt;
44 	int     *idx;
45 	u_char   value;
46 {
47 	pkt[*idx] = value;
48 	*idx = *idx + 1;
49 }
50 
51 void
52 mopPutShort(pkt, idx, value)
53 	u_char  *pkt;
54 	int     *idx;
55 	u_short  value;
56 {
57         int i;
58 	for (i = 0; i < 2; i++) {
59 	  pkt[*idx+i] = value % 256;
60 	  value = value / 256;
61 	}
62 	*idx = *idx + 2;
63 }
64 
65 void
66 mopPutLong(pkt, idx, value)
67 	u_char	       *pkt;
68 	int	       *idx;
69 	u_int32_t	value;
70 {
71         int i;
72 	for (i = 0; i < 4; i++) {
73 	  pkt[*idx+i] = value % 256;
74 	  value = value / 256;
75 	}
76 	*idx = *idx + 4;
77 }
78 
79 void
80 mopPutMulti(pkt, idx, value, size)
81 	u_char *pkt;
82 	int    *idx,size;
83 	const u_char *value;
84 {
85 	int i;
86 
87 	for (i = 0; i < size; i++) {
88 	  pkt[*idx+i] = value[i];
89 	}
90 	*idx = *idx + size;
91 }
92 
93 void
94 mopPutTime(pkt, idx, value)
95 	u_char *pkt;
96 	int    *idx;
97 	time_t	value;
98 {
99 	time_t tnow;
100 	struct tm *timenow;
101 
102 	if ((value == 0)) {
103 	  tnow = time(NULL);
104 	} else {
105 	  tnow = value;
106 	}
107 
108 	timenow = localtime(&tnow);
109 
110 	mopPutChar (pkt,idx,10);
111 	mopPutChar (pkt,idx,(timenow->tm_year / 100) + 19);
112 	mopPutChar (pkt,idx,(timenow->tm_year % 100));
113 	mopPutChar (pkt,idx,(timenow->tm_mon + 1));
114 	mopPutChar (pkt,idx,(timenow->tm_mday));
115 	mopPutChar (pkt,idx,(timenow->tm_hour));
116 	mopPutChar (pkt,idx,(timenow->tm_min));
117 	mopPutChar (pkt,idx,(timenow->tm_sec));
118 	mopPutChar (pkt,idx,0x00);
119 	mopPutChar (pkt,idx,0x00);
120 	mopPutChar (pkt,idx,0x00);
121 }
122 
123 void
124 mopPutHeader(pkt, idx, dst, src, proto, trans)
125 	u_char  *pkt;
126 	int     *idx;
127 	u_char	 dst[], src[];
128 	u_short	 proto;
129 	int	 trans;
130 {
131 
132 	mopPutMulti(pkt, idx, dst, 6);
133 	mopPutMulti(pkt, idx, src, 6);
134 	if (trans == TRANS_8023) {
135 		mopPutShort(pkt, idx, 0);
136 		mopPutChar (pkt, idx, MOP_K_PROTO_802_DSAP);
137 		mopPutChar (pkt, idx, MOP_K_PROTO_802_SSAP);
138 		mopPutChar (pkt, idx, MOP_K_PROTO_802_CNTL);
139 		mopPutChar (pkt, idx, 0x08);
140 		mopPutChar (pkt, idx, 0x00);
141 		mopPutChar (pkt, idx, 0x2b);
142 	}
143 #if !defined(__FreeBSD__)
144 	mopPutChar(pkt, idx, (proto / 256));
145 	mopPutChar(pkt, idx, (proto % 256));
146 #else
147 	if (trans == TRANS_8023) {
148 		mopPutChar(pkt, idx, (proto / 256));
149 		mopPutChar(pkt, idx, (proto % 256));
150 	} else {
151 		mopPutChar(pkt, idx, (proto % 256));
152 		mopPutChar(pkt, idx, (proto / 256));
153 	}
154 #endif
155 	if (trans == TRANS_ETHER)
156 		mopPutShort(pkt, idx, 0);
157 
158 }
159 
160 void
161 mopPutLength(pkt, trans, len)
162 	u_char  *pkt;
163 	int	 trans;
164 	u_short	 len;
165 {
166 	int	 idx = 0;
167 
168 	switch(trans) {
169 	case TRANS_ETHER:
170 		idx = 14;
171 		mopPutChar(pkt, &idx, ((len - 16) % 256));
172 		mopPutChar(pkt, &idx, ((len - 16) / 256));
173 		break;
174 	case TRANS_8023:
175 		idx = 12;
176 #if !defined(__FreeBSD__)
177 		mopPutChar(pkt, &idx, ((len - 14) / 256));
178 		mopPutChar(pkt, &idx, ((len - 14) % 256));
179 #else
180 		mopPutChar(pkt, &idx, ((len - 14) % 256));
181 		mopPutChar(pkt, &idx, ((len - 14) / 256));
182 #endif
183 		break;
184 	}
185 
186 }
187