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