*** postfix-2.8.8/src/smtp/smtp_connect.c	2009-11-27 15:52:10.000000000 +0000
--- postfix-2.8.8_patch/src/smtp/smtp_connect.c	2013-07-17 07:23:15.488674981 +0000
***************
*** 682,687 ****
--- 682,699 ----
      return (session_count);
  }
  
+ typedef struct DESTINATION {
+ 	char *			dest;
+     unsigned int    pref;
+ } DESTINATION;
+ 
+ 
+ static int destination_sort_callback(const void *a, const void *b)
+ {
+     return (((DESTINATION*)a)->pref > ((DESTINATION*)b)->pref);
+ }
+ 
+ 
  /* smtp_connect_remote - establish remote connection */
  
  static void smtp_connect_remote(SMTP_STATE *state, const char *nexthop,
***************
*** 703,711 ****
       * primary destination to be a list (it could be just separators).
       */
      sites = argv_alloc(1);
!     argv_add(sites, nexthop, (char *) 0);
      if (sites->argc == 0)
! 	msg_panic("null destination: \"%s\"", nexthop);
      non_fallback_sites = sites->argc;
      if ((state->misc_flags & SMTP_MISC_FLAG_USE_LMTP) == 0)
  	argv_split_append(sites, var_fallback_relay, ", \t\r\n");
--- 715,763 ----
       * primary destination to be a list (it could be just separators).
       */
      sites = argv_alloc(1);
!     // argv_add(sites, nexthop, (char *) 0);
!     // if (sites->argc == 0)
!     //    msg_panic("null destination: \"%s\"", nexthop);
!  
!     if (nexthop == 0) {
!         msg_panic("null destination: \"%s\"", nexthop);
!     }
! 
!     ARGV 	*records = argv_split(nexthop, ", ");
!     if (msg_verbose)
!         msg_info("split nexthop: %s -- %d", nexthop, records->argc);     
!     DESTINATION * dests = mymalloc(sizeof(DESTINATION) * records->argc);
!     // sort by prefernece
!     char * cp;
!     int i = 0;
!     for (cpp = records->argv; cpp != 0 && i < records->argc; cpp++, i++) {
!         if (msg_verbose)
!             msg_info("handling split record(%d):%s", i, *cpp);
!         if ( (cp = split_at(*cpp, '#')) == 0 ) {
!             (dests+i)->pref = 0;	// default preference is highest: 0
!             (dests+i)->dest = *cpp;	//will remove #xx at split_at function
!         } else {
!             if (alldig(cp)) {
!                 (dests+i)->pref = atoi(cp);
!             } else {
!                 (dests+i)->pref = 0;
!             }
!             (dests+i)->dest = *cpp;
!         }
!         if (msg_verbose)
!             msg_info("handled split record(%s) -- pref: %d, dest: %s", *cpp, (dests+i)->pref, (dests+i)->dest);
!     }
!     qsort(dests, records->argc, sizeof(DESTINATION), destination_sort_callback);
!     for (i = 0; i < records->argc; i++) {
!         if (msg_verbose)
!             msg_info("adding site:%s", (dests+i)->dest);
!         argv_add(sites, (dests+i)->dest);
!     }
!     myfree(dests);
! 
      if (sites->argc == 0)
!         msg_panic("invalid destination: \"%s\"", nexthop);
! 
      non_fallback_sites = sites->argc;
      if ((state->misc_flags & SMTP_MISC_FLAG_USE_LMTP) == 0)
  	argv_split_append(sites, var_fallback_relay, ", \t\r\n");
