xref: /openbsd-src/usr.sbin/mopd/common/put.c (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 /*	$OpenBSD: put.c,v 1.7 2006/04/17 13:17:07 maja Exp $ */
2 
3 /*
4  * Copyright (c) 1993-2006 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 #ifndef lint
28 static const char rcsid[] =
29     "$OpenBSD: put.c,v 1.7 2006/04/17 13:17:07 maja Exp $";
30 #endif
31 
32 #include <stddef.h>
33 #include <sys/types.h>
34 #include <time.h>
35 #include "common/mopdef.h"
36 
37 void
38 mopPutChar(u_char *pkt, int *idx, u_char value)
39 {
40 	pkt[*idx] = value;
41 	(*idx)++;
42 }
43 
44 void
45 mopPutShort(u_char *pkt, int *idx, u_short value)
46 {
47 	pkt[*idx] = value % 256;
48 	pkt[*idx+1] = value / 256;
49 	*idx += 2;
50 }
51 
52 void
53 mopPutNShort(u_char *pkt, int *idx, u_short value)
54 {
55 	pkt[*idx] = value / 256;
56 	pkt[*idx+1] = value % 256;
57 	*idx += 2;
58 }
59 
60 void
61 mopPutLong(u_char *pkt, int *idx, u_long value)
62 {
63 	int i;
64 
65 	for (i = 0; i < 4; i++) {
66 		pkt[*idx+i] = (u_char)(value % 256);
67 		value /= 256;
68 	}
69 	*idx += 4;
70 }
71 
72 void
73 mopPutMulti(u_char *pkt, int *idx, u_char *value, int size)
74 {
75 	int i;
76 
77 	for (i = 0; i < size; i++)
78 		pkt[*idx+i] = value[i];
79 	*idx += size;
80 }
81 
82 void
83 mopPutTime(u_char *pkt, int *idx, time_t value)
84 {
85 	time_t		 tnow;
86 	struct tm	*timenow;
87 
88 	if ((value == 0))
89 		tnow = time(NULL);
90 	else
91 		tnow = value;
92 
93 	timenow = localtime(&tnow);
94 
95 	mopPutChar(pkt, idx, 10);
96 	mopPutChar(pkt, idx, (timenow->tm_year / 100) + 19);
97 	mopPutChar(pkt, idx, (timenow->tm_year % 100));
98 	mopPutChar(pkt, idx, (timenow->tm_mon + 1));
99 	mopPutChar(pkt, idx, (timenow->tm_mday));
100 	mopPutChar(pkt, idx, (timenow->tm_hour));
101 	mopPutChar(pkt, idx, (timenow->tm_min));
102 	mopPutChar(pkt, idx, (timenow->tm_sec));
103 	mopPutChar(pkt, idx, 0x00);
104 	mopPutChar(pkt, idx, 0x00);
105 	mopPutChar(pkt, idx, 0x00);
106 }
107 
108 void
109 mopPutHeader(u_char *pkt, int *idx, u_char *dst, u_char *src, u_short proto,
110     int trans)
111 {
112 	mopPutMulti(pkt, idx, dst, 6);
113 	mopPutMulti(pkt, idx, src, 6);
114 	if (trans == TRANS_8023) {
115 		mopPutShort(pkt, idx, 0);
116 		mopPutChar(pkt, idx, MOP_K_PROTO_802_DSAP);
117 		mopPutChar(pkt, idx, MOP_K_PROTO_802_SSAP);
118 		mopPutChar(pkt, idx, MOP_K_PROTO_802_CNTL);
119 		mopPutChar(pkt, idx, 0x08);
120 		mopPutChar(pkt, idx, 0x00);
121 		mopPutChar(pkt, idx, 0x2b);
122 	}
123 #if !defined(__FreeBSD__)
124 	mopPutNShort(pkt, idx, proto);
125 #else
126 	if (trans == TRANS_8023) {
127 		mopPutNShort(pkt, idx, proto);
128 	} else {
129 		mopPutShort(pkt, idx, proto);
130 	}
131 #endif
132 	if (trans == TRANS_ETHER)
133 		mopPutShort(pkt, idx, 0);
134 
135 }
136 
137 void
138 mopPutLength(u_char *pkt, int trans, u_short len)
139 {
140 	int	 idx;
141 
142 	switch (trans) {
143 	case TRANS_ETHER:
144 		idx = 14;
145 		mopPutShort(pkt, &idx, len-16);
146 		break;
147 	case TRANS_8023:
148 		idx = 12;
149 #if !defined(__FreeBSD__)
150 		mopPutNShort(pkt, &idx, len-14);
151 #else
152 		mopPutShort(pkt, &idx, len-14);
153 #endif
154 		break;
155 	}
156 }
157