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