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