File: talkd.py

package info (click to toggle)
xtalk 1.3-10
  • links: PTS
  • area: main
  • in suites: woody
  • size: 112 kB
  • ctags: 139
  • sloc: python: 759; makefile: 54; sh: 22
file content (193 lines) | stat: -rw-r--r-- 5,524 bytes parent folder | download | duplicates (6)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
######################################################################
#     XTalk - A BSD talk client written in Python.
#     (C) Adam P. Jenkins <adampjenkins@yahoo.com>
#     
#     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., 675 Mass Ave, Cambridge, MA 02139, USA.
#
######################################################################

# Implements the C structures for talking to a talk daemon

import struct, re, socket, string

# This modules defines classes necessary to communicate with a talk
# daemon.  The talk daemon requires communication via binary data
# structures, so this module provides an interface.  There are three
# classes defined, which correspond to C structures.  Each of them has
# a method, toCStruct(), which returns a string containing the proper
# binary representation of the structure, and a method,
# fromCStruct(str), which takes such a binary string, and initializes
# itself from the binary string.  Each class also has a "size"
# attribute which returns its size in bytes.
# The classes are:
# 
# sockaddr_in:
#   family   # one of the family constants from socket module
#   port     # a port number
#   address  # an address in cannonical or number-dot format
#
# CTL_MSG:
#
# CTL_RESPONSE




# The declaration for sockaddr is
# struct sockaddr {
#   unsigned short family;  /* AF_* */
#   char sa_data[14];       /* the resto of the data */
# };
#
# sizeof(sockaddr) == 16
# 
# What's the rest of the data.  Normally it's a sockaddr_in.
# struct in_addr {
#   __u32 s_addr;
# };
#
# struct sockaddr_in {
#   short family;
#   short port;
#   struct in_addr;
#   /* plus padding to fill out to the size of a sockaddr. */
# };
#

TALK_VERSION = 1

# message types
LEAVE_INVITE = 0
LOOK_UP = 1
DELETE = 2
ANNOUNCE = 3

# response types
SUCCESS = 0
NOT_HERE = 1
FAILED = 2
MACHINE_UNKNOWN = 3
PERMISSION_DENIED = 4
UNKNOWN_REQUEST = 5
BADVERSION = 6
BADADDR	= 7
BADCTLADDR = 8

def htons(n):
    return struct.pack('BB', n >> 8, n & 0xff)

def htonl(n):
    return struct.pack('!l', n)

def htonb(n):
    return struct.pack('B', n)

def ntohs(n):
    hi, low = struct.unpack('BB', n)
    return ((hi & 0xff) << 8) + (low & 0xff)

def ntohl(n):
    tmp = struct.unpack('4B', n)
    return (((tmp[0] & 0xff) << 24) +
	    ((tmp[1] & 0xff) << 16) +
	    ((tmp[2] & 0xff) << 8) +
	    (tmp[3] & 0xff))

def ntohb(n):
    tmp = struct.unpack('B', n)
    return (tmp[0] & 0xff)


class sockaddr_in:
    size = 16
    def __init__(self, family=0, port=0, addr='0.0.0.0'):
	self.family = family
	self.port = port
	self.address = addr

    # returns the equivalent of a sockaddr_in, already in network byte
    # order.
    def toCStruct(self):
	# make sure address is in numbers-dots notation
	addr = socket.gethostbyname(self.address)
	# break it up into components
	#addr = regsub.split(addr, '\.')
        addr = string.split(addr, '.')
	# and convert them to numbers
	addr = map(string.atoi, addr)

	return (htons(self.family) + htons(self.port) +
		struct.pack('4B8x', addr[0], addr[1], addr[2], addr[3]))

    def fromCStruct(self, str):
	self.family = ntohs(str[0:2])
	self.port = ntohs(str[2:4])

	addr = struct.unpack('BBBB', str[4:8])
	addr = map(lambda x: x & 0xff, addr)
	self.address = '%d.%d.%d.%d' % (addr[0], addr[1], addr[2], addr[3])

class CTL_MSG:
    size = 84
    version = TALK_VERSION
    type = 0
    idNum = 0
    addr = sockaddr_in()
    ctlAddr = sockaddr_in()
    pid = 0
    localName = ''
    remoteName = ''
    remoteTTY = ''

    def toCStruct(self):
	return (htonb(self.version) + htonb(self.type) + htons(0) +
		htonl(self.idNum) + self.addr.toCStruct() +
		self.ctlAddr.toCStruct() + htonl(self.pid) +
		self.localName + ('\0' * (12 - len(self.localName))) +
		self.remoteName + ('\0' * (12 - len(self.remoteName))) +
		self.remoteTTY + ('\0' * (16 - len(self.remoteTTY))))
			    
    def fromCStruct(self, str):
	self.version = ntohb(str[0])
	self.type = ntohb(str[1])
	self.idNum = ntohl(str[4:8])
	self.addr.fromCStruct(str[8:24])
	self.ctlAddr.fromCStruct(str[24:40])
	self.pid = ntohl(str[40:44])
	self.localName = re.sub('\0*$', '', str[44:56])
	self.remoteName = re.sub('\0*$', '', str[56:68])
	self.remoteTTY = re.sub('\0*$', '', str[68:84])
	

class CTL_RESPONSE:
    size = 24
    version = TALK_VERSION
    type = 0
    answer = 0
    idNum = 0
    addr = sockaddr_in()

    def toCStruct(self):
	return (struct.pack('bbbx', self.version, self.type,
			    self.answer) +
		htonl(self.idNum) + self.addr.toCStruct())

    def fromCStruct(self, str):
	self.version = ntohb(str[0])
	self.type = ntohb(str[1])
	self.answer = ntohb(str[2])
	self.idNum = ntohl(str[4:8])
	self.addr.fromCStruct(str[8:24])