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

Subversion Repositories or1k

[/] [or1k/] [tags/] [stable_0_2_0_rc2/] [or1ksim/] [peripheral/] [channels/] [tcp.c] - Blame information for rev 1557

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

Line No. Rev Author Line
1 1118 sfurman
/* tcp.c -- Definition of functions for peripheral to
2
 * communicate with host via a tcp socket.
3
 
4
   Copyright (C) 2002 Richard Prescott <rip@step.polymtl.ca>
5
 
6
This file is part of OpenRISC 1000 Architectural Simulator.
7
 
8
This program is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 2 of the License, or
11
(at your option) any later version.
12
 
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
GNU General Public License for more details.
17
 
18
You should have received a copy of the GNU General Public License
19
along with this program; if not, write to the Free Software
20
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21
 
22
 
23
#include <stdio.h>
24
#include <sys/types.h>
25
#include <sys/socket.h>
26
#include <netinet/in.h>
27
#include <fcntl.h>
28
#include <errno.h>
29 1308 phoenix
#include <stdlib.h>
30
#include <string.h>
31
#include <unistd.h>
32 1118 sfurman
 
33
#include "channel.h"
34
#include "generic.h"
35
#include "fd.h"
36
 
37
struct tcp_channel
38
{
39
        struct fd_channel fds;
40
        int socket_fd;                          /* Socket to listen to */
41
        int port_number;                        /* TCP port number */
42
        int connected;                          /* If 0, no remote endpoint yet */
43
        int nonblocking;                        /* If 0, read/write will block until
44
                                                                   remote client connects */
45
};
46
 
47
static void * tcp_init(const char * input)
48
{
49
        int port_number, fd, flags;
50
        struct sockaddr_in local_ip;
51
        struct tcp_channel* channel = (struct tcp_channel*)malloc(sizeof(struct tcp_channel));
52
        if (!channel)
53
                return NULL;
54
 
55
        fd = 0;
56
        channel->nonblocking = 1;
57
        channel->fds.fdin = -1;
58
        channel->fds.fdout = -1;
59
        channel->socket_fd = -1;
60
        channel->port_number = -1;
61
 
62
        port_number = atoi(input);
63
        if (port_number == 0)
64
                goto error;
65
 
66
        fd = socket(AF_INET, SOCK_STREAM, 0);
67
        if (fd < 0)
68
                goto error;
69
 
70
        flags = 1;
71
        if(setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(const char*)&flags,sizeof(int)) < 0)
72
    {
73
                perror("Can not set SO_REUSEADDR option on channel socket");
74
                goto error;
75
        }
76
 
77
        memset(&local_ip, 0, sizeof(local_ip));
78
        local_ip.sin_family = AF_INET;
79
        local_ip.sin_addr.s_addr = htonl(INADDR_ANY);
80
        local_ip.sin_port = htons(port_number);
81
        if (bind(fd, (struct sockaddr*)&local_ip, sizeof(local_ip)) < 0) {
82
                perror("Can't bind local address");
83
                goto error;
84
        }
85
 
86
        if (channel->nonblocking) {
87
                if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
88
                {
89
                        perror("Can not make channel socket non-blocking");
90
                        goto error;
91
                }
92
        }
93
 
94
        if (listen(fd, 1) < 0)
95
                goto error;
96
 
97
        channel->socket_fd = fd;
98
        channel->port_number = port_number;
99
        channel->connected = 0;
100
        return (void*)channel;
101
 
102
error:
103
        if (fd)
104
                close(fd);
105
        free(channel);
106
        return NULL;
107
}
108
 
109
static int tcp_open(void * data)
110
{
111
        /* Socket is opened lazily, upon first read or write, so do nothing here */
112
        return 0;
113
}
114
 
115
 
116
static int wait_for_tcp_connect(struct tcp_channel* channel)
117
{
118 1557 nogj
        int fd;
119
        socklen_t sizeof_remote_ip;
120 1118 sfurman
        struct sockaddr_in remote_ip;
121
 
122
        sizeof_remote_ip = sizeof(remote_ip);
123
        fd = accept(channel->socket_fd, (struct sockaddr*)&remote_ip, &sizeof_remote_ip);
124
        if (fd < 0) {
125
                if (channel->nonblocking) {
126
                        /* Not an error if there is not yet a remote connection - try again later */
127
                        if (errno == EAGAIN)
128
                                return 0;
129
                }
130
                perror("Couldn't accept connection");
131
                return -1;
132
        }
133
 
134
        channel->fds.fdin = channel->fds.fdout = fd;
135
        close(channel->socket_fd);
136
        channel->socket_fd = -1;
137
    channel->connected = 1;
138
        return 1;
139
}
140
 
141
static int tcp_read(void * data, char * buffer, int size)
142
{
143
        struct tcp_channel* channel = data;
144
 
145
        /* Lazily connect to tcp partner on read/write */
146
        if (!channel->connected) {
147
                int retval = wait_for_tcp_connect(data);
148
                if (retval <= 0)
149
                        return retval;
150
        }
151
        return fd_read(data, buffer, size);
152
}
153
 
154
static int tcp_write(void * data, const char * buffer, int size)
155
{
156
        struct tcp_channel* channel = data;
157
 
158
        /* Lazily connect to tcp partner on read/write */
159
        if (!channel->connected) {
160
                int retval = wait_for_tcp_connect(data);
161
                if (retval < 0)
162
                        return retval;
163
        }
164
        return fd_write(data, buffer, size);
165
}
166
 
167
struct channel_ops tcp_channel_ops =
168
{
169
        init:   tcp_init,
170
        open:   tcp_open,
171
        close:  generic_close,
172
        read:   tcp_read,
173
        write:  tcp_write,
174
        free:   generic_free,
175
};
176
 
177
/*
178
 * Local variables:
179
 * c-file-style: "linux"
180
 * End:
181
 */
182
 

powered by: WebSVN 2.1.0

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