OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [tcl/] [generic/] [tclIOSock.c] - Blame information for rev 1774

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
/*
2
 * tclIOSock.c --
3
 *
4
 *      Common routines used by all socket based channel types.
5
 *
6
 * Copyright (c) 1995 Sun Microsystems, Inc.
7
 *
8
 * See the file "license.terms" for information on usage and redistribution
9
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
10
 *
11
 * RCS: @(#) $Id: tclIOSock.c,v 1.1.1.1 2002-01-16 10:25:27 markom Exp $
12
 */
13
 
14
#include "tclInt.h"
15
#include "tclPort.h"
16
 
17
/*
18
 *----------------------------------------------------------------------
19
 *
20
 * TclSockGetPort --
21
 *
22
 *      Maps from a string, which could be a service name, to a port.
23
 *      Used by socket creation code to get port numbers and resolve
24
 *      registered service names to port numbers.
25
 *
26
 * Results:
27
 *      A standard Tcl result.  On success, the port number is
28
 *      returned in portPtr. On failure, an error message is left in
29
 *      interp->result.
30
 *
31
 * Side effects:
32
 *      None.
33
 *
34
 *----------------------------------------------------------------------
35
 */
36
 
37
int
38
TclSockGetPort(interp, string, proto, portPtr)
39
    Tcl_Interp *interp;
40
    char *string;               /* Integer or service name */
41
    char *proto;                /* "tcp" or "udp", typically */
42
    int *portPtr;               /* Return port number */
43
{
44
    struct servent *sp;         /* Protocol info for named services */
45
    if (Tcl_GetInt(interp, string, portPtr) != TCL_OK) {
46
        sp = getservbyname(string, proto);
47
        if (sp != NULL) {
48
            *portPtr = ntohs((unsigned short) sp->s_port);
49
            Tcl_ResetResult(interp);    /* clear error message */
50
            return TCL_OK;
51
        }
52
        return TCL_ERROR;
53
    }
54
    if (Tcl_GetInt(interp, string, portPtr) != TCL_OK) {
55
        return TCL_ERROR;
56
    }
57
    if (*portPtr > 0xFFFF) {
58
        Tcl_AppendResult(interp, "couldn't open socket: port number too high",
59
                (char *) NULL);
60
        return TCL_ERROR;
61
    }
62
    return TCL_OK;
63
}
64
 
65
/*
66
 *----------------------------------------------------------------------
67
 *
68
 * TclSockMinimumBuffers --
69
 *
70
 *      Ensure minimum buffer sizes (non zero).
71
 *
72
 * Results:
73
 *      A standard Tcl result.
74
 *
75
 * Side effects:
76
 *      Sets SO_SNDBUF and SO_RCVBUF sizes.
77
 *
78
 *----------------------------------------------------------------------
79
 */
80
 
81
int
82
TclSockMinimumBuffers(sock, size)
83
    int sock;                   /* Socket file descriptor */
84
    int size;                   /* Minimum buffer size */
85
{
86
    int current;
87
    int len;
88
 
89
    len = sizeof(int);
90
    getsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)&current, &len);
91
    if (current < size) {
92
        len = sizeof(int);
93
        setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)&size, len);
94
    }
95
    len = sizeof(int);
96
    getsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&current, &len);
97
    if (current < size) {
98
        len = sizeof(int);
99
        setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&size, len);
100
    }
101
    return TCL_OK;
102
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.