Changeset 4699

Show
Ignore:
Timestamp:
03/19/10 13:48:18 (4 years ago)
Author:
piers
Message:

Fixed bug when trying to send in unicast and hopefully on Win7 too. In my connect/disconnect() fix I also 'cleaned' up the sockaddr declarations (sin) - This was an error as the sin structure was being used by ::sendto() in dosend() - and relying on it being setup in ssock(). I renamed the sin member variable to sin_ (and anotheri(local_preset) so the member variables are '_' postfixed - good idea in general), and added local declarations of sin as and where needed.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • vic/branches/mpeg4/net/net-ip.cpp

    r4681 r4699  
    9797class IPNetwork : public Network { 
    9898    public: 
    99                 IPNetwork() : Network(*(new IPAddress), *(new IPAddress), *(new IPAddress)), local_preset(0) {;} 
     99                IPNetwork() : Network(*(new IPAddress), *(new IPAddress), *(new IPAddress)), local_preset_(0) {;} 
    100100        virtual int command(int argc, const char*const* argv); 
    101101        virtual void reset(); 
     
    112112        } 
    113113    protected: 
    114         struct sockaddr_in sin; 
     114        struct sockaddr_in sin_;//Sockaddr setup in ssock, used by sendto in dosend 
     115        time_t last_reset_; 
     116        int    local_preset_;// Indicates if local_ has been set on cmd line 
     117 
    115118        virtual int dorecv(u_char* buf, int len, Address &from, int fd); 
    116119        int open(const char * host, int port, int ttl); 
    117120        int close(); 
    118121        int localname(sockaddr_in*); 
    119         int local_preset;  // Indicates if local_ has been set on cmd line 
    120122        int openssock(Address & addr, u_short port, int ttl); 
    121123        int disconnect_sock(int fd); 
    122124        int openrsock(Address & g_addr, Address & s_addr_ssm, u_short port, Address & local); 
    123125        void dosend(u_char* buf, int len, int fd); 
    124         time_t last_reset_; 
    125126}; 
    126127 
     
    211212                        if (strlen(tcl.attr("ifAddr"))>1) { 
    212213                                (IPAddress&)local_ = tcl.attr("ifAddr"); 
    213                                 local_preset=1; 
     214                                local_preset_=1; 
    214215                        } 
    215216                        if (open(argv[2], port, ttl) < 0) 
     
    298299 
    299300        // Use Local interface name if already set via command line 
    300         if (local_preset) { 
     301        if (local_preset_) { 
    301302                p->sin_addr.s_addr=(IPAddress&)local_; 
    302303                debug_msg("Setting localname from cmd line:%s\n",(const char*)local_); 
     
    336337{ 
    337338        int fd; 
     339        struct sockaddr_in sin; 
    338340 
    339341        u_int32_t g_addri = (IPAddress&)g_addr; 
     
    383385                        sin.sin_addr.s_addr = INADDR_ANY; 
    384386                        if (bind(fd, (struct sockaddr*)&sin, sizeof(sin)) < 0) { 
     387                                debug_msg("Error binding Recv mcast socket\n"); 
    385388                                perror("bind"); 
    386389                                exit(1); 
     
    423426 
    424427                mr.imr_multiaddr.s_addr = g_addri; 
    425                 if (local_preset) { 
     428                if (local_preset_) { 
    426429                    mr.imr_interface.s_addr = locali; 
    427430                    if (setsockopt(fd, IPPROTO_IP,IP_ADD_MEMBERSHIP,  
     
    450453                sin.sin_addr.s_addr = locali; 
    451454                if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 
     455                        debug_msg("Error binding Recv ucast socket\n"); 
    452456                        perror("bind"); 
    453             ::close(fd); 
    454             return (-1); 
     457                        ::close(fd); 
     458                        return (-1); 
    455459                } 
    456460                /* 
     
    517521#endif 
    518522 
    519         memset((char *)&sin, 0, sizeof(sin)); 
    520         sin.sin_family = AF_INET; 
    521         sin.sin_port = port; 
    522         sin.sin_addr.s_addr = INADDR_ANY; 
    523         if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 
    524           sin.sin_port = 0; 
    525           if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 
     523        memset((char *)&sin_, 0, sizeof(sin_)); 
     524        sin_.sin_family = AF_INET; 
     525        sin_.sin_port = port; 
     526        sin_.sin_addr.s_addr = INADDR_ANY; 
     527        if (bind(fd, (struct sockaddr *)&sin_, sizeof(sin_)) < 0) { 
     528          sin_.sin_port = 0; 
     529          if (bind(fd, (struct sockaddr *)&sin_, sizeof(sin_)) < 0) { 
     530              debug_msg("Error binding send socket\n"); 
    526531              perror("bind"); 
    527532              exit(1); 
     
    529534        } 
    530535 
    531         memset((char *)&sin, 0, sizeof(sin)); 
    532         sin.sin_family = AF_INET; 
    533         sin.sin_port = port; 
    534         sin.sin_addr.s_addr = addri; 
    535  
    536         /* Connect() is useful for localname() call to find the iface addr being used 
    537          * Because of a problem with OSX we disconnect this socket once localname() has  
    538          * found out the ip address of the interface */ 
    539         if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 
     536        memset((char *)&sin_, 0, sizeof(sin_)); 
     537        sin_.sin_family = AF_INET; 
     538        sin_.sin_port = port; 
     539        sin_.sin_addr.s_addr = addri; 
     540 
     541        /* Connect() is useful for localname() to find the interface addr 
     542         * being used. Also because of a problem with OSX we disconnect this 
     543         * socket once localname() has found out the ip addr of the iface */ 
     544        if (connect(fd, (struct sockaddr *)&sin_, sizeof(sin_)) < 0) { 
    540545                perror("connect"); 
    541546                exit(1); 
     
    573578                 * is specified on command line 
    574579                 */ 
    575                 if (local_preset) { 
     580                if (local_preset_) { 
    576581                        u_int32_t locali = (IPAddress&)local_; 
    577582                        if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, 
     
    604609int IPNetwork::disconnect_sock(int fd) 
    605610{ 
     611        struct sockaddr_in sin; 
     612 
    606613        memset((char *)&sin, 0, sizeof(sin)); 
    607614        sin.sin_family = AF_UNSPEC; 
     
    635642void IPNetwork::dosend(u_char* buf, int len, int fd) 
    636643{ 
    637         int cc = ::sendto(fd, (char*)buf, len, 0, (struct sockaddr *)&sin, sizeof(sin)); 
     644        int cc = ::sendto(fd, (char*)buf, len, 0, (struct sockaddr *)&sin_, sizeof(sin_)); 
    638645        if (cc < 0) { 
    639646                switch (errno) {