155682Smarkm/*
2178825Sdfr * Copyright (c) 1997 - 2004 Kungliga Tekniska H�gskolan
3178825Sdfr * (Royal Institute of Technology, Stockholm, Sweden).
4178825Sdfr * All rights reserved.
555682Smarkm *
6178825Sdfr * Redistribution and use in source and binary forms, with or without
7178825Sdfr * modification, are permitted provided that the following conditions
8178825Sdfr * are met:
955682Smarkm *
10178825Sdfr * 1. Redistributions of source code must retain the above copyright
11178825Sdfr *    notice, this list of conditions and the following disclaimer.
1255682Smarkm *
13178825Sdfr * 2. Redistributions in binary form must reproduce the above copyright
14178825Sdfr *    notice, this list of conditions and the following disclaimer in the
15178825Sdfr *    documentation and/or other materials provided with the distribution.
1655682Smarkm *
17178825Sdfr * 3. Neither the name of the Institute nor the names of its contributors
18178825Sdfr *    may be used to endorse or promote products derived from this software
19178825Sdfr *    without specific prior written permission.
2055682Smarkm *
21178825Sdfr * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22178825Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23178825Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24178825Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25178825Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26178825Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27178825Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28178825Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29178825Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30178825Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31178825Sdfr * SUCH DAMAGE.
3255682Smarkm */
3355682Smarkm
3455682Smarkm#include "krb5_locl.h"
35102644Snectar#include "store-int.h"
3655682Smarkm
37178825SdfrRCSID("$Id: store_fd.c 17779 2006-06-30 21:23:19Z lha $");
3855682Smarkm
39178825Sdfrtypedef struct fd_storage {
4055682Smarkm    int fd;
41178825Sdfr} fd_storage;
4255682Smarkm
4355682Smarkm#define FD(S) (((fd_storage*)(S)->data)->fd)
4455682Smarkm
4555682Smarkmstatic ssize_t
46178825Sdfrfd_fetch(krb5_storage * sp, void *data, size_t size)
4755682Smarkm{
4872445Sassar    return net_read(FD(sp), data, size);
4955682Smarkm}
5055682Smarkm
5155682Smarkmstatic ssize_t
52178825Sdfrfd_store(krb5_storage * sp, const void *data, size_t size)
5355682Smarkm{
5472445Sassar    return net_write(FD(sp), data, size);
5555682Smarkm}
5655682Smarkm
5755682Smarkmstatic off_t
58178825Sdfrfd_seek(krb5_storage * sp, off_t offset, int whence)
5955682Smarkm{
6055682Smarkm    return lseek(FD(sp), offset, whence);
6155682Smarkm}
6255682Smarkm
63178825Sdfrstatic void
64178825Sdfrfd_free(krb5_storage * sp)
65178825Sdfr{
66178825Sdfr    close(FD(sp));
67178825Sdfr}
68178825Sdfr
69178825Sdfrkrb5_storage * KRB5_LIB_FUNCTION
7055682Smarkmkrb5_storage_from_fd(int fd)
7155682Smarkm{
72178825Sdfr    krb5_storage *sp;
7390926Snectar
74178825Sdfr    fd = dup(fd);
75178825Sdfr    if (fd < 0)
7690926Snectar	return NULL;
7790926Snectar
78178825Sdfr    sp = malloc(sizeof(krb5_storage));
79178825Sdfr    if (sp == NULL) {
80178825Sdfr	close(fd);
81178825Sdfr	return NULL;
82178825Sdfr    }
83178825Sdfr
8455682Smarkm    sp->data = malloc(sizeof(fd_storage));
8590926Snectar    if (sp->data == NULL) {
86178825Sdfr	close(fd);
8790926Snectar	free(sp);
8890926Snectar	return NULL;
8990926Snectar    }
9055682Smarkm    sp->flags = 0;
91102644Snectar    sp->eof_code = HEIM_ERR_EOF;
9255682Smarkm    FD(sp) = fd;
9355682Smarkm    sp->fetch = fd_fetch;
9455682Smarkm    sp->store = fd_store;
9555682Smarkm    sp->seek = fd_seek;
96178825Sdfr    sp->free = fd_free;
9755682Smarkm    return sp;
9855682Smarkm}
99