155714Skris/* crypto/bio/bss_rtcp.c */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 8280304Sjkim * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15280304Sjkim * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 22280304Sjkim * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 37280304Sjkim * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40280304Sjkim * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 52280304Sjkim * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 5855714Skris 59280304Sjkim/*- 60280304Sjkim * Written by David L. Jones <jonesd@kcgl1.eng.ohio-state.edu> 6155714Skris * Date: 22-JUL-1996 62280304Sjkim * Revised: 25-SEP-1997 Update for 0.8.1, BIO_CTRL_SET -> BIO_C_SET_FD 6355714Skris */ 6455714Skris/* VMS */ 6555714Skris#include <stdio.h> 6655714Skris#include <stdlib.h> 6755714Skris#include <string.h> 6855714Skris#include <errno.h> 6955714Skris#include "cryptlib.h" 7055714Skris#include <openssl/bio.h> 7155714Skris 72280304Sjkim#include <iodef.h> /* VMS IO$_ definitions */ 7355714Skris#include <starlet.h> 7455714Skris 7555714Skristypedef unsigned short io_channel; 7655714Skris/*************************************************************************/ 77280304Sjkimstruct io_status { 78280304Sjkim short status, count; 79280304Sjkim long flags; 80280304Sjkim}; 8155714Skris 82280304Sjkim/* Should have member alignment inhibited */ 83280304Sjkimstruct rpc_msg { 84280304Sjkim /* 'A'-app data. 'R'-remote client 'G'-global */ 85280304Sjkim char channel; 86280304Sjkim /* 'G'-get, 'P'-put, 'C'-confirm, 'X'-close */ 87280304Sjkim char function; 88280304Sjkim /* Amount of data returned or max to return */ 89280304Sjkim unsigned short int length; 90280304Sjkim /* variable data */ 91280304Sjkim char data[4092]; 9255714Skris}; 9355714Skris#define RPC_HDR_SIZE (sizeof(struct rpc_msg) - 4092) 9455714Skris 9555714Skrisstruct rpc_ctx { 9655714Skris int filled, pos; 9755714Skris struct rpc_msg msg; 9855714Skris}; 9955714Skris 100280304Sjkimstatic int rtcp_write(BIO *h, const char *buf, int num); 101280304Sjkimstatic int rtcp_read(BIO *h, char *buf, int size); 102280304Sjkimstatic int rtcp_puts(BIO *h, const char *str); 103280304Sjkimstatic int rtcp_gets(BIO *h, char *str, int size); 104280304Sjkimstatic long rtcp_ctrl(BIO *h, int cmd, long arg1, void *arg2); 10555714Skrisstatic int rtcp_new(BIO *h); 10655714Skrisstatic int rtcp_free(BIO *data); 10755714Skris 108280304Sjkimstatic BIO_METHOD rtcp_method = { 109280304Sjkim BIO_TYPE_FD, 110280304Sjkim "RTCP", 111280304Sjkim rtcp_write, 112280304Sjkim rtcp_read, 113280304Sjkim rtcp_puts, 114280304Sjkim rtcp_gets, 115280304Sjkim rtcp_ctrl, 116280304Sjkim rtcp_new, 117280304Sjkim rtcp_free, 118280304Sjkim NULL, 119280304Sjkim}; 12055714Skris 12155714SkrisBIO_METHOD *BIO_s_rtcp(void) 122280304Sjkim{ 123280304Sjkim return (&rtcp_method); 124280304Sjkim} 125280304Sjkim 12655714Skris/*****************************************************************************/ 127280304Sjkim/* 128280304Sjkim * Decnet I/O routines. 12955714Skris */ 13055714Skris 13155714Skris#ifdef __DECC 132280304Sjkim# pragma message save 133280304Sjkim# pragma message disable DOLLARID 13455714Skris#endif 13555714Skris 136280304Sjkimstatic int get(io_channel chan, char *buffer, int maxlen, int *length) 13755714Skris{ 13855714Skris int status; 13955714Skris struct io_status iosb; 140280304Sjkim status = sys$qiow(0, chan, IO$_READVBLK, &iosb, 0, 0, 141280304Sjkim buffer, maxlen, 0, 0, 0, 0); 142280304Sjkim if ((status & 1) == 1) 143280304Sjkim status = iosb.status; 144280304Sjkim if ((status & 1) == 1) 145280304Sjkim *length = iosb.count; 14655714Skris return status; 14755714Skris} 14855714Skris 149280304Sjkimstatic int put(io_channel chan, char *buffer, int length) 15055714Skris{ 15155714Skris int status; 15255714Skris struct io_status iosb; 153280304Sjkim status = sys$qiow(0, chan, IO$_WRITEVBLK, &iosb, 0, 0, 154280304Sjkim buffer, length, 0, 0, 0, 0); 155280304Sjkim if ((status & 1) == 1) 156280304Sjkim status = iosb.status; 15755714Skris return status; 15855714Skris} 15955714Skris 16055714Skris#ifdef __DECC 161280304Sjkim# pragma message restore 16255714Skris#endif 16355714Skris 16455714Skris/***************************************************************************/ 16555714Skris 16655714Skrisstatic int rtcp_new(BIO *bi) 16755714Skris{ 16855714Skris struct rpc_ctx *ctx; 169280304Sjkim bi->init = 1; 170280304Sjkim bi->num = 0; 171280304Sjkim bi->flags = 0; 172280304Sjkim bi->ptr = OPENSSL_malloc(sizeof(struct rpc_ctx)); 173280304Sjkim ctx = (struct rpc_ctx *)bi->ptr; 174280304Sjkim ctx->filled = 0; 175280304Sjkim ctx->pos = 0; 176280304Sjkim return (1); 17755714Skris} 17855714Skris 17955714Skrisstatic int rtcp_free(BIO *a) 18055714Skris{ 181280304Sjkim if (a == NULL) 182280304Sjkim return (0); 183280304Sjkim if (a->ptr) 184280304Sjkim OPENSSL_free(a->ptr); 185280304Sjkim a->ptr = NULL; 186280304Sjkim return (1); 18755714Skris} 188280304Sjkim 18955714Skrisstatic int rtcp_read(BIO *b, char *out, int outl) 19055714Skris{ 19155714Skris int status, length; 19255714Skris struct rpc_ctx *ctx; 19355714Skris /* 19455714Skris * read data, return existing. 19555714Skris */ 196280304Sjkim ctx = (struct rpc_ctx *)b->ptr; 197280304Sjkim if (ctx->pos < ctx->filled) { 198280304Sjkim length = ctx->filled - ctx->pos; 199280304Sjkim if (length > outl) 200280304Sjkim length = outl; 201280304Sjkim memmove(out, &ctx->msg.data[ctx->pos], length); 202280304Sjkim ctx->pos += length; 203280304Sjkim return length; 20455714Skris } 20555714Skris /* 20655714Skris * Requst more data from R channel. 20755714Skris */ 20855714Skris ctx->msg.channel = 'R'; 20955714Skris ctx->msg.function = 'G'; 21055714Skris ctx->msg.length = sizeof(ctx->msg.data); 211280304Sjkim status = put(b->num, (char *)&ctx->msg, RPC_HDR_SIZE); 212280304Sjkim if ((status & 1) == 0) { 213280304Sjkim return -1; 21455714Skris } 21555714Skris /* 21655714Skris * Read. 21755714Skris */ 21855714Skris ctx->pos = ctx->filled = 0; 219280304Sjkim status = get(b->num, (char *)&ctx->msg, sizeof(ctx->msg), &length); 220280304Sjkim if ((status & 1) == 0) 221280304Sjkim length = -1; 222280304Sjkim if (ctx->msg.channel != 'R' || ctx->msg.function != 'C') { 223280304Sjkim length = -1; 22455714Skris } 22555714Skris ctx->filled = length - RPC_HDR_SIZE; 226280304Sjkim 227280304Sjkim if (ctx->pos < ctx->filled) { 228280304Sjkim length = ctx->filled - ctx->pos; 229280304Sjkim if (length > outl) 230280304Sjkim length = outl; 231280304Sjkim memmove(out, ctx->msg.data, length); 232280304Sjkim ctx->pos += length; 233280304Sjkim return length; 23455714Skris } 23555714Skris 23655714Skris return length; 23755714Skris} 23855714Skris 23968651Skrisstatic int rtcp_write(BIO *b, const char *in, int inl) 24055714Skris{ 24155714Skris int status, i, segment, length; 24255714Skris struct rpc_ctx *ctx; 24355714Skris /* 24455714Skris * Output data, send in chunks no larger that sizeof(ctx->msg.data). 24555714Skris */ 246280304Sjkim ctx = (struct rpc_ctx *)b->ptr; 247280304Sjkim for (i = 0; i < inl; i += segment) { 248280304Sjkim segment = inl - i; 249280304Sjkim if (segment > sizeof(ctx->msg.data)) 250280304Sjkim segment = sizeof(ctx->msg.data); 251280304Sjkim ctx->msg.channel = 'R'; 252280304Sjkim ctx->msg.function = 'P'; 253280304Sjkim ctx->msg.length = segment; 254280304Sjkim memmove(ctx->msg.data, &in[i], segment); 255280304Sjkim status = put(b->num, (char *)&ctx->msg, segment + RPC_HDR_SIZE); 256280304Sjkim if ((status & 1) == 0) { 257280304Sjkim i = -1; 258280304Sjkim break; 259280304Sjkim } 26055714Skris 261280304Sjkim status = get(b->num, (char *)&ctx->msg, sizeof(ctx->msg), &length); 262280304Sjkim if (((status & 1) == 0) || (length < RPC_HDR_SIZE)) { 263280304Sjkim i = -1; 264280304Sjkim break; 265280304Sjkim } 266280304Sjkim if ((ctx->msg.channel != 'R') || (ctx->msg.function != 'C')) { 267280304Sjkim printf("unexpected response when confirming put %c %c\n", 268280304Sjkim ctx->msg.channel, ctx->msg.function); 26955714Skris 270280304Sjkim } 27155714Skris } 272280304Sjkim return (i); 27355714Skris} 27455714Skris 27568651Skrisstatic long rtcp_ctrl(BIO *b, int cmd, long num, void *ptr) 276280304Sjkim{ 277280304Sjkim long ret = 1; 27855714Skris 279280304Sjkim switch (cmd) { 280280304Sjkim case BIO_CTRL_RESET: 281280304Sjkim case BIO_CTRL_EOF: 282280304Sjkim ret = 1; 283280304Sjkim break; 284280304Sjkim case BIO_C_SET_FD: 285280304Sjkim b->num = num; 286280304Sjkim ret = 1; 287280304Sjkim break; 288280304Sjkim case BIO_CTRL_SET_CLOSE: 289280304Sjkim case BIO_CTRL_FLUSH: 290280304Sjkim case BIO_CTRL_DUP: 291280304Sjkim ret = 1; 292280304Sjkim break; 293280304Sjkim case BIO_CTRL_GET_CLOSE: 294280304Sjkim case BIO_CTRL_INFO: 295280304Sjkim case BIO_CTRL_GET: 296280304Sjkim case BIO_CTRL_PENDING: 297280304Sjkim case BIO_CTRL_WPENDING: 298280304Sjkim default: 299280304Sjkim ret = 0; 300280304Sjkim break; 301280304Sjkim } 302280304Sjkim return (ret); 303280304Sjkim} 30455714Skris 30555714Skrisstatic int rtcp_gets(BIO *bp, char *buf, int size) 306280304Sjkim{ 307280304Sjkim return (0); 308280304Sjkim} 30955714Skris 31068651Skrisstatic int rtcp_puts(BIO *bp, const char *str) 31155714Skris{ 31255714Skris int length; 313280304Sjkim if (str == NULL) 314280304Sjkim return (0); 315280304Sjkim length = strlen(str); 316280304Sjkim if (length == 0) 317280304Sjkim return (0); 318280304Sjkim return rtcp_write(bp, str, length); 31955714Skris} 320