1 | #include "sock.hpp" |
---|
2 | |
---|
3 | |
---|
4 | #include <stdio.h> |
---|
5 | #include <stdlib.h> |
---|
6 | #include <fcntl.h> |
---|
7 | #include <unistd.h> |
---|
8 | #include <string.h> |
---|
9 | |
---|
10 | #include "OpenTransport.h" |
---|
11 | #include "OpenTptInternet.h" |
---|
12 | |
---|
13 | //extern fd_set master_set,master_write_set,read_set,exception_set,write_set; |
---|
14 | |
---|
15 | class ot_ip_address : public net_address |
---|
16 | { |
---|
17 | public : |
---|
18 | InetAddress addr; |
---|
19 | |
---|
20 | virtual protocol protocol_type() { return net_address::IP; } |
---|
21 | virtual equal(net_address *who) |
---|
22 | { |
---|
23 | if (who->protocol_type()==IP && |
---|
24 | !memcmp(&addr.fHost,& ((ot_ip_address *)who)->addr.fHost,sizeof(addr.fHost))) |
---|
25 | return 1; |
---|
26 | else return 0; |
---|
27 | } |
---|
28 | virtual int set_port(int port) { addr.fPort=port; return port; } |
---|
29 | ot_ip_address(InetAddress *Addr) { memcpy(&addr,Addr,sizeof(addr)); } |
---|
30 | virtual void print() |
---|
31 | { |
---|
32 | unsigned char *c=(unsigned char *) (&addr.fHost); |
---|
33 | fprintf(stderr,"%d.%d.%d.%d",c[0],c[1],c[2],c[3]); |
---|
34 | } |
---|
35 | int get_port() { return addr.fPort; } |
---|
36 | net_address *copy() { return new ot_ip_address(&addr); } |
---|
37 | ot_ip_address() {} ; |
---|
38 | void store_string(char *st, int st_length) |
---|
39 | { |
---|
40 | char buf[100]; |
---|
41 | unsigned char *c=(unsigned char *) (&addr.fHost); |
---|
42 | sprintf(buf,"%d.%d.%d.%d:%d",c[0],c[1],c[2],c[3],addr.fPort); |
---|
43 | strncpy(st,buf,st_length); |
---|
44 | st[st_length-1]=0; |
---|
45 | } |
---|
46 | }; |
---|
47 | |
---|
48 | class ot_tcpip_protocol : public net_protocol |
---|
49 | { |
---|
50 | protected: |
---|
51 | int HaveOT; |
---|
52 | public : |
---|
53 | // fd_set master_set,master_write_set,read_set,exception_set,write_set; |
---|
54 | |
---|
55 | ot_tcpip_protocol(); |
---|
56 | net_address *get_local_address(); |
---|
57 | net_address *get_node_address(char *&server_name, int def_port, int force_port); |
---|
58 | net_socket *connect_to_server(net_address *addr, |
---|
59 | net_socket::socket_type sock_type=net_socket::SOCKET_SECURE); |
---|
60 | net_socket *create_listen_socket(int port, net_socket::socket_type sock_type, char *name); |
---|
61 | int installed() { return 1; } |
---|
62 | char *name() { return "Mac Open Transport TCPIP"; } |
---|
63 | void cleanup() { ; } |
---|
64 | int select(int block); // return # of sockets available for read & writing |
---|
65 | }; |
---|
66 | |
---|
67 | extern ot_tcpip_protocol ot_tcpip; |
---|
68 | |
---|
69 | class ot_socket : public net_socket |
---|
70 | { |
---|
71 | public : |
---|
72 | // all public since callback function must be able to access |
---|
73 | EndpointRef ep; |
---|
74 | ot_ip_address def_addr; |
---|
75 | int complete; |
---|
76 | OTEventCode code; /* event code */ |
---|
77 | OTResult result; /* result */ |
---|
78 | TCall *call; /* pointer to call structure */ |
---|
79 | TBind *bindReq; /* pointer to bind request structure */ |
---|
80 | TBind *bindRet; /* pointer to bind return structure */ |
---|
81 | OSErr err; |
---|
82 | int ready; |
---|
83 | |
---|
84 | ot_socket() {} |
---|
85 | virtual int error() { return (err != noErr); } |
---|
86 | virtual int ready_to_read() { return ready; } |
---|
87 | virtual int ready_to_write() { return ready; } |
---|
88 | |
---|
89 | virtual ~ot_socket(); |
---|
90 | int get_fd() { return 0; } |
---|
91 | }; |
---|
92 | |
---|
93 | class ot_tcp_socket : public ot_socket |
---|
94 | { |
---|
95 | friend pascal void ot_tcp_EventHandler(void*, OTEventCode event, OTResult result, void* cookie); |
---|
96 | int listening; |
---|
97 | public : |
---|
98 | ot_tcp_socket(); |
---|
99 | virtual int write(void *buf, int size, net_address *addr=NULL); |
---|
100 | virtual int read(void *buf, int size, net_address **addr); |
---|
101 | virtual int listen(int port); |
---|
102 | virtual net_socket *accept(net_address *&addr); |
---|
103 | |
---|
104 | int connect(ot_ip_address addr); |
---|
105 | }; |
---|
106 | |
---|
107 | class ot_udp_socket : public ot_socket |
---|
108 | { |
---|
109 | public : |
---|
110 | ot_udp_socket() : ot_socket() {} |
---|
111 | virtual int read(void *buf, int size, net_address **addr); |
---|
112 | virtual int write(void *buf, int size, net_address *addr=NULL); |
---|
113 | virtual int listen(int port); |
---|
114 | |
---|
115 | int connect(ot_ip_address addr); |
---|
116 | }; |
---|
117 | |
---|