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