Commit 36f911a446e15efa3889a580bf0c85e951d7b921
- Diff rendering mode:
- inline
- side by side
main.c
(1 / 1)
|   | |||
| 45 | 45 | ||
| 46 | 46 | req = mk_req(argv[1], argv[2]); | |
| 47 | 47 | ||
| 48 | while((status=attemptConnection(req)) != RV_SUCCESS) { | ||
| 48 | while((status=attemptConnection(&req)) != RV_SUCCESS) { | ||
| 49 | 49 | t=time(NULL); | |
| 50 | 50 | char *err="unknown"; | |
| 51 | 51 | switch(status) { |
sockets.c
(13 / 13)
|   | |||
| 29 | 29 | rv.host=host; | |
| 30 | 30 | rv.svc=svc; | |
| 31 | 31 | rv.success=0; | |
| 32 | rv.socket=-1; | ||
| 32 | 33 | ||
| 33 | 34 | return rv; | |
| 34 | 35 | } | |
| … | … | ||
| 76 | 76 | } | |
| 77 | 77 | ||
| 78 | 78 | enum returnvalues | |
| 79 | attemptConnection(requested_socket req) | ||
| 79 | attemptConnection(requested_socket *req) | ||
| 80 | 80 | { | |
| 81 | 81 | struct addrinfo hints, *res, *res0; | |
| 82 | 82 | enum returnvalues rv=ERR_ERRNO; | |
| 83 | register int s = -1; | ||
| 84 | 83 | struct linger l; | |
| 85 | 84 | int fflags =0; | |
| 86 | 85 | char *cause=NULL; | |
| 87 | 86 | int err=0; | |
| 88 | 87 | ||
| 89 | if (req.host == NULL || req.svc == NULL) { | ||
| 88 | if (req->host == NULL || req->svc == NULL) { | ||
| 90 | 89 | return (0); | |
| 91 | 90 | } | |
| 92 | 91 | ||
| 93 | 92 | memset(&hints, 0, sizeof(hints)); | |
| 94 | 93 | hints.ai_family = PF_UNSPEC; | |
| 95 | 94 | hints.ai_socktype = SOCK_STREAM; | |
| 96 | err=getaddrinfo(req.host, req.svc, &hints, &res0); | ||
| 95 | err=getaddrinfo(req->host, req->svc, &hints, &res0); | ||
| 97 | 96 | if(err != 0) { | |
| 98 | 97 | fprintf(stderr, "Error looking up %s:%s: %s\n", | |
| 99 | req.host, req.svc, gai_strerror(err)); | ||
| 98 | req->host, req->svc, gai_strerror(err)); | ||
| 100 | 99 | return(ERR_DNS); | |
| 101 | 100 | } | |
| 102 | 101 | ||
| 103 | 102 | cause="no addresses"; | |
| 104 | 103 | for (res = res0; res; res = res->ai_next) { | |
| 105 | 104 | ||
| 106 | if ((s = socket(res->ai_family, res->ai_socktype, | ||
| 105 | if ((req->socket = socket(res->ai_family, res->ai_socktype, | ||
| 107 | 106 | res->ai_protocol)) < 0) { | |
| 108 | 107 | ||
| 109 | 108 | cause="socket"; | |
| … | … | ||
| 111 | 111 | ||
| 112 | 112 | l.l_onoff = 1; | |
| 113 | 113 | l.l_linger = 60; | |
| 114 | setsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &l, sizeof(l)); | ||
| 114 | setsockopt(req->socket, SOL_SOCKET, SO_LINGER, (char *) &l, sizeof(l)); | ||
| 115 | 115 | ||
| 116 | 116 | /* Configure non-blocking IO */ | |
| 117 | fflags = fcntl(s, F_GETFL); | ||
| 118 | if(fcntl(s, F_SETFL, fflags | O_NONBLOCK) < 0) { | ||
| 117 | fflags = fcntl(req->socket, F_GETFL); | ||
| 118 | if(fcntl(req->socket, F_SETFL, fflags | O_NONBLOCK) < 0) { | ||
| 119 | 119 | perror("fcntl"); | |
| 120 | 120 | } | |
| 121 | 121 | ||
| 122 | if (connect(s, res->ai_addr, res->ai_addrlen) < 0) { | ||
| 122 | if (connect(req->socket, res->ai_addr, res->ai_addrlen) < 0) { | ||
| 123 | 123 | if(errno==EINPROGRESS) { | |
| 124 | 124 | rv = RV_SUCCESS; | |
| 125 | 125 | break; | |
| … | … | ||
| 135 | 135 | ||
| 136 | 136 | /* If we got this far, wait for data */ | |
| 137 | 137 | if(rv == RV_SUCCESS) { | |
| 138 | rv=waitForConnect(s); | ||
| 138 | rv=waitForConnect(req->socket); | ||
| 139 | 139 | } else { | |
| 140 | 140 | perror(cause); | |
| 141 | 141 | } | |
| 142 | 142 | ||
| 143 | if(s>=0) { | ||
| 144 | close(s); | ||
| 143 | if(req->socket>=0) { | ||
| 144 | close(req->socket); | ||
| 145 | 145 | } | |
| 146 | 146 | ||
| 147 | 147 | return rv; |
waitforsocket.h
(2 / 1)
|   | |||
| 5 | 5 | typedef struct { | |
| 6 | 6 | char *host; | |
| 7 | 7 | char *svc; | |
| 8 | int socket; | ||
| 8 | 9 | int success; | |
| 9 | 10 | } requested_socket; | |
| 10 | 11 | ||
| 11 | 12 | enum returnvalues { ERR_DNS=-3, ERR_TIMEOUT=-2, ERR_ERRNO=-1, RV_SUCCESS=0 }; | |
| 12 | 13 | ||
| 13 | enum returnvalues attemptConnection(requested_socket); | ||
| 14 | enum returnvalues attemptConnection(requested_socket*); | ||
| 14 | 15 | ||
| 15 | 16 | requested_socket mk_req(char *host, char *svc); |

