File: socket.h

package info (click to toggle)
ts10 0.8.021004-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,572 kB
  • ctags: 11,742
  • sloc: ansic: 68,289; makefile: 466
file content (142 lines) | stat: -rwxr-xr-x 5,030 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// socket.h - Socket definitions
//
// Written by
//  Timothy Stark <sword7@speakeasy.org>
//
// This file is part of the TS-10 Emulator.
// See README for copyright notice.
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stropts.h>
#include <errno.h>

// TUN/TAP connections
#include <linux/if_tun.h>

#define NET_MAXSOCKETS  256  // Maximum number of open sockets.
#define NET_MAXBUF      2048  // Manimum number of bytes of buffer.

typedef struct sockaddr    SOCKADDR;
typedef struct sockaddr_in SOCKADDRIN;

typedef struct Socket     SOCKET;
typedef struct SocketType SOCKTYPE;

struct SocketType {
	char *Name;
	// Callback functions
	void (*Accept)(SOCKET *);
	void (*Eof)(SOCKET *, int, int);
	void (*Process)(SOCKET *, char *, int);
	void (*Status)(SOCKET *);
};

struct Socket {
	// Internal variables
	SOCKET      *Next;    // Next Socket     (Forward)
	SOCKET      *Back;    // Previous Socket (Backward)
	char        *Name;    // Socket Name
	int         idSocket; // Socket ID (FD number)
	int         Flags;    // Socket Flags
	SOCKET      *Server;  // Parent socket.
	int         maxConns; // Maximum number of opened connections.
	int         nConns;   // Current number of opened connections.
	SOCKADDRIN  locAddr;  // Local Internet Address/Port
	SOCKADDRIN  remAddr;  // Remote Internet Address/Port
	SOCKTYPE    *Type;    // Socket Type

	// User-defined variables;
	void        *Device;  // User-defined Device
	int         uPort;    // User-defined Port/Line
	void        *uData;   // User-defined Data

	// Callback functions
	void (*Accept)(SOCKET *);
	void (*Eof)(SOCKET *, int, int);
	void (*Process)(SOCKET *, char *, int);
};

// Flag definitions for Socket variable
#define SCK_OPENED    0x80000000 // Socket is opened or closed.
#define SCK_SERVER    0x40000000 // Socket is server or client.
#define SCK_LISTEN    0x20000000 // Socket is listening.
#define SCK_CONNECT   0x10000000 // Socket is connecting (incoming).
#define SCK_SOCKET    0x08000000 // Socket is real socket.
#define SCK_STDIO     0x04000000 // Socket is standard I/O (TTY)
#define SCK_FILE      0x02000000 // Socket is file I/O
#define SCK_PACKET    0x01000000 // Socket is packet type.

// Socket mode defintions - Server, Client, or Connected.
#define NET_SERVER   1 // Socket's Server role
#define NET_CLIENT   2 // Socket's Client role
#define NET_CONNECT  3 // Incoming socket connection
#define NET_STDIO    4 // Standard I/O (TTY)
#define NET_FILE     5 // File I/O
#define NET_TUN      6 // TUN/TAP connection

// Socket error definitions
#define NET_OK            0 // Operation successful
#define NET_OPENERR       1 // Open Error
#define NET_BINDERR       2 // Bind Error
#define NET_FULLSOCKETS   3 // Sockets are full
#define NET_UNKNOWNMODE   4 // Unknown Mode
#define NET_SOCKERR       5 // Socket Error
#define NET_NOTVALID      6 // Not valid socket

// Telnet code definitions
#define TEL_SE    240 // End of subnegotiation parameters
#define TEL_NOP   241 // No operation.
#define TEL_DM    242 // Data Mark
#define TEL_BRK   243 // Break
#define TEL_IP    244 // Interrupt Process
#define TEL_AO    245 // Abort Output
#define TEL_AYT   246 // Are You There?
#define TEL_EC    247 // Erase Character
#define TEL_EL    248 // Erase Line
#define TEL_GA    249 // Go Ahead
#define TEL_SB    250 // Subnegotiation
#define TEL_WILL  251 // Will (Option code)
#define TEL_WONT  252 // Will Not (Option Code)
#define TEL_DO    253 // Do (Option Code)
#define TEL_DONT  254 // Don't (Option Code)
#define TEL_IAC   255 // Interpret as Command

// Protoype definitions
void   sock_Intialize(void);
void   sock_Cleanup(void);
#ifdef DEBUG
void   sock_Dump(int, uchar *, int, char *);
#endif /* DEBUG */
SOCKET *sock_Open(char *, int, int);
int    sock_Close(SOCKET *);
void   sock_CloseAll(char *);
int    sock_Listen(SOCKET *, int);
SOCKET *sock_Accept(SOCKET *);
int    sock_Send(int, char *, int);
int    sock_Print(SOCKET *, char *, int);
void   SockGetEtherAddr(char *, uint8 *);
int    SockSendPacket(SOCKET *, uint8 *, uint32);
int    SockPrintf(SOCKET *, cchar *, ...);
int    sock_ProcessTelnet(uchar *, int);
void   SocketHandler(int);
void   sock_ShowList(void);