]> rtime.felk.cvut.cz Git - frescor/frsh-forb.git/commitdiff
forb: proto_inet uses internally sockaddr_in instead of its own type
authorMichal Sojka <sojkam1@fel.cvut.cz>
Sun, 13 Mar 2011 22:31:01 +0000 (23:31 +0100)
committerMichal Sojka <sojkam1@fel.cvut.cz>
Sun, 13 Mar 2011 22:31:01 +0000 (23:31 +0100)
This is to simplify connecting to known peers in followup patches.

src/forb/src/proto_inet.c
src/forb/src/tests/test_proto_inet.c

index e1771d11610c1b290acda68888a1842a1ce080ea..cae62dbbd7cc2234e67f5e450e3258867b2faca7 100644 (file)
@@ -83,17 +83,14 @@ extern UL_LOG_CUST(ulogd_forb_proto_inet);
 
 /** Address used by inet protocol. All values are stored in network
  * byte order. */
-struct inet_addr {
-       struct in_addr addr;
-       uint16_t port;          /**< TCP listening port */
-};
+typedef struct sockaddr_in inet_addr_t;
 
 /** INET protocol data for ports. */
 struct inet_port {
        int udp_socket;         /**< Socket for sending and receiving broadcasts */
        int listen_socket;      /*  */
        int epoll_fd;           /**< File descriptor used by epoll() in inet_receive(). */
-       struct inet_addr addr;  /**< Address of this port */
+       inet_addr_t addr;       /**< Address of this port */
        int last_recv_fd;       /**< Used in inet_recv() to read data longer than receiving buffer */
        struct in_addr multicast_addr;
        ul_list_head_t new_peers; /**< List of just connected peers which did not send any data yet. */
@@ -117,9 +114,9 @@ struct inet_peer {
 static CORBA_boolean
 inet_serialize_addr(FORB_CDR_Codec *codec, const void *addr)
 {
-       const struct inet_addr *a = addr;
-       CORBA_unsigned_long haddr = ntohl(a->addr.s_addr);
-       CORBA_unsigned_short hport = ntohs(a->port);
+       const inet_addr_t *a = addr;
+       CORBA_unsigned_long haddr = ntohl(a->sin_addr.s_addr);
+       CORBA_unsigned_short hport = ntohs(a->sin_port);
        CORBA_boolean ret;
        ret = CORBA_unsigned_long_serialize(codec, &haddr);
        if (!ret)
@@ -130,7 +127,7 @@ inet_serialize_addr(FORB_CDR_Codec *codec, const void *addr)
 static CORBA_boolean
 inet_deserialize_addr(FORB_CDR_Codec *codec, void **addr)
 {
-       struct inet_addr *a;
+       inet_addr_t *a;
        CORBA_unsigned_long s_addr;
        CORBA_unsigned_short port;
        CORBA_boolean ret;
@@ -142,8 +139,9 @@ inet_deserialize_addr(FORB_CDR_Codec *codec, void **addr)
        if (!ret)
                return ret;
        ret = CORBA_unsigned_short_deserialize(codec, &port);
-       a->addr.s_addr = htonl(s_addr);
-       a->port = htons(port);
+       a->sin_family = AF_INET;
+       a->sin_addr.s_addr = htonl(s_addr);
+       a->sin_port = htons(port);
        *addr = a;
        return ret;
 }
@@ -170,11 +168,10 @@ static int
 inet_connect(forb_peer_t *peer)
 {
        struct inet_peer *ipeer;
-       struct sockaddr_in sa;
-       struct inet_addr *addr = peer->addr;
+       inet_addr_t *sa = peer->addr;
        int ret;
 
-       if (!addr) {
+       if (!sa) {
                ul_logerr("No address to connect\n");
                goto err;
        }
@@ -186,11 +183,8 @@ inet_connect(forb_peer_t *peer)
                ul_logerr("socket(): %s\n", strerror(errno));
                goto err_free;
        }
-       sa.sin_family = AF_INET;
-       sa.sin_port = addr->port;
-       sa.sin_addr = addr->addr;
-       ul_logtrash("connect to %s:%u\n", inet_ntoa(sa.sin_addr), ntohs(sa.sin_port));
-       ret = connect(ipeer->socket, (struct sockaddr*)&sa, sizeof(sa));
+       ul_logtrash("connect to %s:%u\n", inet_ntoa(sa->sin_addr), ntohs(sa->sin_port));
+       ret = connect(ipeer->socket, (struct sockaddr*)sa, sizeof(*sa));
        if (ret) {
                ul_logerr("connect error: %s\n", strerror(errno));
                goto err_close;
@@ -462,10 +456,10 @@ inet_peer_destroy(forb_peer_t *peer)
 
 size_t inet_addr2str(char *dest, size_t maxlen, const void *addr)
 {
-       const struct inet_addr *a = addr;
+       const inet_addr_t *a = addr;
        size_t ret = 0;
        if (addr) {
-               snprintf(dest, maxlen, "%s:%d", inet_ntoa(a->addr), ntohs(a->port));
+               snprintf(dest, maxlen, "%s:%d", inet_ntoa(a->sin_addr), ntohs(a->sin_port));
        }
        return ret;
 }
@@ -477,15 +471,16 @@ size_t inet_addr2str(char *dest, size_t maxlen, const void *addr)
 
 static void inet_register_cb(forb_port_t *port)
 {
-       struct inet_addr *ia;
+       inet_addr_t *ia;
        
        ia = malloc(sizeof(*ia));
        if (!ia) return;
 
        char *fcb_addr = getenv("FCB_ADDR");
        if (!fcb_addr) fcb_addr = "127.0.0.1";
-       ia->addr.s_addr = inet_addr(fcb_addr);
-       ia->port = htons(FCB_TCP_PORT);
+       ia->sin_family = AF_INET;
+       ia->sin_addr.s_addr = inet_addr(fcb_addr);
+       ia->sin_port = htons(FCB_TCP_PORT);
        forb_new_peer_discovered(port, NULL, FCB_SERVER_ID, ia, "");
 }
 #else
@@ -658,13 +653,14 @@ forb_inet_port_init(struct forb_port_desc *port_desc, struct in_addr listen_on,
                goto err_close_listen;
        }
 
-       port_priv->addr.port = addr.sin_port;
+       port_priv->addr.sin_family = AF_INET;
+       port_priv->addr.sin_port = addr.sin_port;
        if (listen_on.s_addr == INADDR_ANY) {
-               if (get_local_address(port_priv->listen_socket, &port_priv->addr.addr)) {
+               if (get_local_address(port_priv->listen_socket, &port_priv->addr.sin_addr)) {
                        goto err_close_listen;
                }
        } else
-               port_priv->addr.addr = listen_on;
+               port_priv->addr.sin_addr = listen_on;
 
        /* Initialize epoll descriptor */
        port_priv->epoll_fd = epoll_create(10);
index 2b7cbd22bedeb798d7fa846e794cbc3170924be0..2f31192d7e12dc84fe8cd6ad1ae4f1d32677b4ab 100644 (file)
@@ -70,7 +70,7 @@ void timeout(int signal)
 WVTEST_MAIN("AF_INET inter-orb protocol")
 {
        int len;
-       struct inet_addr addr[NUM_PORTS];
+       inet_addr_t addr[NUM_PORTS];
        forb_port_t port[NUM_PORTS];
        int i, ret;
 
@@ -79,8 +79,8 @@ WVTEST_MAIN("AF_INET inter-orb protocol")
        memset(port, 0, sizeof(port));
 
        for (i=0; i<NUM_PORTS; i++) {
-               addr[i].addr.s_addr = htonl(0x7f000001+i);
-               if (!WVPASSEQ(forb_inet_port_init(&port[i].desc, addr[i].addr, 0), 0))
+               addr[i].sin_addr.s_addr = htonl(0x7f000001+i);
+               if (!WVPASSEQ(forb_inet_port_init(&port[i].desc, addr[i].sin_addr, 0), 0))
                        error(1, errno, "forb_inet_port_init(%d)", i);
        }