[BACK]Return to mysocket.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / ox_toolkit

Annotation of OpenXM/src/ox_toolkit/mysocket.c, Revision 1.9

1.1       ohara       1: /* -*- mode: C; coding: euc-japan -*- */
1.9     ! ohara       2: /* $OpenXM: OpenXM/src/ox_toolkit/mysocket.c,v 1.8 2003/01/13 12:03:12 ohara Exp $ */
1.5       ohara       3: /*
1.3       ohara       4: Q: How to get a local port number?
                      5: A: You do setsockopt() to set options and do socket(), bind().
                      6: An OS set a local port for you.
                      7: In order to get the local port, you need to do getsockname().
                      8: (See [1] pp. 91, pp. 187 for detail)
                      9:
                     10: Reference
1.5       ohara      11: [1] W. Richard Stevens, "UNIX Network Programming", 2nd ed. Vol. 1 (Japanese version)
1.1       ohara      12: */
                     13:
                     14: #include <stdio.h>
                     15: #include <stdlib.h>
1.9     ! ohara      16: #include <errno.h>
        !            17: #include <string.h>
        !            18: #include <fcntl.h>
        !            19:
        !            20: #if defined(_MSC_VER) || defined(__MINGW32__)
        !            21: #include <winsock2.h>
        !            22: #else
1.1       ohara      23: #include <unistd.h>
                     24: #include <netdb.h>
                     25: #include <netinet/in.h>
                     26: #include <sys/types.h>
                     27: #include <sys/socket.h>
                     28: #if defined(__sun__)
                     29: #include <arpa/inet.h>
                     30: #endif
1.9     ! ohara      31: #endif
1.1       ohara      32:
                     33: #include "mysocket.h"
1.7       ohara      34: #include "ox_toolkit.h"
1.1       ohara      35:
1.5       ohara      36: typedef struct ox_sockopt {
                     37:     int       level;
                     38:     int       option_name;
                     39:     void*     option_value;
1.6       ohara      40:     int       option_len;
1.5       ohara      41: } ox_sockopt;
                     42:
                     43: static int getsocket(struct sockaddr_in *mp, char *host, int port)
1.1       ohara      44: {
                     45:     struct hostent *ent = gethostbyname(host);
1.5       ohara      46:
1.1       ohara      47:     memset(mp, '\0', sizeof(struct sockaddr_in));
                     48:     mp->sin_family = AF_INET;
1.5       ohara      49:     mp->sin_port = htons((short)port);
1.1       ohara      50:     memcpy((char *)&mp->sin_addr, ent->h_addr, ent->h_length);
                     51:
                     52:     return socket(AF_INET, SOCK_STREAM, 0);
                     53: }
                     54:
1.5       ohara      55: static int ox_connect(char *hostname, int port, ox_sockopt *opt)
1.1       ohara      56: {
1.5       ohara      57:     struct sockaddr_in serv;
                     58:     int s = getsocket(&serv, hostname, port);
                     59:     if (connect(s, (struct sockaddr *)&serv, sizeof(serv)) != 0) {
1.8       ohara      60:         ox_printf("ox_connect: failed. socket = %d, errno = %d\n", s, errno);
1.5       ohara      61:         return -1;
                     62:     }
                     63:     return s;
1.1       ohara      64: }
                     65:
1.5       ohara      66: static int ox_listen(char *hostname, int *port, int backlog, ox_sockopt *opt)
1.1       ohara      67: {
                     68:     struct sockaddr_in me;
1.5       ohara      69:     int s_waiting = getsocket(&me, hostname, *port);
1.1       ohara      70:
1.5       ohara      71:     setsockopt(s_waiting, opt->level, opt->option_name,
                     72:                opt->option_value, opt->option_len);
                     73:     if (bind(s_waiting, (struct sockaddr *)&me, sizeof(me)) < 0
                     74:         || listen(s_waiting, backlog) < 0) {
1.8       ohara      75:         ox_printf("ox_listen: failed.\n");
1.5       ohara      76:         return -1;
                     77:     }
                     78:     return s_waiting;
                     79: }
1.1       ohara      80:
1.5       ohara      81: static int ox_getport(int s_waiting)
                     82: {
                     83:     struct sockaddr_in me;
                     84:     int len = sizeof(me);
                     85:     if (getsockname(s_waiting, (struct sockaddr *)&me, &len) < 0) {
1.8       ohara      86:         ox_printf("ox_getport: failed.\n");
1.5       ohara      87:         return -1;
1.1       ohara      88:     }
1.5       ohara      89:     return ntohs(me.sin_port);
                     90: }
1.1       ohara      91:
1.5       ohara      92: int mysocketAccept(int s_waiting)
                     93: {
                     94:     /* return ox_accept(s_waiting); */
                     95:     return accept(s_waiting, NULL, NULL);
                     96: }
1.1       ohara      97:
1.5       ohara      98: int mysocketListen(char *hostname, int *port)
                     99: {
                    100:     int option     = 1;
                    101:     ox_sockopt opt = {SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option)};
                    102:     int s_waiting  = ox_listen(hostname, port, 1, &opt);
1.1       ohara     103:
1.5       ohara     104:     if (*port == 0) {
                    105:         *port = ox_getport(s_waiting);
                    106:     }
1.1       ohara     107:     return s_waiting;
                    108: }
                    109:
1.5       ohara     110: int mysocketOpen(char* hostname, int port)
1.1       ohara     111: {
1.5       ohara     112:     return ox_connect(hostname, port, NULL);
1.1       ohara     113: }
                    114:
                    115: #if 0
                    116: int mypipe(char *program, int fd1, int fd2)
                    117: {
                    118:     int sockfd[2];
                    119:     if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfd) < 0) {
1.8       ohara     120:         ox_printf("socketpair: fail! errno = %d\n", errno);
1.1       ohara     121:     }
                    122:     if (fork() == 0) {
1.2       ohara     123:         /* child process */
1.1       ohara     124:         close(sockfd[0]);
                    125:         dup2(sockfd[1], fd1);
                    126:         dup2(sockfd[1], fd2);
                    127:         execl(program, program, NULL);
                    128:   }
1.2       ohara     129:   /* parent process */
1.1       ohara     130:     close(sockfd[1]);
                    131:     return sockfd[0];
                    132: }
                    133: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>