xref: /netbsd-src/usr.sbin/mopd/common/put.c (revision 07ce40632e7aec9e3c738f3250b39f23d21c5437)
1 /*	$NetBSD: put.c,v 1.5 2009/10/20 00:51:13 snj 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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 #ifndef lint
29 __RCSID("$NetBSD: put.c,v 1.5 2009/10/20 00:51:13 snj Exp $");
30 #endif
31 
32 #include "os.h"
33 #include "mopdef.h"
34 #include "put.h"
35 
36 void
37 mopPutChar(pkt, idx, value)
38 	u_char  *pkt;
39 	int     *idx;
40 	u_char   value;
41 {
42 	pkt[*idx] = value;
43 	*idx = *idx + 1;
44 }
45 
46 void
47 mopPutShort(pkt, idx, value)
48 	u_char  *pkt;
49 	int     *idx;
50 	u_short  value;
51 {
52         int i;
53 	for (i = 0; i < 2; i++) {
54 	  pkt[*idx+i] = value % 256;
55 	  value = value / 256;
56 	}
57 	*idx = *idx + 2;
58 }
59 
60 void
61 mopPutLong(pkt, idx, value)
62 	u_char	       *pkt;
63 	int	       *idx;
64 	u_int32_t	value;
65 {
66         int i;
67 	for (i = 0; i < 4; i++) {
68 	  pkt[*idx+i] = value % 256;
69 	  value = value / 256;
70 	}
71 	*idx = *idx + 4;
72 }
73 
74 void
75 mopPutMulti(pkt, idx, value, size)
76 	u_char *pkt;
77 	int    *idx,size;
78 	const u_char *value;
79 {
80 	int i;
81 
82 	for (i = 0; i < size; i++) {
83 	  pkt[*idx+i] = value[i];
84 	}
85 	*idx = *idx + size;
86 }
87 
88 void
89 mopPutTime(pkt, idx, value)
90 	u_char *pkt;
91 	int    *idx;
92 	time_t	value;
93 {
94 	time_t tnow;
95 	struct tm *timenow;
96 
97 	if ((value == 0)) {
98 	  tnow = time(NULL);
99 	} else {
100 	  tnow = value;
101 	}
102 
103 	timenow = localtime(&tnow);
104 
105 	mopPutChar (pkt,idx,10);
106 	mopPutChar (pkt,idx,(timenow->tm_year / 100) + 19);
107 	mopPutChar (pkt,idx,(timenow->tm_year % 100));
108 	mopPutChar (pkt,idx,(timenow->tm_mon + 1));
109 	mopPutChar (pkt,idx,(timenow->tm_mday));
110 	mopPutChar (pkt,idx,(timenow->tm_hour));
111 	mopPutChar (pkt,idx,(timenow->tm_min));
112 	mopPutChar (pkt,idx,(timenow->tm_sec));
113 	mopPutChar (pkt,idx,0x00);
114 	mopPutChar (pkt,idx,0x00);
115 	mopPutChar (pkt,idx,0x00);
116 }
117 
118 void
119 mopPutHeader(pkt, idx, dst, src, proto, trans)
120 	u_char  *pkt;
121 	int     *idx;
122 	u_char	 dst[], src[];
123 	u_short	 proto;
124 	int	 trans;
125 {
126 
127 	mopPutMulti(pkt, idx, dst, 6);
128 	mopPutMulti(pkt, idx, src, 6);
129 	if (trans == TRANS_8023) {
130 		mopPutShort(pkt, idx, 0);
131 		mopPutChar (pkt, idx, MOP_K_PROTO_802_DSAP);
132 		mopPutChar (pkt, idx, MOP_K_PROTO_802_SSAP);
133 		mopPutChar (pkt, idx, MOP_K_PROTO_802_CNTL);
134 		mopPutChar (pkt, idx, 0x08);
135 		mopPutChar (pkt, idx, 0x00);
136 		mopPutChar (pkt, idx, 0x2b);
137 	}
138 #if !defined(__FreeBSD__)
139 	mopPutChar(pkt, idx, (proto / 256));
140 	mopPutChar(pkt, idx, (proto % 256));
141 #else
142 	if (trans == TRANS_8023) {
143 		mopPutChar(pkt, idx, (proto / 256));
144 		mopPutChar(pkt, idx, (proto % 256));
145 	} else {
146 		mopPutChar(pkt, idx, (proto % 256));
147 		mopPutChar(pkt, idx, (proto / 256));
148 	}
149 #endif
150 	if (trans == TRANS_ETHER)
151 		mopPutShort(pkt, idx, 0);
152 
153 }
154 
155 void
156 mopPutLength(pkt, trans, len)
157 	u_char  *pkt;
158 	int	 trans;
159 	u_short	 len;
160 {
161 	int	 idx = 0;
162 
163 	switch(trans) {
164 	case TRANS_ETHER:
165 		idx = 14;
166 		mopPutChar(pkt, &idx, ((len - 16) % 256));
167 		mopPutChar(pkt, &idx, ((len - 16) / 256));
168 		break;
169 	case TRANS_8023:
170 		idx = 12;
171 #if !defined(__FreeBSD__)
172 		mopPutChar(pkt, &idx, ((len - 14) / 256));
173 		mopPutChar(pkt, &idx, ((len - 14) % 256));
174 #else
175 		mopPutChar(pkt, &idx, ((len - 14) % 256));
176 		mopPutChar(pkt, &idx, ((len - 14) / 256));
177 #endif
178 		break;
179 	}
180 
181 }
182