PageRenderTime 54ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/usr.sbin/ppp/udp.c

https://gitlab.com/tlevine/DragonFlyBSD
C | 336 lines | 259 code | 47 blank | 30 comment | 59 complexity | b3634b7e776215e2e03130ae6551c47d MD5 | raw file
  1. /*-
  2. * Copyright (c) 1999 Brian Somers <brian@Awfulhak.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. * SUCH DAMAGE.
  25. *
  26. * $FreeBSD: src/usr.sbin/ppp/udp.c,v 1.10.2.4 2002/09/01 02:12:32 brian Exp $
  27. * $DragonFly: src/usr.sbin/ppp/udp.c,v 1.2 2003/06/17 04:30:01 dillon Exp $
  28. */
  29. #include <sys/types.h>
  30. #include <sys/socket.h>
  31. #include <netinet/in.h>
  32. #include <arpa/inet.h>
  33. #include <netdb.h>
  34. #include <errno.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <sysexits.h>
  39. #include <sys/stat.h>
  40. #include <sys/uio.h>
  41. #include <termios.h>
  42. #include <unistd.h>
  43. #include "layer.h"
  44. #include "defs.h"
  45. #include "mbuf.h"
  46. #include "log.h"
  47. #include "timer.h"
  48. #include "lqr.h"
  49. #include "hdlc.h"
  50. #include "throughput.h"
  51. #include "fsm.h"
  52. #include "lcp.h"
  53. #include "ccp.h"
  54. #include "link.h"
  55. #include "async.h"
  56. #include "descriptor.h"
  57. #include "physical.h"
  58. #include "main.h"
  59. #include "udp.h"
  60. #define UDP_CONNECTED 1
  61. #define UDP_UNCONNECTED 2
  62. #define UDP_MAYBEUNCONNECTED 3
  63. struct udpdevice {
  64. struct device dev; /* What struct physical knows about */
  65. struct sockaddr_in sock; /* peer address */
  66. unsigned connected : 2; /* Have we connect()d ? */
  67. };
  68. #define device2udp(d) ((d)->type == UDP_DEVICE ? (struct udpdevice *)d : NULL)
  69. unsigned
  70. udp_DeviceSize(void)
  71. {
  72. return sizeof(struct udpdevice);
  73. }
  74. static ssize_t
  75. udp_Sendto(struct physical *p, const void *v, size_t n)
  76. {
  77. struct udpdevice *dev = device2udp(p->handler);
  78. int ret;
  79. switch (dev->connected) {
  80. case UDP_CONNECTED:
  81. ret = write(p->fd, v, n);
  82. break;
  83. case UDP_UNCONNECTED:
  84. default:
  85. ret = sendto(p->fd, v, n, 0, (struct sockaddr *)&dev->sock,
  86. sizeof dev->sock);
  87. break;
  88. }
  89. if (dev->connected == UDP_MAYBEUNCONNECTED) {
  90. if (ret == -1 && errno == EISCONN) {
  91. dev->connected = UDP_CONNECTED;
  92. ret = write(p->fd, v, n);
  93. } else
  94. dev->connected = UDP_UNCONNECTED;
  95. }
  96. return ret;
  97. }
  98. static ssize_t
  99. udp_Recvfrom(struct physical *p, void *v, size_t n)
  100. {
  101. struct udpdevice *dev = device2udp(p->handler);
  102. int sz, ret;
  103. if (dev->connected == UDP_CONNECTED)
  104. return read(p->fd, v, n);
  105. sz = sizeof dev->sock;
  106. ret = recvfrom(p->fd, v, n, 0, (struct sockaddr *)&dev->sock, &sz);
  107. if (*p->name.full == '\0') {
  108. snprintf(p->name.full, sizeof p->name.full, "%s:%d/udp",
  109. inet_ntoa(dev->sock.sin_addr), ntohs(dev->sock.sin_port));
  110. p->name.base = p->name.full;
  111. }
  112. return ret;
  113. }
  114. static void
  115. udp_Free(struct physical *p)
  116. {
  117. struct udpdevice *dev = device2udp(p->handler);
  118. free(dev);
  119. }
  120. static void
  121. udp_device2iov(struct device *d, struct iovec *iov, int *niov,
  122. int maxiov __unused, int *auxfd __unused, int *nauxfd __unused)
  123. {
  124. int sz = physical_MaxDeviceSize();
  125. iov[*niov].iov_base = realloc(d, sz);
  126. if (iov[*niov].iov_base == NULL) {
  127. log_Printf(LogALERT, "Failed to allocate memory: %d\n", sz);
  128. AbortProgram(EX_OSERR);
  129. }
  130. iov[*niov].iov_len = sz;
  131. (*niov)++;
  132. }
  133. static const struct device baseudpdevice = {
  134. UDP_DEVICE,
  135. "udp",
  136. 0,
  137. { CD_NOTREQUIRED, 0 },
  138. NULL,
  139. NULL,
  140. NULL,
  141. NULL,
  142. NULL,
  143. NULL,
  144. NULL,
  145. udp_Free,
  146. udp_Recvfrom,
  147. udp_Sendto,
  148. udp_device2iov,
  149. NULL,
  150. NULL,
  151. NULL
  152. };
  153. struct device *
  154. udp_iov2device(int type, struct physical *p, struct iovec *iov, int *niov,
  155. int maxiov __unused, int *auxfd __unused, int *nauxfd __unused)
  156. {
  157. if (type == UDP_DEVICE) {
  158. struct udpdevice *dev = (struct udpdevice *)iov[(*niov)++].iov_base;
  159. dev = realloc(dev, sizeof *dev); /* Reduce to the correct size */
  160. if (dev == NULL) {
  161. log_Printf(LogALERT, "Failed to allocate memory: %d\n",
  162. (int)(sizeof *dev));
  163. AbortProgram(EX_OSERR);
  164. }
  165. /* Refresh function pointers etc */
  166. memcpy(&dev->dev, &baseudpdevice, sizeof dev->dev);
  167. physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNC);
  168. return &dev->dev;
  169. }
  170. return NULL;
  171. }
  172. static struct udpdevice *
  173. udp_CreateDevice(struct physical *p, char *host, char *port)
  174. {
  175. struct udpdevice *dev;
  176. struct servent *sp;
  177. if ((dev = malloc(sizeof *dev)) == NULL) {
  178. log_Printf(LogWARN, "%s: Cannot allocate a udp device: %s\n",
  179. p->link.name, strerror(errno));
  180. return NULL;
  181. }
  182. dev->sock.sin_family = AF_INET;
  183. dev->sock.sin_addr = GetIpAddr(host);
  184. if (dev->sock.sin_addr.s_addr == INADDR_NONE) {
  185. log_Printf(LogWARN, "%s: %s: unknown host\n", p->link.name, host);
  186. free(dev);
  187. return NULL;
  188. }
  189. dev->sock.sin_port = htons(atoi(port));
  190. if (dev->sock.sin_port == 0) {
  191. sp = getservbyname(port, "udp");
  192. if (sp)
  193. dev->sock.sin_port = sp->s_port;
  194. else {
  195. log_Printf(LogWARN, "%s: %s: unknown service\n", p->link.name, port);
  196. free(dev);
  197. return NULL;
  198. }
  199. }
  200. log_Printf(LogPHASE, "%s: Connecting to %s:%s/udp\n", p->link.name,
  201. host, port);
  202. p->fd = socket(PF_INET, SOCK_DGRAM, 0);
  203. if (p->fd >= 0) {
  204. log_Printf(LogDEBUG, "%s: Opened udp socket %s\n", p->link.name,
  205. p->name.full);
  206. if (connect(p->fd, (struct sockaddr *)&dev->sock, sizeof dev->sock) == 0) {
  207. dev->connected = UDP_CONNECTED;
  208. return dev;
  209. } else
  210. log_Printf(LogWARN, "%s: connect: %s\n", p->name.full, strerror(errno));
  211. } else
  212. log_Printf(LogWARN, "%s: socket: %s\n", p->name.full, strerror(errno));
  213. close(p->fd);
  214. p->fd = -1;
  215. free(dev);
  216. return NULL;
  217. }
  218. struct device *
  219. udp_Create(struct physical *p)
  220. {
  221. char *cp, *host, *port, *svc;
  222. struct udpdevice *dev;
  223. dev = NULL;
  224. if (p->fd < 0) {
  225. if ((cp = strchr(p->name.full, ':')) != NULL && !strchr(cp + 1, ':')) {
  226. *cp = '\0';
  227. host = p->name.full;
  228. port = cp + 1;
  229. svc = strchr(port, '/');
  230. if (svc && strcasecmp(svc, "/udp")) {
  231. *cp = ':';
  232. return NULL;
  233. }
  234. if (svc) {
  235. p->fd--; /* We own the device but maybe can't use it - change fd */
  236. *svc = '\0';
  237. }
  238. if (*host && *port)
  239. dev = udp_CreateDevice(p, host, port);
  240. *cp = ':';
  241. if (svc)
  242. *svc = '/';
  243. }
  244. } else {
  245. /* See if we're a connected udp socket */
  246. struct stat st;
  247. if (fstat(p->fd, &st) != -1 && (st.st_mode & S_IFSOCK)) {
  248. int type, sz;
  249. sz = sizeof type;
  250. if (getsockopt(p->fd, SOL_SOCKET, SO_TYPE, &type, &sz) == -1) {
  251. log_Printf(LogPHASE, "%s: Link is a closed socket !\n", p->link.name);
  252. close(p->fd);
  253. p->fd = -1;
  254. return NULL;
  255. }
  256. if (sz == sizeof type && type == SOCK_DGRAM) {
  257. struct sockaddr_in sock;
  258. struct sockaddr *sockp = (struct sockaddr *)&sock;
  259. if ((dev = malloc(sizeof *dev)) == NULL) {
  260. log_Printf(LogWARN, "%s: Cannot allocate a udp device: %s\n",
  261. p->link.name, strerror(errno));
  262. return NULL;
  263. }
  264. if (getpeername(p->fd, sockp, &sz) == 0) {
  265. log_Printf(LogPHASE, "%s: Link is a connected udp socket\n",
  266. p->link.name);
  267. dev->connected = UDP_CONNECTED;
  268. } else {
  269. log_Printf(LogPHASE, "%s: Link is a disconnected udp socket\n",
  270. p->link.name);
  271. dev->connected = UDP_MAYBEUNCONNECTED;
  272. if (p->link.lcp.cfg.openmode != OPEN_PASSIVE) {
  273. log_Printf(LogPHASE, "%s: Changing openmode to PASSIVE\n",
  274. p->link.name);
  275. p->link.lcp.cfg.openmode = OPEN_PASSIVE;
  276. }
  277. }
  278. }
  279. }
  280. }
  281. if (dev) {
  282. memcpy(&dev->dev, &baseudpdevice, sizeof dev->dev);
  283. physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNC);
  284. if (p->cfg.cd.necessity != CD_DEFAULT)
  285. log_Printf(LogWARN, "Carrier settings ignored\n");
  286. return &dev->dev;
  287. }
  288. return NULL;
  289. }