2 * Routines for socks versions 4 &5 packet dissection
3 * Copyright 2000, Jeffrey C. Foster <jfoste@woodward.com>
5 * $Id: packet-socks.c,v 1.15 2000/11/19 08:54:08 guy Exp $
7 * Ethereal - Network traffic analyzer
8 * By Gerald Combs <gerald@zing.org>
9 * Copyright 1998 Gerald Combs
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 * The Version 4 decode is based on SOCKS4.protocol and SOCKS4A.protocol.
28 * The Version 5 decoder is based upon rfc-1928
29 * The Version 5 User/Password authentication is based on rfc-1929.
31 * See http://www.socks.nec.com/socksprot.html for these and other documents
35 /* Possible enhancements -
37 * Add GSS-API authentication per rfc-1961
38 * Add CHAP authentication
39 * Decode FLAG bits per
40 * http://www.socks.nec.com/draft/draft-ietf-aft-socks-pro-v-04.txt
41 * In call_next_dissector, could load the destination address into the
42 * pi structure before calling next dissector.
43 * remove display_string or at least make it use protocol identifiers
44 * socks_hash_entry_t needs to handle V5 address type and domain names
55 #ifdef HAVE_SYS_TYPES_H
56 # include <sys/types.h>
59 #ifdef HAVE_NETINET_IN_H
60 # include <netinet/in.h>
67 #ifdef NEED_SNPRINTF_H
68 # include "snprintf.h"
74 #include "alignment.h"
75 #include "conversation.h"
77 #include "packet-tcp.h"
78 #include "packet-udp.h"
82 #define CHECK_PACKET_LENGTH(X) if (!BYTES_ARE_IN_FRAME(offset, X)){ \
83 proto_tree_add_text(tree, NullTVB, offset, 0, "*** FRAME TOO SHORT ***"); \
86 #define compare_packet(X) (X == (fd->num))
87 #define get_packet_ptr (fd->num)
88 #define row_pointer_type guint32
90 #define TCP_PORT_SOCKS 1080
93 /**************** Socks commands ******************/
95 #define CONNECT_COMMAND 1
96 #define BIND_COMMAND 2
97 #define UDP_ASSOCIATE_COMMAND 3
98 #define PING_COMMAND 0x80
99 #define TRACERT_COMMAND 0x81
102 /********** V5 Authentication methods *************/
104 #define NO_AUTHENTICATION 0
105 #define GSS_API_AUTHENTICATION 1
106 #define USER_NAME_AUTHENTICATION 2
107 #define CHAP_AUTHENTICATION 3
108 #define AUTHENTICATION_FAILED 0xff
111 /*********** Header field identifiers *************/
113 static int proto_socks = -1;
115 static int ett_socks = -1;
116 static int ett_socks_auth = -1;
117 static int ett_socks_name = -1;
119 static int hf_socks_ver = -1;
120 static int hf_socks_ip_dst = -1;
121 static int hf_socks_ip6_dst = -1;
122 static int hf_user_name = -1;
123 static int hf_socks_dstport = -1;
124 static int hf_socks_command = -1;
127 /************* State Machine names ***********/
152 gint32 udp_remote_port;
155 row_pointer_type v4_name_row;
156 row_pointer_type v4_user_name_row;
157 row_pointer_type connect_row;
158 row_pointer_type cmd_reply_row;
159 row_pointer_type bind_reply_row;
160 row_pointer_type command_row;
161 row_pointer_type auth_method_row;
162 row_pointer_type user_name_auth_row;
163 guint32 start_done_row;
165 guint32 dst_addr; /* this needs to handle IPv6 */
171 static char *address_type_table[] = {
181 /* String table for the V4 reply status messages */
183 static char *reply_table_v4[] = {
185 "Rejected or Failed",
186 "Rejected because SOCKS server cannot connect to identd on the client",
187 "Rejected because the client program and identd report different user-ids",
192 /* String table for the V5 reply status messages */
194 static char *reply_table_v5[] = {
196 "General SOCKS server failure",
197 "Connection not allowed by ruleset",
198 "Network unreachable",
200 "Connection refused",
202 "Command not supported",
203 "Address type not supported",
208 #define socks_hash_init_count 20
209 #define socks_hash_val_length (sizeof(socks_hash_entry_t))
211 static GMemChunk *socks_vals = NULL;
214 /************************* Support routines ***************************/
217 static int display_string( const u_char *pd, int offset, frame_data *fd,
218 proto_tree *tree, char *label){
220 /* display a string with a length, characters encoding */
221 /* they are displayed under a tree with the name in Label variable */
222 /* return the length of the string and the length byte */
225 proto_tree *name_tree;
230 int length = GBYTE( pd, offset);
232 if (!BYTES_ARE_IN_FRAME(offset, 8)){
233 proto_tree_add_text(tree, NullTVB, offset, 0, "*** FRAME TOO SHORT ***");
237 strncpy( temp, &pd[ offset + 1], length);
240 ti = proto_tree_add_text(tree, NullTVB, offset, length + 1,
241 "%s: %s" , label, temp);
244 name_tree = proto_item_add_subtree(ti, ett_socks_name);
246 proto_tree_add_text( name_tree, NullTVB, offset, 1, "Length: %d", length);
250 proto_tree_add_text( name_tree, NullTVB, offset, length, "String: %s", temp);
257 static char *get_auth_method_name( guint Number){
259 /* return the name of the authenication method */
261 if ( Number == 0) return "No authentication";
262 if ( Number == 1) return "GSSAPI";
263 if ( Number == 2) return "Username/Password";
264 if ( Number == 3) return "Chap";
265 if (( Number >= 4) && ( Number <= 0x7f))return "IANA assigned";
266 if (( Number >= 0x80) && ( Number <= 0xfe)) return "private method";
267 if ( Number == 0xff) return "no acceptable method";
269 /* shouldn't reach here */
271 return "Bad method number (not 0-0xff)";
275 static char *get_command_name( guint Number){
277 /* return the name of the command as a string */
279 if ( Number == 0) return "Unknow";
280 if ( Number == 1) return "Connect";
281 if ( Number == 2) return "Bind";
282 if ( Number == 3) return "UdpAssociate";
283 if ( Number == 0x80) return "Ping";
284 if ( Number == 0x81) return "Traceroute";
289 static int display_address( const u_char *pd, int offset,
290 frame_data *fd, proto_tree *tree) {
292 /* decode and display the v5 address, return offset of next byte */
294 int a_type = GBYTE( pd, offset);
296 proto_tree_add_text( tree, NullTVB, offset, 1,
297 "Address Type: %d (%s)", a_type,
298 address_type_table[ MAX( 0, MIN( a_type,
299 array_length( address_type_table)-1))]);
303 if ( a_type == 1){ /* IPv4 address */
304 if (!BYTES_ARE_IN_FRAME(offset, 4))
305 proto_tree_add_text(tree, NullTVB, offset, 0, "*** FRAME TOO SHORT ***");
307 proto_tree_add_ipv4( tree, hf_socks_ip_dst, NullTVB, offset,
308 4, GWORD( pd, offset));
311 else if ( a_type == 3){ /* domain name address */
313 offset += display_string( pd, offset, fd, tree,
316 else if ( a_type == 4){ /* IPv6 address */
317 if (!BYTES_ARE_IN_FRAME(offset, 16))
318 proto_tree_add_text(tree, NullTVB, offset, 0, "*** FRAME TOO SHORT ***");
320 proto_tree_add_ipv6( tree, hf_socks_ip6_dst, NullTVB, offset,
329 static int get_address_v5( const u_char *pd, int offset,
330 socks_hash_entry_t *hash_info) {
332 /* decode the v5 address and return offset of next byte */
333 /*$$$ this needs to handle IPV6 and domain name addresses */
336 int a_type = GBYTE( pd, offset++);
338 if ( a_type == 1){ /* IPv4 address */
341 hash_info->dst_addr = GWORD( pd, offset);
345 else if ( a_type == 4) /* IPv6 address */
348 else if ( a_type == 3) /* domain name address */
349 offset += GBYTE( pd, offset) + 1;
355 /********************* V5 UDP Associate handlers ***********************/
357 static void socks_udp_dissector( const u_char *pd, int offset, frame_data *fd,
360 /* Conversation dissector called from UDP dissector. Decode and display */
361 /* the socks header, the pass the rest of the data to the udp port */
362 /* decode routine to handle the payload. */
365 socks_hash_entry_t *hash_info;
366 conversation_t *conversation;
367 proto_tree *socks_tree;
371 conversation = find_conversation( &pi.src, &pi.dst, pi.ptype,
372 pi.srcport, pi.destport, 0);
374 g_assert( conversation); /* should always find a conversation */
376 hash_info = (socks_hash_entry_t*)conversation->data;
378 if (check_col(fd, COL_PROTOCOL))
379 col_set_str(fd, COL_PROTOCOL, "Socks");
381 if (check_col(fd, COL_INFO))
382 col_add_fstr(fd, COL_INFO, "Version: 5, UDP Associated packet");
385 ti = proto_tree_add_protocol_format( tree, proto_socks, NullTVB, offset,
386 END_OF_FRAME, "Socks" );
388 socks_tree = proto_item_add_subtree(ti, ett_socks);
390 CHECK_PACKET_LENGTH( 3);
392 proto_tree_add_text( socks_tree, NullTVB, offset, 2, "Reserved");
395 proto_tree_add_text( socks_tree, NullTVB, offset, 1, "Fragment Number: %d", GBYTE( pd,offset));
399 offset = display_address( pd, offset, fd, socks_tree);
400 hash_info->udp_remote_port = pntohs( &pd[ offset]);
402 CHECK_PACKET_LENGTH( 2);
403 proto_tree_add_uint( socks_tree, hf_socks_dstport, NullTVB,
404 offset, 2, hash_info->udp_remote_port);
408 else { /* no tree, skip past the socks header */
409 CHECK_PACKET_LENGTH( 3);
411 offset = get_address_v5( pd,offset, 0) + 2;
415 /* set pi src/dst port and call the udp sub-dissector lookup */
417 if ( pi.srcport == hash_info->port)
422 *ptr = hash_info->udp_remote_port;
424 tvb = tvb_create_from_top(0);
425 decode_udp_ports( tvb, offset, &pi, tree, pi.srcport, pi.destport);
427 *ptr = hash_info->udp_port;
432 void new_udp_conversation( socks_hash_entry_t *hash_info){
434 conversation_t *conversation = conversation_new( &pi.src, &pi.dst, PT_UDP,
435 hash_info->udp_port, hash_info->port, hash_info, 0);
437 g_assert( conversation);
439 old_conversation_set_dissector(conversation, socks_udp_dissector);
445 /**************** Protocol Tree Display routines ******************/
448 void display_socks_v4( const u_char *pd, int offset, frame_data *fd,
449 proto_tree *parent, proto_tree *tree, socks_hash_entry_t *hash_info) {
452 /* Display the protocol tree for the V5 version. This routine uses the */
453 /* stored conversation information to decide what to do with the row. */
454 /* Per packet information would have been better to do this, but we */
455 /* didn't have that when I wrote this. And I didn't expect this to get */
461 /* Display command from client */
462 if (compare_packet( hash_info->connect_row)){
464 CHECK_PACKET_LENGTH( 8);
465 proto_tree_add_text( tree, NullTVB, offset, 1,
466 "Version: %u ", hash_info->version);
468 command = GBYTE( pd, offset);
470 proto_tree_add_text( tree, NullTVB, offset, 1,
471 "Command: %u (%s)", command,
472 get_command_name( command));
476 proto_tree_add_uint( tree, hf_socks_dstport, NullTVB, offset, 2,
477 pntohs( &pd[ offset]));
480 /* Do destination address */
481 proto_tree_add_ipv4( tree, hf_socks_ip_dst, NullTVB, offset,
482 4, GWORD( pd, offset));
486 /*$$ check this, needs to do length checking */
487 /* display user name */
488 proto_tree_add_string( tree, hf_user_name, NullTVB, offset,
489 strlen( &pd[offset]) + 1,
493 /*Display command response from server*/
495 else if ( compare_packet( hash_info->cmd_reply_row)){
497 CHECK_PACKET_LENGTH( 8);
498 proto_tree_add_text( tree, NullTVB, offset, 1,
499 "Version: %u (should be 0) ", GBYTE( pd, offset));
501 /* Do results code */
502 proto_tree_add_text( tree, NullTVB, offset, 1,
503 "Result Code: %u (%s)", GBYTE( pd, offset) ,
504 reply_table_v4[ MAX(0, MIN( GBYTE( pd, offset) - 90, 4))]);
508 proto_tree_add_uint( tree, hf_socks_dstport, NullTVB, offset, 2,
509 pntohs( &pd[ offset]));
511 /* Do remote address */
512 proto_tree_add_ipv4( tree, hf_socks_ip_dst, NullTVB, offset, 4,
516 else if ( compare_packet( hash_info->v4_user_name_row)){
518 /*$$ check this, needs to do length checking */
519 proto_tree_add_text( tree, NullTVB, offset, strlen( &pd[offset]),
520 "User Name: %s", &pd[offset]);
526 void display_socks_v5( const u_char *pd, int offset, frame_data *fd,
527 proto_tree *parent, proto_tree *tree, socks_hash_entry_t *hash_info) {
529 /* Display the protocol tree for the version. This routine uses the */
530 /* stored conversation information to decide what to do with the row. */
531 /* Per packet information would have been better to do this, but we */
532 /* didn't have that when I wrote this. And I didn't expect this to get */
540 if (compare_packet( hash_info->connect_row)){
542 proto_tree *AuthTree;
545 CHECK_PACKET_LENGTH( 2);
547 proto_tree_add_uint( tree, hf_socks_ver, NullTVB, offset, 1,
551 temp = GBYTE( pd, offset); /* Get Auth method count */
552 /* build auth tree */
553 ti = proto_tree_add_text( tree, NullTVB, offset, 1,
554 "Client Authentication Methods");
556 AuthTree = proto_item_add_subtree(ti, ett_socks_auth);
558 proto_tree_add_text( AuthTree, NullTVB, offset, 1,
562 CHECK_PACKET_LENGTH( temp);
564 for( i = 0; i < temp; ++i) {
566 AuthMethodStr = get_auth_method_name(
567 GBYTE( pd, offset + i));
568 proto_tree_add_text( AuthTree, NullTVB, offset + i, 1,
569 "Method[%d]: %u (%s)", i,
570 GBYTE( pd, offset + i), AuthMethodStr);
573 } /* Get accepted auth method */
574 else if (compare_packet( hash_info->auth_method_row)) {
577 CHECK_PACKET_LENGTH( 1);
579 proto_tree_add_text( tree, NullTVB, offset, 1,
580 "Accepted Auth Method: 0x%0x (%s)", GBYTE( pd, offset),
581 get_auth_method_name( GBYTE( pd, offset)));
584 } /* handle user/password auth */
585 else if (compare_packet( hash_info->user_name_auth_row)) {
587 proto_tree_add_text( tree, NullTVB, offset, 1,
588 "Version: %u ", hash_info->version);
590 /* process user name */
591 offset += display_string( pd, offset, fd, tree,
593 /* process password */
594 offset += display_string( pd, offset, fd, tree,
597 /* command to the server */
598 /* command response from server */
599 else if ((compare_packet( hash_info->command_row)) ||
600 (compare_packet( hash_info->cmd_reply_row)) ||
601 (compare_packet( hash_info->bind_reply_row))){
603 proto_tree_add_text( tree, NullTVB, offset, 1,
604 "Version: %u ", hash_info->version);
606 CHECK_PACKET_LENGTH( 1);
610 command = GBYTE( pd, offset);
612 if (compare_packet( hash_info->command_row))
613 proto_tree_add_text( tree, NullTVB, offset, 1, "Command: %u (%s)",
614 command, get_command_name( command));
616 proto_tree_add_text( tree, NullTVB, offset, 1, "Status: %d (%s)",
617 GBYTE( pd, offset), reply_table_v5[ MAX( 0,
618 MIN(GBYTE( pd, offset) - 90, 9))]);
621 proto_tree_add_text( tree, NullTVB, offset, 1,
622 "Reserved: 0x%0x (should = 0x00)", GBYTE( pd, offset));
625 offset = display_address( pd, offset, fd, tree);
627 CHECK_PACKET_LENGTH( 2);
629 proto_tree_add_text( tree, NullTVB, offset, 2,
631 (compare_packet( hash_info->bind_reply_row) ?
632 "Remote Host " : ""),
633 pntohs( &pd[ offset]));
639 /**************** Decoder State Machines ******************/
642 static guint state_machine_v4( socks_hash_entry_t *hash_info, const u_char *pd,
643 int offset, frame_data *fd) {
645 /* Decode V4 protocol. This is done on the first pass through the */
646 /* list. Based upon the current state, decode the packet and determine */
647 /* what the next state should be. If we had per packet information, */
648 /* this would be the place to load them up. */
650 if ( hash_info->state == None) { /* new connection */
652 if (check_col(fd, COL_INFO))
653 col_append_str(fd, COL_INFO, " Connect to server request");
655 hash_info->state = Connecting; /* change state */
657 hash_info->command = GBYTE( pd, offset + 1);
658 /* get remote port */
659 if ( hash_info->command == CONNECT_COMMAND)
660 hash_info->port = pntohs( &pd[ offset + 2]);
661 /* get remote address */
662 hash_info->dst_addr = GWORD( pd, offset + 4);
664 /* save the packet pointer */
665 hash_info->connect_row = get_packet_ptr;
667 /* skip past this stuff */
668 hash_info->connect_offset = offset + 8;
672 if ( offset == pi.len) /* if no user name */
674 hash_info->state = V4UserNameWait;
677 hash_info->connect_offset += strlen( &pd[ offset]) + 1;
679 if ( !hash_info->dst_addr){ /* if no dest address */
681 if ( hash_info->connect_offset < pi.len ) {
682 /*$$$ copy remote name here ??? */
683 hash_info->state = Connecting;
686 hash_info->state = V4NameWait;
688 /* waiting for V4 user name */
689 }else if ( hash_info->state == V4UserNameWait){
691 if (check_col(fd, COL_INFO))
692 col_append_str(fd, COL_INFO, " Connect Request (User name)");
694 hash_info->v4_user_name_row = get_packet_ptr;
695 /*$$$ may need to check for domain name here */
696 hash_info->state = Connecting;
698 /* waiting for V4 domain name */
699 else if ( hash_info->state == V4NameWait){
701 hash_info->v4_name_row = get_packet_ptr;
702 hash_info->state = Connecting;
705 else if ( hash_info->state == Connecting){
707 if (check_col(fd, COL_INFO))
708 col_append_str(fd, COL_INFO, " Connect Response");
710 /* save packet pointer */
711 hash_info->cmd_reply_row = get_packet_ptr;
712 hash_info->state = Done; /* change state */
721 static void state_machine_v5( socks_hash_entry_t *hash_info, const u_char *pd,
722 int offset, frame_data *fd) {
724 /* Decode V5 protocol. This is done on the first pass through the */
725 /* list. Based upon the current state, decode the packet and determine */
726 /* what the next state should be. If we had per packet information, */
727 /* this would be the place to load them up. */
732 if ( hash_info->state == None) {
734 if (check_col(fd, COL_INFO))
735 col_append_str(fd, COL_INFO, " Connect to server request");
737 hash_info->state = Connecting; /* change state */
738 hash_info->connect_row = get_packet_ptr;
740 if (!BYTES_ARE_IN_FRAME(offset, 1)){
741 hash_info->state = Done; /* change state */
745 temp = GBYTE( pd, offset + 1);
746 /* skip past auth methods */
747 offset = hash_info->connect_offset = offset + 1 + temp;
749 else if ( hash_info->state == Connecting){
751 guint AuthMethod = GBYTE( pd, offset + 1);
753 if (check_col(fd, COL_INFO))
754 col_append_str(fd, COL_INFO, " Connect to server response");
756 hash_info->auth_method_row = get_packet_ptr;
758 if ( AuthMethod == NO_AUTHENTICATION)
759 hash_info->state = V5Command;
761 else if ( AuthMethod == USER_NAME_AUTHENTICATION)
762 hash_info->state = UserNameAuth;
764 else if ( AuthMethod == GSS_API_AUTHENTICATION)
765 /*$$$ should be this hash_info->state = GssApiAuth; */
766 hash_info->state = Done;
768 else hash_info->state = Done; /*Auth failed or error*/
772 else if ( hash_info->state == V5Command) { /* Handle V5 Command */
776 if (!BYTES_ARE_IN_FRAME(offset, 1)){
777 hash_info->state = Done; /* change state */
781 hash_info->command = GBYTE( pd, offset + 1); /* get command */
783 if (check_col(fd, COL_INFO))
784 col_append_fstr(fd, COL_INFO, " Command Request - %s",
785 get_command_name(hash_info->command));
787 hash_info->state = V5Reply;
788 hash_info->command_row = get_packet_ptr;
790 offset += 3; /* skip to address type */
792 offset = get_address_v5( pd, offset, hash_info);
794 if (!BYTES_ARE_IN_FRAME(offset, 1)){
795 hash_info->state = Done;
798 temp = GBYTE( pd, offset);
800 if (( hash_info->command == CONNECT_COMMAND) ||
801 ( hash_info->command == UDP_ASSOCIATE_COMMAND))
802 /* get remote port */
803 hash_info->port = pntohs( &pd[ offset]);
806 else if ( hash_info->state == V5Reply) { /* V5 Command Reply */
809 if (check_col(fd, COL_INFO))
810 col_append_fstr(fd, COL_INFO, " Command Response - %s",
811 get_command_name(hash_info->command));
813 hash_info->cmd_reply_row = get_packet_ptr;
815 if (( hash_info->command == CONNECT_COMMAND) ||
816 (hash_info->command == PING_COMMAND) ||
817 (hash_info->command == TRACERT_COMMAND))
818 hash_info->state = Done;
820 else if ( hash_info->command == BIND_COMMAND)
821 hash_info->state = V5BindReply;
823 else if ( hash_info->command == UDP_ASSOCIATE_COMMAND){
824 offset += 3; /* skip to address type */
825 offset = get_address_v5( pd, offset, hash_info);
827 /* save server udp port and create upd conversation */
828 if (!BYTES_ARE_IN_FRAME(offset, 2)){
829 hash_info->state = Done;
832 hash_info->udp_port = pntohs( &pd[ offset]);
834 new_udp_conversation( hash_info);
836 /*$$ may need else statement to handle unknows and generate error message */
840 else if ( hash_info->state == V5BindReply) { /* V5 Bind Second Reply */
842 if (check_col(fd, COL_INFO))
843 col_append_str(fd, COL_INFO, " Command Response: Bind remote host info");
845 hash_info->bind_reply_row = get_packet_ptr;
846 hash_info->state = Done;
848 else if ( hash_info->state == UserNameAuth) { /* Handle V5 User Auth*/
849 if (check_col(fd, COL_INFO))
850 col_append_str(fd, COL_INFO,
851 " User authentication response");
853 hash_info->user_name_auth_row = get_packet_ptr;
854 hash_info->state = AuthReply;
857 else if ( hash_info->state == AuthReply){ /* V5 User Auth reply */
858 hash_info->cmd_reply_row = get_packet_ptr;
859 if (check_col(fd, COL_INFO))
860 col_append_str(fd, COL_INFO, " User authentication reply");
861 hash_info->state = V5Command;
867 static void display_ping_and_tracert( const u_char *pd, int offset,
868 frame_data *fd, proto_tree *tree, socks_hash_entry_t *hash_info) {
870 /* Display the ping/trace_route conversation */
873 const u_char *data, *dataend;
874 const u_char *lineend, *eol;
877 /* handle the end command */
878 if ( pi.destport == TCP_PORT_SOCKS){
879 if (check_col(fd, COL_INFO))
880 col_append_str(fd, COL_INFO, ", Terminate Request");
883 proto_tree_add_text(tree, NullTVB, offset, 1,
884 (hash_info->command == PING_COMMAND) ?
885 "Ping: End command" :
886 "Traceroute: End command");
888 else{ /* display the PING or Traceroute results */
889 if (check_col(fd, COL_INFO))
890 col_append_str(fd, COL_INFO, ", Results");
893 proto_tree_add_text(tree, NullTVB, offset, END_OF_FRAME,
894 (hash_info->command == PING_COMMAND) ?
896 "Traceroute Results");
899 dataend = data + END_OF_FRAME;
901 while (data < dataend) {
903 lineend = find_line_end(data, dataend, &eol);
904 linelen = lineend - data;
906 proto_tree_add_text( tree, NullTVB, offset, linelen,
907 format_text(data, linelen));
917 static void call_next_dissector( const u_char *pd, int offset, frame_data *fd,
918 proto_tree *tree, socks_hash_entry_t *hash_info) {
920 /* Display the results for PING and TRACERT extensions or */
921 /* Call TCP dissector for the port that was passed during the */
922 /* connect process */
923 /* Load pointer to pi.XXXport depending upon the direction, change */
924 /* pi port to the remote port, call next dissecotr to decode the */
925 /* payload, and restore the pi port after that is done. */
930 if (( hash_info->command == PING_COMMAND) ||
931 ( hash_info->command == TRACERT_COMMAND))
933 display_ping_and_tracert( pd, offset, fd, tree, hash_info);
935 else { /* call the tcp port decoder to handle the payload */
937 /*$$$ may want to load dest address here */
939 if ( pi.destport == TCP_PORT_SOCKS)
944 *ptr = hash_info->port;
945 tvb = tvb_create_from_top(0);
946 decode_tcp_ports( tvb, offset, &pi, tree, pi.srcport, pi.destport);
947 *ptr = TCP_PORT_SOCKS;
954 dissect_socks(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
956 proto_tree *socks_tree;
958 socks_hash_entry_t *hash_info;
959 conversation_t *conversation;
961 OLD_CHECK_DISPLAY_AS_DATA(proto_socks, pd, offset, fd, tree);
963 conversation = find_conversation( &pi.src, &pi.dst, pi.ptype,
964 pi.srcport, pi.destport, 0);
966 if ( conversation) /* conversation found */
967 hash_info = conversation->data;
969 /* new conversation create local data structure */
971 hash_info = g_mem_chunk_alloc(socks_vals);
972 hash_info->start_done_row = G_MAXINT;
973 hash_info->state = None;
974 hash_info->port = -1;
975 hash_info->version = GBYTE( pd, offset); /* get version*/
977 if (( hash_info->version != 4) && /* error test version */
978 ( hash_info->version != 5))
979 hash_info->state = Done;
981 conversation_new( &pi.src, &pi.dst, pi.ptype,
982 pi.srcport, pi.destport, hash_info, 0);
985 /* display summary window information */
987 if (check_col(fd, COL_PROTOCOL))
988 col_set_str(fd, COL_PROTOCOL, "Socks");
990 if (check_col(fd, COL_INFO)){
991 if (( hash_info->version == 4) || ( hash_info->version == 5)){
992 col_add_fstr(fd, COL_INFO, "Version: %d",
995 else /* unknown version display error */
996 col_set_str(fd, COL_INFO, "Unknown");
999 if ( hash_info->command == PING_COMMAND)
1000 col_append_str(fd, COL_INFO, ", Ping Req");
1001 if ( hash_info->command == TRACERT_COMMAND)
1002 col_append_str(fd, COL_INFO, ", Traceroute Req");
1004 if ( hash_info->port != -1)
1005 col_append_fstr(fd, COL_INFO, ", Remote Port: %d",
1010 /* run state machine if needed */
1012 if ((hash_info->state != Done) && ( !fd->flags.visited)){
1014 if ( hash_info->version == 4)
1015 state_machine_v4( hash_info, pd, offset, fd);
1017 else if ( hash_info->version == 5)
1018 state_machine_v5( hash_info, pd, offset, fd);
1020 if (hash_info->state == Done) { /* if done now */
1021 hash_info->start_done_row = fd->num;
1025 /* if proto tree, decode and display */
1028 ti = proto_tree_add_item( tree, proto_socks, NullTVB, offset,
1029 END_OF_FRAME, FALSE );
1031 socks_tree = proto_item_add_subtree(ti, ett_socks);
1033 if ( hash_info->version == 4)
1034 display_socks_v4( pd, offset, fd, tree, socks_tree,
1037 else if ( hash_info->version == 5)
1038 display_socks_v5( pd, offset, fd, tree, socks_tree,
1041 /* if past startup, add the faked stuff */
1042 if ( fd->num > hash_info->start_done_row){
1043 /* add info to tree */
1044 proto_tree_add_text( socks_tree, NullTVB, offset, 0,
1045 "Command: %d (%s)", hash_info->command,
1046 get_command_name(hash_info->command));
1048 proto_tree_add_ipv4( socks_tree, hf_socks_ip_dst, NullTVB,
1049 offset, 0, hash_info->dst_addr);
1051 /* no fake address for ping & traceroute */
1053 if (( hash_info->command != PING_COMMAND) &&
1054 ( hash_info->command != TRACERT_COMMAND)){
1055 proto_tree_add_uint( socks_tree, hf_socks_dstport, NullTVB,
1056 offset, 0, hash_info->port);
1063 /* call next dissector if ready */
1065 if ( fd->num > hash_info->start_done_row){
1066 call_next_dissector( pd, offset, fd, tree, hash_info);
1072 static void socks_reinit( void){
1074 /* Do the cleanup work when a new pass through the packet list is */
1075 /* performed. Reset the highest row seen counter and re-initialize the */
1076 /* conversation memory chunks. */
1079 g_mem_chunk_destroy(socks_vals);
1081 socks_vals = g_mem_chunk_new("socks_vals", socks_hash_val_length,
1082 socks_hash_init_count * socks_hash_val_length,
1088 proto_register_socks( void){
1090 /*** Prep the socks protocol, register it and a initialization routine */
1091 /* to clear the hash stuff. */
1094 static gint *ett[] = {
1101 static hf_register_info hf[] = {
1105 { "Version", "socks.ver", FT_UINT8, BASE_NONE, NULL,
1110 { "Remote Address", "socks.dst", FT_IPv4, BASE_NONE, NULL,
1114 { &hf_socks_ip6_dst,
1115 { "Remote Address", "socks.dstV6", FT_IPv6, BASE_NONE, NULL,
1121 { "User Name", "socks.username", FT_STRING, BASE_NONE,
1125 { &hf_socks_dstport,
1126 { "Remote Port", "socks.dstport", FT_UINT16,
1127 BASE_DEC, NULL, 0x0, ""
1130 { &hf_socks_command,
1131 { "Command", "socks.command", FT_UINT16,
1132 BASE_DEC, NULL, 0x0, ""
1139 proto_socks = proto_register_protocol (
1140 "Socks Protocol", "socks");
1142 proto_register_field_array(proto_socks, hf, array_length(hf));
1143 proto_register_subtree_array(ett, array_length(ett));
1145 register_init_routine( &socks_reinit); /* register re-init routine */
1150 proto_reg_handoff_socks(void) {
1152 /* dissector install routine */
1154 old_dissector_add("tcp.port", TCP_PORT_SOCKS, dissect_socks);